/* ============================================================
   Tic Tac Toe · Otterfly — styles
   Palette, type and motifs follow https://www.otterfly.com/branding
   ============================================================ */

@font-face {
  font-family: "JetBrains Mono";
  src: url("fonts/JetBrainsMono-latin.woff2") format("woff2");
  font-weight: 100 800;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Inter";
  src: url("fonts/Inter-latin.woff2") format("woff2");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  /* Otterfly palette */
  --of-dark: #0c0c0c;
  --of-amber: #e8a924;
  --of-cream: #faf9f7;
  --of-ink: #1a1a1a;
  --of-fog: #e8e6e3;
  --of-coal: #262626;
  --of-mist: #e5e5e5;
  --of-stone: #999999;
  --of-slate: #6b6b6b;
  --of-ember: #ef4444;
  --of-moss: #22c55e;

  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  --radius: 14px;
  --ease-out: cubic-bezier(.2, .9, .25, 1);
  --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
}

/* Dark mode is the default for screens. Roles, not raw colours, below. */
html[data-theme="dark"] {
  --bg: var(--of-dark);
  --surface: var(--of-ink);       /* cards and popovers */
  --muted: var(--of-coal);        /* borders, inputs, muted surfaces */
  --text: var(--of-fog);          /* reading text */
  --strong: var(--of-cream);      /* wordmark, headings */
  --quiet: var(--of-stone);       /* captions, metadata, tagline */
  --accent: var(--of-amber);
  --accent-hover: #f5b83d;
  --on-accent: var(--of-dark);
  --cell: var(--of-dark);
  --x: var(--of-cream);
  --o: var(--of-amber);
  --dim: var(--of-stone);
  --dots: var(--of-coal);
  color-scheme: dark;
}
/* Light mode only when the OS asks for it (or the player picks it). */
html[data-theme="light"] {
  --bg: var(--of-cream);
  --surface: var(--of-cream);
  --muted: var(--of-mist);
  --text: var(--of-ink);
  --strong: var(--of-dark);
  --quiet: var(--of-slate);
  --accent: var(--of-amber);
  --accent-hover: #d49a1f;
  --on-accent: var(--of-dark);
  --cell: var(--of-cream);
  --x: var(--of-dark);
  --o: var(--of-amber);
  --dim: var(--of-mist);
  --dots: var(--of-mist);
  color-scheme: light;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  transition: background-color .4s ease, color .4s ease;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }
kbd {
  font-family: var(--mono);
  font-size: .78em;
  font-weight: 500;
  padding: .1em .45em;
  border-radius: 6px;
  border: 1px solid var(--muted);
  background: var(--bg);
  color: var(--text);
}
svg { display: block; }

/* subtle dot grid, kept off the logo area so the logo sits on a plain plate */
.backdrop {
  position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background-image: radial-gradient(var(--dots) 1.2px, transparent 1.4px);
  background-size: 26px 26px;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, transparent 150px, #000 320px);
  mask-image: linear-gradient(to bottom, transparent 0, transparent 150px, #000 320px);
}
#fx { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 50; }

/* ---------- layout ---------- */
.app {
  width: min(580px, 100% - 32px);
  margin: 0 auto;
  padding: 22px 0 26px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-height: 100%;
}

/* ---------- brand ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand {
  display: flex;
  align-items: flex-end;     /* mark and text share a baseline at the bottom */
  gap: 13px;
  text-decoration: none;
}
.brand-mark { width: 64px; height: 64px; margin-bottom: 1px; flex: none; }
.brand-text { display: flex; flex-direction: column; font-family: var(--mono); line-height: 1; }
.brand-word { font-size: 24px; font-weight: 700; letter-spacing: -0.025em; color: var(--strong); }
.brand-cursor { color: var(--of-amber); animation: blink 1.1s steps(1) infinite; }
.brand-tagline { font-size: 8.64px; letter-spacing: -0.025em; margin-top: 4px; color: var(--quiet); }
@keyframes blink { 50% { opacity: 0; } }

.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--muted);
  color: var(--text);
  transition: transform .2s var(--ease-spring), border-color .2s, color .2s;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); transform: translateY(-2px); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
html[data-theme="dark"] #themeBtn .moon, html[data-theme="light"] #themeBtn .sun { display: none; }
#soundBtn .off { display: none; }
#soundBtn[aria-pressed="false"] .on { display: none; }
#soundBtn[aria-pressed="false"] .off { display: block; }

/* ---------- hero ---------- */
.hero { padding-top: 6px; }
.page-title {
  margin: 0;
  font: 700 clamp(30px, 7vw, 40px)/1.05 var(--mono);
  letter-spacing: -.02em;
  color: var(--strong);
}
/* blinking cursor behind page titles (added in code, never typed) */
.cursor {
  display: inline-block;
  width: .55em; height: .11em;
  margin-left: .12em;
  background: var(--of-amber);
  vertical-align: baseline;
  animation: blink 1.1s steps(1) infinite;
}
.lede { margin: 10px 0 0; font-size: 15px; line-height: 1.5; color: var(--quiet); max-width: 46ch; }

/* static amber underscore that closes section headings */
.section-title::after { content: "_"; color: var(--of-amber); margin-left: .08em; }

/* ---------- segmented controls ---------- */
.controls { display: flex; flex-direction: column; gap: 10px; }
.seg {
  position: relative;
  display: flex;
  padding: 4px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--muted);
}
.seg button {
  position: relative; z-index: 1;
  flex: 1;
  border: 0; background: none;
  padding: 9px 6px;
  border-radius: 8px;
  font: 500 13px var(--mono);
  color: var(--quiet);
  white-space: nowrap;
  transition: color .25s;
}
.seg button[aria-checked="true"] { color: var(--strong); font-weight: 700; }
.seg button:hover { color: var(--accent); }
.seg button[aria-checked="true"]:hover { color: var(--strong); }
.seg-glider {
  position: absolute; z-index: 0; top: 4px; bottom: 4px; left: 0; width: 0;
  border-radius: 8px;
  background: var(--muted);
  transition: transform .45s var(--ease-spring), width .45s var(--ease-spring);
}
.tag {
  font: 600 9px var(--mono); letter-spacing: .04em; text-transform: uppercase;
  padding: 2px 5px; border-radius: 4px; margin-left: 4px; vertical-align: 1px;
  border: 1px solid var(--quiet); color: var(--quiet);
}
.seg button[aria-checked="true"] .tag { border-color: var(--of-amber); color: var(--of-amber); }

/* ---------- scoreboard ---------- */
.scoreboard { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 10px; align-items: stretch; }
.player {
  position: relative;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--muted);
  transition: transform .4s var(--ease-spring), border-color .3s;
}
.player.po { justify-content: flex-end; }
.player .avatar {
  flex: none; width: 44px; height: 44px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--bg); border: 1px solid var(--muted);
  overflow: hidden;
}
.player .avatar svg { width: 28px; height: 28px; fill: none; stroke-width: 13; stroke-linecap: round; }
.px .avatar svg { stroke: var(--x); }
.po .avatar svg { stroke: var(--o); }
.avatar-otterfly { width: 40px; height: 40px; display: none; margin-top: 4px; }
.po.vs-ai .avatar-otterfly { display: block; }
.po.vs-ai .avatar-o { display: none; }
.pinfo { display: flex; flex-direction: column; min-width: 0; }
.pinfo.right { align-items: flex-end; text-align: right; }
.pname { font: 500 12px var(--mono); color: var(--quiet); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pscore { font: 700 28px/1.05 var(--mono); color: var(--strong); font-variant-numeric: tabular-nums; }
.pscore.bump { animation: bump .6s var(--ease-spring); }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.45); } 100% { transform: scale(1); } }

.turn-pill {
  position: absolute; bottom: -10px; left: 50%;
  transform: translate(-50%, 4px) scale(.8);
  font: 700 10px var(--mono); letter-spacing: .06em; text-transform: uppercase;
  padding: 3px 9px; border-radius: 6px;
  opacity: 0; white-space: nowrap;
  transition: opacity .3s, transform .35s var(--ease-spring);
  pointer-events: none;
}
.px .turn-pill { background: var(--x); color: var(--bg); }
.po .turn-pill { background: var(--o); color: var(--on-accent); }
.player.active { transform: translateY(-3px); }
.player.active .turn-pill { opacity: 1; transform: translate(-50%, 0) scale(1); }
.px.active { border-color: var(--x); }
.po.active { border-color: var(--o); }
.player.thinking .turn-pill::after { content: "_"; animation: blink .8s steps(1) infinite; }

.ties {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 8px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--muted);
  min-width: 76px;
}
.ties-label { font: 600 10px var(--mono); letter-spacing: .12em; text-transform: uppercase; color: var(--quiet); }
.ties-num { font: 700 22px var(--mono); color: var(--strong); font-variant-numeric: tabular-nums; }
.streak { font: 600 10px var(--mono); color: var(--accent); min-height: 12px; white-space: nowrap; }

/* ---------- board ---------- */
.stage { display: flex; flex-direction: column; align-items: center; gap: 14px; perspective: 1200px; }
.board-wrap {
  position: relative;
  width: min(100%, 460px);
  aspect-ratio: 1;
  transform-style: preserve-3d;
  transition: transform .5s var(--ease-out);
  will-change: transform;
}
.board {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 10px;
  padding: 10px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--muted);
}
html[data-theme="light"] .board { background: var(--of-mist); }
.board.shake { animation: shake .45s ease; }
@keyframes shake {
  20% { transform: translateX(-8px) rotate(-.6deg); }
  40% { transform: translateX(7px) rotate(.5deg); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(3px); }
}

.cell {
  position: relative;
  border: 1px solid var(--muted);
  border-radius: 12px;
  background: var(--cell);
  padding: 0;
  overflow: hidden;
  outline: none;
  transition: transform .25s var(--ease-spring), border-color .2s;
  transform: translateZ(0);
}
.board[data-turn="X"] { --turn: var(--x); }
.board[data-turn="O"] { --turn: var(--o); }
.board:not(.locked) .cell:not(.filled):hover { transform: translateY(-2px) translateZ(0); border-color: var(--accent); }
.cell:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.board:not(.locked) .cell:active:not(.filled) { transform: scale(.96); }
.board.locked .cell, .cell.filled { cursor: default; }
.cell .num {
  position: absolute; top: 7px; left: 9px; z-index: 2;
  font: 500 10px var(--mono); color: var(--quiet);
}

/* the mark layer fills the whole cell and centres its mark */
.slot, .ghost {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  pointer-events: none;
}
.slot { z-index: 1; }
.mark { width: 62%; height: 62%; overflow: visible; }
.mark path, .mark circle { fill: none; stroke-width: 13; stroke-linecap: round; transition: stroke .5s; }
.mark.x path { stroke: var(--x); }
.mark.o circle { stroke: var(--o); }

/* draw-on animation */
.mark.x path { stroke-dasharray: 90; stroke-dashoffset: 90; animation: draw .28s var(--ease-out) forwards; }
.mark.x path:nth-child(2) { animation-delay: .14s; }
.mark.o circle { stroke-dasharray: 190; stroke-dashoffset: 190; animation: draw .5s var(--ease-out) forwards; transform-origin: 50% 50%; transform: rotate(-90deg); }
@keyframes draw { to { stroke-dashoffset: 0; } }
.cell.pop .mark { animation: pop .5s var(--ease-spring); }
@keyframes pop { 0% { transform: scale(.4); } 100% { transform: scale(1); } }

/* ripple on placement */
.ripple {
  position: absolute; left: 50%; top: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px;
  border-radius: 50%;
  border: 2px solid var(--c);
  animation: ripple .7s var(--ease-out) forwards;
  pointer-events: none;
}
@keyframes ripple { to { transform: scale(12); opacity: 0; } }

/* ghost preview of the next mark */
.ghost { opacity: 0; transform: scale(.85); transition: opacity .2s, transform .3s var(--ease-spring); }
.ghost svg { width: 62%; height: 62%; overflow: visible; }
.ghost path, .ghost circle { fill: none; stroke-width: 9; stroke-linecap: round; stroke-dasharray: 1 15; }
.board[data-turn="X"] .ghost path { stroke: var(--x); }
.board[data-turn="O"] .ghost circle { stroke: var(--o); }
.board[data-turn="X"] .ghost circle, .board[data-turn="O"] .ghost path { display: none; }
.board:not(.locked) .cell:not(.filled):hover .ghost,
.board:not(.locked) .cell:not(.filled):focus-visible .ghost { opacity: 1; transform: scale(1); }

/* hint */
.cell.hint { animation: hintGlow 1.1s ease-in-out 3; }
@keyframes hintGlow { 50% { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent); } }
.cell.hint .ghost { opacity: 1 !important; transform: scale(1) !important; }

/* Vanishing: the oldest mark of the player about to move */
.cell.doomed .mark { animation: flicker 1.4s steps(1) infinite; }
.cell.doomed::after {
  content: ""; position: absolute; inset: 6px; border-radius: 9px;
  border: 1.5px dashed var(--quiet);
}
@keyframes flicker { 0%, 100% { opacity: 1; } 10% { opacity: .2; } 14% { opacity: .9; } 18% { opacity: .3; } 30% { opacity: .85; } 60% { opacity: .4; } 70% { opacity: .85; } }
.cell .vanish-out { position: absolute; inset: 19%; width: 62%; height: 62%; animation: vanish .55s ease-in forwards !important; z-index: 1; }
@keyframes vanish { to { opacity: 0; transform: scale(1.5) rotate(25deg); filter: blur(6px); } }
.age { position: absolute; bottom: 7px; right: 9px; z-index: 2; display: flex; gap: 3px; }
.age i { width: 5px; height: 5px; border-radius: 50%; background: var(--muted); }
.age i.on { background: var(--c); }

/* results */
.cell.win { border-color: var(--wc); }
.cell.win .mark { animation: winPulse 1.2s ease-in-out infinite; }
@keyframes winPulse { 50% { transform: scale(1.08); } }
.board.over .cell:not(.win) .mark path,
.board.over .cell:not(.win) .mark circle,
.board.draw .cell .mark path,
.board.draw .cell .mark circle { stroke: var(--dim); }

.win-line { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; overflow: visible; }
.win-line line {
  fill: none;
  stroke: var(--wl, var(--accent)); stroke-width: 7; stroke-linecap: round;
  stroke-dasharray: 500; stroke-dashoffset: 500;
  opacity: 0;
}
.win-line.show line { opacity: 1; animation: draw .55s var(--ease-out) forwards; }

/* result banner: tilted Otterfly on the left, words on the right */
.banner {
  position: absolute; inset: 0; z-index: 5;
  display: grid; place-items: center;
  pointer-events: none; opacity: 0;
  transition: opacity .35s;
}
.banner.show { opacity: 1; pointer-events: auto; }
.banner-inner {
  display: flex; align-items: center; gap: 16px;
  max-width: calc(100% - 28px);
  padding: 18px 22px 18px 16px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--muted);
  box-shadow: 0 24px 60px var(--of-dark);
  transform: translateY(18px) scale(.92);
  transition: transform .55s var(--ease-spring);
}
html[data-theme="light"] .banner-inner { box-shadow: 0 20px 50px var(--of-stone); }
.banner.show .banner-inner { transform: none; }
.banner-art { width: 104px; height: auto; flex: none; }
.banner-copy { display: flex; flex-direction: column; align-items: flex-start; }
.banner-title { font: 700 clamp(24px, 6vw, 32px)/1.05 var(--mono); letter-spacing: -.02em; color: var(--strong); }
.banner-title .hl { color: var(--accent); }
.banner-sub { color: var(--quiet); margin: 6px 0 14px; font-size: 14px; line-height: 1.4; max-width: 24ch; }

.status { margin: 0; min-height: 1.4em; font-size: 15px; color: var(--quiet); text-align: center; }
.status b { color: var(--strong); font-weight: 600; }

/* ---------- buttons ---------- */
.actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 10px 13px; border-radius: 10px;
  font: 600 13px var(--mono);
  background: var(--surface); border: 1px solid var(--muted);
  color: var(--text);
  transition: transform .2s var(--ease-spring), border-color .2s, color .2s, background-color .2s;
}
.btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn:hover:not(:disabled) { transform: translateY(-2px); border-color: var(--accent); color: var(--accent); }
.btn:active:not(:disabled) { transform: scale(.96); }
.btn:disabled { color: var(--quiet); border-color: var(--muted); cursor: not-allowed; }
.btn:disabled svg { stroke: var(--quiet); }
.btn.primary { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--on-accent); }
.btn.primary kbd { background: var(--on-accent); color: var(--of-amber); border-color: var(--on-accent); }
.btn.quiet { background: none; }
.btn.danger:hover:not(:disabled) { border-color: var(--of-ember); color: var(--of-ember); }
.btn:focus-visible, .icon-btn:focus-visible, .seg button:focus-visible, .brand:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.foot {
  margin-top: auto;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 12px; color: var(--quiet); text-align: center;
}
.foot p { margin: 0; line-height: 1.9; }
.foot a { color: var(--text); text-decoration: none; border-bottom: 1px solid var(--muted); }
.foot a:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- help dialog ---------- */
.modal {
  width: min(500px, calc(100% - 32px));
  border: 1px solid var(--muted);
  border-radius: 18px;
  padding: 24px;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 40px 100px var(--of-dark);
}
html[data-theme="light"] .modal { box-shadow: 0 30px 80px var(--of-stone); }
.modal[open] { animation: modalIn .45s var(--ease-spring); }
@keyframes modalIn { from { opacity: 0; transform: translateY(20px) scale(.95); } }
.modal::backdrop { background: rgba(12, 12, 12, .7); }
.modal-head { display: flex; gap: 16px; align-items: center; margin-bottom: 16px; }
.modal-art { width: 104px; height: auto; flex: none; }
.modal h2 { margin: 0 0 6px; font: 700 22px var(--mono); color: var(--strong); }
.modal h3 { margin: 0 0 6px; font: 700 14px var(--mono); color: var(--strong); }
.modal p { margin: 0; color: var(--text); line-height: 1.55; font-size: 14px; }
.modal b { color: var(--strong); font-weight: 600; }
.rules { display: grid; gap: 10px; }
.rule { padding: 14px; border-radius: 12px; background: var(--bg); border: 1px solid var(--muted); }
.keys { padding-left: 18px; margin: 16px 0 20px; color: var(--quiet); font-size: 13px; line-height: 2; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; top: 18px; z-index: 60;
  transform: translate(-50%, -30px); opacity: 0;
  padding: 10px 16px; border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--muted);
  color: var(--text);
  font: 500 13px var(--mono);
  transition: transform .45s var(--ease-spring), opacity .3s;
  pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---------- wide screens: controls on the left, board on the right ---------- */
@media (min-width: 960px) {
  .app {
    width: min(1120px, 100% - 64px);
    display: grid;
    grid-template-columns: 400px minmax(0, 1fr);
    grid-template-areas:
      "top     top"
      "hero    stage"
      "controls stage"
      "score   stage"
      "actions stage"
      ".       stage"
      "foot    foot";
    grid-template-rows: auto auto auto auto auto 1fr auto;
    column-gap: 64px;
    row-gap: 22px;
    padding-top: 28px;
    align-content: start;
  }
  .topbar { grid-area: top; margin-bottom: 8px; }
  .hero { grid-area: hero; padding-top: 18px; }
  .controls { grid-area: controls; }
  .scoreboard { grid-area: score; }
  .actions { grid-area: actions; justify-content: flex-start; }
  .stage { grid-area: stage; align-self: start; }
  .foot { grid-area: foot; flex-direction: row; justify-content: space-between; margin-top: 18px; }
  .board-wrap { width: min(100%, 580px, calc(100vh - 210px)); min-width: 380px; }
  .page-title { font-size: 44px; }
}

/* ---------- responsive ---------- */
@media (max-width: 520px) {
  .seg button { font-size: 12px; padding: 9px 3px; }
  .tag { display: none; }
  .player { padding: 10px; gap: 8px; }
  .player .avatar { width: 36px; height: 36px; }
  .player .avatar svg { width: 22px; height: 22px; }
  .avatar-otterfly { width: 34px; height: 34px; }
  .pscore { font-size: 22px; }
  .ties { min-width: 58px; padding: 8px; }
  .board { gap: 7px; padding: 7px; }
  .btn { padding: 9px 11px; font-size: 12px; }
  .banner-art { width: 76px; }
  .modal-art { width: 80px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
  .brand-cursor, .cursor, .player.thinking .turn-pill::after { animation: none !important; opacity: 1 !important; }
}
@media print {
  .brand-cursor, .cursor { animation: none; }
}
