/* Phone-first ledger. Light/dark follow the system; spacing tuned for
   thumbs (44px+ targets); inputs are 16px so iOS never auto-zooms. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1a202c;
  --muted: #69707d;
  --line: #e6e8ec;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --chip-active: #1f2937;
  --chip-active-ink: #f9fafb;
  --credit: #047857;
  --danger: #b91c1c;
  --pending-bg: #fef3c7;
  --pending-ink: #92400e;
  --review-bg: #fee2e2;
  --review-ink: #991b1b;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.16);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #1a2027;
    --text: #e7eaee;
    --muted: #96a0ad;
    --line: #2a323c;
    --accent: #2dd4bf;
    --accent-ink: #08312d;
    --chip-active: #e5e7eb;
    --chip-active-ink: #111827;
    --credit: #34d399;
    --danger: #f87171;
    --pending-bg: #453510;
    --pending-ink: #fcd34d;
    --review-bg: #46181c;
    --review-ink: #fca5a5;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
               Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
a { color: inherit; text-decoration: none; }

.app {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

/* -- top bar ------------------------------------------------------------ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.topbar h1 { font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }
.topbar-actions { display: flex; align-items: center; gap: 2px; }
.inline-form { display: contents; }
.iconbtn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 12px; color: var(--muted);
}
.iconbtn:active { background: var(--line); }
#refresh-btn.busy svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* -- search + chips ------------------------------------------------------ */

.search-row { padding: 2px 16px 8px; }
.search-row input {
  width: 100%;
  font-size: 16px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.search-row input:focus { border-color: var(--accent); }

.chips {
  display: flex;
  gap: 8px;
  padding: 4px 16px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.chip.active { background: var(--chip-active); border-color: var(--chip-active); color: var(--chip-active-ink); }
.chip.review.active,
.chip.review:not(.active) .badge { background: var(--review-bg); color: var(--review-ink); }
.chip.review.active { border-color: var(--review-bg); }
.select-chip { position: relative; padding-right: 10px; }
.select-chip select {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  padding-right: 2px;
  max-width: 44vw;
  text-overflow: ellipsis;
}
.select-chip svg { pointer-events: none; opacity: 0.6; }
.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--review-bg);
  color: var(--review-ink);
}
.chip.active .badge { background: rgba(255, 255, 255, 0.25); color: inherit; }

/* -- summary ------------------------------------------------------------- */

.summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 8px 20px 2px;
}
.summary-count { font-size: 13px; color: var(--muted); }
.summary-net { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; }
.summary-sub {
  padding: 0 20px;
  font-size: 12px;
  color: var(--muted);
  text-align: right;
}

/* -- day groups + rows --------------------------------------------------- */

.day { margin: 14px 12px 0; }
.day-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0 10px 6px;
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.day-total { font-variant-numeric: tabular-nums; font-weight: 600; }
.day .txn {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 11px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  min-height: 62px;
  -webkit-tap-highlight-color: transparent;
}
.day .txn:first-of-type { border-radius: 14px 14px 0 0; }
.day .txn:last-of-type { border-radius: 0 0 14px 14px; border-bottom: 0; }
.day .txn:first-of-type:last-of-type { border-radius: 14px; }
.day .txn:active { background: color-mix(in srgb, var(--surface) 88%, var(--muted)); }
.cat-dot {
  flex: 0 0 auto;
  width: 12px; height: 12px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  background: #EEEEEE;
}
/* category colors — same pastels as the sheet's ledger */
.cat-dining { background: #FFE0B2; }
.cat-groceries { background: #C8E6C9; }
.cat-travel { background: #B3E5FC; }
.cat-transport { background: #BBDEFB; }
.cat-shopping { background: #E1BEE7; }
.cat-subscriptions { background: #D1C4E9; }
.cat-bills-utilities { background: #CFD8DC; }
.cat-entertainment { background: #F8BBD0; }
.cat-health { background: #B2DFDB; }
.cat-pets { background: #FFF9C4; }
.cat-personal-care { background: #FFCCBC; }
.cat-other { background: #EEEEEE; }
.cat-uncategorized { background: #FFCDD2; box-shadow: 0 0 0 2px color-mix(in srgb, #ef4444 30%, transparent); }
.txn-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.txn-merchant {
  font-size: 15px;
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.txn-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.txn-right { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.txn-amount { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.txn-amount.big { font-weight: 800; }
.txn-amount.credit { color: var(--credit); }
.txn-amount.pay { color: var(--muted); font-weight: 500; font-style: italic; }
.pending-tag {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--pending-bg);
  color: var(--pending-ink);
}

.empty {
  text-align: center;
  padding: 70px 20px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.noscript-note { text-align: center; color: var(--muted); padding: 20px; }

/* -- FAB ----------------------------------------------------------------- */

.fab {
  position: fixed;
  right: max(18px, env(safe-area-inset-right));
  bottom: calc(22px + env(safe-area-inset-bottom));
  width: 58px; height: 58px;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-ink);
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
  z-index: 25;
}
.fab:active { transform: scale(0.94); }

/* -- bottom sheet -------------------------------------------------------- */

.backdrop {
  position: fixed; inset: 0;
  background: rgba(10, 14, 18, 0.45);
  z-index: 30;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.backdrop.open { opacity: 1; }
.sheet {
  position: fixed;
  left: 50%;
  transform: translate(-50%, 100%);
  bottom: 0;
  width: min(100%, 640px);
  max-height: 90dvh;
  z-index: 31;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0.22, 1);
  overflow: hidden;
  display: flex;
}
.sheet.open { transform: translate(-50%, 0); }
.sheet-form { display: flex; flex-direction: column; width: 100%; max-height: 90dvh; }
.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { flex: 1; font-size: 16px; font-weight: 700; text-align: center; }
.textbtn { font-weight: 700; padding: 8px 12px; border-radius: 10px; color: var(--muted); }
.textbtn.primary { color: var(--accent); font-size: 15px; }
.textbtn.primary:active { background: var(--line); }
.sheet-body {
  padding: 16px 16px calc(18px + env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 0; }
.field > span { font-size: 12px; font-weight: 650; color: var(--muted); }
.field input, .field select {
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  width: 100%;
  min-height: 44px;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field-row { display: flex; gap: 10px; }

.seg-row { display: flex; flex-direction: column; gap: 10px; }
.seg {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 3px;
  gap: 2px;
}
.seg-opt { flex: 1; }
.seg-opt input { position: absolute; opacity: 0; pointer-events: none; }
.seg-opt span {
  display: block;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 4px;
  border-radius: 8px;
  color: var(--muted);
  text-transform: capitalize;
}
.seg-opt input:checked + span {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.14);
}

.rulebox {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}
.checkline { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 600; }
.checkline input { width: 20px; height: 20px; accent-color: var(--accent); }
.rule-fields { display: flex; flex-direction: column; gap: 8px; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.45; }
.meta { font-size: 12px; color: var(--muted); line-height: 1.5; overflow-wrap: anywhere; }

.danger-btn {
  margin-top: 4px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  color: var(--danger);
  font-weight: 700;
  text-align: center;
}
.danger-btn.confirm { background: var(--danger); border-color: var(--danger); color: #fff; }

/* -- toast --------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  bottom: calc(96px + env(safe-area-inset-bottom));
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 14px;
  background: #22272e;
  color: #f3f4f6;
  font-size: 14px;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 14px;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); color: #fff; }
.toast button { color: #5eead4; font-weight: 800; white-space: nowrap; }

/* -- login --------------------------------------------------------------- */

.login-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: min(100%, 360px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-mark {
  width: 56px; height: 56px;
  margin: 0 auto;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 30px;
  font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.login-card h1 { font-size: 20px; font-weight: 800; }
.login-card input {
  font-size: 16px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  text-align: center;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  padding: 12px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}
.login-card button:disabled { opacity: 0.5; }
.login-error { color: var(--danger); font-size: 13px; font-weight: 600; }

/* -- misc ---------------------------------------------------------------- */

.loading { opacity: 0.55; pointer-events: none; transition: opacity 0.15s; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
