/* 版面：< 900px 是手機（上方列＋底部分頁＋右下＋），>= 900px 是電腦（左側欄） */
:root {
  --bg: #f5f5f4; --card: #ffffff; --text: #1c1917; --muted: #78716c; --line: #e7e5e4; --chip: #f0efed;
  --accent: #0f766e; --accent-text: #ffffff; --danger: #b91c1c; --grid: #ecebe9;
  /* 圖表兩個系列（dataviz 參考色盤第 1、2 位，light／dark 都驗證過） */
  --income: #2a78d6; --expense: #eb6834;
  --side-w: 248px; --tab-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a19; --card: #262524; --text: #f0efec; --muted: #a8a29e; --line: #3d3a37; --chip: #33312f;
    --accent: #14b8a6; --accent-text: #042f2e; --danger: #f87171; --grid: #34322f;
    --income: #3987e5; --expense: #d95926;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1a19; --card: #262524; --text: #f0efec; --muted: #a8a29e; --line: #3d3a37; --chip: #33312f;
  --accent: #14b8a6; --accent-text: #042f2e; --danger: #f87171; --grid: #34322f;
  --income: #3987e5; --expense: #d95926;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h2, h3 { margin: 0; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
button, input, .button { font: inherit; color: inherit; }
button, .button {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 8px 14px; cursor: pointer;
  text-decoration: none; display: inline-block;
}
button:disabled { opacity: .4; cursor: default; }
.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.danger { color: var(--danger); }
button.link { border: none; background: none; padding: 4px 0; color: var(--accent); }
input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
input:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 14px; color: var(--muted); margin: 12px 0; }
label input { margin-top: 4px; font-size: 16px; color: var(--text); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.4em; margin: 4px 0; font-size: 14px; }
.card { background: var(--card); border-radius: 16px; padding: 16px; }
.spacer { flex: 1; }
.r { text-align: right; }

/* ---------- 登入 ---------- */
.login { padding: 0 16px; }
.login-card { max-width: 400px; margin: 18vh auto 0; padding: 24px; }
.login-card h1 { margin: 0 0 8px; font-size: 24px; }
.login-card button { width: 100%; padding: 12px; margin-top: 8px; }

/* ---------- 外框（手機） ---------- */
.side {
  position: sticky; top: 0; z-index: 10; background: var(--bg);
  padding: env(safe-area-inset-top) 16px 0;
}
.side-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; height: 56px; }
.ledger-switch, .user-switch { position: relative; }
.ledger-btn {
  display: flex; align-items: center; gap: 8px; border: none; background: none; padding: 6px 8px 6px 0;
  font-weight: 600; font-size: 18px; max-width: 70vw;
}
.ledger-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { color: var(--muted); font-size: 12px; }
.user-btn {
  width: 36px; height: 36px; border-radius: 50%; padding: 0; background: var(--chip); border: none; font-weight: 600;
}
.popover {
  position: absolute; top: calc(100% + 6px); z-index: 30; background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 6px; min-width: 220px; box-shadow: 0 10px 30px rgba(0,0,0,.18);
  display: flex; flex-direction: column; gap: 2px;
}
.ledger-menu { left: 0; max-height: 70vh; overflow: auto; }
.user-menu { right: 0; min-width: 180px; }
.popover button { border: none; background: none; text-align: left; padding: 9px 10px; border-radius: 8px; }
.popover button:hover { background: var(--chip); }
.popover p { margin: 6px 10px; }
.popover-sep { height: 1px; background: var(--line); margin: 4px 0; }
.popover .current { font-weight: 600; }
#ledger-list { display: flex; flex-direction: column; gap: 2px; }
.popover .current { display: flex; justify-content: space-between; gap: 12px; }
.popover .current::after { content: "✓"; color: var(--accent); }
.popover .group-label { font-size: 12px; color: var(--muted); padding: 8px 10px 2px; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: 1fr 1fr;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 12px;
}
.tabs a span { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: .7; }
.tabs a.active { color: var(--accent); font-weight: 600; }
.tabs a.active span { filter: none; opacity: 1; }
.side-foot { display: none; }

.content {
  padding: 4px 16px calc(var(--tab-h) + 96px + env(safe-area-inset-bottom));
  max-width: 1120px; margin: 0 auto;
}
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 12px; flex-wrap: wrap; }

.fab {
  position: fixed; right: 20px; bottom: calc(var(--tab-h) + 16px + env(safe-area-inset-bottom)); z-index: 15;
  width: 58px; height: 58px; border-radius: 50%; font-size: 30px; padding: 0; line-height: 1;
  background: var(--accent); color: var(--accent-text); border: none; box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.add-inline { display: none; }

/* ---------- 共用元件 ---------- */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--chip); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { border: none; background: transparent; padding: 8px 12px; }
.seg button.active { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg-sm { border-radius: 10px; padding: 3px; }
.seg-sm button { padding: 4px 12px; font-size: 14px; border-radius: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button { border-radius: 999px; padding: 6px 14px; background: var(--chip); border-color: transparent; font-size: 15px; }
.chips button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: 0; }
.key-income { background: var(--income); }
.key-expense { background: var(--expense); }
.empty { text-align: center; margin-top: 40px; }

/* ---------- 儀表板 ---------- */
.period-note { margin: -6px 0 12px; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--card); border-radius: 16px; padding: 14px 16px; }
.tile-label { display: block; font-size: 13px; color: var(--muted); }
.tile strong { display: block; font-size: 24px; font-weight: 650; margin-top: 2px; letter-spacing: -.01em; overflow-wrap: anywhere; }
.empty-card { margin-top: 12px; text-align: center; padding: 28px 16px; }
.empty-card p { margin: 0 0 12px; }
.dash-grid { display: grid; gap: 12px; margin-top: 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.num-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.num-table th { font-weight: 500; color: var(--muted); font-size: 13px; text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.num-table td { text-align: right; padding: 8px 4px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.num-table th:first-child, .num-table td:first-child { text-align: left; }
.num-table tr:last-child td { border-bottom: none; }
.num-table tfoot td { font-weight: 600; border-top: 1px solid var(--line); }
.bars { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.bars li { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 14px; }
.bars .bar-track { grid-column: 1 / -1; height: 8px; border-radius: 4px; background: var(--chip); overflow: hidden; }
.bars .bar-fill { height: 100%; border-radius: 4px; }
.bars .v { font-variant-numeric: tabular-nums; }
.bars .pct { color: var(--muted); margin-left: 6px; font-size: 13px; }

/* ---------- 記帳 ---------- */
.month-sum { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--card); border-radius: 16px; padding: 12px 8px; text-align: center; }
.month-sum span { display: block; font-size: 13px; }
.month-sum strong { font-size: 18px; font-variant-numeric: tabular-nums; }
#party-filter { margin: 12px 0 4px; }
.rec-head { display: none; }
.day { margin-top: 16px; }
.day-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); padding: 0 4px 6px; }
.day ul { list-style: none; margin: 0; padding: 0; background: var(--card); border-radius: 14px; overflow: hidden; }
.tx {
  display: grid; grid-template-columns: 32px 1fr auto; grid-template-areas: "ic main amt"; align-items: center;
  gap: 4px 12px; padding: 12px 14px; border-top: 1px solid var(--line); cursor: pointer;
}
.tx:first-child { border-top: none; }
.tx:hover { background: var(--chip); }
.tx .ic { grid-area: ic; font-size: 22px; text-align: center; }
.tx .main { grid-area: main; min-width: 0; }
.tx .cat { font-weight: 500; }
.tx .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx .party, .tx .note, .tx .who { display: none; }
.tx .amt { grid-area: amt; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.tx .amt i { margin-right: 6px; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--chip); color: var(--text); margin-right: 6px; }

/* ---------- 對話框 ---------- */
dialog {
  border: none; border-radius: 18px; padding: 20px; width: min(560px, calc(100vw - 32px));
  background: var(--card); color: var(--text); max-height: calc(100dvh - 32px); overflow: auto;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin-bottom: 12px; font-size: 18px; }
.form-row { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
.amount-label input { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; }
.field { margin: 14px 0; }
.field-label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.field-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.field-head .field-label { margin: 0; }
.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.cat-grid button { padding: 8px 4px; background: var(--chip); border-color: transparent; font-size: 14px; }
.cat-grid button.active { border-color: var(--accent); background: var(--card); font-weight: 600; box-shadow: inset 0 0 0 1px var(--accent); }
.actions { display: flex; gap: 8px; margin-top: 12px; align-items: center; }
.edit-list, .manage { list-style: none; margin: 0 0 6px; padding: 0; }
.edit-list li, .manage li { display: flex; gap: 6px; align-items: center; padding: 4px 0; }
.manage li { border-top: 1px solid var(--line); padding: 6px 0; }
.manage li:first-child { border-top: none; }
.manage li.archived input { opacity: .5; }
.manage input, .edit-list input { padding: 7px 10px; }
.manage button, .edit-list button { padding: 6px 9px; font-size: 13px; flex: none; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; }
label.check input { width: auto; margin: 0; }
.icon-input { width: 52px !important; flex: none; text-align: center; }
.manage-add { display: flex; gap: 6px; }
.manage-add button { flex: none; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.onboard { max-width: 520px; margin: 10vh auto 0; text-align: center; padding: 28px 20px; }
.onboard p { margin: 10px 0 18px; }

/* 手機：對話框從底部滑上來 */
@media (max-width: 899px) {
  dialog {
    width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 18px 18px 0 0;
    padding-bottom: calc(20px + env(safe-area-inset-bottom)); max-height: 92dvh;
  }
}

/* ---------- 電腦 ---------- */
@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: var(--side-w) 1fr; min-height: 100vh; }
  .side {
    height: 100vh; padding: 16px 12px; display: flex; flex-direction: column; gap: 8px;
    background: var(--card); border-right: 1px solid var(--line);
  }
  .side-top { height: auto; }
  .ledger-btn { max-width: 180px; padding: 8px; border-radius: 10px; font-size: 16px; }
  .ledger-btn:hover { background: var(--chip); }
  .user-switch { display: none; }
  .tabs {
    position: static; height: auto; padding: 0; display: flex; flex-direction: column; gap: 2px;
    background: none; border: none; margin-top: 8px;
  }
  .tabs a { flex-direction: row; justify-content: flex-start; gap: 10px; padding: 10px 12px; border-radius: 10px; font-size: 15px; color: var(--text); }
  .tabs a span { font-size: 18px; }
  .tabs a:hover { background: var(--chip); }
  .tabs a.active { background: var(--chip); color: var(--text); }
  .side-foot { display: flex; margin-top: auto; align-items: center; justify-content: space-between; padding: 8px; }
  .content { padding: 24px 32px 48px; width: 100%; }
  h2 { font-size: 24px; }
  .fab { display: none; }
  .add-inline { display: inline-block; }

  .tiles { grid-template-columns: repeat(4, 1fr); gap: 12px; }
  .tile strong { font-size: 28px; }
  .dash-grid { grid-template-columns: 1fr 1fr; }

  .records { background: var(--card); border-radius: 16px; overflow: hidden; margin-top: 12px; }
  .rec-head, .tx { grid-template-columns: 200px 120px 1fr 100px 130px; }
  .rec-head {
    display: grid; gap: 12px; padding: 10px 18px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line);
  }
  .day { margin: 0; }
  .day-head { background: var(--bg); padding: 6px 18px; border-bottom: 1px solid var(--line); }
  .day ul { border-radius: 0; background: none; }
  .tx { grid-template-areas: none; gap: 12px; padding: 10px 18px; }
  .tx .ic { display: none; }
  .tx .cat::before { content: attr(data-icon); margin-right: 8px; }
  .tx .main, .tx .amt { grid-area: auto; }
  .tx .sub { display: none; }
  .tx .party, .tx .note, .tx .who { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tx .note { color: var(--text); }
  .tx .who { color: var(--muted); font-size: 14px; }
  .tx .amt { text-align: right; }
  .form-row { grid-template-columns: 1fr 1fr; }
  .empty { margin: 32px 0; }
}
