/* iOS 27-inspired mobile web UI with calm, in-flow game controls. */

:root {
  --paper: #dfe9fb;
  --surface: rgba(255, 255, 255, 0.9);
  --surface-strong: rgba(255, 255, 255, 0.96);
  --surface-muted: #edf3fb;
  --ink: #15213b;
  --ink-soft: #53627a;
  --x: #bc2b1a;
  --o: #006bd6;
  --blue: #007aff;
  --line: rgba(29, 50, 88, 0.16);
  --shadow-soft: 0 14px 34px rgba(46, 81, 142, 0.15), 0 3px 8px rgba(46, 81, 142, 0.08);
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --radius: 24px;
  --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", system-ui, sans-serif;
}

/* --- reset / base ------------------------------------------------- */
* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding:
    max(1.25rem, calc(env(safe-area-inset-top) + 0.75rem))
    max(1rem, env(safe-area-inset-right))
    max(1.5rem, calc(env(safe-area-inset-bottom) + 0.75rem))
    max(1rem, env(safe-area-inset-left));
  background:
    radial-gradient(80rem 42rem at -15% -15%, rgba(82, 173, 255, 0.48), transparent 60%),
    radial-gradient(56rem 38rem at 115% 20%, rgba(170, 129, 255, 0.35), transparent 58%),
    radial-gradient(45rem 40rem at 50% 110%, rgba(83, 214, 187, 0.34), transparent 64%),
    var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.55;
}

button { font: inherit; color: inherit; }

::selection { background: var(--blue); color: #fff; }

:focus { outline: none; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 3px; border-radius: 7px; }

/* Thin, themed scrollbar */
::-webkit-scrollbar { width: 12px; }
::-webkit-scrollbar-track { background: var(--paper); }
::-webkit-scrollbar-thumb { background: rgba(41, 72, 122, 0.28); border-radius: 6px; border: 3px solid var(--paper); }
::-webkit-scrollbar-thumb:hover { background: rgba(41, 72, 122, 0.42); }

/* --- layout ------------------------------------------------------- */
.app {
  width: min(100%, 27rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

/* --- header ------------------------------------------------------- */
.app-header { width: 100%; text-align: left; }

.title-row {
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.app-title {
  margin: 0;
  font-family: var(--font-ui);
  font-size: clamp(2.2rem, 10vw, 3.5rem);
  font-weight: 700;
  letter-spacing: -0.035em;
  line-height: 1.05;
}

.version-badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.7rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 12px rgba(46, 81, 142, 0.1);
  color: var(--ink-soft);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.app-intro {
  margin: 0.4rem 0 0;
  max-width: 31ch;
  color: var(--ink-soft);
  font-size: 0.98rem;
}

.panel-title {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

/* --- scoreboard --------------------------------------------------- */
.score {
  width: 100%;
  padding: 0.9rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 8px 20px rgba(46, 81, 142, 0.1);
  font-variant-numeric: tabular-nums;
}

.score-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.score-item {
  display: flex;
  min-height: 3.7rem;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.55rem 0.65rem;
  border-radius: 12px;
  background: var(--surface-muted);
  box-shadow: inset 0 -3px 0 transparent;
  font-weight: 600;
  font-size: 0.95rem;
}

.score-item[data-player="X"] { color: var(--x); }
.score-item[data-player="O"] { color: var(--o); }
.score-item.is-draw { color: var(--ink-soft); }

.score-item.is-active[data-player="X"] { box-shadow: inset 0 -3px 0 var(--x); }
.score-item.is-active[data-player="O"] { box-shadow: inset 0 -3px 0 var(--o); }

.score-num { font-size: 1.35rem; font-weight: 800; line-height: 1; }

/* --- board -------------------------------------------------------- */
.board-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--surface-strong);
  box-shadow: var(--shadow-soft);
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 0.65rem;
  gap: 0;
  width: 100%;
  aspect-ratio: 1;
}

.cell {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 0;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

/* the four grid lines: right edge of columns 0-1, bottom edge of rows 0-1 */
.cell:nth-child(3n + 1),
.cell:nth-child(3n + 2) { border-right: 1.5px solid var(--line); }
.cell:nth-child(n + 1):nth-child(-n + 6) { border-bottom: 1.5px solid var(--line); }

.cell:hover { background: rgba(0, 122, 255, 0.08); }
.cell:active { background: rgba(0, 122, 255, 0.14); }
.cell:focus-visible { box-shadow: inset 0 0 0 3px var(--blue); border-radius: 8px; }

.cell.taken { cursor: default; }
.cell.taken:hover { background: transparent; }

/* marks ------------------------------------------------------------ */
.mark { width: 60%; height: 60%; }
.mark-x line { stroke: var(--x); stroke-width: 12; stroke-linecap: round; }
.mark-o circle { stroke: var(--o); stroke-width: 12; fill: none; stroke-linecap: round; }

/* stroke draw-in — the game's one authored motion system */
.mark-x line {
  stroke-dasharray: 72;
  stroke-dashoffset: 72;
  animation: drawin 0.32s var(--ease-out) forwards;
}
.mark-o circle {
  stroke-dasharray: 171;
  stroke-dashoffset: 171;
  animation: drawin 0.4s var(--ease-out) forwards;
  transform-origin: 50% 50%;
  transform: rotate(-90deg);
}

.cell.win .mark { width: 68%; height: 68%; }

@keyframes drawin { to { stroke-dashoffset: 0; } }

/* winning-line overlay --------------------------------------------- */
.winline {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.winline.win-x line { stroke: var(--x); }
.winline.win-o line { stroke: var(--o); }
.winline line {
  stroke-width: 7;
  stroke-linecap: round;
  opacity: 0.92;
  stroke-dasharray: var(--wl, 420);
  stroke-dashoffset: var(--wl, 420);
  animation: drawin 0.5s var(--ease-out) 0.12s forwards;
}

/* --- status ------------------------------------------------------- */
.status {
  min-height: 1.4em;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5ch;
  font-size: 1.18rem;
  font-weight: 650;
}

.status-panel {
  width: 100%;
  min-height: 3.1rem;
  display: grid;
  place-items: start;
  padding: 0.35rem 1rem;
}

.status .mark-char { font-weight: 800; }
.status .mark-char.x { color: var(--x); }
.status .mark-char.o { color: var(--o); }

.status .thinking-dots { display: inline-flex; gap: 2px; }
.status .thinking-dots i {
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--ink-soft);
  animation: dotpulse 1.2s ease-in-out infinite;
}
.status .thinking-dots i:nth-child(2) { animation-delay: 0.18s; }
.status .thinking-dots i:nth-child(3) { animation-delay: 0.36s; }

@keyframes dotpulse { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.legend {
  margin: 0.25rem 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}
.legend strong.x { color: var(--x); font-weight: 700; }
.legend strong.o { color: var(--o); font-weight: 700; }

/* --- setup segments ------------------------------------------------ */
.setup-panel {
  width: 100%;
  display: grid;
  gap: 0.85rem;
  padding: 1rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 8px 20px rgba(46, 81, 142, 0.1);
}

.controls-toggle {
  position: fixed;
  right: max(0.75rem, calc(env(safe-area-inset-right) + 0.5rem));
  bottom: max(0.75rem, calc(env(safe-area-inset-bottom) + 0.5rem));
  z-index: 2;
  display: inline-grid;
  width: 3.1rem;
  height: 3.1rem;
  place-items: center;
  appearance: none;
  border: 0;
  border-radius: 50%;
  background: var(--blue);
  box-shadow: 0 10px 24px rgba(0, 91, 190, 0.3);
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform 0.15s var(--ease-out), background-color 0.15s var(--ease-out);
}

.controls-toggle:hover { background: #006ee6; }
.controls-toggle:active { transform: scale(0.94); }
.controls-toggle svg {
  width: 1.35rem;
  height: 1.35rem;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-width: 2;
}

.setup { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.6rem; }

.setup .seg { width: 100%; }
.setup .seg-btn { flex: 1 1 0; }

.seg {
  display: inline-flex;
  gap: 3px;
  padding: 3px;
  border-radius: 999px;
  background: rgba(30, 56, 100, 0.12);
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  min-height: 2.65rem;
  padding: 0.42rem 0.95rem;
  border-radius: 999px;
  font-size: 0.92rem;
  font-weight: 650;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s var(--ease-out), color 0.15s var(--ease-out);
}
.seg-btn:hover { color: var(--ink); }
.seg-btn[aria-checked="true"] {
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  box-shadow: 0 3px 10px rgba(46, 81, 142, 0.14);
}

.seg-btn .dot { display: inline-block; width: 0.6em; height: 0.6em; border-radius: 50%; margin-right: 0.4em; vertical-align: 0; }
.seg-btn[data-side="X"] .dot { background: var(--x); }
.seg-btn[data-side="O"] .dot { background: var(--o); }

/* --- actions ------------------------------------------------------- */
.actions { display: flex; width: 100%; gap: 0.7rem; }

.btn {
  appearance: none;
  border: 1.5px solid transparent;
  border-radius: 999px;
  min-height: 2.75rem;
  padding: 0.68rem 1.5rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s var(--ease-out), background-color 0.15s var(--ease-out), border-color 0.15s var(--ease-out), opacity 0.15s var(--ease-out);
}
.actions .btn { flex: 1 1 0; }
.btn:active { transform: translateY(1px); }

.btn-primary { background: var(--blue); color: #fff; box-shadow: 0 7px 16px rgba(0, 104, 214, 0.28); }
.btn-primary:hover { background: #006ee6; }
.btn-primary:disabled { opacity: 0.45; cursor: default; }

.btn-ghost { background: rgba(255, 255, 255, 0.48); color: var(--ink); border-color: rgba(34, 66, 115, 0.16); }
.btn-ghost:hover { border-color: rgba(34, 66, 115, 0.36); background: rgba(255, 255, 255, 0.76); }
.btn-ghost:disabled { opacity: 0.4; cursor: default; }
.btn-ghost:disabled:hover { border-color: rgba(34, 66, 115, 0.2); background: rgba(255, 255, 255, 0.34); }

/* --- footer -------------------------------------------------------- */
.app-footer {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--ink-soft);
  font-size: 0.85rem;
  flex-wrap: wrap;
}

.link-btn {
  appearance: none;
  border: 0;
  background: none;
  padding: 0.2rem 0.1rem;
  color: var(--ink-soft);
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  font-size: 0.85rem;
}
.link-btn:hover { color: var(--ink); }

/* --- busy board (AI thinking) -------------------------------------- */
.board.busy { cursor: wait; }
.board.busy .cell { pointer-events: none; }

/* --- reduced motion ------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .mark-x line, .mark-o circle, .winline line {
    animation: none;
    stroke-dashoffset: 0;
  }
  .status .thinking-dots i { animation: none; opacity: 0.6; }
  .seg-btn, .btn, .controls-toggle { transition: none; }
}

@media (prefers-contrast: more) {
  .app-intro, .legend, .app-footer, .panel-title { color: #33415b; }
}

/* --- wide-screen enhancement ----------------------------------------- */
@media (min-width: 640px) {
  body {
    justify-content: center;
    padding: 2.75rem 1rem;
  }

  .app { width: min(92vw, 29rem); gap: 1.25rem; }
  .app-header { text-align: center; }
  .title-row { justify-content: center; }
  .app-intro { margin-inline: auto; max-width: 34ch; }
  .status-panel { place-items: center; }
  .setup { justify-content: center; }
  .setup .seg { width: auto; }
  .setup .seg-btn { flex: 0 0 auto; }
  .controls-toggle { display: none; }
}

@media (max-width: 380px) {
  .app { gap: 0.85rem; }
  .app-title { font-size: 2rem; }
  .app-intro { font-size: 0.92rem; }
  .score-item { min-height: 3.35rem; padding: 0.45rem; }
  .score-num { font-size: 1.2rem; }
  .setup-panel { padding: 0.85rem; }
}

@media (orientation: landscape) and (max-width: 960px) {
  body { padding-block: 0.75rem; }
  .app { width: min(94vw, 32rem); gap: 0.75rem; }
  .board-wrap { width: min(100%, 22rem); }
}
