:root {
  --bg:        #eef2ff;
  --surface:   #ffffff;
  --card:      #ffffff;
  --border:    #c8d0e8;
  --red:       #e3350d;
  --red-dark:  #b02a0a;
  --blue:      #3b5dc9;
  --yellow:    #f8d030;
  --yellow-d:  #c8a800;
  --green:     #2dc73e;
  --green-d:   #1a8e29;
  --gray:      #7a88a8;
  --lightgray: #e4e9f8;
  --text:      #1a1e2e;
  --muted:     #7a88a8;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Nunito', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  background-image:
    radial-gradient(ellipse at 10% 10%, rgba(232,53,13,.08) 0%, transparent 50%),
    radial-gradient(ellipse at 90% 90%, rgba(59,93,201,.09) 0%, transparent 50%);
}

/* ──────────── SETUP ──────────── */
#setup-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 40px 16px;
}

.setup-card {
  background: var(--surface);
  border: 3px solid var(--text);
  border-radius: 22px;
  box-shadow: 7px 7px 0 var(--text);
  padding: 48px 44px 44px;
  width: 100%; max-width: 460px;
  position: relative; overflow: hidden;
}
.setup-card::before {
  content: "";
  position: absolute; top: 0; left: 0; right: 0; height: 10px;
  background: linear-gradient(90deg, var(--red) 50%, var(--blue) 50%);
}
.setup-card::after {
  content: "⚪";
  position: absolute; top: -30px; right: -30px;
  font-size: 120px; opacity: .04;
  pointer-events: none;
}

.setup-eyebrow {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px; color: var(--muted);
  letter-spacing: 2px; margin-bottom: 10px;
  display: block;
}
.setup-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 26px; color: var(--red);
  line-height: 1.5; margin-bottom: 6px;
  text-shadow: 4px 4px 0 rgba(176,42,10,.18);
}
.setup-sub {
  color: var(--muted); font-size: 13px; font-weight: 700;
  margin-bottom: 32px;
}

label.f-label {
  display: block;
  font-family: 'Press Start 2P', monospace;
  font-size: 7px; color: var(--blue);
  letter-spacing: 1.5px; margin-bottom: 10px;
}

textarea {
  width: 100%; height: 175px;
  background: var(--lightgray);
  border: 2px solid var(--border); border-radius: 14px;
  color: var(--text);
  font-family: 'Nunito', sans-serif; font-size: 14px; font-weight: 700;
  padding: 14px 16px; resize: none; outline: none; line-height: 1.9;
  transition: border-color .2s, box-shadow .2s;
}
textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(59,93,201,.12); }
textarea::placeholder { color: var(--muted); font-weight: 400; }

.text-inp {
  width: 100%;
  background: var(--lightgray);
  border: 2px solid var(--border); border-radius: 14px;
  color: var(--text);
  font-family: 'Nunito', sans-serif; font-size: 14px; font-weight: 700;
  padding: 12px 16px; outline: none; margin-bottom: 16px;
  transition: border-color .2s, box-shadow .2s;
}
.text-inp:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(59,93,201,.12); }
.text-inp::placeholder { color: var(--muted); font-weight: 400; }

.count-row {
  display: flex; justify-content: space-between; align-items: center;
  margin: 8px 0 26px;
}
.count-txt { font-size: 12px; font-weight: 700; color: var(--muted); }
.count-txt span { color: var(--red); }

.btn-start {
  width: 100%; padding: 17px;
  background: var(--red); color: #fff;
  border: 3px solid var(--text); border-radius: 14px;
  box-shadow: 5px 5px 0 var(--red-dark);
  font-family: 'Press Start 2P', monospace; font-size: 12px; letter-spacing: 1px;
  cursor: pointer; transition: transform .12s, box-shadow .12s;
}
.btn-start:hover { transform: translateY(-2px); box-shadow: 5px 7px 0 var(--red-dark); }
.btn-start:active { transform: translateY(2px); box-shadow: 3px 3px 0 var(--red-dark); }

/* ──────────── BRACKET SCREEN ──────────── */
#bracket-screen { display: none; }

.b-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px;
  background: var(--surface); border-bottom: 3px solid var(--text);
  position: sticky; top: 0; z-index: 100;
  box-shadow: 0 4px 0 rgba(0,0,0,.06);
}
.b-header-left { display: flex; align-items: center; gap: 14px; }
.b-stripe {
  display: block; width: 10px; height: 40px;
  background: linear-gradient(to bottom, var(--red) 50%, var(--blue) 50%);
  border-radius: 5px; border: 2px solid var(--text);
}
.b-title {
  font-family: 'Press Start 2P', monospace; font-size: 13px;
  color: var(--text); line-height: 1.6;
}
.b-title em { color: var(--red); font-style: normal; }

.btn-reset {
  padding: 9px 18px;
  background: var(--surface); border: 2px solid var(--border); border-radius: 10px;
  color: var(--muted); font-family: 'Nunito', sans-serif;
  font-size: 13px; font-weight: 700; cursor: pointer; transition: all .18s;
}
.btn-reset:hover { border-color: var(--red); color: var(--red); }

/* ──────────── BRACKET LAYOUT ──────────── */
.bracket-scroll { overflow-x: auto; padding: 40px 36px 70px; }

/* The key layout insight:
   - All round-matches divs share the SAME fixed height (set by JS after first paint).
   - Each match-spacer inside uses flex:1, so R0 spacers are small, R1 are 2x, R2 4x, etc.
   - This naturally centres each match at the vertical midpoint of its "slot".
   - Connectors are drawn on a <canvas> by measuring actual bounding boxes.
*/
.bracket-area {
  position: relative;
  width: max-content;
}

#cv {
  position: absolute; top: 0; left: 0;
  pointer-events: none; z-index: 0;
}

.rounds-flex {
  display: flex; gap: 0; align-items: flex-start;
  position: relative; z-index: 1;
}

.round-col { display: flex; flex-direction: column; width: 240px; flex-shrink: 0; }

.round-label {
  height: 44px; /* MUST stay in sync with LABEL_H constant in JS */
  display: flex; align-items: flex-end; justify-content: center;
  padding-bottom: 14px;
  font-family: 'Press Start 2P', monospace; font-size: 7px;
  color: var(--blue); letter-spacing: 1.5px;
}

.round-matches { display: flex; flex-direction: column; }

.match-spacer {
  display: flex; align-items: center; justify-content: center;
  flex: 1; /* equal vertical slots — JS fixes height of parent */
}

.conn-gap { width: 52px; flex-shrink: 0; } /* gap between rounds for connectors */

/* ──────────── MATCH CARD ──────────── */
.match-card {
  background: var(--surface);
  border: 2px solid var(--border); border-radius: 14px;
  width: 210px; overflow: hidden;
  box-shadow: 3px 3px 0 var(--border);
  transition: border-color .18s, box-shadow .18s;
  margin: 0 15px;
}
.match-card.playable { border-color: var(--blue); box-shadow: 3px 3px 0 rgba(59,93,201,.3); }
.match-card.done     { border-color: var(--border); box-shadow: 3px 3px 0 var(--border); opacity:.65; }
.match-card.bye-card { border-color: var(--lightgray); box-shadow: none; opacity:.55; }
.match-card.err      { border-color: var(--red) !important; box-shadow: 3px 3px 0 rgba(227,53,13,.3) !important; }

.type-bar { height: 6px; background: var(--lightgray); }
.match-card.playable .type-bar { background: linear-gradient(90deg, var(--blue), var(--yellow)); }
.match-card.done .type-bar     { background: var(--border); }

.player-row {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px; transition: background .15s;
}
.player-row + .player-row { border-top: 1px solid var(--lightgray); }
.match-card.playable .player-row:hover { background: var(--lightgray); }

.pdot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
  background: var(--border); border: 1.5px solid transparent;
}
.player-row.win .pdot { background: var(--green); border-color: var(--green-d); }

.pname {
  flex: 1; font-size: 13px; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.player-row.win  .pname { color: var(--green-d); }
.player-row.loss .pname { color: var(--muted); text-decoration: line-through; }
.player-row.empty .pname { color: var(--muted); font-weight: 400; font-style: italic; }

.score-inp {
  width: 38px; flex-shrink: 0;
  background: var(--lightgray); border: 2px solid var(--border); border-radius: 8px;
  color: var(--text); text-align: center;
  font-family: 'Press Start 2P', monospace; font-size: 11px;
  padding: 4px 2px; outline: none; transition: border-color .18s;
}
.score-inp:focus { border-color: var(--blue); }
.score-inp:disabled { opacity: .35; cursor: not-allowed; }

.card-foot { border-top: 1px solid var(--lightgray); text-align: center; }

.validate-btn {
  width: 100%; padding: 9px;
  background: var(--yellow); border: none;
  color: var(--text);
  font-family: 'Press Start 2P', monospace; font-size: 7px; letter-spacing: .5px;
  cursor: pointer; transition: background .15s;
}
.validate-btn:hover:not(:disabled) { background: var(--yellow-d); }
.validate-btn:disabled { background: var(--lightgray); color: var(--muted); cursor: not-allowed; }

.done-lbl {
  padding: 9px;
  font-family: 'Press Start 2P', monospace; font-size: 7px; letter-spacing: 1px;
  color: var(--muted);
}
.bye-lbl {
  padding: 9px; text-align: center;
  font-family: 'Press Start 2P', monospace; font-size: 7px;
  color: var(--muted); letter-spacing: 1px;
}

/* ──────────── WINNER BANNER ──────────── */
#winner-banner {
  display: none; text-align: center;
  padding: 60px 20px 90px;
  background: linear-gradient(to bottom, var(--bg), #fff);
  border-top: 3px solid var(--text);
}
.w-trophy { font-size: 72px; display: block; margin-bottom: 20px; animation: wobble .7s ease infinite alternate; }
@keyframes wobble { from { transform: rotate(-12deg) scale(1); } to { transform: rotate(12deg) scale(1.12); } }
.w-eyebrow {
  font-family: 'Press Start 2P', monospace; font-size: 9px; letter-spacing: 3px;
  color: var(--muted); margin-bottom: 14px;
}
.w-name {
  font-family: 'Press Start 2P', monospace; font-size: 30px;
  color: var(--red); text-shadow: 5px 5px 0 rgba(176,42,10,.18);
  line-height: 1.5;
}
.w-sub { margin-top: 14px; font-size: 15px; color: var(--muted); font-weight: 700; }

.ranking {
  margin: 40px auto 0;
  max-width: 480px;
  border: 3px solid var(--text);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 5px 5px 0 var(--text);
}
.ranking-header {
  background: var(--text);
  padding: 10px 20px;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px; letter-spacing: 2px; color: #fff;
  display: grid; grid-template-columns: 52px 1fr 60px;
  gap: 8px;
}
.ranking-row {
  display: grid; grid-template-columns: 52px 1fr 60px;
  gap: 8px; align-items: center;
  padding: 11px 20px;
  background: var(--surface);
  border-top: 1px solid var(--lightgray);
  font-size: 14px; font-weight: 700;
}
.ranking-row.medal-1 { background: #fffbe6; }
.ranking-row.medal-2 { background: #f5f7ff; }
.ranking-row.medal-3 { background: #fff4f0; }
.rank-place {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px; color: var(--muted);
}
.ranking-row.medal-1 .rank-place { color: #c8a800; }
.ranking-row.medal-2 .rank-place { color: var(--gray); }
.ranking-row.medal-3 .rank-place { color: #b06040; }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score {
  text-align: right;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px; color: var(--blue);
}