:root {
  --bg: #f5f1e8;
  --bg-elev: #ece5d4;
  --bg-elev-2: #e2dcc6;
  --fg: #1a1a1a;
  --fg-soft: #3a3a3a;
  --muted: #6e6a60;
  --rule: rgba(26, 26, 26, 0.18);
  --accent: #cb1c1f; /* overridden per club via JS */
  --accent-glow: rgba(203, 28, 31, 0.35);

  --font-display: "Rockwell", "Rockwell Nova", "Rokkitt", "Roboto Slab", Georgia, serif;
  --font-body: "Rockwell", "Rockwell Nova", "Rokkitt", "Roboto Slab", Georgia, serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", ui-monospace, monospace;

  --shadow-soft: 0 14px 32px -16px rgba(26, 26, 26, 0.22);
  --shadow-card: 0 18px 60px -18px rgba(26, 26, 26, 0.28);
  --sidebar-w: 15.5rem;
  --plays-w: min(26rem, 42vw);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-feature-settings: "kern" 1, "liga" 1;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

::selection { background: var(--accent); color: var(--bg); }

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ── Canvas + atmosphere ───────────────────────────────────────── */

.canvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  z-index: 0;
  cursor: grab;
}
.canvas:active { cursor: grabbing; }
.canvas.over-node { cursor: pointer; }

.labels {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  overflow: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background-image:
    linear-gradient(to right, rgba(26, 26, 26, 0.045) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(26, 26, 26, 0.045) 1px, transparent 1px);
  background-size: 38px 38px;
}

.vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background: radial-gradient(ellipse at center, transparent 35%, rgba(26, 26, 26, 0.18) 110%);
  opacity: 0.85;
}

.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0.11;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.6 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

/* ── Masthead ──────────────────────────────────────────────────── */

.masthead {
  position: fixed;
  top: 1.8rem;
  left: 2rem;
  z-index: 4;
  max-width: min(46vw, 34rem);
  pointer-events: none;
}
.masthead > * { pointer-events: auto; }

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  color: var(--fg);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
}
.brand-mark {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--bg), 0 0 0 4px var(--fg);
  transition: background 0.4s ease;
}

.back-button {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: 1rem;
  padding: 0.2rem 0.6rem;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-elev);
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  vertical-align: middle;
}
.back-button:hover { border-color: var(--fg); }

.masthead-team {
  margin: 0.55rem 0 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.7rem, 3.2vw, 2.6rem);
  line-height: 1.05;
  letter-spacing: -0.01em;
}
.team-accent { color: var(--accent); }

.masthead-sub {
  margin: 0.55rem 0 0;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ── Stats (top-right) ─────────────────────────────────────────── */

.stats {
  position: fixed;
  top: 1.9rem;
  right: 2rem;
  z-index: 4;
  display: flex;
  gap: 1.6rem;
}
.stat { display: flex; flex-direction: column; align-items: flex-end; }
.stat-label {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.stat-value {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.15;
}

/* ── Rail (bottom-left) ────────────────────────────────────────── */

.rail {
  position: fixed;
  left: 2rem;
  bottom: 1.8rem;
  z-index: 4;
}
.rail-form { margin: 0; }
.control-label {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.6rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.team-button {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 14rem;
  padding: 0.55rem 0.8rem 0.55rem 0.6rem;
  border: 1px solid var(--rule);
  border-radius: 12px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-soft);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s ease;
}
.team-button:hover { border-color: var(--fg); }
.team-button-crest { width: 26px; height: 26px; object-fit: contain; }
.team-button-chevron { margin-left: auto; color: var(--muted); }

/* ── Sidebar ───────────────────────────────────────────────────── */

.sidebar {
  position: fixed;
  left: 2rem;
  top: 9rem;
  z-index: 4;
  width: var(--sidebar-w);
  max-height: calc(100vh - 16rem);
  overflow-y: auto;
  padding: 0.9rem 0.95rem 0.6rem;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(6px);
  scrollbar-width: thin;
}
.sidebar-content { transition: opacity 0.2s ease; }
.sidebar-content.fading { opacity: 0; }

.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.4rem;
}
.sidebar-section-context {
  color: var(--fg);
  letter-spacing: 0.05em;
  text-transform: none;
  font-family: var(--font-body);
  font-size: 0.85rem;
  font-weight: 600;
}
.headshot {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  object-position: top;
  background: var(--bg-elev-2);
  border: 1.5px solid var(--accent);
  flex: none;
}
.headshot-pair { display: inline-flex; }
.headshot-pair .headshot + .headshot { margin-left: -8px; }

.metric-card {
  padding: 0.55rem 0 0.6rem;
  border-top: 1px solid var(--rule);
}
.metric-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.metric-label {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.metric-info {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0 0.1rem;
}
.metric-info:hover { color: var(--fg); }
.metric-value {
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  line-height: 1.1;
  margin: 0.15rem 0 0.25rem;
}
.metric-stars { display: flex; gap: 2px; margin-bottom: 0.3rem; }
.bball circle, .bball line, .bball path { stroke-width: 1.3; }
.bball.filled circle { fill: var(--accent); stroke: var(--accent); }
.bball.filled line, .bball.filled path { stroke: var(--bg); }
.bball.empty circle { fill: none; stroke: var(--rule); }
.bball.empty line, .bball.empty path { stroke: var(--rule); }

.metric-range, .metric-spectrum {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0.2rem 0 0.3rem;
  font-family: var(--font-mono);
  font-size: 0.58rem;
  color: var(--muted);
}
.metric-range-track {
  position: relative;
  flex: 1;
  height: 1px;
  background: var(--rule);
}
.metric-range-dot {
  position: absolute;
  top: 50%;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(-50%, -50%);
}
.metric-rank {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.metric-rank.clickable {
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.metric-rank.clickable:hover { color: var(--fg); }
.metric-pair-sub {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.rank-popover {
  position: fixed;
  z-index: 9;
  width: 14rem;
  max-height: 20rem;
  overflow-y: auto;
  padding: 0.3rem;
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: var(--bg);
  box-shadow: var(--shadow-card);
  scrollbar-width: thin;
}
.rank-popover-list { list-style: none; margin: 0; padding: 0; }
.rank-popover-item {
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  gap: 0.4rem;
  padding: 0.3rem 0.45rem;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
}
.rank-popover-item:hover { background: var(--bg-elev); }
.rank-popover-item.current { background: var(--bg-elev-2); font-weight: 600; }
.rank-popover-rank, .rank-popover-value {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  color: var(--muted);
}
.rank-popover-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-toggle { display: none; }

/* ── Labels ────────────────────────────────────────────────────── */

.label {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, 0);
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: auto;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.25s ease;
  will-change: transform;
}
.label-name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.86rem;
  color: var(--fg);
  text-shadow: 0 0 6px var(--bg), 0 0 2px var(--bg);
}
.label-stat {
  font-family: var(--font-mono);
  font-size: 0.56rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-shadow: 0 0 6px var(--bg);
}
.label.dimmed { opacity: 0.22; }
.label.hidden { opacity: 0; pointer-events: none; }
.label.pinned .label-name { color: var(--accent); }
.label.hover .label-name { text-decoration: underline; text-underline-offset: 3px; }

/* ── Hint, errors, footer ──────────────────────────────────────── */

.hint {
  position: fixed;
  left: 50%;
  bottom: 2.2rem;
  z-index: 5;
  transform: translate(-50%, 8px);
  max-width: 24rem;
  padding: 0.6rem 1rem;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-soft);
  font-size: 0.82rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.hint.show { opacity: 1; transform: translate(-50%, 0); }

.load-error {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: none;
  place-items: center;
  background: rgba(245, 241, 232, 0.7);
}
.load-error.visible { display: grid; }
.load-error-msg { font-size: 1.1rem; margin: 0 0 0.6rem; }
.load-error-retry {
  padding: 0.5rem 1rem;
  border: 1px solid var(--fg);
  border-radius: 999px;
  background: var(--bg);
  cursor: pointer;
}

.meta {
  position: fixed;
  right: 2rem;
  bottom: 1.6rem;
  z-index: 4;
  font-family: var(--font-mono);
  font-size: 0.58rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ── Team overlay ──────────────────────────────────────────────── */

.team-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  overflow-y: auto;
  padding: 4rem 3rem;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(10px);
}
.team-overlay.open { display: block; }
.team-overlay-close {
  position: fixed;
  top: 1.4rem;
  right: 1.8rem;
  width: 2.4rem;
  height: 2.4rem;
  border: 1px solid var(--rule);
  border-radius: 50%;
  background: var(--bg-elev);
  font-size: 1.3rem;
  cursor: pointer;
}
.team-overlay-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
  gap: 0.9rem;
  max-width: 70rem;
  margin: 0 auto;
}
.team-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1.1rem 0.8rem 0.9rem;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: var(--bg-elev);
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.team-cell:hover, .team-cell:focus-visible {
  transform: translateY(-2px);
  border-color: var(--team-accent);
  box-shadow: var(--shadow-soft);
  outline: none;
}
.team-cell.current { border-color: var(--team-accent); box-shadow: inset 0 0 0 1px var(--team-accent); }
.team-cell-logo { width: 56px; height: 56px; object-fit: contain; }
.team-cell-name { font-weight: 600; font-size: 0.9rem; text-align: center; line-height: 1.2; }
.team-cell-country {
  font-family: var(--font-mono);
  font-size: 0.56rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ── Info modal ────────────────────────────────────────────────── */

.info-modal {
  width: min(28rem, calc(100vw - 2rem));
  padding: 1.6rem 1.7rem 1.5rem;
  border: 1px solid var(--rule);
  border-radius: 16px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow-card);
}
.info-modal::backdrop { background: rgba(26, 26, 26, 0.35); backdrop-filter: blur(3px); }
.info-modal-close {
  position: absolute;
  top: 0.7rem;
  right: 0.8rem;
  border: 0;
  background: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--muted);
}
.info-modal-title { margin: 0 0 0.6rem; font-family: var(--font-display); font-size: 1.35rem; }
.info-modal-def { margin: 0 0 0.7rem; line-height: 1.5; font-size: 0.92rem; }
.info-modal-wiki { color: var(--accent); font-size: 0.85rem; }
.info-modal-divider {
  margin: 1.1rem 0 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.6rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
.info-modal-basketball { margin: 0; line-height: 1.5; font-size: 0.92rem; }

/* ── Plays panel (pair mode) ───────────────────────────────────── */

.plays-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  width: var(--plays-w);
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--rule);
  background: color-mix(in srgb, var(--bg-elev) 96%, transparent);
  box-shadow: var(--shadow-card);
  transform: translateX(100%);
  transition: transform 0.6s cubic-bezier(0.2, 0.7, 0.2, 1);
}
.plays-panel.open { transform: translateX(0); }
.plays-panel-header { padding: 1.1rem 1.5rem 0.9rem; border-bottom: 1px solid var(--rule); }
.plays-panel-back {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0 0 0.7rem;
  padding: 0.25rem 0.65rem;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg);
  font-family: var(--font-mono);
  font-size: 0.64rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}
.plays-panel-back:hover { border-color: var(--fg); }
.plays-panel-title {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.15rem;
  line-height: 1.2;
}
.plays-panel-heads { display: inline-flex; flex: none; }
.plays-panel-heads .headshot + .headshot { margin-left: -8px; }
.plays-panel-amp { color: var(--accent); }
.plays-panel-sub {
  margin: 0.45rem 0 0;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.plays-panel-list { flex: 1; overflow-y: auto; padding: 0.6rem 1rem 1rem; scrollbar-width: thin; }
.plays-panel-empty { padding: 1rem 0.5rem; color: var(--muted); font-size: 0.9rem; }
.play-group { margin-bottom: 0.9rem; }
.play-group-head {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0.5rem 0.3rem;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.play-group-head a { color: inherit; text-decoration: none; }
.play-group-head a:hover { color: var(--fg); text-decoration: underline; text-underline-offset: 3px; }
.play-group-count {
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--bg-elev-2);
  color: var(--fg);
}
.play-link {
  display: block;
  padding: 0.45rem 0.5rem;
  border-radius: 8px;
  color: var(--fg);
  text-decoration: none;
  font-size: 0.86rem;
  line-height: 1.35;
}
.play-link:hover { background: var(--bg-elev-2); }
.play-link-meta {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.58rem;
  letter-spacing: 0.1em;
  color: var(--muted);
}
.plays-panel-footer {
  padding: 0.7rem 1.5rem;
  border-top: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 0.56rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

body.pair-mode .stats, body.pair-mode .meta { right: calc(var(--plays-w) + 2rem); }

/* ── Mobile ────────────────────────────────────────────────────── */

@media (max-width: 720px) {
  :root { --plays-w: 100vw; }
  .masthead { left: 1rem; top: 1rem; max-width: 70vw; }
  .masthead-team { font-size: 1.5rem; }
  .stats { top: auto; bottom: 6.2rem; right: 1rem; gap: 1rem; }
  .stat-value { font-size: 1.15rem; }
  .rail { left: 1rem; bottom: 1.2rem; }
  .team-button { min-width: 11rem; }
  .meta { display: none; }
  .sidebar {
    left: 1rem;
    right: 1rem;
    width: auto;
    top: auto !important;
    bottom: 5.4rem;
    max-height: 50vh;
    display: none;
  }
  body.sidebar-open .sidebar { display: block; }
  .sidebar-toggle {
    display: block;
    position: fixed;
    right: 1rem;
    bottom: 1.4rem;
    z-index: 5;
    padding: 0.5rem 0.9rem;
    border: 1px solid var(--rule);
    border-radius: 999px;
    background: var(--bg-elev);
    font-family: var(--font-mono);
    font-size: 0.66rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    cursor: pointer;
  }
  /* Pair mode on phones: the plays list becomes a bottom sheet so the
     two players (and the masthead Back button) stay visible above it. */
  .plays-panel {
    top: auto;
    height: 58vh;
    border-left: 0;
    border-top: 1px solid var(--rule);
    border-radius: 18px 18px 0 0;
    transform: translateY(100%);
  }
  .plays-panel.open { transform: translateY(0); }
  .plays-panel-header { padding: 0.9rem 1rem 0.7rem; }
  .plays-panel-title { font-size: 1rem; }
  .plays-panel-list { padding: 0.4rem 0.6rem 0.8rem; }
  .plays-panel-footer { display: none; }
  body.pair-mode .rail, body.pair-mode .sidebar-toggle, body.pair-mode .sidebar { display: none; }
  .team-overlay { padding: 4rem 1rem 2rem; }
  .team-overlay-grid { grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
  .hint { bottom: auto; top: 6.5rem; max-width: calc(100vw - 2rem); }
  body.pair-mode .stats { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .plays-panel, .hint, .label, .sidebar-content { transition: none; }
}
