/* Prisoner's Dilemma Tournament: one page, plain CSS, light and dark. */

:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f1efe9;
  --border: #e3e0d7;
  --text: #1d1c19;
  --muted: #6b675d;
  --accent: #3b3fd8;
  --accent-ink: #ffffff;
  --coop: #0f8a6a;
  --coop-soft: #dcf3ea;
  --defect: #d2432f;
  --defect-soft: #fbe3de;
  --gold: #b7861b;
  --shadow: 0 1px 2px rgb(20 18 10 / 0.05), 0 8px 24px rgb(20 18 10 / 0.06);
  --radius: 14px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --surface: #1a1b1e;
    --surface-2: #232428;
    --border: #2e3035;
    --text: #ecebe6;
    --muted: #9c9a93;
    --accent: #8b8ff7;
    --accent-ink: #121330;
    --coop: #3fcf9f;
    --coop-soft: #133a2f;
    --defect: #f26d58;
    --defect-soft: #45201a;
    --gold: #e0b44c;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px rgb(0 0 0 / 0.25);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---- Hero ---------------------------------------------------------------- */

.hero {
  max-width: 1240px;
  margin: 0 auto;
  padding: 48px 24px 24px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.hero h1 {
  margin: 0;
  font-size: clamp(30px, 5vw, 46px);
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.lede {
  max-width: 68ch;
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 17px;
}

.move-word { font-weight: 600; }
.move-word--c { color: var(--coop); }
.move-word--d { color: var(--defect); }

/* ---- Layout -------------------------------------------------------------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  max-width: 1240px;
  margin: 0 auto;
  padding: 8px 24px 32px;
}

@media (min-width: 980px) {
  .layout { grid-template-columns: 380px minmax(0, 1fr); align-items: start; }
  .setup { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; padding-bottom: 4px; }
}

.setup, .results { display: grid; gap: 16px; min-width: 0; }

/* On a narrow screen the results (already run on arrival) come first. */
@media (max-width: 979px) {
  .results { order: -1; }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  min-width: 0;
}

.panel__head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
}

.panel__title { margin: 0; font-size: 16px; letter-spacing: -0.01em; }
#rules-heading { margin-bottom: 12px; }
.panel__hint { margin: 4px 0 12px; color: var(--muted); font-size: 13px; }
.panel__meta { color: var(--muted); font-size: 13px; }
.panel--results .panel__head { margin-bottom: 12px; }

/* ---- Entrants ------------------------------------------------------------ */

.entrants { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }

.entrant {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color 120ms, background 120ms;
}

.entrant:hover { border-color: var(--accent); }
.entrant:has(:checked) { background: var(--surface-2); border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.entrant__check { margin-top: 3px; accent-color: var(--accent); width: 16px; height: 16px; flex: none; }
.entrant__body { display: grid; }
.entrant__name { font-weight: 600; }
.entrant__blurb { color: var(--muted); font-size: 13px; }

/* ---- Your fighter -------------------------------------------------------- */

.panel--fighter { border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.panel--fighter.is-off .field,
.panel--fighter.is-off .rules,
.panel--fighter.is-off .presets { opacity: 0.45; }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; font-weight: 600; }
.switch__input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch__track {
  position: relative;
  width: 34px; height: 20px;
  border-radius: 999px;
  background: var(--border);
  transition: background 150ms;
}
.switch__track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  transition: transform 150ms;
}
.switch__input:checked + .switch__track { background: var(--accent); }
.switch__input:checked + .switch__track::after { transform: translateX(14px); }
.switch__input:focus-visible + .switch__track { outline: 2px solid var(--accent); outline-offset: 2px; }

.rules { display: grid; gap: 8px; margin: 12px 0; }

.rule {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.rule__label { float: left; padding: 0; font-size: 13px; }
.rule[data-rule="first"] .rule__label { font-weight: 600; }

.segmented {
  display: inline-flex;
  flex: none;
  padding: 2px;
  border-radius: 9px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.segmented__input { position: absolute; opacity: 0; width: 1px; height: 1px; }

.segmented__option {
  padding: 4px 10px;
  border-radius: 7px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background 120ms, color 120ms;
}

.segmented__input:checked + .segmented__option--c { background: var(--coop); color: #fff; }
.segmented__input:checked + .segmented__option--d { background: var(--defect); color: #fff; }
.segmented__input:focus-visible + .segmented__option { outline: 2px solid var(--accent); outline-offset: 1px; }

.presets { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.presets__label { color: var(--muted); font-size: 12px; margin-right: 2px; }

.chip {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---- Fields -------------------------------------------------------------- */

.field { margin: 0 0 14px; padding: 0; border: 0; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field__label { display: block; margin: 0 0 6px; padding: 0; font-size: 13px; font-weight: 600; }

.input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; border-color: var(--accent); }
.input--number { width: 84px; font-family: var(--mono); text-align: right; }
.input--seed { width: 120px; }

.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input[type="range"] { flex: 1; accent-color: var(--accent); }

.payoff__grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.payoff__cell {
  display: grid;
  gap: 4px;
  padding: 8px;
  border-radius: 10px;
  background: var(--surface-2);
}
.payoff__label { font-size: 12px; color: var(--muted); line-height: 1.3; }
.payoff__label abbr { text-decoration: none; font-weight: 700; color: var(--text); }
.payoff__cell .input--number { width: 100%; }

.warning { margin: 8px 0 0; color: var(--gold); font-size: 13px; }
.error {
  margin: 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--defect-soft);
  color: var(--defect);
  font-weight: 600;
}

/* ---- Buttons ------------------------------------------------------------- */

.button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.button:hover { border-color: var(--accent); }
.button:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.button--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.button--primary:hover { filter: brightness(1.08); }
.button--run { width: 100%; padding: 12px; font-size: 16px; }

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

.status { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -0.01em; min-height: 1.5em; }

.table-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table thead th { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.table tbody tr:last-child > * { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-size: 13px; }
.strong { font-weight: 700; }

.leaderboard__name { font-weight: 600; }
.leaderboard .is-leader { background: color-mix(in srgb, var(--gold) 10%, transparent); }

.rank-badge {
  display: inline-grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--muted);
}
.rank-badge--1 { background: var(--gold); color: #fff; }
.rank-badge--2 { background: color-mix(in srgb, var(--muted) 45%, var(--surface)); color: var(--text); }
.rank-badge--3 { background: color-mix(in srgb, #a0652f 55%, var(--surface)); color: var(--text); }

.tag {
  margin-left: 8px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  vertical-align: 2px;
}

.coop { display: flex; align-items: center; gap: 8px; }
.coop__pct { font-family: var(--mono); font-size: 12px; color: var(--muted); width: 3.5ch; text-align: right; }
.meter { position: relative; display: block; width: 90px; height: 6px; border-radius: 999px; background: var(--defect-soft); overflow: hidden; }
.meter__fill { position: absolute; inset: 0 auto 0 0; width: calc(var(--fill, 0) * 100%); background: var(--coop); border-radius: inherit; }

/* Head-to-head grid */
.grid th, .grid td { padding: 3px; border: 0; text-align: center; }
.grid__corner { width: 1%; }
.grid__col { font-family: var(--mono); font-size: 11px !important; }
.grid__row { text-align: right !important; padding-right: 10px !important; font-weight: 600; font-size: 13px; }
.grid__self { background: repeating-linear-gradient(135deg, var(--surface-2) 0 4px, transparent 4px 8px); border-radius: 8px; }
.grid__cell {
  width: 100%;
  min-width: 52px;
  padding: 8px 4px;
  border: 2px solid transparent;
  border-radius: 8px;
  background: color-mix(in srgb, var(--coop) calc(var(--share, 0.5) * 70%), var(--defect-soft));
  color: var(--text);
  font: 600 13px var(--mono);
  cursor: pointer;
}
.grid__cell:hover { border-color: var(--text); }
.grid__cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.grid__cell.is-selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

/* Match */
.match-pick { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.match-pick .field__label { margin: 0; }
.match-pick .input { width: auto; flex: 1 1 160px; }
.match-pick__vs { color: var(--muted); font-weight: 600; }

.match-score { margin: 0 0 12px; font-size: 16px; }
.match-score__note { color: var(--muted); font-size: 14px; }

.strip { display: grid; gap: 3px; margin-bottom: 14px; }
.strip__row {
  display: grid;
  grid-template-columns: repeat(var(--rounds, 1), minmax(0, 1fr));
  /* A fixed 1px gap sets a floor of rounds - 1 px, which pushed the page
     sideways past ~350 rounds on a phone. A share of the width has no floor. */
  gap: min(1px, 100% / var(--rounds, 1) / 2);
  height: 14px;
}
.pip { border-radius: 2px; }
.pip--c { background: var(--coop); }
.pip--d { background: var(--defect); }

.table-wrap--rounds { max-height: 420px; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; margin: 0; padding: 0; }
.rounds thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; box-shadow: inset 0 -1px 0 var(--border); }

.move { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.move--c { background: var(--coop-soft); color: var(--coop); }
.move--d { background: var(--defect-soft); color: var(--defect); }

/* ---- Footer -------------------------------------------------------------- */

.footer { max-width: 1240px; margin: 0 auto; padding: 8px 24px 40px; color: var(--muted); font-size: 13px; }

@media (max-width: 520px) {
  .hero { padding: 32px 16px 16px; }
  .layout { padding: 8px 16px 24px; }
  .footer { padding: 8px 16px 32px; }
  .payoff__grid { grid-template-columns: 1fr; }
  .meter { width: 48px; }
}

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