:root {
  --bg: #f5f1ea;
  --card: #fffdf9;
  --ink: #1f1d24;
  --muted: #77727d;
  --line: #e8e2d8;
  --accent: #e2572b;
  --accent-ink: #ffffff;
  --ok: #2f9e6e;
  --warn: #e0a019;
  --over: #d23b3b;
  --toy: #e2572b;
  --game: #5b5bd6;
  --merch: #1b9aa0;
  --field: #f7f3ec;
  --shadow: 0 1px 2px rgb(40 30 20 / 6%), 0 4px 16px rgb(40 30 20 / 5%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17161b;
    --card: #222128;
    --ink: #f1eef5;
    --muted: #a19ca8;
    --line: #34323b;
    --accent: #f06b3f;
    --toy: #f06b3f;
    --game: #8b8bf0;
    --merch: #3dc2c8;
    --field: #2a2930;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  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; }

h1, h2, p { margin: 0; }
h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
ul { list-style: none; margin: 0; padding: 0; }
button, input { font: inherit; color: inherit; }

.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 13px; }
.error { color: var(--over); font-size: 14px; min-height: 0; }
.error:empty { display: none; }

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) 16px calc(env(safe-area-inset-bottom) + 32px);
  display: grid;
  gap: 12px;
}

.card {
  background: var(--card);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow);
}

/* Header */
.top {
  display: grid;
  grid-template-columns: 44px 1fr 44px 44px;
  align-items: center;
  gap: 4px;
  padding: 4px 0;
}
.top-title { text-align: center; }
.top-title h1 { font-size: 18px; }
.top-title p { font-size: 13px; color: var(--muted); }
.icon-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 12px;
  background: transparent;
  font-size: 26px; line-height: 1;
  display: grid; place-items: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.icon-btn:disabled { opacity: 0.25; }

/* Hero */
.label { font-size: 13px; color: var(--muted); }
.big {
  font-size: 44px; font-weight: 700; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  margin: 2px 0 12px;
}
.big.over { color: var(--over); }
.bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar span { display: block; height: 100%; border-radius: inherit; background: var(--ok); transition: width .3s; }
.bar span[data-level="warn"] { background: var(--warn); }
.bar span[data-level="over"] { background: var(--over); }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.value { font-variant-numeric: tabular-nums; }
.value strong { font-size: 18px; }
.vouchers { margin-top: 10px; display: grid; gap: 6px; }
.vouchers:empty { display: none; }
.vouchers li {
  display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: baseline;
  font-size: 14px; padding: 8px 10px; border-radius: 10px; background: var(--field);
  font-variant-numeric: tabular-nums;
}
.vouchers li small { color: var(--muted); }
.vouchers li.expiring { outline: 1.5px solid var(--warn); }
.vouchers li.used-up { opacity: .5; }
.note { margin-top: 10px; font-size: 13px; color: var(--muted); }
.note:empty { display: none; }

/* Forms */
.expense-form { display: grid; gap: 12px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > span, .seg legend { font-size: 13px; color: var(--muted); }
.field input {
  width: 100%; min-width: 0;
  height: 46px; padding: 0 12px;
  border: 1.5px solid transparent; border-radius: 12px;
  background: var(--field);
  font-size: 16px; /* 16px keeps iOS from zooming on focus */
  outline: none;
  -webkit-appearance: none; appearance: none;
}
.field input[type="date"] { line-height: 46px; }
.field input:focus { border-color: var(--accent); }
.amount-field input { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

.seg { border: 0; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg legend { padding: 0; margin-bottom: 4px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; }
.seg span {
  display: grid; place-items: center; height: 42px;
  border-radius: 12px; background: var(--field); font-weight: 600; font-size: 15px;
  border: 1.5px solid transparent;
}
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { border-color: var(--ink); }
#settingsForm .seg { grid-template-columns: 1fr 1fr; }

.btn {
  height: 46px; padding: 0 18px;
  border: 0; border-radius: 12px;
  background: var(--field); font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--over); }
.btn.danger[data-armed="1"] { background: var(--over); color: #fff; }
.btn:disabled { opacity: .5; }
.btn:active { transform: scale(.98); }
.actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.spacer { flex: 1; }

/* Categories */
.cats { display: grid; gap: 10px; }
.cats li { display: grid; grid-template-columns: 52px 1fr auto; gap: 10px; align-items: center; }
.cat-bar { height: 8px; background: var(--field); border-radius: 99px; overflow: hidden; }
.cat-bar span { display: block; height: 100%; border-radius: inherit; background: var(--c); }
.cat-amount { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 64px; text-align: right; }

[data-cat="toy"] { --c: var(--toy); }
[data-cat="game"] { --c: var(--game); }
[data-cat="merch"] { --c: var(--merch); }
.chip {
  display: inline-grid; place-items: center;
  height: 24px; padding: 0 8px; border-radius: 8px;
  font-size: 12px; font-weight: 700;
  color: var(--c);
  background: color-mix(in srgb, var(--c) 14%, transparent);
}

/* List */
.list { display: grid; }
.date-head { font-size: 13px; color: var(--muted); padding: 10px 2px 4px; }
.date-head:first-child { padding-top: 0; }
.item {
  width: 100%; display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center;
  padding: 10px 2px; border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left; cursor: pointer;
}
.item-text { display: grid; min-width: 0; }
.item-name, .item-note { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-note { font-size: 13px; }
.item-note:empty { display: none; }
.item-amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { text-align: center; padding: 12px 0; }

/* Dialogs as bottom sheets */
.sheet {
  width: 100%; max-width: 520px;
  margin: auto auto 0;
  padding: 20px 16px calc(env(safe-area-inset-bottom) + 20px);
  border: 0; border-radius: 22px 22px 0 0;
  background: var(--card); color: var(--ink);
  max-height: 92dvh;
}
.sheet::backdrop { background: rgb(0 0 0 / 40%); }
.sheet h2 { font-size: 18px; }
#settingsForm { display: grid; gap: 12px; }
.rules { font-size: 14px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 12px; }
.rules summary { cursor: pointer; color: var(--ink); font-weight: 600; }
.rules ul { list-style: disc; padding-left: 20px; display: grid; gap: 6px; margin-top: 8px; }

/* Login */
.login {
  min-height: 100dvh; display: grid; place-items: center;
  padding: 16px;
}
.login-card { width: 100%; max-width: 360px; display: grid; gap: 14px; text-align: center; }
.login-card .field { text-align: left; }
.login-icon { margin: 0 auto; border-radius: 18px; }
.login-card h1 { font-size: 22px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translate(-50%, 20px);
  width: max-content; max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: 14px;
  background: var(--ink); color: var(--bg); font-size: 15px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
