:root {
  --ink: #f4f7fb;
  --dim: #a9b8cc;
  --navy: #0f1d33;
  --panel: rgba(12, 24, 44, 0.82);
  --ball: #e4ff3a;
  --hot: #ff6a3d;
  --cool: #38b8ff;
  --good: #7dff6a;
  --bad: #ff5a4a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--navy); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#fx { pointer-events: none; z-index: 2; }
.hidden { display: none !important; }
button {
  font: inherit; color: inherit; cursor: pointer; border: 0; touch-action: manipulation;
}

/* ---- HUD ---- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 3; }
#hud button { pointer-events: auto; }
#score {
  position: absolute; top: calc(10px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border-radius: 14px; padding: 6px 12px;
  font-weight: 800; box-shadow: 0 4px 18px rgba(0,0,0,.25); white-space: nowrap;
}
#score .side { display: flex; align-items: center; gap: 7px; }
#score .nm { font-size: 12px; letter-spacing: .08em; color: var(--dim); max-width: 22vw; overflow: hidden; text-overflow: ellipsis; }
#score b { font-size: 24px; min-width: 1.2em; text-align: center; font-variant-numeric: tabular-nums; }
#score .p b { color: var(--hot); }
#score .c b { color: var(--cool); }
#score .srv { width: 10px; height: 10px; border-radius: 50%; background: var(--ball); box-shadow: 0 0 8px var(--ball); visibility: hidden; }
#score .srv.on { visibility: visible; }
#call { font-size: 13px; color: var(--ball); padding: 2px 8px; border-left: 1px solid #ffffff22; border-right: 1px solid #ffffff22; letter-spacing: .05em; }
#rally {
  position: absolute; top: calc(62px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  font-weight: 800; font-size: 13px; letter-spacing: .12em; color: var(--ball); text-shadow: 0 2px 6px #0008;
  opacity: 0; transition: opacity .2s;
}
#rally.on { opacity: 1; }
.hbtn {
  position: absolute; top: calc(10px + var(--safe-t));
  background: var(--panel); border-radius: 12px; height: 44px; min-width: 44px; padding: 0 12px;
  font-weight: 800; font-size: 13px; letter-spacing: .06em;
}
#btnPause { left: calc(10px + var(--safe-l)); }
#btnDepth { right: calc(10px + var(--safe-r)); }
#btnDepth.at-net { background: var(--ball); color: #12210a; }
#flash {
  position: absolute; left: 50%; top: 36%; transform: translate(-50%, -50%) scale(.6);
  font-weight: 900; font-style: italic; font-size: clamp(30px, 8vw, 64px); letter-spacing: .02em;
  text-shadow: 0 4px 0 #0006, 0 0 24px #0005; opacity: 0; transition: transform .12s ease-out, opacity .12s;
}
#flash.on { transform: translate(-50%, -50%) scale(1); opacity: 1; }
#banner {
  position: absolute; left: 50%; top: 22%; transform: translateX(-50%); text-align: center;
  background: var(--panel); border-radius: 16px; padding: 10px 26px 12px; opacity: 0; transition: opacity .2s;
  min-width: 220px;
}
#banner.on { opacity: 1; }
#banner b { display: block; font-size: clamp(24px, 6vw, 40px); font-weight: 900; font-style: italic; }
#banner span { display: block; font-size: 14px; letter-spacing: .1em; color: var(--dim); font-weight: 700; margin-top: 2px; }
#banner.win b { color: var(--good); }
#banner.lose b { color: var(--bad); }
#prompt {
  position: absolute; left: 50%; bottom: calc(26px + var(--safe-b)); transform: translateX(-50%);
  font-weight: 800; font-size: 16px; letter-spacing: .08em; background: var(--panel); border-radius: 999px; padding: 10px 20px;
  opacity: 0; transition: opacity .25s; white-space: nowrap;
}
#prompt.on { opacity: 1; animation: nudge 1.2s ease-in-out infinite; }
@keyframes nudge { 50% { transform: translate(-50%, -6px); } }

/* narrow phones: the score drops below the corner buttons */
@media (max-width: 560px) {
  #score { top: calc(62px + var(--safe-t)); padding: 5px 10px; gap: 8px; }
  #score .nm { font-size: 11px; max-width: 26vw; }
  #score b { font-size: 21px; }
  #rally { top: calc(108px + var(--safe-t)); }
}

/* ---- panels ---- */
.panel {
  position: fixed; z-index: 5; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(92vw, 440px); max-height: calc(100% - 24px - var(--safe-t) - var(--safe-b)); overflow-y: auto;
  background: var(--panel); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-radius: 22px; padding: 22px 20px; text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,.4);
}
#title, #champ { top: auto; bottom: calc(14px + var(--safe-b)); transform: translateX(-50%); }
h1 { margin: 0; font-size: clamp(40px, 12vw, 64px); font-weight: 900; font-style: italic; letter-spacing: -.02em; line-height: .95; }
h1 span { color: var(--ball); }
h1 small { display: block; font-size: .3em; font-weight: 800; letter-spacing: .32em; font-style: normal; color: #cfe0f2; margin-bottom: .25em; }
h2 { margin: 0 0 12px; font-size: 30px; font-weight: 900; font-style: italic; }
.tag { margin: 6px 0 14px; color: var(--dim); font-size: 14px; }
.row { display: flex; gap: 8px; justify-content: center; margin: 10px 0; }
.picker { align-items: center; }
.picker .who { flex: 1; }
.picker small { display: block; font-size: 11px; letter-spacing: .14em; color: var(--dim); }
.picker b { font-size: 22px; font-weight: 900; }
.arrow { width: 48px; height: 48px; border-radius: 50%; background: #ffffff18; font-size: 30px; line-height: 1; }
.levels button { flex: 1; height: 44px; border-radius: 12px; background: #ffffff14; font-weight: 800; letter-spacing: .06em; font-size: 14px; }
.levels button.on { background: var(--ball); color: #12210a; }
.big {
  display: block; width: 100%; height: 56px; margin: 12px 0 4px; border-radius: 16px;
  background: var(--hot); color: #fff; font-size: 22px; font-weight: 900; font-style: italic; letter-spacing: .06em;
  box-shadow: 0 4px 0 #b8401f;
}
.big:active { transform: translateY(2px); box-shadow: 0 2px 0 #b8401f; }
.small button { flex: 1; height: 40px; border-radius: 12px; background: #ffffff14; font-weight: 700; font-size: 12px; letter-spacing: .08em; }
.ghost { margin-top: 10px; background: none; color: var(--dim); font-weight: 700; letter-spacing: .1em; font-size: 13px; padding: 10px; }
.record { margin: 8px 0 0; color: var(--dim); font-size: 12px; letter-spacing: .06em; min-height: 1em; }
#help ul { text-align: left; padding-left: 18px; margin: 0; font-size: 15px; line-height: 1.45; }
#help li { margin: 7px 0; }
#help li.keys { color: var(--dim); font-size: 13px; }
.final { font-size: 44px; font-weight: 900; margin: 0 0 8px; font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 8px; }
.stats div { background: #ffffff10; border-radius: 12px; padding: 8px 4px; }
.stats b { display: block; font-size: 22px; color: var(--ball); }
.stats small { font-size: 11px; letter-spacing: .08em; color: var(--dim); }
#resTitle.win { color: var(--good); }
#resTitle.lose { color: var(--bad); }

/* ---- tournament ---- */
#btnTour { height: auto; min-height: 56px; padding: 6px 0 8px; line-height: 1.1; }
#btnTour small { display: block; font-size: 11px; font-style: normal; font-weight: 800; letter-spacing: .14em; opacity: .85; margin-top: 3px; }
.quick { margin-top: 12px; padding-top: 4px; border-top: 1px solid #ffffff1a; }
.quick .levels { margin: 8px 0 6px; }
.quick .levels button { height: 38px; font-size: 12px; }
.mid {
  display: block; width: 100%; height: 42px; border-radius: 14px; background: #ffffff22;
  font-size: 15px; font-weight: 900; font-style: italic; letter-spacing: .08em;
}
#bracket { width: min(96vw, 540px); }
#brTitle { margin-bottom: 8px; }
.bracket { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: left; }
.bracket .col { display: flex; flex-direction: column; justify-content: space-around; gap: 8px; }
.bracket .hd { font-size: 10px; letter-spacing: .14em; color: var(--dim); font-weight: 800; text-align: center; }
.bracket .hd.now { color: var(--ball); }
.bracket .m { background: #ffffff10; border-radius: 10px; padding: 3px 0; border: 1px solid transparent; }
.bracket .m.mine { border-color: var(--hot); }
.bracket .pl { display: flex; justify-content: space-between; gap: 4px; padding: 3px 8px; font-size: 13px; font-weight: 700; color: var(--dim); }
.bracket .pl span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bracket .pl b { font-variant-numeric: tabular-nums; }
.bracket .pl.w { color: var(--ink); }
.bracket .pl.w b { color: var(--ball); }
.bracket .pl.me span { color: var(--hot); }
.bracket .pl.out { text-decoration: line-through; text-decoration-color: #ffffff55; }
.bracket .pl.tbd { color: #ffffff44; }
.brnext { margin: 12px 0 0; font-size: 14px; font-weight: 800; letter-spacing: .06em; }
.brnext em { font-style: normal; color: var(--hot); }
.brnext small { display: block; margin-top: 3px; color: var(--dim); font-size: 12px; font-weight: 700; letter-spacing: .1em; }
#btnTourQuit.arm { background: var(--bad); color: #fff; }
.crown { width: 64px; height: 40px; fill: var(--ball); filter: drop-shadow(0 0 10px #e4ff3a88); margin-bottom: 4px; }
#champ h1 { font-size: clamp(34px, 10vw, 52px); }
.chname { margin: 8px 0 6px; font-size: 22px; font-weight: 900; color: var(--hot); letter-spacing: .04em; }
.chpath { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 4px; }
.chpath div { background: #ffffff10; border-radius: 12px; padding: 8px 4px; }
.chpath b { display: block; font-size: 18px; color: var(--ball); font-variant-numeric: tabular-nums; }
.chpath small { display: block; font-size: 10px; letter-spacing: .08em; color: var(--dim); }

#loading {
  position: fixed; inset: 0; z-index: 9; display: flex; flex-direction: column; gap: 16px; align-items: center; justify-content: center;
  background: var(--navy); font-weight: 700; letter-spacing: .08em; color: var(--dim); transition: opacity .4s;
}
#loading.done { opacity: 0; pointer-events: none; }
.ballspin { width: 34px; height: 34px; border-radius: 50%; background: var(--ball); box-shadow: 0 0 24px var(--ball); animation: hop .7s ease-in-out infinite alternate; }
@keyframes hop { from { transform: translateY(-16px); } to { transform: translateY(16px) scaleY(.85); } }

/* landscape: the title panel goes to the right so the court shows */
@media (orientation: landscape) {
  #title, #champ { left: auto; right: calc(4vw + var(--safe-r)); bottom: auto; top: 50%; transform: translateY(-50%); width: min(40vw, 420px); }
}
@media (orientation: landscape) and (max-height: 520px) {
  #title, #champ { right: calc(12px + var(--safe-r)); width: min(46vw, 380px); padding: 14px; }
  .quick { margin-top: 6px; }
  .quick .levels { margin: 4px 0; }
  .quick .levels button, .mid { height: 34px; }
  #bracket { padding: 10px 14px; }
  #brTitle { font-size: 22px; margin-bottom: 4px; }
  .bracket .pl { padding: 1px 8px; font-size: 12px; }
  .bracket .col { gap: 4px; }
  .brnext { margin-top: 6px; }
  h1 { font-size: 38px; }
  .tag { margin: 4px 0 6px; }
  .big { height: 46px; margin: 8px 0 2px; font-size: 18px; }
  .panel { padding: 14px; }
  #help ul { font-size: 13px; }
  #flash { top: 40%; }
}
