:root {
  color-scheme: light;
  --page: #f6f3ea;
  --panel: #fffdf8;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #d8d1c2;
  --shadow: 0 14px 38px rgba(30, 41, 59, 0.11);
  --x: #f97316;
  --x-soft: #ffedd5;
  --o: #dc2626;
  --o-soft: #fee2e2;
  --win: #15803d;
  --win-soft: #dcfce7;
  --hint: #facc15;
  --hint-soft: #fef9c3;
  --radius: 8px;
  --page-pad: clamp(10px, 1.5vw, 18px);
  --shell-gap: clamp(8px, 1.2vw, 14px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background:
    linear-gradient(120deg, rgba(23, 105, 170, 0.1), transparent 32%),
    linear-gradient(240deg, rgba(194, 65, 12, 0.11), transparent 34%),
    var(--page);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: 100vw;
  height: 100svh;
  margin: 0 auto;
  padding: max(var(--page-pad), env(safe-area-inset-top)) max(var(--page-pad), env(safe-area-inset-right)) max(var(--page-pad), env(safe-area-inset-bottom)) max(var(--page-pad), env(safe-area-inset-left));
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  gap: var(--shell-gap);
  overflow: hidden;
}

.top-panel,
.controls,
.status-card {
  background: rgba(255, 253, 248, 0.9);
  border: 1px solid rgba(216, 209, 194, 0.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.top-panel {
  display: grid;
  grid-template-columns: 1fr minmax(430px, 0.9fr);
  gap: 14px;
  align-items: center;
  padding: 14px;
}

.eyebrow,
.status-kicker,
.control-label,
.score-label {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 4px 0 0;
  font-size: clamp(1.6rem, 3.4vw, 2.6rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.scoreboard {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.score-tile {
  min-height: 74px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  display: grid;
  grid-template-columns: 34px 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 8px;
}

.score-tile svg {
  width: 31px;
  height: 31px;
  stroke-width: 2.7;
}

.score-tile:first-child svg {
  color: var(--x);
}

.score-tile:nth-child(2) svg {
  color: var(--o);
}

.score-tile strong {
  grid-column: 1 / -1;
  margin-top: 4px;
  font-size: clamp(1.75rem, 3.2vw, 2.25rem);
  line-height: 1;
}

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

.starter-control {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.segmented {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.starter-button,
.icon-button {
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0 18px;
  font-weight: 800;
  cursor: pointer;
}

.starter-button {
  min-width: 128px;
}

.starter-button svg,
.icon-button svg {
  width: 26px;
  height: 26px;
  stroke-width: 2.7;
  flex: 0 0 auto;
}

.starter-button.is-active[data-starter="X"] {
  border-color: rgba(23, 105, 170, 0.36);
  background: var(--x-soft);
  color: var(--x);
}

.starter-button.is-active[data-starter="O"] {
  border-color: rgba(194, 65, 12, 0.36);
  background: var(--o-soft);
  color: var(--o);
}

.status-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
}

.turn-indicator {
  width: 48px;
  height: 48px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--x-soft);
  color: var(--x);
  flex: 0 0 auto;
}

.turn-indicator.is-o {
  background: var(--o-soft);
  color: var(--o);
}

.turn-indicator.is-win {
  background: var(--win-soft);
  color: var(--win);
}

.turn-indicator svg {
  width: 31px;
  height: 31px;
  stroke-width: 2.8;
}

.status-text {
  margin: 2px 0 0;
  font-size: clamp(1rem, 2.2vw, 1.25rem);
  font-weight: 850;
  line-height: 1.2;
}

.board-wrap {
  min-height: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: grid;
  place-items: center;
}

.board {
  width: var(--board-size, min(92vw, 62svh, 720px));
  height: var(--board-size, min(92vw, 62svh, 720px));
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  grid-auto-rows: 1fr;
  gap: clamp(8px, 1.2vw, 13px);
  padding: clamp(8px, 1.2vw, 13px);
  border-radius: var(--radius);
  background: #263241;
  box-shadow: 0 16px 42px rgba(15, 23, 42, 0.19);
}

.cell {
  appearance: none;
  border: 0;
  border-radius: var(--radius);
  background: #fffdf8;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  line-height: 0;
  contain: layout paint;
  transition: box-shadow 120ms ease;
}

.cell:focus-visible,
.starter-button:focus-visible,
.icon-button:focus-visible {
  outline: 4px solid rgba(23, 105, 170, 0.34);
  outline-offset: 3px;
}

.cell:not(:disabled):hover {
  background: #f8fafc;
}

.cell[data-player="X"] {
  color: var(--x);
  background: var(--x-soft);
}

.cell[data-player="O"] {
  color: var(--o);
  background: var(--o-soft);
}

.board .cell.is-winning {
  color: var(--win);
  background: #bbf7d0;
  background-color: #bbf7d0;
  box-shadow: inset 0 0 0 7px rgba(21, 128, 61, 0.28);
  transition: none;
}

.board .cell.is-hint {
  background: var(--hint-soft);
  background-color: var(--hint-soft);
  box-shadow: inset 0 0 0 7px rgba(250, 204, 21, 0.55), 0 0 0 4px rgba(250, 204, 21, 0.18);
  transition: none;
}

.cell.is-hint[data-hint-player="X"] {
  color: var(--x);
}

.cell.is-hint[data-hint-player="O"] {
  color: var(--o);
}

.cell svg {
  width: 68%;
  height: 68%;
  display: block;
  stroke-width: 2.55;
}

.cell .hint-icon {
  opacity: 0.36;
}

.cell:disabled {
  cursor: default;
}

.action-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button:disabled {
  cursor: default;
  opacity: 0.42;
}

@media (orientation: landscape) and (min-width: 900px) {
  .app-shell {
    --page-pad: clamp(8px, 1.2vw, 14px);
    --shell-gap: clamp(8px, 1.1vw, 12px);
    grid-template-columns: minmax(252px, 320px) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    align-items: start;
  }

  .top-panel,
  .controls,
  .status-card {
    grid-column: 1;
  }

  .top-panel {
    grid-template-columns: 1fr;
  }

  .scoreboard {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .score-tile {
    min-height: 58px;
    grid-template-columns: 32px 1fr auto;
    grid-template-rows: 1fr;
    column-gap: 8px;
  }

  .score-tile strong {
    grid-column: auto;
    margin: 0;
    font-size: 1.9rem;
  }

  .controls,
  .starter-control {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented {
    width: 100%;
  }

  .starter-button,
  .icon-button {
    width: 100%;
  }

  .action-buttons {
    width: 100%;
  }

  .board-wrap {
    grid-column: 2;
    grid-row: 1 / -1;
    align-self: stretch;
  }

  .board {
    width: var(--board-size, min(68vw, 94svh, 820px));
    height: var(--board-size, min(68vw, 94svh, 820px));
  }

  h1 {
    font-size: clamp(1.75rem, 3.6vw, 2.6rem);
  }
}

@media (max-width: 899px), (orientation: portrait) {
  .app-shell {
    --page-pad: clamp(8px, 1.8vw, 14px);
    --shell-gap: clamp(7px, 1.2vw, 11px);
  }

  .top-panel {
    grid-template-columns: minmax(170px, 0.8fr) minmax(0, 1.2fr);
    padding: 10px;
  }

  .eyebrow,
  .status-kicker,
  .control-label,
  .score-label {
    font-size: 0.72rem;
  }

  h1 {
    font-size: clamp(1.55rem, 5vw, 2.2rem);
  }

  .scoreboard {
    gap: 8px;
  }

  .score-tile {
    min-height: 58px;
    padding: 8px;
    grid-template-columns: 24px 1fr;
  }

  .score-tile svg {
    width: 23px;
    height: 23px;
  }

  .score-tile strong {
    font-size: 1.55rem;
  }

  .controls {
    align-items: center;
  }

  .starter-control {
    flex: 1 1 auto;
  }

  .starter-button {
    min-width: 106px;
  }

  .starter-button,
  .icon-button {
    min-height: 48px;
    padding: 0 12px;
  }

  .starter-button svg,
  .icon-button svg {
    width: 22px;
    height: 22px;
  }

  .action-buttons {
    flex: 0 0 auto;
  }

  .board {
    width: var(--board-size, min(96vw, 68svh, 760px));
    height: var(--board-size, min(96vw, 68svh, 760px));
  }
}

@media (max-width: 680px) {
  .top-panel {
    grid-template-columns: 1fr;
  }

  .controls,
  .starter-control {
    align-items: stretch;
    flex-direction: column;
  }

  .action-buttons,
  .starter-button,
  .icon-button {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
