:root {
  --desk: #e6ebf0;
  --paper: #ffffff;
  --ink: #1d2733;
  --muted: #687584;
  --line: #d3dae2;
  --thermal: #4b3a8c;     /* 感熱紙列印的紫墨色 */
  --ok: #0d7a68;
  --ok-press: #0a6153;
  --on-ok: #ffffff;
  --warn: #a86300;
  --warn-bg: #fff3df;
  --income: #0d7a68;
  --danger: #b4232c;
  --radius-s: 6px;
  --radius-m: 12px;
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Heiti TC", system-ui, sans-serif;
  --tab-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme=light]) {
    --desk: #151b22;
    --paper: #212a34;
    --ink: #e8edf2;
    --muted: #9aa7b5;
    --line: #34404d;
    --thermal: #b8a8ff;
    --ok: #2bb59c;
    --ok-press: #239985;
    --on-ok: #06231d;
    --warn: #f0a640;
    --warn-bg: #3a2c14;
    --income: #3cc7ad;
    color-scheme: dark;
  }
}
:root[data-theme=dark] {
    --desk: #151b22;
    --paper: #212a34;
    --ink: #e8edf2;
    --muted: #9aa7b5;
    --line: #34404d;
    --thermal: #b8a8ff;
    --ok: #2bb59c;
    --ok-press: #239985;
    --on-ok: #06231d;
    --warn: #f0a640;
    --warn-bg: #3a2c14;
    --income: #3cc7ad;
    color-scheme: dark;
  }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--desk);
  -webkit-tap-highlight-color: transparent;
  font-variant-numeric: tabular-nums;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--thermal); outline-offset: 2px; border-radius: 4px; }
[hidden] { display: none !important; }
a { color: var(--thermal); }

/* ---------- 版面 ---------- */
.shell { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--desk) 88%, transparent);
  backdrop-filter: blur(10px);
}
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; flex: 1; letter-spacing: .02em; }
.scope { border: 1px solid var(--line); background: var(--paper); border-radius: 999px; padding: 6px 12px; font-size: 14px; }
main { flex: 1; padding: 4px 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 96px); width: 100%; max-width: 760px; margin: 0 auto; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--paper); border-top: 1px solid var(--line);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--muted); text-decoration: none; position: relative;
}
.tabs a svg { width: 24px; height: 24px; }
.tabs a[aria-current="page"] { color: var(--ink); font-weight: 700; }
.badge {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
}
.fab {
  position: fixed; right: 18px; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px); z-index: 7;
  height: 52px; padding: 0 20px 0 16px; border: 0; border-radius: 26px;
  display: flex; align-items: center; gap: 6px;
  background: var(--ink); color: var(--paper); font-weight: 700; font-size: 16px;
  box-shadow: 0 6px 18px rgba(20, 30, 45, .25);
}
.fab svg { width: 22px; height: 22px; }

@media (min-width: 900px) {
  .shell { flex-direction: row; }
  .tabs {
    position: sticky; top: 0; height: 100vh; width: 220px; flex: none;
    grid-template-columns: 1fr; grid-auto-rows: 52px; align-content: start;
    padding: 16px 12px 0; border-top: 0; border-right: 1px solid var(--line);
  }
  .tabs a { flex-direction: row; justify-content: flex-start; gap: 12px; padding: 0 14px; font-size: 15px; border-radius: var(--radius-m); }
  .tabs a[aria-current="page"] { background: var(--desk); }
  .badge { position: static; margin-left: auto; }
  .content { flex: 1; min-width: 0; }
  main { padding-bottom: 120px; }
  .fab { bottom: 28px; right: 32px; }
  .brand { display: flex; align-items: center; padding: 0 14px; font-weight: 700; font-size: 18px; margin-bottom: 12px; }
}
@media (max-width: 899px) { .brand { display: none; } }

/* ---------- 待確認：收據 ---------- */
.receipts { display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; padding-top: 8px; }
@media (min-width: 1200px) { main.wide { max-width: 1040px; } main.wide .receipts { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; } }

.receipt {
  position: relative;
  background: var(--paper);
  padding: 18px 18px 26px;
  border-radius: var(--radius-s) var(--radius-s) 0 0;
  filter: drop-shadow(0 1px 1px rgba(20, 30, 45, .08)) drop-shadow(0 4px 10px rgba(20, 30, 45, .06));
  /* 底部鋸齒撕紙邊 */
  --tooth: 10px;
  -webkit-mask: conic-gradient(from -45deg at bottom, #0000, #000 1deg 89deg, #0000 90deg) 50% / calc(var(--tooth) * 2) 100%;
          mask: conic-gradient(from -45deg at bottom, #0000, #000 1deg 89deg, #0000 90deg) 50% / calc(var(--tooth) * 2) 100%;
  transition: transform .35s ease, opacity .35s ease;
}
.receipt-head { display: flex; justify-content: space-between; gap: 8px; color: var(--thermal); font-size: 13px; }
.receipt-head .src { font-weight: 700; }
.who { color: var(--muted); font-size: 13px; }
.amount-row { display: flex; align-items: baseline; gap: 10px; margin: 10px 0 2px; }
.amount {
  font-size: 40px; font-weight: 800; letter-spacing: -.01em; line-height: 1.1;
  border: 0; background: none; padding: 0; width: 100%; min-width: 0;
}
.amount.income { color: var(--income); }
.amount-row .cur { color: var(--muted); font-size: 15px; font-weight: 700; }
.merchant-input {
  width: 100%; border: 0; border-bottom: 1px dashed var(--line); background: none;
  font-size: 19px; font-weight: 600; padding: 4px 0 6px;
}
.merchant-input::placeholder { color: var(--muted); font-weight: 400; }
.perf { border: 0; border-top: 2px dotted var(--line); margin: 14px -18px; }
.kv { display: grid; grid-template-columns: 4.5em 1fr; gap: 6px 10px; font-size: 14px; align-items: center; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }
.kv input { width: 100%; border: 0; border-bottom: 1px solid transparent; background: none; padding: 2px 0; }
.kv input:focus { border-bottom-color: var(--line); outline: none; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 6px 14px; font-size: 14px; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--paper); font-weight: 700; }

.flag { display: flex; gap: 8px; align-items: flex-start; margin-top: 12px; padding: 8px 10px; border-radius: var(--radius-s); background: var(--warn-bg); color: var(--warn); font-size: 13px; }
details.raw { margin-top: 12px; font-size: 13px; color: var(--muted); }
details.raw summary { cursor: pointer; list-style: none; }
details.raw summary::-webkit-details-marker { display: none; }
details.raw summary::before { content: "▸ "; }
details.raw[open] summary::before { content: "▾ "; }
details.raw pre { white-space: pre-wrap; word-break: break-all; font-family: inherit; margin: 6px 0 0; padding: 8px 10px; background: var(--desk); border-radius: var(--radius-s); color: var(--ink); }

.actions { display: flex; gap: 10px; margin-top: 16px; }
.btn {
  text-decoration: none;
  height: 46px; padding: 0 18px; border-radius: var(--radius-m); border: 1px solid var(--line);
  background: transparent; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { flex: 1; background: var(--ok); border-color: var(--ok); color: var(--on-ok); font-weight: 700; }
.btn.primary:active { background: var(--ok-press); }
.btn.danger { color: var(--danger); }
.btn.small { height: 36px; padding: 0 12px; font-size: 14px; border-radius: var(--radius-s); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

/* 確認時蓋章 */
.stamp {
  position: absolute; top: 46px; right: 22px; pointer-events: none;
  padding: 4px 12px; border: 3px solid var(--ok); border-radius: 8px; color: var(--ok);
  font-size: 22px; font-weight: 800; letter-spacing: .15em;
  transform: rotate(-12deg) scale(2.2); opacity: 0;
}
.receipt.stamped .stamp { animation: stamp .32s cubic-bezier(.2, 1.6, .5, 1) forwards; }
.receipt.stamped.rejected .stamp { border-color: var(--muted); color: var(--muted); }
.receipt.leaving { transform: translateY(-14px) scale(.97); opacity: 0; }
@keyframes stamp { to { transform: rotate(-12deg) scale(1); opacity: .9; } }
@media (prefers-reduced-motion: reduce) {
  .receipt, .receipt.stamped .stamp { animation: none; transition: none; }
  .receipt.stamped .stamp { opacity: .9; transform: rotate(-12deg); }
}

.batchbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 14px; }

/* ---------- 空狀態 ---------- */
.empty { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty h2 { color: var(--ink); font-size: 18px; margin: 0 0 6px; }
.empty p { margin: 0 auto 18px; max-width: 26em; }

/* ---------- 帳本 ---------- */
.monthbar { display: flex; align-items: center; gap: 8px; margin: 4px 0 12px; }
.monthbar .m { font-size: 17px; font-weight: 700; min-width: 7.5em; text-align: center; }
.iconbtn { width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--paper); display: grid; place-items: center; }
.iconbtn svg { width: 18px; height: 18px; }
.summary { display: flex; gap: 24px; margin: 0 0 18px; }
.summary div { display: flex; flex-direction: column; }
.summary span { color: var(--muted); font-size: 13px; }
.summary strong { font-size: 24px; }
.summary .inc strong { color: var(--income); }
.search { width: 100%; height: 42px; border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--paper); padding: 0 14px; margin-bottom: 14px; }

.day { margin-bottom: 18px; }
.day-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; padding: 0 4px 6px; }
.rows { background: var(--paper); border-radius: var(--radius-m); overflow: hidden; }
.row {
  display: grid; grid-template-columns: 38px 1fr auto; gap: 12px; align-items: center;
  width: 100%; padding: 12px 14px; border: 0; background: none; text-align: left;
}
.row + .row { border-top: 1px solid var(--line); }
.cat-dot { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; font-size: 15px; font-weight: 700; color: #fff; }
.row .t { min-width: 0; }
.row .t b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .t small { display: block; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .a { font-weight: 700; }
.row .a.income { color: var(--income); }

/* ---------- 統計 ---------- */
.panel { background: var(--paper); border-radius: var(--radius-m); padding: 16px; margin-bottom: 16px; }
.panel h2 { font-size: 15px; margin: 0 0 12px; }
.bar-row { display: grid; grid-template-columns: 5.5em 1fr auto; gap: 10px; align-items: center; font-size: 14px; padding: 5px 0; }
.bar-track { display: block; height: 10px; border-radius: 5px; background: var(--desk); overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 5px; }
.bar-row .v { text-align: right; min-width: 5.5em; }
.days-chart { width: 100%; height: 120px; display: block; }

/* ---------- 設定 ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.input, .field input, .field select, .field textarea {
  width: 100%; min-height: 44px; border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--paper); padding: 8px 12px;
}
.field textarea { min-height: 96px; resize: vertical; }
.copyline { display: flex; gap: 8px; }
.copyline code { flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; padding: 10px 12px; background: var(--desk); border-radius: var(--radius-s); font-size: 13px; }
.guide { font-size: 14px; }
.guide ol { padding-left: 1.3em; margin: 8px 0; }
.guide li { margin-bottom: 6px; }
.guide code { background: var(--desk); padding: 1px 5px; border-radius: 4px; font-size: 13px; word-break: break-all; }
.panel details > summary { cursor: pointer; font-weight: 700; padding: 4px 0; }
.list-plain { list-style: none; margin: 0; padding: 0; }
.list-plain li { padding: 10px 0; border-top: 1px solid var(--line); font-size: 14px; }
.list-plain li:first-child { border-top: 0; }
.muted { color: var(--muted); }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

/* ---------- 登入 ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth form { width: 100%; max-width: 360px; }
.auth h1 { font-size: 26px; margin: 0 0 4px; }
.auth p { color: var(--muted); margin: 0 0 24px; }
.error { color: var(--danger); font-size: 14px; min-height: 1.5em; margin: 0 0 8px; }

/* ---------- 底部面板 ---------- */
dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 560px;
  max-height: 92vh; border-radius: 18px 18px 0 0; background: var(--paper); color: var(--ink);
}
@media (min-width: 900px) { dialog.sheet { margin: auto; border-radius: 18px; } }
dialog.sheet::backdrop { background: rgba(15, 22, 30, .45); }
.sheet-body { padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: 92vh; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; margin-bottom: 12px; }
.seg button { border: 0; background: none; padding: 6px 16px; border-radius: 999px; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper); font-weight: 700; }
.big-amount { font-size: 34px !important; font-weight: 800; }
.paste-hint { font-size: 13px; color: var(--muted); margin: -6px 0 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 84px); z-index: 20;
  transform: translateX(-50%); background: var(--ink); color: var(--paper);
  padding: 10px 16px; border-radius: 999px; font-size: 14px; display: flex; gap: 12px; align-items: center;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .2); white-space: nowrap;
}
.toast button { border: 0; background: none; color: var(--thermal); font-weight: 700; filter: brightness(1.8); }
.demo-banner { background: #4b3a8c; color: #fff; text-align: center; font-size: 13px; padding: 6px 12px; }

/* 桌面版：標題列與內容對齊 */
.topbar { width: 100%; max-width: 760px; margin: 0 auto; }
@media (min-width: 1200px) { .content:has(main.wide) .topbar { max-width: 1040px; } }
