:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --line: #e6e2db;
  --accent: #2b2b2b;
  --accent-ink: #ffffff;
  --radius: 10px;
  --shadow: 0 10px 40px rgba(0,0,0,0.06);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 1fr 380px;
  height: 100vh;
  height: 100dvh;
  width: 100vw;
}

/* ============ Viewer ============ */
.viewer {
  position: relative;
  background: var(--bg);
}

#canvas-wrap {
  position: absolute; inset: 0;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.viewer-hud {
  position: absolute;
  left: 28px;
  top: 24px;
  pointer-events: none;
}

.hud-title {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.hud-sub {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
}

.viewer-help {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--muted);
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(6px);
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  pointer-events: none;
  white-space: nowrap;
}

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 5;
  font-size: 14px;
  color: var(--muted);
  transition: opacity 0.4s ease;
}
.loading.hidden { opacity: 0; pointer-events: none; }

/* ============ Panel ============ */
.panel {
  background: var(--surface);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  box-shadow: var(--shadow);
}

.panel-head {
  padding: 18px 28px 16px;
  border-bottom: 1px solid var(--line);
}

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

.panel-head h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.price {
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  white-space: nowrap;
}

.tagline {
  margin: 6px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.group {
  padding: 18px 28px;
  border-bottom: 1px solid var(--line);
}

.group h2 {
  margin: 0 0 14px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

/* Swatches */
.group h2 .hint {
  margin-left: 8px;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: none;
  color: var(--muted);
  opacity: 0.8;
}

.swatches {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
}

.swatch {
  position: relative;
  aspect-ratio: 1;
  border-radius: 50%;
  border: 0;
  padding: 0;
  cursor: pointer;
  background: #ccc;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.10), 0 1px 3px rgba(0,0,0,0.12);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.swatch:hover {
  transform: translateY(-2px) scale(1.06);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.10), 0 4px 10px rgba(0,0,0,0.20);
}
.swatch:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.10), 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}
.swatch.active {
  /* white gap + dark ring so the picked finish is unmistakable */
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.10), 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}
/* Checkmark badge on the selected swatch */
.swatch.active::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6L9 17l-5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  box-shadow: 0 0 0 2px var(--surface);
}

.finish-name {
  margin-top: 16px;
  font-size: 13px;
  color: var(--ink);
  font-weight: 500;
}

.finish-desc {
  margin: 6px 0 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}

/* Info list */
.group-info { background: rgba(0,0,0,0.015); }
.info-list {
  margin: 0;
  display: grid;
  gap: 10px;
}
.info-list > div {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 12px;
  font-size: 12px;
  line-height: 1.5;
}
.info-list dt {
  color: var(--muted);
  font-weight: 500;
}
.info-list dd {
  margin: 0;
  color: var(--ink);
}

/* Range row */
.range-row {
  display: grid;
  grid-template-columns: 1fr 70px;
  align-items: center;
  gap: 12px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--line);
  border-radius: 2px;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
  transition: transform 0.1s ease;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.15); }
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

output {
  font-size: 13px;
  color: var(--ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Footer */
.panel-foot {
  padding: 16px 28px 20px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

button.ghost, button.primary {
  padding: 12px;
  font-size: 13px;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  transition: all 0.15s ease;
}
button.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
}
button.ghost:hover { border-color: var(--accent); }
button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border: 1px solid var(--accent);
}
button.primary:hover { opacity: 0.9; }

/* ============ Viewer corner tools ============ */
.viewer-tools {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  gap: 8px;
  z-index: 3;
}

.chip {
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 500;
  background: rgba(255,255,255,0.85);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  backdrop-filter: blur(8px);
  transition: all 0.15s ease;
}
.chip:hover {
  background: #fff;
  border-color: var(--accent);
}

/* Prominent AR chip */
.chip-ar {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}
.chip-ar svg { stroke: currentColor; }
.chip-ar:hover {
  background: #000;
  border-color: #000;
  color: #fff;
}

/* model-viewer is used only to launch AR. It must stay renderable (not opacity:0/
   display:none) so its WebXR DOM overlay works, so we hide it *behind* the opaque
   Three.js canvas instead. model-viewer renders on-demand, so this costs ~nothing. */
#ar-viewer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;            /* behind #canvas */
  pointer-events: none;  /* the Three.js canvas handles all interaction */
  --poster-color: transparent;
  background: transparent;
}
#canvas { position: relative; z-index: 1; }  /* opaque — covers #ar-viewer in normal view */

/* In-AR repositioning hint — only visible during a WebXR AR session (Android) */
.ar-hint { display: none; }
#ar-viewer[ar-status="session-started"] .ar-hint,
#ar-viewer[ar-status="object-placed"] .ar-hint {
  display: flex;
  justify-content: center;
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(28px + env(safe-area-inset-bottom));
  z-index: 10;
  pointer-events: none;
  animation: arHintFade 6s ease forwards;
}
.ar-hint-pill {
  max-width: 86vw;
  padding: 11px 18px;
  border-radius: 999px;
  background: rgba(20,20,20,0.82);
  color: #fff;
  font-size: 14px;
  line-height: 1.3;
  text-align: center;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}
@keyframes arHintFade {
  0%, 72% { opacity: 1; }
  100% { opacity: 0; }
}

/* AR QR dialog */
.ar-card { width: min(92vw, 360px); }
.ar-body { text-align: center; }
.ar-lead { margin: 0 0 16px; font-size: 14px; color: var(--ink); }
.ar-qr {
  display: inline-flex;
  padding: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
}
.ar-qr img { display: block; width: 200px; height: 200px; }
.ar-url {
  margin: 14px 0 6px;
  word-break: break-all;
}
.ar-url code { font-size: 12px; color: #666; }
.ar-note { margin: 10px 0 0; font-size: 12px; color: #888; }

/* ============ Cart badge ============ */
.cart-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  color: var(--ink);
  transition: all 0.15s ease;
}
.cart-btn:hover {
  border-color: var(--accent);
  background: rgba(0,0,0,0.03);
}

.cart-count {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
  transition: transform 0.2s ease;
}
.cart-count.empty { display: none; }
.cart-count.bump { transform: scale(1.25); }

.price-row {
  margin-top: 6px;
}

/* ============ Toast ============ */
.toast {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: var(--accent-ink);
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 100;
  max-width: 80vw;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ============ Dialog (modal) ============ */
.dialog {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 520px;
  width: calc(100vw - 32px);
  max-height: calc(100vh - 64px);
  max-height: calc(100dvh - 64px);
  border-radius: 14px;
  overflow: visible;
}
.dialog::backdrop {
  background: rgba(20, 20, 22, 0.45);
  backdrop-filter: blur(4px);
}

.dialog-card {
  background: var(--surface);
  border-radius: 14px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.25);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 64px);
  max-height: calc(100dvh - 64px);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
}
.dialog-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.dialog-close {
  background: transparent;
  border: none;
  font-size: 22px;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-family: inherit;
  transition: all 0.15s ease;
}
.dialog-close:hover {
  background: var(--bg);
  color: var(--ink);
}

.dialog-body {
  padding: 20px 22px;
  overflow-y: auto;
  flex: 1;
}

.dialog-foot {
  padding: 14px 22px 18px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ============ Cart items ============ */
.cart-items {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  display: grid;
  gap: 10px;
}
.cart-items:empty { display: none; }

.cart-item {
  display: grid;
  grid-template-columns: 36px 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.cart-item-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}
.cart-item-info { min-width: 0; }
.cart-item-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cart-item-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}
.cart-item-price {
  font-size: 13px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.cart-item-remove {
  background: transparent;
  border: none;
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  font-family: inherit;
  transition: all 0.15s ease;
}
.cart-item-remove:hover {
  background: var(--bg);
  color: var(--ink);
}

.cart-empty {
  padding: 24px 12px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.cart-empty.hidden { display: none; }

.cart-summary {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  color: var(--muted);
}
.cart-summary strong {
  color: var(--ink);
  font-size: 17px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ============ Inquiry form ============ */
.form-summary {
  background: var(--bg);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 12px;
  color: var(--ink);
  margin-bottom: 16px;
  line-height: 1.5;
}
.form-summary b { font-weight: 600; }
.form-summary .summary-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.form-summary .summary-row + .summary-row { margin-top: 4px; }
.form-summary .muted { color: var(--muted); }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field + .field { margin-top: 12px; }

.field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
}

.req { color: #c0392b; }

.field input,
.field textarea {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  color: var(--ink);
  background: var(--surface);
  transition: border-color 0.15s ease;
}
.field input:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.field textarea { resize: vertical; min-height: 80px; }
.field.invalid input,
.field.invalid textarea {
  border-color: #c0392b;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}
.field-row .field + .field { margin-top: 0; }

.checkbox {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-top: 14px;
  font-size: 12px;
  color: var(--ink);
  cursor: pointer;
  line-height: 1.5;
}
.checkbox input { margin-top: 2px; }

.form-success {
  padding: 28px 22px;
  text-align: center;
}
.form-success h3 {
  margin: 0 0 8px;
  font-size: 17px;
  font-weight: 600;
}
.form-success p {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
}

/* ============ Responsive ============ */
@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: 45vh 1fr;
    grid-template-rows: 45dvh 1fr;
  }
  .viewer { min-height: 0; }
  .panel {
    height: auto;
    max-height: none;
    min-height: 0;
    border-left: none;
    border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px rgba(0,0,0,0.05);
  }

  .panel-head { padding: 16px 18px 12px; }
  .panel-head h1 { font-size: 18px; }
  .price { font-size: 13px; }
  .tagline { font-size: 12px; }

  .group { padding: 14px 18px; }
  .group h2 { margin-bottom: 10px; font-size: 11px; }
  .info-list > div { grid-template-columns: 90px 1fr; gap: 10px; }

  .panel-foot {
    padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
    gap: 8px;
  }
  button.ghost, button.primary { padding: 13px 10px; font-size: 13px; }

  /* Viewer HUD compacted */
  .viewer-hud { left: 16px; top: 12px; }
  .hud-title { font-size: 20px; }
  .hud-sub { font-size: 11px; }
  .viewer-tools { top: 10px; right: 10px; gap: 6px; }
  .chip { padding: 7px 11px; font-size: 11px; }
  .viewer-help { display: none; }

  /* Forms / dialogs */
  .field-row { grid-template-columns: 1fr; }
  .dialog { width: calc(100vw - 16px); }
  .dialog-head { padding: 14px 18px; }
  .dialog-body { padding: 16px 18px; }
  .dialog-foot {
    padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 480px) {
  .app { grid-template-rows: 42vh 1fr; grid-template-rows: 42dvh 1fr; }
  .panel-foot { grid-template-columns: 1fr; }
  .dialog-foot { grid-template-columns: 1fr; }
  .cart-item {
    grid-template-columns: 32px 1fr auto;
    gap: 10px;
    padding: 10px;
  }
  .cart-item-remove { grid-column: 3; grid-row: 1; justify-self: end; }
  .cart-item-price { grid-column: 1 / -1; grid-row: 2; }
}

/* Landscape phones: keep side-by-side so the 3D view isn't tiny */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  .app {
    grid-template-columns: 1fr 280px;
    grid-template-rows: 1fr;
  }
  .panel {
    border-left: 1px solid var(--line);
    border-top: none;
    box-shadow: var(--shadow);
  }
  .viewer-help { display: none; }
}
