:root {
  color-scheme: light;

  --bg: #f2f4f7;
  --card: #ffffff;
  --card-soft: #f7f8fa;
  --border: #e6e9ee;
  --border-soft: #eef0f4;

  --text: #14161a;
  --text-2: #4b5563;
  --muted: #8b93a1;

  --accent: #2f6df6;
  --accent-soft: #e8efff;
  --expense: #e0484d;
  --income: #0f9d58;
  --warn: #ef9d1f;

  --r-sm: 8px;
  --r: 14px;
  --r-lg: 20px;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .05);
  --shadow: 0 2px 8px rgba(16, 24, 40, .06);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, .10);

  --nav-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --bg: #0f1115;
    --card: #171a20;
    --card-soft: #1d212a;
    --border: #272c36;
    --border-soft: #21252e;

    --text: #e9ecf1;
    --text-2: #b3bac6;
    --muted: #7d8695;

    --accent: #5b8cff;
    --accent-soft: #1b2740;
    --expense: #f0666b;
    --income: #34c97b;
    --warn: #f0ad3c;

    --shadow-sm: none;
    --shadow: 0 2px 10px rgba(0, 0, 0, .35);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}

b, strong { font-weight: 700; }

/* ---------- header ---------- */

header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border-soft);
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 8px;
}

header h1 {
  font-size: 16px;
  margin: 0;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  letter-spacing: .2px;
}

.logo { width: 26px; height: 26px; flex: 0 0 auto; }
.logo.lg { width: 44px; height: 44px; }

header select {
  flex: 1 1 auto;
  min-width: 0;
  height: 36px;
  padding: 0 34px 0 13px;
  text-overflow: ellipsis;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-soft);
  font-size: 14px;
  font-weight: 600;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 15px, calc(100% - 11px) 15px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

header input[type=month] {
  width: auto;
  height: 36px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--card-soft);
  font-size: 13px;
  color: var(--text-2);
}

/* ---------- nav ---------- */

nav {
  display: flex;
  gap: 2px;
  background: var(--card);
  border-bottom: 1px solid var(--border-soft);
  padding: 0 8px;
  position: sticky;
  top: 57px;
  z-index: 19;
  overflow-x: auto;
  scrollbar-width: none;
}

nav::-webkit-scrollbar { display: none; }

nav button {
  flex: 0 0 auto;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-size: 14px;
  padding: 12px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: color .15s;
}

nav .nav-ico { display: none; }
nav button:hover { color: var(--text-2); }
nav button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 700; }

main {
  padding: 16px;
  max-width: 900px;
  margin: 0 auto;
  padding-bottom: 40px;
}

section[hidden] { display: none; }

/* ---------- card ---------- */

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  margin-bottom: 14px;
}

.card h2 {
  font-size: 13px;
  margin: 0 0 14px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: .4px;
}

/* ---------- hero ---------- */

.hero {
  background: linear-gradient(140deg, var(--accent) 0%, color-mix(in srgb, var(--accent) 72%, #7b3fe4) 100%);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  padding: 22px 20px 18px;
  margin-bottom: 16px;
  color: #fff;
}

.hero-label { font-size: 13px; opacity: .85; letter-spacing: .5px; }

.hero-value {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -.5px;
  font-variant-numeric: tabular-nums;
  margin: 2px 0 16px;
}

.hero-split { display: flex; align-items: center; gap: 14px; }
.hero-item { flex: 1; min-width: 0; }
.hero-sep { width: 1px; align-self: stretch; background: rgba(255, 255, 255, .25); }

.hero-cap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  opacity: .85;
}

.hero-item b {
  display: block;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  color: #fff;
  margin-top: 2px;
}

.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: inline-block; }
.hero .dot { background: rgba(255, 255, 255, .9); }

.expense { color: var(--expense); }
.income { color: var(--income); }

/* ---------- form ---------- */

label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 5px;
  font-weight: 600;
  letter-spacing: .2px;
}

input, select, textarea, button { font: inherit; color: var(--text); }

input, select, textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--card-soft);
  transition: border-color .15s, box-shadow .15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

input::placeholder { color: var(--muted); }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1 1 130px; }
.field { margin-bottom: 14px; }

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--r-sm);
  padding: 11px 18px;
  cursor: pointer;
  font-weight: 700;
  white-space: nowrap;
  transition: filter .15s, transform .06s;
}

.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--text-2); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--card-soft); filter: none; }
.btn.danger { background: var(--expense); }
.btn.sm { padding: 6px 11px; font-size: 13px; font-weight: 600; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.chips { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
  border: 1px solid var(--border);
  background: var(--card-soft);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s;
}

.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.on {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 2px 8px color-mix(in srgb, var(--accent) 35%, transparent);
}

/* ---------- list ---------- */

.list { list-style: none; margin: 0; padding: 0; }

.list li {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--border-soft);
}

.list li:last-child { border-bottom: none; }

.tx-ico {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: 11px;
  background: var(--card-soft);
  border: 1px solid var(--border-soft);
  display: grid;
  place-items: center;
  font-size: 18px;
}

.list .meta { flex: 1; min-width: 0; }
.list .top { display: flex; gap: 7px; align-items: baseline; }
.list .cat { font-weight: 600; }
.list .note { color: var(--text-2); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .date { color: var(--muted); font-size: 12px; margin-top: 1px; }

.list .amt {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  font-size: 15px;
}

.src {
  font-size: 10px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
  vertical-align: 1px;
}

/* ---------- 進度條 ---------- */

.bar-wrap { background: var(--border-soft); border-radius: 999px; height: 8px; overflow: hidden; margin-top: 7px; }
.bar { height: 100%; background: var(--income); border-radius: 999px; transition: width .4s ease; }
.bar.warn { background: var(--warn); }
.bar.over { background: var(--expense); }

.budget-item { padding: 12px 0; border-bottom: 1px solid var(--border-soft); }
.budget-item:last-child { border-bottom: none; }
.budget-head { display: flex; justify-content: space-between; font-size: 14px; font-weight: 600; }
.budget-head + .bar-wrap + .muted { margin-top: 5px; }

.empty {
  color: var(--muted);
  text-align: center;
  padding: 28px 0;
  font-size: 14px;
  border-bottom: none !important;
  justify-content: center;
}

.muted { color: var(--muted); font-size: 13px; }

canvas { max-height: 260px; }

/* ---------- 設定頁手風琴 ---------- */

.acc-list { padding: 0 16px; }
.acc { border-bottom: 1px solid var(--border-soft); }
.acc:last-of-type { border-bottom: none; }

.acc > summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 2px;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
  user-select: none;
}

.acc > summary::-webkit-details-marker { display: none; }
.acc > summary:hover .acc-title { color: var(--accent); }
.acc > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 6px; }

.acc-title { flex: 1; transition: color .15s; }
.acc-hint { color: var(--muted); font-size: 13px; font-weight: 400; }

.acc > summary::after {
  content: '';
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform .2s;
}

.acc[open] > summary::after { transform: rotate(45deg); }
.acc-body { padding: 2px 2px 18px; }

/* ---------- 代碼框 / 標籤 ---------- */

.code-box {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 5px;
  text-align: center;
  padding: 16px;
  background: var(--accent-soft);
  border: 1px dashed var(--accent);
  border-radius: var(--r-sm);
  color: var(--accent);
  user-select: all;
}

.tag {
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  white-space: nowrap;
}

.tag.owner { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(8px);
  background: #101318;
  color: #fff;
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  z-index: 100;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  box-shadow: var(--shadow-lg);
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--expense); }

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

.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login-box { width: 100%; max-width: 340px; padding: 26px 22px; border-radius: var(--r-lg); box-shadow: var(--shadow-lg); }
.login-box h1 { font-size: 22px; margin: 0 0 6px; display: flex; align-items: center; justify-content: center; gap: 10px; }
.login-box p { text-align: center; color: var(--muted); font-size: 13px; margin: 0 0 22px; }
.login-box .btn { width: 100%; padding: 12px; margin-top: 4px; }
.error { color: var(--expense); font-size: 13px; min-height: 18px; margin-top: 10px; text-align: center; }

/* ---------- dialog ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--card);
  color: var(--text);
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow-lg);
}

dialog::backdrop { background: rgba(8, 10, 14, .5); backdrop-filter: blur(2px); }

/* ---------- 手機版：分頁列移到底部 ---------- */

@media (max-width: 640px) {
  main { padding: 14px 12px calc(var(--nav-h) + 24px); }

  nav {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding: 0 4px env(safe-area-inset-bottom);
    border-top: 1px solid var(--border);
    border-bottom: none;
    box-shadow: 0 -2px 12px rgba(16, 24, 40, .06);
    overflow: visible;
    background: color-mix(in srgb, var(--card) 92%, transparent);
    backdrop-filter: saturate(180%) blur(12px);
  }

  nav button {
    flex: 1;
    flex-direction: column;
    gap: 3px;
    font-size: 11px;
    padding: 9px 2px;
    border-bottom: none;
    border-top: 2px solid transparent;
  }

  nav .nav-ico { display: block; font-size: 19px; line-height: 1; }
  nav button.active { border-top-color: transparent; }
  nav button.active .nav-ico {
    background: var(--accent-soft);
    border-radius: 999px;
    padding: 2px 12px;
  }

  /* 空間有限，操作鈕縮成圖示（文字保留在 title 供輔助技術讀取） */
  .list .icon-btn {
    font-size: 0;
    width: 34px;
    height: 34px;
    padding: 0;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
  }

  .list .icon-btn::before { content: attr(data-icon); font-size: 14px; }
  .list li { gap: 9px; }
  .tx-ico { width: 34px; height: 34px; font-size: 16px; border-radius: 10px; }

  .toast { bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); }
  .app-name { display: none; }
  header { gap: 7px; padding-left: 12px; padding-right: 12px; }
  .hero-value { font-size: 32px; }
  .acc-list { padding: 0 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
