:root {
  --bg: #0f1620;
  --panel: #1a2533;
  --panel-light: #233044;
  --accent: #4fd1c5;
  --accent-dark: #2ca39a;
  --text: #eef3f8;
  --text-dim: #9fb0c2;
  --muted: #7c8da0;
  --correct: #4ade80;
  --incorrect: #f87171;
  --land: #46596f;
  --land-context: #1c2531;
  --land-stroke: #1a2533;
  --highlight: #f6ad55;
  --ocean: #101820;

  /* the two halves of the split */
  --side-a: #4fd1c5;
  --side-b: #f6ad55;

  --diff-1: #4ade80;
  --diff-2: #facc15;
  --diff-3: #fb923c;
  --diff-4: #f87171;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
}

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

h1 {
  text-align: center;
  font-size: 2.2rem;
  margin-bottom: 4px;
  letter-spacing: 1px;
}

.subtitle {
  text-align: center;
  color: var(--text-dim);
  margin-top: 0;
  margin-bottom: 28px;
}

/* ---------- Setup ---------- */

.setup-group { margin-bottom: 24px; }

.setup-group h2 {
  font-size: 1.05rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  margin-bottom: 10px;
  font-weight: 600;
}

.setup-group h2 .hint {
  text-transform: none;
  letter-spacing: normal;
  font-weight: 400;
  font-size: 0.85rem;
  opacity: 0.8;
}

.options-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.option-btn {
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 14px 16px;
  text-align: left;
  cursor: pointer;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.option-btn:hover {
  background: var(--panel-light);
  transform: translateY(-1px);
}

.option-btn.selected {
  border-color: var(--accent);
  background: var(--panel-light);
}

#difficulty-options .option-btn[data-difficulty="1"] { border-color: color-mix(in srgb, var(--diff-1) 35%, transparent); }
#difficulty-options .option-btn[data-difficulty="2"] { border-color: color-mix(in srgb, var(--diff-2) 35%, transparent); }
#difficulty-options .option-btn[data-difficulty="3"] { border-color: color-mix(in srgb, var(--diff-3) 35%, transparent); }
#difficulty-options .option-btn[data-difficulty="4"] { border-color: color-mix(in srgb, var(--diff-4) 35%, transparent); }

#difficulty-options .option-btn[data-difficulty="1"].selected { border-color: var(--diff-1); box-shadow: 0 0 0 1px var(--diff-1) inset; }
#difficulty-options .option-btn[data-difficulty="2"].selected { border-color: var(--diff-2); box-shadow: 0 0 0 1px var(--diff-2) inset; }
#difficulty-options .option-btn[data-difficulty="3"].selected { border-color: var(--diff-3); box-shadow: 0 0 0 1px var(--diff-3) inset; }
#difficulty-options .option-btn[data-difficulty="4"].selected { border-color: var(--diff-4); box-shadow: 0 0 0 1px var(--diff-4) inset; }

.opt-title { font-weight: 700; font-size: 1.05rem; }
.opt-desc { font-size: 0.82rem; color: var(--text-dim); }

.primary-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 14px;
  font-size: 1.1rem;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #06231f;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.primary-btn:hover:not(:disabled) { background: var(--accent-dark); }
.primary-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.primary-btn.hidden { display: none; }

.back-link { text-align: center; margin-top: 16px; font-size: 0.9rem; }
.back-link a { color: var(--text-dim); text-decoration: none; }
.back-link a:hover { color: var(--accent); }

.how-to {
  border: 1px solid var(--panel-light);
  border-radius: 8px;
  margin-top: 20px;
  background: var(--panel);
  max-width: 520px;
  width: 100%;
  text-align: left;
}

.how-to-summary {
  display: block;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.how-to-summary::-webkit-details-marker { display: none; }
.how-to-summary::before {
  content: "\25B8";
  display: inline-block;
  margin-right: 8px;
  transition: transform 0.2s;
}
details.how-to[open] > .how-to-summary::before { transform: rotate(90deg); }
.how-to-summary:hover { color: var(--text); }

.how-to-body { padding: 0 16px 12px; }
.how-to-body p {
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.55;
  margin: 0 0 10px;
}
.how-to-body p:last-child { margin-bottom: 0; }
.how-to-body strong { color: var(--text); }

.text-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 4px 6px;
}
.text-btn:hover { color: var(--text); }

.hud-toggle {
  background: var(--panel);
  border: 1px solid var(--panel-light);
  border-radius: 6px;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 5px 10px;
  transition: border-color 0.15s, color 0.15s;
}
.hud-toggle:hover { border-color: var(--accent); color: var(--text); }
.hud-toggle.active { border-color: var(--accent); color: var(--accent); }

/* ---------- How it works ---------- */

.filter-dropdown {
  border: 1px solid var(--panel-light);
  border-radius: 8px;
  margin-top: 20px;
  background: var(--panel);
}

.filter-summary {
  display: block;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.filter-summary::-webkit-details-marker { display: none; }
.filter-summary::before {
  content: "\25B8";
  display: inline-block;
  margin-right: 8px;
  transition: transform 0.2s;
}
details.filter-dropdown[open] > .filter-summary::before { transform: rotate(90deg); }
.filter-summary:hover { color: var(--text); }

.filter-body { padding: 0 16px 12px; }
.filter-body p {
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.55;
  margin: 0 0 10px;
}
.filter-body p.src-note { font-size: 0.78rem; opacity: 0.7; margin-bottom: 0; }
.filter-body strong { color: var(--text); }

/* ---------- Game screen ---------- */

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.hud {
  display: flex;
  gap: 18px;
  font-weight: 600;
  color: var(--text-dim);
}

.hud-right { display: flex; align-items: center; gap: 12px; }
#score-counter { color: var(--accent); }

#question-text {
  text-align: center;
  font-size: 1.15rem;
  font-weight: 600;
  margin: 4px 0 2px;
}

#region-name { color: var(--accent); }

.region-sub {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
  margin: 0 0 10px;
}

.map-wrap {
  position: relative;
  background: var(--ocean);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid #233044;

  /* The map owns every drag that starts inside it. Without this, dragging
     vertically — the only way to move a horizontal line — scrolls the page
     instead, and a mouse drag starts a text selection that auto-scrolls near
     the edges. touch-action is not inherited, so the SVG's children need it
     spelled out too; setting it on the <svg> alone is not enough. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#map-svg {
  display: block;
  width: 100%;
  height: 56vh;
  max-height: 560px;
  touch-action: none;
  cursor: crosshair;
}

#map-svg * { touch-action: none; }

/* Locked angle: the whole map slides the line, so the cursor says "grab". */
#map-svg.locked { cursor: grab; }
#map-svg.locked:active { cursor: grabbing; }
#map-svg.revealed,
#map-svg.locked.revealed { cursor: default; }

.map-hint {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 22, 32, 0.85);
  border: 1px solid var(--panel-light);
  color: var(--text-dim);
  padding: 5px 14px;
  border-radius: 20px;
  font-size: 0.8rem;
  pointer-events: none;
  transition: opacity 0.25s;
}
.map-hint.hidden { opacity: 0; }

/* ---------- Map layers ---------- */

.country-context {
  fill: var(--land-context);
  stroke: #141c26;
  stroke-width: 0.4;
}

.country-region {
  fill: var(--land);
  stroke: #5c7089;
  stroke-width: 0.5;
}

.region-outline {
  fill: none;
  stroke: #8ea4bd;
  stroke-width: 1.1;
  pointer-events: none;
}

.country-label {
  fill: var(--text-dim);
  font-size: 10px;
  text-anchor: middle;
  pointer-events: none;
  paint-order: stroke;
  stroke: rgba(15, 22, 32, 0.85);
  stroke-width: 2.5px;
  stroke-linejoin: round;
  opacity: 0.85;
}

.half-wash { pointer-events: none; }
.half-wash.a { fill: var(--side-a); fill-opacity: 0.06; }
.half-wash.b { fill: var(--side-b); fill-opacity: 0.06; }

.split-line {
  stroke: #ffffff;
  stroke-width: 2.5;
  stroke-linecap: round;
  pointer-events: none;
  filter: drop-shadow(0 0 3px rgba(0, 0, 0, 0.9));
}

.split-line-hit {
  stroke: transparent;
  stroke-width: 24;
  cursor: move;
}

.optimal-line {
  stroke: #ffffff;
  stroke-width: 2;
  stroke-dasharray: 8 6;
  opacity: 0.75;
  pointer-events: none;
}

.line-handle {
  fill: #ffffff;
  stroke: #0f1620;
  stroke-width: 2;
  cursor: grab;
}
.line-handle:active { cursor: grabbing; }

/* Once the grid is revealed the land recedes so the population reads as the
   figure rather than competing with the basemap. */
#map-svg.revealed .country-region { fill: #2c3847; stroke: #3d4c5e; }
#map-svg.revealed .country-context { fill: #161d27; }
#map-svg.revealed .region-outline { stroke: #61748c; }

.pop-dot { pointer-events: none; }
.pop-dot.a { fill: var(--side-a); }
.pop-dot.b { fill: var(--side-b); }

.side-badge {
  font-size: 13px;
  font-weight: 700;
  paint-order: stroke;
  stroke: rgba(15, 22, 32, 0.9);
  stroke-width: 3.5px;
  stroke-linejoin: round;
  pointer-events: none;
}
.side-badge.a { fill: var(--side-a); }
.side-badge.b { fill: var(--side-b); }

/* ---------- Result panel ---------- */

.result-panel {
  margin-top: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-light);
  border-radius: 10px;
  padding: 12px 16px;
}
.result-panel.hidden { display: none; }

.result-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.result-grade { display: flex; align-items: baseline; gap: 12px; }
.grade-text { font-size: 1.2rem; font-weight: 700; }
.round-score { font-size: 1.2rem; font-weight: 700; color: var(--accent); }
.result-detail { color: var(--text-dim); font-size: 0.88rem; }

.split-bar {
  display: flex;
  height: 30px;
  border-radius: 6px;
  overflow: hidden;
  background: var(--panel-light);
}

.split-half { transition: width 0.6s ease; }
.split-a { background: var(--side-a); }
.split-b { background: var(--side-b); }

/* Labels live outside the bar: a 3% sliver cannot hold its own caption. */
.split-legend {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 7px;
  font-size: 0.82rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.legend-item::before {
  content: "";
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  margin-right: 7px;
  vertical-align: baseline;
}
.legend-item.a { color: var(--side-a); }
.legend-item.b { color: var(--side-b); }
.legend-item.a::before { background: var(--side-a); }
.legend-item.b::before { background: var(--side-b); }

.action-row { margin-top: 12px; }

/* ---------- Duels ---------- */

.option-btn-toggle {
  width: 100%;
  text-align: left;
  font-weight: 700;
}

/* Each player brings their own difficulty, so the shared selector would be a
   lie once duels is on. */
body.duels-mode #difficulty-setup-group { display: none; }

.duel-hud {
  display: flex;
  align-items: center;
  gap: 12px;
}
.duel-hud.hidden { display: none; }

.duel-player-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 3px 12px;
  border: 1.5px solid var(--panel-light);
  border-radius: 8px;
  opacity: 0.5;
  transition: border-color 0.15s, opacity 0.15s;
  min-width: 80px;
}
.duel-player-chip.active { border-color: var(--accent); opacity: 1; }

.duel-player-name {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.duel-player-score {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--text);
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.duel-vs { font-size: 0.85rem; color: var(--text-dim); }

#score-counter.hidden { display: none; }

/* ---------- Modals ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}
.modal-overlay.hidden { display: none; }

.modal-box {
  background: var(--panel);
  border: 1px solid var(--panel-light);
  border-radius: 12px;
  padding: 1.75rem;
  max-width: 440px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-box h2 { margin: 0 0 1rem; font-size: 1.3rem; }

.modal-box ul {
  padding-left: 1.25rem;
  margin: 0 0 1.5rem;
  line-height: 1.8;
  color: var(--text-dim);
}
.modal-box ul strong { color: var(--text); }

.modal-label {
  margin: 1rem 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
}
.modal-label span { font-weight: 400; }

/* The modal shares the panel colour, so these need their own ground to read as
   buttons at all. Tier colours match the main difficulty selector. */
.modal-box .options-grid .option-btn {
  padding: 10px 14px;
  background: var(--bg);
  align-items: center;
  text-align: center;
}
.modal-box .options-grid .option-btn:hover { background: var(--panel-light); }

#player-setup-difficulties .option-btn[data-setup-difficulty="1"] { border-color: color-mix(in srgb, var(--diff-1) 40%, transparent); }
#player-setup-difficulties .option-btn[data-setup-difficulty="2"] { border-color: color-mix(in srgb, var(--diff-2) 40%, transparent); }
#player-setup-difficulties .option-btn[data-setup-difficulty="3"] { border-color: color-mix(in srgb, var(--diff-3) 40%, transparent); }
#player-setup-difficulties .option-btn[data-setup-difficulty="4"] { border-color: color-mix(in srgb, var(--diff-4) 40%, transparent); }

#player-setup-difficulties .option-btn[data-setup-difficulty="1"].selected { border-color: var(--diff-1); box-shadow: 0 0 0 1px var(--diff-1) inset; }
#player-setup-difficulties .option-btn[data-setup-difficulty="2"].selected { border-color: var(--diff-2); box-shadow: 0 0 0 1px var(--diff-2) inset; }
#player-setup-difficulties .option-btn[data-setup-difficulty="3"].selected { border-color: var(--diff-3); box-shadow: 0 0 0 1px var(--diff-3) inset; }
#player-setup-difficulties .option-btn[data-setup-difficulty="4"].selected { border-color: var(--diff-4); box-shadow: 0 0 0 1px var(--diff-4) inset; }

.player-name-input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--panel-light);
  border-radius: 6px;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
.player-name-input:focus { outline: none; border-color: var(--accent); }

#player-setup-next-btn { margin-top: 1.25rem; }

/* ---------- Results screen ---------- */

.final-score {
  text-align: center;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 18px;
}

.round-breakdown {
  max-width: 520px;
  margin: 0 auto 22px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.breakdown-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.9rem;
}
.breakdown-row .bd-name { font-weight: 600; }
.breakdown-row .bd-split { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.breakdown-row .bd-score { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Loading ---------- */

#loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-dim);
  z-index: 100;
}
#loading-overlay.hidden { display: none; }

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--panel-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes dotIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@media (max-width: 600px) {
  .game-header { flex-wrap: wrap; gap: 8px; }
  .duel-hud { order: 3; width: 100%; justify-content: center; }
  .duel-player-chip { min-width: 0; flex: 1; }
  .hud { gap: 12px; font-size: 0.85rem; }
  .hud-toggle { font-size: 0.75rem; padding: 4px 8px; }
  .options-grid { grid-template-columns: 1fr; }
  h1 { font-size: 1.6rem; }
  #question-text { font-size: 1rem; }
  #map-svg { height: 46vh; }
  .result-top { gap: 6px; }
}
