:root {
  --frame-blue: #1e5fa8;
  --frame-blue-dark: #123a68;
  --bg: #05070c;
  --panel: #131722;
  --panel-2: #1b2030;
  --line: #2a3145;
  --text: #eceef5;
  --muted: #9aa0b4;
  --accent: #2ecc71;
  --accent-ink: #06210f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: #eee;
  font-family: 'Segoe UI', Arial, sans-serif;
}

[hidden] { display: none !important; }

#game { padding: 16px; }

#game-layout {
  max-width: 1720px;
  margin: 0 auto;
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

#game-main { flex: 1 1 0; min-width: 0; }

#stack {
  display: flex;
  flex-direction: column;
  width: 100%;
}

#game-frame {
  position: relative;
  background: #000;
  border: 6px solid var(--frame-blue);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  overflow: hidden;
  line-height: 0;
}

#field {
  width: 100%;
  height: auto;
  display: block;
  background: #000;
}

#leg-title {
  position: absolute;
  top: 16px;
  right: 26px;
  font-size: clamp(14px, 1.8vw, 26px);
  font-weight: 800;
  color: #f0f0f0;
  text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  letter-spacing: 1px;
  z-index: 5;
  text-align: right;
  line-height: 1.2;
  pointer-events: none;
}
#leg-time {
  font-size: 0.7em;
  font-weight: 600;
  opacity: 0.8;
  font-variant-numeric: tabular-nums;
}

#controls {
  position: absolute;
  top: 10px;
  left: 16px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  z-index: 5;
  line-height: 1;
  max-width: 70%;
}

#controls button {
  background: rgba(20, 30, 50, 0.7);
  color: #eee;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 4px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 14px;
}
#controls button:hover { background: rgba(40, 60, 100, 0.85); }

#controls .seg { display: inline-flex; border: 1px solid rgba(255,255,255,0.3); border-radius: 4px; overflow: hidden; }
#controls .seg button { border: 0; border-radius: 0; background: rgba(20, 30, 50, 0.7); padding: 6px 9px; font-size: 13px; }
#controls .seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
#controls input[type=range] { width: 80px; accent-color: var(--accent); }

.clock {
  background: rgba(20, 30, 50, 0.7);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #eee;
}

#pause-badge {
  position: absolute;
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.75);
  padding: 6px 18px;
  border-radius: 999px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 14px;
  z-index: 6;
  line-height: 1.4;
}

#winner-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  z-index: 10;
  line-height: 1.3;
}
#winner-overlay.hidden { display: none; }

#winner-card {
  text-align: center;
  padding: clamp(16px, 3vw, 40px) clamp(20px, 5vw, 72px);
  border-radius: 14px;
  border: 4px solid rgba(255,255,255,0.85);
  box-shadow: 0 20px 80px rgba(0,0,0,0.6);
  max-width: 90%;
}
#winner-kicker { text-transform: uppercase; letter-spacing: 0.25em; font-weight: 700; font-size: clamp(12px, 1.5vw, 20px); opacity: 0.8; }
#winner-text { font-size: clamp(28px, 5.5vw, 84px); font-weight: 900; margin: 6px 0; overflow-wrap: anywhere; }
#winner-sub { font-size: clamp(12px, 1.4vw, 19px); opacity: 0.85; }
#winner-actions { display: flex; gap: 12px; justify-content: center; margin-top: clamp(12px, 2vw, 26px); }
#winner-actions button {
  background: rgba(0,0,0,0.35); color: #fff; border: 1px solid rgba(255,255,255,0.6);
  padding: 8px 20px; border-radius: 8px; font-size: 15px; cursor: pointer;
}
#winner-actions button.primary { background: #fff; color: #111; border-color: #fff; font-weight: 700; }

#bins {
  display: flex;
  gap: 2px;
  height: 140px;
  background: var(--frame-blue-dark);
  border: 6px solid var(--frame-blue);
  border-top: none;
  border-radius: 0 0 8px 8px;
  padding: 4px 4px 0;
}

.bin {
  flex: 1 1 0;
  min-width: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  background: rgba(255,255,255,0.03);
  border-radius: 2px 2px 0 0;
  overflow: hidden;
}

.bin-fill {
  position: relative;
  width: 100%;
  height: 0%;
  background: repeating-linear-gradient(
    90deg,
    var(--bin-colour, #888) 0px, var(--bin-colour, #888) 12px,
    rgba(0,0,0,0.3) 12px, rgba(0,0,0,0.3) 15px
  );
  transition: height 0.15s ease-out;
}

/* Rounded caps on each "straw", sitting at the current top of the fill so
   the bundle reads as packed rounded-top rods rather than a flat bar —
   matches the reference screenshot's bucket look. */
.bin-fill::before {
  content: '';
  position: absolute;
  top: -6px;
  left: 0;
  right: 0;
  height: 12px;
  background-image: radial-gradient(circle, var(--bin-colour, #888) 58%, transparent 60%);
  background-size: 15px 12px;
  background-repeat: repeat-x;
}

.bin.eliminated .bin-fill { height: 100% !important; background: #000; }
.bin.eliminated .bin-fill::before { display: none; }
.bin.eliminated::after {
  content: '\2716';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--frame-blue);
  font-size: 22px;
  font-weight: 900;
}

.bin-count {
  position: absolute;
  bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.9);
  z-index: 2;
}
.bin.eliminated .bin-count { display: none; }

.bin-badge {
  position: absolute;
  bottom: 4px;
  display: none;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #111;
  border: 2px solid #fff;
  color: #fff;
  font-size: 12px;
  z-index: 3;
}
.bin.complete .bin-badge { display: flex; }


/* ---------- leaderboard ---------- */
#leaderboard {
  flex: none;
  width: 260px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  max-height: calc(100vh - 32px);
  overflow: auto;
  position: sticky;
  top: 16px;
}
#leaderboard h2 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
#leaderboard h2:not(:first-child) { margin-top: 16px; }
#leaderboard ol { list-style: none; margin: 0; padding: 0; }

.lb-row {
  display: grid;
  grid-template-columns: 20px 12px 1fr auto;
  align-items: center;
  gap: 6px;
  padding: 3px 4px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  border-radius: 4px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--lb-colour, transparent) 22%, transparent) var(--pct, 0%), transparent var(--pct, 0%));
}
.lb-rank { color: var(--muted); text-align: right; }
.lb-sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex: none; }
.lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-n { color: var(--muted); text-align: right; white-space: nowrap; }
.lb-win { color: #ffd54a; font-weight: 700; }
.lb-row.out .lb-name { text-decoration: line-through; color: #666; }
.lb-row.out .lb-n { color: #666; }
.lb-row.out .lb-sw { opacity: 0.4; }

.log-row {
  display: grid;
  grid-template-columns: 44px 44px 1fr;
  gap: 6px;
  align-items: center;
  padding: 2px 4px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.log-leg { color: var(--muted); }
.log-out { display: flex; align-items: center; gap: 6px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.log-row.current { color: var(--accent); }
.log-row.current .log-out, .log-row.current .log-leg { color: var(--accent); }

/* ---------- fullscreen: keep the whole arena (field + bins) in view ---------- */
#game-main:fullscreen {
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}
#game-main:fullscreen #stack {
  width: min(100vw, calc((100vh - 160px) * 16 / 9 + 12px));
}

/* ---------- setup screen ---------- */
#setup { max-width: 1100px; margin: 0 auto; padding: 28px 20px 60px; }
.hero { margin-bottom: 26px; }
.hero h1 { margin: 0; font-size: 40px; line-height: 1.15; letter-spacing: -0.02em; }
.hero p { color: var(--muted); margin: 8px 0 0; max-width: 60ch; }

#setup section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px;
  margin-bottom: 18px;
}
#setup h2 { margin: 0; font-size: 18px; line-height: 1.15; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.tools .hint { margin: 0 6px 0 0; color: var(--text); }

#setup button, #setup select {
  font: inherit;
  color: var(--text);
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel-2);
  padding: 7px 14px;
  border-radius: 8px;
}
#setup button:hover { border-color: #4a5470; }
#setup button:focus-visible, #setup input:focus-visible, #setup select:focus-visible, #setup summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
#setup button.ghost { background: transparent; }
#setup button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 700;
  padding: 12px 40px;
  font-size: 18px;
}
#setup button.primary:hover { filter: brightness(1.08); }
.start-row { display: flex; justify-content: center; margin: 26px 0 14px; }

.team-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.team-field {
  display: flex;
  align-items: center;
  min-width: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.team-field:focus-within { border-color: var(--accent); }
.team-field.off { opacity: 0.45; }
.team-check { margin: 0 8px; accent-color: var(--accent); width: 16px; height: 16px; flex: none; cursor: pointer; }
.team-field .chip { flex: none; width: 26px; align-self: stretch; min-height: 38px; background: var(--chip); cursor: pointer; }
.team-field .team-name {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: 0;
  color: var(--text);
  font: inherit;
  padding: 10px 8px;
}
.team-field .team-name::placeholder { color: #737a92; }

.settings-grid { display: flex; flex-wrap: wrap; gap: 20px 32px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { color: var(--muted); font-size: 13px; }

#setup details { margin-top: 14px; }
#setup summary { cursor: pointer; color: var(--muted); }
#setup summary:hover { color: var(--text); }
.paste textarea {
  width: 100%;
  margin: 8px 0;
  padding: 10px;
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  resize: vertical;
}

.music-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
#musicName { margin: 0; color: var(--text); }
.credit a { color: var(--accent); }

#setup .seg, .seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
#setup .seg button, .seg-file { border: 0; border-radius: 0; background: transparent; padding: 7px 14px; cursor: pointer; display: inline-block; color: var(--text); font: inherit; }
#setup .seg button:hover, .seg-file:hover { background: rgba(255,255,255,0.06); }
#setup .seg button.on, .seg-file.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.seg-file input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg-file:focus-within { outline: 2px solid var(--accent); outline-offset: -2px; }

.howto { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 18px; }
.howto ul { margin: 10px 0 0; padding-left: 20px; color: var(--muted); }
.howto li { margin: 5px 0; }
.howto b { color: var(--text); }

/* ---------- narrow screens ---------- */
@media (max-width: 1100px) {
  #game-layout { flex-direction: column; }
  #game-main { width: 100%; flex: none; }
  #leaderboard { width: 100%; position: static; max-height: none; }
  #leaderboard ol { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); column-gap: 16px; }
  .team-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  #bins { height: 90px; }
  .bin-count { font-size: 9px; }
  #controls input[type=range] { display: none; }
  #controls { gap: 5px; }
  .team-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero h1 { font-size: 30px; }
}
@media (max-width: 460px) {
  .team-grid { grid-template-columns: 1fr; }
}
