/* App layout and components. Theme tokens and primitives are in theme.css. */

:root[data-mode="dark"] { color-scheme: dark; }

/* The marks follow the theme: X in the brand ink, O in the warm status
   colour, both readable on the cells in all 14 combinations. The QR code is
   the fixed-meaning exception, like the footer heart: dark on light or it
   does not scan. */
:root {
  --mark-x: var(--brand-ink);
  --mark-o: var(--warn);
  --qr-light: #ffffff;
  --qr-dark: #121815;
}

h1, h2, h3 { font-weight: normal; }

:focus-visible {
  outline: 2px solid var(--brand-ink);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* The update bar, when there is one, then the page column. */
body {
  display: flex;
  flex-direction: column;
}

.page {
  flex: 1 0 auto;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 14px;
  padding-top: max(14px, env(safe-area-inset-top));
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px 10px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 7px;
}
.brand-name {
  font-size: 1.2rem;
  color: var(--brand-ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 22px 20px;
  text-align: center;
}
.card p {
  line-height: 1.5;
  color: var(--ink);
}

.foot {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}
.footer-heart {
  display: inline-flex;
  width: 15px;
  height: 15px;
  /* Fixed meaning: reads as a heart whatever the brand colour. */
  color: #34c759;
}
.footer-heart svg { width: 100%; height: 100%; }

/* Buttons. */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 12px;
  font-size: 0.85rem;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-primary {
  background: var(--brand-fill);
  color: var(--on-brand);
}
.btn-primary:hover { background: var(--brand-fill-strong); }
.btn-quiet {
  color: var(--ink);
  border: 1px solid var(--surface-border);
  background: var(--surface-strong);
}
.pill { border-radius: 999px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 11px;
  border-radius: 999px;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  font-size: 0.85rem;
  color: var(--ink);
}
.chip:empty { display: none; }
.chip-btn { cursor: pointer; letter-spacing: 0.03em; }

/* The game card. Panels swap with a class flip; the entrance is CSS. */
.game .hidden { display: none !important; }

.panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  animation: panel-in 0.2s ease;
}
@keyframes panel-in {
  from { opacity: 0; transform: translateY(6px); }
}
.panel-title {
  font-size: 1.3rem;
  color: var(--brand-ink);
}

/* New game. */
.setup { gap: 14px; }
.field-group {
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}
.field-label {
  font-size: 0.9rem;
  color: var(--ink);
}
.card .field-note {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
}
.field-note:empty { display: none; }
.mode-toggle.thirds { grid-template-columns: repeat(3, 1fr); }
.mode-btn[aria-checked="true"] {
  background: var(--brand-fill);
  color: var(--on-brand);
}
.mode-btn { color: var(--muted-strong); }

.level-pick {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface);
}
.level-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 7px 2px;
  border-radius: 10px;
  color: var(--muted-strong);
  transition: background 0.15s ease, color 0.15s ease;
}
.level-btn b {
  font-weight: normal;
  font-size: 1.1rem;
}
.level-btn small { font-size: 0.72rem; }
.level-btn[aria-checked="true"] {
  background: var(--brand-fill);
  color: var(--on-brand);
}

.start-btn {
  padding: 12px 26px;
  font-size: 1rem;
}
.start-btn svg { width: 18px; height: 18px; }
.join {
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 12px;
  border-top: 1px solid var(--surface-border);
  text-align: left;
}

/* Text fields: the seed, a join code, and the leaderboard name. */
.guess {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 460px;
}
.field {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--surface-strong);
  /* 3:1 against the card, as a field's edge must be. */
  border: 1px solid color-mix(in srgb, var(--ink) 45%, transparent);
  color: var(--ink);
  font-size: 1rem;
}
.field::placeholder { color: var(--muted-strong); opacity: 1; }
.seed-field,
.code-field { letter-spacing: 0.06em; text-transform: uppercase; }
.seed-field::placeholder,
.code-field::placeholder { letter-spacing: normal; text-transform: none; }
.field.shake { animation: shake 0.3s ease; }
@keyframes shake {
  25% { transform: translateX(-6px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
}
.guess .btn {
  padding: 10px 16px;
  font-size: 0.95rem;
}

/* The game. */
.play { gap: 10px; }
.game-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

/* Whose turn: the two players side by side, with a tint that slides to
   whoever is to move, and rests on the winner at the end. */
.players {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: min(100%, 420px);
  padding: 4px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
}
.player {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
  padding: 8px 6px;
  font-size: 0.9rem;
  color: var(--muted-strong);
  transition: color 0.2s ease;
}
.player > span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.player-mark {
  display: inline-flex;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}
.player-mark .mark { width: 100%; height: 100%; stroke-width: 13; }
.player-mark .mark path,
.player-mark .mark circle { animation: none; }
.players-slider {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc(50% - 4px);
  border-radius: 10px;
  background: var(--brand-fill);
  transition: transform 0.25s ease, opacity 0.2s ease;
}
.players[data-turn="o"] .players-slider { transform: translateX(100%); }
.players[data-turn="none"] .players-slider { opacity: 0; }
.players[data-turn="x"] .player[data-side="x"],
.players[data-turn="o"] .player[data-side="o"] { color: var(--on-brand); }
/* On the brand tint, the marks take the tint's own ink. */
.players[data-turn="x"] .player[data-side="x"] .mark,
.players[data-turn="o"] .player[data-side="o"] .mark { stroke: var(--on-brand); }

/* The board: a square grid of cells, 3 to 5 a side. */
.board {
  position: relative;
  width: min(100%, 420px);
  aspect-ratio: 1;
}
.board .grid {
  display: grid;
  grid-template-columns: repeat(var(--size, 3), 1fr);
  grid-template-rows: repeat(var(--size, 3), 1fr);
  gap: 8px;
  width: 100%;
  height: 100%;
}
.board[data-size="5"] .grid { gap: 6px; }
.cell {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 16px;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  box-shadow: var(--shadow);
  cursor: default;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
.board[data-size="5"] .cell { border-radius: 12px; }
.board.live .cell[aria-disabled="false"] { cursor: pointer; }
.board.live .cell[aria-disabled="false"]:hover {
  background: var(--brand-fill);
  transform: translateY(-1px);
}
.cell:focus-visible {
  outline: 3px solid var(--brand-ink);
  outline-offset: 2px;
  z-index: 2;
}
/* The last move: a ring, so it can be found at a glance. */
.cell.last { border-color: color-mix(in srgb, var(--ink) 50%, transparent); }
.cell.win { background: var(--brand-fill-strong); }
.cell.win .mark { stroke: var(--on-brand); }

/* Hints, when the setting is on: a dot where the side to move wins, a
   dashed ring where it has to block. */
.cell.hint-win::after,
.cell.hint-block::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  animation: fade-in 0.2s ease;
}
.cell.hint-win::after {
  inset: 36%;
  background: var(--ok);
}
.cell.hint-block::after {
  inset: 22%;
  border: 3px dashed var(--warn);
}
@keyframes fade-in {
  from { opacity: 0; }
}

.mark {
  width: 70%;
  height: 70%;
  fill: none;
  stroke-width: 11;
  stroke-linecap: round;
  overflow: visible;
}
.mark-x { stroke: var(--mark-x); }
.mark-o { stroke: var(--mark-o); }
/* Each mark draws itself in; the second stroke of an X follows the first. */
.mark path,
.mark circle {
  stroke-dasharray: 1;
  animation: draw 0.22s ease-out both;
}
.mark-x path:nth-child(2) { animation-delay: 0.1s; }
@keyframes draw {
  from { stroke-dashoffset: 1; }
  to { stroke-dashoffset: 0; }
}

/* The stroke through a winning line. */
.strike {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}
/* It crosses the winning cells, which sit on the brand fill. */
.strike line {
  stroke: var(--on-brand);
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-dasharray: 1;
  opacity: 0.75;
  animation: draw 0.35s ease-out 0.15s both;
}

.card .status {
  min-height: 1.5em;
  font-size: 0.95rem;
  color: var(--ink);
}

.game-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.game-actions .btn { padding: 9px 15px; }
#resignBtn.armed,
#leaveBtn.armed {
  color: var(--error);
  border-color: var(--error);
}
.undo-note { max-width: 460px; }

/* Network. */
.net-bar {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--surface-strong);
  font-size: 0.85rem;
  color: var(--ink);
}
.net-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--busy);
}
.net-bar[data-tone="ok"] .net-dot { background: var(--ok); }
.net-bar[data-tone="warn"] .net-dot { background: var(--warn); }
.net-bar[data-tone="error"] .net-dot { background: var(--error); }

.takeback {
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--warn) 14%, transparent);
  animation: panel-in 0.2s ease;
}
.card .takeback p { color: var(--warn); }

.net .net-lead { color: var(--muted); max-width: 30em; }
.code-big {
  font-size: 2.4rem;
  letter-spacing: 0.22em;
  color: var(--brand-ink);
  user-select: text;
}
.qr svg {
  width: 180px;
  height: 180px;
  display: block;
  margin: 0 auto;
  border-radius: 12px;
}
.qr .qr-bg { fill: var(--qr-light); }
.qr .qr-fg { fill: var(--qr-dark); }
.card .net-status { min-height: 1.5em; color: var(--ink); }
.card .net-status.error { color: var(--error); }

/* The replay. */
.replay {
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  animation: panel-in 0.2s ease;
}
.replay-controls {
  display: flex;
  gap: 8px;
}
.replay-controls .play-btn {
  background: var(--brand-fill);
  color: var(--on-brand);
}
.speed-pick {
  grid-template-columns: repeat(4, 1fr);
  width: min(100%, 280px);
}
.scrub {
  width: 100%;
  accent-color: var(--brand-ink);
}
.card .replay-label {
  font-size: 0.85rem;
  color: var(--muted);
}
.move-list {
  list-style: none;
  width: 100%;
  max-height: 132px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 2px 6px;
  padding: 6px;
  border-radius: 12px;
  background: var(--surface);
  text-align: left;
}
.ply-btn {
  width: 100%;
  padding: 3px 8px;
  border-radius: 6px;
  text-align: left;
  color: var(--ink);
  font-size: 0.85rem;
}
.ply-num { color: var(--muted); }
.ply-btn.current {
  background: var(--brand-fill);
  color: var(--on-brand);
}
.ply-btn.current .ply-num { color: var(--on-brand); }

/* A finished game. */
.result {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  padding-top: 14px;
  border-top: 1px solid var(--surface-border);
  animation: panel-in 0.2s ease;
}
.result-title {
  font-size: 1.7rem;
  color: var(--brand-ink);
}
.result-title:focus { outline: none; }
.card .result-reason { color: var(--muted); }
.card .result-score { font-size: 1.05rem; max-width: 32em; }
.seed-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.seed-text {
  letter-spacing: 0.06em;
  color: var(--ink);
  user-select: text;
}
.submit {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-top: 4px;
}
.submit-label {
  font-size: 0.95rem;
  color: var(--ink);
}
.card .submit-msg {
  min-height: 1.2em;
  font-size: 0.85rem;
  color: var(--error);
}
.card .submit-note,
.card .not-scored {
  font-size: 0.8rem;
  color: var(--muted);
  max-width: 30em;
}
.submitted {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 460px;
  padding: 10px 14px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--ok) 14%, transparent);
  text-align: left;
}
.card .submitted p { color: var(--ok); }
.ok-icon {
  display: inline-flex;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--ok);
}
.ok-icon svg { width: 100%; height: 100%; }

.round-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 4px;
}
.round-actions .btn { padding: 9px 16px; }

/* Confetti for a win, over everything and never in the way. */
.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 70;
  pointer-events: none;
}

/* Settings modal. */
.modal .hidden { display: none !important; }
.saved-name {
  font-size: 0.95rem;
  color: var(--ink);
}
.modal .guess { max-width: none; }
.clear-name { align-self: flex-start; }

.setting-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  font-size: 0.9rem;
  color: var(--ink);
  text-align: left;
}
.setting-toggle > span:first-child { flex: 1; }
.switch-state {
  font-size: 0.8rem;
  color: var(--muted-strong);
}
.switch {
  position: relative;
  flex-shrink: 0;
  width: 40px;
  height: 24px;
  border-radius: 999px;
  background: var(--surface-strong);
  border: 1px solid color-mix(in srgb, var(--ink) 45%, transparent);
  transition: background 0.18s ease, border-color 0.18s ease;
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--muted-strong);
  transition: transform 0.18s ease, background 0.18s ease;
}
.setting-toggle[aria-checked="true"] .switch {
  background: var(--brand-ink);
  border-color: var(--brand-ink);
}
.setting-toggle[aria-checked="true"] .switch::after {
  background: var(--bg);
  transform: translateX(16px);
}

/* Leaderboard modal. */
.board-body {
  min-height: 120px;
  max-height: 50vh;
  overflow-y: auto;
  transition: opacity 0.15s ease;
}
.board-body[aria-busy="true"] { opacity: 0.6; }
.board-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.board-table th {
  padding: 6px 8px;
  text-align: left;
  font-weight: normal;
  font-size: 0.8rem;
  color: var(--muted-strong);
}
.board-table td {
  padding: 7px 8px;
  border-top: 1px solid var(--surface-border);
  color: var(--ink);
  overflow-wrap: anywhere;
}
.board-table th:first-child,
.board-table td:first-child { width: 2.5em; color: var(--muted-strong); }
.board-empty {
  padding: 32px 8px;
  text-align: center;
  color: var(--muted-strong);
}

/* Update bar, per update-bar-spec.md. Quieter than the header, full width,
   tinted enough to read as a notice. */
.update-notice {
  flex: none;
  background: color-mix(in srgb, var(--brand-ink) 12%, var(--bg));
  border-bottom: 1px solid var(--surface-border);
  color: var(--ink);
  font-size: 0.875rem;
}
.update-notice-inner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: 720px;
  margin: 0 auto;
  padding: 0.5rem 1rem;
  /* Under the notch on a phone, where the bar is the topmost thing on screen. */
  padding-top: max(0.5rem, env(safe-area-inset-top));
}
.update-notice p { flex: 1 1 auto; min-width: 0; }
.update-notice .btn {
  flex: none;
  padding: 6px 12px;
}

/* With the bar showing, it owns the notch and the page does not pad for it. */
.update-notice + .page { padding-top: 14px; }

@media (max-width: 480px) {
  .page {
    padding: 10px;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .update-notice + .page { padding-top: 10px; }
  .topbar { padding: 8px 8px 8px 12px; }
  /* Four header buttons leave no room for the name on a phone; the icon
     stays, and the heading is still read out. */
  .brand-name {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
  .topbar-actions { gap: 6px; }
  .game { padding: 16px 12px; }
  .board .grid { gap: 6px; }
  .board[data-size="5"] .grid { gap: 4px; }
  .cell { border-radius: 12px; }
  .board[data-size="5"] .cell { border-radius: 9px; }
  .level-btn small { font-size: 0.62rem; }
  .mode-toggle.thirds .mode-btn { font-size: 0.78rem; padding: 8px 4px; }
  .mode-toggle.thirds .mode-btn svg { display: none; }
  .result-title { font-size: 1.45rem; }
  .code-big { font-size: 2rem; }
  .update-notice-inner { flex-wrap: wrap; }
  .update-notice p { flex-basis: 100%; }
}
