/* ═══════════════════════════════════════════════════════════════════════
   cf-speedtest — AFDC theme
   Consumes the v2 design system (shared/design-tokens-v2.css + primitives).
   All color/spacing/typography comes from tokens so the light/dark switch
   is a single data-theme attribute flip.
   ═══════════════════════════════════════════════════════════════════════ */

/* Semantic colors unique to the speedtest — orange/purple are NOT brand primary,
   they're semantic "down direction" and "up direction" per the Cloudflare
   speedtest convention. Theme-aware via HSL pairs. */
:root, [data-theme="light"] {
  --dl:         #f48120;
  --dl-soft:    rgba(244, 129, 32, 0.12);
  --dl-soft-2:  rgba(244, 129, 32, 0.02);
  --ul:         #7030A0;   /* maps to AFDC primary purple, same semantic as Cloudflare's */
  --ul-soft:    rgba(112, 48, 160, 0.14);
  --ul-soft-2:  rgba(112, 48, 160, 0.02);
  --p90:        #cbd3de;
}
[data-theme="dark"] {
  --dl:         #ffa05a;
  --dl-soft:    rgba(255, 160, 90, 0.14);
  --dl-soft-2:  rgba(255, 160, 90, 0.02);
  --ul:         #B080E0;
  --ul-soft:    rgba(176, 128, 224, 0.18);
  --ul-soft-2:  rgba(176, 128, 224, 0.02);
  --p90:        #3a3f52;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg-page); }
body {
  color: var(--fg);
  font-family: var(--f-family);
  font-size: var(--f-sm);
  line-height: 1.5;
  font-feature-settings: "ss01", "cv11";
  -webkit-font-smoothing: antialiased;
  transition: background var(--t-slow), color var(--t-slow);
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
.tnum { font-variant-numeric: tabular-nums; }

.page { max-width: var(--content-max); margin: 0 auto; padding: var(--s5) var(--s7) 0; }

/* ─── Top header ────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s2) 0 var(--s4);
  border-bottom: 1px solid var(--border-default);
  margin-bottom: var(--s5);
}
.brand { display: flex; align-items: center; gap: var(--s3); }
.brand-mark {
  width: 32px; height: 32px; display: grid; place-items: center;
  color: var(--primary);
  background: var(--primary-bg);
  border-radius: var(--radius-sm);
}
.brand-mark svg { width: 20px; height: 20px; }
.brand-title {
  font-size: var(--f-lg); font-weight: var(--f-w-bold); letter-spacing: -0.01em;
}
.topbar-right {
  font-size: var(--f-xs); color: var(--fg-muted); font-weight: var(--f-w-medium);
  display: flex; align-items: center; gap: var(--s3);
}

/* ─── Main grid (Speed | Map) ───────────────────────────── */
.main-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(340px, 1fr);
  gap: var(--s6);
}
@media (max-width: 1100px) { .main-grid { grid-template-columns: 1fr; } }

.panel { background: transparent; padding: 0; }
.panel-title {
  font-size: var(--f-md); font-weight: var(--f-w-semibold);
  letter-spacing: -0.005em; color: var(--fg);
  display: flex; align-items: center; gap: var(--s2);
  margin: 0 0 var(--s3);
}
.info-dot {
  display: inline-flex; width: 14px; height: 14px;
  border: 1px solid var(--fg-dim); border-radius: 50%;
  align-items: center; justify-content: center;
  color: var(--fg-dim); font-size: 9px; font-weight: var(--f-w-bold);
}

/* ─── Speed panel ────────────────────────────────────────── */
.speed-grid {
  display: grid;
  grid-template-columns: 1fr 1fr minmax(130px, auto);
  gap: var(--s7);
  padding: var(--s1) 0 var(--s3);
}
.speed-block { min-width: 0; }
.speed-block-header {
  display: flex; align-items: baseline; gap: var(--s2);
  font-size: var(--f-xs); font-weight: var(--f-w-semibold);
  color: var(--fg); letter-spacing: 0.02em;
  margin-bottom: var(--s1);
}
.speed-block-header.dl { color: var(--dl); }
.speed-block-header.ul { color: var(--ul); }
.speed-value {
  font-size: var(--f-4xl); font-weight: var(--f-w-regular);
  letter-spacing: -0.03em; line-height: 1.02;
  font-variant-numeric: tabular-nums;
  display: flex; align-items: baseline; gap: var(--s2);
  color: var(--fg);
}
.speed-value .unit {
  font-size: var(--f-sm); font-weight: var(--f-w-regular);
  color: var(--fg-muted); letter-spacing: 0;
}
.chart-wrap { position: relative; height: 86px; margin-top: var(--s2); }
.chart-wrap canvas { width: 100%; height: 100%; display: block; }
.chart-p90-label {
  position: absolute; top: 2px; left: 4px;
  font-size: 10px; color: var(--fg-dim);
  pointer-events: none; padding: 0 3px;
  background: var(--bg-page);
}

.sub-metrics { display: flex; flex-direction: column; gap: var(--s3); min-width: 120px; }
.sub-metric-title {
  font-size: var(--f-xs); font-weight: var(--f-w-semibold);
  color: var(--fg); letter-spacing: 0.02em;
}
.sub-metric-value {
  font-size: var(--f-2xl); font-weight: var(--f-w-regular);
  letter-spacing: -0.02em; font-variant-numeric: tabular-nums;
  line-height: 1.1; color: var(--fg);
}
.sub-metric-value .unit {
  font-size: var(--f-xs); color: var(--fg-muted);
  margin-left: 3px; font-weight: var(--f-w-regular); letter-spacing: 0;
}
.sub-metric-split {
  display: flex; gap: var(--s3);
  font-size: 11px; color: var(--fg-secondary);
  font-variant-numeric: tabular-nums; margin-top: 2px;
}
.sub-metric-split .dl:before { content: "↓ "; color: var(--dl); font-weight: var(--f-w-bold); }
.sub-metric-split .ul:before { content: "↑ "; color: var(--ul); font-weight: var(--f-w-bold); }

/* ─── Actions row ───────────────────────────────────────── */
.actions {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s4) 0 var(--s1); margin-top: var(--s1);
  flex-wrap: wrap; gap: var(--s3);
}
.actions-left { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.measured-at {
  font-size: var(--f-xs); color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
}
.measured-at .clock-icon { color: var(--fg-muted); margin-right: 4px; }

/* ─── Server Location panel ─────────────────────────────── */
.map-panel { min-width: 0; }
.map-wrap {
  position: relative;
  height: 260px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--bg-subtle);
}
.map-wrap .leaflet-container { background: var(--bg-muted); }
[data-theme="dark"] .leaflet-tile { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.95); }

.server-info {
  margin-top: var(--s3);
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  padding: var(--s3) var(--s4);
  display: grid; gap: var(--s2);
  font-size: var(--f-xs);
}
.server-info-row {
  display: grid;
  grid-template-columns: 24px minmax(110px, 1fr) minmax(100px, auto);
  align-items: baseline; gap: var(--s2);
}
.server-info-row .icon { color: var(--fg-muted); width: 16px; display: inline-flex; }
.server-info-row .key { color: var(--fg-muted); }
.server-info-row .val { font-weight: var(--f-w-medium); text-align: right; color: var(--fg); }

/* ─── Access Path ───────────────────────────────────────── */
.path-card {
  margin-top: var(--s3);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  padding: var(--s3) var(--s4) var(--s3);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-xs);
}
.path-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: var(--s2); gap: var(--s2); flex-wrap: wrap;
}
.path-loc-badge {
  width: 100%; margin-top: 4px;
  padding: 4px var(--s2); border-radius: var(--radius-xs);
  background: var(--primary-bg); color: var(--primary);
  font-size: 11px; font-weight: var(--f-w-medium);
  display: flex; align-items: center; gap: var(--s1);
}
.path-loc-badge strong { font-weight: var(--f-w-semibold); }
.path-loc-badge .loc-ip {
  font-family: var(--f-mono); font-size: 10px; color: var(--primary);
  opacity: .75; margin-left: auto;
}
.path-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.14em;
  color: var(--fg-muted); font-weight: var(--f-w-semibold);
}
.path-kind {
  font-size: var(--f-xs); font-weight: var(--f-w-semibold); color: var(--dl);
  display: inline-flex; align-items: center; gap: var(--s2);
}
.path-kind::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--dl); box-shadow: 0 0 0 3px var(--dl-soft);
}
.path-kind.is-lan    { color: var(--cta); }
.path-kind.is-lan::before { background: var(--cta); box-shadow: 0 0 0 3px var(--cta-bg); }
.path-kind.is-ts     { color: var(--primary); }
.path-kind.is-ts::before  { background: var(--primary); box-shadow: 0 0 0 3px var(--primary-bg); }

.path-diagram {
  display: flex; align-items: center; gap: 0;
  padding: var(--s2) 0;
  overflow-x: auto;
}
.path-hop {
  flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center;
  min-width: 72px; position: relative;
}
.path-hop .node {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  border: 1.5px solid var(--border-default); background: var(--bg);
  font-size: 17px; color: var(--fg); font-weight: var(--f-w-semibold);
  z-index: 2; transition: all var(--t-base);
}
.path-hop.active .node {
  background: var(--dl); color: #fff; border-color: var(--dl);
  box-shadow: 0 0 0 4px var(--dl-soft);
}
.path-hop .hop-label {
  margin-top: 5px; font-size: 10px; font-weight: var(--f-w-semibold); color: var(--fg);
  letter-spacing: 0.01em; white-space: nowrap;
}
.path-hop .hop-sub {
  font-size: 9px; color: var(--fg-muted); margin-top: 1px;
  font-variant-numeric: tabular-nums; white-space: nowrap;
  font-family: var(--f-mono);
}
.path-edge {
  flex: 1 1 auto;
  height: 2px;
  margin: 0 -2px;
  position: relative; top: -14px;
  min-width: 18px;
  background: repeating-linear-gradient(
    90deg,
    var(--border-default) 0 8px, transparent 8px 16px
  );
}
.path-edge.active {
  background-size: 20px 2px;
  background-image: repeating-linear-gradient(
    90deg, var(--dl) 0 8px, var(--dl-soft) 8px 16px
  );
  animation: flow 1.6s linear infinite;
}
@keyframes flow { 0% { background-position: 0 0; } 100% { background-position: 20px 0; } }

.path-diagram[data-kind="lan-home"] .path-edge.active,
.path-diagram[data-kind="lan-factory"] .path-edge.active {
  background-image: repeating-linear-gradient(
    90deg, var(--cta) 0 8px, var(--cta-bg) 8px 16px
  );
}
.path-diagram[data-kind="tailscale"] .path-edge.active {
  background-image: repeating-linear-gradient(
    90deg, var(--primary) 0 8px, var(--primary-bg) 8px 16px
  );
}
.path-diagram[data-kind="lan-home"] .path-hop.active .node,
.path-diagram[data-kind="lan-factory"] .path-hop.active .node {
  background: var(--cta); border-color: var(--cta);
  box-shadow: 0 0 0 4px var(--cta-bg);
}
.path-diagram[data-kind="tailscale"] .path-hop.active .node {
  background: var(--primary); border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--primary-bg);
}

.path-detail {
  font-size: 11px; color: var(--fg-muted);
  line-height: 1.55;
  margin-top: var(--s2); padding-top: var(--s2);
  border-top: 1px dashed var(--border-subtle);
}

/* ─── Network Quality Score ─────────────────────────────── */
.nqs-section { margin-top: var(--s7); }
.nqs-title-row { display: flex; align-items: center; gap: var(--s3); margin-bottom: var(--s3); }
.nqs-panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  padding: var(--s4) var(--s5);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--s6);
  position: relative;
  box-shadow: var(--shadow-xs);
}
.nqs-panel .dot-sep {
  position: absolute; top: 50%;
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--fg-dim);
  transform: translate(-50%, -50%);
}
.nqs-panel .dot-sep.first  { left: 33.33%; }
.nqs-panel .dot-sep.second { left: 66.66%; }
.nqs-cell {
  display: flex; align-items: baseline; justify-content: center; gap: var(--s3);
  font-size: var(--f-sm);
}
.nqs-cell .key { font-weight: var(--f-w-medium); color: var(--fg); }
.nqs-cell .val { font-weight: var(--f-w-bold); font-size: var(--f-sm); }
.nqs-good    { color: var(--cta); }
.nqs-average { color: var(--warning); }
.nqs-poor    { color: var(--danger); }

/* ─── Measurements ──────────────────────────────────────── */
.meas-section { margin-top: var(--s8); }
.meas-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--s7);
  align-items: flex-start;
}
@media (max-width: 1100px) { .meas-grid { grid-template-columns: 1fr; } }
.meas-col-title {
  font-size: var(--f-md); font-weight: var(--f-w-semibold);
  display: flex; align-items: center; gap: var(--s2);
  margin-bottom: var(--s3); color: var(--fg);
}

.boxplot-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  padding: var(--s2) var(--s3) var(--s3);
  margin-bottom: var(--s2);
  box-shadow: var(--shadow-xs);
  transition: box-shadow var(--t-fast);
}
.boxplot-card:hover { box-shadow: var(--shadow-sm); }
.boxplot-head {
  display: flex; align-items: center; justify-content: space-between;
  font-size: var(--f-sm); font-weight: var(--f-w-medium); color: var(--fg);
}
.boxplot-head .count {
  color: var(--fg-muted); font-weight: var(--f-w-regular);
  margin-left: var(--s2); font-family: var(--f-mono); font-size: 11px;
}
.boxplot-head .caret { color: var(--fg-muted); font-size: 14px; }
.boxplot-svg { width: 100%; height: 40px; display: block; margin-top: 2px; }

.loss-bar {
  position: relative;
  background: var(--bg-subtle);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xs);
  height: 24px; overflow: hidden;
}
.loss-bar .received {
  position: absolute; left: 0; top: 0; height: 100%;
  background: var(--cta);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 11px; font-weight: var(--f-w-semibold);
  letter-spacing: 0.5px; transition: width var(--t-slow);
}
.loss-bar .lost-label {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  color: var(--fg); font-size: 11px; font-weight: var(--f-w-semibold);
}
.loss-meta {
  font-size: 10px; color: var(--fg-muted); margin-top: var(--s1);
  display: flex; justify-content: space-between;
  font-variant-numeric: tabular-nums; font-family: var(--f-mono);
}

/* ─── Footer ────────────────────────────────────────────── */
.footer {
  margin-top: var(--s10); padding: var(--s5) 0;
  border-top: 1px solid var(--border-default);
  display: flex; align-items: center; justify-content: space-between;
  font-size: var(--f-xs); color: var(--fg-muted);
}
.footer-left { display: flex; gap: var(--s5); }
.footer-left a:hover { color: var(--fg); }
.footer-right { color: var(--fg-muted); font-size: 11px; }
.footer-right .brand-accent { color: var(--primary); font-weight: var(--f-w-semibold); }

/* ─── Theme toggle ──────────────────────────────────────── */
.theme-toggle {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--bg-elevated); border: 1px solid var(--border-default);
  color: var(--fg-secondary); cursor: pointer;
  transition: all var(--t-fast); padding: 0;
}
.theme-toggle:hover {
  background: var(--bg-hover); color: var(--primary);
  border-color: var(--primary-border);
  transform: rotate(12deg);
}
.theme-toggle .icon { font-size: 16px; line-height: 1; }

/* uPlot theming hooks */
.u-legend, .u-axis { display: none; }
.u-over { cursor: default; }

.flash { animation: flash 0.5s var(--ease); }
@keyframes flash {
  0%   { background: var(--dl-soft); }
  100% { background: transparent; }
}
