:root {
  --bg: #f3f3f5;
  --surface: #ffffff;
  --text: #1a1a1f;
  --muted: #6b6b76;
  --border: #dcdce2;
  --btn-bg: #1a1a1f;
  --btn-text: #ffffff;
  --mine: #fff3c4;
  --focus: #2b6cff;
  --red: #e5484d;
  --green: #2f9e63;
  --blue: #3e63dd;
  --gold: #e8b923;
  --silver: #b4b8c2;
  --bronze: #c98a52;
  --radius: 16px;
  --thumb: 24px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111114;
    --surface: #1b1b20;
    --text: #f2f2f5;
    --muted: #9a9aa6;
    --border: #34343c;
    --btn-bg: #f2f2f5;
    --btn-text: #111114;
    --mine: #4a3f12;
    --focus: #7aa2ff;
    --red: #ff6b70;
    --green: #4cc38a;
    --blue: #7b98ff;
  }
}

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

[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px 48px;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* ---------- Header ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 0 16px;
}

.logo {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.lr { color: var(--red); }
.lg { color: var(--green); }
.lb { color: var(--blue); }

.hud {
  display: flex;
  gap: 18px;
  font-variant-numeric: tabular-nums;
}

.hud strong {
  font-size: 1.1rem;
}

.hud-label {
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-right: 2px;
}

.hud-of {
  color: var(--muted);
}

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

.btn {
  font: inherit;
  font-weight: 650;
  min-height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}

.btn.primary {
  background: var(--btn-bg);
  border-color: var(--btn-bg);
  color: var(--btn-text);
}

.btn.big {
  display: block;
  width: 100%;
  min-height: 54px;
  font-size: 1.08rem;
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

@media (hover: hover) {
  .btn:not(:disabled):hover {
    filter: brightness(1.12);
  }
}

/* ---------- Start screen ---------- */

.chips {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  height: 84px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}

.lede {
  font-size: 1.08rem;
  margin: 20px 0 10px;
}

.tr { color: var(--red); }
.tg { color: var(--green); }
.tb { color: var(--blue); }

.how {
  margin: 0 0 22px;
  color: var(--muted);
}

.how summary {
  cursor: pointer;
  font-weight: 600;
}

.how p {
  margin: 8px 0 0;
}

/* ---------- Play screen ---------- */

.swatch-pair {
  display: flex;
  height: clamp(170px, 32vh, 300px);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface);
}

.swatch {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  transition: flex-grow 0.45s ease;
}

.swatch.guess {
  flex-grow: 0;
}

.swatch-pair.revealed .swatch.guess {
  flex-grow: 1;
}

.swatch-label {
  position: absolute;
  left: 14px;
  bottom: 12px;
  font-size: 0.85rem;
  line-height: 1.35;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  opacity: 0;
  transition: opacity 0.3s ease 0.2s;
}

.swatch-label b {
  display: block;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.swatch-pair.revealed .swatch-label {
  opacity: 1;
}

.controls {
  margin-top: 18px;
}

.channel,
.channel-head {
  display: grid;
  grid-template-columns: 1.4rem minmax(0, 1fr) 4.4rem 4.4rem;
  gap: 12px;
  align-items: center;
}

.channel + .channel {
  margin-top: 10px;
}

.channel-head {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  text-align: center;
  margin-bottom: 4px;
}

.channel[data-channel="r"] { --ch: var(--red); --track: linear-gradient(to right, #000, #f00); }
.channel[data-channel="g"] { --ch: var(--green); --track: linear-gradient(to right, #000, #0f0); }
.channel[data-channel="b"] { --ch: var(--blue); --track: linear-gradient(to right, #000, #00f); }

.ch-name {
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--ch);
  text-align: center;
}

.track {
  position: relative;
  height: 44px;
  display: flex;
  align-items: center;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 44px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  touch-action: pan-y;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 12px;
  border-radius: 6px;
  background: var(--track);
  border: 1px solid var(--border);
}

input[type="range"]::-moz-range-track {
  height: 12px;
  border-radius: 6px;
  background: var(--track);
  border: 1px solid var(--border);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: var(--thumb);
  height: var(--thumb);
  margin-top: calc((12px - var(--thumb)) / 2);
  box-sizing: border-box;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #1a1a1f;
  box-shadow: 0 1px 4px rgb(0 0 0 / 0.35);
}

input[type="range"]::-moz-range-thumb {
  box-sizing: border-box;
  width: var(--thumb);
  height: var(--thumb);
  border-radius: 50%;
  background: #fff;
  border: 2px solid #1a1a1f;
  box-shadow: 0 1px 4px rgb(0 0 0 / 0.35);
}

input[type="range"]:disabled {
  cursor: default;
}

/* Points down at the real value, centred where the thumb's centre would be. */
.marker {
  position: absolute;
  top: 0;
  left: calc(var(--thumb) / 2 + (100% - var(--thumb)) * var(--pos, 0));
  width: 0;
  height: 0;
  transform: translateX(-50%);
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 9px solid var(--text);
  pointer-events: none;
}

.num {
  width: 100%;
  height: 44px;
  padding: 0 4px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 650;
  text-align: center;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
  appearance: textfield;
}

.num::-webkit-inner-spin-button,
.num::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.num:disabled {
  opacity: 1;
  color: var(--text);
  background: transparent;
}

.actual {
  height: 44px;
  border-radius: 10px;
  border: 1.5px dashed var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-weight: 650;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.actual.shown {
  border-style: solid;
  border-color: var(--ch);
  color: var(--text);
}

.actual .val {
  font-size: 1.05rem;
}

.actual .diff {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
}

.result-line {
  min-height: 2.3em;
  margin: 16px 0 12px;
  text-align: center;
  font-size: 1.05rem;
  color: var(--muted);
}

.result-line .pts {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.result-line .grade {
  font-weight: 700;
  color: var(--text);
}

.hint {
  margin: 12px 0 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
}

@media (hover: none) {
  .hint {
    display: none;
  }
}

/* ---------- End screen ---------- */

.final-label {
  margin: 8px 0 0;
  text-align: center;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.8rem;
}

.final-score {
  margin: 0 0 16px;
  text-align: center;
  font-size: 3.4rem;
  font-weight: 800;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.final-of {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--muted);
}

.recap {
  list-style: none;
  margin: 0 0 22px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(var(--rounds, 5), minmax(0, 1fr));
  gap: 8px;
}

.recap li {
  text-align: center;
  font-size: 0.85rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.recap .pair {
  display: flex;
  height: 46px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  margin-bottom: 4px;
}

.recap .pair span {
  flex: 1;
}

.name-form label {
  display: block;
  font-weight: 650;
  margin-bottom: 6px;
}

.name-row {
  display: flex;
  gap: 8px;
}

.name-row input {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}

.name-row .btn {
  min-height: 48px;
  min-width: 88px;
}

.form-msg {
  min-height: 1.5em;
  margin: 8px 0 0;
  font-size: 0.92rem;
  color: var(--muted);
}

.form-msg.error {
  color: var(--red);
}

.end-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

/* ---------- Leaderboard ---------- */

.leaderboard {
  margin-top: 28px;
  padding: 18px 18px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}

.leaderboard h2 {
  margin: 0 0 10px;
  font-size: 1.1rem;
}

.board-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.board-list li {
  display: grid;
  grid-template-columns: 2rem minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  padding: 8px 6px;
  border-radius: 10px;
  font-variant-numeric: tabular-nums;
}

.board-list li + li {
  border-top: 1px solid var(--border);
}

.board-list li.mine {
  background: var(--mine);
  border-top-color: transparent;
}

.board-list li.mine + li {
  border-top-color: transparent;
}

.board-list li.gap {
  display: block;
  padding: 2px 6px;
  text-align: center;
  color: var(--muted);
}

.rank {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 50%;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
}

.rank.r1 { background: var(--gold); color: #1a1a1f; }
.rank.r2 { background: var(--silver); color: #1a1a1f; }
.rank.r3 { background: var(--bronze); color: #1a1a1f; }

.who {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.pts-cell {
  font-weight: 750;
}

.when {
  color: var(--muted);
  font-size: 0.82rem;
  min-width: 4.5rem;
  text-align: right;
}

.board-empty,
.board-note {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

@media (max-width: 380px) {
  .when {
    display: none;
  }

  .board-list li {
    grid-template-columns: 2rem minmax(0, 1fr) auto;
  }

  .channel,
  .channel-head {
    grid-template-columns: 1.2rem minmax(0, 1fr) 3.9rem 3.9rem;
    gap: 9px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .swatch,
  .swatch-label {
    transition: none;
  }
}
