:root {
  --bg: #f7f7f8;
  --panel: #fff;
  --text: #222;
  --muted: #6b6f76;
  --line: #e2e3e6;
  --accent: #2563eb;
  --color-warn: #b45309;
  --color-warn-bg: #fff7ea;
  --color-ok: #15803d;
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  margin: 1rem;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

h1 { font-size: 1.3rem; margin: 0; }
h2 { font-size: 1rem; margin: 0; }
h3 { font-size: 0.9rem; margin: 0.25rem 0; }

button, select, input { font: inherit; }
button {
  border: 1px solid var(--line); background: #fff; border-radius: 6px;
  padding: 0.3rem 0.7rem; cursor: pointer;
}
button:hover { border-color: #b8bbc2; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: 0.45; cursor: default; }
button.icon { border: none; background: none; padding: 0 0.3rem; color: var(--muted); font-size: 1.1rem; }
input[type=number] { width: 5.5rem; }

.app-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }

.layout { display: block; }   /* stacked: cabinets full width on top, inventory below */

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 1rem; }
.panel-header { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.6rem; }
.status { color: var(--muted); font-size: 0.85rem; margin-left: auto; }

.collapsible-section .section-header { cursor: pointer; margin-bottom: 0; }
.collapsible-section:not(.collapsed) .section-header { margin-bottom: 0.6rem; }
.collapsible-section.collapsed .section-body { display: none; }
.section-toggle { margin-left: auto; transition: transform 0.15s; }
.collapsible-section:not(.collapsed) .section-toggle { transform: rotate(90deg); }

.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 0.85rem; }
.badge { display: inline-block; font-size: 0.72rem; padding: 0 0.35rem; border-radius: 4px; background: #eef0f3; color: var(--muted); margin-left: 0.3rem; }
.badge.warn { background: var(--color-warn-bg); color: var(--color-warn); }

/* wizard */
.wizard p { margin-top: 0; }
.wizard-cols { display: flex; gap: 2rem; flex-wrap: wrap; }
.count-row { display: flex; justify-content: space-between; gap: 1rem; margin: 0.2rem 0; width: 12rem; }
.count-row input { width: 4.5rem; }
.wizard-sum { margin: 0.6rem 0; font-weight: 600; color: var(--color-warn); }
.wizard-sum.ok { color: var(--color-ok); }
.wizard-actions { display: flex; gap: 0.5rem; margin-top: 0.6rem; }

/* groups */
.swatch { display: inline-block; width: 0.8rem; height: 0.8rem; border-radius: 3px; background: var(--gc, #ccc); flex: none; }
.swatch.none { background: transparent; border: 1px dashed #c4c7cc; }
.groups-bar { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; margin-bottom: 0.6rem; }
.group-chip { display: flex; align-items: center; gap: 0.35rem; border: 1px solid var(--line); border-left: 4px solid var(--gc); border-radius: 6px; padding: 0.15rem 0.4rem; }
.group-chip.drop { outline: 2px solid var(--accent); }
.group-name { cursor: text; font-weight: 600; }
.keep { font-size: 0.8rem; color: var(--muted); }

/* inventory rows */
.inv-list { display: flex; flex-direction: column; gap: 0.25rem; }
.inv-row { display: flex; align-items: center; gap: 0.4rem; padding: 0.2rem 0.3rem; border-radius: 6px; border: 1px solid transparent; cursor: grab; }
.inv-row:hover { background: #fafafb; border-color: var(--line); }
.inv-row.drop { border-color: var(--accent); background: #eef3ff; }
.inv-row.dragging { opacity: 0.4; }
.inv-row.placeholder { background: repeating-linear-gradient(135deg, #fff, #fff 6px, #f4f5f7 6px, #f4f5f7 12px); }
.label-input { flex: 1; min-width: 6rem; }
.gap-input { width: 4.5rem !important; }
.ph { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.add-bar { display: flex; gap: 0.4rem; margin-top: 0.75rem; }

/* height + alerts */
.height-control { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.6rem; }
.height-control input[type=range] { flex: 1; }
#height-readout { min-width: 11rem; text-align: right; font-variant-numeric: tabular-nums; }
.alert { padding: 0.2rem 0; }
.alert.current strong { color: var(--accent); }
.report { padding: 0.4rem 0.6rem; border-radius: 6px; background: #f3f5f8; margin: 0.4rem 0; }
.report.warn { background: var(--color-warn-bg); color: var(--color-warn); }

/* cabinets */
.summary { margin: 0.75rem 0 0.5rem; font-weight: 600; }
.cabinets { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 0.6rem; }
.cabinet { border: 1px solid var(--line); border-radius: 6px; padding: 0.4rem 0.5rem; }
.cabinet-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.25rem; margin-bottom: 0.3rem; white-space: nowrap; }
.cabinet-head .fill { margin-left: auto; font-variant-numeric: tabular-nums; }
.slots { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.slot { display: flex; align-items: center; gap: 0.35rem; padding: 0.15rem 0.3rem; border-radius: 4px; background: #f6f7f9; border-left: 3px solid var(--gc, #d0d3d8); font-size: 0.85rem; }
.slot.placeholder { background: transparent; border: 1px dashed #b9bdc4; border-left: 3px dashed var(--gc, #b9bdc4); color: var(--muted); }
.slot-name { font-weight: 600; }
.slot .holes { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.free { font-size: 0.8rem; margin-top: 0.25rem; text-align: right; }

/* settings */
table.grid { border-collapse: collapse; width: 100%; }
table.grid th, table.grid td { border-bottom: 1px solid var(--line); padding: 0.25rem 0.5rem; text-align: left; }
table.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.field { display: flex; align-items: center; gap: 0.6rem; margin: 0.3rem 0; }
.field > span:first-child { min-width: 14rem; }
.profile { border-top: 1px solid var(--line); padding: 0.4rem 0; }
.profile:first-child { border-top: none; }

.app-version { text-align: center; font-size: 0.7rem; font-style: italic; color: var(--muted); }

/* ---- compact pass ---- */
.inv-list { gap: 1px; }
.inv-row { flex-wrap: wrap; padding: 0.15rem 0.35rem; cursor: pointer; }
.inv-size { width: 4.2rem; font-variant-numeric: tabular-nums; }
.inv-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-row.open { background: #f5f7fb; border-color: var(--line); }
.inv-edit { flex-basis: 100%; display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; padding: 0.35rem 0 0.2rem 1.2rem; cursor: default; font-size: 0.85rem; }
.lock { font-size: 0.85rem; opacity: 0.35; }
.lock.on { opacity: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-bottom: 0.5rem; }
.chip { border-radius: 999px; padding: 0.15rem 0.6rem; font-variant-numeric: tabular-nums; }
.chip.current { border-color: var(--accent); color: var(--accent); background: #eef3ff; }
.chip .delta { color: var(--muted); font-size: 0.8rem; }
.chip.tall { border-style: dashed; }

.summary { margin: 0.5rem 0; font-weight: normal; }
.run { display: flex; gap: 6px; align-items: flex-end; overflow-x: auto; padding-bottom: 0.25rem; }
.cab { flex: 0 0 64px; }
.cab-body { display: flex; align-items: flex-end; }
.cab-inner { width: 100%; border: 2px solid #9aa0a8; border-radius: 3px; display: flex; flex-direction: column; justify-content: flex-end; gap: 1px; padding: 1px; background: #fafafa; }
.block { background: var(--gc, #cfd3d9); border-radius: 2px; color: #fff; font-size: 0.68rem; line-height: 1.1; overflow: hidden; display: flex; align-items: center; justify-content: center; text-align: center; padding: 0 2px; }
.block:not([style*="--gc"]) { color: #333; }
.block.placeholder { background: transparent; border: 1px dashed #9aa0a8; color: var(--muted); }
.cab-foot { text-align: center; font-size: 0.75rem; margin-top: 2px; white-space: nowrap; }
.inv-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); column-gap: 0.5rem; }
.inv-row.open { grid-column: 1 / -1; }
.run { overflow-x: visible; }
.cab { flex: 1 1 0; min-width: 34px; max-width: 72px; }
.block span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
button.done { margin-left: auto; }
button.danger { color: #b42318; border-color: #f1c4bf; }
button.danger:hover { background: #fdf0ee; border-color: #b42318; }
button.icon-btn { display: inline-flex; align-items: center; padding: 0.25rem 0.4rem; }
button.icon-btn svg { width: 16px; height: 16px; }

/* ---- header buttons, count mode, phone layout ---- */
.header-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; justify-content: flex-end; }
label.button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 0.3rem 0.7rem; cursor: pointer; }
label.button:hover { border-color: #b8bbc2; }

.count-total { display: flex; align-items: center; gap: 0.4rem; font-size: 1.1rem; margin-bottom: 0.5rem; }
.section-head { display: flex; align-items: center; gap: 0.5rem; width: 100%; border: none; border-bottom: 1px solid var(--line); border-radius: 0; background: none; padding: 0.6rem 0.1rem; font-size: 1rem; font-weight: 600; text-align: left; }
.section-head .muted { font-weight: normal; margin-left: auto; }
.section-head .chev { display: inline-block; transition: transform 0.15s; color: var(--muted); }
.count-gen.open .section-head .chev { transform: rotate(90deg); }
.item-chip.placeholder { border-style: dashed; color: var(--muted); }
.tally { padding: 0.35rem 0; border-bottom: 1px solid var(--line); }
.tally-head { display: flex; align-items: center; gap: 0.6rem; }
.tally-name { font-size: 1.05rem; }
.tally.has .tally-name { font-weight: 600; }
.tally-n { color: var(--muted); font-variant-numeric: tabular-nums; }
.add-btn { margin-left: auto; min-width: 4.5rem; height: 2.5rem; font-size: 1rem; touch-action: manipulation; }
.tally-items { display: flex; flex-wrap: wrap; gap: 0.35rem; padding: 0.4rem 0 0.1rem; }
.item-chip { display: inline-flex; align-items: center; gap: 0.35rem; border-radius: 999px; padding: 0.3rem 0.7rem; font-size: 0.9rem; border-color: var(--gc, var(--line)); touch-action: manipulation; }

.sheet-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); display: flex; align-items: flex-end; justify-content: center; z-index: 10; cursor: pointer; }
.sheet { background: #fff; width: 100%; max-width: 480px; border-radius: 14px 14px 0 0; display: flex; flex-direction: column; max-height: 85vh; max-height: 85dvh; cursor: default; }
.sheet-head { display: flex; align-items: center; gap: 0.5rem; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--line); }
.sheet-title { flex: 1; font-weight: 600; text-align: center; padding-left: 2.5rem; }
.sheet-close { width: 2.5rem; height: 2.5rem; border-radius: 999px; font-size: 1.1rem; padding: 0; }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 0.6rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 0.4rem; }
.sheet-opt { display: flex; align-items: center; gap: 0.6rem; min-height: 3rem; font-size: 1.05rem; text-align: left; border-left: 4px solid var(--gc, var(--line)); flex: none; }
.sheet-opt.danger { color: #b42318; border-left-color: #b42318; }
.sheet-opt svg { width: 18px; height: 18px; }
/* Page behind a sheet can't scroll (iOS needs position: fixed). */
body.sheet-open { position: fixed; left: 0; right: 0; overflow: hidden; }

@media (max-width: 700px) {
  body { margin: 0.5rem; }
  .layout { display: block; }
  .app-header { flex-wrap: wrap; gap: 0.5rem; }
  .panel { padding: 0.6rem; }
  .field { flex-wrap: wrap; }
  .field > span:first-child { min-width: 0; flex-basis: 100%; }
  table.grid { font-size: 0.8rem; }
  #sizes { overflow-x: auto; }
}

/* full-width run */
.cab { max-width: 150px; }
.block { font-size: 0.78rem; }
button.tally-name { display: inline-flex; align-items: center; gap: 0.45rem; border: none; background: none; padding: 0.3rem 0; font: inherit; font-size: 1.05rem; color: inherit; cursor: pointer; }
button.tally-name:disabled { cursor: default; opacity: 1; }
.tally-name .chev { display: inline-block; color: var(--muted); font-size: 0.8rem; transition: transform 0.15s; }
.tally.open .tally-name .chev { transform: rotate(90deg); }
.tally.has button.tally-name { font-weight: 600; }
.run { overflow-x: auto; }
.cab { min-width: 58px; }
.cab-foot { overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 700px) {
  .height-control { flex-wrap: wrap; }
  .height-control input[type=range] { flex-basis: 100%; order: 3; }
  #height-readout { min-width: 0; margin-left: auto; }
  .cab-foot { font-size: 0.7rem; }
}
.block { flex-direction: column; gap: 0; }
.block span { max-width: 100%; }
.block-group { font-size: 0.66rem; opacity: 0.9; line-height: 1.05; }
/* blocks positioned by slotLayout() */
.cab-inner { position: relative; display: block; padding: 0; }
.cab-inner .block { position: absolute; left: 2px; right: 2px; margin-bottom: 2px; }
.cab-inner { border-width: 2px; box-sizing: content-box; }
.run { padding-right: 3px; }
.tally-head .add-btn { min-width: 0; padding: 0 0.7rem; }
.tally-head .add-btn + .add-btn { margin-left: 0; }
.sheet-row { display: flex; gap: 0.5rem; }
.sheet-number { flex: 1; width: auto !important; font-size: 1.4rem; padding: 0.4rem 0.6rem; min-height: 3rem; }
.sheet-ok { min-width: 6rem; font-size: 1.05rem; }

/* side views */
.cab { cursor: pointer; }
.side-views { display: flex; flex-wrap: wrap; gap: 1rem 1.5rem; align-items: flex-end; }
.side-card { margin: 0; }
.side-card figcaption { font-size: 0.85rem; margin-bottom: 0.2rem; }
.sideview { display: block; max-width: 100%; height: auto; }
.sv-side { fill: #ecdcc0; stroke: #9a8465; stroke-width: 1.5; }
.sv-panel { fill: #dcc8a4; stroke: #9a8465; stroke-width: 1.5; }
.sv-hole { fill: #fff; stroke: #6f5f47; stroke-width: 1.5; }
.sv-hole.used { fill: #111; stroke: #111; }
.sv-hole-ring { fill: none; stroke: #111; stroke-width: 2.5; }
.sv-front { font-size: 26px; fill: var(--muted); }
.sv-sys { fill: var(--gc, #c3c8cf); fill-opacity: 0.5; stroke: var(--gc, #7b818a); stroke-width: 2; }
.sv-sys.placeholder { fill: none; stroke-dasharray: 8 6; }
.sv-sys-label { font-size: 26px; text-anchor: middle; dominant-baseline: middle; fill: #111; font-weight: 600; }
.sv-slide { fill: #55595f; fill-opacity: 0.35; stroke: #3a3d42; stroke-width: 1.5; }
.sv-leader { stroke: #666; stroke-width: 1; stroke-dasharray: 4 4; }
.sv-label { font-size: 30px; dominant-baseline: middle; fill: #333; }
.sv-hole-no { font-weight: 700; }
.sv-dim { font-size: 28px; text-anchor: middle; fill: var(--muted); }

/* phase 3: drag, locks, sheets */
.block { cursor: grab; touch-action: pan-y; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.block.dragging { opacity: 0.3; }
.block.ghost { position: fixed; z-index: 20; pointer-events: none; opacity: 0.9; box-shadow: 0 6px 18px rgba(0,0,0,0.25); cursor: grabbing; }
body.dragging-block { cursor: grabbing; user-select: none; }
.block.locked { outline: 2px solid #111; outline-offset: -2px; }
.block-hole { position: absolute; left: 3px; bottom: 1px; font-size: 0.6rem; opacity: 0.7; }
.block-lock { position: absolute; right: 3px; top: 2px; font-size: 0.85rem; line-height: 1; }
.block-gap { position: absolute; right: 3px; bottom: 1px; font-size: 0.6rem; font-weight: 700; }
.cab.drop-ok .cab-inner { outline: 3px solid var(--color-ok); outline-offset: 1px; }
.cab.drop-bad .cab-inner { outline: 3px solid #b42318; outline-offset: 1px; }
.cab.locked .cab-inner { border-color: #111; }
button.cab-foot { display: block; width: 100%; border: none; background: none; padding: 2px 0; font: inherit; font-size: 0.75rem; cursor: pointer; }
.new-cab { opacity: 0.6; }
.new-cab .cab-inner { border-style: dashed; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 0.8rem; }
.new-cab .cab-inner span { position: static; }
#toast { position: fixed; left: 50%; bottom: calc(1rem + env(safe-area-inset-bottom)); transform: translate(-50%, 150%); background: #222; color: #fff; padding: 0.6rem 1rem; border-radius: 8px; z-index: 30; transition: transform 0.2s; max-width: 90vw; font-size: 0.95rem; }
#toast.show { transform: translate(-50%, 0); }
#toast.bad { background: #b42318; }
/* Every column's label is the same height, so all boxes line up. */
.cab-foot { height: 1.4rem; line-height: 1.4rem; padding: 0 !important; margin-top: 2px; box-sizing: border-box; }
/* "+ new" only shows while dragging; it keeps its space so nothing shifts. */
.new-cab { visibility: hidden; opacity: 1; }
body.dragging-block .new-cab { visibility: visible; }
.new-cab .cab-inner { border-color: #9aa0a8; background: #fafafa; }

/* 3D view */
#view3d { position: relative; width: 100%; height: 520px; border-radius: 6px; overflow: hidden; background: #f4f5f7; touch-action: none; }
@media (max-width: 700px) { #view3d { height: 42vh; } }
#view3d canvas { display: block; }
.v3-labels { position: absolute; inset: 0; pointer-events: none; }
.v3-label { font-size: 11px; background: rgba(255,255,255,0.8); padding: 0 3px; border-radius: 3px; color: #222; white-space: nowrap; }
.v3-toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 0.5rem; }
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.v3-toolbar button.on { background: #eef3ff; border-color: var(--accent); color: var(--accent); }
.v3-info { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin-top: 0.5rem; min-height: 2rem; }
/* selection, shared by every view */
.block.selected { box-shadow: 0 0 0 3px var(--accent); z-index: 1; }
.item-chip.selected { box-shadow: 0 0 0 3px var(--accent); }
