:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --brand: #1f6f5c;
  --brand-ink: #ffffff;
  --brand-soft: #e3f0eb;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-joint: #898781;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-text: #b42727;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --nav-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --brand: #3fa88b;
    --brand-ink: #0b0b0b;
    --brand-soft: #16302a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --good-text: #0ca30c;
    --critical-text: #e66767;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --brand: #3fa88b;
  --brand-ink: #0b0b0b;
  --brand-soft: #16302a;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --good-text: #0ca30c;
  --critical-text: #e66767;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
a { color: var(--brand); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 24px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); }
.ink-2 { color: var(--ink-2); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--good-text); }
.neg { color: var(--critical-text); }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { display: flex; min-height: 100dvh; }
.sidebar {
  width: 232px; flex-shrink: 0; padding: 20px 12px;
  background: var(--surface); border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 4px 10px 18px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.nav-link {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--ink-2); text-decoration: none; font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--brand-soft); color: var(--brand); }
.nav-link .ico { width: 22px; text-align: center; font-size: 18px; }
.sidebar .spacer { flex: 1; }
main { flex: 1; min-width: 0; padding: 24px 28px 48px; max-width: 1180px; margin: 0 auto; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.bottom-nav { display: none; }

@media (max-width: 820px) {
  .sidebar { display: none; }
  main { padding: 16px 16px calc(var(--nav-h) + var(--safe-bottom) + 24px); }
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    height: calc(var(--nav-h) + var(--safe-bottom)); padding-bottom: var(--safe-bottom);
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 500;
  }
  .bottom-nav a .ico { font-size: 20px; line-height: 1; }
  .bottom-nav a.active { color: var(--brand); }
  h1 { font-size: 21px; }
}

/* ---------- cards & grids ---------- */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1000px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid-3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 680px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } .grid-4 { gap: 10px; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 14px; }
.card-head a { font-size: 14px; text-decoration: none; }

.hero {
  background: var(--brand); color: var(--brand-ink); border: none;
  display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; align-items: flex-end;
}
.hero .label { opacity: 0.85; font-size: 14px; font-weight: 500; }
.hero .figure { font-size: 44px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin: 4px 0; }
.hero .sub { opacity: 0.9; font-size: 14px; }
.hero .side { display: flex; gap: 24px; flex-wrap: wrap; }
.hero .side div b { display: block; font-size: 20px; }
.hero .side div span { font-size: 13px; opacity: 0.85; }
@media (max-width: 680px) { .hero .figure { font-size: 36px; } }

.stat .label { font-size: 13px; color: var(--ink-2); }
.stat .value { font-size: 22px; font-weight: 700; margin-top: 2px; }
.stat .delta { font-size: 13px; margin-top: 2px; }
@media (max-width: 680px) { .stat { padding: 14px; } .stat .value { font-size: 19px; } }

/* ---------- lists ---------- */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 11px 2px;
  border-bottom: 1px solid var(--grid);
}
.row:last-child { border-bottom: none; }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .amount { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row.clickable { cursor: pointer; }
.row.clickable:hover { background: var(--surface-2); }
.emoji {
  width: 38px; height: 38px; border-radius: 10px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 19px; flex-shrink: 0;
}
.day-head { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; padding: 14px 2px 4px; }

/* ---------- progress ---------- */
.progress { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.progress > span { display: block; height: 100%; border-radius: 4px; background: var(--good); }
.progress.over > span { background: var(--critical); }
.progress.close > span { background: var(--warning); }
.progress.info > span { background: var(--series-1); }
.status-tag { font-size: 12px; font-weight: 600; display: inline-flex; gap: 4px; align-items: center; }
.status-tag.over { color: var(--critical-text); }
.status-tag.close { color: var(--ink-2); }
.status-tag.ok { color: var(--good-text); }

.budget-row { padding: 12px 2px; border-bottom: 1px solid var(--grid); }
.budget-row:last-child { border-bottom: none; }
.budget-row .top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.budget-row .top .name { font-weight: 500; }
.budget-row .bottom { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-top: 4px; gap: 8px; }

/* ---------- alerts ---------- */
.alert {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 10px;
  background: var(--surface-2); margin-bottom: 8px; font-size: 14px; border-left: 4px solid var(--series-1);
}
.alert.danger { border-left-color: var(--critical); }
.alert.warn { border-left-color: var(--warning); }
.alert .ico { flex-shrink: 0; }

/* ---------- buttons & forms ---------- */
button, .btn {
  font: inherit; font-weight: 600; font-size: 14px; border-radius: 999px; padding: 9px 16px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; min-height: 40px;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
button.primary:hover { filter: brightness(1.07); }
button.danger { color: var(--critical-text); }
button.ghost { border-color: transparent; background: transparent; }
button.small { padding: 5px 12px; min-height: 32px; font-size: 13px; }
button:disabled { opacity: 0.55; cursor: default; }
.icon-btn { padding: 6px 10px; min-height: 34px; }

label.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 14px; }
label.check { display: flex; align-items: center; gap: 10px; font-size: 15px; margin-bottom: 12px; cursor: pointer; }
label.check input { width: 20px; height: 20px; accent-color: var(--brand); }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--axis); border-radius: 10px; padding: 10px 12px; width: 100%; min-height: 44px; font-weight: 400;
}
textarea { min-height: 120px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; border-color: transparent; }
.field-hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 480px) { .form-row { grid-template-columns: 1fr; gap: 0; } }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar > * { width: auto; flex: 0 1 auto; }
.toolbar input[type="search"] { flex: 1 1 200px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); font-size: 12px; font-weight: 600; color: var(--ink-2);
}
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex-shrink: 0; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--surface); border-radius: 18px; width: 100%; max-width: 480px;
  max-height: calc(100dvh - 32px); overflow: auto; padding: 22px;
}
.modal h2 { margin-bottom: 16px; font-size: 19px; }
.modal .buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.modal .buttons .left { margin-right: auto; }
@media (max-width: 560px) {
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal { border-radius: 18px 18px 0 0; max-height: 92dvh; padding-bottom: calc(22px + var(--safe-bottom)); }
}

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
  background: var(--ink); color: var(--surface); padding: 10px 18px; border-radius: 999px; z-index: 60;
  font-size: 14px; font-weight: 500; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2); max-width: calc(100vw - 32px);
}
@media (min-width: 821px) { .toast { bottom: 24px; } }
.toast.error { background: var(--critical); color: #fff; }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 420px; }
.auth-card .brand { justify-content: center; font-size: 24px; padding-bottom: 6px; }
.auth-card .tagline { text-align: center; color: var(--ink-2); margin: 0 0 22px; }
.tabs { display: flex; background: var(--surface-2); border-radius: 999px; padding: 4px; margin-bottom: 18px; }
.tabs button { flex: 1; border: none; background: transparent; min-height: 36px; }
.tabs button.active { background: var(--surface); box-shadow: var(--shadow); }
.error-text { color: var(--critical-text); font-size: 14px; margin: 0 0 12px; }

/* ---------- charts ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .bar { transition: opacity 0.12s; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .hit:focus + .bar { opacity: 0.8; }
.chart .hit:focus { outline: none; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); margin-bottom: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 13px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12); white-space: nowrap;
}
.tooltip .t-head { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; }
.tooltip .t-row b { font-variant-numeric: tabular-nums; }
.tooltip .key { width: 12px; height: 2px; border-radius: 1px; display: inline-block; }

.stackbar { display: flex; height: 14px; gap: 2px; border-radius: 4px; overflow: hidden; margin: 6px 0 12px; }
.stackbar span { display: block; height: 100%; min-width: 3px; }

details.table-view summary { cursor: pointer; font-size: 13px; color: var(--muted); margin-top: 10px; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 8px; }
table.data th, table.data td { padding: 6px 4px; border-bottom: 1px solid var(--grid); text-align: right; font-variant-numeric: tabular-nums; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 600; }

.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.empty .big { font-size: 34px; margin-bottom: 6px; }
.loading { padding: 48px; text-align: center; color: var(--muted); }
.install-hint { font-size: 14px; }
.kbd { font-family: ui-monospace, monospace; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; }

/* ---------- multi-select ---------- */
.selecting { position: relative; padding-left: 34px !important; cursor: pointer; user-select: none; }
.selecting::before {
  content: ''; position: absolute; left: 6px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; border-radius: 5px; border: 2px solid var(--muted); background: var(--surface);
}
.selected { background: var(--brand-soft); }
.selected::before {
  border-color: var(--brand); background: var(--brand);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
.select-bar {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  width: min(640px, calc(100vw - 24px)); display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--grid); border-radius: 14px; padding: 10px 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
}
.select-bar .check { margin: 0; }
.select-bar .count { font-size: 14px; color: var(--ink-2); flex: 1; white-space: nowrap; }
.select-bar .acts { display: flex; gap: 8px; margin-left: auto; }
@media (min-width: 821px) { .select-bar { bottom: 20px; } }
/* Keep the last rows and any message clear of the bar while selecting. */
body:has(.select-bar) main { padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 130px); }
body:has(.select-bar) .toast { bottom: calc(var(--nav-h) + var(--safe-bottom) + 130px); }
@media (min-width: 821px) {
  body:has(.select-bar) main { padding-bottom: 120px; }
  body:has(.select-bar) .toast { bottom: 100px; }
}
