/* ===== Reset ===== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
button { font: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* ===== Theme tokens ===== */
:root {
  --bg-deep: #0c0f1c;
  --bg-warm: #1a1726;
  --surface: rgba(255, 255, 255, 0.06);
  --surface-hover: rgba(255, 255, 255, 0.12);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.18);

  --board-cream: #f5e6c4;
  --board-cream-deep: #ead4a4;
  --board-line: #b08856;

  --x: #e63946;
  --x-glow: rgba(230, 57, 70, 0.45);
  --o: #2e86de;
  --o-glow: rgba(46, 134, 222, 0.45);
  --gold: #f5b942;
  --gold-soft: #ffd97a;

  --text: #f0e9d6;
  --text-strong: #fff7e3;
  --text-muted: #9aa0b4;

  --cell-size: 30px;
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow-soft: 0 10px 40px rgba(0,0,0,0.45);
  --shadow-board: 0 30px 70px -20px rgba(0,0,0,0.6), 0 0 0 1px rgba(245,185,66,0.08);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

@media (max-width: 640px) { :root { --cell-size: 24px; } }
@media (max-width: 360px) { :root { --cell-size: 20px; } }

/* ===== Layout ===== */
html, body { height: 100%; }
body {
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(ellipse 80% 60% at 50% 100%, rgba(245,185,66,0.08), transparent 70%),
    radial-gradient(ellipse 100% 80% at 50% 0%, rgba(46,134,222,0.06), transparent 60%),
    linear-gradient(180deg, var(--bg-deep) 0%, var(--bg-warm) 100%);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 16px 28px;
  gap: 18px;
  letter-spacing: -0.005em;
  -webkit-font-smoothing: antialiased;
  position: relative;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0.035 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  opacity: 0.55;
  mix-blend-mode: overlay;
}

body > * { position: relative; z-index: 1; }

/* ===== Header ===== */
.app-header {
  width: 100%;
  max-width: 760px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  background: linear-gradient(180deg, rgba(255,255,255,0.05), rgba(255,255,255,0.015));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: var(--shadow-soft);
}

.app-title {
  font-family: 'Fraunces', 'Times New Roman', serif;
  font-weight: 600;
  font-size: 1.8rem;
  letter-spacing: -0.025em;
  color: var(--text-strong);
  font-feature-settings: 'ss01' on;
  margin-right: auto;
  order: 1;
}
.app-title::first-letter { color: var(--gold); }

.mode-toggle {
  display: inline-flex;
  background: rgba(0,0,0,0.28);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  order: 2;
}
.mode-btn {
  padding: 7px 14px;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
  border-radius: 999px;
  transition: color 0.2s var(--ease), background 0.2s var(--ease);
}
.mode-btn:hover { color: var(--text); }
.mode-btn.is-active {
  background: linear-gradient(180deg, #faf0d8, #e9d6a8);
  color: #1a1726;
  font-weight: 600;
  box-shadow: 0 1px 0 rgba(255,255,255,0.6) inset, 0 4px 12px rgba(0,0,0,0.25);
}
.mode-btn:focus-visible,
.ctrl-btn:focus-visible,
.share-btn:focus-visible,
.difficulty-btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.header-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  order: 3;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 1rem;
  line-height: 1;
  transition: background 0.2s var(--ease), transform 0.2s var(--ease);
}
.share-btn:hover { background: var(--surface-hover); transform: translateY(-1px); }

.streak-display {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 12px;
  background: linear-gradient(135deg, rgba(245,185,66,0.2), rgba(245,185,66,0.08));
  border: 1px solid rgba(245,185,66,0.3);
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
}
.streak-icon { font-size: 1rem; }
.streak-value { color: var(--gold-soft); font-variant-numeric: tabular-nums; }
.streak-display.is-hidden { display: none; }

.difficulty-selector {
  display: flex;
  background: rgba(0,0,0,0.28);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  flex-basis: 100%;
  max-width: 360px;
  margin: 2px auto 0;
  justify-content: center;
  order: 4;
}
.difficulty-btn {
  padding: 6px 18px;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
  border-radius: 999px;
  transition: color 0.2s var(--ease), background 0.2s var(--ease);
}
.difficulty-btn:hover { color: var(--text); }
.difficulty-btn.is-active {
  background: linear-gradient(180deg, var(--o), #1a6fc4);
  color: #fff;
  font-weight: 600;
  box-shadow: 0 4px 12px var(--o-glow);
}
.difficulty-selector.is-hidden { display: none; }

.score-panel {
  flex-basis: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  order: 5;
}
.score {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  padding: 10px 12px;
  background: rgba(0,0,0,0.22);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.score-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.score-value {
  font-family: 'Fraunces', serif;
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-strong);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.score-x .score-label { color: var(--x); }
.score-o .score-label { color: var(--o); }
.score-draws .score-label { color: var(--text-muted); }

/* ===== Main ===== */
.app-main {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.status-bar {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  background: rgba(0,0,0,0.28);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  min-height: 2em;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

/* ===== Board wrapper ===== */
.board-wrapper {
  overflow: auto;
  max-width: 100%;
  padding: 16px;
  position: relative;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(245,185,66,0.1), transparent 70%),
    linear-gradient(160deg, rgba(245,185,66,0.06), rgba(176,136,86,0.03));
  border: 1px solid rgba(245,185,66,0.14);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-board);
}

/* ===== Board grid ===== */
.board-grid {
  display: grid;
  grid-template-columns: repeat(20, var(--cell-size));
  grid-template-rows: repeat(20, var(--cell-size));
  background: var(--board-line);
  gap: 1px;
  border: 2px solid var(--board-line);
  border-radius: 4px;
  position: relative;
  box-shadow:
    inset 0 0 0 1px rgba(255,255,255,0.18),
    inset 0 2px 10px rgba(255,255,255,0.2),
    inset 0 -2px 10px rgba(0,0,0,0.2);
}

.board-grid.disabled { pointer-events: none; }

/* ===== Cells ===== */
.cell {
  background: linear-gradient(180deg, var(--board-cream), var(--board-cream-deep));
  cursor: pointer;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Fraunces', serif;
  font-size: calc(var(--cell-size) * 0.62);
  font-weight: 700;
  line-height: 1;
  user-select: none;
  touch-action: manipulation;
  transition: background 0.15s var(--ease);
  position: relative;
}

.cell:hover { background: linear-gradient(180deg, #fef2d6, #ead8b3); }
.cell:focus-visible { outline: 2px solid var(--gold); outline-offset: -2px; }

@keyframes piece-pop {
  0%   { transform: scale(0.2) rotate(-10deg); opacity: 0; }
  60%  { transform: scale(1.18) rotate(0deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); }
}

.cell.x::after,
.cell.o::after {
  display: inline-block;
  animation: piece-pop 0.25s var(--ease);
}

.cell.x::after {
  content: 'X';
  color: var(--x);
  text-shadow: 0 1px 0 rgba(255,255,255,0.4), 0 2px 6px var(--x-glow);
}

.cell.o::after {
  content: 'O';
  color: var(--o);
  text-shadow: 0 1px 0 rgba(255,255,255,0.4), 0 2px 6px var(--o-glow);
}

@keyframes last-move-pulse {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--gold), 0 0 0 0 rgba(245,185,66,0.35); }
  50%      { box-shadow: inset 0 0 0 2px var(--gold-soft), 0 0 0 4px rgba(245,185,66,0.22); }
}

.cell.last-move {
  animation: last-move-pulse 1.6s ease-in-out infinite;
  z-index: 1;
}

/* ===== Win line SVG overlay ===== */
.win-overlay {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  display: none;
  overflow: visible;
}

.win-overlay.is-visible { display: block; }

@keyframes win-draw {
  from { stroke-dashoffset: var(--win-len, 1000); }
  to   { stroke-dashoffset: 0; }
}

.win-line {
  stroke: var(--gold);
  stroke-width: 5;
  stroke-linecap: round;
  opacity: 0.95;
  stroke-dasharray: var(--win-len, 1000);
  animation: win-draw 0.5s ease-out forwards;
  filter: drop-shadow(0 0 8px var(--gold));
}

/* ===== Controls / Footer ===== */
.app-footer {
  width: 100%;
  max-width: 760px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.ctrl-btn {
  padding: 10px 22px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  transition: background 0.2s var(--ease), transform 0.15s var(--ease), border-color 0.2s var(--ease);
}
.ctrl-btn:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}
.ctrl-btn:active { transform: translateY(0); }
.ctrl-btn:disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }

#btn-restart {
  background: linear-gradient(180deg, rgba(245,185,66,0.18), rgba(245,185,66,0.05));
  border-color: rgba(245,185,66,0.4);
  color: var(--gold-soft);
}
#btn-restart:hover {
  background: linear-gradient(180deg, rgba(245,185,66,0.28), rgba(245,185,66,0.1));
  border-color: rgba(245,185,66,0.6);
}

/* ===== Daily puzzle banner ===== */
.puzzle-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: linear-gradient(135deg, rgba(245,185,66,0.14), rgba(245,185,66,0.04));
  border: 1px solid rgba(245,185,66,0.22);
  border-radius: var(--radius);
  margin-bottom: 6px;
  font-size: 0.9rem;
}
.puzzle-goal { font-weight: 600; color: var(--gold-soft); }
.puzzle-attempts { letter-spacing: 3px; color: var(--text-muted); font-size: 1.05rem; }

/* ===== Modals (shared) ===== */
.puzzle-modal, .stats-modal, .replay-modal, .room-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.puzzle-modal-backdrop, .stats-modal-backdrop,
.replay-modal-backdrop, .room-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 20, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.puzzle-modal-panel, .stats-modal-panel,
.replay-modal-panel, .room-modal-panel {
  position: relative;
  background: linear-gradient(180deg, #1f1d2e, #181625);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  width: 92%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,0.55);
  animation: stats-slide-up 0.3s var(--ease);
}
.stats-modal-panel { max-height: 88vh; overflow-y: auto; }

@keyframes stats-slide-up {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.puzzle-modal-title, .stats-modal-title,
.replay-modal-title, .room-modal-title {
  font-family: 'Fraunces', serif;
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-strong);
  letter-spacing: -0.02em;
}

.puzzle-modal-subtitle, .room-modal-subtitle {
  color: var(--text-muted);
  margin-bottom: 18px;
  font-size: 0.95rem;
}
.puzzle-modal-emoji {
  font-size: 1.6rem;
  letter-spacing: 6px;
  margin-bottom: 18px;
}
.puzzle-modal-streak {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 22px;
  font-size: 1rem;
  color: var(--text-muted);
}
.puzzle-modal-actions, .replay-modal-actions, .room-modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ===== Stats modal ===== */
.stats-section { margin-bottom: 20px; }
.stats-section-title {
  font-size: 0.78rem;
  color: var(--gold);
  margin-bottom: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.stats-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 4px;
}
.stats-val {
  font-family: 'Fraunces', serif;
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}
.stats-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
  text-align: center;
  letter-spacing: 0.04em;
}

.stats-dist { margin-top: 12px; }
.stats-dist-title { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 8px; }
.dist-row { display: flex; align-items: center; gap: 8px; margin-bottom: 5px; }
.dist-label {
  width: 1.2rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.dist-bar-wrap {
  flex: 1;
  height: 14px;
  background: rgba(0,0,0,0.3);
  border-radius: 7px;
  overflow: hidden;
}
.dist-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--gold), var(--gold-soft));
  border-radius: 7px;
  min-width: 4px;
  transition: width 0.4s var(--ease);
}
.dist-bar.is-highlight {
  background: linear-gradient(90deg, #fff, var(--gold-soft));
  box-shadow: 0 0 0 1px var(--gold);
}
.dist-count {
  width: 1.5rem;
  text-align: right;
  font-size: 0.85rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.achieve-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.achieve-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 4px;
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
}
.achieve-card.is-locked { opacity: 0.35; filter: grayscale(1); }
.achieve-icon { font-size: 1.5rem; }
.achieve-title { font-size: 0.7rem; color: var(--text-muted); line-height: 1.2; }

.stats-modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  padding-top: 6px;
}

/* ===== Replay / Room ===== */
.replay-status { color: var(--text-muted); margin-bottom: 14px; }
.replay-progress-wrap {
  width: 100%;
  height: 10px;
  background: rgba(0,0,0,0.3);
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 18px;
}
.replay-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--gold), var(--gold-soft));
  border-radius: 5px;
  transition: width 0.2s ease;
  width: 0%;
}
.replay-preview { margin-bottom: 16px; }

.room-modal-link { display: flex; gap: 8px; margin-bottom: 16px; }
.room-modal-link input {
  flex: 1;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: rgba(0,0,0,0.3);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}
.room-modal-link input:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.room-modal-status { color: var(--gold-soft); margin-bottom: 16px; min-height: 1.4em; }

/* ===== Disconnect banner ===== */
.disconnect-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 150;
  background: linear-gradient(180deg, #c0392b, #a92e22);
  color: #fff;
  padding: 10px;
  text-align: center;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 2px 12px rgba(192,57,43,0.4);
}
.disconnect-banner .spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Latency badge ===== */
.latency-badge {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 90;
  padding: 5px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.latency-badge.is-high { color: var(--x); font-weight: 700; }

/* ===== Toast notification ===== */
.share-toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 200;
  padding: 12px 22px;
  background: rgba(31,29,46,0.95);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 999px;
  font-size: 0.95rem;
  box-shadow: 0 12px 30px rgba(0,0,0,0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s var(--ease);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.share-toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== Mobile tweaks ===== */
@media (max-width: 640px) {
  body { padding: 12px 10px 16px; gap: 12px; }
  .app-header { padding: 12px; gap: 10px; }
  .app-title { font-size: 1.45rem; }
  .header-actions { order: 2; }
  .mode-toggle { flex-basis: 100%; justify-content: center; order: 3; }
  .mode-btn { padding: 6px 10px; font-size: 0.8rem; flex: 1; }
  .board-wrapper { padding: 10px; }
  .score { padding: 8px 10px; }
  .score-value { font-size: 1.3rem; }
}

@media (max-width: 360px) {
  .mode-btn { padding: 5px 8px; font-size: 0.75rem; }
  .share-btn { width: 34px; height: 34px; }
  .streak-display { height: 34px; padding: 0 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .cell.x::after, .cell.o::after,
  .win-line, .mode-btn, .ctrl-btn, .share-btn,
  .stats-modal-panel, .cell.last-move {
    animation: none;
    transition: none;
  }
}
