:root {
  --quartz: #F1F1EE;
  --panel: #FAFAF8;
  --sunken: #E7E7E2;
  --graphite: #22262A;
  --slate: #5E666D;
  --sage: #4F6B57;
  --sage-hover: #3F5847;
  --brass: #B8893B;
  --brass-text: #8A6424;
  --maple: #D9C3A0;
  --danger: #A33A2B;
  --border: rgba(34, 38, 42, 0.10);
  --border-strong: rgba(34, 38, 42, 0.18);
  --shadow-sheet: 0 10px 30px rgba(40, 60, 48, 0.10), 0 1px 2px rgba(40, 60, 48, 0.06);
  --font-head: 'Young Serif', Georgia, serif;
  --font-body: 'Instrument Sans', system-ui, -apple-system, sans-serif;
  --r-panel: 12px;
  --r-ctl: 8px;
  --ease: cubic-bezier(.2, .7, .2, 1);
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--quartz);
  color: var(--graphite);
  font-family: var(--font-body);
  font-size: 0.9375rem;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
h1, h2, h3 { font-family: var(--font-head); font-weight: 400; margin: 0; }
h2 { font-size: 1.25rem; line-height: 1.2; letter-spacing: -0.01em; }
h3 { font-size: 1.0625rem; line-height: 1.25; letter-spacing: -0.01em; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

.app {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr) 300px;
  grid-template-rows: 60px minmax(0, 1fr) 60px;
  grid-template-areas: "top top top" "cat stage insp" "est est est";
  height: 100vh;
  height: 100dvh;
}

/* top bar */
.topbar {
  grid-area: top;
  display: flex; align-items: center; gap: 20px;
  padding: 0 16px 0 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; flex: none; }
.brand-mark { width: 26px; height: 26px; color: var(--graphite); }
.brand-name { font-family: var(--font-head); font-size: 1.125rem; letter-spacing: -0.01em; }
.plan-title { display: flex; flex-direction: column; min-width: 0; flex: 1; padding-left: 20px; border-left: 1px solid var(--border); }
.plan-name {
  border: 1px solid transparent; background: transparent; border-radius: 6px;
  padding: 0 6px; margin-left: -6px; font-weight: 500; line-height: 1.4; max-width: 340px; width: 100%;
}
.plan-name:hover { border-color: var(--border); }
.plan-name:focus-visible { outline: 2px solid var(--brass); outline-offset: 1px; }
.plan-customer { font-size: 0.8125rem; color: var(--slate); line-height: 1.3; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

/* buttons */
.btn {
  text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border-radius: var(--r-ctl); padding: 9px 16px; border: 1px solid transparent;
  cursor: pointer; font-weight: 500; font-size: 0.875rem; line-height: 1.2; white-space: nowrap;
  transition: transform .15s var(--ease), opacity .15s var(--ease);
}
.btn:focus-visible, .icon-btn:focus-visible, .seg-btn:focus-visible, .cat-item:focus-visible,
.swatch:focus-visible, .chip:focus-visible, .estimate-total:focus-visible, .fab:focus-visible {
  outline: 2px solid var(--brass); outline-offset: 2px;
}
.btn:active, .icon-btn:active, .seg-btn:active, .cat-item:active, .chip:active, .swatch:active, .fab:active { transform: scale(0.97); }
.btn-primary { background: var(--sage); color: #fff; }
.btn-primary:hover { background: var(--sage-hover); transform: translateY(-1px); }
.btn-secondary { background: transparent; border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--sunken); }
.btn-ghost { background: transparent; color: var(--slate); }
.btn-ghost:hover { background: var(--sunken); color: var(--graphite); }
.btn-danger { background: transparent; color: var(--danger); border-color: rgba(163, 58, 43, .3); }
.btn-danger:hover { background: rgba(163, 58, 43, .07); }
.btn-sm { padding: 6px 12px; font-size: 0.8125rem; }
.icon-btn {
  width: 32px; height: 32px; border-radius: var(--r-ctl); border: 0; background: transparent; cursor: pointer;
  display: inline-grid; place-items: center; color: var(--slate);
}
.icon-btn:hover { background: var(--sunken); color: var(--graphite); }
.icon-btn svg { width: 18px; height: 18px; }

.seg { display: inline-flex; background: var(--sunken); border-radius: var(--r-ctl); padding: 3px; }
.seg-btn {
  border: 0; background: transparent; border-radius: 6px; padding: 5px 12px; cursor: pointer;
  font-size: 0.8125rem; font-weight: 500; color: var(--slate);
}
.seg-btn:hover { color: var(--graphite); }
.seg-btn.is-on { background: var(--panel); color: var(--graphite); box-shadow: 0 1px 2px rgba(34,38,42,.12); }

/* side panels */
.sheet { background: var(--panel); overflow-y: auto; overscroll-behavior: contain; }
.catalog { grid-area: cat; border-right: 1px solid var(--border); padding: 18px 14px 24px; }
.inspector { grid-area: insp; border-left: 1px solid var(--border); padding: 18px 18px 28px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-close { display: none; }
.hint { color: var(--slate); font-size: 0.8125rem; margin: 4px 0 14px; line-height: 1.45; }

.cat-group { margin-bottom: 16px; }
.cat-group h3 { font-family: var(--font-body); font-size: 0.8125rem; font-weight: 500; color: var(--slate); margin: 0 0 6px 4px; }
.cat-item {
  display: grid; grid-template-columns: 44px 1fr; align-items: center; gap: 10px; width: 100%;
  padding: 6px; border: 1px solid transparent; border-radius: var(--r-ctl); background: transparent;
  text-align: left; cursor: grab; touch-action: pan-y; user-select: none; -webkit-user-select: none;
}
.cat-item:hover { background: var(--quartz); border-color: var(--border); }
.cat-item:active { cursor: grabbing; }
.cat-icon { width: 44px; height: 38px; border-radius: 6px; background: var(--sunken); display: grid; place-items: center; }
.cat-icon svg { width: 34px; height: 30px; }
.cat-name { font-size: 0.875rem; font-weight: 500; line-height: 1.25; display: block; }
.cat-size { font-size: 0.8125rem; color: var(--slate); font-stretch: 75%; font-variant-numeric: tabular-nums; }

/* inspector */
.insp-section { padding: 16px 0; border-top: 1px solid var(--border); }
.insp-section:first-of-type { border-top: 0; padding-top: 6px; }
.insp-section h3 { margin-bottom: 10px; }
.insp-title { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.insp-sub { color: var(--slate); font-size: 0.8125rem; margin: 0 0 4px; }
.field-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.field-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field label, .field .lbl { font-size: 0.75rem; color: var(--slate); }
.field input[type=text], .field select {
  width: 100%; height: 34px; padding: 0 9px; border-radius: var(--r-ctl);
  border: 1px solid var(--border-strong); background: #fff;
  font-stretch: 75%; font-size: 0.9375rem; font-variant-numeric: tabular-nums;
}
.field select { font-stretch: 100%; font-size: 0.875rem; }
.field input:hover, .field select:hover { border-color: rgba(34,38,42,.32); }
.field input:focus-visible, .field select:focus-visible { outline: 2px solid var(--brass); outline-offset: 0; border-color: var(--brass); }
.field input.is-bad { border-color: var(--danger); }
.field-error { color: var(--danger); font-size: 0.75rem; margin: 6px 0 0; line-height: 1.4; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.warn { margin: 10px 0 0; padding: 8px 10px; border-radius: var(--r-ctl); background: rgba(163,58,43,.07); color: var(--danger); font-size: 0.8125rem; line-height: 1.4; }
.price-line { display: flex; justify-content: space-between; font-size: 0.875rem; margin-top: 10px; color: var(--slate); }
.price-line b { color: var(--graphite); font-weight: 500; font-stretch: 75%; font-size: 1rem; font-variant-numeric: tabular-nums; }

.opt-label { font-size: 0.8125rem; color: var(--slate); margin: 12px 0 6px; }
.opt-label:first-child { margin-top: 0; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 30px; height: 30px; border-radius: 9999px; cursor: pointer; padding: 0;
  border: 1px solid var(--border-strong); background-size: cover; background-position: center;
  transition: transform .15s var(--ease);
}
.swatch:hover { transform: scale(1.08); }
.swatch.is-on { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--brass); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border-strong); background: transparent; border-radius: 9999px;
  padding: 4px 11px; font-size: 0.8125rem; cursor: pointer; line-height: 1.4;
}
.chip:hover { background: var(--sunken); }
.chip.is-on { background: var(--graphite); color: #fff; border-color: var(--graphite); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 0.875rem; margin-right: 14px; cursor: pointer; }
.check input { accent-color: var(--sage); width: 16px; height: 16px; }
.check input:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }
.win-card { border: 1px solid var(--border); border-radius: var(--r-ctl); padding: 8px 10px 10px; margin-top: 8px; background: var(--quartz); }
.win-head { display: flex; align-items: center; justify-content: space-between; font-size: 0.8125rem; font-weight: 500; margin: -2px -4px 4px 0; }

/* stage */
.stage { grid-area: stage; position: relative; overflow: hidden; background: var(--quartz); }
.viewport { position: absolute; inset: 0; touch-action: none; }
.viewport canvas { display: block; outline: none; }
.loading {
  position: absolute; inset: 0; display: grid; place-items: center; color: var(--slate);
  font-size: 0.875rem; background: var(--quartz); transition: opacity .4s var(--ease);
}
.loading.is-done { opacity: 0; pointer-events: none; }
.stage-help {
  position: absolute; left: 16px; bottom: 12px; margin: 0; max-width: 60ch;
  font-size: 0.8125rem; color: var(--slate); background: rgba(250,250,248,.86);
  padding: 6px 10px; border-radius: var(--r-ctl); border: 1px solid var(--border);
  transition: opacity .3s var(--ease);
}
.stage-help.is-gone { opacity: 0; pointer-events: none; }
.fab { display: none; }
.show-sm { display: none; }
.short-label { display: none; }

/* labels drawn by CSS2DRenderer */
.dim-label {
  font-family: var(--font-body); font-stretch: 75%; font-size: 13px; font-weight: 500; font-variant-numeric: tabular-nums;
  color: var(--graphite); background: rgba(241,241,238,.92); padding: 0 4px; border-radius: 3px;
  line-height: 1.35; white-space: nowrap; pointer-events: none;
}
.dim-label.is-brass { color: var(--brass-text); }
.dim-label.is-room { color: var(--slate); font-size: 12px; }

/* estimate bar */
.estimate-bar {
  grid-area: est; position: relative; display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 16px 0 20px; background: var(--panel); border-top: 1px solid var(--border);
}
.estimate-summary { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.estimate-label { font-size: 0.8125rem; color: var(--slate); }
.estimate-total {
  border: 0; background: transparent; cursor: pointer; padding: 2px 6px; margin-left: -6px; border-radius: 6px;
  font-stretch: 75%; font-weight: 500; font-size: 1.375rem; font-variant-numeric: tabular-nums; line-height: 1.2;
}
.estimate-total:hover { background: var(--sunken); }
.estimate-note { font-size: 0.75rem; color: var(--slate); }
.estimate-actions { display: flex; gap: 8px; }
.estimate-panel {
  position: absolute; left: 16px; bottom: calc(100% + 8px); width: 460px; max-width: calc(100vw - 32px); z-index: 30;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--r-panel);
  box-shadow: var(--shadow-sheet); padding: 14px 16px;
}
.estimate-panel table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.estimate-panel td { padding: 5px 0; border-bottom: 1px solid var(--border); }
.estimate-panel td:last-child { text-align: right; font-stretch: 75%; font-variant-numeric: tabular-nums; font-size: 0.9375rem; }
.estimate-panel tr.total td { border-bottom: 0; font-weight: 500; padding-top: 9px; }
.estimate-panel p { margin: 10px 0 0; color: var(--slate); font-size: 0.75rem; line-height: 1.45; }
.estimate-panel a { color: var(--sage); font-weight: 500; }
.estimate-panel a:hover { color: var(--sage-hover); }
.estimate-panel a:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; border-radius: 3px; }
.est-table { margin-top: 8px; }
.est-table th { font-weight: 500; font-size: 0.75rem; color: var(--slate); text-align: right; padding: 0 0 6px; border-bottom: 1px solid var(--border-strong); }
.est-table th:first-child { text-align: left; }
.est-table td { text-align: right; font-stretch: 75%; font-variant-numeric: tabular-nums; font-size: 0.9375rem; }
.estimate-panel .est-table td:first-child { text-align: left; font-stretch: 100%; font-size: 0.875rem; }
.est-table tfoot td { border-bottom: 0; color: var(--slate); }
.est-table tfoot tr.total td { color: var(--graphite); font-weight: 500; font-size: 1rem; border-top: 1px solid var(--border-strong); }
.est-detail { display: block; font-size: 0.75rem; color: var(--slate); line-height: 1.3; }
.est-table tr.is-off td:not(:first-child) { color: var(--slate); text-decoration: line-through; }
.est-toggle { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.est-toggle input { accent-color: var(--sage); width: 15px; height: 15px; margin: 0; }
.est-toggle input:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

.toast {
  position: fixed; left: 50%; bottom: 76px; z-index: 50;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  background: var(--graphite); color: #fff; font-size: 0.875rem; padding: 8px 14px; border-radius: var(--r-ctl);
  transition: opacity .2s var(--ease), transform .2s var(--ease);
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }

/* small screens: rails become bottom sheets */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0,1fr); grid-template-rows: 56px minmax(0,1fr) 60px; grid-template-areas: "top" "stage" "est"; }
  .topbar { gap: 6px; padding: 0 6px 0 10px; }
  .brand-name, .hide-sm, .plan-customer { display: none; }
  .show-sm { display: inline-flex; }
  .brand-mark { width: 22px; height: 22px; }
  .plan-title { padding-left: 8px; min-width: 0; }
  .plan-name { max-width: none; text-overflow: ellipsis; }
  .topbar-actions { gap: 2px; }
  .topbar-actions .btn { padding: 8px; }
  .seg-btn { padding: 5px 9px; }
  .long-label { display: none; }
  .short-label { display: inline; }
  .sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; max-height: 62vh;
    border: 1px solid var(--border); border-radius: 16px 16px 0 0; box-shadow: var(--shadow-sheet);
    transform: translateY(105%); transition: transform .28s var(--ease);
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
  .sheet.is-open { transform: translateY(0); }
  .sheet-close { display: inline-grid; }
  .catalog-list { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 4px 8px; }
  .catalog-list .cat-group { display: contents; }
  .catalog-list .cat-group h3 { grid-column: 1 / -1; margin-top: 8px; }
  .fab {
    display: inline-flex; align-items: center; gap: 6px; position: absolute; right: 12px; bottom: 12px;
    background: var(--sage); color: #fff; border: 0; border-radius: 9999px; padding: 11px 18px 11px 14px;
    font-weight: 500; font-size: 0.875rem; cursor: pointer; box-shadow: 0 6px 18px rgba(40,60,48,.25);
  }
  .fab:hover { background: var(--sage-hover); }
  .fab svg { width: 18px; height: 18px; }
  .stage-help { right: 130px; font-size: 0.75rem; }
  .estimate-bar { padding: 0 10px 0 12px; gap: 8px; }
  .estimate-note { display: none; }
  .estimate-label { display: none; }
  .estimate-panel { left: 8px; right: 8px; width: auto; max-height: 70vh; overflow-y: auto; }
  .btn { padding: 9px 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}

/* ---------- dialogs and forms (shared) ---------- */
dialog.dlg {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--panel); color: var(--graphite); box-shadow: 0 24px 60px rgba(34, 48, 40, .22);
}
dialog.dlg::backdrop { background: rgba(34, 38, 42, .38); }
.dlg-form { padding: 22px 24px 20px; }
.dlg-form h2 { font-size: 1.5rem; letter-spacing: -0.015em; margin-bottom: 4px; }
.dlg-sub { color: var(--slate); margin: 0 0 16px; font-size: 0.9375rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.form-field label { font-size: 0.8125rem; color: var(--slate); }
.form-field input, .form-field textarea {
  width: 100%; border: 1px solid var(--border-strong); border-radius: var(--r-ctl); background: #fff;
  padding: 8px 10px; font-size: 0.9375rem; line-height: 1.4;
}
.form-field textarea { resize: vertical; min-height: 64px; }
.form-field input:hover, .form-field textarea:hover { border-color: rgba(34,38,42,.32); }
.form-field input:focus-visible, .form-field textarea:focus-visible { outline: 2px solid var(--brass); outline-offset: 0; border-color: var(--brass); }
.form-field input[readonly] { background: var(--quartz); color: var(--slate); }
.form-error { color: var(--danger); font-size: 0.8125rem; margin: 12px 0 0; line-height: 1.45; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.dlg-actions .spacer { flex: 1; }
.link-box { display: flex; gap: 8px; }
.link-box input { flex: 1; min-width: 0; font-size: 0.875rem; }
.choice-row { display: flex; gap: 8px; flex-wrap: wrap; }
.customer-btn {
  border: 0; background: transparent; padding: 0 4px; margin-left: -4px; border-radius: 4px; cursor: pointer;
  font-size: 0.8125rem; color: var(--slate); text-decoration: underline; text-decoration-color: rgba(94,102,109,.35); text-underline-offset: 2px;
}
.customer-btn:hover { color: var(--graphite); text-decoration-color: currentColor; }
.customer-btn:focus-visible { outline: 2px solid var(--brass); outline-offset: 1px; }
.customer-btn:active { transform: scale(0.98); }
.save-status { font-size: 0.8125rem; color: var(--slate); margin-left: 6px; }
.save-status.is-error { color: var(--danger); }
.brand-link { color: inherit; text-decoration: none; border-radius: 6px; }
.brand-link:hover .brand-name { text-decoration: underline; text-underline-offset: 3px; }
.brand-link:focus-visible { outline: 2px solid var(--brass); outline-offset: 3px; }
.app.is-view { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "stage" "est"; }
@media (max-width: 860px) { .app.is-view .plan-customer { display: none; } }
.versions { margin-top: 18px; }
.versions h3 { font-size: 1.0625rem; margin-bottom: 6px; }
.version-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--border); font-size: 0.875rem; }
.version-row .v-when { flex: 1; color: var(--slate); }
.version-row .v-total { font-stretch: 75%; font-variant-numeric: tabular-nums; font-size: 1rem; }
.versions .empty-note { color: var(--slate); font-size: 0.8125rem; margin: 0; }
.estimate-actions .save-status { align-self: center; }
