:root {
  --bg: #0f1020;
  --bg-soft: #191a33;
  --line: #2c2e50;
  --text: #eceafd;
  --muted: #9a98c4;
  --accent: #7c6cff;
  --accent-2: #29d3b0;
  --danger: #ff5c7a;
  --gold: #ffd24a;
  --silver: #cfd6e6;
  --bronze: #e39b52;
  --radius: 16px;

  /* Fluid rhythm: everything scales with the viewport between phone and desktop. */
  --gutter: clamp(14px, 4vw, 32px);
  --gap: clamp(10px, 2.2vw, 16px);
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  min-height: 100svh;
  background: radial-gradient(1100px 620px at 50% -10%, #241f4d 0%, var(--bg) 60%);
  background-attachment: fixed;
  color: var(--text);
  font: 16px/1.5 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
  padding: env(safe-area-inset-top) var(--gutter) calc(24px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden; /* nothing should ever scroll the page sideways */
}

/* Single-column screens stay narrow and readable; the game view opts into more width. */
#app { width: 100%; max-width: 520px; margin: 0 auto; padding-top: clamp(18px, 3vh, 34px); }
#app:has(#view-game:not([hidden])) { max-width: 1000px; }
#app:has(#view-results:not([hidden])) { max-width: 640px; }

.view { animation: rise .35s cubic-bezier(.2,.8,.2,1); }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }

/* ---- entry ---- */
.brand { text-align: center; margin-bottom: 22px; }
.brand h1 {
  font-size: clamp(2.1rem, 9vw, 2.9rem); font-weight: 800; letter-spacing: -.02em;
  background: linear-gradient(100deg, var(--accent-2), var(--accent) 70%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.brand p { color: var(--muted); font-size: .95rem; margin-top: 6px; }

.card {
  background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px;
  box-shadow: 0 18px 50px -22px #000;
}

label { display: block; font-size: .8rem; color: var(--muted); margin: 0 0 6px; font-weight: 600; }

input {
  width: 100%; padding: 14px 16px; margin-bottom: 14px;
  background: #12132a; border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); font: inherit; font-size: 1rem; /* >=16px: iOS won't zoom */
  transition: border-color .15s, box-shadow .15s;
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px #7c6cff28; }
#code { text-transform: uppercase; letter-spacing: .5em; text-align: center; font-weight: 700; }

button {
  width: 100%; padding: 14px 18px; border: 1px solid var(--line); border-radius: 12px;
  background: #23244a; color: var(--text); font: inherit; font-weight: 700;
  cursor: pointer; transition: transform .08s, filter .15s, opacity .15s;
}
button:hover:not(:disabled) { filter: brightness(1.15); }
button:active:not(:disabled) { transform: scale(.98); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: linear-gradient(100deg, var(--accent), #9a63ff); border-color: transparent; }
button.danger { background: transparent; border-color: #4a2740; color: var(--danger); margin-top: 22px; }

.divider { position: relative; text-align: center; margin: 22px 0 16px; }
.divider::before { content: ""; position: absolute; inset: 50% 0 auto; height: 1px; background: var(--line); }
.divider span { position: relative; background: var(--bg-soft); padding: 0 12px; color: var(--muted); font-size: .8rem; }

.error { color: var(--danger); font-size: .875rem; min-height: 1.3em; margin-top: 10px; text-align: center; }
.muted { color: var(--muted); font-size: .85rem; }
.center { text-align: center; }

/* ---- lobby ---- */
.roomcode {
  font-size: clamp(2.4rem, 13vw, 3.2rem); font-weight: 800; text-align: center;
  letter-spacing: .28em; text-indent: .28em; color: var(--accent-2); margin: 4px 0 20px;
}
.players { list-style: none; margin-bottom: 20px; }
.players li {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; background: #12132a; border: 1px solid var(--line);
  border-radius: 12px; margin-bottom: 8px; font-weight: 600;
  animation: rise .3s backwards;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); flex: none; }
.tag { margin-left: auto; font-size: .7rem; color: var(--muted); font-weight: 700; letter-spacing: .06em; }

/* ---- game ---- */
.hud { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; color: var(--muted); font-size: .85rem; }
.hud b { color: var(--text); }
/* In one column the HUD reaches the right edge, where the fixed sound button sits. */
.hud-code { letter-spacing: .2em; font-weight: 700; margin-right: 52px; }
@media (min-width: 860px) { .hud-code { margin-right: 0; } }

/* One column by default. At >=860px the play stage and the leaderboard sit side by side,
   so on tablets/desktops the board is visible without scrolling past the input. */
.layout { display: grid; gap: clamp(18px, 3vw, 34px); align-items: start; }

@media (min-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1.15fr) minmax(300px, .85fr); }
  /* Align the side pane's first heading with the HUD row across the gutter. */
  .pane-side .section-title:first-child { margin-top: 0; }
  .stage { margin-bottom: 26px; }

  /* Lift the game toward the optical centre rather than stranding it against the top.
     Capped: past ~900px tall the block would drift too far down the page. */
  #view-game { display: flex; flex-direction: column; justify-content: center; min-height: min(72svh, 620px); }
}

.stage { text-align: center; margin-bottom: 20px; }
.letter {
  font-size: clamp(3.4rem, 18vw, 5.6rem); font-weight: 800; line-height: 1.15;
  text-transform: uppercase; margin: 2px 0 14px;
  /* A single glyph is narrow, so a text-sized gradient box samples only one slice of
     the ramp (it looked flat blue). Give it a wide fixed box and center the letter. */
  display: inline-block; min-width: 1.6em; max-width: 100%; padding-bottom: .06em;
  background: linear-gradient(100deg, var(--accent-2), var(--accent));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.letter.pop { animation: pop .4s cubic-bezier(.2,1.5,.4,1); }
@keyframes pop { from { transform: scale(.6); opacity: 0; } }

.timer {
  position: relative; margin: 0 auto 10px;
  width: clamp(92px, 26vw, 132px); height: clamp(92px, 26vw, 132px);
}
.timer svg { transform: rotate(-90deg); width: 100%; height: 100%; }
.timer circle { fill: none; stroke-width: 8; stroke-linecap: round; }
.timer-track { stroke: var(--line); }
.timer-arc { stroke: var(--accent-2); transition: stroke-dashoffset .3s linear, stroke .4s; }
.timer.low .timer-arc { stroke: var(--danger); }
.timer.low #timer-text { color: var(--danger); }
.timer.low { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.05); } }
#timer-text {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: clamp(1.6rem, 6vw, 2.2rem); font-weight: 800; font-variant-numeric: tabular-nums;
}

.turn-label { font-weight: 700; font-size: clamp(1rem, 3.4vw, 1.2rem); }
.turn-label.you { color: var(--accent-2); }

#play { display: flex; gap: 10px; max-width: 560px; margin: 0 auto; }
#play input { margin: 0; min-width: 0; flex: 1; } /* min-width:0 lets it shrink below its intrinsic size */
#play button { width: auto; flex: none; padding-inline: clamp(14px, 4vw, 24px); }
#play.disabled { opacity: .45; pointer-events: none; }

/* Very narrow phones (iPhone SE): stack the button under the field rather than squeeze both. */
@media (max-width: 340px) {
  #play { flex-direction: column; }
  #play button { width: 100%; }
}
#word.shake { animation: shake .35s; border-color: var(--danger); }
@keyframes shake { 25% { transform: translateX(-7px); } 75% { transform: translateX(7px); } }

.section-title { font-size: .78rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin: 26px 0 10px; }

.board { list-style: none; }
.board li {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 12px; margin-bottom: 8px;
  transition: border-color .3s, background .3s;
}
.board li.active { border-color: var(--accent); background: #221f47; }
.board li.gone { opacity: .45; }
.rank { width: 1.6em; font-weight: 800; color: var(--muted); flex: none; }
/* min-width:0 is what actually lets a long name ellipsis inside a flex row. */
.who { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.who .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pts { margin-left: auto; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--accent-2); flex: none; }
.pts.bump { animation: bump .5s; }
@keyframes bump { 40% { transform: scale(1.35); color: var(--gold); } }

.history { list-style: none; }
.history li {
  display: flex; align-items: baseline; gap: 10px; padding: 9px 14px;
  border-bottom: 1px solid var(--line); font-size: .9rem;
  animation: rise .3s;
}
.history .w { font-weight: 700; text-transform: capitalize; overflow: hidden; text-overflow: ellipsis; }
.history .by { color: var(--muted); font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.history .p { margin-left: auto; color: var(--accent-2); font-weight: 700; flex: none; padding-left: 8px; }

/* ---- results ---- */
.results-title { text-align: center; font-size: 1.8rem; font-weight: 800; margin-bottom: 24px; }
.podium { list-style: none; margin-bottom: 18px; }
.podium li {
  display: flex; align-items: center; gap: 14px; padding: 18px;
  border-radius: var(--radius); margin-bottom: 10px;
  background: var(--bg-soft); border: 1px solid var(--line);
  animation: rise .5s backwards;
}
.podium li:nth-child(1) { animation-delay: .05s; border-color: var(--gold); box-shadow: 0 0 40px -14px var(--gold); }
.podium li:nth-child(2) { animation-delay: .18s; border-color: var(--silver); }
.podium li:nth-child(3) { animation-delay: .31s; border-color: var(--bronze); }
.medal { font-size: 2rem; flex: none; }
.podium .who { font-size: 1.15rem; }
.podium .pts { font-size: 1.3rem; }
.sub { display: block; font-size: .75rem; color: var(--muted); font-weight: 500; }

/* ---- chrome ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 140%);
  background: #23244a; border: 1px solid var(--line); border-radius: 999px;
  padding: 11px 20px; font-weight: 600; font-size: .9rem;
  box-shadow: 0 14px 40px -12px #000; transition: transform .3s cubic-bezier(.2,.9,.3,1);
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); }
.toast.good { border-color: var(--accent-2); }
.toast.bad { border-color: var(--danger); }

.sound {
  position: fixed; top: calc(12px + env(safe-area-inset-top)); right: 12px;
  width: 42px; height: 42px; padding: 0; border-radius: 50%;
  background: #ffffff10; border-color: transparent; font-size: 1.05rem;
}
.sound.off { opacity: .4; }

/* Landscape phones: almost no vertical room. Sit the letter next to the ring and
   shrink the stage so the input stays above the fold. */
@media (max-height: 520px) and (orientation: landscape) and (max-width: 900px) {
  #app { padding-top: 10px; max-width: 900px; }
  .stage { display: flex; align-items: center; justify-content: center; gap: 22px; flex-wrap: wrap; margin-bottom: 14px; }
  .stage .muted { display: none; }
  .letter { font-size: 3.2rem; min-width: 1.3em; margin: 0; }
  .timer { width: 78px; height: 78px; margin: 0; }
  #timer-text { font-size: 1.4rem; }
  .turn-label { width: 100%; }
  .brand { margin-bottom: 12px; }
}

/* Big screens: a touch more breathing room, and cap line length in the podium. */
@media (min-width: 1200px) {
  #app:has(#view-game:not([hidden])) { max-width: 1080px; }
}

/* Coarse pointers (touch): enforce comfortable hit targets. */
@media (pointer: coarse) {
  button { min-height: 48px; }
  input { min-height: 48px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
