:root {
  color-scheme: light;
  --bg: #f1f5f9;
  --ink: #0f172a;
  --muted: #64748b;
  --card: #ffffff;
  --line: #e2e8f0;
  --press: #e2e8f0;
  --off-bg: #cbd5e1;
  --off-ink: #94a3b8;
  --blue: #2563eb;
  --green: #16a34a;
  --orange: #ea580c;
  --purple: #7c3aed;
  --slate: #475569; /* neutral */
  --radius: 18px;
}

/* Dark: follows the system unless the user picked a theme (data-theme set by script). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b1220; --ink: #f1f5f9; --muted: #94a3b8; --card: #172033;
    --line: #26324a; --press: #26324a; --off-bg: #26324a; --off-ink: #64748b;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1220; --ink: #f1f5f9; --muted: #94a3b8; --card: #172033;
  --line: #26324a; --press: #26324a; --off-bg: #26324a; --off-ink: #64748b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 500 17px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  touch-action: manipulation;
}

button, input { font: inherit; color: inherit; }
button { border: 0; cursor: pointer; }
button:disabled { cursor: default; }

.btn {
  width: 100%;
  min-height: 64px;
  border-radius: var(--radius);
  font-size: 22px;
  font-weight: 800;
  color: #fff;
}
.btn.green { background: var(--green); }
.btn:active { filter: brightness(.9); transform: scale(.99); }
.btn:disabled { background: var(--off-bg); color: var(--off-ink); filter: none; transform: none; }

input {
  width: 100%;
  min-height: 60px;
  padding: 0 18px;
  border: 3px solid transparent;
  border-radius: var(--radius);
  background: var(--card);
  font-size: 20px;
  outline: none;
}
input:focus { border-color: var(--ink); }
input::placeholder { color: var(--muted); opacity: .8; }

.icon-btn {
  width: 52px;
  height: 52px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--bg);
  font-size: 28px;
  line-height: 1;
}
.icon-btn:active { background: var(--press); }
.icon-btn:disabled { opacity: .3; }
.icon-btn.danger { background: var(--slate); color: #fff; font-size: 24px; }
.icon-btn.danger.armed { background: var(--orange); width: auto; padding: 0 20px; border-radius: 28px; font-size: 18px; font-weight: 800; }

/* ---------- login ---------- */
#login { max-width: 560px; margin: 0 auto; padding: 12vh 16px 0; display: flex; flex-direction: column; gap: 14px; }
.logo { display: block; width: 76px; height: 76px; margin: 0 auto 18px; }
#code { font-size: 32px; letter-spacing: .3em; text-align: center; font-variant-numeric: tabular-nums; }
#loginErr, #sheetErr { margin: 0; color: var(--orange); font-weight: 700; text-align: center; }
#loginErr:empty, #sheetErr:empty { display: none; }

/* ---------- navbar ---------- */
nav {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.nav-in {
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}
.month-label { padding: 6px 8px; border-radius: 12px; background: none; font-size: 20px; font-weight: 800; white-space: nowrap; }
.month-label:active { background: var(--press); }
#menuBtn { margin-left: auto; }

/* ---------- home ---------- */
.wrap { max-width: 560px; margin: 0 auto; padding: 16px 16px 0; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg-btn {
  --c: var(--orange);
  min-height: 64px;
  border-radius: var(--radius);
  background: var(--card);
  color: var(--c);
  box-shadow: inset 0 0 0 3px var(--c);
  font-size: 30px;
  font-weight: 800;
}
.seg-btn[data-c="USD"] { --c: var(--blue); }
.seg-btn.on { background: var(--c); color: #fff; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.chip {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--c);
  color: #fff;
  font-weight: 800;
  font-size: 15px;
  white-space: nowrap;
}

#list { list-style: none; margin: 0; padding: 0 0 200px; display: flex; flex-direction: column; gap: 10px; }
.chips:empty { display: none; }
#list { margin-top: 14px; }
.chips:not(:empty) + #list { margin-top: 0; }
.row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px 10px 10px;
  border-radius: var(--radius);
  background: var(--card);
  text-align: left;
}
.row:active { background: var(--press); }
.row .ico {
  position: relative;
  flex: none;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: var(--c);
  display: grid;
  place-items: center;
  font-size: 26px;
}
.row .mid { flex: 1; min-width: 0; }
.row .badge {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--card);
  font-size: 13px;
  box-shadow: 0 0 0 2px var(--card);
}
.row .name { display: block; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { display: block; color: var(--muted); font-size: 14px; }
.row .amt { max-width: 52%; text-align: right; font-weight: 800; font-variant-numeric: tabular-nums; }
.row .amt b { font-weight: 800; white-space: nowrap; }
.row .amt .usd { color: var(--muted); font-weight: 700; font-size: 15px; white-space: nowrap; }
.row.miss, .row.check { box-shadow: inset 0 0 0 3px var(--orange); }
.row.ghost { background: transparent; box-shadow: inset 0 0 0 2px var(--off-bg); }
.row.ghost .ico, .row.ghost .amt { opacity: .55; }
.day { padding: 10px 6px 0; color: var(--muted); font-size: 13px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
.day:first-child { padding-top: 0; }
.row.miss .amt b { color: var(--orange); }
.empty { text-align: center; font-size: 40px; padding: 30px 0; opacity: .5; }

.fab-wrap {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 24px 16px calc(16px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 28%);
  pointer-events: none;
}
.fab-in { max-width: 528px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; pointer-events: auto; }
.total-bar {
  --c: var(--blue);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 12px 20px;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: inset 0 0 0 3px var(--c);
}
.total-bar span { color: var(--muted); font-weight: 700; }
.total-bar b { color: var(--c); font-size: 28px; font-weight: 800; font-variant-numeric: tabular-nums; }
.fab-in .btn { box-shadow: 0 6px 20px rgb(22 163 74 / .35); }

/* ---------- hamburger menu ---------- */
#menuWrap { position: fixed; inset: 0; z-index: 20; background: rgb(15 23 42 / .55); }
#menu {
  position: absolute;
  top: calc(68px + env(safe-area-inset-top));
  left: 12px; right: 12px;
  max-width: 380px;
  margin-left: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-radius: 26px;
  background: var(--card);
  box-shadow: 0 20px 50px rgb(0 0 0 / .35);
}
.theme-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.theme-btn {
  min-height: 60px;
  border-radius: var(--radius);
  background: var(--bg);
  font-size: 18px;
  font-weight: 800;
}
.theme-btn.on { background: var(--purple); color: #fff; }
.menu-btn {
  min-height: 60px;
  padding: 0 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-radius: var(--radius);
  background: var(--c);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  text-align: left;
}
.menu-btn:active { filter: brightness(.9); }

/* ---------- add / edit sheet ---------- */
dialog {
  border: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  width: 100%;
  max-width: 100%;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0;
  inset: 0;
  flex-direction: column;
}
dialog[open] { display: flex; }
dialog::backdrop { background: rgb(15 23 42 / .6); }
@media (min-width: 600px) {
  dialog { max-width: 520px; height: auto; max-height: 92dvh; margin: auto; border-radius: 28px; }
}

.sheet-top { display: flex; justify-content: space-between; padding: calc(14px + env(safe-area-inset-top)) 16px 0; }
.sheet-top .icon-btn:not(.danger) { background: var(--card); }
.sheet-body { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 14px; }
.sheet-foot { padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; }

.money { position: relative; }
.money input { min-height: 80px; padding: 0 56px; font-size: 40px; font-weight: 800; text-align: center; font-variant-numeric: tabular-nums; }
.money .sym { position: absolute; left: 22px; top: 50%; transform: translateY(-50%); font-size: 34px; font-weight: 800; color: var(--orange); pointer-events: none; }
.money .sym.usd { color: var(--blue); }

.rep { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.rep-btn { min-height: 60px; padding: 0 4px; border-radius: var(--radius); background: var(--card); font-size: 16px; font-weight: 800; }
.rep-btn.on { background: var(--purple); color: #fff; }

.series-box {
  min-height: 60px;
  padding: 0 8px 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-radius: var(--radius);
  background: var(--card);
  font-weight: 800;
}
.pill { min-height: 44px; padding: 0 18px; border-radius: 999px; background: var(--slate); color: #fff; font-weight: 800; }
.pill.armed { background: var(--orange); }

.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.cat {
  min-height: 68px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-radius: var(--radius);
  background: var(--c);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  text-align: left;
  transition: opacity .12s;
}
.cat .e { font-size: 26px; }
.cat:last-child:nth-child(odd) { grid-column: 1 / -1; }
.cat-grid.picked .cat { opacity: .3; }
.cat-grid.picked .cat.on { opacity: 1; box-shadow: 0 0 0 4px var(--bg), 0 0 0 7px var(--c); }
.cat:active { filter: brightness(.9); }

/* ---------- motion ---------- */
@keyframes rise { from { transform: translateY(28px); opacity: 0; } }
@keyframes drop { from { transform: translateY(-8px) scale(.98); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
dialog[open] { animation: rise .22s ease-out; }
dialog[open]::backdrop { animation: fade .22s ease-out; }
#menuWrap:not([hidden]) { animation: fade .15s ease-out; }
#menuWrap:not([hidden]) #menu { animation: drop .18s ease-out; transform-origin: top right; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after, dialog::backdrop { animation: none !important; transition: none !important; }
}
