/* ------------------------------------------------------------------ tokens */
:root {
  --bg:        #0e0c15;
  --bg-glow-1: #2a1c4d;
  --bg-glow-2: #0d2f3e;
  --surface:   #191527;
  --surface-2: #221c35;
  --line:      #322a4a;
  --text:      #ece9f5;
  --muted:     #9a93b3;
  --accent:    #f5c451;
  --accent-2:  #7c5cff;
  --hit:       #35d6a4;
  --danger:    #ff6b7a;
  --radius:    14px;
  --shadow:    0 18px 40px -18px rgba(0,0,0,.75);
  --font:      'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;

  --pad-x:     1rem;
  --pad-top:   max(1rem, env(safe-area-inset-top));
  --pad-bot:   calc(1.2rem + env(safe-area-inset-bottom));
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html {
  /* Stops iOS/Android from inflating our type in landscape. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  /* Two soft off-screen glows keep the flat dark background from reading as a
     void, without costing an image request. */
  background-image:
    radial-gradient(60rem 40rem at 15% -10%, var(--bg-glow-1) 0%, transparent 60%),
    radial-gradient(50rem 35rem at 110% 110%, var(--bg-glow-2) 0%, transparent 60%);
  background-attachment: fixed;
  padding: var(--pad-top) var(--pad-x) var(--pad-bot);
  display: flex;
  justify-content: center;
  /* No rubber-band / pull-to-refresh: an errant swipe while hunting for a
     square should not reload the page mid-game. */
  overscroll-behavior-y: contain;
}

/* Every interactive element opts out of double-tap-to-zoom, which otherwise
   costs ~300ms on every tap on some mobile browsers. */
button, input, .cell, .word { touch-action: manipulation; }

#app { width: 100%; max-width: 30rem; }
.screen { display: flex; flex-direction: column; gap: 1.25rem; }
.screen[hidden] { display: none; }
@media (min-width: 720px) { #app:has(.wide:not([hidden])) { max-width: 46rem; } }

/* ------------------------------------------------------------------ branding */
.brand { text-align: center; padding-top: 1rem; }

.brand-mark {
  width: 3.5rem; height: 3.5rem; margin: 0 auto .85rem;
  /* flex, not grid: grid would treat the "B" text node and the <span>!</span>
     as two items and stack them vertically. */
  display: flex; align-items: center; justify-content: center;
  font-size: 1.65rem; font-weight: 800; letter-spacing: -.04em;
  color: #1b1526;
  background: linear-gradient(145deg, var(--accent), #e0952f);
  border-radius: 1.1rem;
  box-shadow: 0 10px 26px -8px rgba(245,196,81,.55);
  transform: rotate(-6deg);
}
.brand-mark span { color: var(--accent-2); }

.brand h1 {
  font-size: clamp(2.1rem, 10.5vw, 3rem);
  line-height: .95;
  letter-spacing: -.035em;
  font-weight: 800;
}
.tagline { color: var(--muted); margin-top: .75rem; font-size: .93rem; line-height: 1.5; }

/* --------------------------------------------------------------------- card */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.2rem;
  display: flex; flex-direction: column; gap: .95rem;
}
.card.center { text-align: center; align-items: center; }
.card h2 { font-size: 1.25rem; letter-spacing: -.02em; }

.muted { color: var(--muted); font-size: .9rem; line-height: 1.5; }
.center-text { text-align: center; }
.hint { color: var(--muted); font-size: .8rem; min-height: 1.1em; word-break: break-all; }

/* ------------------------------------------------------------------- inputs */
.field { display: flex; flex-direction: column; gap: .45rem; text-align: left; }
.field span { font-size: .76rem; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); }

input {
  /* Must stay >=16px: anything smaller makes iOS Safari zoom the whole page
     when the field takes focus, and it never zooms back out. */
  font-family: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .85rem .9rem;
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
input:focus { outline: none; border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(124,92,255,.22); }
input::placeholder { color: #5f587a; }

.join-row { display: flex; gap: .6rem; }
#joinCode {
  flex: 1; min-width: 0;
  text-transform: uppercase; letter-spacing: .3em; text-indent: .3em;
  text-align: center; font-weight: 700; font-size: 18px;
}
.join-row .btn { flex: 0 0 auto; }

.divider { display: flex; align-items: center; gap: .8rem; color: var(--muted); font-size: .78rem; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }

/* ------------------------------------------------------------------ buttons */
.btn {
  font-family: inherit; font-size: 1rem; font-weight: 650;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: .85rem 1.15rem;
  cursor: pointer;
  transition: transform .12s, filter .15s, background .15s;
  /* 48px: the smallest target that is comfortable with a thumb. */
  min-height: 3rem;
}
.btn:active:not(:disabled) { transform: scale(.975); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary {
  background: linear-gradient(145deg, var(--accent), #e2a232);
  color: #1b1526;
  box-shadow: 0 10px 24px -12px rgba(245,196,81,.7);
}
.btn-ghost { background: var(--surface-2); color: var(--text); border-color: var(--line); }

/* Hover styling only where a real pointer exists - on a touch screen it
   sticks after a tap and makes buttons look permanently pressed. */
@media (hover: hover) {
  .btn-primary:hover:not(:disabled) { filter: brightness(1.07); }
  .btn-ghost:hover:not(:disabled) { background: #2b2440; }
}

.err { color: var(--danger); font-size: .88rem; text-align: center; }
.err[hidden] { display: none; }

/* -------------------------------------------------------------------- lobby */
.pulse-dot {
  width: .8rem; height: .8rem; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(245,196,81,.6);
  animation: pulse 1.8s infinite;
}
@keyframes pulse {
  70%  { box-shadow: 0 0 0 .9rem rgba(245,196,81,0); }
  100% { box-shadow: 0 0 0 0 rgba(245,196,81,0); }
}

.code-display {
  font-size: clamp(2.3rem, 14vw, 3.4rem);
  font-weight: 800; letter-spacing: .2em; text-indent: .2em;
  color: var(--accent);
  background: var(--bg);
  border: 1px dashed var(--line);
  border-radius: 12px;
  padding: .7rem .5rem;
  width: 100%;
  user-select: all;               /* long-press selects the whole code */
}

/* ---------------------------------------------------------------- selection */
.topbar { display: flex; align-items: flex-start; justify-content: space-between; gap: .9rem; }
.topbar h2 { font-size: 1.3rem; letter-spacing: -.02em; }

.counter {
  flex-shrink: 0;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; padding: .4rem .85rem;
  font-size: .88rem; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.counter b { color: var(--accent); font-size: 1.05rem; }
.counter.full b { color: var(--hit); }

.word-grid {
  display: grid;
  /* Two comfortable columns on a phone, more as the screen grows. */
  grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr));
  gap: .5rem;
  padding-bottom: .25rem;
}

.word {
  font-family: inherit; text-align: left;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .7rem .75rem;
  cursor: pointer;
  line-height: 1.25;
  font-size: .87rem;
  transition: background .13s, border-color .13s, transform .1s;
  display: flex; align-items: center; gap: .5rem;
  min-height: 3.1rem;
}
.word::before {
  content: ''; flex-shrink: 0;
  width: 1rem; height: 1rem;
  border: 1.5px solid var(--line);
  border-radius: 5px;
  transition: all .13s;
}
.word:active:not(:disabled) { transform: scale(.97); }
.word[aria-pressed="true"] {
  background: rgba(124,92,255,.16);
  border-color: var(--accent-2);
}
.word[aria-pressed="true"]::before {
  background: var(--accent-2); border-color: var(--accent-2);
  box-shadow: inset 0 0 0 2px var(--surface);
}
.word:disabled { opacity: .38; cursor: not-allowed; }

.sticky-actions {
  position: sticky;
  bottom: calc(env(safe-area-inset-bottom) * -1 - .2rem);
  display: flex; flex-direction: column; gap: .55rem;
  padding: .8rem 0 var(--pad-bot);
  /* Opaque at the bottom so the buttons never sit on top of word text. */
  background: linear-gradient(to top, var(--bg) 0%, var(--bg) 55%, transparent 100%);
  margin-bottom: calc(var(--pad-bot) * -1);
}
.opp-status { color: var(--muted); font-size: .84rem; text-align: center; min-height: 1.2em; }
.opp-status.ready { color: var(--hit); }

/* --------------------------------------------------------------------- play */
[data-screen="play"] {
  /* Fill the viewport and centre the card, so the board sits under the thumb
     rather than at the top of a mostly-empty screen. */
  min-height: calc(100dvh - var(--pad-top) - var(--pad-bot));
  justify-content: center;
  gap: 1rem;
}

.scoreline {
  display: flex; align-items: center; justify-content: space-between;
  gap: .7rem;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; padding: .55rem .95rem;
  font-size: .9rem;
}
.who { display: flex; align-items: center; gap: .4rem; min-width: 0; flex: 1; }
.who.opp { justify-content: flex-end; }
.who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .dot { width: .55rem; height: .55rem; border-radius: 50%; flex-shrink: 0; }
.who.me .dot { background: var(--accent); }
.who.opp .dot { background: #4d4666; transition: background .2s; }
.who.opp .dot.online { background: var(--hit); }
.vs {
  color: var(--muted); font-size: .72rem; text-transform: uppercase;
  letter-spacing: .08em; flex-shrink: 0;
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: .5rem;
  /* The board is square, so clamping its width also clamps its height. This
     is what guarantees the whole card is reachable without scrolling, in
     portrait and landscape alike. */
  width: min(100%, calc(100dvh - 15.5rem));
  margin-inline: auto;
}

.cell {
  font-family: inherit; font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  aspect-ratio: 1;
  padding: .35rem .25rem;
  cursor: pointer;
  display: grid; place-items: center; text-align: center;
  /* cqw keeps long entries like "Autocomplete on steroids" legible: the type
     scales with the square itself, not with the viewport. */
  container-type: inline-size;
  line-height: 1.15;
  overflow: hidden;
  overflow-wrap: anywhere;
  position: relative;
  transition: background .15s, border-color .15s, transform .1s, color .15s;
}
.cell > span { font-size: clamp(.6rem, 13cqw, .92rem); }
.cell:active { transform: scale(.96); }

.cell.hit {
  background: linear-gradient(150deg, rgba(53,214,164,.24), rgba(53,214,164,.1));
  border-color: var(--hit);
  color: #d5fff1;
}
/* A struck square gets a diagonal slash, the way you would cross a paper card. */
.cell.hit::after {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(to top left,
    transparent calc(50% - 1px), rgba(53,214,164,.55) 50%, transparent calc(50% + 1px));
}
.cell.win {
  background: linear-gradient(150deg, rgba(245,196,81,.32), rgba(245,196,81,.14));
  border-color: var(--accent);
  color: #fff3d4;
  animation: pop .45s ease-out;
}
.cell.win::after { background: linear-gradient(to top left,
  transparent calc(50% - 1px), rgba(245,196,81,.65) 50%, transparent calc(50% + 1px)); }
@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.09); } 100% { transform: scale(1); } }

.board.small { width: min(100%, 17.5rem); gap: .4rem; }
.board.small .cell { cursor: default; border-radius: 9px; }
.muted-board .cell { opacity: .82; }

/* Opponent progress: shape only, no words, until the round is over. */
.opp-track {
  display: flex; align-items: center; justify-content: center; gap: .7rem;
  color: var(--muted); font-size: .8rem;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; padding: .5rem .95rem;
  font-variant-numeric: tabular-nums;
}
.mini { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; flex: 0 0 auto; }
.mini i {
  width: .42rem; height: .42rem; border-radius: 1.5px;
  background: #3a3352; transition: background .2s;
}
.mini i.on { background: var(--hit); }

/* --------------------------------------------------------------------- done */
.verdict {
  font-size: clamp(2.3rem, 13vw, 3.4rem);
  font-weight: 800; letter-spacing: -.02em;
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text;
  color: transparent;
  animation: pop .5s ease-out;
}
.verdict.lost { background: linear-gradient(120deg, #6f6890, #4d4666); -webkit-background-clip: text; background-clip: text; }
.score { color: var(--muted); font-size: .95rem; }
.score b { color: var(--text); font-size: 1.1rem; }
.reveal { width: 100%; display: flex; flex-direction: column; gap: .55rem; }
.reveal h3 { font-size: .76rem; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); font-weight: 600; }
[data-screen="done"] .btn { width: 100%; }

/* -------------------------------------------------------------------- toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(1.1rem + env(safe-area-inset-bottom));
  max-width: calc(100vw - 2rem);
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); font-size: .87rem; text-align: center;
  padding: .6rem 1rem; border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 10;
}
.toast[hidden] { display: none; }

/* --------------------------------------------------- short / landscape phones
   Trim the chrome so the 3x3 card still fits between the notch and the home
   indicator when the phone is on its side. */
@media (max-height: 560px) {
  :root { --pad-top: max(.6rem, env(safe-area-inset-top)); --pad-bot: calc(.7rem + env(safe-area-inset-bottom)); }
  .screen { gap: .7rem; }
  [data-screen="play"] .center-text { display: none; }
  .board { width: min(100%, calc(100dvh - 9rem)); gap: .4rem; }
  /* Keep the pills the same width as the height-clamped board. */
  [data-screen="play"] .scoreline,
  [data-screen="play"] .opp-track {
    width: min(100%, calc(100dvh - 9rem)); margin-inline: auto;
  }
  .brand { padding-top: 0; }
  .brand h1 { font-size: 1.9rem; }
  .tagline { display: none; }
}

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