/* 版面概念：繪圖桌——灰泥色桌面上的描圖紙面板，尺寸讀數用等寬字，像施工圖上的標註 */
:root {
  --plaster: #e9e6e0;
  --sheet: #f8f7f4;
  --sheet-2: #f0eee9;
  --ink: #23262b;
  --ink-2: #5d6168;
  --rule: #d8d4cc;
  --accent: #2c5d8f;
  --accent-soft: #dce6f0;
  --accent-ink: #ffffff;
  --danger: #b8432f;
  --danger-soft: #f5dfda;
  --ok: #3f7a52;
  --scene: #e4e1db;
  --shadow: 0 1px 2px rgba(35, 38, 43, .06), 0 8px 24px rgba(35, 38, 43, .08);
  --font-display: "Noto Serif TC", "Songti TC", serif;
  --font-body: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --left-w: 288px;
  --right-w: 300px;
  --top-h: 54px;
  --r: 8px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --plaster: #16181b; --sheet: #1e2125; --sheet-2: #262a2f; --ink: #e8e6e1; --ink-2: #a2a6ad;
    --rule: #33383e; --accent: #7aaee0; --accent-soft: #24384d; --accent-ink: #0f1a26; --danger: #e7826d;
    --danger-soft: #3b2420; --ok: #7cc493; --scene: #24272b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --plaster: #16181b; --sheet: #1e2125; --sheet-2: #262a2f; --ink: #e8e6e1; --ink-2: #a2a6ad;
  --rule: #33383e; --accent: #7aaee0; --accent-soft: #24384d; --accent-ink: #0f1a26; --danger: #e7826d;
  --danger-soft: #3b2420; --ok: #7cc493; --scene: #24272b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--plaster);
  color: var(--ink);
  font: 14px/1.5 var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  height: 100%;
  display: grid;
  grid-template-columns: var(--left-w) minmax(0, 1fr) var(--right-w);
  grid-template-rows: var(--top-h) minmax(0, 1fr);
  grid-template-areas: "top top top" "left stage right";
}

/* ---------- 上方工具列 ---------- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding-block: 0;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--sheet);
  border-bottom: 1px solid var(--rule);
  min-width: 0;
}
.brand { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.brand-mark {
  width: 22px; height: 22px; border: 2px solid var(--ink); border-radius: 2px; position: relative;
}
.brand-mark::before, .brand-mark::after { content: ""; position: absolute; background: var(--accent); }
.brand-mark::before { left: 3px; bottom: 3px; width: 8px; height: 5px; }
.brand-mark::after { right: 3px; top: 3px; width: 5px; height: 9px; }
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: 17px; letter-spacing: .04em; }

.room-chip {
  display: inline-flex; align-items: baseline; gap: 4px;
  font-family: var(--font-mono); font-size: 13px; font-variant-numeric: tabular-nums;
  background: var(--sheet-2); border: 1px dashed var(--rule); border-radius: 999px; padding: 4px 12px;
}
.room-chip:hover { border-color: var(--accent); color: var(--accent); }
.room-chip .unit { color: var(--ink-2); font-size: 11px; }

.views { display: flex; background: var(--sheet-2); border: 1px solid var(--rule); border-radius: 8px; padding: 2px; margin-left: auto; }
.seg { border: 0; background: none; padding: 5px 12px; border-radius: 6px; font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.seg:hover { color: var(--ink); }
.seg.is-on { background: var(--sheet); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); font-weight: 500; }

.tools { display: flex; align-items: center; gap: 4px; margin-left: 8px; }
.divider { width: 1px; height: 22px; background: var(--rule); margin: 0 6px; }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center; border: 0; border-radius: 8px; background: none; color: var(--ink-2);
}
.icon-btn:hover:not(:disabled) { background: var(--sheet-2); color: var(--ink); }
.icon-btn:disabled { opacity: .35; cursor: default; }

.btn {
  border: 1px solid var(--rule); background: var(--sheet); border-radius: 8px; padding: 6px 14px; font-size: 13px; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn.ghost { border-color: transparent; background: none; color: var(--ink-2); }
.btn.ghost:hover { background: var(--sheet-2); color: var(--ink); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; cursor: progress; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; min-width: 190px;
  background: var(--sheet); border: 1px solid var(--rule); border-radius: 10px; box-shadow: var(--shadow); padding: 6px;
  display: flex; flex-direction: column;
}
.menu button { text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; font-size: 13px; }
.menu button:hover { background: var(--sheet-2); }
.menu button.danger { color: var(--danger); }

/* ---------- 面板 ---------- */
.panel { background: var(--sheet); overflow-y: auto; overscroll-behavior: contain; min-width: 0; }
.panel.left { grid-area: left; border-right: 1px solid var(--rule); display: flex; flex-direction: column; }
.panel.right { grid-area: right; border-left: 1px solid var(--rule); }

.tabs { display: flex; border-bottom: 1px solid var(--rule); position: sticky; top: 0; background: var(--sheet); z-index: 2; }
.tab { flex: 1; border: 0; background: none; padding: 12px 8px; font-size: 13px; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.is-on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 500; }

.tab-body { padding: 14px 16px 24px; display: flex; flex-direction: column; gap: 12px; }

.search { display: flex; align-items: center; gap: 8px; background: var(--sheet-2); border: 1px solid var(--rule); border-radius: 8px; padding: 0 10px; color: var(--ink-2); }
.search input { border: 0; background: none; outline: none; padding: 8px 0; width: 100%; color: var(--ink); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--rule); background: none; border-radius: 999px; padding: 3px 11px; font-size: 12px; color: var(--ink-2); }
.chip.is-on { background: var(--ink); border-color: var(--ink); color: var(--sheet); }
.chip .n { font-family: var(--font-mono); font-size: 11px; opacity: .7; margin-left: 3px; }
.hint { margin: 0; font-size: 12px; color: var(--ink-2); }

.lib-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.lib-card {
  display: flex; flex-direction: column; text-align: left; gap: 2px; padding: 6px 6px 8px;
  border: 1px solid var(--rule); background: var(--sheet); border-radius: var(--r); cursor: grab; position: relative; min-width: 0;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.lib-card:hover { border-color: var(--accent); }
.lib-card:active { cursor: grabbing; }
.lib-card .thumb { aspect-ratio: 4 / 3; max-width: 100%; border-radius: 5px; background: var(--sheet-2); display: grid; place-items: center; overflow: hidden; }
.lib-card .thumb img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.lib-card .thumb.photo img { object-fit: cover; }
.lib-card .name { font-size: 12.5px; font-weight: 500; padding: 4px 2px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-card .size { font-family: var(--font-mono); font-size: 10.5px; color: var(--ink-2); padding: 0 2px; font-variant-numeric: tabular-nums; }
.lib-card .del {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 6px; border: 0;
  background: var(--sheet); color: var(--ink-2); display: none; place-items: center; font-size: 14px; line-height: 1;
}
.lib-card:hover .del { display: grid; }
.lib-card .badge { position: absolute; top: 10px; left: 10px; font-size: 10px; background: var(--accent); color: var(--accent-ink); border-radius: 4px; padding: 0 5px; }
.empty { font-size: 12.5px; color: var(--ink-2); border: 1px dashed var(--rule); border-radius: var(--r); padding: 14px; grid-column: 1 / -1; }

.block { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--rule); border-radius: var(--r); padding: 12px; }
.block h3, .block summary { margin: 0; font-size: 13.5px; font-weight: 700; }
.block summary { cursor: pointer; }
details.block[open] summary { margin-bottom: 4px; }
.sub { margin: 0; font-size: 12px; color: var(--ink-2); line-height: 1.55; }
.row { display: flex; gap: 6px; }
.row > input { flex: 1; min-width: 0; }
input[type="url"], input[type="text"], input[type="number"], select, textarea {
  background: var(--sheet); border: 1px solid var(--rule); border-radius: 7px; padding: 7px 9px; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; font-size: 13px; }
.notice { font-size: 12px; line-height: 1.55; background: var(--accent-soft); border-radius: var(--r); padding: 10px 12px; }
.notice.warn { background: var(--danger-soft); }
.bookmarklet {
  align-self: flex-start; text-decoration: none; color: var(--accent-ink); background: var(--accent);
  border-radius: 7px; padding: 6px 12px; font-size: 12.5px; font-weight: 500; cursor: grab;
}
.file-drop { border: 1.5px dashed var(--rule); border-radius: var(--r); padding: 14px; text-align: center; font-size: 12.5px; color: var(--ink-2); cursor: pointer; }
.file-drop:hover, .file-drop.over { border-color: var(--accent); color: var(--accent); }
.file-drop input { display: none; }

.result-head { display: flex; gap: 10px; align-items: flex-start; }
.result-head img { width: 72px; height: 72px; object-fit: cover; border-radius: 6px; background: var(--sheet-2); flex-shrink: 0; }
.result-head .meta { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.result-head .src { font-size: 11px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-head .price { font-family: var(--font-mono); font-size: 12px; }
.tag { display: inline-block; font-size: 10.5px; border-radius: 4px; padding: 0 6px; background: var(--sheet-2); color: var(--ink-2); }
.tag.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.tag.warn { background: var(--danger-soft); color: var(--danger); }

/* ---------- 屬性面板 ---------- */
.props { padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.props h2 { margin: 0; font-family: var(--font-display); font-size: 18px; font-weight: 700; text-wrap: balance; }
.eyebrow { margin: 0; font-size: 11px; letter-spacing: .12em; color: var(--ink-2); text-transform: uppercase; }
.group { display: flex; flex-direction: column; gap: 8px; }
.group > .label { font-size: 11.5px; letter-spacing: .08em; color: var(--ink-2); }
.dims3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.dims3 label { display: flex; flex-direction: column; gap: 3px; position: relative; min-width: 0; }
.dims3 label span { font-size: 11.5px; color: var(--ink-2); }
.dims3 input { font-family: var(--font-mono); font-variant-numeric: tabular-nums; padding-right: 30px; }
.dims3 em { position: absolute; right: 9px; bottom: 8px; font-style: normal; font-size: 11px; color: var(--ink-2); pointer-events: none; }
.field { display: flex; flex-direction: column; gap: 3px; }
.field > span { font-size: 11.5px; color: var(--ink-2); }
.inline { display: flex; align-items: center; gap: 8px; }
.inline input[type="range"] { flex: 1; accent-color: var(--accent); min-width: 0; }
.readout { font-family: var(--font-mono); font-size: 12.5px; font-variant-numeric: tabular-nums; min-width: 44px; text-align: right; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sw { width: 26px; height: 26px; border-radius: 50%; border: 1px solid rgba(0,0,0,.12); padding: 0; }
.sw.is-on { outline: 2px solid var(--accent); outline-offset: 2px; }
.sw-custom { width: 30px; height: 30px; border: 0; padding: 0; background: none; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }
.btn-row .btn { flex: 1; }
.rot-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.rot-row .btn { padding-inline: 4px; }
.toggles { display: flex; flex-direction: column; gap: 2px; }
.toggle { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; font-size: 13px; cursor: pointer; }
.toggle input { appearance: none; width: 34px; height: 20px; border-radius: 999px; background: var(--rule); position: relative; transition: background .15s; cursor: pointer; flex-shrink: 0; }
.toggle input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--sheet); transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(14px); }
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; background: var(--rule); border: 1px solid var(--rule); border-radius: var(--r); overflow: hidden; }
.stats div { background: var(--sheet); padding: 8px 10px; display: flex; flex-direction: column; }
.stats b { font-family: var(--font-mono); font-weight: 500; font-size: 15px; font-variant-numeric: tabular-nums; }
.stats small { font-size: 11px; color: var(--ink-2); }
.warn-line { font-size: 12px; color: var(--danger); background: var(--danger-soft); border-radius: 6px; padding: 6px 10px; }
.item-head { display: flex; gap: 10px; align-items: center; }
.item-head img { width: 54px; height: 44px; object-fit: contain; border-radius: 6px; background: var(--sheet-2); flex-shrink: 0; }
.item-head .name-input { font-family: var(--font-display); font-weight: 700; font-size: 16px; border-color: transparent; padding: 4px 6px; background: none; }
.item-head .name-input:hover { border-color: var(--rule); }
.link { color: var(--accent); font-size: 12px; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link:hover { text-decoration: underline; }
.list-items { display: flex; flex-direction: column; border: 1px solid var(--rule); border-radius: var(--r); max-height: 220px; overflow-y: auto; }
.list-items button { display: flex; justify-content: space-between; gap: 8px; border: 0; border-bottom: 1px solid var(--rule); background: none; padding: 7px 10px; font-size: 12.5px; text-align: left; }
.list-items button:last-child { border-bottom: 0; }
.list-items button:hover { background: var(--sheet-2); }
.list-items .sz { font-family: var(--font-mono); font-size: 11px; color: var(--ink-2); white-space: nowrap; }
.list-items .bad { color: var(--danger); }

/* ---------- 3D 舞台 ---------- */
.stage { grid-area: stage; position: relative; min-width: 0; overflow: hidden; background: var(--scene); }
.viewport { position: absolute; inset: 0; touch-action: none; }
.viewport canvas { display: block; }
.viewport.grab { cursor: grab; }
.viewport.dragging { cursor: grabbing; }
.hud-tl {
  position: absolute; top: 12px; left: 12px; display: flex; gap: 6px; flex-wrap: wrap; pointer-events: none;
  font-family: var(--font-mono); font-size: 11.5px; font-variant-numeric: tabular-nums;
}
.hud-tl span { background: color-mix(in srgb, var(--sheet) 88%, transparent); border: 1px solid var(--rule); border-radius: 6px; padding: 3px 8px; }
.hud-tl span.bad { color: var(--danger); border-color: var(--danger); }
.hud-bl {
  position: absolute; left: 12px; bottom: calc(12px + env(safe-area-inset-bottom, 0px)); display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 11.5px; color: var(--ink-2);
  background: color-mix(in srgb, var(--sheet) 85%, transparent); border-radius: 8px; padding: 6px 10px; pointer-events: none; max-width: calc(100% - 24px);
}
kbd { font-family: var(--font-mono); font-size: 10.5px; border: 1px solid var(--rule); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; margin-right: 3px; background: var(--sheet); color: var(--ink); }
.drop-hint { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--sheet); padding: 5px 12px; border-radius: 999px; font-size: 12px; pointer-events: none; }

/* 3D 尺寸標註 */
.dim-label {
  font-family: var(--font-mono); font-size: 11px; font-variant-numeric: tabular-nums; color: var(--accent);
  background: var(--sheet); border: 1px solid var(--accent); border-radius: 4px; padding: 0 5px; white-space: nowrap; pointer-events: none;
}
.dim-label.room { color: var(--ink-2); border-color: var(--rule); background: color-mix(in srgb, var(--sheet) 85%, transparent); }
.dim-label.bad { color: var(--danger); border-color: var(--danger); }

/* ---------- 對話框與提示 ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: color-mix(in srgb, var(--plaster) 55%, transparent); backdrop-filter: blur(3px);
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); padding-block: 16px;
}
.modal { width: min(480px, 100%); background: var(--sheet); border: 1px solid var(--rule); border-radius: 14px; box-shadow: var(--shadow); padding: 24px; display: flex; flex-direction: column; gap: 14px; max-height: 100%; overflow-y: auto; }
.modal.small { width: min(380px, 100%); }
.modal h2 { margin: 0; font-family: var(--font-display); font-size: 22px; text-wrap: balance; }
.modal .dims3 input { font-size: 18px; padding-block: 9px; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.presets button { border: 1px solid var(--rule); background: none; border-radius: 999px; padding: 4px 11px; font-size: 12px; color: var(--ink-2); }
.presets button:hover { border-color: var(--accent); color: var(--accent); }
.area { margin: 0; font-family: var(--font-mono); font-size: 12.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.toasts { position: fixed; z-index: 60; left: 50%; transform: translateX(-50%); bottom: calc(20px + env(safe-area-inset-bottom, 0px)); display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { background: var(--ink); color: var(--sheet); border-radius: 8px; padding: 8px 14px; font-size: 13px; box-shadow: var(--shadow); animation: toast-in .2s ease-out; max-width: 90vw; }
.toast.err { background: var(--danger); color: #fff; }
@keyframes toast-in { from { transform: translateY(6px); opacity: .4; } to { transform: none; opacity: 1; } }

.scrim { position: fixed; inset: var(--top-h) 0 0 0; background: rgba(0,0,0,.25); z-index: 19; }
.only-mobile, .only-narrow { display: none !important; }

/* ---------- 響應式 ---------- */
@media (max-width: 1180px) {
  :root { --left-w: 256px; --right-w: 268px; }
  .hide-narrow { display: none !important; }
  .only-narrow { display: block !important; }
  .brand-name { display: none; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "stage"; }
  .only-mobile { display: grid !important; }
  .panel {
    position: fixed; top: var(--top-h); bottom: 0; z-index: 20; width: min(330px, 88vw); box-shadow: var(--shadow);
    transition: transform .22s ease; padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .panel.left { left: 0; transform: translateX(-102%); padding-left: env(safe-area-inset-left, 0px); }
  .panel.right { right: 0; transform: translateX(102%); padding-right: env(safe-area-inset-right, 0px); }
  .panel.open { transform: none; }
  .topbar { gap: 6px; }
  .views { margin-left: auto; }
  .seg { padding: 5px 8px; }
  .tools { margin-left: 0; }
  .tools .divider, #btnUndo, #btnRedo { display: none; }
}
@media (max-width: 520px) {
  .room-chip { display: none; }
  .seg[data-view="front"] { display: none; }
  .brand { display: none; }
  .views { margin-left: 0; flex: 1; }
  .seg { flex: 1; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
