:root {
  --bg: #f2f5f1;
  --surface: #ffffff;
  --surface-2: #f7faf6;
  --ink: #131a15;
  --muted: #64736a;
  --line: #e0e7e0;
  --accent: #1e8e4e;
  --accent-soft: #e6f5ec;
  --ball: #d8f43f;
  --team-a: #1e8e4e;
  --team-a-bg: #e6f5ec;
  --team-b: #b4531a;
  --team-b-bg: #fdefe4;
  --danger: #b3261e;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 30, 20, .06), 0 8px 24px -16px rgba(16, 30, 20, .3);
}

:root[data-theme="dark"] {
  --bg: #0d120f;
  --surface: #151d18;
  --surface-2: #1b241d;
  --ink: #e7efe8;
  --muted: #96a89c;
  --line: #27332b;
  --accent: #58d68d;
  --accent-soft: #17301f;
  --team-a: #58d68d;
  --team-a-bg: #16301f;
  --team-b: #f0a163;
  --team-b-bg: #33220f;
  --danger: #f28c84;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -16px rgba(0, 0, 0, .8);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d120f;
    --surface: #151d18;
    --surface-2: #1b241d;
    --ink: #e7efe8;
    --muted: #96a89c;
    --line: #27332b;
    --accent: #58d68d;
    --accent-soft: #17301f;
    --team-a: #58d68d;
    --team-a-bg: #16301f;
    --team-b: #f0a163;
    --team-b-bg: #33220f;
    --danger: #f28c84;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -16px rgba(0, 0, 0, .8);
  }
}

* { box-sizing: border-box; }

/* Own display on #result/#rounds would otherwise beat the UA [hidden] rule. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { font-family: Outfit, Inter, system-ui, sans-serif; letter-spacing: -.02em; margin: 0; }

.wrap { width: 100%; max-width: 940px; margin: 0 auto; padding: 0 16px; }

/* ------------------------------------------------------------- top bar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-block: 12px; }
.topbar-inner nav { display: flex; gap: 4px; align-items: center; margin-left: auto; }
.topbar-inner nav a {
  color: var(--muted); text-decoration: none; font-size: 13.5px; font-weight: 500;
  padding: 7px 11px; border-radius: 9px;
}
.topbar-inner nav a:hover { background: var(--surface-2); color: var(--ink); }
.topbar-inner nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.brand a { color: inherit; text-decoration: none; display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 19px; font-weight: 700; }
.brand p { margin: 1px 0 0; font-size: 12.5px; color: var(--muted); }
.ball {
  width: 34px; height: 34px; flex: none; border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,.55), transparent 60%),
    var(--ball);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12);
  position: relative;
}
.ball::after {
  content: "";
  position: absolute; inset: 6px;
  background:
    radial-gradient(circle at 20% 22%, #2b3a2f 0 2.2px, transparent 2.4px),
    radial-gradient(circle at 72% 18%, #2b3a2f 0 2.2px, transparent 2.4px),
    radial-gradient(circle at 46% 52%, #2b3a2f 0 2.2px, transparent 2.4px),
    radial-gradient(circle at 16% 72%, #2b3a2f 0 2.2px, transparent 2.4px),
    radial-gradient(circle at 80% 70%, #2b3a2f 0 2.2px, transparent 2.4px);
}
.icon-btn {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--ink);
  width: 38px; height: 38px; border-radius: 10px; font-size: 17px; cursor: pointer; flex: none;
}

/* --------------------------------------------------------------- cards */
main { padding-block: 20px 8px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.card-head h2 { font-size: 16px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.step {
  display: grid; place-items: center;
  width: 22px; height: 22px; border-radius: 7px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 12px; font-weight: 700;
}
.badge {
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 1px 9px; font-size: 12px; color: var(--muted); font-weight: 600;
}
.head-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ------------------------------------------------------------- buttons */
button { font: inherit; }
.ghost {
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px;
  padding: 7px 12px; font-size: 13px; font-weight: 500; cursor: pointer;
}
.ghost:hover { border-color: var(--accent); color: var(--accent); }
.ghost.danger:hover { border-color: var(--danger); color: var(--danger); }
.ghost.add { width: 100%; padding: 10px; border-style: dashed; }
.primary {
  background: var(--accent); color: #fff;
  border: none; border-radius: 11px;
  padding: 13px 28px; font-size: 15px; font-weight: 600; cursor: pointer;
  box-shadow: 0 6px 18px -8px var(--accent);
}
:root[data-theme="dark"] .primary { color: #08140c; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .primary { color: #08140c; } }
.primary:hover { filter: brightness(1.05); }
.cta { display: flex; gap: 10px; align-items: center; justify-content: center; flex-wrap: wrap; }

/* -------------------------------------------------------------- roster */
.roster-head, .roster-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px 78px 38px;
  gap: 8px;
  align-items: center;
}
.roster[data-times="on"] .roster-head,
.roster[data-times="on"] .roster-row {
  grid-template-columns: minmax(0, 1fr) 92px 74px 104px 104px 38px;
}
.roster-head {
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 600; padding: 0 2px 8px;
}
.roster-head .col-time, .roster-row .col-time { display: none; }
.roster[data-times="on"] .col-time { display: block; }
.roster-row { padding: 4px 0; }
.roster-row + .roster-row { border-top: 1px solid var(--line); }

input, select, textarea {
  font: inherit; color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 9px;
  padding: 9px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
select { appearance: none; background-image: none; }
.roster-row .del {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--muted);
  border-radius: 9px; height: 38px; cursor: pointer; font-size: 15px;
}
.roster-row .del:hover { color: var(--danger); border-color: var(--danger); }
.roster-empty { padding: 22px 6px; text-align: center; color: var(--muted); font-size: 14px; }
.roster-foot { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.roster-foot .add { flex: 1 1 200px; }
.hint { color: var(--muted); font-size: 12.5px; margin: 10px 0 0; }
.hint code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 12px; }

/* -------------------------------------------------------------- config */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.field { display: grid; gap: 5px; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.switches { display: grid; gap: 10px; margin-top: 14px; }
.check { display: flex; align-items: center; gap: 9px; font-size: 13.5px; cursor: pointer; }
.check input { width: 17px; height: 17px; flex: none; accent-color: var(--accent); padding: 0; }

/* --------------------------------------------------------------- alert */
.alert {
  background: #fdecea; color: #8c1d18; border: 1px solid #f3c3bd;
  border-radius: var(--radius); padding: 12px 14px; margin: 0; font-size: 14px;
}
:root[data-theme="dark"] .alert { background: #341714; color: #f6b6ae; border-color: #5d2a24; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .alert { background: #341714; color: #f6b6ae; border-color: #5d2a24; } }

/* ------------------------------------------------------------ summary */
#result { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 10px; }
.stat {
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 11px; padding: 11px 12px;
}
.stat b { display: block; font-family: Outfit, sans-serif; font-size: 22px; font-weight: 600; line-height: 1.15; }
.stat span { font-size: 12px; color: var(--muted); }

/* -------------------------------------------------------------- rounds */
#rounds { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.round {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}
.round-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 11px 16px; background: var(--surface-2); border-bottom: 1px solid var(--line);
}
.round-head h3 { font-size: 14px; font-weight: 600; }
.round-head time { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.courts { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1px; background: var(--line); }
.court { background: var(--surface); padding: 12px 16px; }
.court-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 9px; }
.court-no { font-size: 12px; font-weight: 600; color: var(--muted); }
.tag { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); }
.tag-men { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.tag-women { background: var(--team-b-bg); color: var(--team-b); border-color: transparent; }
.tag-mixed { background: #eef0fb; color: #4b53a8; border-color: transparent; }
.tag-uneven { background: #fdecea; color: #8c1d18; border-color: transparent; }
:root[data-theme="dark"] .tag-mixed { background: #1e2140; color: #a6adf0; }
:root[data-theme="dark"] .tag-uneven { background: #341714; color: #f6b6ae; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tag-mixed { background: #1e2140; color: #a6adf0; }
  :root:not([data-theme="light"]) .tag-uneven { background: #341714; color: #f6b6ae; }
}
.gap-note { font-size: 11.5px; color: var(--muted); margin-left: auto; }

.vs { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px; align-items: center; }
.team { display: grid; gap: 5px; border-radius: 10px; padding: 9px 10px; min-width: 0; }
.team-a { background: var(--team-a-bg); }
.team-b { background: var(--team-b-bg); text-align: right; }
.team .p { font-size: 14px; font-weight: 500; display: flex; gap: 6px; align-items: baseline; }
.team-b .p { justify-content: flex-end; }
.team .p i { font-style: normal; font-size: 10.5px; font-weight: 700; opacity: .75; }
.team-a .p i { color: var(--team-a); }
.team-b .p i { color: var(--team-b); }
.vs-label { font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .08em; }
.resting { padding: 9px 16px; font-size: 12.5px; color: var(--muted); border-top: 1px dashed var(--line); }
.resting b { color: var(--ink); font-weight: 500; }
.round-note { padding: 11px 16px; font-size: 13px; color: var(--muted); }

/* --------------------------------------------------------------- table */
.table-scroll { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
.stat-table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
.stat-table th, .stat-table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.stat-table th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.stat-table td.num { font-variant-numeric: tabular-nums; font-weight: 600; }
.stat-table td.partners { white-space: normal; color: var(--muted); font-size: 12.5px; min-width: 200px; }
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); }

/* --------------------------------------------------------------- misc */
.foot { padding: 18px 16px 34px; color: var(--muted); font-size: 12px; text-align: center; }
dialog {
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink);
  padding: 0; width: min(520px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(10, 18, 13, .45); }
.dialog-body { padding: 18px; display: grid; gap: 10px; }
.dialog-body h3 { font-size: 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 13.5px; font-weight: 500; z-index: 50;
}

@media (max-width: 520px) {
  .roster-head { display: none; }
  .roster-row,
  .roster[data-times="on"] .roster-row {
    grid-template-columns: minmax(0, 1fr) 68px 48px 34px;
    gap: 6px;
    padding: 7px 0;
  }
  .roster-row input, .roster-row select { padding: 8px 7px; }
  .roster-row .del { height: 34px; }
  .roster[data-times="on"] .roster-row {
    grid-template-areas:
      "name   gender level del"
      "from   from   to   to";
  }
  .roster[data-times="on"] .roster-row .name   { grid-area: name; }
  .roster[data-times="on"] .roster-row .gender { grid-area: gender; }
  .roster[data-times="on"] .roster-row .level  { grid-area: level; }
  .roster[data-times="on"] .roster-row .from   { grid-area: from; }
  .roster[data-times="on"] .roster-row .to     { grid-area: to; }
  .roster[data-times="on"] .roster-row .del    { grid-area: del; }
  .vs { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .team-b { text-align: left; }
  .team-b .p { justify-content: flex-start; }
  .vs-label { justify-self: center; }
}

@media print {
  .topbar, .cta, .head-actions, .roster-foot, #card-players, #card-config, .foot, .hint { display: none !important; }
  body { background: #fff; font-size: 12px; }
  .card, .round { box-shadow: none; break-inside: avoid; }
  .round { page-break-inside: avoid; }
}

.tag-battle { background: #fdefe4; color: #b4531a; border-color: transparent; }
:root[data-theme="dark"] .tag-battle { background: #33220f; color: #f0a163; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tag-battle { background: #33220f; color: #f0a163; }
}
.alert.notice { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* Printing always uses the light palette — matches the dark-mode selectors'
   specificity and comes last, so it wins regardless of the device theme. */
@media print {
  :root,
  :root[data-theme="dark"],
  :root:not([data-theme="light"]) {
    --bg: #ffffff;
    --surface: #ffffff;
    --surface-2: #ffffff;
    --ink: #111111;
    --muted: #555555;
    --line: #cccccc;
    --accent: #1e8e4e;
    --accent-soft: #eef7f1;
    --team-a: #1e8e4e;
    --team-a-bg: #eef7f1;
    --team-b: #a34c18;
    --team-b-bg: #fdf1e8;
    --shadow: none;
  }
  .toast, #alert, #card-summary .head-actions { display: none !important; }
  .round-head { background: #f4f4f4; }
}

/* ===================================================== app-wide components */

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head h2 { font-size: 20px; font-weight: 600; }
.page-head p { margin: 3px 0 0; color: var(--muted); font-size: 13.5px; }

.empty {
  text-align: center; color: var(--muted); font-size: 14px;
  padding: 28px 16px; border: 1px dashed var(--line); border-radius: var(--radius);
}

.spinner { color: var(--muted); font-size: 14px; padding: 20px 0; text-align: center; }

/* --- session list ------------------------------------------------------- */
.session-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.session-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; text-decoration: none; color: inherit; box-shadow: var(--shadow);
}
.session-card:hover { border-color: var(--accent); }
.session-date {
  flex: none; width: 52px; text-align: center; line-height: 1.1;
  background: var(--accent-soft); color: var(--accent); border-radius: 10px; padding: 7px 0;
}
.session-date b { display: block; font-family: Outfit, sans-serif; font-size: 19px; font-weight: 600; }
.session-date span { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; }
.session-main { min-width: 0; flex: 1; }
.session-main h3 { font-size: 15px; font-weight: 600; }
.session-main p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.session-go { color: var(--muted); font-size: 18px; flex: none; }

/* --- roster picker ------------------------------------------------------ */
.picker { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 7px;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--ink);
  border-radius: 999px; padding: 7px 13px; font-size: 13.5px; cursor: pointer;
  font-weight: 500;
}
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] .chip[aria-pressed="true"] { color: #08140c; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .chip[aria-pressed="true"] { color: #08140c; }
}
.chip i { font-style: normal; font-size: 11px; font-weight: 700; opacity: .7; }
.chip .rank { font-variant-numeric: tabular-nums; opacity: .6; font-size: 11.5px; }

.picker-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.picker-bar input[type="search"] { flex: 1 1 180px; }

/* --- score entry -------------------------------------------------------- */
.team.is-winner { outline: 2px solid var(--accent); outline-offset: 1px; }
.team .win-badge { font-size: 10.5px; font-weight: 700; color: var(--accent); letter-spacing: .04em; }

.score-row {
  display: flex; align-items: center; gap: 7px; margin-top: 10px;
  padding-top: 10px; border-top: 1px dashed var(--line);
}
.score-row .score {
  width: 58px; text-align: center; font-variant-numeric: tabular-nums;
  font-weight: 600; padding: 7px 4px;
}
.score-row .score-sep { color: var(--muted); font-weight: 600; }
.score-row .spacer { flex: 1; }
.score-row button { font-size: 12.5px; padding: 7px 12px; }
.score-row .saved { font-size: 12px; color: var(--accent); font-weight: 600; }
.score-final {
  font-family: Outfit, sans-serif; font-size: 17px; font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* --- leaderboard -------------------------------------------------------- */
.rank-cell { font-family: Outfit, sans-serif; font-weight: 600; color: var(--muted); width: 34px; }
.rank-1 { color: #c79400; }
.rank-2 { color: #8c96a0; }
.rank-3 { color: #b1703a; }
.bar {
  display: block; height: 5px; border-radius: 3px; background: var(--line);
  margin-top: 4px; overflow: hidden;
}
.bar span { display: block; height: 100%; background: var(--accent); }
.rating-cell { font-family: Outfit, sans-serif; font-weight: 600; font-size: 15px; }
.muted { color: var(--muted); }
.inactive td { opacity: .5; }

/* --- share -------------------------------------------------------------- */
.share {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px;
}
.share code {
  flex: 1 1 200px; min-width: 0; font-size: 12.5px; color: var(--accent);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

@media (max-width: 520px) {
  .score-row .score { width: 52px; }
  .session-card { padding: 12px; gap: 10px; }
}

/* Roster rows on the session page show registry data as text, not inputs. */
.roster-row .static {
  display: flex; align-items: center; font-size: 14px; padding: 0 2px;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.roster-row .gender.static, .roster-row .level.static { color: var(--muted); font-size: 13px; }
.toast-error { background: var(--danger); color: #fff; }
.score-row .score-label { font-size: 12px; color: var(--muted); font-weight: 500; }

@media (max-width: 560px) {
  .brand p { display: none; }
  .brand h1 { font-size: 16px; }
  .ball { width: 28px; height: 28px; }
  .topbar-inner { gap: 6px; }
  .topbar-inner nav a { padding: 6px 8px; font-size: 12.5px; }
  .icon-btn { width: 34px; height: 34px; font-size: 15px; }
  .page-head h2 { font-size: 18px; }
}

@media print {
  #card-players, #card-config, .cta, .share, .page-head .head-actions,
  .score-row, .picker-bar, .topbar { display: none !important; }
  .team.is-winner { outline: 1px solid #1e8e4e; }
}
.stat-table td.row-actions { display: flex; gap: 6px; }

/* Live attendance: chips carry the round window somebody joined or left on. */
.live-hint {
  margin: 0 0 12px; font-size: 12.5px; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 12px;
}
.chip em {
  font-style: normal; font-size: 10.5px; font-weight: 700;
  padding: 1px 6px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}
.chip[aria-pressed="true"] em { background: rgba(255, 255, 255, .25); color: inherit; }
.chip[data-state="left"], .chip[data-state="partial"] {
  opacity: .62; border-style: dashed;
}
.chip[data-state="left"] em, .chip[data-state="partial"] em {
  background: var(--team-b-bg); color: var(--team-b);
}
