/* ElScoreKam styles. Plain CSS, no CDN: the app must load on a court with bad signal. */
:root {
  --bg: #070b14;
  --text: #f4f7ff;
  --muted: #93a0bd;
  --line: #2a3552;
  --surface: #141d33;
  --radius: 22px;
}
* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
/* Rapid taps on +1 must each count: no double-tap zoom and no tap delay (pinch-zoom still works).
   Also stop iPhones enlarging text when the phone is turned sideways. */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
button, .tile, .chip, .score { touch-action: manipulation; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
           max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}

/* ---------- top bar ---------- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: clamp(1rem, 2.4vmin, 1.5rem); }
#matchInfo { color: var(--muted); font-weight: 700; }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.btn {
  min-height: 46px; padding: 0 16px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-family: inherit; font-weight: 700; font-size: 1rem; cursor: pointer;
}
.btn:hover { background: #1b2646; }
.btn:active { transform: scale(.97); }
.btn:focus-visible, .name:focus-visible { outline: 3px solid #ffd24a; outline-offset: 2px; }
.btn.icon { min-width: 46px; padding: 0 12px; }
#conn { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: .85rem; font-weight: 700; }
#conn i { width: 10px; height: 10px; border-radius: 50%; background: #6b7690; }
#conn[data-s="on"] i { background: #35d07f; box-shadow: 0 0 8px #35d07f; }
#conn[data-s="retry"] i { background: #ffb020; }
#conn[data-s="badkey"] i { background: #ff4d4d; }
#conn[data-s="badkey"] { color: #ff8a8a; }

/* ---------- board ----------
   Each team panel is a size container: the name, the big score and the buttons size themselves from the
   panel's own width and height (cqw / cqh), so the board fits any phone, tablet or laptop, either way round. */
.board { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; min-height: 0; }
@media (orientation: portrait) {
  .board { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
}
.team {
  --c: #888;
  container-type: size;
  position: relative; min-height: 0; min-width: 0; overflow: hidden;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; justify-items: center;
  padding: 8px 10px 10px;
  border-radius: var(--radius);
  border: 4px solid color-mix(in srgb, var(--c) 40%, #0b1020);
  background: radial-gradient(120% 90% at 50% 0%,
    color-mix(in srgb, var(--c) 38%, #0b1020),
    color-mix(in srgb, var(--c) 12%, #070b14));
  transition: border-color .2s, box-shadow .2s;
}
.team.serving {
  border-color: var(--c);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 50%, transparent),
              0 0 44px color-mix(in srgb, var(--c) 55%, transparent);
}
.team.won { border-color: #ffd24a; box-shadow: 0 0 50px rgba(255, 210, 74, .45); }
.head { width: 100%; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.name {
  width: 100%; min-width: 0; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-weight: 800; line-height: 1.1; text-transform: uppercase; letter-spacing: .04em;
  font-size: max(1rem, min(3.2rem, 11cqh, 8cqw));
}
.line { display: flex; align-items: center; justify-content: center; gap: 1.2em; flex-wrap: wrap;
        color: rgba(255,255,255,.75); font-weight: 800; letter-spacing: .12em;
        font-size: max(.72rem, min(1.2rem, 6cqh, 4.2cqw)); }
.sets { display: flex; align-items: center; gap: .4em; }
.pip { display: inline-block; width: 1.1em; aspect-ratio: 1; border-radius: 50%; border: 2px solid rgba(255,255,255,.55); }
.pip.on { background: #fff; border-color: #fff; }
.score {                              /* its own size container, so the number fills whatever room is left */
  container-type: size; width: 100%; min-height: 0;
  display: flex; align-items: center; justify-content: center;
}
.score span {
  display: block; font-weight: 900; line-height: .9; font-variant-numeric: tabular-nums; letter-spacing: -.02em;
  font-size: min(100cqh, 52cqw);
  text-shadow: 0 6px 30px rgba(0,0,0,.4);
}
.score.bump { animation: bump .25s ease-out; }
@keyframes bump { 40% { transform: scale(1.1); } }
@media (prefers-reduced-motion: reduce) { .score.bump { animation: none; } }

/* bottom row: serve chip, rotation chip, −1, +1 */
.foot { width: 100%; display: flex; gap: 8px; align-items: stretch;
        height: max(44px, min(84px, 17cqh)); font-size: max(.8rem, min(1.2rem, 6cqh, 4cqw)); }
.chip {
  flex: 0 0 auto; padding: 0 .9em; border-radius: 999px; border: 2px solid rgba(255,255,255,.25);
  background: rgba(0,0,0,.28); color: #fff; font-family: inherit; font-weight: 800; letter-spacing: .06em;
  font-size: inherit; cursor: pointer; white-space: nowrap;
}
.chip.serve-on { background: #fff; color: #10172b; border-color: #fff; }
.chip.dim { opacity: .55; }
.chip[hidden] { display: none; }
.foot .btn { min-height: 0; height: 100%; border-radius: 16px; font-size: 1.6em; padding: 0 .6em; }
.foot .minus { flex: 0 0 auto; }
.foot .minus[hidden] { display: none; }
.foot .plus { flex: 1 1 30%; min-width: 30%; background: rgba(255,255,255,.92); color: #0b1020; border-color: #fff; }
@container (max-width: 520px) { .chip.serve .txt { display: none; } .chip { padding: 0 .7em; } }
.foot .plus:hover { background: #fff; }

/* ---------- history ---------- */
.history { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; min-height: 36px; color: var(--muted); font-weight: 700; }
.hist { padding: 6px 12px; border-radius: 999px; background: var(--surface); border: 2px solid var(--line); color: var(--text); }

/* ---------- overlay / toast ---------- */
#overlay { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px;
           background: rgba(4, 8, 16, .6); backdrop-filter: blur(3px); }
#overlay[hidden] { display: none; }
.card { width: min(640px, 100%); max-height: 100%; overflow: auto; text-align: center; padding: clamp(18px, 4vmin, 40px);
        border-radius: 28px; background: #101830; border: 2px solid var(--line); box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.ov-title { font-weight: 900; font-size: clamp(1.6rem, 5vmin, 3rem); text-transform: uppercase; letter-spacing: .03em; }
.ov-score { font-weight: 900; font-size: clamp(2.4rem, 9vmin, 5.5rem); margin: 10px 0; font-variant-numeric: tabular-nums; }
.ov-sub { color: var(--muted); font-weight: 700; margin-bottom: 18px; }
.ov-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.ov-actions .btn { min-height: 64px; font-size: 1.3rem; padding: 0 28px; }
.btn.primary { background: #fff; color: #0b1020; border-color: #fff; }
.btn.primary:hover { background: #e8eefc; }
.ov-hint { margin-top: 16px; color: var(--muted); font-size: .9rem; }

#toast { position: fixed; left: 50%; bottom: 70px; transform: translate(-50%, 20px); opacity: 0; z-index: 30; pointer-events: none;
         padding: 12px 20px; border-radius: 999px; background: #fff; color: #0b1020; font-weight: 800;
         transition: opacity .2s, transform .2s; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- dialogs ---------- */
dialog { margin: auto; width: min(600px, 94vw); max-height: 92dvh; overflow: auto;
         background: #101830; color: var(--text); border: 1px solid var(--line); border-radius: 22px; padding: 22px;
         -webkit-user-select: text; user-select: text; }
dialog::backdrop { background: rgba(0, 0, 0, .65); }
dialog h2 { font-size: 1.4rem; margin-bottom: 14px; }
dialog h3 { font-size: 1rem; margin: 18px 0 8px; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
label.f { display: flex; flex-direction: column; gap: 6px; font-weight: 700; font-size: .9rem; color: var(--muted); }
label.chk { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 8px 0; }
label.chk input { width: 22px; height: 22px; }
input[type=text], input[type=number], select {
  min-height: 44px; border-radius: 12px; border: 1px solid var(--line); background: #0a1124; color: var(--text);
  font-family: inherit; font-size: 1rem; padding: 0 12px;
}
input[type=color] { width: 100%; min-height: 44px; border: 1px solid var(--line); border-radius: 12px; background: #0a1124; padding: 4px; }
table { border-collapse: collapse; width: 100%; font-size: .95rem; }
td, th { text-align: start; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
kbd { padding: 2px 8px; border-radius: 6px; background: #0a1124; border: 1px solid var(--line); font-family: ui-monospace, Menlo, monospace; font-weight: 700; }
code { font-family: ui-monospace, Menlo, monospace; font-size: .88rem; color: #bcd0ff; }
.note { color: var(--muted); font-size: .9rem; line-height: 1.4; margin-bottom: 8px; }
.keyrow { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; font-weight: 700; }
.keyrow .btn { min-width: min(190px, 50%); }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }

/* ---------- read-only live view (?w=CODE) ---------- */
body.viewer .foot .btn, body.viewer #btnUndo, body.viewer #btnSwap, body.viewer #btnSettings, body.viewer #btnLog,
body.viewer #btnHelp, body.viewer .ov-actions, body.viewer .ov-hint { display: none; }
body.viewer .chip { pointer-events: none; }
body.waiting .board, body.waiting .history { visibility: hidden; }

/* ---------- new-match setup screen ---------- */
#setup { position: fixed; inset: 0; z-index: 40; overflow: auto; background: var(--bg);
         padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
#setup[hidden] { display: none; }
.setup-card { width: min(720px, 100%); margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.setup-card h1 { font-size: clamp(1.6rem, 5vw, 2.4rem); font-weight: 900; display: flex; align-items: center; gap: 10px; }
.setup-card h1 small { font-size: .9rem; color: var(--muted); font-weight: 700; }
.setup-card h3 { font-size: .85rem; color: var(--muted); letter-spacing: .1em; text-transform: uppercase; margin-top: 6px; }
.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg label { flex: 1; min-width: 120px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 56px; padding: 0 14px;
            border-radius: 16px; border: 2px solid var(--line); background: var(--surface); font-weight: 800; font-size: 1.05rem; cursor: pointer; }
.seg input:checked + span { background: #fff; color: #0b1020; border-color: #fff; }
.seg input:focus-visible + span { outline: 3px solid #ffd24a; outline-offset: 2px; }
.seg input:disabled + span { opacity: .45; cursor: not-allowed; }
.team-row { display: grid; grid-template-columns: minmax(0, 1fr) 56px; gap: 8px; min-width: 0; }
.team-row input[type=color] { min-height: 44px; }
#btnStart { min-height: 64px; font-size: 1.3rem; margin-top: 6px; }

/* ---------- point log ---------- */
.log { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 2px; font-variant-numeric: tabular-nums; }
.log li { display: grid; grid-template-columns: 5.5em 1fr auto; gap: 10px; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.log time { color: var(--muted); }
.log .via { color: var(--muted); font-size: .85rem; }
.log .after { font-weight: 800; }
.log li.undo { color: #ffb020; }
.log-empty { color: var(--muted); padding: 12px 0; }
.team-row input[type=text] { width: 100%; min-width: 0; }
@media (max-width: 520px) { #setup .grid2 { grid-template-columns: 1fr; } }
@media (max-width: 480px) { .actions .btn > span { display: none; } .actions { gap: 6px; } }
.sport-fields { display: flex; flex-direction: column; gap: 12px; }
.sport-fields[hidden] { display: none; }

/* ---------- padel board extras ---------- */
.games { display: none; align-items: baseline; gap: .4em; }
body.padel .games { display: flex; }
.games b { font-size: 1.5em; color: #fff; font-variant-numeric: tabular-nums; }
.badge { display: inline-block; margin-inline-start: 8px; padding: 2px 10px; border-radius: 999px; background: #ffd24a; color: #0b1020;
         font-weight: 900; letter-spacing: .04em; }

/* ---------- phones and short screens ---------- */
#matchInfo { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand { min-width: 0; max-width: 100%; }
@media (max-width: 600px) {
  .top { gap: 6px; }
  .brand { width: 100%; font-size: .95rem; }
  .actions { width: 100%; justify-content: space-between; }
  .actions .btn { min-height: 40px; flex: 1 1 0; padding: 0 6px; min-width: 0; }
  #app { gap: 8px; }
}
@media (max-height: 500px) {
  #app { gap: 6px; grid-template-rows: auto minmax(0, 1fr); }
  .history { display: none; }
  .top { flex-wrap: nowrap; }
  .brand { font-size: .9rem; }
  .actions .btn { min-height: 36px; }
  .actions .btn > span { display: none; }
}

/* ---------- live button + hands-free dialog ---------- */
#btnLive { display: inline-flex; align-items: center; gap: 6px; }
#btnLive .dot { width: 10px; height: 10px; border-radius: 50%; background: #6b7690; flex: 0 0 auto; }
#btnLive[data-s="on"] .dot { background: #35d07f; box-shadow: 0 0 8px #35d07f; }
#btnLive[data-s="retry"] .dot { background: #ffb020; }
.linkrow { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.linkrow code { flex: 1 1 100%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
                padding: 10px 12px; border-radius: 12px; background: #0a1124; border: 1px solid var(--line); }
.cmdrow { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; font-weight: 700; }
.steps { padding-inline-start: 1.3em; display: flex; flex-direction: column; gap: 2px; }
#dlgLive .btn[disabled] { opacity: .45; cursor: not-allowed; }
body.viewer #btnLive { pointer-events: none; }
.linkrow .btn { flex: 1 1 0; }
.qr { width: min(240px, 62vw); margin: 10px auto 0; padding: 10px; border-radius: 14px; background: #fff; }
.qr[hidden] { display: none; }
.qr svg { display: block; width: 100%; height: auto; }
.qr-more summary { cursor: pointer; color: var(--muted); font-weight: 700; padding: 8px 0 0; }
.logo { width: 1.3em; height: 1.3em; border-radius: 22%; flex: 0 0 auto; }
.install-tip { text-align: center; margin-top: 4px; }
@media (display-mode: standalone) { .install-tip { display: none; } }

/* ---------- right-to-left (Arabic) ---------- */
/* The two panels stay on the same physical sides as the court; only the text inside them flips. */
.board { direction: ltr; }
[dir="rtl"] .team, [dir="rtl"] .tiles .tile { direction: rtl; }
[dir="rtl"] body { font-family: system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif; }
.foot .btn, .tile .p { direction: ltr; unicode-bidi: isolate; }

/* ---------- Americano / Mexicano organiser (americano.html) ---------- */
body.org { overflow: auto; height: auto; min-height: 100%;
           padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom)); }
body.org .setup-card { gap: 12px; }
body.org section[hidden] { display: none; }
.back-link { color: var(--muted); font-weight: 700; text-decoration: none; align-self: flex-start; }
.seg-small span { min-height: 46px; font-size: .95rem; }
.seg[hidden] { display: none; }
.group-row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.group-row label.f { flex: 1 1 180px; }
.group-row .btn[hidden] { display: none; }
.player-rows { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.player-rows input { width: 100%; min-width: 0; }
.player-rows.pairs { grid-template-columns: minmax(0, 1fr); }
.pair-row { display: grid; grid-template-columns: 4.5em minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px; align-items: center; }
.pair-row span { color: var(--muted); font-weight: 700; font-size: .9rem; }
.pair-row b { color: var(--muted); }
.form-error { color: #ff8a8a; font-weight: 800; min-height: 1.2em; }
.btn.big { min-height: 64px; font-size: 1.25rem; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.org-top h1 { font-size: clamp(1.3rem, 5vw, 2rem); }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }
.status i { width: 10px; height: 10px; border-radius: 50%; background: #6b7690; flex: 0 0 auto; }
.status[data-s="on"] i { background: #35d07f; box-shadow: 0 0 8px #35d07f; }
.status[data-s="retry"] i { background: #ffb020; }
.round-nav { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; }
.round-nav h2 { text-align: center; font-size: 1.4rem; }
.round-nav .btn[disabled] { opacity: .35; }
.timer-line { text-align: center; font-size: 2.4rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.timer-line.up { color: #ffb020; }
.timer-line[hidden] { display: none; }
.court-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.court-card { padding: 14px; border-radius: 18px; background: var(--surface); border: 2px solid var(--line);
              display: flex; flex-direction: column; gap: 10px; }
.court-card.done { border-color: #35d07f66; }
.court-card h3 { margin: 0; }
.pairs-vs { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px; align-items: center;
            font-weight: 800; font-size: 1.1rem; }
.pairs-vs > div:last-child { text-align: end; }
.pairs-vs span { color: var(--muted); font-size: .85rem; }
.score-line { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 40px; }
.score-line b { font-size: 1.8rem; font-variant-numeric: tabular-nums; }
.score-line small { color: var(--muted); font-weight: 700; flex: 1; }
.score-line .btn { margin-inline-start: auto; }
.score-line.live small i { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #ff4d4d; }
.score-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.score-form input { width: 4.2em; text-align: center; font-size: 1.3rem; font-weight: 800; }
.org-actions { display: flex; flex-direction: column; gap: 8px; }
.org-actions.row { flex-direction: row; flex-wrap: wrap; }
.org-actions.row .btn { flex: 1 1 140px; }
.org-actions .btn[hidden] { display: none; }
.standings { font-variant-numeric: tabular-nums; }
.standings td:nth-child(n+3), .standings th:nth-child(n+3) { text-align: center; }
.standings tr.top1 td { color: #ffd24a; font-weight: 800; }
.standings tr.top2 td, .standings tr.top3 td { font-weight: 800; }
.court-qrs { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.court-qr { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.court-qr h3 { margin: 0; }
.court-qr .btn { text-decoration: none; display: inline-flex; align-items: center; }
body.org.viewer .org-only, body.org.viewer .org-actions:not(.row), body.ended #btnStartTimer { display: none; }
body.org #toast { position: fixed; }
@media (max-width: 420px) { .player-rows { grid-template-columns: minmax(0, 1fr); } }

/* ---------- court phone in an Americano session ---------- */
.court-banner { display: none; }
body.court .court-banner { display: flex; width: 100%; align-items: center; justify-content: space-between; gap: 10px;
  padding: 6px 12px; border-radius: 12px; background: #1b2646; border: 1px solid var(--line); font-weight: 800; }
body.court .court-banner .btn { min-height: 34px; }
body.court #history { display: none; }
body.race .games, body.race .sets { display: none; }
body.race .chip.serve { text-transform: none; }
.americano-link { display: flex; align-items: center; justify-content: center; text-decoration: none; }
body.race .chip.serve-on .txt { display: inline; max-width: 9em; overflow: hidden; text-overflow: ellipsis; }
body.court .court-banner { font-size: .9rem; }
#btnLeaveCourt { white-space: nowrap; }
