/* ==========================================================================
   Tokens — palette, surfaces, ink. Light is default; dark is selected via
   OS preference AND the in-page toggle (data-theme wins over the media query).
   ========================================================================== */
:root {
  color-scheme: light;

  --surface-1:      #fcfcfb;
  --surface-2:      #ffffff;
  --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;

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

  /* sequential — blue ramp */
  --seq-100: #cde2fb;
  --seq-150: #b7d3f6;
  --seq-200: #9ec5f4;
  --seq-450: #2a78d6;
  --seq-track: var(--seq-150);
  --seq-fill: var(--seq-450);

  /* diverging — blue (favorable) <-> gray (neutral) <-> red (unfavorable) */
  --div-lo:  #e34948;
  --div-mid: #f0efec;
  --div-hi:  #2a78d6;

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

  --radius-card: 16px;
  --radius-control: 10px;
  --shadow-card: 0 1px 2px rgba(11,11,11,0.04), 0 8px 24px rgba(11,11,11,0.06);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;

    --surface-1:      #1a1a19;
    --surface-2:      #202020;
    --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;

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

    --seq-track: #24344a;
    --seq-fill: #3987e5;

    --div-lo:  #e66767;
    --div-mid: #383835;
    --div-hi:  #3987e5;

    --shadow-card: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.35);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;

  --surface-1:      #1a1a19;
  --surface-2:      #202020;
  --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;

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

  --seq-track: #24344a;
  --seq-fill: #3987e5;

  --div-lo:  #e66767;
  --div-mid: #383835;
  --div-hi:  #3987e5;

  --shadow-card: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.35);
}

/* ==========================================================================
   Reset & base
   ========================================================================== */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", sans-serif;
  background: var(--page-plane);
  color: var(--text-primary);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-weight: 700; margin: 0; }
p { margin: 0; }
button { font-family: inherit; }
a { color: inherit; }
ul { list-style: none; margin: 0; padding: 0; }

.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 1.25rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ==========================================================================
   Header
   ========================================================================== */
.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface-1) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.site-header__inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.site-header h1 {
  font-size: 1.35rem;
  letter-spacing: -0.01em;
}
.site-header__subtitle {
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-top: 0.15rem;
}
.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 1.05rem;
  flex: none;
  transition: transform 0.15s ease, background 0.15s ease;
}
.icon-btn:hover { background: var(--gridline); }
.icon-btn:active { transform: scale(0.94); }

/* ==========================================================================
   Filter bar
   ========================================================================== */
.filter-bar {
  margin-top: 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1rem 1.25rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow-card);
}
.filter-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.filter-field label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
}
.filter-field--range { flex: none; }
.filter-field--search { margin-left: auto; min-width: 200px; }

select, input[type="date"], input[type="search"] {
  font-size: 0.9rem;
  padding: 0.5em 0.7em;
  border-radius: var(--radius-control);
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
}
select:focus, input:focus { outline: 2px solid var(--series-1); outline-offset: 1px; }

.date-range { display: flex; align-items: center; gap: 0.4em; }
.date-range__sep { color: var(--text-muted); }

.btn {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.55em 1.1em;
  border-radius: var(--radius-control);
  border: 1px solid transparent;
  cursor: pointer;
  transition: filter 0.15s ease, background 0.15s ease;
}
.btn--primary { background: var(--series-1); color: #fff; }
.btn--primary:hover { filter: brightness(1.08); }
.btn--ghost { background: transparent; border-color: var(--border); color: var(--text-secondary); margin-left: 0.4em; }
.btn--ghost:hover { background: var(--gridline); }

.link-btn {
  background: none;
  border: none;
  color: var(--series-1);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.2em 0.4em;
}
.link-btn:hover { text-decoration: underline; }

/* ==========================================================================
   KPI row
   ========================================================================== */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}
.stat-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 1.1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.stat-tile__label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
}
.stat-tile__label small { font-weight: 400; color: var(--text-muted); margin-left: 0.3em; }
.stat-tile__value {
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.stat-tile__delta {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ==========================================================================
   Chart cards
   ========================================================================== */
.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  align-items: stretch;
}
.chart-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 1.25rem 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.chart-card--wide { grid-column: 1 / -1; }
.chart-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0;
}
.chart-card__head h2 { font-size: 1.05rem; }
.chart-card__note {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: -0.4rem;
}
.chart-card__body { flex: 1; }

/* Donut */
#card-donut .chart-card__body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}
@media (min-width: 520px) {
  #card-donut .chart-card__body {
    flex-direction: row;
    align-items: center;
    gap: 1.5rem;
  }
  #card-donut .donut-wrap { flex: none; }
  #card-donut .legend { flex: 1; margin-top: 0; }
}
.donut-wrap {
  position: relative;
  width: 100%;
  max-width: 220px;
  margin: 0 auto;
}
.donut-wrap svg { width: 100%; height: auto; display: block; }
.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  pointer-events: none;
}
.donut-center__value {
  font-size: 1.5rem;
  font-weight: 700;
}
.donut-center__label {
  font-size: 0.7rem;
  color: var(--text-muted);
}
.donut-slice { cursor: pointer; transition: opacity 0.15s ease; }
.donut-slice:hover, .donut-slice:focus-visible { opacity: 0.85; outline: none; }

.legend {
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.legend__item {
  display: flex;
  align-items: center;
  gap: 0.55em;
  font-size: 0.85rem;
  padding: 0.15rem 0.3rem;
  border-radius: 6px;
}
.legend__item:hover { background: var(--page-plane); }
.legend__swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: none;
}
.legend__name { color: var(--text-primary); flex: 1; }
.legend__value { color: var(--text-secondary); font-variant-numeric: tabular-nums; }

/* Diverging bar chart (win-rate ranking) */
.divbar-chart {
  max-height: 460px;
  overflow-y: auto;
  padding-right: 0.25rem;
}
.divbar-row {
  display: grid;
  grid-template-columns: 128px 1fr 52px;
  align-items: center;
  gap: 0.6rem;
  padding: 0.28rem 0;
  cursor: pointer;
  border-radius: 6px;
}
.divbar-row:hover { background: var(--page-plane); }
.divbar-row__name {
  font-size: 0.82rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.divbar-row__track {
  position: relative;
  height: 18px;
  background: var(--page-plane);
  border-radius: 3px;
}
.divbar-row__center {
  position: absolute;
  top: -2px; bottom: -2px;
  left: 50%;
  width: 1px;
  background: var(--baseline);
}
.divbar-row__bar {
  position: absolute;
  top: 1px;
  bottom: 1px;
  border-radius: 4px;
}
.divbar-row__value {
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  text-align: right;
}

/* Heatmap */
.heatmap-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 0.25rem;
}
.heatmap {
  display: grid;
  gap: 2px;
  width: max-content;
}
.heatmap-cell {
  width: 56px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  border-radius: 4px;
  cursor: default;
}
.heatmap-cell--head {
  background: transparent;
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 0.68rem;
  cursor: default;
}
.heatmap-cell--rowhead {
  justify-content: flex-end;
  text-align: right;
  padding-right: 0.5rem;
  white-space: nowrap;
  width: auto;
  min-width: 96px;
}
.heatmap-cell--colhead {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  height: 96px;
  /* vertical writing rotates the flex axes: align-items is now the horizontal
     (block) axis and justify-content the vertical (inline) one — so center
     horizontally and bottom-align toward the grid via justify-content. */
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 0.4rem;
}
.heatmap-cell--empty {
  background: var(--page-plane) !important;
  color: var(--text-muted);
}
.heatmap-cell--diag {
  background: var(--gridline) !important;
  color: var(--text-muted);
}
.scale-legend {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.scale-legend__bar {
  height: 8px;
  flex: 1;
  max-width: 220px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--div-lo), var(--div-mid), var(--div-hi));
}

/* Tooltip (shared) */
.viz-tooltip {
  position: fixed;
  z-index: 60;
  background: var(--text-primary);
  color: var(--surface-1);
  font-size: 0.78rem;
  padding: 0.5em 0.7em;
  border-radius: 8px;
  pointer-events: none;
  max-width: 240px;
  line-height: 1.45;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.1s ease, transform 0.1s ease;
}
.viz-tooltip.is-visible { opacity: 1; transform: translateY(0); }
.viz-tooltip strong { font-variant-numeric: tabular-nums; }

/* Table-view twin */
.table-view {
  margin-top: 0.5rem;
  border-top: 1px solid var(--gridline);
  padding-top: 0.75rem;
  overflow-x: auto;
}
.table-view table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.table-view th, .table-view td {
  padding: 0.4em 0.6em;
  text-align: right;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
}
.table-view th:first-child, .table-view td:first-child { text-align: left; font-variant-numeric: normal; }
.table-view th { color: var(--text-secondary); font-weight: 600; }

/* ==========================================================================
   Deck list table
   ========================================================================== */
.deck-table-wrap { overflow-x: auto; }
#summary-table { width: 100%; border-collapse: collapse; }
#summary-table th {
  text-align: left;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 0.6em 0.75em;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}
#summary-table td {
  padding: 0.55em 0.75em;
  border-bottom: 1px solid var(--gridline);
  vertical-align: middle;
  font-size: 0.88rem;
}
#summary-table th.col-num, #summary-table td.col-num { text-align: right; font-variant-numeric: tabular-nums; }
#summary-table td.col-rank { color: var(--text-muted); font-variant-numeric: tabular-nums; }
#summary-table tbody tr:hover td { background: var(--page-plane); }

.deck-link {
  color: var(--series-1);
  font-weight: 600;
  text-decoration: none;
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
  cursor: pointer;
  font-family: inherit;
}
.deck-link:hover { text-decoration: underline; }

.meter-cell { display: flex; align-items: center; gap: 0.5em; min-width: 150px; }
.meter-cell__value {
  font-variant-numeric: tabular-nums;
  font-size: 0.82rem;
  color: var(--text-secondary);
  width: 3.6em;
  text-align: right;
  flex: none;
}
.meter-track {
  position: relative;
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--seq-track);
  overflow: hidden;
}
.meter-track--diverging { background: var(--page-plane); overflow: visible; }
.meter-fill {
  position: absolute;
  top: 0; bottom: 0;
  left: 0;
  border-radius: 4px;
  background: var(--seq-fill);
}
.meter-fill--div {
  border-radius: 4px;
}
.meter-track--diverging .meter-center {
  position: absolute;
  top: -2px; bottom: -2px;
  left: 50%;
  width: 1px;
  background: var(--baseline);
}

/* ==========================================================================
   Modal
   ========================================================================== */
#modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,10,10,0.55);
  z-index: 1000;
  overflow: auto;
  padding: 3vh 1rem;
}
#modal.is-open { display: flex; align-items: flex-start; justify-content: center; }
#modal-content {
  background: var(--surface-2);
  color: var(--text-primary);
  margin: 0 auto;
  padding: 1.75rem;
  width: 100%;
  max-width: 720px;
  border-radius: var(--radius-card);
  position: relative;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
}
#modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  border-radius: 999px;
}
#modal-close:hover { background: var(--page-plane); }

.modal-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  margin: 1rem 0 1.25rem;
  padding: 0.9rem 1rem;
  background: var(--page-plane);
  border-radius: 12px;
}
.modal-summary__item { display: flex; flex-direction: column; gap: 0.15rem; }
.modal-summary__item span:first-child { font-size: 0.72rem; color: var(--text-muted); font-weight: 600; }
.modal-summary__item span:last-child { font-size: 1.05rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.matchup-list {
  max-height: 360px;
  overflow-y: auto;
  padding-right: 0.25rem;
}
.matchup-row {
  display: grid;
  grid-template-columns: 1fr 120px 60px;
  align-items: center;
  gap: 0.6rem;
  padding: 0.3rem 0;
}
.matchup-row__name {
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.matchup-row__n { font-size: 0.72rem; color: var(--text-muted); margin-left: 0.4em; }

/* ==========================================================================
   Footer
   ========================================================================== */
.site-footer {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2.5rem;
  color: var(--text-muted);
  font-size: 0.78rem;
}

/* ==========================================================================
   Loading state (refetch keeps the frame — no skeleton flash)
   ========================================================================== */
.is-loading { opacity: 0.5; pointer-events: none; transition: opacity 0.15s ease; }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 900px) {
  .chart-grid { grid-template-columns: 1fr; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .page { padding: 0 0.9rem 2.25rem; gap: 1.1rem; }
  .site-header__inner { padding: 0.85rem 0.9rem; }
  .site-header h1 { font-size: 1.1rem; }
  .site-header__subtitle { font-size: 0.78rem; }

  .filter-bar { flex-direction: column; align-items: stretch; padding: 0.9rem; gap: 0.85rem; }
  .filter-field { width: 100%; }
  .filter-field--search { margin-left: 0; width: 100%; }
  .filter-field--range .date-range { flex-wrap: wrap; }
  .filter-field--range input[type="date"] { flex: 1; min-width: 0; }
  .filter-field select, .filter-field input { width: 100%; }
  #apply-filter, #reset-filter { flex: 1; }
  .filter-field:has(#apply-filter) { flex-direction: row; }

  .kpi-row { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .stat-tile { padding: 0.85rem 0.9rem; }
  .stat-tile__value { font-size: 1.25rem; }
  .stat-tile__label { font-size: 0.72rem; }

  .chart-card { padding: 1rem 1rem 1.2rem; }
  .chart-card__head h2 { font-size: 0.95rem; }

  .divbar-row { grid-template-columns: 84px 1fr 44px; gap: 0.4rem; }
  .divbar-row__name { font-size: 0.75rem; }
  .divbar-row__value { font-size: 0.72rem; }
  .divbar-chart { max-height: 380px; }

  .heatmap-cell { width: 46px; height: 34px; font-size: 0.66rem; }
  .heatmap-cell--rowhead { min-width: 76px; font-size: 0.66rem; }
  .heatmap-cell--colhead { height: 82px; }

  #summary-table, #summary-table thead, #summary-table tbody, #summary-table th, #summary-table td, #summary-table tr {
    display: block;
  }
  #summary-table thead { display: none; }
  #summary-table tbody tr {
    margin-bottom: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem 0.75rem;
  }
  #summary-table td {
    border-bottom: none;
    padding: 0.3rem 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
  }
  #summary-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.72rem;
    flex: none;
  }
  .meter-cell { justify-content: flex-end; min-width: 0; width: 60%; }
  .meter-cell__value { width: 3.2em; font-size: 0.78rem; }

  .modal-summary { gap: 0.85rem; padding: 0.75rem 0.85rem; }
  .modal-summary__item span:last-child { font-size: 0.92rem; }
  .matchup-row { grid-template-columns: 1fr 84px 42px; gap: 0.4rem; }
  .matchup-row__name { font-size: 0.78rem; }
  #modal-content { padding: 1.25rem 1rem; }
}

@media (max-width: 380px) {
  .kpi-row { grid-template-columns: 1fr; }
  .divbar-row { grid-template-columns: 72px 1fr 40px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
