/* =========================================================
   Colors live here as variables, so light & dark mode are
   handled in one place. The charts read these too.
   ========================================================= */
:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #75746f;
  --accent: #2a78d6;
  --accent-text: #ffffff;
  --grid: #e9e8e3;

  /* chart colors (one fixed color per category) */
  --c-food: #2a78d6;
  --c-transport: #eb6834;
  --c-groceries: #1baf7a;
  --c-bills: #eda100;
  --c-shopping: #e87ba4;
  --c-health: #008300;
  --c-entertainment: #6250d6;
  --c-tobacco: #e34948;
  --c-personal: #7a8597;
  --c-other: #b3b2ab;
  --c-bar: #2a78d6;

  /* status colors (budgets, up/down) — always shown with an icon/label */
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --good-text: #006300;
  --warning-text: #8a5a00;
  --critical-text: #b42828;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #9a998f;
    --accent: #3987e5;
    --grid: #2a2a28;

    --c-food: #3987e5;
    --c-transport: #d95926;
    --c-groceries: #199e70;
    --c-bills: #c98500;
    --c-shopping: #d55181;
    --c-health: #008300;
    --c-entertainment: #9085e9;
    --c-tobacco: #e66767;
    --c-personal: #8b95a6;
    --c-other: #6b6a64;
    --c-bar: #3987e5;

    --good-text: #4cc94c;
    --warning-text: #fab219;
    --critical-text: #f07373;
    --shadow: none;
  }
}

/* ===================== Base ===================== */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 1rem; font-weight: 650; }
h3 { font-size: 0.8rem; font-weight: 600; color: var(--text-2); }
.muted { color: var(--text-2); }
.small { font-size: 0.82rem; }
[hidden] { display: none !important; }

.page { max-width: 1200px; margin: 0 auto; padding: 20px 16px 40px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
  min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }

/* ===================== Header ===================== */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.topbar p { margin: 2px 0 0; }
.topbar-actions { display: flex; gap: 8px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 550; cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); background: var(--accent); }
.btn.icon-only { padding: 0 10px; }
.btn.loading svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.link-btn { background: none; border: 0; padding: 0; margin-top: 10px; color: var(--text-2); text-decoration: underline; font: inherit; font-size: 0.85rem; cursor: pointer; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===================== Notices ===================== */
.notice {
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px;
  border: 1px solid var(--border); background: var(--surface);
  border-left: 4px solid var(--text-3);
}
.notice h2 { font-size: 1rem; margin-bottom: 4px; }
.notice p, .notice ul, .notice ol { margin: 4px 0; }
.notice ul, .notice ol { padding-left: 20px; }
.notice.error { border-left-color: var(--critical); }
.notice.warn { border-left-color: var(--warning); }
.notice.info { border-left-color: var(--accent); }
.notice .actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.notice code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 0.85em; word-break: break-all; }
.notice details { margin-top: 6px; }
.notice summary { cursor: pointer; color: var(--text-2); }

/* ===================== Key form ===================== */
.key-card p { margin: 6px 0 12px; }
.key-form { display: flex; gap: 8px; flex-wrap: wrap; }
.key-form input { flex: 1 1 220px; }

/* ===================== Inputs ===================== */
input, select {
  min-height: 40px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); font: inherit;
}
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.8rem; color: var(--text-2); font-weight: 550; }
.field select, .field input { min-width: 150px; }
.filter-summary { margin: 0 0 8px auto; color: var(--text-2); font-size: 0.85rem; }

/* ===================== KPIs ===================== */
.kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 6px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; min-width: 0; }
.kpi-value { margin: 6px 0 2px; font-size: 1.35rem; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value .unit { font-size: 0.75rem; font-weight: 600; color: var(--text-2); margin-left: 3px; }
.kpi-sub { margin: 0; font-size: 0.78rem; color: var(--text-2); }
.kpi-note { margin: 0 0 16px; }
.loading-state .kpi-value, .loading-state .kpi-sub { color: transparent; background: var(--surface-2); border-radius: 6px; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

/* ===================== Insights ===================== */
.insights-body { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 20px; }
.insight-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.insight-list li { display: flex; gap: 10px; align-items: flex-start; }
.insight-icon { flex: 0 0 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; background: var(--surface-2); font-size: 0.85rem; }
.insight-list p { margin: 2px 0 0; }
.up { color: var(--critical-text); font-weight: 600; }
.down { color: var(--good-text); font-weight: 600; }
.tobacco-box h3 { margin-bottom: 6px; }

/* ===================== Charts ===================== */
.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.charts .card { margin-bottom: 0; }
.charts { margin-bottom: 16px; }
.chart-wrap { position: relative; height: 260px; }
.small-chart { height: 170px; }
.category-layout { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 16px; align-items: center; }
.donut-wrap { height: 220px; }
.donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; text-align: center; }
.donut-center strong { font-size: 1rem; }
.legend-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 0.85rem; }
.legend-list li { display: grid; grid-template-columns: 12px minmax(0, 1fr) auto 44px; gap: 8px; align-items: center; }
.legend-list .pct { text-align: right; color: var(--text-2); }
.legend-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.empty-chart { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-2); font-size: 0.9rem; text-align: center; padding: 10px; }

/* ===================== Budgets ===================== */
.budget-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 24px; }
.budget-row .top { display: flex; justify-content: space-between; gap: 8px; font-size: 0.88rem; }
.budget-row .top b { font-weight: 600; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 6px 0 4px; }
.bar > span { display: block; height: 100%; border-radius: 99px; background: var(--good); }
.bar.warn > span { background: var(--warning); }
.bar.over > span { background: var(--critical); }
.budget-row .status { font-size: 0.78rem; color: var(--text-2); }
.budget-row .status.warn { color: var(--warning-text); font-weight: 600; }
.budget-row .status.over { color: var(--critical-text); font-weight: 600; }

/* ===================== Table ===================== */
.table-head input { flex: 1 1 220px; max-width: 320px; }
.table-scroll { overflow-x: auto; }
.expense-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.expense-table th { text-align: left; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-2); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--border); }
.expense-table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.expense-table tr:last-child td { border-bottom: 0; }
.expense-table .num { text-align: right; white-space: nowrap; font-weight: 600; }
.expense-table .sub { display: block; color: var(--text-3); font-size: 0.78rem; }
.expense-table .date { white-space: nowrap; }
.cat-pill { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.expense-table .mobile-cat { display: none; margin-top: 2px; font-size: 0.78rem; color: var(--text-2); }
.flag { color: var(--warning-text); font-size: 0.75rem; display: block; font-weight: 500; }
.more-btn { margin: 12px auto 0; display: flex; }
.footer { text-align: center; margin-top: 24px; }

/* ===================== Phone & tablet ===================== */
@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .charts { grid-template-columns: minmax(0, 1fr); }
  .insights-body { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .page { padding: 14px 12px 32px; }
  h1 { font-size: 1.3rem; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi-value { font-size: 1.15rem; }
  .topbar-actions .btn span { display: none; }
  .topbar-actions .btn { padding: 0 11px; }
  .filters .field { flex: 1 1 140px; }
  .field select, .field input { min-width: 0; width: 100%; }
  .filter-summary { margin: 0; flex-basis: 100%; }
  .category-layout { grid-template-columns: minmax(0, 1fr); }
  .donut-wrap { height: 200px; }
  .budget-list { grid-template-columns: minmax(0, 1fr); }
  .table-head input { max-width: none; }
  .expense-table th:nth-child(3), .expense-table td:nth-child(3) { display: none; }
  .expense-table .mobile-cat { display: inline-flex; }
}
