:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --border-strong: #cfcdc7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --sales: #2a78d6;
  --expense: #eb6834;
  --base: #a3a29c;       /* "period A" in comparisons */
  --pos: #0b7a0b;         /* positive delta text */
  --neg: #c03434;         /* negative delta text */
  --pos-bar: #2a78d6;
  --neg-bar: #e34948;
  --grid: #ebeae6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #2e2e2b; --border-strong: #42423e;
    --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --sales: #3987e5; --expense: #d95926; --base: #6f6e68;
    --pos: #4cc24c; --neg: #ef7b7b; --pos-bar: #3987e5; --neg-bar: #e66767; --grid: #2a2a27;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #2e2e2b; --border-strong: #42423e;
  --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --sales: #3987e5; --expense: #d95926; --base: #6f6e68;
  --pos: #4cc24c; --neg: #ef7b7b; --pos-bar: #3987e5; --neg-bar: #e66767; --grid: #2a2a27;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 17px; }
h2 { font-size: 15px; }
button, input, select { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 18px;
}
.sub { margin: 0; color: var(--text-2); font-size: 12.5px; }
.controls { display: flex; align-items: flex-end; gap: 8px; flex-wrap: wrap; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  background: none; border: 0; padding: 12px 14px; cursor: pointer; color: var(--text-2);
  border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 500;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 20px 24px 60px; }
.panel { display: none; }
.panel.active { display: block; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.icon-btn { background: none; border: 0; font-size: 22px; line-height: 1; cursor: pointer; color: var(--text-2); }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--text-2); font-weight: 500; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
.field input, .field select {
  height: 36px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface); min-width: 0; width: 100%;
}
.field input:focus, .field select:focus, .btn:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.field.invalid input, .field.invalid select { border-color: var(--neg); }

.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 6px 12px; cursor: pointer; color: var(--text-2); font-size: 13px; }
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* ---------- cards & layout ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-2 > .card { margin-bottom: 16px; }

.chart { position: relative; height: 260px; }
.chart.tall { height: 320px; }
.chart.short { height: 200px; }

.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.sw.sales { background: var(--sales); }
.sw.expense { background: var(--expense); }
.sw.base { background: var(--base); }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.kpi .value { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.kpi .meta { font-size: 12px; color: var(--text-3); margin-top: 4px; }
.kpi .meta b { font-weight: 600; }

.delta { font-weight: 600; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--pos); }
.delta.down { color: var(--neg); }
.delta.flat { color: var(--text-3); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-2); font-weight: 600; font-size: 12px; background: var(--surface-2); position: sticky; top: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.weekend td { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
tr.loss td.profit { color: var(--neg); }
tfoot td { font-weight: 650; border-top: 2px solid var(--border-strong); }
td.desc { white-space: normal; min-width: 180px; color: var(--text-2); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.pill.sale { background: color-mix(in srgb, var(--sales) 16%, transparent); color: var(--text); }
.pill.expense { background: color-mix(in srgb, var(--expense) 18%, transparent); color: var(--text); }
.row-actions button { background: none; border: 0; cursor: pointer; color: var(--text-2); padding: 2px 6px; border-radius: 6px; }
.row-actions button:hover { background: var(--surface-2); color: var(--text); }

details.table-toggle { margin-top: 12px; }
details.table-toggle summary { cursor: pointer; color: var(--accent); font-weight: 500; font-size: 13px; }
details.table-toggle .table-wrap { max-height: 420px; overflow: auto; margin-top: 10px; }

.hint { color: var(--text-3); font-size: 12.5px; margin: 10px 0 0; }

/* ---------- filters ---------- */
.filters { display: grid; grid-template-columns: repeat(5, minmax(120px, 1fr)) minmax(180px, 2fr); gap: 10px; margin-bottom: 12px; }
.filter-totals { display: flex; gap: 20px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); margin-bottom: 10px; }
.filter-totals b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- compare ---------- */
.compare-form { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.compare-form .field { width: 190px; }
.vs { padding-bottom: 8px; color: var(--text-3); font-weight: 600; }
.quick { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 4px; }
.quick button { font-size: 12.5px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border-strong); background: transparent; cursor: pointer; color: var(--text-2); }
.quick button:hover { color: var(--text); background: var(--surface-2); }
.cmp-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; margin-top: 16px; }
.cmp-section h3 { font-size: 13.5px; margin: 18px 0 8px; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(10, 10, 10, .45); }
#entryForm { padding: 18px 20px 20px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.type-switch { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 9px; overflow: hidden; margin-bottom: 14px; }
.type-switch input { position: absolute; opacity: 0; pointer-events: none; }
.type-switch span { display: block; padding: 7px 20px; cursor: pointer; color: var(--text-2); font-weight: 500; }
.type-switch label + label span { border-left: 1px solid var(--border-strong); }
.type-switch input:checked + span { background: var(--surface-2); color: var(--text); font-weight: 650; }
.type-switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span-2 { grid-column: span 2; }
.gst-note { font-size: 12.5px; color: var(--text-3); margin: 10px 0 0; min-height: 1em; }
.form-error { color: var(--neg); font-size: 13px; margin: 6px 0 0; min-height: 1em; }
.dlg-actions { display: flex; align-items: center; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.keep { margin-right: auto; font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 500;
  transition: opacity .2s, transform .2s; pointer-events: none; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.empty { padding: 30px; text-align: center; color: var(--text-3); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .grid-2, .cmp-grid { grid-template-columns: 1fr; }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 560px) {
  .topbar, .tabs { padding-left: 16px; padding-right: 16px; }
  main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .compare-form .field { width: 100%; }
  .chart.tall { height: 260px; }
  .kpi .value { font-size: 20px; }
}
