:root, [data-bs-theme="dark"] {
  --bg: #070b18; --panel: rgba(22, 30, 56, .72); --line: rgba(148, 170, 255, .16);
  --text: #eef2ff; --muted: #99a5c9; --glow: rgba(88, 101, 242, .28); --nav: rgba(11, 16, 34, .94);
}
[data-bs-theme="light"] {
  --bg: #eef1fb; --panel: rgba(255, 255, 255, .86); --line: rgba(40, 60, 140, .13);
  --text: #141b34; --muted: #596387; --glow: rgba(88, 101, 242, .14); --nav: rgba(255, 255, 255, .96);
}
[hidden] { display: none !important; }
body {
  margin: 0; min-height: 100vh; min-height: 100dvh; color: var(--text); background-color: var(--bg);
  background-image: radial-gradient(60vmax 50vmax at 85% -10%, var(--glow), transparent 65%),
    radial-gradient(50vmax 45vmax at -10% 105%, rgba(34, 211, 238, .13), transparent 65%);
  background-attachment: fixed; font-variant-numeric: tabular-nums; -webkit-tap-highlight-color: transparent;
}
[data-bs-theme="light"] .btn-outline-light { --bs-btn-color: #28305a; --bs-btn-border-color: #9aa3c7; }
.hint { color: var(--muted); font-size: .8rem; margin-bottom: .25rem; }
.btn { min-height: 38px; border-radius: .75rem; }
.btn-gradient { color: #08101f; font-weight: 700; border: 0; background: linear-gradient(110deg, #5eead4, #4cc9f0 45%, #b388ff); }
.btn-gradient:hover, .btn-gradient:focus-visible { color: #08101f; filter: brightness(1.08); }
.btn-gradient:disabled { color: #08101f; opacity: .45; background: linear-gradient(110deg, #5eead4, #b388ff); }
.form-control, .form-select { font-size: 16px; min-height: 44px; border-radius: .75rem; }

/* ===== 版面：手機直立（預設） ===== */
.app-body { padding: 0 max(12px, env(safe-area-inset-right)) calc(92px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); max-width: 1640px; margin: 0 auto; }
.topbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; justify-content: space-between; padding: calc(14px + env(safe-area-inset-top)) 0 12px; }
.topbar h1 { font-size: 1.35rem; font-weight: 800; margin: 0 .5rem 0 0; display: inline-block; vertical-align: middle; }
.app-nav {
  position: fixed; z-index: 1030; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); background: var(--nav); border-top: 1px solid var(--line); backdrop-filter: blur(16px);
}
.brand { display: none; }
.nav-btn { border: 0; background: none; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-height: 50px; border-radius: 14px; font-size: .72rem; }
.nav-btn i { font-style: normal; font-size: 1.3rem; line-height: 1.2; }
.nav-btn.active { color: var(--text); background: rgba(124, 140, 255, .2); }
.nav-add i { width: 32px; height: 32px; line-height: 32px; border-radius: 50%; color: #08101f; background: linear-gradient(120deg, #5eead4, #b388ff); font-weight: 800; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 16px; box-shadow: 0 18px 40px -28px #000; backdrop-filter: blur(14px); min-width: 0; }
.panel h2 { font-size: .98rem; font-weight: 700; margin-bottom: .75rem; }
.panel h2 small { font-weight: 400; font-size: .74rem; color: var(--muted); margin-left: .35rem; }

/* ===== 儀表板 ===== */
.dash-grid { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); grid-template-areas: "hero" "score" "minis" "tips" "donut" "bars" "ccy"; }
.hero { grid-area: hero; position: relative; overflow: hidden; background-image: linear-gradient(135deg, rgba(76, 201, 240, .22), rgba(179, 136, 255, .2) 60%, transparent); display: flex; flex-direction: column; justify-content: center; }
.score { grid-area: score; text-align: center; }
.minis { grid-area: minis; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; align-content: center; text-align: center; }
.minis p { margin: 0; font-size: .85rem; font-weight: 600; }
.minis small { display: block; color: var(--muted); font-weight: 400; font-size: .68rem; }
.donut-panel { grid-area: donut; } .bars-panel { grid-area: bars; } .ccy-panel { grid-area: ccy; } .tips-panel { grid-area: tips; }
.label { color: var(--muted); margin: 0; font-size: .85rem; }
.hero-value { font-size: clamp(1.9rem, 8.5vw, 3.1rem); font-weight: 800; letter-spacing: -.02em; margin: .15rem 0 .8rem; overflow-wrap: anywhere; }
.split-bar { display: flex; height: 10px; border-radius: 99px; overflow: hidden; background: var(--line); margin-bottom: .7rem; }
.split-bar i { transition: width .8s; }
.c0 { background: #4cc9f0; } .c1 { background: #b388ff; }
.hero-split { display: flex; flex-wrap: wrap; gap: .4rem 1.4rem; font-size: .9rem; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: .4rem; flex: none; }

.gauge { width: 100%; max-width: 150px; display: block; margin: 0 auto; overflow: visible; }
.gauge.big { max-width: 270px; }
.gauge .track, .gauge .arc { fill: none; stroke-width: 15; stroke-linecap: round; }
.gauge .track { stroke: var(--line); }
.gauge .arc { transition: stroke-dashoffset 1.2s cubic-bezier(.2, .8, .2, 1); filter: drop-shadow(0 0 5px rgba(94, 234, 212, .5)); }
.gauge .tick { stroke: var(--muted); stroke-width: 1.2; opacity: .55; }
.gauge .needle { fill: var(--text); transform-origin: 100px 100px; transition: transform 1.2s cubic-bezier(.2, .8, .2, 1); }
.gauge-num { fill: var(--text); text-anchor: middle; font-size: 38px; font-weight: 800; }
.gauge.big .gauge-num { font-size: 40px; }

.donut-wrap { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 14px; }
#donut { width: min(190px, 60vw); }
.donut-track, .donut-seg { fill: none; stroke-width: 5; }
.donut-track { stroke: var(--line); }
.donut-num { fill: var(--text); font-size: 5px; font-weight: 700; text-anchor: middle; }
.legend, .plain, .checklist { list-style: none; padding: 0; margin: 0; }
.legend { flex: 1 1 170px; font-size: .84rem; }
.legend li { display: flex; align-items: center; gap: .2rem; padding: .18rem 0; }
.legend .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar { margin-bottom: .65rem; font-size: .83rem; }
.hbar-head { display: flex; justify-content: space-between; gap: .5rem; margin-bottom: .2rem; }
.hbar-track { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.hbar-track i { display: block; height: 100%; border-radius: 99px; }
.plain li b { white-space: nowrap; }
.plain li { display: flex; justify-content: space-between; gap: .5rem; font-size: .82rem; padding: .3rem 0; border-bottom: 1px solid var(--line); }
.tip { border-left: 3px solid #4cc9f0; background: rgba(124, 140, 255, .09); border-radius: 0 12px 12px 0; padding: .5rem .75rem; margin-bottom: .5rem; font-size: .85rem; }
.tip b { display: block; } .tip span { color: var(--muted); }
.tip.warn { border-color: #ffb020; } .tip.good { border-color: #37d9a0; }

/* ===== 資產清單 ===== */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.toolbar input { flex: 1 1 200px; width: auto; } .toolbar select { flex: 0 1 190px; width: auto; }
.asset-list { display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }
.asset { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px; text-align: left; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 12px; transition: transform .15s, border-color .15s; }
.asset:hover, .asset:focus-visible { border-color: #7c8cff; transform: translateY(-2px); }
.avatar { width: 40px; height: 40px; border-radius: 13px; display: grid; place-items: center; font-weight: 800; color: #08101f; background: linear-gradient(135deg, #5eead4, #b388ff); }
.who, .amount, .cell { display: flex; flex-direction: column; min-width: 0; }
.who b, .who small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.asset small { color: var(--muted); font-size: .72rem; }
.amount { align-items: flex-end; }
.cell { display: none; align-items: flex-end; }

/* ===== 新增 / AI / 設定 ===== */
.seg { display: flex; gap: 4px; padding: 4px; margin: 0 auto 12px; max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; }
.seg button { flex: 1; min-height: 40px; border: 0; border-radius: 12px; background: none; color: var(--muted); }
.seg button.active { background: rgba(124, 140, 255, .28); color: var(--text); font-weight: 700; }
.narrow { max-width: 720px; margin: 0 auto; }
.photo-grid { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); align-items: start; }
#redactCanvas { display: block; max-width: 100%; max-height: 62vh; margin: 0 auto; border-radius: 12px; border: 1px dashed var(--muted); touch-action: none; cursor: crosshair; }
.draft { border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 10px; }
#payload { max-height: 320px; overflow: auto; font-size: .75rem; background: rgba(0, 0, 0, .22); border-radius: 12px; padding: .75rem; white-space: pre-wrap; }
.ai-result { white-space: pre-wrap; line-height: 1.75; }
.checklist li { padding: .35rem 0 .35rem 1.4rem; position: relative; font-size: .86rem; }
.checklist li::before { content: "✓"; position: absolute; left: 0; color: #37d9a0; font-weight: 800; }

.diff > div { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .25rem .75rem; padding: .45rem 0; border-bottom: 1px solid var(--line); font-size: .88rem; }
.diff del { color: var(--muted); } .diff ins { text-decoration: none; font-weight: 700; color: #20b988; overflow-wrap: anywhere; }
.offcanvas.sheet { --bs-offcanvas-width: min(430px, 100vw); }
.offcanvas-title, .modal-title { font-size: 1.1rem; }

.private .money, .private .diff del, .private #payload { filter: blur(9px); user-select: none; }
.lock, .veil { position: fixed; inset: 0; z-index: 2000; background: var(--bg); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem; padding: 2rem; text-align: center; }
.veil { z-index: 2100; font-size: 1.6rem; font-weight: 800; }
.toast-zone { position: fixed; z-index: 1900; left: 12px; right: 12px; bottom: calc(86px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.note { max-width: 520px; padding: .65rem 1rem; border-radius: 14px; background: #28325c; color: #fff; box-shadow: 0 10px 30px -12px #000; font-size: .88rem; overflow-wrap: anywhere; }
.note.success { background: #0f6b50; } .note.error { background: #99253f; }

/* 手機直立：編輯面板改成底部抽屜 */
@media (max-width: 599.98px) {
  .offcanvas.offcanvas-end.sheet { top: auto; left: 0; width: 100%; height: 88dvh; border-radius: 24px 24px 0 0; border: 1px solid var(--line); transform: translateY(100%); }
  .offcanvas.offcanvas-end.sheet.showing, .offcanvas.offcanvas-end.sheet.show:not(.hiding) { transform: none; }
}

/* ===== 摺疊機展開 / 平板（600px 以上）：左側窄欄 + 雙欄 ===== */
@media (min-width: 600px) {
  .app { display: grid; grid-template-columns: 82px minmax(0, 1fr); }
  .app-nav { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 6px; border-top: 0; border-right: 1px solid var(--line); padding: calc(12px + env(safe-area-inset-top)) 8px 12px max(8px, env(safe-area-inset-left)); overflow-y: auto; }
  .brand { display: flex; justify-content: center; margin-bottom: 8px; }
  .brand img { border-radius: 10px; } .brand span { display: none; }
  .nav-btn { min-height: 60px; flex: none; }
  .app-body { width: 100%; padding: 0 16px 28px; }
  .toast-zone { bottom: 20px; }
  .dash-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-areas: "hero hero" "score minis" "donut bars" "tips ccy"; }
  .minis { grid-template-columns: 1fr; }
  .minis > div { display: grid; grid-template-columns: 82px 1fr; align-items: center; text-align: left; }
  .asset-list { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
}
@media (min-width: 820px) {
  .photo-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ===== 桌面：完整側欄 + 12 欄儀表板 + 表格式清單 ===== */
@media (min-width: 1200px) {
  .app { grid-template-columns: 220px minmax(0, 1fr); }
  .brand { justify-content: flex-start; align-items: center; gap: .6rem; padding: 0 8px 12px; font-weight: 800; font-size: 1.15rem; }
  .brand span { display: inline; }
  .nav-btn { flex-direction: row; justify-content: flex-start; gap: .75rem; min-height: 48px; padding: 0 14px; font-size: .95rem; }
  .nav-btn i { width: 32px; text-align: center; }
  .app-body { padding: 0 28px 32px; }
  .dash-grid { gap: 16px; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-template-areas:
    "hero hero hero hero hero score score score score minis minis minis"
    "donut donut donut donut donut bars bars bars bars ccy ccy ccy"
    "tips tips tips tips tips tips tips tips tips tips tips tips"; }
  .tips-panel #tips { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0 12px; }
  .asset-list { grid-template-columns: minmax(0, 1fr); }
  .asset { grid-template-columns: auto minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr) minmax(190px, 1fr); }
  .cell { display: flex; }
  .photo-grid:has(> :nth-child(3)) { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* 摺疊機雙區段（支援 Viewport Segments 的瀏覽器）：避開中間摺線 */
@media (horizontal-viewport-segments: 2) {
  .dash-grid, .photo-grid { column-gap: 32px; }
}
/* 手機橫向：壓低高度 */
@media (orientation: landscape) and (max-height: 500px) and (min-width: 600px) {
  .brand { display: none; }
  .nav-btn { min-height: 44px; }
  .nav-btn span { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
