:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --border: #e3e0d8;
  --text: #1d1f1c;
  --muted: #6b6f68;
  --accent: #2f6f4f;
  --accent-ink: #ffffff;
  --accent-soft: #e5efe9;
  --debit: #a8402f;
  --credit: #2f6f4f;
  --danger: #b3261e;
  --focus: #2f6f4f55;
  --recurring: #fffbe3;
  --recurring-hover: #fff6c7;
  --recurring-edge: #e9d98a;
  --radius: 8px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
  --sans: "Segoe UI Variable", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141614;
    --surface: #1c1f1c;
    --surface-2: #202420;
    --border: #2f342f;
    --text: #e7e9e4;
    --muted: #9aa097;
    --accent: #5fb38a;
    --accent-ink: #0d1a13;
    --accent-soft: #233229;
    --debit: #e58a78;
    --credit: #6fc79a;
    --danger: #f2867e;
    --focus: #5fb38a55;
    --recurring: #2b2818;
    --recurring-hover: #34301c;
    --recurring-edge: #5c5326;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
}

h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 0; }
.money { font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--debit); }

/* ---- Controls ---- */

input, select, button { font: inherit; color: inherit; }

input, select {
  width: 100%;
  padding: 7px 9px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}
input:focus, select:focus, button:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus);
}
input[type="checkbox"] { width: auto; accent-color: var(--accent); }

label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label > input, label > select { color: var(--text); font-size: 14px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: none;
  cursor: pointer;
  font-weight: 500;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { border-color: var(--border); background: var(--surface); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-link { color: var(--muted); padding: 4px; }
.btn-link:hover { color: var(--text); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-icon { padding: 3px 7px; font-size: 13px; color: var(--muted); }
.btn-icon:hover { color: var(--text); background: var(--surface-2); }

.stack { display: grid; gap: 14px; }
.row { display: flex; gap: 12px; }
.row > label { flex: 1; }
.row-end { justify-content: flex-end; }

/* ---- Brand ---- */

.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 16px; letter-spacing: 0.01em; }
.brand-mark {
  display: grid; place-items: center;
  width: 26px; height: 26px; border-radius: 6px;
  background: var(--accent); color: var(--accent-ink);
  font-family: Georgia, serif; font-weight: 700;
}
.brand-lg { font-size: 22px; justify-content: center; margin-bottom: 8px; }
.brand-lg .brand-mark { width: 34px; height: 34px; font-size: 20px; }

/* ---- Login ---- */

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%;
  max-width: 360px;
  display: grid;
  gap: 20px;
  padding: 32px 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.login-card p { margin: 0; }
.code-input { font-family: var(--mono); font-size: 22px; letter-spacing: 0.3em; text-align: center; }

/* ---- App layout ---- */

.app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 270px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top" "side main";
}
.topbar {
  grid-area: top;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-right { display: flex; align-items: center; gap: 12px; }

.sidebar {
  grid-area: side;
  padding: 18px 12px;
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 6px;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 0 8px 6px; }
.sidebar-head h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.group-label { font-size: 12px; color: var(--muted); padding: 10px 8px 4px; }
.account-link {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 8px; border-radius: 6px;
  color: inherit; text-decoration: none;
}
.account-link:hover { background: var(--surface); }
.account-link.active { background: var(--accent-soft); }
.account-link .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-link.archived .name { color: var(--muted); font-style: italic; }
.sidebar-total {
  display: flex; justify-content: space-between;
  margin-top: 8px; padding: 10px 8px 0;
  border-top: 1px solid var(--border);
  font-weight: 600;
}
.show-archived { display: flex; flex-direction: row; align-items: center; gap: 6px; padding: 8px; }

.main { grid-area: main; padding: 24px 28px; min-width: 0; }

.empty { max-width: 460px; margin: 60px auto; text-align: center; color: var(--muted); }
.empty h1 { color: var(--text); font-size: 20px; margin-bottom: 8px; }

/* ---- Register ---- */

.account-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.account-head h1 { font-size: 22px; }
.account-head .type { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.balances { display: flex; gap: 28px; }
.balance { text-align: right; }
.balance .label { font-size: 12px; color: var(--muted); }
.balance .money { font-size: 20px; font-weight: 600; }
.account-actions { display: flex; gap: 6px; margin-top: 10px; }

.register-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table.register { width: 100%; border-collapse: collapse; }
.register th, .register td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.register th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); text-transform: uppercase; letter-spacing: 0.05em; }
.register tbody tr:last-child td { border-bottom: none; }
.register .num { text-align: right; }
.register .c { text-align: center; width: 36px; }
.register .actions { width: 84px; text-align: right; white-space: nowrap; }
.register td.date { white-space: nowrap; font-family: var(--mono); font-size: 13px; }
.register td .memo { display: block; font-size: 12px; color: var(--muted); }
.register tbody tr.txn:hover { background: var(--surface-2); }
.register tr.txn .actions button { visibility: hidden; }
.register tr.txn:hover .actions button, .register tr.txn:focus-within .actions button { visibility: visible; }
.register tr.entry td { background: var(--accent-soft); padding: 6px 4px; }
.register tr.entry input { padding: 5px 7px; font-size: 13px; }
.register tr.entry input.amount { text-align: right; font-family: var(--mono); }
.register tr.entry .desc-cell { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 4px; }
.register tr.entry .actions { width: auto; }
.register col.date { width: 130px; }
.register col.amt { width: 120px; }
.register-empty { padding: 28px; text-align: center; color: var(--muted); }
.register .actions { width: 120px; }

/* Recurring: recorded occurrences and scheduled (projected) ones share the yellow tint. */
.register tr.recurring td, .register tr.projected td { background: var(--recurring); }
.register tr.recurring:hover td { background: var(--recurring-hover); }
.register tr.projected td { color: var(--muted); font-style: italic; border-bottom-style: dashed; }
.register tr.projected td .money { font-style: normal; }
.register tr.projected .actions button { font-style: normal; }
.register tr.projected.due td:first-child { color: var(--text); }
.register tr.section td {
  padding: 5px 10px;
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); background: var(--surface-2);
}
.badge {
  display: inline-block; margin-left: 6px; padding: 0 6px;
  border-radius: 4px; font: 600 10px/16px var(--sans); font-style: normal;
  text-transform: uppercase; letter-spacing: 0.04em;
  background: var(--recurring-edge); color: var(--text);
}

.balance.projected .label::before { content: "↻ "; }
.balance.projected .money { color: var(--muted); }

.schedules { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: -6px 0 14px; }
.schedules-label { font-size: 12px; color: var(--muted); margin-right: 4px; }
.schedule-chip {
  padding: 3px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--recurring-edge); background: var(--recurring);
  font-size: 12px;
}
.schedule-chip:hover { background: var(--recurring-hover); }
.schedule-chip .money { font-size: 12px; }

.dialog-note { margin: -6px 0 0; font-size: 12px; }
.dialog-note:empty { display: none; }
.spacer { flex: 1; }

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

dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: #0006; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  padding: 10px 16px; border-radius: 8px;
  background: var(--text); color: var(--bg);
  box-shadow: 0 6px 24px #0003;
}

/* ---- Narrow screens ---- */

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "side" "main"; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .main { padding: 16px; }
  .register .memo-col { display: none; }
}
