:root {
  color-scheme: dark;
  --bg: #0b1020;
  --card: rgba(15, 23, 42, 0.82);
  --card-2: rgba(30, 41, 59, 0.92);
  --text: #f8fafc;
  --muted: #cbd5e1;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --cell: #0f172a;
  --red: #ef4444;
  --yellow: #facc15;
  --win: #34d399;
  --line: rgba(148, 163, 184, 0.24);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100svh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(59, 130, 246, 0.35), transparent 34rem),
    radial-gradient(circle at bottom right, rgba(250, 204, 21, 0.18), transparent 28rem),
    var(--bg);
  display: grid;
  place-items: center;
  padding: 20px;
  overscroll-behavior: none;
}

.app { width: min(94vw, 820px); display: grid; gap: 18px; }
.hero, .panel, .board-wrap, .form-card {
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(14px);
  border-radius: 28px;
}
.hero { padding: clamp(22px, 5vw, 42px); text-align: center; }
.eyebrow { margin: 0 0 8px; letter-spacing: 0.2em; text-transform: uppercase; color: #93c5fd; font-size: 0.78rem; font-weight: 800; }
h1 { margin: 0; font-size: clamp(3rem, 12vw, 6.5rem); line-height: 0.92; }
h2 { margin: 0 0 14px; font-size: 1.15rem; }
.subtitle { margin: 18px auto 0; max-width: 62ch; color: var(--muted); font-size: clamp(1rem, 2.4vw, 1.18rem); }

.hidden { display: none !important; }
.lobby { padding: 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-card { padding: 22px; display: grid; gap: 10px; background: rgba(30, 41, 59, 0.62); }
label, .small-label, .me { color: var(--muted); font-weight: 800; font-size: 0.88rem; }
input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 14px;
  background: rgba(2, 6, 23, 0.65);
  color: var(--text);
  font: inherit;
  outline: none;
}
input:focus { border-color: #93c5fd; box-shadow: 0 0 0 4px rgba(147,197,253,.13); }
.code-input { text-transform: uppercase; letter-spacing: .28em; font-weight: 900; }
button {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  background: linear-gradient(135deg, #60a5fa, var(--blue));
  color: white;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(37, 99, 235, 0.32);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:hover { transform: translateY(-1px); }
button:active { transform: translateY(1px) scale(.985); }
button:disabled { opacity: .42; cursor: not-allowed; transform: none; }

.game { display: grid; gap: 18px; }
.panel { padding: 16px; }
.room-panel, .control-panel { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.room-code { font-size: clamp(2rem, 7vw, 3.8rem); font-weight: 950; letter-spacing: .16em; line-height: 1; }
a { color: #93c5fd; font-weight: 900; }
.players { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.player-pill { border: 1px solid var(--line); border-radius: 999px; padding: 10px 12px; background: rgba(2,6,23,.42); font-weight: 900; }
.player-pill.red::before, .player-pill.yellow::before { content: ''; display: inline-block; width: .75em; height: .75em; border-radius: 50%; margin-right: 8px; vertical-align: -1px; }
.player-pill.red::before { background: var(--red); }
.player-pill.yellow::before { background: var(--yellow); }
.status { font-weight: 900; font-size: clamp(1.05rem, 3vw, 1.35rem); }
.board-wrap { padding: clamp(12px, 3vw, 22px); }
.board {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: clamp(6px, 1.8vw, 12px);
  padding: clamp(10px, 2.8vw, 18px);
  background: linear-gradient(180deg, var(--blue), var(--blue-dark));
  border-radius: 24px;
  box-shadow: inset 0 -8px 0 rgba(15, 23, 42, 0.2);
  user-select: none;
  touch-action: manipulation;
}
.cell { aspect-ratio: 1; width: 100%; border-radius: 999px; border: none; background: var(--cell); box-shadow: inset 0 9px 16px rgba(0, 0, 0, 0.5), 0 3px 0 rgba(255,255,255,0.12); display: grid; place-items: center; padding: 0; }
.cell::before { content: ""; width: 82%; aspect-ratio: 1; border-radius: 999px; background: #020617; transition: transform 120ms ease, background 120ms ease, filter 120ms ease; }
.cell.red::before { background: radial-gradient(circle at 32% 28%, #fecaca, var(--red) 42%, #991b1b 100%); transform: scale(1.02); }
.cell.yellow::before { background: radial-gradient(circle at 32% 28%, #fef9c3, var(--yellow) 42%, #a16207 100%); transform: scale(1.02); }
.cell.win { outline: 4px solid var(--win); outline-offset: -3px; }
.cell:not(:disabled):active::before { transform: scale(.86); filter: brightness(1.25); }
.move-pending .cell.pending-lock::before { filter: saturate(.85); }
.my-turn .board { box-shadow: inset 0 -8px 0 rgba(15, 23, 42, 0.2), 0 0 0 3px rgba(52, 211, 153, .35), 0 0 32px rgba(52, 211, 153, .14); }
.hint { text-align: center; color: var(--muted); margin: 0; }
.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: rgba(15,23,42,.96); border: 1px solid var(--line); padding: 12px 16px; border-radius: 999px; box-shadow: 0 18px 50px rgba(0,0,0,.45); font-weight: 900; z-index: 20; }

@media (max-width: 680px) {
  body { padding: 10px; align-items: start; }
  .app { width: 100%; gap: 10px; }
  .lobby { grid-template-columns: 1fr; }
  .room-panel, .control-panel { flex-direction: column; align-items: stretch; text-align: center; }
  .players { justify-content: center; }
  button { width: 100%; }
}

@media (max-width: 680px) and (orientation: portrait) {
  body.playing {
    padding: max(6px, env(safe-area-inset-top)) 6px max(6px, env(safe-area-inset-bottom));
    place-items: start center;
    overflow-x: hidden;
  }
  body.playing .app { width: 100%; max-width: none; gap: 6px; }
  body.playing .hero {
    padding: 7px 10px;
    border-radius: 16px;
    box-shadow: none;
    background: rgba(15, 23, 42, .55);
  }
  body.playing .eyebrow,
  body.playing .subtitle { display: none; }
  body.playing h1 { font-size: clamp(1.3rem, 8vw, 2.05rem); letter-spacing: -.04em; }
  body.playing .game { gap: 6px; }
  body.playing .panel { padding: 8px 10px; border-radius: 16px; box-shadow: none; }
  body.playing .room-panel {
    display: grid;
    grid-template-columns: auto 1fr;
    text-align: left;
    align-items: center;
  }
  body.playing .small-label { display: none; }
  body.playing .room-code { font-size: clamp(1.15rem, 7vw, 1.85rem); letter-spacing: .12em; }
  body.playing #shareLink { font-size: .72rem; max-width: 46vw; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  body.playing .players { justify-content: end; gap: 5px; }
  body.playing .player-pill { padding: 6px 8px; font-size: .74rem; }
  body.playing .control-panel { display: grid; grid-template-columns: 1fr auto; align-items: center; text-align: left; }
  body.playing .status { font-size: clamp(.96rem, 4.4vw, 1.15rem); line-height: 1.08; }
  body.playing .me { font-size: .72rem; margin-top: 2px; }
  body.playing #reset { width: auto; padding: 8px 10px; font-size: .78rem; }
  body.playing .board-wrap {
    padding: 4px;
    border-radius: 18px;
    background: transparent;
    border: 0;
    box-shadow: none;
  }
  body.playing .board {
    width: min(98vw, calc((100svh - 154px) * 1.1667));
    max-width: 100%;
    margin: 0 auto;
    gap: clamp(5px, 1.25vw, 8px);
    padding: clamp(7px, 1.8vw, 10px);
    border-radius: 18px;
  }
  body.playing .cell { min-height: 0; box-shadow: inset 0 6px 11px rgba(0,0,0,.48), 0 2px 0 rgba(255,255,255,.12); }
  body.playing .hint { font-size: .82rem; line-height: 1.15; min-height: 1.1em; }
}

@media (max-height: 720px) and (max-width: 680px) and (orientation: portrait) {
  body.playing .hero { display: none; }
  body.playing .board { width: min(98vw, calc((100svh - 112px) * 1.1667)); }
  body.playing .hint { display: none; }
}
