/* Great British Grid
   Palette: the validated reference categorical set. Slot order is the
   colourblind-safety mechanism — the stack order below matches slot order 1..8,
   which is the ordering that clears every adjacent gate in both modes. Do not
   reassign a fuel to a different slot without re-running the palette validator. */

:root {
  color-scheme: light;

  --page:            #f9f9f7;
  --surface:         #fcfcfb;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --grid:            #e1e0d9;
  --axis:            #c3c2b7;
  --border:          rgba(11, 11, 11, .10);
  --hover-wash:      rgba(11, 11, 11, .04);
  --selected-wash:   rgba(42, 120, 214, .10);

  /* Categorical slots 1-8 */
  --s1: #2a78d6;  /* wind    */
  --s2: #eb6834;  /* solar   */
  --s3: #1baf7a;  /* hydro   */
  --s4: #eda100;  /* nuclear */
  --s5: #e87ba4;  /* biomass */
  --s6: #008300;  /* imports */
  --s7: #4a3aa7;  /* other   */
  --s8: #e34948;  /* gas     */

  /* Sequential (blue), for magnitude */
  --q1: #cde2fb; --q2: #9ec5f4; --q3: #6da7ec;
  --q4: #3987e5; --q5: #256abf; --q6: #184f95; --q7: #0d366b;

  /* Status — fixed, never themed. Always paired with an icon and a label. */
  /* Map surfaces. Land recedes; GB is tinted so the grid it represents reads
     as the subject rather than one country among several. */
  --map-land: #e9e8e3;
  --map-gb:   #dbe8fa;
  --map-edge: #c9c8c0;

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

  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 11, 11, .04);
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* Dark values are declared under both scopes: the media query covers the OS
   setting, the [data-theme] scope lets the in-page toggle win either direction. */
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19;
    --text-primary: #fff; --text-secondary: #c3c2b7; --text-muted: #898781;
    --grid: #2c2c2a; --axis: #383835;
    --border: rgba(255, 255, 255, .10);
    --hover-wash: rgba(255, 255, 255, .06);
    --selected-wash: rgba(57, 135, 229, .18);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --map-land: #242423; --map-gb: #1b2a3f; --map-edge: #3d3d3a;
    --q1: #0d366b; --q2: #184f95; --q3: #256abf;
    --q4: #3987e5; --q5: #6da7ec; --q6: #9ec5f4; --q7: #cde2fb;
    --success-text: #0ca30c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19;
  --text-primary: #fff; --text-secondary: #c3c2b7; --text-muted: #898781;
  --grid: #2c2c2a; --axis: #383835;
  --border: rgba(255, 255, 255, .10);
  --hover-wash: rgba(255, 255, 255, .06);
  --selected-wash: rgba(57, 135, 229, .18);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --map-land: #242423; --map-gb: #1b2a3f; --map-edge: #3d3d3a;
  --q1: #0d366b; --q2: #184f95; --q3: #256abf;
  --q4: #3987e5; --q5: #6da7ec; --q6: #9ec5f4; --q7: #cde2fb;
  --success-text: #0ca30c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 400 15px/1.55 var(--sans);
  /* The dashboard is wide; never let a chart push the page sideways. */
  overflow-x: hidden;
}

h1, h2, h3 { line-height: 1.25; margin: 0; font-weight: 600; }
p { margin: 0; }
a { color: inherit; text-decoration-color: var(--axis); text-underline-offset: 2px; }
a:hover { text-decoration-color: currentColor; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}
.skip {
  position: absolute; left: -9999px; top: 0; z-index: 10;
  background: var(--surface); padding: 10px 14px; border-radius: 0 0 var(--radius) 0;
}
.skip:focus { left: 0; }

:focus-visible { outline: 2px solid var(--s1); outline-offset: 2px; border-radius: 4px; }

/* ── top bar ───────────────────────────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-in, .hero, .kpis, .filterbar, .grid, .notes, .footer-in {
  max-width: 1360px; margin-inline: auto; padding-inline: clamp(16px, 3vw, 32px);
}
.topbar-in {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; min-height: 56px;
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark { width: 21px; height: 21px; color: var(--s4); flex: none; }
.brand-name { font-weight: 600; letter-spacing: -.01em; }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.livestate {
  display: flex; align-items: center; gap: 7px;
  font-size: 12.5px; color: var(--text-secondary);
}
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-muted); flex: none;
  box-shadow: 0 0 0 2px var(--page);
}
.dot.is-live { background: var(--good); animation: pulse 2.6s ease-in-out infinite; }
.dot.is-stale { background: var(--warning); }
.dot.is-down { background: var(--critical); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .dot.is-live { animation: none; } }

.iconbtn {
  display: grid; place-items: center; width: 30px; height: 30px;
  background: none; border: 1px solid var(--border); border-radius: 8px;
  color: var(--text-secondary); cursor: pointer;
}
.iconbtn:hover { background: var(--hover-wash); color: var(--text-primary); }

/* ── hero ──────────────────────────────────────────────────────────────────── */

.hero {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 28px;
  padding-top: clamp(24px, 4vw, 44px); padding-bottom: 22px;
}
.hero-main { flex: 1 1 320px; min-width: 0; }
.hero-label { font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.hero-figure {
  display: flex; align-items: baseline; gap: 9px;
  font-size: clamp(48px, 9vw, 76px); font-weight: 600;
  letter-spacing: -.035em; line-height: 1;
  /* Proportional figures: tabular-nums makes a large number look loose. */
  font-variant-numeric: proportional-nums;
}
.hero-unit {
  font-size: clamp(14px, 1.6vw, 17px); font-weight: 500;
  color: var(--text-secondary); letter-spacing: 0;
}
.hero-meta { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.hero-vs { font-size: 13px; color: var(--text-secondary); }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; font-weight: 600; padding: 3px 9px 3px 7px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-primary);
}
.badge-ico { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); flex: none; }
.badge[data-level="very low"] .badge-ico,
.badge[data-level="low"]      .badge-ico { background: var(--good); }
.badge[data-level="moderate"] .badge-ico { background: var(--warning); }
.badge[data-level="high"]     .badge-ico { background: var(--serious); }
.badge[data-level="very high"].badge-ico,
.badge[data-level="very high"] .badge-ico { background: var(--critical); }

.hero-spark { flex: 0 1 300px; margin: 0; min-width: 220px; }
.hero-spark figcaption { font-size: 11.5px; color: var(--text-muted); margin-bottom: 4px; }
.spark-holder { height: 54px; }

/* ── KPI tiles ─────────────────────────────────────────────────────────────── */

.kpis {
  display: grid; gap: 12px; padding-bottom: 26px;
  grid-template-columns: repeat(auto-fit, minmax(178px, 1fr));
}
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 13px 15px 14px; box-shadow: var(--shadow);
  min-width: 0;
}
.kpi-label {
  font-size: 12px; color: var(--text-secondary); margin-bottom: 6px;
  display: flex; align-items: center; gap: 6px;
}
.kpi-value {
  font-size: 25px; font-weight: 600; letter-spacing: -.02em; line-height: 1.1;
  font-variant-numeric: proportional-nums;
  display: flex; align-items: baseline; gap: 5px;
}
.kpi-unit { font-size: 12.5px; font-weight: 500; color: var(--text-secondary); letter-spacing: 0; }
.kpi-sub { font-size: 12px; color: var(--text-muted); margin-top: 5px; }
.kpi-status { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; }
.kpi-status svg { width: 13px; height: 13px; flex: none; }
.kpi-status[data-s="good"]     { color: var(--success-text); }
.kpi-status[data-s="warning"]  { color: var(--text-primary); }
.kpi-status[data-s="critical"] { color: var(--critical); }

/* ── filter bar ────────────────────────────────────────────────────────────── */

.filterbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding-bottom: 18px;
}
.filterbar-label {
  font-size: 12px; font-weight: 600; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: .06em;
}
.segmented {
  display: inline-flex; background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 2px; gap: 2px;
  /* Five range labels overflow a narrow phone on one line; let them wrap. */
  flex-wrap: wrap; max-width: 100%;
}
.segmented button {
  font: 500 13px var(--sans); color: var(--text-secondary);
  background: none; border: 0; border-radius: 7px;
  padding: 5px 12px; cursor: pointer; white-space: nowrap;
}
.segmented button:hover { background: var(--hover-wash); color: var(--text-primary); }
.segmented button[aria-pressed="true"] {
  background: var(--selected-wash); color: var(--text-primary); font-weight: 600;
}
.filterbar-note { font-size: 12px; color: var(--text-muted); }

/* ── chart cards ───────────────────────────────────────────────────────────── */

.grid {
  display: grid; gap: 14px; padding-bottom: 32px;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
}
.card-wide { grid-column: 1 / -1; }
@media (min-width: 1000px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px 14px;
  box-shadow: var(--shadow); min-width: 0;
  /* Refetch keeps the frame: fade, never a skeleton or a layout jump. */
  transition: opacity .18s ease;
}
.card.is-loading { opacity: .45; }
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 14px; margin-bottom: 10px;
}
.card h3 { font-size: 14.5px; letter-spacing: -.005em; }
.card-sub { font-size: 12px; color: var(--text-secondary); margin-top: 3px; max-width: 62ch; }
.card-tools { display: flex; gap: 6px; flex: none; }
.toolbtn {
  font: 500 12px var(--sans); color: var(--text-secondary);
  background: none; border: 1px solid var(--border);
  border-radius: 7px; padding: 4px 9px; cursor: pointer; white-space: nowrap;
}
.toolbtn:hover { background: var(--hover-wash); color: var(--text-primary); }
.toolbtn[aria-pressed="true"] { background: var(--selected-wash); color: var(--text-primary); }

/* Height includes the x-axis band, so the axis never gets its own scrollbar. */
.plot { height: 236px; position: relative; }
.card-wide .plot { height: 300px; }
/* Bar lists grow with their content — a fixed height would clip rows and
   spill them over the section below. Specificity has to beat `.card-wide .plot`. */
.plot.plot-bars, .card-wide .plot.plot-bars { height: auto; min-height: 60px; }
.plot svg { display: block; width: 100%; height: 100%; overflow: visible; }

.plot-empty {
  display: grid; place-items: center; height: 100%;
  font-size: 13px; color: var(--text-muted); text-align: center; padding: 20px;
}

/* ── legend ────────────────────────────────────────────────────────────────── */

.legend {
  display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 10px;
  font-size: 12px; color: var(--text-secondary);
}
.legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  background: none; border: 0; padding: 1px 0; cursor: pointer;
  font: inherit; color: inherit; border-radius: 4px;
}
.legend-item[aria-pressed="false"] { opacity: .4; }
.legend-item:hover { color: var(--text-primary); }
/* Legends mirror the mark: a rect for areas and bars, a stroke for lines. */
.legend-swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.legend-line { width: 14px; height: 2px; border-radius: 1px; flex: none; }
.legend-line.is-dotted {
  height: 0; border-top: 2px dotted currentColor; background: none !important;
}
.legend-val { color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ── bar lists (interconnectors, regions) ──────────────────────────────────── */

.barlist { display: grid; gap: 2px; }
.barrow {
  display: grid; grid-template-columns: minmax(104px, 190px) 1fr minmax(74px, auto);
  align-items: center; gap: 12px; padding: 3px 4px; border-radius: 6px;
  /* Hit target comfortably exceeds the 24px minimum. */
  min-height: 28px;
}
.barrow:hover, .barrow:focus-visible { background: var(--hover-wash); }
.barrow-name { font-size: 12.5px; min-width: 0; }
.barrow-name b { font-weight: 600; }
.barrow-name span { color: var(--text-muted); }
.barrow-track { position: relative; height: 15px; }
.barrow-val {
  font-size: 12.5px; text-align: right; font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}
.barrow-val b { color: var(--text-primary); font-weight: 600; }
@media (max-width: 560px) {
  /* Give the bar its share back: at phone width a 190px name column leaves the
     track too short to compare anything. */
  .barrow { grid-template-columns: minmax(88px, 124px) 1fr minmax(66px, auto); gap: 8px; }
  .barrow-name { font-size: 12px; }
}

.axis-zero {
  font-size: 11px; color: var(--text-muted); text-align: center;
  padding-top: 4px; border-top: 1px solid var(--grid); margin-top: 4px;
}
.barlegend {
  display: flex; gap: 14px; font-size: 12px; color: var(--text-secondary);
  margin-bottom: 8px; flex-wrap: wrap;
}

/* ── table views ───────────────────────────────────────────────────────────── */

.tableview { overflow-x: auto; max-height: 340px; overflow-y: auto; margin-top: 4px; }
.tableview table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.tableview th, .tableview td {
  padding: 5px 10px; text-align: right; white-space: nowrap;
  border-bottom: 1px solid var(--grid);
}
.tableview th:first-child, .tableview td:first-child { text-align: left; }
.tableview thead th {
  position: sticky; top: 0; background: var(--surface);
  font-weight: 600; color: var(--text-secondary);
  border-bottom: 1px solid var(--axis);
}
.tableview td { font-variant-numeric: tabular-nums; color: var(--text-secondary); }
.tableview td:first-child { color: var(--text-primary); }
.tableview caption {
  caption-side: top; text-align: left; font-size: 12px;
  color: var(--text-muted); padding-bottom: 8px;
}

/* ── tooltip ───────────────────────────────────────────────────────────────── */

.tip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px;
  box-shadow: 0 4px 14px rgba(11, 11, 11, .12);
  font-size: 12px; min-width: 130px; max-width: 260px;
}
.tip-head {
  font-weight: 600; color: var(--text-primary);
  margin-bottom: 5px; padding-bottom: 5px; border-bottom: 1px solid var(--grid);
}
.tip-row {
  display: grid; grid-template-columns: 14px 1fr auto;
  align-items: center; gap: 7px; padding: 1.5px 0;
}
/* Values lead, labels follow: the reader has the series and wants the number. */
.tip-row .k { width: 14px; height: 2px; border-radius: 1px; }
.tip-row .n { color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; }
.tip-row .v {
  color: var(--text-primary); font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.tip-note { color: var(--text-muted); margin-top: 5px; font-size: 11.5px; }

/* ── notes & footer ────────────────────────────────────────────────────────── */

.notes { padding-block: 8px 34px; }
.notes h2 { font-size: 15px; margin-bottom: 14px; }
.notes-grid {
  display: grid; gap: 22px 34px;
  grid-template-columns: repeat(auto-fit, minmax(258px, 1fr));
}
.notes h3 { font-size: 13px; margin-bottom: 5px; }
.notes p { font-size: 13px; color: var(--text-secondary); max-width: 58ch; }

.footer { border-top: 1px solid var(--border); background: var(--surface); }
.footer-in {
  display: grid; gap: 22px 34px; padding-block: 26px 34px;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  font-size: 12.5px; color: var(--text-secondary);
}
.footer-strong { font-weight: 600; color: var(--text-primary); margin-bottom: 7px; }
.footer p + p { margin-top: 6px; }
.footer-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.footer-list a { font-weight: 500; }
.footer code { font: 500 11.5px var(--mono); }
.footer-archive { color: var(--text-muted); }

/* ── print / forced colours: texture is the backup identity channel ────────── */

@media (forced-colors: active) {
  .card, .kpi, .tip { border: 1px solid CanvasText; }
  .dot, .badge-ico { forced-color-adjust: none; }
}


/* ── interconnector map ────────────────────────────────────────────────────── */

.intergrid {
  display: grid; gap: 20px; align-items: start;
  grid-template-columns: minmax(300px, 560px) 1fr;
}
@media (max-width: 820px) { .intergrid { grid-template-columns: 1fr; } }

.intermap { min-width: 0; }
.intermap svg {
  display: block; width: 100%; height: auto;
  /* Clip the coastline's cut edges, which sit just outside the frame. */
  overflow: hidden;
}

.map-land { fill: var(--map-land); stroke: var(--map-edge); stroke-width: .7; }
.map-gb   { fill: var(--map-gb);   stroke: var(--map-edge); stroke-width: .7; }

.map-label, .map-label-gb {
  font-size: 9px; fill: var(--text-muted); pointer-events: none;
  paint-order: stroke; stroke: var(--surface); stroke-width: 2.5px; stroke-linejoin: round;
}
.map-label-gb { font-size: 10px; font-weight: 600; fill: var(--text-secondary); }

.map-cable { cursor: pointer; }
.map-cable-casing {
  fill: none; stroke: var(--surface); stroke-width: 5;
  /* The surface ring, applied to a line: keeps crossing cables legible. */
}
.map-cable-line { fill: none; stroke-linecap: round; }
.map-cable-flow {
  fill: none; stroke-linecap: round; stroke-dasharray: 3 14;
  animation: gbg-flow 1.4s linear infinite;
}
/* Dash plus gap, so the cycle repeats seamlessly. */
@keyframes gbg-flow { to { stroke-dashoffset: -17; } }

.map-cable-hit { fill: none; stroke: transparent; stroke-width: 20; }
.map-cable-label {
  font-size: 9px; font-weight: 600; fill: var(--text-secondary); pointer-events: none;
  /* Halo in the surface colour so a label stays readable over land or a cable. */
  paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round;
}
.map-node { stroke: var(--surface); stroke-width: 1.5; }

.map-cable:focus { outline: none; }
.map-cable:focus-visible .map-cable-line { stroke-width: 5; }
.map-cable:hover .map-cable-line { stroke-width: 4; }

@media (prefers-reduced-motion: reduce) {
  .map-cable-flow { animation: none; }
}
@media (forced-colors: active) {
  .map-land, .map-gb { fill: Canvas; stroke: CanvasText; }
  .map-cable-line, .map-cable-flow { stroke: CanvasText; }
}
