/* ==========================================================================
   Northwind Analytics Dashboard — theme tokens (light default, dark via prefers-color-scheme
   or an explicit [data-theme] stamp from a theme toggle)
   ========================================================================== */

: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);
  --success-text:   #006300;
  --trend-total-line: #0b0b0b;

  /* categorical — fixed order used everywhere except the Value/Person tab:
     Retail, Wholesale, Warranty, Service, Online — validated at this
     position via scripts/validate_palette.js
     "#eb6834,#2a78d6,#d03b3b,#bfa000,#008300" --mode light */
  --line-retail:      #eb6834;
  --line-online:       #008300;
  --line-wholesale:        #2a78d6;
  --line-service:  #bfa000;

  /* Value/Person tab only — Service + Warranty combined client-side
     (app.js buildMergedPersonRow) into a single "Extended" row, reusing
     Service's yellow since Service is the larger of the two. */
  --line-extended:        #bfa000;

  /* categorical — Trends tab "Overlay metrics" chart only (Revenue/Good
     Parts/Rework/Rework % as one comparison, not the 5 production lines
     above). aqua/violet/magenta are the palette's 3 otherwise-unused hues;
     the 4th (gold, for Rework %) is a custom hue picked specifically to
     clear the adjacent-pair CVD check against magenta in both themes —
     re-run scripts/validate_palette.js from the dataviz skill before
     reordering or swapping any of the four. */
  --metric-revenue:      #1baf7a;
  --metric-good-parts:   #4a3aa7;
  --metric-rework:       #e87ba4;
  --metric-rework-rate:  #a67c00;

  /* sequential blue ramp (ordinal use — weekly breakdown) */
  --seq-250: #86b6ef;
  --seq-350: #5598e7;
  --seq-450: #2a78d6;
  --seq-550: #1c5cab;

  /* status (fixed, never themed) */
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;

  /* Warranty (fixed, never themed) — part of the categorical palette
     above; also used standalone on Top Data's Customer/Finish views. */
  --line-warranty: #d03b3b;
}

@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);
    --success-text:   #0ca30c;
    --trend-total-line: #ffffff;

    --line-retail:      #d95926;
    --line-online:       #008300;
    --line-wholesale:        #3987e5;
    --line-service:  #b08e00;
    --line-extended:        #b08e00;

    --metric-revenue:      #199e70;
    --metric-good-parts:   #9085e9;
    --metric-rework:       #d55181;
    --metric-rework-rate:  #a88320;

    --seq-250: #184f95;
    --seq-350: #104281;
    --seq-450: #3987e5;
    --seq-550: #86b6ef;
  }
}
: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);
  --success-text:   #0ca30c;
  --trend-total-line: #ffffff;

  --line-retail:      #d95926;
  --line-online:       #008300;
  --line-wholesale:        #3987e5;
  --line-service:  #b08e00;
  --line-extended:        #b08e00;

  --metric-revenue:      #199e70;
  --metric-good-parts:   #9085e9;
  --metric-rework:       #d55181;
  --metric-rework-rate:  #a88320;

  --seq-250: #184f95;
  --seq-350: #104281;
  --seq-450: #3987e5;
  --seq-550: #86b6ef;
}

* { 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;
}

body {
  min-height: 100vh;
}

.wrap {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

/* ---------- theme toggle ---------- */

.theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 10;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-toggle:hover {
  border-color: var(--baseline);
}

/* ---------- header ---------- */

.dash-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
}

.dash-header h1 {
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}

.dash-header .updated {
  font-size: 13px;
  color: var(--text-muted);
}

.updated-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.sign-out {
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: underline;
}

.dash-footer {
  margin-top: 32px;
  padding: 16px 0 8px;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
}

/* ---------- view tabs ---------- */

.view-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 24px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.view-tab {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.view-tab:hover {
  background: var(--gridline);
}

.view-tab[aria-selected="true"] {
  background: var(--line-wholesale);
  color: #fff;
}

.view-tab:focus-visible {
  outline: 2px solid var(--line-wholesale);
  outline-offset: 2px;
}

/* ---------- range filter ---------- */

.range-filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.range-filter,
.stat-toggle {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
}

.range-filter:hover,
.stat-toggle:hover {
  background: var(--gridline);
}

.range-filter[aria-pressed="true"],
.stat-toggle[aria-pressed="true"] {
  background: var(--line-wholesale);
  border-color: var(--line-wholesale);
  color: #fff;
}

.range-filter:focus-visible,
.stat-toggle:focus-visible {
  outline: 2px solid var(--line-wholesale);
  outline-offset: 2px;
}

/* ---------- date select ---------- */

.date-select-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.date-select {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 5px 28px 5px 12px;
  border-radius: 999px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23888' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.date-select:hover {
  background-color: var(--gridline);
}

.date-select:focus-visible {
  outline: 2px solid var(--line-wholesale);
  outline-offset: 2px;
}

.select-pair {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

/* ---------- view sections ---------- */

.view {
  display: none;
}
.view.active {
  display: block;
}

.top-data-subview {
  display: none;
}
.top-data-subview.active {
  display: block;
}

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

.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 4px;
}

.card .subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 16px;
}

/* ---------- KPI stat tiles ---------- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}

.stat-tile .label {
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}

.stat-tile .swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: none;
}

.stat-tile .value {
  font-size: 24px;
  font-weight: 600;
  line-height: 1.1;
}

.stat-tile .delta {
  font-size: 12px;
  margin-top: 4px;
  font-weight: 500;
}
.stat-tile .delta.up { color: var(--success-text); }
.stat-tile .delta.down { color: var(--status-critical); }
.stat-tile .delta.flat { color: var(--text-muted); }

/* ---------- legend ---------- */

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--text-secondary);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-item .key {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex: none;
}

.legend-item .key.line-key {
  height: 2px;
  border-radius: 1px;
}

.legend-item.interactive {
  cursor: pointer;
  user-select: none;
  border-radius: 4px;
}

.legend-item.interactive:hover {
  color: var(--text-primary);
}

.legend-item.interactive.inactive {
  opacity: 0.4;
}

/* ---------- charts ---------- */

.chart-svg {
  width: 100%;
  height: auto;
  display: block;
  overflow: visible;
}

.chart-svg text {
  fill: var(--text-secondary);
  font-size: 11px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

.chart-svg .value-label {
  fill: var(--text-primary);
  font-size: 11px;
  font-weight: 600;
}

.chart-svg .axis-line {
  stroke: var(--baseline);
  stroke-width: 1;
}

.chart-svg .gridline {
  stroke: var(--gridline);
  stroke-width: 1;
}

.chart-svg .reference-line {
  stroke: var(--text-secondary);
  stroke-width: 1.5;
  stroke-dasharray: 4 4;
  fill: none;
}

.chart-svg .reference-line-median {
  stroke: var(--text-secondary);
  stroke-width: 1.5;
  stroke-dasharray: 1.5 3;
  fill: none;
}

.chart-svg .line-mark {
  fill: none;
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.status-key {
  font-size: 12px;
  color: var(--text-secondary);
  margin: 0 0 12px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.status-key .good { color: var(--status-good); font-weight: 600; }
.status-key .warning { color: var(--status-warning); font-weight: 600; }
.status-key .critical { color: var(--status-critical); font-weight: 600; }

.summary-card {
  padding: 48px 40px;
}

.tone-good { color: var(--success-text); font-weight: 600; }
.tone-bad { color: var(--status-critical); font-weight: 600; }

.summary-card .summary-headline {
  font-size: 32px;
  font-weight: 600;
  margin: 0 0 28px;
  line-height: 1.35;
}

.summary-bullets {
  margin: 0;
  padding-left: 24px;
  color: var(--text-secondary);
  font-size: 18px;
  line-height: 1.8;
}

.summary-bullets li {
  margin-bottom: 12px;
}

.bar-mark {
  cursor: pointer;
}
.bar-mark:hover, .bar-mark:focus {
  opacity: 0.85;
  outline: none;
}

/* ---------- tooltip ---------- */

#tooltip {
  position: fixed;
  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 16px rgba(0,0,0,0.15);
  z-index: 100;
  display: none;
  max-width: 240px;
}

#tooltip.visible { display: block; }

#tooltip .tt-title {
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-primary);
}

#tooltip .tt-row {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: space-between;
  gap: 10px;
}

#tooltip .tt-key {
  display: inline-block;
  width: 10px;
  height: 2px;
  border-radius: 1px;
  flex: none;
}

#tooltip .tt-name {
  color: var(--text-secondary);
  flex: 1;
}

#tooltip .tt-value {
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* ---------- meter ---------- */

.meter {
  position: relative;
  height: 20px;
  border-radius: 10px;
  background: var(--gridline);
  overflow: visible;
  margin: 10px 0 8px;
}

.meter .fill {
  position: absolute;
  inset: 0;
  width: 0%;
  background: var(--status-good);
  border-radius: 10px;
}

.meter .marker {
  position: absolute;
  top: -4px;
  bottom: -4px;
  width: 2px;
  background: var(--text-primary);
}

.meter-caption {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
}

/* ---------- table ---------- */

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table.data-table th,
table.data-table td {
  text-align: right;
  padding: 8px 10px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.data-table th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

table.data-table th:first-child,
table.data-table td:first-child {
  text-align: left;
  font-variant-numeric: normal;
}

/* Not display:flex — that would override the <td>'s table-cell display,
   decoupling its height from sibling cells and misaligning its border-bottom
   against the rest of the row. Vertical-align + inline-block keeps it a
   normal table cell while still lining the swatch up with the text. */
table.data-table td.line-name {
  font-weight: 500;
  vertical-align: middle;
}

table.data-table td.line-name span {
  vertical-align: middle;
}

table.data-table .swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  margin-right: 8px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  margin-left: 8px;
}

table.data-table tr.clickable {
  cursor: pointer;
}

table.data-table tr.clickable:hover {
  background: var(--gridline);
}

table.data-table tr.clickable:focus-visible {
  outline: 2px solid var(--line-wholesale);
  outline-offset: -2px;
}

/* ---------- allocation editor ---------- */

.alloc-line {
  padding: 12px 0;
  border-bottom: 1px solid var(--gridline);
}

.alloc-line:last-child {
  border-bottom: none;
}

.alloc-line h3 {
  font-size: 13px;
  font-weight: 600;
  margin: 0 0 8px;
  display: flex;
  align-items: center;
}

.alloc-line h3 .swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  margin-right: 8px;
}

.alloc-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  min-height: 1px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--gridline);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 4px 6px 4px 12px;
  font-size: 12px;
}

.chip.chip-over {
  background: color-mix(in srgb, var(--status-warning) 55%, var(--gridline));
  color: var(--text-primary);
  font-weight: 600;
}

.chip-remove {
  appearance: none;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 999px;
}

.chip-remove:hover,
.chip-remove:focus-visible {
  background: var(--border);
  color: var(--text-primary);
}

.alloc-add-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.alloc-name-input,
.alloc-fraction-select,
.alloc-fraction-custom {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 13px;
  padding: 5px 12px;
  border-radius: 999px;
}

.alloc-name-input {
  flex: 1 1 160px;
  min-width: 120px;
}

.alloc-fraction-custom {
  width: 70px;
}

.alloc-add-btn,
.alloc-save-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 5px 16px;
  border-radius: 999px;
  cursor: pointer;
}

.alloc-add-btn:hover,
.alloc-save-btn:hover {
  background: var(--gridline);
}

.alloc-save-btn {
  background: var(--line-wholesale);
  border-color: var(--line-wholesale);
  color: #fff;
}

.alloc-save-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.alloc-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.alloc-actions .subtitle {
  margin: 0;
}

table.data-table tr.clickable.selected {
  background: var(--gridline);
  box-shadow: inset 3px 0 0 var(--line-wholesale);
}

.drilldown-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.drilldown-header h2 {
  margin: 0;
  flex: 1 1 auto;
}

.trend-jump-btn {
  appearance: none;
  border: 1px solid var(--line-wholesale);
  background: var(--line-wholesale);
  color: #fff;
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 14px;
  border-radius: 999px;
  cursor: pointer;
  flex: none;
  white-space: nowrap;
}

.trend-jump-btn:hover {
  opacity: 0.85;
}

.drilldown-close {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 16px;
  line-height: 1;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  flex: none;
}

.drilldown-close:hover {
  border-color: var(--baseline);
}

#top-customer-drilldown h3 {
  font-size: 13px;
  font-weight: 600;
  margin: 16px 0 8px;
}

#top-customer-drilldown h3:first-child {
  margin-top: 0;
}

.table-scroll-limited {
  overflow-x: auto;
  overflow-y: auto;
  max-height: 384px; /* header + 10 rows, plus a half-row peek of row 11 as a scroll cue */
}

.table-scroll-limited table.data-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-1);
}

/* ---------- responsive ---------- */

@media (max-width: 640px) {
  table.data-table { font-size: 12px; }
  table.data-table th, table.data-table td { padding: 6px 6px; }
}
