:root {
  --bg: #0f1419;
  --panel: #171d26;
  --panel-2: #1e2633;
  --line: #2a3441;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #38bdf8;
  --accent-2: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --gold: #fbbf24;
}

* { box-sizing: border-box; }

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

h1, h2, h3, h4 { margin: 0; font-weight: 650; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  padding: 14px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 18px; }
.brand .sub { margin: 0; font-size: 12px; color: var(--muted); }
.ball { font-size: 26px; }
.tabs { display: flex; gap: 6px; margin-left: auto; }
.tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: #04121c; background: var(--accent); }
.topactions { display: flex; gap: 8px; }

/* ---------- generic ---------- */
main { padding: 22px; max-width: 1400px; margin: 0 auto; }

.btn {
  background: var(--accent);
  color: #04121c;
  border: 1px solid transparent;
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 650;
  font-size: 14px;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; filter: none; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); }
.btn.danger { color: var(--danger); border-color: rgba(239, 68, 68, .4); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.icon-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: 15px; padding: 4px 6px; border-radius: 6px;
}
.icon-btn:hover { color: var(--danger); background: var(--panel-2); }

input, select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 7px 10px;
  border-radius: 7px;
  font-size: 14px;
  font-family: inherit;
}
input:focus, select:focus { outline: 2px solid rgba(56, 189, 248, .45); outline-offset: -1px; }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); font-weight: 600; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
}
.card h3 { font-size: 15px; margin-bottom: 10px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.section-title { display: flex; align-items: center; gap: 12px; margin: 4px 0 14px; }
.section-title h2 { font-size: 17px; }
.hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; }
.pill {
  font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  padding: 3px 9px; border-radius: 99px; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line);
}
.pill.ok { color: var(--accent-2); border-color: rgba(34, 197, 94, .4); }
.pill.warn { color: var(--warn); border-color: rgba(245, 158, 11, .4); }

/* ---------- setup ---------- */
.player-row { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; }
.player-row .seedno { width: 22px; color: var(--muted); font-size: 12px; text-align: right; }
.player-row input { flex: 1; }
.config-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; }
td.num, th.num { text-align: center; }
tr.qualify td { background: rgba(34, 197, 94, .07); }
tr.qualify td:first-child { box-shadow: inset 3px 0 0 var(--accent-2); }
.rank { color: var(--muted); font-variant-numeric: tabular-nums; }
.tiedflag { color: var(--warn); font-size: 12px; margin-left: 4px; }

/* ---------- match rows ---------- */
.match {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px;
  margin-bottom: 7px; background: var(--panel-2);
}
.match .names { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.match .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.match .nm.win { font-weight: 700; color: var(--accent-2); }
.match .vs { color: var(--muted); font-size: 12px; }
.match .score { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 42px; text-align: center; }
.match .sets { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.match.done { border-color: rgba(34, 197, 94, .35); }

/* ---------- bracket ---------- */
.bracket-scroll { overflow-x: auto; padding: 4px 2px 18px; }
.bracket-canvas { position: relative; }
.bracket-heads { position: relative; }
.bracket-body { position: relative; }

.round-head {
  position: absolute; top: 0;
  display: flex; align-items: center; gap: 8px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 700;
}
.round-head .rname { flex: 1; }
.round-head .rprog { font-variant-numeric: tabular-nums; opacity: .8; }
.round-head select.round-bo {
  padding: 1px 4px; font-size: 11px; border-radius: 5px;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
}
.round-head select.round-bo:hover { color: var(--text); }

/* connector lines */
svg.links { position: absolute; inset: 0; pointer-events: none; }
svg.links .link { fill: none; stroke: var(--line); stroke-width: 2; }
svg.links .link.live { stroke: var(--accent-2); stroke-width: 2.5; opacity: .85; }

.ko-match {
  position: absolute;
  border: 1px solid var(--line); border-radius: 10px; background: var(--panel);
  overflow: hidden; cursor: pointer; transition: border-color .12s, transform .12s;
}
.ko-match:hover { border-color: var(--accent); transform: translateX(1px); }
.ko-match.ready { border-color: rgba(56, 189, 248, .45); }
.ko-match.done { border-color: rgba(34, 197, 94, .45); }
.ko-match.bye { opacity: .72; cursor: default; }
.ko-match.bye:hover { border-color: var(--line); transform: none; }

/* the two sides of the table */
.slot { display: flex; align-items: center; gap: 8px; padding: 7px 10px; font-size: 14px; }
.slot .sname { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.slot .ssets { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--muted); }
.slot.winner { background: rgba(34, 197, 94, .12); }
.slot.winner .sname { font-weight: 700; }
.slot.winner .ssets { color: var(--accent-2); }
.slot.loser { opacity: .5; }
.slot.loser .sname { text-decoration: line-through; text-decoration-color: rgba(239,68,68,.5); }
.slot.empty .sname { color: var(--muted); font-style: italic; }
.slot .seedtag {
  font-size: 10px; font-weight: 700; color: var(--muted); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px;
  min-width: 20px; text-align: center; flex: none;
}

/* divider between the two players */
.ko-split { position: relative; height: 0; }
.ko-split::before {
  content: ''; position: absolute; left: 10px; right: 10px; top: 0;
  border-top: 1px solid var(--line);
}
.ko-split span {
  position: absolute; left: 50%; top: 0; transform: translate(-50%, -50%);
  background: var(--panel); color: var(--muted);
  font-size: 9px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  padding: 0 5px;
}

.ko-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 11px; color: var(--muted); padding: 5px 10px 6px;
  border-top: 1px solid var(--line); background: var(--panel-2);
}
.ko-foot .ftxt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ko-foot .bo {
  font-size: 10px; font-weight: 700; color: var(--muted);
  border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; flex: none;
}

.champion {
  position: absolute;
  display: flex; flex-direction: column; gap: 4px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); padding: 10px;
}
.champion.crowned { border-color: var(--gold); background: rgba(251, 191, 36, .06); }

.pod-row {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border-radius: 8px;
  background: rgba(148, 163, 184, .05);
  border: 1px solid transparent;
}
.pod-row .medal { font-size: 17px; flex: none; }
.pod-row .pname {
  flex: 1; min-width: 0; font-weight: 700; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.pod-row .pplace {
  font-size: 10px; font-weight: 700; letter-spacing: .06em;
  color: var(--muted); opacity: .7; flex: none;
}
.pod-row .pwait { display: none; }
.pod-row.filled { background: rgba(148, 163, 184, .1); }
.pod-row.filled .pname { color: var(--text); }
.pod-row.filled.p1 {
  background: rgba(251, 191, 36, .12); border-color: var(--gold);
}
.pod-row.filled.p1 .pname { color: var(--gold); font-size: 15px; }

/* ---------- third place play-off ---------- */
svg.links .link.drop {
  stroke-dasharray: 5 4; stroke-width: 1.5; opacity: .5;
}
svg.links .link.drop.live { stroke: var(--accent); opacity: .7; }
.ko-match.bronze { border-style: dashed; overflow: visible; }
.ko-match.bronze.done { border-style: solid; }
.ko-match .ko-tag {
  position: absolute; top: -8px; left: 10px;
  font-size: 9px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase;
  color: var(--muted); background: var(--bg); padding: 0 6px; border-radius: 4px;
}

.seed-editor { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 10px; }
.seed-slot { display: flex; align-items: center; gap: 8px; }
.seed-slot .lbl { font-size: 12px; color: var(--muted); width: 42px; }
.seed-slot select { flex: 1; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(4, 8, 12, .72);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  width: min(460px, 100%); max-height: 90vh; display: flex; flex-direction: column;
}
.modal-head, .modal-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px;
}
.modal-head { border-bottom: 1px solid var(--line); }
.modal-foot { border-top: 1px solid var(--line); }
.modal-head h2 { font-size: 15px; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.set-row { display: grid; grid-template-columns: 54px 1fr 14px 1fr 30px; align-items: center; gap: 8px; margin-bottom: 8px; }
.set-row .setlbl { font-size: 12px; color: var(--muted); font-weight: 600; }
.set-row input { width: 100%; text-align: center; font-variant-numeric: tabular-nums; }
.set-row .sep { text-align: center; color: var(--muted); }
.set-row input.bad { border-color: var(--warn); }
.modal .names-head { display: flex; justify-content: space-between; font-weight: 650; margin-bottom: 12px; gap: 10px; }
.modal .names-head span { flex: 1; }
.modal .names-head span:last-child { text-align: right; }
.tally { text-align: center; font-size: 13px; color: var(--muted); margin-top: 10px; }
.tally b { color: var(--text); }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 18px; border-radius: 99px; z-index: 80; font-size: 14px;
}

.empty-state { text-align: center; padding: 50px 20px; color: var(--muted); }
.empty-state h3 { color: var(--text); margin-bottom: 8px; }

/* ---------- added: standings diffs, per-round best-of, winner picker ---------- */
td.num.pos { color: var(--accent-2); }
td.num.neg { color: var(--danger); }

.winner-pick { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.btn.pick { max-width: 48%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn.pick.active { background: var(--accent-2); color: #04121c; border-color: transparent; }

/* ---------- group schedule rounds ---------- */
.sched-round { margin-bottom: 14px; }
.sched-round:last-child { margin-bottom: 0; }
.sched-head {
  display: flex; align-items: center; gap: 10px;
  margin: 0 0 6px; padding-bottom: 4px;
  border-bottom: 1px dashed var(--border);
}
.sched-head .rnum {
  font-size: 11px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--muted);
}
.sched-head .resting {
  font-size: 11px; color: var(--muted); opacity: .85; font-style: italic;
}

.section-title .spacer { flex: 1; }

/* ---------- printable group sheets ---------- */
#printArea { display: none; }

@media print {
  @page { size: A4 portrait; margin: 12mm; }

  body { background: #fff !important; color: #000 !important; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; font: 10.5pt/1.3 -apple-system, "Segoe UI", Helvetica, Arial, sans-serif; color: #000; }

  .sheet { page-break-after: always; break-after: page; }
  .sheet:last-child { page-break-after: auto; break-after: auto; }

  .sheet header {
    display: flex; justify-content: space-between; align-items: flex-end;
    border-bottom: 2px solid #000; padding-bottom: 6px; margin-bottom: 10px;
  }
  .sheet .t { font-size: 9pt; text-transform: uppercase; letter-spacing: .06em; color: #444; }
  .sheet h1 { margin: 2px 0 0; font-size: 20pt; }
  .sheet h1 span { font-size: 10pt; font-weight: 400; color: #444; }
  .sheet .meta { display: flex; gap: 14px; font-size: 9pt; }
  .sheet .line { display: inline-block; width: 34mm; border-bottom: 1px solid #000; margin-left: 4px; }
  .sheet .line.short { width: 14mm; }

  .sheet h2 { font-size: 11pt; margin: 12px 0 5px; }
  .sheet h2 span { font-weight: 400; font-size: 8.5pt; color: #555; }

  .sheet table { width: 100%; border-collapse: collapse; }
  .sheet th, .sheet td { border: 1px solid #777; padding: 3px 5px; vertical-align: middle; }
  .sheet th { color: #000; background: #eee; font-size: 8.5pt; text-align: left; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .sheet tr { break-inside: avoid; page-break-inside: avoid; }

  .sheet .no { width: 7mm; text-align: center; color: #555; }
  .sheet .pl { white-space: nowrap; }
  .sheet .vs { color: #777; font-size: 8pt; margin: 0 6px; }

  .sched td { height: 8.5mm; }
  .sched .rhead td {
    height: auto; background: #f4f4f4; border-left: 0; border-right: 0;
    font-size: 8.5pt; font-weight: 700; padding-top: 5px;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .sched .rhead span { font-weight: 400; font-style: italic; color: #555; }
  .sched .setc { width: 21mm; text-align: center; white-space: nowrap; }
  .sched .setc i { font-style: normal; margin: 0 2px; }
  .sched .setc b { display: inline-block; min-width: 6mm; }
  .sched .box { display: inline-block; width: 7mm; height: 5.5mm; border: 1px solid #999; vertical-align: middle; }
  .sched .win { width: 30mm; }
  .sched .ok { width: 8mm; text-align: center; }
  .sched .tick { display: inline-block; width: 4mm; height: 4mm; border: 1px solid #999; }

  .xtab td { height: 9mm; }
  .xtab .gc { width: 13mm; text-align: center; font-size: 9pt; }
  .xtab .self { background: #bbb; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .xtab .st { width: 13mm; text-align: center; }
  .xtab .rank { font-weight: 700; }

  .sheet .rules { margin-top: 8px; font-size: 8pt; color: #555; }
  /* if a big group spills over, move the whole results grid rather than split it */
  .sheet h2 { break-after: avoid; page-break-after: avoid; }
  .sheet .xtab { break-inside: avoid; page-break-inside: avoid; }
  .sheet thead { display: table-header-group; }

  .sheet.compact { font-size: 9.5pt; }
  .sheet.compact h2 { margin: 8px 0 4px; }
  .sheet.compact .sched td { height: 6.6mm; padding: 1px 5px; }
  .sheet.compact .sched .rhead td { padding-top: 2px; padding-bottom: 1px; }
  .sheet.compact .sched .box { height: 4.6mm; }
  .sheet.compact .xtab td { height: 7mm; }
}
