/* 極簡黑白 + 蜜柑橘。淺色為主，深色自動跟隨系統。 */

:root {
  --bg:        #ffffff;
  --surface:   #ffffff;
  --raised:    #fafafa;
  --text:      #111111;
  --muted:     #737373;
  --faint:     #a3a3a3;
  --line:      #e5e5e5;
  --line-soft: #f0f0f0;

  --accent:      #e8590c;
  /* 白字壓在橘底上時專用的深一階橘。
     #e8590c 配白字對比只有 3.58，小字級過不了 WCAG AA；#cc4d0a 是 4.53，
     肉眼幾乎看不出差別。大數字、FAB、進度條仍用原本的 --accent。 */
  --accent-ink:  #cc4d0a;
  --accent-weak: #fff1e9;
  --danger:      #d4351c;
  --danger-weak: #fdeceb;
  --ok:          #00875a;

  --r: 10px;
  --tap: 48px;
  --safe-b: env(safe-area-inset-bottom, 0px);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #0a0a0a;
    --surface:   #0a0a0a;
    --raised:    #171717;
    --text:      #fafafa;
    --muted:     #a3a3a3;
    --faint:     #6b6b6b;
    --line:      #262626;
    --line-soft: #1c1c1c;
    --accent-weak: #2a1408;
    --danger-weak: #2a0f0d;
  }
}

:root[data-theme="dark"] {
  --bg:        #0a0a0a;
  --surface:   #0a0a0a;
  --raised:    #171717;
  --text:      #fafafa;
  --muted:     #a3a3a3;
  --faint:     #6b6b6b;
  --line:      #262626;
  --line-soft: #1c1c1c;
  --accent-weak: #2a1408;
  --danger-weak: #2a0f0d;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior-y: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; padding: 0; }

.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

.hidden { display: none !important; }

/* ---------------------------------------------------------------- 版面 */

#app { min-height: 100%; display: flex; flex-direction: column; }

.screen {
  flex: 1;
  padding-bottom: calc(76px + var(--safe-b));
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  border-bottom: 1px solid var(--line-soft);
}

/* 內容跟 .screen 對齊在同一個 720px 欄位裡，底線才鋪滿整個寬度 */
.topbar-inner {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px 12px;
  max-width: 720px; margin: 0 auto;
}

.topbar h1 { margin: 0; font-size: 19px; font-weight: 650; letter-spacing: -0.01em; }
.topbar .spacer { flex: 1; }

.sync-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--ok); flex: none;
  transition: background .2s;
}
.sync-dot[data-state="syncing"] { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.sync-dot[data-state="offline"] { background: var(--faint); }
.sync-dot[data-state="error"],
.sync-dot[data-state="unauthed"] { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.sync-label { font-size: 12px; color: var(--muted); }

/* ---------------------------------------------------------------- 首頁 */

.hero { padding: 26px 18px 22px; border-bottom: 1px solid var(--line); }
.hero .label { font-size: 13px; color: var(--muted); letter-spacing: .02em; }
.hero .primary {
  font-size: 44px; font-weight: 700; letter-spacing: -0.03em;
  color: var(--accent); margin-top: 4px; line-height: 1.1;
}
.hero .secondary { font-size: 15px; color: var(--muted); margin-top: 2px; }

.statgrid {
  display: grid; grid-template-columns: 1fr 1fr;
  border-bottom: 1px solid var(--line);
}
.stat { padding: 16px 18px; border-right: 1px solid var(--line-soft); border-bottom: 1px solid var(--line-soft); }
.stat:nth-child(2n) { border-right: 0; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 21px; font-weight: 650; letter-spacing: -0.02em; margin-top: 2px; }
.stat .sub { font-size: 12px; color: var(--faint); }

.section-head {
  display: flex; align-items: baseline; gap: 8px;
  padding: 22px 18px 8px;
}
.section-head h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); letter-spacing: .04em; }
.section-head .more { margin-left: auto; font-size: 13px; color: var(--accent); }

/* ---------------------------------------------------------------- 明細列表 */

.daygroup { border-bottom: 1px solid var(--line); }
.dayhead {
  display: flex; align-items: baseline; gap: 8px;
  padding: 14px 18px 6px;
  background: var(--bg);
}
.dayhead .d { font-size: 13px; font-weight: 600; color: var(--muted); }
.dayhead .t { margin-left: auto; font-size: 13px; color: var(--faint); }

.entry {
  display: flex; align-items: center; gap: 13px;
  padding: 12px 18px;
  width: 100%; text-align: left;
  border-top: 1px solid var(--line-soft);
}
.entry:active { background: var(--raised); }
.entry .icon { font-size: 21px; width: 28px; text-align: center; flex: none; }
.entry .body { flex: 1; min-width: 0; }
.entry .name { font-size: 15px; font-weight: 500; }
.entry .meta { font-size: 12px; color: var(--faint); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .amt { text-align: right; flex: none; }
.entry .amt .a { font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.entry .amt .twd { font-size: 12px; color: var(--faint); }
.entry.income .amt .a { color: var(--ok); }

.empty { padding: 60px 24px; text-align: center; color: var(--faint); }
.empty .big { font-size: 34px; margin-bottom: 10px; }
.empty p { margin: 0; font-size: 14px; line-height: 1.7; }

/* ---------------------------------------------------------------- 底部列 */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding-bottom: var(--safe-b);
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.tabbar-inner {
  display: grid; grid-template-columns: 1fr 1fr 88px 1fr 1fr;
  align-items: center;
  max-width: 720px; margin: 0 auto;
}

.tab {
  height: 60px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; color: var(--muted);
}
.tab .ic { font-size: 19px; line-height: 1; }
.tab[aria-current="page"] { color: var(--text); font-weight: 600; }

.fab-slot { display: flex; justify-content: center; }
.fab {
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 30px; font-weight: 300; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  margin-top: -18px;
  box-shadow: 0 4px 14px rgba(232, 89, 12, .35);
}
.fab:active { transform: scale(.94); }

/* ---------------------------------------------------------------- 記帳鍵盤 */

.sheet {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; flex-direction: column;
  animation: slideUp .18s ease-out;
}
@keyframes slideUp { from { transform: translateY(100%); } }

.sheet-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line-soft);
  flex: none;
}
.sheet-head .x { font-size: 24px; width: var(--tap); height: var(--tap); color: var(--muted); }
.sheet-head .spacer { flex: 1; }

.chip {
  padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--raised);
  font-size: 14px; font-weight: 600;
}
.chip:active { background: var(--accent-weak); }
.chip.on { background: var(--accent-ink); border-color: var(--accent-ink); color: #fff; }

.amount-area {
  flex: none;
  padding: 26px 22px 22px;
  text-align: right;
  border-bottom: 1px solid var(--line);
}
.amount-area .big {
  font-size: 52px; font-weight: 700; letter-spacing: -0.035em;
  color: var(--accent); line-height: 1.05;
  word-break: break-all;
}
.amount-area .big.zero { color: var(--faint); }
.amount-area .conv { font-size: 15px; color: var(--muted); margin-top: 6px; min-height: 22px; }
.amount-area .pending { font-size: 13px; color: var(--faint); min-height: 18px; }

.catgrid {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 2px; padding: 2px;
  background: var(--line-soft);
  align-content: start;
}
.cat {
  background: var(--bg);
  min-height: 74px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
}
.cat:active { background: var(--accent-weak); }
.cat .ic { font-size: 23px; line-height: 1; }
.cat .nm { font-size: 12px; color: var(--muted); }
.cat.armed { background: var(--accent-ink); }
.cat.armed .nm { color: #fff; }

.keypad {
  flex: none;
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 1px; padding: 1px 1px calc(1px + var(--safe-b));
  background: var(--line-soft);
}
.key {
  background: var(--bg);
  height: 58px;
  font-size: 23px; font-weight: 500;
  display: flex; align-items: center; justify-content: center;
}
.key:active { background: var(--raised); }
.key.fn { color: var(--muted); font-size: 20px; }
.key.go { background: var(--accent-ink); color: #fff; font-size: 19px; font-weight: 700; }
.key.go:active { filter: brightness(.92); }
.key.go[disabled] { background: var(--line); color: var(--faint); }

/* ---------------------------------------------------------------- 提示條 */

.toast {
  position: fixed; left: 16px; right: 16px; z-index: 200;
  bottom: calc(76px + var(--safe-b));
  display: flex; align-items: center; gap: 12px;
  padding: 13px 16px;
  border-radius: var(--r);
  background: var(--text); color: var(--bg);
  font-size: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .22);
  animation: toastIn .18s ease-out;
  max-width: 688px; margin: 0 auto;
}
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } }
.toast .undo {
  margin-left: auto; flex: none;
  color: var(--accent); font-weight: 650;
  padding: 4px 6px;
}
.toast.warn { background: var(--danger); color: #fff; }
.toast.warn .undo { color: #fff; text-decoration: underline; }

/* ---------------------------------------------------------------- 登入 */

.login {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px; padding: 32px 28px calc(48px + var(--safe-b));
  max-width: 380px; margin: 0 auto;
}
.login .mark { font-size: 40px; }
.login h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.02em; }
.login p { margin: -8px 0 4px; font-size: 14px; color: var(--muted); text-align: center; }
.login input {
  width: 100%; height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--r);
  background: var(--raised);
  font-size: 16px;
}
.login input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.login button {
  width: 100%; height: var(--tap);
  border-radius: var(--r);
  background: var(--accent-ink); color: #fff;
  font-size: 16px; font-weight: 650;
}
.login button:disabled { background: var(--line); color: var(--faint); }
.login .err { color: var(--danger); font-size: 14px; min-height: 20px; margin: 0; }

/* ---------------------------------------------------------------- 設定 */

.rows { border-top: 1px solid var(--line); }
.row {
  display: flex; align-items: center; gap: 14px;
  width: 100%; text-align: left;
  padding: 15px 18px;
  border-bottom: 1px solid var(--line-soft);
  min-height: var(--tap);
}
.row:active { background: var(--raised); }
.row .k { font-size: 15px; }
.row .v { margin-left: auto; font-size: 14px; color: var(--muted); text-align: right; }
.row.danger .k { color: var(--danger); }
.row.limit { display: block; }

/* 表單控制項嵌在 row 裡，看起來像純文字，點下去才是輸入 */
.row .sel, .row .inp {
  border: 0; background: none;
  font-size: 15px; color: var(--text);
  text-align: right;
  max-width: 62%;
  padding: 6px 0;
  min-height: 34px;
}
.row .inp.grow { flex: 1; max-width: none; }
.row .sel { appearance: none; padding-right: 14px; background:
  linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 5px) 52% / 5px 5px no-repeat; }
.row .inp:focus, .row .sel:focus { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.row .inp::placeholder { color: var(--faint); }
.row .chk {
  margin-left: auto; width: 22px; height: 22px;
  accent-color: var(--accent); flex: none;
}

/* 進度條：預算、卡片額度、分類佔比共用 */
.bar {
  height: 6px; border-radius: 3px;
  background: var(--line); overflow: hidden;
  margin-top: 10px;
}
.bar .fill { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.bar .fill.over { background: var(--danger); }
.barlabel { font-size: 12px; color: var(--muted); margin-top: 5px; }

/* 頂欄的旅程切換 */
.trip-btn {
  display: flex; align-items: baseline; gap: 6px;
  font-size: 19px; font-weight: 650; letter-spacing: -0.01em;
  color: var(--text);
  max-width: 70%;
}
.trip-btn .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trip-btn .caret { font-size: 12px; color: var(--muted); flex: none; }
.trip-btn:active { opacity: .6; }

.entry.exchange .icon { color: var(--muted); }
.entry.exchange .amt .a { color: var(--ok); }

.note { padding: 14px 18px 26px; font-size: 12px; color: var(--faint); line-height: 1.7; }

/* ---------------------------------------------------------------- 幣別選單 */

.picker {
  position: fixed; inset: 0; z-index: 150;
  display: flex; flex-direction: column;
  background: var(--bg);
  margin: 0 auto;
}
.picker-list { flex: 1; overflow-y: auto; padding-bottom: var(--safe-b); }
.picker .row .code { font-weight: 650; width: 52px; }
.picker .row.on { background: var(--accent-weak); }
.picker .row.on .code { color: var(--accent); }

/* ---------------------------------------------------------------- 桌面
   手機版是滿版；在寬螢幕上滿版會把數字鍵撐到 300 多 px 寬，很荒謬。
   改成置中的一欄面板，用超大 box-shadow 當背景遮罩（不需要額外的 DOM）。 */

@media (min-width: 640px) {
  .sheet, .picker {
    max-width: 420px;
    margin: 0 auto;                 /* inset:0 + auto 邊界 = 水平置中 */
    box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .45);
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
  .sheet { animation: fadeIn .14s ease-out; }
  @keyframes fadeIn { from { opacity: 0; } }

  .fab:hover { filter: brightness(1.06); }
  .entry:hover, .row:hover, .key:hover, .cat:hover { background: var(--raised); }
  .key.go:hover { background: var(--accent-ink); filter: brightness(1.08); }
}

/* 筆電視窗常常很矮。原本的尺寸會把分類區壓到幾乎消失，這裡整體縮一號。 */
@media (max-height: 720px) {
  .amount-area { padding: 14px 22px 12px; }
  .amount-area .big { font-size: 38px; }
  .amount-area .conv { font-size: 14px; margin-top: 3px; }
  .cat { min-height: 60px; }
  .cat .ic { font-size: 20px; }
  .key { height: 48px; font-size: 21px; }
  .catgrid { min-height: 124px; }
}

/* 桌面用實體鍵盤輸入時，提示哪個分類會被 Enter 選中 */
.cat.focus { outline: 2px solid var(--accent); outline-offset: -2px; }
