:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #1c1c1e;
  --muted: #8e8e93;
  --line: #e5e5ea;
  --fill: #e9e9ee;
  --accent: #0a7aff;
  --on-accent: #ffffff;
  --pos: #1f8a3b;
  --neg: #d70015;
  --warn: #c93400;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #f2f2f7;
    --muted: #8e8e93;
    --line: #38383a;
    --fill: #2c2c2e;
    --accent: #0a84ff;
    --pos: #30d158;
    --neg: #ff453a;
    --warn: #ff9f0a;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Sukhumvit Set", "Noto Sans Thai", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) 16px calc(96px + env(safe-area-inset-bottom));
}
.topbar h1 { font-size: 30px; margin: 12px 0 14px; letter-spacing: -0.3px; }
h2 { font-size: 17px; margin: 0 0 8px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
.card.info p { margin: 6px 0; }

/* ---------- inputs & buttons ---------- */
input[type="text"], input[type="date"], input:not([type]) {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px; /* prevents iOS zoom on focus */
  appearance: none;
  -webkit-appearance: none;
}
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: 10px;
  background: var(--fill);
  font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.danger { background: transparent; color: var(--neg); }
.icon-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--accent);
  font-size: 28px; line-height: 1;
}
.link-btn { display: block; margin: 14px auto 0; background: none; border: 0; color: var(--accent); padding: 10px; }

.seg { display: flex; background: var(--fill); border-radius: 10px; padding: 2px; margin-bottom: 12px; }
.seg button { flex: 1; min-height: 36px; border: 0; background: transparent; border-radius: 8px; font-size: 14px; font-weight: 500; }
.seg button.on { background: var(--card); font-weight: 700; box-shadow: 0 1px 3px rgba(0,0,0,.15); }

/* ---------- todo ---------- */
.todo-form { display: grid; gap: 10px; }
.todo-form-row { display: flex; gap: 10px; align-items: center; }
.date-field { display: flex; align-items: center; gap: 8px; flex: 1; font-size: 14px; color: var(--muted); }
.date-field input { flex: 1; min-width: 0; }

.list { list-style: none; margin: 8px 0 0; padding: 0; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.list:empty { display: none; }
.todo { display: flex; align-items: center; gap: 4px; padding: 4px 4px 4px 8px; border-bottom: 1px solid var(--line); }
.todo:last-child { border-bottom: 0; }
.check {
  flex: none; width: 44px; height: 44px; border: 0; background: transparent; position: relative;
}
.check::before {
  content: ""; position: absolute; inset: 10px;
  border: 2px solid var(--muted); border-radius: 50%;
  transition: background .15s, border-color .15s;
}
.todo.done .check::before { background: var(--accent); border-color: var(--accent); }
.todo.done .check::after {
  content: ""; position: absolute; left: 16px; top: 13px; width: 6px; height: 11px;
  border: solid #fff; border-width: 0 2.5px 2.5px 0; transform: rotate(45deg);
}
.todo-body { flex: 1; min-width: 0; border: 0; background: transparent; text-align: left; padding: 8px 4px; }
.todo-title { display: block; overflow-wrap: anywhere; }
.todo.done .todo-title { color: var(--muted); text-decoration: line-through; }
.badge { display: inline-block; margin-top: 2px; font-size: 12px; color: var(--muted); }
.badge.overdue { color: var(--neg); font-weight: 600; }
.badge.today { color: var(--warn); font-weight: 600; }

.empty { text-align: center; color: var(--muted); padding: 36px 0; line-height: 1.7; }

/* ---------- money ---------- */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.month-label { border: 0; background: transparent; font-size: 18px; font-weight: 700; padding: 8px 12px; }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 10px; min-width: 0; }
.stat span { display: block; font-size: 12px; color: var(--muted); }
.stat b { display: block; font-size: 17px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }

.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; margin: 10px 0; font-size: 14px; }
.bar-row:last-child { margin-bottom: 0; }
.bar-row .amt { font-variant-numeric: tabular-nums; color: var(--muted); }
.bar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--fill); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.day { margin-bottom: 14px; }
.day-head { display: flex; justify-content: space-between; padding: 0 6px 6px; font-size: 13px; color: var(--muted); }
.tx { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 14px; border: 0; border-bottom: 1px solid var(--line); background: var(--card); text-align: left; }
.tx:last-child { border-bottom: 0; }
.day-list { border-radius: var(--radius); overflow: hidden; }
.tx-ico { flex: none; width: 36px; height: 36px; border-radius: 50%; background: var(--fill); display: grid; place-items: center; font-size: 18px; }
.tx-main { flex: 1; min-width: 0; }
.tx-main b { display: block; font-weight: 600; }
.tx-main small { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

.fab {
  position: fixed; right: max(20px, calc(50% - 320px + 16px));
  bottom: calc(76px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border: 0; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 32px; line-height: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,.3);
  z-index: 20;
}

/* ---------- more ---------- */
.menu { padding: 0; overflow: hidden; }
.menu-item { display: block; width: 100%; padding: 14px; border: 0; border-bottom: 1px solid var(--line); background: transparent; text-align: left; cursor: pointer; }
.menu-item:last-child { border-bottom: 0; }
.menu-item.danger { color: var(--neg); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: color-mix(in srgb, var(--card) 85%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-inner { max-width: 640px; margin: 0 auto; display: flex; }
.tabbar button { flex: 1; border: 0; background: transparent; padding: 8px 0 6px; color: var(--muted); display: grid; justify-items: center; gap: 0; min-height: 52px; }
.tabbar button i { font-style: normal; font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar button span { font-size: 11px; font-weight: 600; }
.tabbar button.on { color: var(--accent); }
.tabbar button.on i { filter: none; opacity: 1; }

/* ---------- dialogs ---------- */
dialog {
  width: min(92vw, 440px);
  max-height: 90vh;
  padding: 18px;
  border: 0; border-radius: 18px;
  background: var(--card); color: var(--text);
  margin: auto;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { font-size: 19px; margin-bottom: 14px; }
.field { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.field > input, .field > .chips { margin-top: 6px; color: var(--text); }
.dlg-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.spacer { flex: 1; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-height: 38px; padding: 0 12px; border: 1px solid var(--line); border-radius: 19px; background: var(--bg); font-size: 14px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }

@media (max-width: 520px) {
  dialog { margin: auto auto 0; width: 100vw; max-width: 100vw; border-radius: 18px 18px 0 0; padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(76px + env(safe-area-inset-bottom));
  z-index: 100; max-width: 92vw;
  display: flex; align-items: center; gap: 14px;
  background: #2c2c2e; color: #fff; padding: 12px 16px; border-radius: 12px;
  font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.35);
}
.toast button { border: 0; background: none; color: #64b5ff; font-weight: 700; padding: 0; }

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