:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #256abf;
  --accent-ink: #ffffff;
  --good: #006300;
  --warn: #a35a00;
  --warn-bg: #fff4dc;
  --danger: #b42323;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --good: #0ca30c;
    --warn: #f0a83a;
    --warn-bg: #2e2412;
    --danger: #e66767;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.1); --accent: #3987e5;
  --good: #0ca30c; --warn: #f0a83a; --warn-bg: #2e2412; --danger: #e66767;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 0.5rem; }
h2 { font-size: 1.35rem; margin: 0.5rem 0 0.75rem; }
h3 { font-size: 1.05rem; display: flex; align-items: center; gap: 0.5rem; }
h4 { font-size: 0.9rem; color: var(--ink-2); margin: 1rem 0 0.4rem; }
a { color: var(--accent); }
.muted { color: var(--ink-2); }
.small { font-size: 0.85rem; }
.good { color: var(--good); }
.warn { color: var(--warn); }
.center { text-align: center; }

/* Header */
.top {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: grid; grid-template-columns: 1fr auto; gap: 0 1rem;
  padding: 0.75rem 16px 0;
}
.brand h1 { font-size: 1.15rem; margin: 0; }
.brand p { margin: 0; color: var(--ink-2); font-size: 0.85rem; }
.io { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
#sync { font-size: 0.78rem; color: var(--muted); display: inline-flex; align-items: center; gap: 0.35rem; }
#sync::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--axis); }
#sync[data-state="cloud"]::before { background: var(--good); }
#sync[data-state="saving"]::before { background: var(--accent); }
#sync[data-state="error"] { color: var(--warn); }
#sync[data-state="error"]::before { background: var(--warn); }
#tabs { grid-column: 1 / -1; display: flex; gap: 0.25rem; overflow-x: auto; margin-top: 0.5rem; scrollbar-width: none; }
#tabs button {
  border: 0; background: none; color: var(--ink-2); padding: 0.55rem 0.75rem; border-radius: 0;
  border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 500;
}
#tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

main { max-width: 1180px; margin: 0 auto; padding: 1rem 16px 4rem; }

/* Controls */
button {
  font: inherit; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  padding: 0.45rem 0.85rem; border-radius: 8px;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.ghost { background: transparent; }
button.danger { color: var(--danger); }
button.danger-solid { background: var(--danger); color: var(--accent-ink); border-color: transparent; }
button.link { border: 0; background: none; color: var(--accent); padding: 0.2rem 0.3rem; }
button.link.danger { color: var(--danger); }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--axis); border-radius: 6px; padding: 0.4rem 0.5rem; width: 100%; min-width: 0;
}
input[type="checkbox"] { width: auto; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(230px, 100%), 1fr)); gap: 0.75rem 1rem; margin: 0.5rem 0; }
.field { display: flex; flex-direction: column; gap: 0.25rem; }
.field.wide { grid-column: 1 / -1; }
.field > span { font-size: 0.85rem; font-weight: 500; color: var(--ink-2); }
.field small { color: var(--muted); font-size: 0.78rem; line-height: 1.35; }
.affix { display: flex; align-items: stretch; }
.affix input { border-radius: 0; }
.affix input:first-child { border-top-left-radius: 6px; border-bottom-left-radius: 6px; }
.affix input:last-child { border-top-right-radius: 6px; border-bottom-right-radius: 6px; }
.affix em {
  font-style: normal; font-size: 0.85rem; color: var(--ink-2); background: var(--surface-2);
  border: 1px solid var(--axis); padding: 0.4rem 0.5rem; display: flex; align-items: center; white-space: nowrap;
}
.affix em:first-child { border-right: 0; border-radius: 6px 0 0 6px; }
.affix em:last-child { border-left: 0; border-radius: 0 6px 6px 0; }

/* Cards & layout */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.1rem; margin-bottom: 1rem; box-shadow: var(--shadow); min-width: 0;
}
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 0 1rem; }
.toolbar { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.5rem; }
.toolbar > span { flex: 1; }
.toolbar h2, .toolbar h3 { margin: 0; }
.notice { padding: 0.6rem 0.9rem; border-radius: 8px; margin-bottom: 1rem; }
.notice.warn { background: var(--warn-bg); }
.empty { color: var(--muted); padding: 1rem 0; }
.empty-state ol { padding-left: 1.2rem; }
.empty-state li { margin-bottom: 0.4rem; }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem 1rem; display: flex; flex-direction: column; }
.kpi span { color: var(--ink-2); font-size: 0.85rem; }
.kpi b { font-size: 1.7rem; font-weight: 600; letter-spacing: -0.01em; }
.kpi small { color: var(--muted); font-size: 0.75rem; }

.project h3 i, .legend i, .tip i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.bar { height: 10px; background: var(--surface-2); border-radius: 5px; overflow: hidden; margin: 0.5rem 0; }
.bar span { display: block; height: 100%; border-radius: 5px; }
.pb { margin: 0 0 0.75rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 0.5rem 1rem; margin: 0; }
.stats div { margin: 0; }
.stats dt { color: var(--ink-2); font-size: 0.8rem; }
.stats dd { margin: 0; font-weight: 600; }
.stats dd.small { font-weight: 400; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -0.25rem; }
table { border-collapse: collapse; width: 100%; }
.data th, .data td { padding: 0.45rem 0.5rem; border-bottom: 1px solid var(--grid); text-align: left; white-space: nowrap; }
.data thead th { font-size: 0.8rem; font-weight: 600; color: var(--ink-2); border-bottom-color: var(--axis); }
.data tfoot th { border-bottom: 0; border-top: 1px solid var(--axis); }
.data .num, .breakdown td, .breakdown tfoot th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
.data.edit td { padding: 0.25rem 0.3rem; }
.data.edit input { padding: 0.3rem 0.4rem; min-width: 5.5rem; }
.data.edit input[type="date"] { min-width: 9rem; }
.rates input { min-width: 6.25rem; }
.rates input.note { min-width: 12rem; }
.rates tr.unverified td:first-child { box-shadow: inset 3px 0 0 var(--warn); }
.split input { min-width: 4rem; }
tr.err td { background: var(--warn-bg); }
.badge { display: inline-block; font-size: 0.72rem; padding: 0.05rem 0.45rem; border-radius: 999px; font-weight: 600; }
.badge.ok { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.badge.amber { background: var(--warn-bg); color: var(--warn); }
label.check { display: inline-flex; gap: 0.3rem; align-items: center; cursor: pointer; }

.months { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 0.4rem; }
.months span { background: var(--surface-2); border-radius: 6px; padding: 0.3rem 0.5rem; display: flex; flex-direction: column; font-variant-numeric: tabular-nums; }
.months b { font-size: 0.75rem; color: var(--ink-2); }

/* Charts */
.chart { position: relative; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .dlabel { fill: var(--ink-2); font-size: 11px; font-weight: 600; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .tip {
  position: absolute; pointer-events: none; z-index: 2;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); padding: 0.4rem 0.6rem; font-size: 0.8rem; min-width: 160px;
}
.chart .tip div { display: flex; align-items: center; gap: 0.4rem; }
.chart .tip span { margin-left: auto; padding-left: 1rem; font-variant-numeric: tabular-nums; font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; font-size: 0.85rem; color: var(--ink-2); margin-bottom: 0.25rem; }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }

/* Dialogs */
dialog { border: 0; border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink); max-width: min(980px, calc(100vw - 32px)); width: 100%; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
#welcome { max-width: 520px; }
#ask { max-width: 420px; }
.ask { padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem; }
.ask p { margin: 0; }
.ask .actions { justify-content: flex-end; }
.welcome { padding: 1.5rem; }
.editor header, .editor footer { display: flex; align-items: center; gap: 0.5rem; padding: 0.9rem 1.1rem; }
.editor header { border-bottom: 1px solid var(--border); }
.editor header h2 { margin: 0; flex: 1; font-size: 1.15rem; }
.editor footer { border-top: 1px solid var(--border); }
.editor footer span { flex: 1; }
.editor-body { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 1.25rem; padding: 1rem 1.1rem; max-height: calc(100vh - 180px); overflow: auto; }
.period-grid { margin: 0.75rem 0; }
.period-grid th, .period-grid td { padding: 0.25rem 0.3rem; text-align: left; font-size: 0.85rem; }
.period-grid thead th { color: var(--ink-2); font-weight: 600; }
.preview { background: var(--surface-2); border-radius: 10px; padding: 0.75rem; align-self: start; }
.breakdown th, .breakdown td { padding: 0.25rem 0.3rem; font-size: 0.85rem; border-bottom: 1px solid var(--grid); }
.breakdown thead th { color: var(--ink-2); text-align: right; }
.breakdown td:first-child { text-align: left; color: var(--ink-2); }
.breakdown tfoot th { text-align: left; }
details.adv { margin-top: 0.75rem; }
details.adv summary { cursor: pointer; color: var(--accent); font-size: 0.9rem; }

#toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--page); padding: 0.5rem 1rem; border-radius: 8px; z-index: 20; }

@media (max-width: 720px) {
  .editor-body { grid-template-columns: 1fr; }
  .brand p { display: none; }
  #sync { order: 3; width: 100%; justify-content: flex-end; }
  .kpi b { font-size: 1.4rem; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .card { padding: 0.85rem; }
  dialog { max-width: calc(100vw - 16px); }
}

/* Bill import */
.dropzone {
  border: 1.5px dashed var(--axis); border-radius: var(--radius); padding: 1rem 1.1rem; margin: 0.25rem 0 1rem;
  display: flex; flex-direction: column; gap: 0.2rem; background: var(--surface);
}
.dropzone b { font-weight: 600; }
.dropzone button.link { padding: 0; font: inherit; }
#drop-overlay {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--page) 80%, transparent);
}
#drop-overlay[hidden] { display: none; }
#drop-overlay div {
  border: 2px dashed var(--accent); border-radius: 16px; padding: 2.5rem 2rem; max-width: 520px; text-align: center;
  background: var(--surface); font-size: 1.1rem; font-weight: 600;
}
.imp-body { padding: 0.75rem 1.1rem; max-height: calc(100vh - 180px); overflow: auto; }
.imp-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.75rem; }
.imp { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.75rem 0.9rem; display: flex; flex-direction: column; gap: 0.35rem; min-width: 0; }
.imp p { margin: 0; }
.imp.err { background: var(--warn-bg); }
.imp-head { display: flex; flex-wrap: wrap; gap: 0.4rem 0.75rem; align-items: center; justify-content: space-between; }
.check-row { display: flex; gap: 0.5rem; align-items: baseline; font-size: 0.9rem; cursor: pointer; }
.check-row input { flex: none; transform: translateY(1px); }
.field.inline { max-width: 320px; }
.badge { background: var(--surface-2); color: var(--ink-2); }

/* Date field + calendar */
.datefield { display: flex; align-items: stretch; min-width: 0; }
.datefield .date-text { border-radius: 6px 0 0 6px; min-width: 7.5rem; font-variant-numeric: tabular-nums; }
.datefield .date-btn {
  border: 1px solid var(--axis); border-left: 0; border-radius: 0 6px 6px 0; background: var(--surface-2);
  color: var(--ink-2); padding: 0 0.55rem; display: inline-flex; align-items: center;
}
.datefield .date-btn:hover { color: var(--ink); }
.datefield.invalid .date-text { border-color: var(--danger); box-shadow: inset 0 0 0 1px var(--danger); }
.data.edit .datefield .date-text { min-width: 7.5rem; }
.datepop {
  position: fixed; z-index: 40; width: 272px; padding: 0.6rem; border-radius: 12px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}
.dp-head { display: grid; grid-template-columns: auto 1fr auto auto; gap: 0.3rem; align-items: center; margin-bottom: 0.4rem; }
.dp-head select { padding: 0.3rem 0.35rem; font-size: 0.9rem; }
.dp-head .dp-year { width: auto; }
.dp-nav { padding: 0.2rem 0.55rem; font-size: 1.1rem; line-height: 1; }
.dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.dp-grid abbr { text-decoration: none; text-align: center; font-size: 0.72rem; color: var(--muted); padding: 0.2rem 0; }
.dp-grid button {
  border: 0; background: none; padding: 0; height: 2.1rem; border-radius: 8px; font-variant-numeric: tabular-nums; font-size: 0.9rem;
}
.dp-grid button:hover { background: var(--surface-2); }
.dp-grid button.today { box-shadow: inset 0 0 0 1px var(--axis); }
.dp-grid button.sel { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.dp-foot { display: flex; justify-content: space-between; margin-top: 0.4rem; }
@media (prefers-reduced-motion: no-preference) { .datepop { animation: dp-in 120ms ease-out; } }
@keyframes dp-in { from { transform: translateY(-4px); } to { transform: none; } }
.notice-inline { background: var(--warn-bg); border-radius: 8px; padding: 0.4rem 0.6rem; margin: 0 0 0.5rem; }
