/* ------------------------------------------------------------------
   Palette — dataviz reference instance (categorical slots 1 & 2,
   status palette, chrome & ink). Light and dark are both selected.
   ------------------------------------------------------------------ */
:root {
  color-scheme: light;
  --page:            #f9f9f7;
  --surface-1:       #fcfcfb;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --gridline:        #e1e0d9;
  --baseline:        #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);

  --series-income:   #2a78d6;  /* categorical slot 1 */
  --series-expense:  #eb6834;  /* categorical slot 2 */

  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-critical: #d03b3b;
  --success-text:    #006300;

  --radius: 14px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page:           #0d0d0d;
    --surface-1:      #1a1a19;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --gridline:       #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --series-income:  #3987e5;
    --series-expense: #d95926;
    --success-text:   #0ca30c;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page:           #0d0d0d;
  --surface-1:      #1a1a19;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --gridline:       #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --series-income:  #3987e5;
  --series-expense: #d95926;
  --success-text:   #0ca30c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }
.muted { color: var(--text-secondary); }

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

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(360px, 100%);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  display: grid;
  gap: 12px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; font-size: 14px; }
.error { color: var(--status-critical); font-size: 14px; min-height: 1em; }

/* ---------------- Chrome ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--page);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 17px; font-weight: 600; }

main {
  padding: 16px 16px calc(150px + env(safe-area-inset-bottom));
  max-width: 900px;
  margin: 0 auto;
}

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 6;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 11px;
  padding: 8px 4px;
  min-height: var(--tap);
  display: grid;
  gap: 2px;
  justify-items: center;
  cursor: pointer;
}
.tab.active { color: var(--series-income); font-weight: 600; }
.tab-icon { font-size: 18px; line-height: 1; }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(74px + env(safe-area-inset-bottom));
  z-index: 7;
  width: 56px; height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--series-income);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

/* ---------------- Cards & rows ---------------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 12px; font-size: 15px; font-weight: 600; }

.hero { padding: 8px 4px 18px; }
.hero-label { font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.hero-value { font-size: clamp(32px, 9vw, 44px); font-weight: 650; letter-spacing: -0.02em; }

.tiles {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.tile-label { font-size: 11px; color: var(--text-secondary); margin-bottom: 6px; }
.tile-value { font-size: 17px; font-weight: 600; }
.income { color: var(--series-income); }
.expense { color: var(--series-expense); }
.positive { color: var(--success-text); }
.negative { color: var(--status-critical); }

.rows { display: grid; }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--gridline);
  min-height: var(--tap);
}
.row:last-child { border-bottom: 0; }
.row-main { min-width: 0; }
.row-title { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.row-value { font-size: 14px; font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.empty { color: var(--text-secondary); font-size: 14px; padding: 8px 0; }

/* ---------------- Goal progress ---------------- */

.goal { padding: 12px 0; border-bottom: 1px solid var(--gridline); }
.goal:last-child { border-bottom: 0; }
.goal-head { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.goal-name { font-size: 14px; }
.goal-value { font-size: 13px; color: var(--text-secondary); white-space: nowrap; font-variant-numeric: tabular-nums; }
.goal-track {
  height: 8px;
  background: var(--gridline);
  border-radius: 4px;
  margin-top: 8px;
  overflow: hidden;
}
.goal-fill { height: 100%; border-radius: 4px; }
.goal-fill.ok { background: var(--status-good); }
.goal-fill.warn { background: var(--status-warning); }
.goal-fill.danger { background: var(--status-critical); }
.goal-status { font-size: 12px; margin-top: 6px; display: flex; align-items: center; gap: 5px; }
.goal-status.ok { color: var(--success-text); }
.goal-status.warn { color: var(--text-secondary); }
.goal-status.danger { color: var(--status-critical); }

/* ---------------- Charts ---------------- */

/* <figure> carries a 40px default side margin in every browser — reset it. */
figure { margin: 0; }

.chart-card { padding-bottom: 12px; position: relative; }
.chart-card figcaption { margin-bottom: 12px; }
.chart-card figcaption h2 { margin-bottom: 8px; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--text-secondary); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.swatch-income { background: var(--series-income); }
.swatch-expense { background: var(--series-expense); }

.chart-scroll { overflow-x: auto; }
/* The SVG is sized in real pixels by the renderer — never rescale it here. */
.chart svg { display: block; }

.tooltip {
  position: absolute;
  z-index: 3;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  white-space: nowrap;
}
.tooltip-title { font-weight: 600; margin-bottom: 4px; }
.tooltip-row { display: flex; align-items: center; gap: 6px; }
.tooltip-row .swatch { flex: none; }
.tooltip-value { font-variant-numeric: tabular-nums; margin-left: auto; padding-left: 10px; }

.table-wrap { overflow-x: auto; margin-top: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 7px 10px 7px 0; border-bottom: 1px solid var(--gridline); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

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

.filter-row { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }

select, input, button { font: inherit; }

select, input {
  background: var(--surface-1);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: var(--tap);
  width: 100%;
}
.filter-row select { width: auto; flex: 1 1 140px; }

label { display: grid; gap: 5px; font-size: 13px; color: var(--text-secondary); margin-bottom: 12px; }

.btn-primary {
  background: var(--series-income);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 13px 16px;
  min-height: var(--tap);
  width: 100%;
  font-weight: 600;
  cursor: pointer;
}
.btn-ghost {
  background: none;
  border: 1px dashed var(--baseline);
  color: var(--text-secondary);
  border-radius: 10px;
  padding: 11px 14px;
  min-height: var(--tap);
  width: 100%;
  margin-top: 12px;
  cursor: pointer;
}
.btn-link {
  background: none;
  border: 0;
  color: var(--series-income);
  padding: 10px 0 0;
  min-height: 32px;
  font-size: 13px;
  cursor: pointer;
}
.icon-btn {
  background: none;
  border: 0;
  color: var(--text-secondary);
  font-size: 20px;
  min-width: var(--tap);
  min-height: var(--tap);
  cursor: pointer;
}
.btn-delete {
  background: none;
  border: 0;
  color: var(--text-muted);
  font-size: 18px;
  min-width: 40px;
  min-height: 40px;
  cursor: pointer;
}
.seg { display: flex; gap: 8px; margin-bottom: 14px; }
.seg button {
  flex: 1;
  min-height: var(--tap);
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: 10px;
  cursor: pointer;
}
.seg button.active {
  border-color: var(--series-income);
  color: var(--series-income);
  font-weight: 600;
}

/* ---------------- Modal & toast ---------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal {
  background: var(--surface-1);
  width: min(460px, 100%);
  max-height: 88dvh;
  overflow-y: auto;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 18px 18px calc(22px + env(safe-area-inset-bottom));
}
@media (min-width: 560px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: var(--radius); }
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: 17px; }

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(140px + env(safe-area-inset-bottom));
  z-index: 30;
  background: var(--text-primary);
  color: var(--page);
  padding: 11px 16px;
  border-radius: 10px;
  font-size: 14px;
  max-width: calc(100% - 32px);
}

@media (min-width: 720px) {
  /* The nav sits after <main> in the DOM (thumb-reachable on phones);
     on wide screens flex order lifts it under the top bar instead. */
  #app { display: flex; flex-direction: column; min-height: 100dvh; }
  .topbar { order: 0; position: static; }
  .tabbar { order: 1; }
  main { order: 2; width: 100%; }

  .tabbar {
    position: sticky;
    top: 0;
    bottom: auto;
    left: auto;
    right: auto;
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    border-top: 0;
    border-bottom: 1px solid var(--border);
    background: var(--page);
    padding-bottom: 0;
  }
  .tab { grid-auto-flow: column; font-size: 13px; }
  main { padding-bottom: 40px; }
  .fab { bottom: 28px; }
}
