/* Bobo 平面轉3D — 樣式（手機優先） */
:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #1c2630;
  --muted: #5d6b78;
  --line: #d8dfe5;
  --primary: #1f5f8b;
  --primary-ink: #ffffff;
  --primary-soft: #e3eef6;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --ok: #1e7a46;
  --ok-soft: #e4f4ea;
  --warn: #8a5a00;
  --warn-soft: #fff3d6;
  --info: #3c4c9e;
  --info-soft: #e8ebfa;
  --grey-soft: #eceff1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 32, .06), 0 2px 8px rgba(16, 24, 32, .06);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1a2128;
    --surface-2: #222b33;
    --text: #e6ecf1;
    --muted: #9aa8b4;
    --line: #2f3a44;
    --primary: #5fa8d8;
    --primary-ink: #0d1a24;
    --primary-soft: #1d3446;
    --danger: #f2867c;
    --danger-soft: #3a1f1d;
    --ok: #6fcf97;
    --ok-soft: #173326;
    --warn: #f0c060;
    --warn-soft: #3a2f14;
    --info: #9fb0f5;
    --info-soft: #23294a;
    --grey-soft: #2a333b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .5em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }

/* 頂列 */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .9rem; }

.app { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(96px + var(--safe-b)); }
.loading { color: var(--muted); text-align: center; padding: 40px 0; }

/* 卡片與表單 */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.field { margin-bottom: 14px; }
.field > label, .label { display: block; font-weight: 600; margin-bottom: 6px; }
.hint { color: var(--muted); font-size: .85rem; margin-top: 4px; }
.req { color: var(--danger); margin-left: 2px; }
input[type="text"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
  width: 100%;
  font: inherit;
  font-size: 16px; /* 避免 iOS 放大 */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px;
  font: inherit; font-weight: 600;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn-link { background: none; border: none; color: var(--primary); font: inherit; cursor: pointer; padding: 6px 4px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row-between { display: flex; gap: 8px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }

.msg { border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; font-size: .95rem; }
.msg-error { background: var(--danger-soft); color: var(--danger); }
.msg-ok { background: var(--ok-soft); color: var(--ok); }
.msg-warn { background: var(--warn-soft); color: var(--warn); }
.msg-info { background: var(--info-soft); color: var(--info); }

/* 登入 */
.login-wrap { min-height: 70vh; display: flex; flex-direction: column; justify-content: center; max-width: 380px; margin: 0 auto; }
.login-title { text-align: center; margin-bottom: 4px; font-size: 1.5rem; }
.login-sub { text-align: center; color: var(--muted); margin-bottom: 20px; }

/* 案件清單 */
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar input[type="search"] { flex: 1 1 auto; }
.case-item {
  display: block; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.case-item:active { background: var(--surface-2); }
.case-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 4px; }
.case-id { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .9rem; font-weight: 600; }
.case-name { font-weight: 700; font-size: 1.05rem; margin-bottom: 2px; word-break: break-word; }
.case-meta { color: var(--muted); font-size: .85rem; }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }

.badge {
  display: inline-block; border-radius: 999px; padding: 2px 10px;
  font-size: .78rem; font-weight: 700; white-space: nowrap;
}
.b-awaiting_recognition { background: var(--warn-soft); color: var(--warn); }
.b-recognized { background: var(--info-soft); color: var(--info); }
.b-plan_confirmed { background: var(--ok-soft); color: var(--ok); }
.b-archived { background: var(--grey-soft); color: var(--muted); }
.b-incomplete { background: var(--danger-soft); color: var(--danger); }
.b-lock { background: var(--primary-soft); color: var(--primary); }

.check { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--muted); }
.check input { width: 18px; height: 18px; }

/* 固定底部主要按鈕（手機） */
.fab-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  padding: 12px 16px calc(12px + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.fab-bar .btn { width: 100%; max-width: 728px; margin: 0 auto; display: flex; }

/* 建案：頁面清單 */
.drop {
  border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 18px; text-align: center; color: var(--muted); background: var(--surface);
}
.drop.drag { border-color: var(--primary); background: var(--primary-soft); }
.drop input[type="file"] { display: none; }
.pages { list-style: none; margin: 12px 0 0; padding: 0; }
.page-row {
  display: grid; grid-template-columns: 88px 1fr; gap: 12px; align-items: start;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.page-row:first-child { border-top: none; }
.thumb {
  width: 88px; height: 88px; object-fit: contain;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
}
.page-info { min-width: 0; }
.page-title { font-weight: 600; }
.page-sub { color: var(--muted); font-size: .8rem; word-break: break-all; margin-bottom: 6px; }
.page-ctrl { display: flex; gap: 6px; align-items: center; }
.page-ctrl input { max-width: 140px; }

.progress { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: 8px 0; }
.progress > div { height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.steps { list-style: none; margin: 8px 0 0; padding: 0; font-size: .9rem; }
.steps li { padding: 3px 0; color: var(--muted); }
.steps li.done { color: var(--ok); }
.steps li.now { color: var(--text); font-weight: 600; }
.steps li.fail { color: var(--danger); font-weight: 600; }

/* 案件概覽 */
.kv { list-style: none; margin: 0; padding: 0; }
.kv li { display: flex; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); font-size: .95rem; }
.kv li:first-child { border-top: none; }
.kv .k { color: var(--muted); flex: 0 0 5.5em; }
.kv .v { flex: 1 1 auto; min-width: 0; word-break: break-word; }
.gallery { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 560px) { .gallery { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.g-item { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px; cursor: pointer; text-align: left; font: inherit; color: inherit; }
.g-img { width: 100%; aspect-ratio: 4 / 3; object-fit: contain; background: #fff; border-radius: 6px; display: block; }
.g-cap { font-size: .8rem; color: var(--muted); margin-top: 4px; }
.instr {
  white-space: pre-wrap; word-break: break-word;
  background: var(--surface-2); border-radius: 10px; padding: 10px 12px;
  font-size: .88rem; margin: 8px 0;
}

/* 看圖 */
.viewer {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .92);
  display: flex; flex-direction: column;
}
.viewer-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px; color: #fff;
}
.viewer-bar .btn { background: rgba(255, 255, 255, .12); color: #fff; border-color: rgba(255, 255, 255, .25); }
.viewer-body { flex: 1 1 auto; overflow: auto; -webkit-overflow-scrolling: touch; }
.viewer-body img { display: block; background: #fff; margin: 0 auto; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-b)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  z-index: 60; font-size: .95rem; max-width: calc(100% - 32px);
}
.small { font-size: .85rem; }
.muted { color: var(--muted); }

/* ---------- 平面編輯（v1.1） ---------- */
body.editing { overflow: hidden; overscroll-behavior: none; }
body.editing .app { padding: 0; }
.ed {
  position: fixed; left: 0; right: 0; bottom: 0; top: 0; z-index: 10;
  display: grid; grid-template-rows: auto auto auto minmax(0, 1fr) minmax(0, 42vh); grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "tool" "layers" "banner" "canvas" "panel";
  background: var(--bg);
}
.ed-toolbar { grid-area: tool; }
.ed-layers { grid-area: layers; }
.ed-banner { grid-area: banner; }
.ed-canvas { grid-area: canvas; }
.ed-panel { grid-area: panel; }
.ed-toolbar {
  display: flex; gap: 6px; align-items: center; padding: 6px 8px;
  overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.ed-toolbar::-webkit-scrollbar { display: none; }
.ed-toolbar .btn { flex: 0 0 auto; min-width: 40px; }
.ed-title { flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .9rem; }
.ed-floor { width: auto; min-height: 36px; padding: 4px 8px; flex: 0 0 auto; }
.ed-layers { display: flex; gap: 14px; flex-wrap: wrap; padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--line); }
.ed-banner { padding: 6px 8px 0; }
.ed-banner .msg { margin-bottom: 6px; }
.ed-canvas { position: relative; min-height: 0; overflow: hidden; background: #fff; }
.ed-svg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; display: block; }
.ed-hit { cursor: pointer; }
.ed-handle { fill: #fff; stroke: #d81b60; stroke-width: 2; vector-effect: non-scaling-stroke; cursor: grab; }
.ed-label { fill: #1c2630; paint-order: stroke; stroke: rgba(255, 255, 255, .85); stroke-width: 3px; stroke-linejoin: round; pointer-events: none; dominant-baseline: middle; font-weight: 600; }
.ed-label-sm { font-weight: 400; fill: #4a148c; }
.ed-panel { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-top: 1px solid var(--line); }
.ed-panel-head { display: flex; gap: 6px; align-items: center; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.ed-tab { background: none; border: none; font: inherit; font-weight: 600; color: var(--muted); padding: 8px 10px; border-radius: 8px; cursor: pointer; min-height: 36px; }
.ed-tab.on { color: var(--primary); background: var(--primary-soft); }
.ed-dirty { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #ffca28; margin-left: 2px; }
.ed-panel-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px calc(12px + var(--safe-b)); }
.ed-panel-body h3 { margin: 10px 0 6px; font-size: .95rem; }
.ed-field { display: block; margin-bottom: 10px; }
.ed-flabel { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: 3px; }
.ed-field input, .ed-field select { min-height: 40px; padding: 8px 10px; }
.ed-grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 10px; }
.ed-inline { flex-wrap: nowrap; }
.ed-inline input { flex: 1 1 auto; min-width: 0; }
.ed-check { margin: 4px 14px 10px 0; color: var(--text); }
.ed-selhead { margin-bottom: 8px; }
.ed-selhead .badge { margin-left: 8px; }
.ed-conf-l { background: var(--warn-soft); color: var(--warn); }
.ed-conf-m { background: var(--grey-soft); color: var(--muted); }
.ed-conf-h { background: var(--ok-soft); color: var(--ok); }
.ed-chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 4px 0 8px; }
.ed-chip { border: 1px solid var(--line); background: var(--surface-2); color: var(--text); border-radius: 999px; padding: 3px 10px; font: inherit; font-size: .8rem; cursor: pointer; }
.ed-rot { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: end; margin-bottom: 4px; }
.ed-rot .row { margin-bottom: 10px; flex-wrap: nowrap; gap: 4px; }
.ed-rot .row .btn { padding: 6px 8px; min-width: 0; }
.ed-issue { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; }
.ed-issue.on { border-color: #f5b400; box-shadow: 0 0 0 2px rgba(245, 180, 0, .25); }
.ed-issue.done { opacity: .6; }
.ed-issue-main { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; margin-bottom: 4px; }
.ed-issue-msg { font-size: .9rem; }
.ed-issue-mini { font-size: .85rem; padding: 6px 10px; margin-bottom: 6px; }
.ed-sev-warn { background: var(--warn-soft); color: var(--warn); }
.ed-sev-info { background: var(--info-soft); color: var(--info); }
.ed-sev-error { background: var(--danger-soft); color: var(--danger); }
.ed-errs { margin: 4px 0 0; padding-left: 18px; }
@media (min-width: 900px) {
  .ed {
    grid-template-columns: minmax(0, 1fr) 360px; grid-template-rows: auto auto auto minmax(0, 1fr);
    grid-template-areas: "tool tool" "layers layers" "banner banner" "canvas panel";
  }
  .ed-panel { border-top: none; border-left: 1px solid var(--line); }
}
@media (prefers-color-scheme: dark) {
  .ed-canvas { background: #f2f4f6; }
}

/* ---------- 3D 檢視（v1.2） ---------- */
.v3 {
  position: fixed; left: 0; right: 0; bottom: 0; top: 0; z-index: 10;
  display: grid; grid-template-rows: auto auto minmax(0, 1fr); grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "tool" "sub" "stage";
  background: var(--bg);
}
.v3-subbar { grid-area: sub; }
.v3-stage { grid-area: stage; }
.v3-segs { display: inline-flex; flex: 0 0 auto; }
.v3-segs .btn { border-radius: 0; min-width: 52px; }
.v3-segs .btn:first-child { border-radius: 10px 0 0 10px; }
.v3-segs .btn:last-child { border-radius: 0 10px 10px 0; border-left: none; }
.v3-draft { background: var(--warn-soft); color: var(--warn); margin-left: 6px; }
.v3-subbar { background: var(--surface); border-bottom: 1px solid var(--line); padding: 4px 12px; display: flex; flex-direction: column; gap: 2px; }
.v3-subbar:empty { display: none; }
.v3-sun { display: flex; align-items: center; gap: 8px; min-height: 30px; }
.v3-sun-l { font-weight: 600; font-size: .85rem; }
.v3-range { flex: 1 1 auto; min-width: 0; accent-color: #f5a623; }
.v3-time { font-variant-numeric: tabular-nums; font-size: .85rem; min-width: 6.5em; text-align: right; }
.v3-note { font-size: .8rem; color: var(--warn); }
.v3-stage { position: relative; min-height: 0; overflow: hidden; }
.v3-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; cursor: grab; }
.v3-stage.walking .v3-canvas { cursor: crosshair; }
.v3-labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.v3-label {
  position: absolute; left: 0; top: 0; white-space: nowrap; text-align: center; line-height: 1.25;
  color: #1c2630; text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 2px #fff;
}
.v3-label b { display: block; font-size: 13px; }
.v3-label span { display: block; font-size: 11px; }
.v3-info {
  position: absolute; top: 8px; left: 8px; right: 8px; max-width: 340px; z-index: 2;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 12px; box-shadow: 0 4px 16px rgba(0, 0, 0, .15);
}
.v3-x { padding: 0 4px; font-size: 1rem; color: var(--muted); }
.v3-joy {
  position: absolute; left: 18px; bottom: calc(18px + var(--safe-b)); width: 124px; height: 124px; z-index: 2;
  border-radius: 50%; background: rgba(255, 255, 255, .28); border: 2px solid rgba(255, 255, 255, .75);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .25); touch-action: none; user-select: none; -webkit-user-select: none;
  display: flex; align-items: center; justify-content: center;
}
.v3-knob { width: 52px; height: 52px; border-radius: 50%; background: rgba(255, 255, 255, .92); box-shadow: 0 1px 6px rgba(0, 0, 0, .3); pointer-events: none; }
.v3-hint {
  position: absolute; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); z-index: 2;
  background: rgba(0, 0, 0, .65); color: #fff; padding: 6px 12px; border-radius: 10px; font-size: .85rem; white-space: nowrap;
}
.v3-entry { flex-wrap: nowrap; }
@media (min-width: 900px) {
  .v3-joy { display: none; }
  .v3-hint { bottom: 24px; }
}
@media (pointer: coarse) and (min-width: 900px) {
  .v3-joy { display: flex; }
  .v3-joy[hidden] { display: none !important; }
}
