/* ==========================================================================
   Maybank Forecast Dashboard — design tokens from the dataviz skill's
   reference palette (references/palette.md). Light is the default via
   prefers-color-scheme; :root[data-theme] overrides win for the manual
   toggle in either direction.
   ========================================================================== */

:root {
  /* chart chrome & ink */
  --surface-1: #fcfcfb;      /* chart surface */
  --page-plane: #f9f9f7;     /* page background */
  --card-surface: #ffffff;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border-hairline: rgba(11, 11, 11, 0.10);
  --success-text: #006300;

  /* categorical (fixed order, never cycled) */
  --series-1: #2a78d6;  /* blue */
  --series-2: #1baf7a;  /* aqua */
  --series-3: #eda100;  /* yellow */
  --series-4: #008300;  /* green */
  --series-5: #4a3aa7;  /* violet */
  --series-6: #e34948;  /* red */
  --series-7: #e87ba4;  /* magenta */
  --series-8: #eb6834;  /* orange */

  /* sequential (blue), used for the ordinal scenario ramp */
  --seq-100: #cde2fb;
  --seq-250: #86b6ef;
  --seq-400: #3987e5;
  --seq-450: #2a78d6;
  --seq-600: #184f95;

  /* diverging (blue <-> red), used for driver correlation */
  --div-pos-strong: #184f95;
  --div-pos: #2a78d6;
  --div-pos-soft: #86b6ef;
  --div-mid: #f0efec;
  --div-neg-soft: #f0a3a2;
  --div-neg: #e34948;
  --div-neg-strong: #a92222;

  /* status */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface-1: #1a1a19;
    --page-plane: #0d0d0d;
    --card-surface: #201f1e;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border-hairline: rgba(255, 255, 255, 0.10);
    --success-text: #0ca30c;

    --series-1: #3987e5;
    --series-2: #199e70;
    --series-3: #c98500;
    --series-4: #008300;
    --series-5: #9085e9;
    --series-6: #e66767;
    --series-7: #d55181;
    --series-8: #d95926;

    --div-pos-strong: #3987e5;
    --div-pos: #256abf;
    --div-pos-soft: #184f95;
    --div-mid: #383835;
    --div-neg-soft: #7a2b2b;
    --div-neg: #e66767;
    --div-neg-strong: #f08a8a;

    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --surface-1: #1a1a19;
  --page-plane: #0d0d0d;
  --card-surface: #201f1e;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border-hairline: rgba(255, 255, 255, 0.10);
  --success-text: #0ca30c;
  --series-1: #3987e5;
  --series-2: #199e70;
  --series-3: #c98500;
  --series-4: #008300;
  --series-5: #9085e9;
  --series-6: #e66767;
  --series-7: #d55181;
  --series-8: #d95926;
  --div-pos-strong: #3987e5;
  --div-pos: #256abf;
  --div-pos-soft: #184f95;
  --div-mid: #383835;
  --div-neg-soft: #7a2b2b;
  --div-neg: #e66767;
  --div-neg-strong: #f08a8a;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --surface-1: #fcfcfb;
  --page-plane: #f9f9f7;
  --card-surface: #ffffff;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border-hairline: rgba(11, 11, 11, 0.10);
  --success-text: #006300;
  --series-1: #2a78d6;
  --series-2: #1baf7a;
  --series-3: #eda100;
  --series-4: #008300;
  --series-5: #4a3aa7;
  --series-6: #e34948;
  --series-7: #e87ba4;
  --series-8: #eb6834;
  --div-pos-strong: #184f95;
  --div-pos: #2a78d6;
  --div-pos-soft: #86b6ef;
  --div-mid: #f0efec;
  --div-neg-soft: #f0a3a2;
  --div-neg: #e34948;
  --div-neg-strong: #a92222;
  color-scheme: light;
}

/* ========================================================================
   Base
   ======================================================================== */

* { 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;
  overflow-x: hidden;
}

.viz-root {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2, h3 { font-family: inherit; font-weight: 600; margin: 0; }
p { margin: 0; }

/* ========================================================================
   Header
   ======================================================================== */

.page-header {
  border-bottom: 1px solid var(--border-hairline);
  background: var(--card-surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 28px; line-height: 1; }
.brand-text h1 { font-size: 19px; color: var(--text-primary); }
.brand-sub { font-size: 12.5px; color: var(--text-secondary); margin-top: 2px; }

.header-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.ticker-badge {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border-hairline);
  border-radius: 999px;
  padding: 4px 10px;
}

.as-of { font-size: 12.5px; color: var(--text-muted); }

.theme-toggle {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border-hairline);
  background: var(--surface-1);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
}
.theme-toggle:hover { background: var(--gridline); }
.theme-icon-dark { display: none; }
@media (prefers-color-scheme: dark) {
  .theme-icon-light { display: none; }
  .theme-icon-dark { display: inline; }
}
:root[data-theme="dark"] .theme-icon-light { display: none; }
:root[data-theme="dark"] .theme-icon-dark { display: inline; }
:root[data-theme="light"] .theme-icon-light { display: inline; }
:root[data-theme="light"] .theme-icon-dark { display: none; }

/* ========================================================================
   Layout
   ======================================================================== */

.dashboard-main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 1;
}

.card {
  background: var(--card-surface);
  border: 1px solid var(--border-hairline);
  border-radius: 12px;
  padding: 20px 22px;
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.card-header h2 { font-size: 16px; color: var(--text-primary); }
.card-sub { font-size: 12.5px; color: var(--text-secondary); margin-top: 4px; max-width: 60ch; }

.card-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.panel-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 20px;
}
@media (max-width: 860px) {
  .panel-grid { grid-template-columns: 1fr; }
}

/* ========================================================================
   KPI row / stat tiles
   ======================================================================== */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
}
@media (max-width: 1100px) { .kpi-row { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 620px) { .kpi-row { grid-template-columns: repeat(2, 1fr); } }

.stat-tile {
  background: var(--card-surface);
  border: 1px solid var(--border-hairline);
  border-radius: 12px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.stat-value {
  font-size: 26px;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: proportional-nums;
  line-height: 1.15;
}

.stat-sub { font-size: 11.5px; color: var(--text-muted); }

.stat-delta {
  font-size: 13px;
  font-weight: 600;
}
.stat-delta.is-up { color: var(--success-text); }
.stat-delta.is-down { color: var(--div-neg-strong); }

/* ========================================================================
   Charts
   ======================================================================== */

.chart-canvas-wrap { position: relative; width: 100%; }
.chart-canvas-wrap--tall { height: 380px; }
.chart-canvas-wrap--medium { height: 300px; }
.chart-canvas-wrap--short { height: 240px; }

.range-group {
  display: inline-flex;
  border: 1px solid var(--border-hairline);
  border-radius: 8px;
  overflow: hidden;
}
.range-btn {
  border: none;
  background: var(--card-surface);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  cursor: pointer;
  border-right: 1px solid var(--border-hairline);
}
.range-btn:last-child { border-right: none; }
.range-btn:hover { background: var(--surface-1); }
.range-btn.active { background: var(--series-1); color: #ffffff; }

.table-toggle-btn {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border-hairline);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
.table-toggle-btn:hover { background: var(--gridline); }
.table-toggle-btn.active { background: var(--series-1); color: #fff; border-color: var(--series-1); }

.legend-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--text-secondary);
}
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-swatch { width: 14px; height: 2px; border-radius: 1px; display: inline-block; }
.legend-swatch.is-area { width: 14px; height: 10px; border-radius: 2px; opacity: .35; }
.legend-swatch.is-dashed { background: none; border-top: 2px dashed currentColor; height: 0; }

/* ========================================================================
   Table view (accessibility twin of every chart)
   ======================================================================== */

.table-view {
  margin-top: 14px;
  overflow-x: auto;
  border-top: 1px solid var(--gridline);
  padding-top: 12px;
}
.table-view table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}
.table-view th, .table-view td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
}
.table-view th {
  color: var(--text-secondary);
  font-weight: 600;
  font-variant-numeric: normal;
}
.table-view td:first-child, .table-view th:first-child { font-variant-numeric: normal; }

/* ========================================================================
   Risk callouts
   ======================================================================== */

.risk-callouts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 16px;
}
@media (max-width: 500px) { .risk-callouts { grid-template-columns: 1fr; } }
.risk-callout {
  background: var(--surface-1);
  border: 1px solid var(--border-hairline);
  border-radius: 10px;
  padding: 10px 12px;
}
.risk-callout .rc-label { font-size: 11.5px; color: var(--text-secondary); }
.risk-callout .rc-value { font-size: 17px; font-weight: 600; color: var(--text-primary); margin-top: 2px; }

/* ========================================================================
   Recommendations
   ======================================================================== */

.reco-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.reco-list li {
  position: relative;
  padding-left: 22px;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text-primary);
}
.reco-list li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 7px;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--series-1);
}
.reco-list strong { color: var(--text-primary); }

/* ========================================================================
   Methodology
   ======================================================================== */

.method-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-top: 14px;
}
@media (max-width: 860px) { .method-grid { grid-template-columns: 1fr; } }
.method-grid h3 { font-size: 13.5px; color: var(--text-primary); margin-bottom: 6px; }
.method-grid p { font-size: 13px; color: var(--text-secondary); line-height: 1.55; }

/* ========================================================================
   Footer
   ======================================================================== */

.page-footer {
  border-top: 1px solid var(--border-hairline);
  padding: 20px 24px 28px;
  max-width: 1280px;
  margin: 0 auto;
  width: 100%;
}
.page-footer p { font-size: 11.5px; color: var(--text-muted); line-height: 1.6; max-width: 100ch; }

/* ========================================================================
   Chart.js tooltip override (matches card surface, not Chart.js default)
   ======================================================================== */

.chartjs-tooltip-key {
  display: inline-block;
  width: 10px;
  height: 2px;
  margin-right: 6px;
  vertical-align: middle;
}
