/* 家庭財務中控台 — 手機優先 */
:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --blue: #2a78d6;
  --red: #e34948;
  --good: #0ca30c;
  --good-text: #006300;
  --critical: #d03b3b;
  --warning: #fab219;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink);
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

#app { max-width: 560px; margin: 0 auto; padding-bottom: 84px; }

header.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 8px;
}
header.topbar h1 { font-size: 20px; margin: 0; font-weight: 700; }
header.topbar .who { color: var(--ink-2); font-size: 14px; }
header.topbar button { background: none; border: none; font-size: 20px; cursor: pointer; padding: 4px; }

.view { padding: 0 16px; }

.card {
  background: var(--surface);
  border: none;
  box-shadow: 0 1px 2px rgba(11, 11, 11, 0.05), 0 2px 8px rgba(11, 11, 11, 0.05);
  border-radius: 16px;
  padding: 18px;
  margin-bottom: 14px;
}
.card h2 {
  font-size: 13px; margin: 0 0 12px; color: var(--muted);
  font-weight: 700; letter-spacing: 0.04em;
}
.card h2 .hint { font-weight: 400; font-size: 12px; color: var(--muted); opacity: 0.8; }

/* 主卡：深藍漸層 */
.card.hero {
  background: linear-gradient(135deg, #0d366b 0%, #1c5cab 55%, #2a78d6 100%);
  color: #fff;
  padding: 22px 20px;
}
.hero .label { font-size: 13px; color: rgba(255, 255, 255, 0.75); }
.hero .num { font-size: 36px; font-weight: 700; margin: 4px 0 2px; letter-spacing: -0.01em; }
.hero .sub { font-size: 12px; color: rgba(255, 255, 255, 0.6); }
.hero .split { display: flex; gap: 12px; margin-top: 14px; font-size: 13px; flex-wrap: wrap; }
.hero .split > div {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 10px; padding: 8px 14px;
  color: rgba(255, 255, 255, 0.85);
}
.hero .split .v { font-weight: 700; color: #fff; display: block; font-size: 16px; margin-top: 2px; }
.hero .split .v.neg { color: #ffb4b3; }
.hero .split .v.pos-good { color: #8fe89a; }
.hero .btnrow button.ghost {
  border-color: rgba(255, 255, 255, 0.4); color: #fff; background: rgba(255, 255, 255, 0.08);
}
.hero .btnrow { justify-content: flex-start !important; }

/* T 字帳資產負債表 */
.bs-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 22px; }
.bs-col:last-child { border-left: 1px solid var(--grid); padding-left: 22px; }
.bs-head {
  font-size: 12px; font-weight: 700; color: var(--ink-2);
  padding-bottom: 6px; border-bottom: 2px solid var(--baseline); margin-bottom: 4px;
}
.bs-sub { font-size: 11px; color: var(--muted); margin-top: 10px; }
.bs-grid .row { padding: 8px 0; font-size: 14px; }
.bs-total { border-top: 2px solid var(--baseline); font-weight: 700; }
.bs-networth { background: rgba(42, 120, 214, 0.08); border-radius: 10px; padding: 10px 12px !important; margin-top: 10px; }
@media (max-width: 420px) {
  .bs-grid { grid-template-columns: 1fr; }
  .bs-col:last-child { border-left: none; padding-left: 0; margin-top: 14px; }
}

.row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid var(--grid);
  gap: 10px;
}
.row:last-child { border-bottom: none; }
.row .name { flex: 1; min-width: 0; }
.row .name .t { font-weight: 500; }
.row .name .s { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .val { text-align: right; font-variant-numeric: tabular-nums; }
.row .val .s { font-size: 12px; color: var(--muted); }
.row.clickable { cursor: pointer; }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; flex: none; }

.neg { color: var(--critical); }
.pos-good { color: var(--good-text); }

button.primary, button.ghost, button.danger {
  border-radius: 10px; padding: 10px 16px; font-size: 15px; cursor: pointer; font-family: inherit;
}
button.primary { background: var(--blue); color: #fff; border: none; }
button.ghost { background: none; border: 1px solid var(--baseline); color: var(--ink); }
button.danger { background: none; border: 1px solid var(--critical); color: var(--critical); }
button.small { padding: 6px 12px; font-size: 13px; }
button:disabled { opacity: 0.5; }
.btnrow { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* 底部導覽 */
nav.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; justify-content: space-around;
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
  max-width: 100%; z-index: 10;
}
nav.tabbar button {
  background: none; border: none; font-size: 11px; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  cursor: pointer; padding: 4px 10px; font-family: inherit;
}
nav.tabbar button .ico { font-size: 20px; }
nav.tabbar button.active { color: var(--blue); font-weight: 600; }
nav.tabbar .brand { display: none; }

/* 表單 */
dialog {
  border: none; border-radius: var(--radius); padding: 0;
  width: min(92vw, 420px); max-height: 85vh;
}
dialog::backdrop { background: rgba(0,0,0,0.35); }
dialog .dlg { padding: 18px; overflow-y: auto; max-height: 85vh; }
dialog h3 { margin: 0 0 14px; font-size: 17px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px; border: 1px solid var(--baseline); border-radius: 10px;
  font-size: 16px; font-family: inherit; background: #fff; color: var(--ink);
}
.field .note { font-size: 12px; color: var(--muted); margin-top: 3px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button {
  border: 1px solid var(--baseline); background: none; border-radius: 999px;
  padding: 7px 14px; font-size: 14px; cursor: pointer; font-family: inherit; color: var(--ink);
}
.chips button.sel { background: var(--blue); color: #fff; border-color: var(--blue); }

.seg {
  display: flex; background: rgba(11, 11, 11, 0.05);
  border-radius: 999px; padding: 3px; gap: 2px;
}
.seg button {
  flex: 1; border: none; background: none; padding: 8px 10px; font-size: 14px;
  cursor: pointer; font-family: inherit; color: var(--ink-2); border-radius: 999px;
}
.seg button.sel {
  background: var(--surface); color: var(--ink); font-weight: 600;
  box-shadow: 0 1px 3px rgba(11, 11, 11, 0.15);
}

/* 預算條 */
.meter { height: 8px; background: var(--grid); border-radius: 4px; overflow: hidden; margin-top: 6px; }
.meter > i { display: block; height: 100%; background: var(--blue); border-radius: 4px; }
.meter > i.over { background: var(--critical); }
.budget-top { display: flex; justify-content: space-between; font-size: 14px; }
.budget-top .over-tag { color: var(--critical); font-weight: 600; font-size: 12px; }

/* 現金流圖 */
.chart-wrap { overflow-x: auto; }
.cashflow-chart { display: block; }
.chart-note { font-size: 12px; color: var(--muted); margin-top: 6px; }

table.simple { width: 100%; border-collapse: collapse; font-size: 13px; }
table.simple th, table.simple td { padding: 6px 4px; text-align: right; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
table.simple th { color: var(--muted); font-weight: 500; }
table.simple th:first-child, table.simple td:first-child { text-align: left; }
.table-scroll { overflow-x: auto; }

/* 登入 */
#login-view { padding: 60px 24px; max-width: 400px; margin: 0 auto; }
#login-view h1 { text-align: center; }
#login-view .err { color: var(--critical); font-size: 14px; min-height: 20px; }

.empty { color: var(--muted); font-size: 14px; text-align: center; padding: 16px 0; }
.toast {
  position: fixed; top: 14px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 99; opacity: 0; transition: opacity .2s; pointer-events: none;
  max-width: 90vw;
}
.toast.show { opacity: 1; }

/* ===== 電腦版（≥900px）：側邊導覽＋12 欄網格儀表板 ===== */
@media (min-width: 900px) {
  #app { max-width: 1200px; padding-left: 170px; padding-right: 20px; padding-bottom: 24px; }
  nav.tabbar {
    left: 0; top: 0; bottom: 0; right: auto; width: 150px;
    flex-direction: column; justify-content: flex-start; gap: 6px;
    padding: 28px 0 0; border-top: none; border-right: 1px solid var(--border);
  }
  nav.tabbar button {
    flex-direction: row; gap: 10px; font-size: 15px;
    width: 100%; padding: 12px 22px; justify-content: flex-start;
    border-radius: 0;
  }
  nav.tabbar button.active { background: var(--page); border-right: 3px solid var(--blue); }
  nav.tabbar .brand {
    display: block; font-size: 15px; font-weight: 700;
    padding: 0 22px 18px; color: var(--ink);
  }
  header.topbar { padding: 22px 16px 14px; }
  header.topbar h1 { font-size: 24px; }
  .hero-main { display: flex; align-items: center; gap: 36px; flex-wrap: wrap; }
  .hero-left { flex: 1 1 46%; min-width: 260px; }
  .hero-chart { flex: 1 1 46%; min-width: 320px; }
  .hero .split { margin-top: 18px; }
  .hero .split > div { flex: 1; max-width: 180px; }

  .view {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 14px;
    align-items: start;
    grid-auto-flow: row dense;
  }
  .view[hidden] { display: none !important; }
  .view > * { grid-column: span 12; }
  .view > .card { margin-bottom: 0; }
  .view > .stretch { align-self: stretch; }
  .view > .span-4 { grid-column: span 4; }
  .view > .span-5 { grid-column: span 5; }
  .view > .span-6 { grid-column: span 6; }
  .view > .span-7 { grid-column: span 7; }
  .view > .span-8 { grid-column: span 8; }
  .view > .seg { max-width: 480px; }
  .hero .num { font-size: 42px; }
  .card.hero .split { gap: 48px; }
  dialog { width: min(90vw, 500px); }
  .empty-note { display: none; }
}
@media (min-width: 1400px) {
  #app { max-width: 1360px; }
}

/* 桌面限定區塊（預算矩陣等） */
.desktop-only { display: none; }
@media (min-width: 900px) { .desktop-only { display: block; } }

/* 年度預算規劃矩陣 */
.matrix-scroll { overflow-x: auto; }
table.matrix { border-collapse: collapse; font-size: 13px; min-width: 1000px; }
table.matrix th, table.matrix td {
  padding: 5px 4px; text-align: right; border-bottom: 1px solid var(--grid);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
table.matrix th { color: var(--muted); font-weight: 500; font-size: 12px; }
table.matrix th:first-child, table.matrix td:first-child {
  text-align: left; position: sticky; left: 0; background: var(--surface); min-width: 120px;
  padding-right: 10px;
}
table.matrix tr.group td {
  font-weight: 700; background: rgba(11, 11, 11, 0.03); cursor: pointer;
}
table.matrix tr.group td:first-child { background: #f2f2ef; }
table.matrix td input {
  width: 64px; border: none; background: none; text-align: right; font: inherit;
  color: var(--ink); padding: 2px; border-radius: 4px;
}
table.matrix td input:hover { background: rgba(42, 120, 214, 0.07); }
table.matrix td input:focus { outline: 2px solid var(--blue); background: #fff; }
table.matrix tr.net-row td { font-weight: 700; border-top: 2px solid var(--baseline); }
table.matrix .del-item { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 12px; padding: 0 4px; }
table.matrix .del-item:hover { color: var(--critical); }

/* 信用卡卡面示意 */
.cc-face {
  width: 100%; max-width: 340px; aspect-ratio: 1.586;
  border-radius: 14px; padding: 16px 18px; margin-bottom: 14px;
  color: #fff; position: relative; overflow: hidden;
  display: flex; flex-direction: column; justify-content: space-between;
  box-shadow: 0 4px 14px rgba(11, 11, 11, 0.25);
}
.cc-face::after {
  content: ''; position: absolute; right: -30px; top: -40px;
  width: 160px; height: 160px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
}
.cc-face .cc-bank { font-size: 15px; font-weight: 700; letter-spacing: 0.02em; }
.cc-face .cc-chip {
  width: 38px; height: 28px; border-radius: 6px;
  background: linear-gradient(135deg, #e6c96a, #b8933a);
  position: relative;
}
.cc-face .cc-chip::after {
  content: ''; position: absolute; inset: 5px 8px;
  border: 1px solid rgba(0, 0, 0, 0.25); border-radius: 3px;
}
.cc-face .cc-num { font-size: 15px; letter-spacing: 3px; opacity: 0.85; font-variant-numeric: tabular-nums; }
.cc-face .cc-bottom { display: flex; justify-content: space-between; align-items: flex-end; font-size: 12px; }
.cc-face .cc-holder { text-transform: uppercase; letter-spacing: 0.06em; opacity: 0.9; }
.cc-face .cc-meta { text-align: right; opacity: 0.75; font-size: 11px; line-height: 1.5; }

/* 圖表通用 */
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-2); margin-bottom: 6px; flex-wrap: wrap; }
.legend span { display: flex; align-items: center; gap: 5px; }
.legend i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.legend i.sq { width: 10px; height: 10px; border-radius: 3px; }
svg.chart { width: 100%; height: auto; display: block; }

/* 圖表滑鼠浮動提示 */
#chart-tip {
  position: fixed; z-index: 999; display: none; pointer-events: none;
  background: rgba(11, 11, 11, 0.92); color: #fff;
  padding: 8px 12px; border-radius: 8px;
  font-size: 12.5px; line-height: 1.6; white-space: pre-line;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  font-variant-numeric: tabular-nums;
  max-width: 280px;
}
