/* ==========================================================================
   Design tokens — validated palette (see dataviz skill / palette.md)
   ========================================================================== */
:root {
  color-scheme: light;
  --surface-1:      #fcfcfb;
  --page-plane:      #f9f9f7;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --gridline:       #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11,11,11,0.10);

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

  --series-1: #2a78d6; /* Intraday */
  --series-2: #eb6834; /* Short-term */
  --series-3: #1baf7a; /* Other */

  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px rgba(11,11,11,0.06), 0 4px 16px rgba(11,11,11,0.06);
}

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

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

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

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
}

body { overflow-x: hidden; }

button, input, select {
  font-family: inherit;
  font-size: 15px;
}

a { color: var(--series-1); }

/* ---- layout shell ---- */
.app-shell { display: none; height: 100vh; flex-direction: column; }
.app-shell.visible { display: flex; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 600; }
.topbar .user { color: var(--text-secondary); font-size: 13px; }
.topbar-logo { height: 32px; width: auto; display: block; }

/* ---- sidebar navigation ---- */
.body-row {
  display: flex;
  flex: 1;
  min-height: 0; /* let sidebar/main scroll independently instead of growing the page */
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.side-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 8px;
}
.side-link {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 9px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
  text-align: left;
}
.side-link:hover { background: var(--page-plane); }
.side-link.active { background: var(--series-1); color: #fff; }
.side-link.sub { padding-left: 26px; font-size: 13.5px; }
.side-group-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 14px 12px 2px;
  font-weight: 600;
}
.side-group-label.active-group { color: var(--series-1); }

.sidebar-backdrop { display: none; }

#sidebarToggleBtn { display: none; }

@media (max-width: 860px) {
  #sidebarToggleBtn { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 16px rgba(0,0,0,0.2);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.visible {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 40;
  }
}

main { flex: 1; padding: 20px; box-sizing: border-box; overflow-y: auto; min-width: 0; }

.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.dash-tile { grid-column: span 1; }
.dash-tile.wide { grid-column: span 2; }
@media (max-width: 640px) {
  .dash-tile.wide { grid-column: span 1; }
}
.dash-charts {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.dash-charts-row {
  display: flex;
  gap: 16px;
}
.dash-charts-row > .chart-card { flex: 1 1 0; min-width: 0; }
@media (max-width: 900px) {
  .dash-charts-row { flex-direction: column; }
}
.progress-track {
  background: var(--gridline);
  border-radius: 999px;
  height: 10px;
  overflow: hidden;
  margin: 8px 0 4px;
}
.progress-fill { height: 100%; border-radius: 999px; }

/* Every tab stays at a comfortable reading width and centered, except
   Dashboard — which intentionally goes edge-to-edge (minus padding) so
   its charts get real room instead of being squeezed into a 1400px column. */
.view { display: none; max-width: 1400px; margin: 0 auto; }
.view.active { display: block; }
#view-dashboard.view { max-width: none; }

/* ---- login ---- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
  max-width: 360px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
.login-card p.subtitle { color: var(--text-secondary); margin: 0 0 24px; font-size: 14px; text-align: center; }
.login-logo-wrap { display: flex; justify-content: center; margin-bottom: 12px; }
.login-logo { height: 110px; width: auto; display: block; }

/* ---- form controls ---- */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.field input, .field select {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.field input:focus, .field select:focus {
  outline: 2px solid var(--series-1);
  outline-offset: -1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-weight: 600;
  cursor: pointer;
  background: var(--series-1);
  color: #fff;
}
.btn:disabled { opacity: 0.6; cursor: default; }
.btn.block { width: 100%; }
.btn.secondary { background: var(--page-plane); color: var(--text-primary); border: 1px solid var(--border); }
.btn.danger { background: var(--status-critical); color: #fff; }
.btn.small { padding: 6px 12px; font-size: 13px; }

.error-text { color: var(--status-critical); font-size: 13px; margin-top: 10px; }
.hint-text { color: var(--text-muted); font-size: 12px; margin-top: 6px; }

/* ---- stat tiles ---- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 20px;
}
.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
}
.stat-tile .label { font-size: 12px; color: var(--text-secondary); margin-bottom: 6px; }
.stat-tile .value { font-size: 22px; font-weight: 600; }

.hero-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex;
  align-items: center;
  gap: 28px;
  flex-wrap: wrap;
}
.hero-figure .label { font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; }
.hero-figure .value { font-size: 48px; font-weight: 700; line-height: 1; }

/* ---- recovery ring ---- */
.ring-wrap { position: relative; width: 148px; height: 148px; flex-shrink: 0; }
.ring-wrap svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.ring-center .pct { font-size: 24px; font-weight: 700; }
.ring-center .lbl { font-size: 11px; color: var(--text-secondary); }

/* ---- table ---- */
.table-wrap { overflow-x: auto; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius-lg); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left;
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
tbody td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--page-plane); }
.amount-pos { color: var(--status-good); font-weight: 600; }
.amount-neg { color: var(--status-critical); font-weight: 600; }
.row-actions { display: flex; gap: 6px; }
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: var(--page-plane);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.filter-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: flex-end; }
.filter-row .field { margin-bottom: 0; min-width: 140px; }

/* ---- calendar grid ---- */
.cal-weekdays, .cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.cal-weekdays { margin-bottom: 6px; }
.cal-weekday {
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
}
.cal-cell {
  aspect-ratio: 1 / 0.85;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 6px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  cursor: default;
  background: var(--surface-1);
}
.cal-cell.has-data { cursor: pointer; }
.cal-cell.has-data:hover { filter: brightness(0.97); }
.cal-cell.empty { visibility: hidden; }
.cal-daynum { font-size: 12px; color: var(--text-secondary); font-weight: 600; }
.cal-amount { font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; }
.cal-today .cal-daynum { color: var(--series-1); }

.section-header { display: flex; align-items: center; justify-content: space-between; margin: 24px 0 12px; }
.section-header h2 { font-size: 16px; margin: 0; }
.section-header:first-child { margin-top: 0; }

/* ---- yearly bar chart ---- */
.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 20px;
  transition: box-shadow 0.2s ease, transform 0.2s ease;
}
.chart-card:hover { box-shadow: 0 2px 4px rgba(11,11,11,0.08), 0 8px 24px rgba(11,11,11,0.10); transform: translateY(-1px); }
.chart-card h3 { font-size: 14px; }
/* Safety cap — a chart with very few data points shouldn't ever blow up to
   fill the card's full width-driven height; it scales down within this box
   (preserving aspect ratio) instead of stretching tall and sparse. */
.chart-card svg { max-height: 240px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; font-size: 12px; color: var(--text-secondary); }
.legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* Bars grow up/down from the zero line on first paint — staggered per bar via inline animation-delay. */
@keyframes barGrowIn {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}
/* The withdraw stacked bar's two segments grow inward horizontally instead. */
@keyframes barGrowInX {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}
.chart-bar, .year-bar {
  animation: barGrowIn 0.7s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.withdraw-seg {
  animation: barGrowInX 0.75s cubic-bezier(0.16, 1, 0.3, 1) both;
}
@media (prefers-reduced-motion: reduce) {
  .chart-bar, .year-bar, .withdraw-seg { animation: none; }
}

/* Growth chart lines draw themselves in left-to-right via a dash-offset
   reveal; the fill area underneath just fades in. */
@keyframes lineDrawIn {
  to { stroke-dashoffset: 0; }
}
@keyframes areaFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.growth-line { animation: lineDrawIn 1.1s cubic-bezier(0.16, 1, 0.3, 1) both; }
.growth-area { animation: areaFadeIn 0.9s ease-out 0.3s both; }
@media (prefers-reduced-motion: reduce) {
  .growth-line, .growth-area { animation: none; }
}

.bar-tooltip {
  position: absolute;
  background: var(--text-primary);
  color: var(--surface-1);
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  white-space: nowrap;
  z-index: 20;
}

/* ---- modal ---- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  display: none;
  align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-backdrop.visible { display: flex; }
.modal {
  background: var(--surface-1);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-secondary); }

.loading-spinner {
  width: 28px; height: 28px;
  border: 3px solid var(--gridline);
  border-top-color: var(--series-1);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  bottom: 20px; left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
.toast.visible { opacity: 1; transform: translateX(-50%) translateY(-4px); }

@media (max-width: 560px) {
  .hero-card { flex-direction: column; text-align: center; }
  main { padding: 14px; }
}
