/* Layout: drafting-table app — a top bar with the four-step rail, then a drawing sheet beside an
   editor panel; the two stack into one column on phones. */
:root {
  --paper: #e9eeee;        /* tracing-vellum ground */
  --sheet: #fbfcfc;        /* drawing surface */
  --ink: #1b2733;          /* blue-black drafting ink */
  --muted: #5a6875;
  --rule: #cdd7da;
  --grid: #dfe7e9;
  --accent: #1f5c99;       /* drafting blue */
  --accent-soft: #dde8f4;
  --accent-ink: #ffffff;
  --void: #5b6570;         /* unused site, as in the thesis plans */
  --room: #ffffff;
  --danger: #b3261e;
  --ok: #2e7d4f;
  --warn: #8a5a00;
  --font-display: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  --font-body: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  --radius: 6px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #11171c; --sheet: #172028; --ink: #dde6ec; --muted: #95a4b0; --rule: #2b3843; --grid: #1f2b34;
    --accent: #78aee8; --accent-soft: #1d3248; --accent-ink: #0b1622; --void: #0d1318; --room: #263440;
    --danger: #f1867d; --ok: #74cf9b; --warn: #e2b55c;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #11171c; --sheet: #172028; --ink: #dde6ec; --muted: #95a4b0; --rule: #2b3843; --grid: #1f2b34;
  --accent: #78aee8; --accent-soft: #1d3248; --accent-ink: #0b1622; --void: #0d1318; --room: #263440;
  --danger: #f1867d; --ok: #74cf9b; --warn: #e2b55c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.6 var(--font-body);
  -webkit-font-smoothing: antialiased;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10;
  background: var(--sheet);
}
h1, h2, h3 { margin: 0; text-wrap: balance; }
h2 { font: 700 1.25rem/1.3 var(--font-display); letter-spacing: .04em; }
h3 { font-size: .95rem; font-weight: 700; display: flex; align-items: baseline; gap: .5em; }
h3 small, .hint, .note { color: var(--muted); font-weight: 400; }
.hint, .note { margin: .25rem 0 0; font-size: .875rem; max-width: 62ch; }
code { font-family: var(--font-mono); font-size: .9em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- top bar */
.bar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: var(--sheet);
  border-bottom: 1px solid var(--rule);
}
.bar-in {
  display: flex; flex-wrap: wrap; align-items: center; gap: .75rem 1.25rem;
  padding-block: .6rem;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.brand { display: flex; align-items: center; gap: .6rem; color: var(--ink); text-decoration: none; }
.brand-mark { width: 30px; height: 30px; color: var(--accent); flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font: 900 1.2rem/1.1 var(--font-display); letter-spacing: .12em; }
.brand-text small { font-size: .72rem; color: var(--muted); letter-spacing: .08em; }

.steps { display: flex; gap: 2px; overflow-x: auto; flex: 1 1 auto; min-width: 0; }
.step {
  display: flex; align-items: center; gap: .45rem; white-space: nowrap;
  border: 0; background: none; color: var(--muted); font: 500 .9rem var(--font-body);
  padding: .45rem .7rem; border-radius: var(--radius); cursor: pointer;
}
.step-no {
  display: inline-grid; place-items: center; width: 1.45rem; height: 1.45rem; border-radius: 50%;
  border: 1.5px solid currentColor; font: 500 .75rem var(--font-mono);
}
.step:hover { color: var(--ink); background: var(--paper); }
.step[aria-current="step"] { color: var(--accent); background: var(--accent-soft); }
.step[aria-current="step"] .step-no { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.bar-tools { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }
/* 手機：頂列不固定，步驟改為數字在上、四格等寬 */
@media (max-width: 640px) {
  .bar { position: static; }
  .bar-in { gap: .5rem 1rem; }
  .steps { order: 3; flex: 1 1 100%; }
  .step { flex: 1 1 0; flex-direction: column; gap: .2rem; padding: .35rem .2rem; font-size: .8rem; }
}

/* ---------------------------------------------------------------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 2.25rem; padding: .35rem .85rem; border-radius: var(--radius);
  border: 1px solid var(--rule); background: var(--sheet); color: var(--ink);
  font: 500 .9rem var(--font-body); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); color: var(--accent-ink); }
.btn.ghost { border-color: transparent; background: none; }
.btn.ghost:hover { background: var(--paper); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
select, input[type="text"], input[type="number"], textarea {
  font: 400 .9rem var(--font-body); color: var(--ink); background: var(--sheet);
  border: 1px solid var(--rule); border-radius: var(--radius); padding: .35rem .5rem; min-height: 2.25rem;
  width: 100%; min-width: 0;
}
input[type="number"] { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
textarea { font-family: var(--font-mono); font-size: .85rem; line-height: 1.5; resize: vertical; }
select:focus, input:focus, textarea:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 2px var(--accent-soft); }
input.bad { border-color: var(--danger); }
.preset select { width: auto; max-width: 15rem; }

.field { display: flex; flex-direction: column; gap: .2rem; min-width: 0; }
.field > span:first-child { font-size: .78rem; color: var(--muted); letter-spacing: .04em; }
.unit-in { display: flex; align-items: center; gap: .3rem; }
.unit-in i { font-style: normal; font-size: .8rem; color: var(--muted); white-space: nowrap; }
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.nav-row { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: flex-end; }

.chips { display: flex; flex-wrap: wrap; gap: .3rem; }
.chip {
  border: 1px solid var(--rule); background: var(--sheet); color: var(--ink);
  font: 500 .82rem var(--font-body); padding: .2rem .65rem; min-height: 1.9rem; border-radius: 999px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--rule); border-radius: var(--radius); padding: 2px; margin: 0; gap: 2px; min-width: 0; }
.seg legend { font-size: .78rem; color: var(--muted); padding: 0 .2rem; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: block; padding: .3rem .75rem; border-radius: 4px; font-size: .88rem; cursor: pointer; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg.compact span { padding: .25rem .65rem; }

/* ---------------------------------------------------------------- workspace */
.work {
  padding-block: 1rem 2rem;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  max-width: 1440px; margin: 0 auto;
}
.view { display: grid; grid-template-columns: minmax(0, 1fr) 21rem; gap: 1rem; align-items: start; }
@media (max-width: 900px) { .view { grid-template-columns: minmax(0, 1fr); } }
.sheet {
  background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius);
  min-width: 0;
}
.sheet-head { padding: 1rem 1.1rem .75rem; border-bottom: 1px solid var(--rule); }
.plan-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: .75rem; }
.sheet-body { padding: 1rem 1.1rem 1.1rem; min-width: 0; }
.panel { display: flex; flex-direction: column; gap: 1rem; min-width: 0; position: sticky; top: calc(env(safe-area-inset-top, 0px) + 4.5rem); }
@media (max-width: 900px) { .panel { position: static; } }
.panel-sec { display: flex; flex-direction: column; gap: .6rem; background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); padding: .9rem; }
.panel > .field { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); padding: .9rem; }

.facts { display: grid; grid-template-columns: auto 1fr; gap: .3rem .9rem; margin: 0; font-size: .88rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.panel > .facts { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); padding: .9rem; }

/* ---------------------------------------------------------------- site editor */
.site-canvas {
  display: block; width: 100%; height: auto; max-height: 72vh; aspect-ratio: 1; max-width: 100%;
  background: var(--sheet); touch-action: none; cursor: crosshair; user-select: none;
}
.sc-grid { stroke: var(--grid); stroke-width: 1; vector-effect: non-scaling-stroke; }
.sc-grid.major { stroke: var(--rule); }
.sc-axis { fill: var(--muted); font-family: var(--font-mono); }
.sc-rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; fill-opacity: .75; }
.sc-rect.top { stroke-width: 2.5; }
.sc-label { fill: var(--accent); font-family: var(--font-mono); font-weight: 700; }
.sc-drag { fill: var(--accent); fill-opacity: .15; stroke: var(--accent); stroke-dasharray: 4 3; stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.sc-dim { fill: var(--ink); font-family: var(--font-mono); font-weight: 500; }

.site-list { display: flex; flex-direction: column; gap: .5rem; }
.site-row { display: grid; grid-template-columns: 2rem repeat(4, minmax(0, 1fr)); gap: .35rem; align-items: end; }
.site-row .tag { font: 700 .85rem var(--font-mono); color: var(--accent); padding-bottom: .5rem; }
.site-row .field > span:first-child { font-size: .7rem; }
.site-row .size { grid-column: 2 / -1; font: 400 .78rem var(--font-mono); color: var(--muted); margin-top: -.15rem; }
.empty { color: var(--muted); font-size: .88rem; margin: 0; }

/* ---------------------------------------------------------------- rooms editor */
.room-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr)); gap: .75rem; }
.room-card {
  border: 1px solid var(--rule); border-radius: var(--radius); padding: .75rem; display: flex; flex-direction: column; gap: .6rem;
  background: var(--sheet); min-width: 0;
}
.room-card.has-err { border-color: var(--danger); }
.room-card header { display: flex; gap: .5rem; align-items: center; }
.room-no { font: 700 .85rem var(--font-mono); color: var(--accent); min-width: 1.6rem; }
.room-card .room-name { font-weight: 700; }
.room-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .45rem .6rem; }
.room-opt { display: flex; flex-direction: column; gap: .3rem; }
.room-opt > span { font-size: .78rem; color: var(--muted); }
.room-err { color: var(--danger); font-size: .8rem; margin: 0; }
.add-room { margin-top: .75rem; width: 100%; border-style: dashed; }
.budget { display: flex; flex-direction: column; gap: .5rem; font-size: .88rem; }
.meter { height: .55rem; border-radius: 999px; background: var(--grid); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--accent); }
.meter.over i { background: var(--danger); }

/* ---------------------------------------------------------------- relations */
.rel-body { display: grid; grid-template-columns: minmax(0, auto) minmax(0, 1fr); gap: 1.25rem; align-items: start; }
@media (max-width: 1100px) { .rel-body { grid-template-columns: minmax(0, 1fr); } }
.matrix-wrap, .rel-table-wrap, .room-table-wrap { overflow-x: auto; min-width: 0; }
.rel-table-wrap { grid-column: 1 / -1; }
.matrix { border-collapse: collapse; font-size: .8rem; }
.matrix th { font-weight: 500; color: var(--muted); padding: .2rem .35rem; white-space: nowrap; }
.matrix thead th { writing-mode: vertical-rl; text-align: left; vertical-align: bottom; height: 5.5rem; }
.matrix tbody th { text-align: right; }
.matrix td { width: 2.1rem; height: 2.1rem; padding: 0; border: 1px solid var(--rule); text-align: center; }
.matrix td.diag { background: var(--grid); }
.matrix td button {
  width: 100%; height: 100%; border: 0; background: none; color: var(--muted); cursor: pointer;
  font: 700 .82rem var(--font-body);
}
.matrix td button:hover { background: var(--accent-soft); }
.matrix td.k-connect button { color: var(--accent); }
.matrix td.k-hidden button { color: var(--accent); font-weight: 400; }
.matrix td.k-dist button { color: var(--warn); }
.matrix td.sel { outline: 2px solid var(--accent); outline-offset: -2px; }
.matrix-key { display: flex; flex-wrap: wrap; gap: .3rem .9rem; font-size: .78rem; color: var(--muted); margin-top: .5rem; }
.rel-form { display: flex; flex-direction: column; gap: .8rem; min-width: 0; }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: .5rem; align-items: end; }
.pair-and { padding-bottom: .5rem; color: var(--muted); }
.rel-params { display: flex; flex-direction: column; gap: .7rem; }
.rel-params[hidden] { display: none; }
.rel-params .num { max-width: 12rem; }
.form-msg { font-size: .85rem; color: var(--muted); }
.form-msg.err { color: var(--danger); }
.rel-table, .room-table { border-collapse: collapse; width: 100%; font-size: .85rem; }
.rel-table th, .rel-table td, .room-table th, .room-table td { padding: .35rem .5rem; border-bottom: 1px solid var(--rule); text-align: left; white-space: nowrap; }
.rel-table th, .room-table th { color: var(--muted); font-weight: 500; font-size: .78rem; }
.rel-table td.num, .room-table td.num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; text-align: right; }
.rel-table tr.sel td { background: var(--accent-soft); }
.rel-table button { min-height: 1.8rem; padding: .1rem .5rem; font-size: .8rem; }

/* ---------------------------------------------------------------- plan */
.plan-wrap {
  background: var(--sheet); border-radius: var(--radius); min-height: 18rem; display: grid; place-items: center;
  overflow-x: auto;
}
.plan-wrap > svg { display: block; width: 100%; height: auto; max-height: 76vh; max-width: 100%; }
.plan-empty { color: var(--muted); padding: 3rem 1rem; text-align: center; max-width: 36ch; }
.plan-empty.err { color: var(--danger); }
.plan .pl-void { fill: var(--void); }
.plan .pl-hatch { stroke: var(--muted); stroke-opacity: .45; stroke-width: .3; }
/* 同色描邊蓋住相鄰矩形間的反鋸齒細縫 */
.plan .pl-room { fill: var(--room); stroke: var(--room); stroke-width: .3; }
.plan .pl-site { fill: none; stroke: var(--ink); stroke-width: .6; }
.plan .pl-wall { fill: var(--ink); }
.plan .pl-door { fill: none; stroke: var(--ink); stroke-width: .7; }
.plan .pl-out { fill: var(--room); stroke: var(--ink); stroke-width: .7; }
.plan text { font-family: var(--font-body); }
.plan .pl-name { fill: var(--ink); font-weight: 700; }
.plan .pl-sub { fill: var(--ink); font-family: var(--font-mono); }
.plan .pl-dim { fill: var(--muted); }
.plan .pl-dim path { fill: none; stroke: var(--muted); stroke-width: .5; }
.plan .pl-dim .pl-north { fill: var(--ink); stroke: none; }
.searching .plan-wrap > svg { opacity: .35; transition: opacity .2s; }

.gallery-wrap { margin-top: 1rem; border-top: 1px solid var(--rule); padding-top: .8rem; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); gap: .5rem; margin-top: .5rem; }
.thumb {
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--sheet); padding: .3rem; cursor: pointer;
  display: flex; flex-direction: column; gap: .2rem; color: var(--muted); font: 400 .72rem var(--font-mono);
}
.thumb svg { width: 100%; height: auto; display: block; }
.thumb:hover { border-color: var(--accent); }
.thumb[aria-current="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); color: var(--accent); }
.actions .btn { flex: 1 1 auto; }
.issues { border: 1px solid var(--danger); border-radius: var(--radius); padding: .75rem .9rem; color: var(--danger); font-size: .86rem; background: var(--sheet); }
.issues.warn { border-color: var(--warn); color: var(--warn); }
.issues ul { margin: .3rem 0 0; padding-left: 1.2rem; }
.room-table td:first-child { white-space: normal; }

/* ---------------------------------------------------------------- dialogs, toast */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgb(10 16 22 / .45);
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}
.dialog {
  background: var(--sheet); color: var(--ink); border-radius: 8px; border: 1px solid var(--rule);
  width: min(40rem, 100%); max-height: 100%; overflow-y: auto; padding: 1.1rem 1.2rem 1.2rem;
  display: flex; flex-direction: column; gap: .8rem;
}
.dialog.wide { width: min(48rem, 100%); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.prose { font-size: .92rem; }
.prose h3 { margin: 1rem 0 .3rem; }
.prose p, .prose li { max-width: 68ch; }
.prose ul, .prose ol { padding-left: 1.3rem; margin: .3rem 0; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 30;
  bottom: calc(1.2rem + env(safe-area-inset-bottom, 0px));
  background: var(--ink); color: var(--sheet); padding: .55rem 1rem; border-radius: var(--radius);
  font-size: .9rem; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgb(0 0 0 / .2);
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
