:root {
  --bg: var(--tg-theme-bg-color, #11151c);
  --surface: var(--tg-theme-secondary-bg-color, #1b222c);
  --text: var(--tg-theme-text-color, #f1f5f9);
  --muted: var(--tg-theme-hint-color, #9aa6b8);
  --surface-2: color-mix(in srgb, var(--text) 10%, var(--surface));
  --border: color-mix(in srgb, var(--text) 14%, transparent);
  --accent: var(--tg-theme-button-color, #2f81f7);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --badge-bg: color-mix(in srgb, var(--accent) 18%, transparent);
  --badge-text: var(--accent);
  --danger: #d93036;
  --danger-bg: rgba(217, 48, 54, 0.14);
  --danger-border: rgba(217, 48, 54, 0.35);
  --warn-text: #d97706;
  --success: #2ea043;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

:root[data-theme="dark"] {
  --warn-text: #f5b942;
  --danger: #ff7b7f;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 960px;
  margin: 0 auto;
  overflow: hidden;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-title {
  min-width: 0;
  flex: 1;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.item-title {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.subtitle {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--badge-bg);
  color: var(--badge-text);
  flex-shrink: 0;
}

.topbar-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.icon-btn.small {
  width: 34px;
  height: 34px;
  font-size: 15px;
}

.icon-btn:active {
  transform: scale(0.95);
}

/* 3D Viewport */
.viewport-wrap {
  position: relative;
  flex: 1 1 46%;
  min-height: 230px;
  background: radial-gradient(circle at 50% 35%, #f6f4ef 0%, #e5e1d8 100%);
  overflow: hidden;
  touch-action: none;
}

.canvas-host {
  width: 100%;
  height: 100%;
}

.canvas-host canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.overlay-topbar {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 4;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  pointer-events: none;
}

.overlay-topbar > * {
  pointer-events: auto;
}

.overlay-tools {
  display: flex;
  gap: 4px;
}

.overlay-bottom-left {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 4;
  background: rgba(17, 21, 28, 0.78);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 9px;
  border-radius: 8px;
  backdrop-filter: blur(6px);
  pointer-events: none;
}

.seg-group {
  display: inline-flex;
  background: rgba(17, 21, 28, 0.78);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
  backdrop-filter: blur(6px);
}

.seg-btn {
  border: none;
  background: transparent;
  color: #d6dde8;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 9px;
  border-radius: 8px;
  cursor: pointer;
}

.seg-btn.active {
  background: var(--accent);
  color: var(--accent-text);
}

.pill-btn {
  border: none;
  background: rgba(17, 21, 28, 0.78);
  color: #eef2f7;
  font-size: 12px;
  font-weight: 600;
  padding: 7px 9px;
  border-radius: 9px;
  cursor: pointer;
  backdrop-filter: blur(6px);
}

.pill-btn.active {
  background: rgba(47, 129, 247, 0.92);
  color: #fff;
}

.center-overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  background: rgba(17, 21, 28, 0.62);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.overlay-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
}

.overlay-card.compact {
  max-width: 280px;
  text-align: center;
  font-weight: 500;
}

.overlay-card h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.overlay-card p {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.4;
}

.missing-fields-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

/* Price bar */
.price-bar {
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  flex-shrink: 0;
}

.price-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.price-label {
  font-size: 12px;
  color: var(--muted);
}

.price-value {
  font-size: 18px;
  letter-spacing: -0.01em;
}

.price-breakdown {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.zero-price-hint {
  font-size: 11px;
  color: var(--warn-text);
  margin-top: 3px;
}

/* Editor panel */
.editor-panel {
  display: flex;
  flex-direction: column;
  flex: 1 1 44%;
  min-height: 220px;
  background: var(--bg);
  overflow: hidden;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 6px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 4px;
  border-radius: 9px;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--surface-2);
  color: var(--text);
}

.tab-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

.tab-pane {
  display: none;
}

.tab-pane.active {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-row.inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.field-row label,
.dim-head label {
  font-size: 13px;
  color: var(--muted);
  font-weight: 500;
}

.text-input,
.num-input,
.select-input {
  width: 100%;
  height: 42px;
  padding: 0 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

.num-input {
  width: 112px;
  text-align: right;
  font-weight: 600;
}

.text-input.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

.dim-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.dim-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.range-input {
  width: 100%;
  accent-color: var(--accent);
  height: 24px;
}

.toggles-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.toggle-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 11px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  cursor: pointer;
}

.toggle-chip.full {
  width: 100%;
  margin: 10px 0;
}

.toggle-chip input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.steppers-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.stepper-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.stepper-card span {
  font-size: 12px;
  color: var(--muted);
}

.stepper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.stepper button {
  width: 38px;
  height: 38px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
}

.stepper strong {
  font-size: 17px;
  min-width: 24px;
  text-align: center;
}

.select-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.editor-footer {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.primary-btn,
.secondary-btn,
.danger-btn {
  height: 44px;
  border-radius: 11px;
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  padding: 0 14px;
}

.primary-btn {
  background: var(--accent);
  color: var(--accent-text);
}

.primary-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.secondary-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.danger-btn {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger-border);
}

.full-width {
  width: 100%;
}

/* Public view info */
.public-info {
  padding: 12px 14px calc(14px + var(--safe-bottom));
  background: var(--bg);
  overflow-y: auto;
  flex-shrink: 0;
  max-height: 36%;
}

.public-specs-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.spec-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
}

.spec-pill small {
  display: block;
  font-size: 11px;
  color: var(--muted);
}

.spec-pill strong {
  font-size: 13px;
}

.public-note {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
}

.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: var(--safe-bottom);
}

.modal-card.tall {
  max-height: 82dvh;
  display: flex;
  flex-direction: column;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.modal-head h3 {
  margin: 0;
  font-size: 16px;
}

.modal-desc {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.4;
}

.share-link-box {
  display: flex;
  gap: 8px;
  margin: 10px 0;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.modal-actions .primary-btn {
  flex: 1;
}

.projects-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 6px;
}

.project-group {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
}

.project-group-title {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 6px;
}

.project-item-btn {
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  margin-top: 5px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.project-item-btn small {
  color: var(--muted);
  display: block;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(68px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 30;
  background: rgba(17, 21, 28, 0.94);
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  max-width: 90%;
  text-align: center;
}

@media (min-width: 720px) {
  .select-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .public-specs-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
