:root {
  --bg: #f5f2ea;
  --surface: #fffdf8;
  --border: #e2ddce;
  --x-color: #bf5340;
  --o-color: #3f7797;
  --x-glow: rgba(191,83,64,0.16);
  --o-glow: rgba(63,119,151,0.16);
  --text: #3d3a33;
  --muted: #a49c8a;
  --accent: #bf5340;
  --cell: 46px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Rajdhani', sans-serif;
  min-height: 100vh;
  padding: 20px;
  overflow-x: hidden;
}

/* Dot grid bg */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: radial-gradient(circle, #e8e2d2 1px, transparent 1px);
  background-size: 28px 28px;
  pointer-events: none;
  z-index: 0;
}

/* ── Two-column layout: sidebar (info/controls) + board panel ── */
.layout {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 28px;
}

.sidebar {
  flex: 0 0 26%;
  min-width: 240px;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
}

.board-panel {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

@media (max-width: 820px) {
  .layout { flex-direction: column; align-items: center; }
  .sidebar {
    position: static;
    max-height: none;
    overflow-y: visible;
    width: 100%;
    max-width: 480px;
  }
  .board-panel { width: 100%; }
}

/* ── Header ── */
.header { text-align: center; }
.header h1 {
  font-size: clamp(1.6rem, 2.4vw, 2.1rem);
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: #2c2a25;
  text-shadow: 0 0 30px rgba(191,83,64,0.18);
}
.header .sub {
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.62rem;
  letter-spacing: 0.15em;
  color: var(--muted);
  margin-top: 4px;
}

/* ── Mode Tabs ── */
.mode-tabs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}
.mode-tabs button {
  padding: 5px 16px;
  background: transparent;
  border: 1px solid var(--muted);
  color: var(--muted);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.65rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s;
}
.mode-tabs button.active {
  border-color: var(--x-color);
  color: var(--x-color);
  background: rgba(191,83,64,0.08);
}

/* ── Feature Flags ── */
.flags-section {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: 100%;
}
.flags-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 4px 0;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--muted);
  transition: color 0.2s;
}
.flags-header:hover { color: #6b6455; }
.flags-chevron {
  display: inline-block;
  font-size: 0.6rem;
  transition: transform 0.2s;
  transform: rotate(90deg);
}
.flags-section.collapsed .flags-chevron { transform: rotate(0deg); }
.flags-label {
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.55rem;
  letter-spacing: 0.25em;
  color: inherit;
  text-transform: uppercase;
  text-align: center;
}
.flags-summary {
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.55rem;
  letter-spacing: 0.06em;
  color: #b7ae9a;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.5;
}
.flags-section.collapsed .flags-body {
  display: none;
}
.flags-body {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: 100%;
}
.flag-btn {
  flex: 1 1 auto;
  padding: 4px 14px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.58rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-align: center;
  cursor: pointer;
  border-radius: 12px;
  transition: all 0.2s;
}
.flag-btn:hover { border-color: #c7bfa9; color: #6b6455; }
.flag-btn.on {
  border-color: var(--o-color);
  color: var(--o-color);
  background: rgba(63,119,151,0.08);
}
.flag-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.flag-btn:disabled:hover { border-color: var(--border); color: var(--muted); background: transparent; }
.flag-btn.on:disabled:hover { border-color: var(--o-color); color: var(--o-color); background: rgba(63,119,151,0.08); }

/* One feature per line: button fills the row, icon pinned to the right
   edge so its tooltip can anchor consistently and never gets clipped
   by the sidebar's edge, unlike the old wrapped-pill layout. */
.flag-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; }

.info-icon {
  position: relative;
  width: 14px;
  height: 14px;
  flex: none;
  border-radius: 50%;
  border: 1px solid var(--muted);
  color: var(--muted);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.55rem;
  font-style: italic;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: help;
  transition: all 0.2s;
  outline: none;
}
.info-icon:hover, .info-icon:focus { border-color: var(--o-color); color: var(--o-color); }

.info-tooltip {
  position: absolute;
  bottom: calc(100% + 9px);
  right: 0;
  left: auto;
  transform: translateY(4px);
  width: 180px;
  padding: 8px 10px;
  background: #2c2a25;
  color: #f2eee1;
  font-family: 'Rajdhani', sans-serif;
  font-size: 0.72rem;
  font-style: normal;
  font-weight: 500;
  letter-spacing: 0.01em;
  text-transform: none;
  line-height: 1.35;
  border-radius: 5px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.25);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 10;
}
.info-tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: auto;
  right: 12px;
  border: 5px solid transparent;
  border-top-color: #2c2a25;
}
.info-icon:hover .info-tooltip,
.info-icon:focus .info-tooltip {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* ── Score Bar ── */
.scorebar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  gap: 16px;
}
.score-block { text-align: center; }
.score-block .name {
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.6rem;
  letter-spacing: 0.25em;
  color: var(--muted);
  text-transform: uppercase;
}
.score-block .val {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
  margin-top: 2px;
}
.score-block.col-x .val { color: var(--x-color); text-shadow: 0 0 14px var(--x-glow); }
.score-block.col-o .val { color: var(--o-color); text-shadow: 0 0 14px var(--o-glow); }
.score-block.col-d .val { color: #b3ab99; }
.score-sep { width: 1px; height: 40px; background: var(--border); }

/* ── Status ── */
.status {
  min-height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.65rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}
.status.x-turn { color: var(--x-color); }
.status.o-turn { color: var(--o-color); }
.status.win { animation: blink 0.9s infinite; }
.status.ai { color: #a49c8a; }
.status.illegal { color: #a83b26; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.45} }

/* ── Opening decisions ── */
.decision-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.choice-btn {
  padding: 7px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.6rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s;
}
.choice-btn:hover { background: rgba(191,83,64,0.08); border-color: var(--x-color); color: var(--x-color); }

/* ── Enclosure capture ── */
.capture-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.capture-btn {
  padding: 6px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.58rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s;
}
.capture-btn:hover:not(:disabled) { background: rgba(191,83,64,0.08); border-color: var(--x-color); color: var(--x-color); }
.capture-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.destroy-btn.armed {
  border-color: #a83b26;
  color: #a83b26;
  background: rgba(168,59,38,0.1);
  animation: destroyArmedPulse 1s ease-in-out infinite;
}
@keyframes destroyArmedPulse { 0%,100%{opacity:1} 50%{opacity:0.6} }

/* ── Board ── */
.board-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  box-shadow: 0 4px 30px rgba(61,58,51,0.08);
  transition: opacity 0.2s;
}
.board-wrap.inactive { opacity: 0.45; pointer-events: none; }
.board-wrap.fire-win { animation: boardFireGlow 1s ease-in-out infinite alternate; }
.board-wrap.tsunami-win { animation: boardWaveGlow 1.3s ease-in-out infinite; }
@keyframes boardFireGlow {
  0% { box-shadow: 0 4px 30px rgba(61,58,51,0.08), 0 0 30px rgba(255,90,20,0.15); }
  100% { box-shadow: 0 4px 30px rgba(61,58,51,0.08), 0 0 55px rgba(255,60,10,0.32); }
}
@keyframes boardWaveGlow {
  0%, 100% { box-shadow: 0 4px 30px rgba(61,58,51,0.08), 0 0 30px rgba(47,111,146,0.18); }
  50% { box-shadow: 0 4px 30px rgba(61,58,51,0.08), 0 0 55px rgba(47,111,146,0.35); }
}

.board {
  display: grid;
  grid-template-columns: repeat(10, var(--cell));
  grid-template-rows: repeat(10, var(--cell));
  gap: 2px;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  background: #efe9db;
  border: 1px solid #e1dac8;
  border-radius: 4px;
  cursor: pointer;
  position: relative;
  transition: background 0.12s;
  font-size: 1.4rem;
}
.cell:hover:not(.taken) { background: #e6ddc8; }
.cell.taken { cursor: default; }
.cell.win-cell { border-color: #cfc7b2; overflow: visible; z-index: 1; }

/* ── Red win: flames rise from every stone on the winning line ── */
.cell.win-cell.px {
  background: rgba(191,83,64,0.2) !important;
  animation: fireGlow 0.85s ease-in-out calc(var(--wi, 0) * 70ms) infinite alternate;
}
.cell.win-cell.px::before,
.cell.win-cell.px::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: 55%;
  width: 60%;
  height: 65%;
  background: radial-gradient(circle at 50% 100%, #fff5c2 0%, #ffb347 28%, #ff6a1f 58%, #c0270f 82%, transparent 100%);
  border-radius: 50% 50% 45% 45% / 60% 60% 40% 40%;
  transform-origin: bottom center;
  transform: translateX(-50%);
  mix-blend-mode: multiply;
  filter: saturate(1.3);
  animation: flameFlicker 0.5s ease-in-out calc(var(--wi, 0) * 70ms) infinite alternate;
  pointer-events: none;
  z-index: 2;
}
.cell.win-cell.px::after {
  width: 36%;
  height: 42%;
  bottom: 60%;
  background: radial-gradient(circle at 50% 100%, #fffdf2 0%, #ffe07a 32%, #ff8c3d 68%, transparent 100%);
  mix-blend-mode: normal;
  opacity: 0.9;
  animation-duration: 0.35s;
  animation-delay: calc(var(--wi, 0) * 70ms + 90ms);
}
@keyframes fireGlow {
  0% { box-shadow: 0 0 10px var(--x-glow) inset, 0 0 10px 2px rgba(255,110,30,0.3); }
  100% { box-shadow: 0 0 16px var(--x-glow) inset, 0 0 24px 8px rgba(255,80,10,0.55); }
}
@keyframes flameFlicker {
  0% { transform: translateX(-50%) scaleY(1) scaleX(1) rotate(-4deg); }
  50% { transform: translateX(-44%) scaleY(1.18) scaleX(0.88) rotate(3deg); }
  100% { transform: translateX(-53%) scaleY(0.92) scaleX(1.06) rotate(-2deg); }
}
.ember {
  position: absolute;
  bottom: 32%;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #ffcf6b;
  box-shadow: 0 0 5px 1px #ff7a1f;
  pointer-events: none;
  z-index: 3;
  opacity: 0;
  animation: emberRise 1.15s ease-in infinite;
}
@keyframes emberRise {
  0% { opacity: 0; transform: translate(0, 0) scale(1); }
  15% { opacity: 1; }
  100% { opacity: 0; transform: translate(var(--ex, 6px), -36px) scale(0.25); }
}

/* ── Blue win: a Hokusai-style wave crests over the winning line ── */
.cell.win-cell.po {
  background: rgba(63,119,151,0.2) !important;
  animation: waveCellGlow 1.1s ease-in-out calc(var(--wi, 0) * 90ms) infinite;
}
.cell.win-cell.po::before {
  content: '';
  position: absolute;
  left: -15%;
  right: -15%;
  bottom: 38%;
  height: 60%;
  background: linear-gradient(180deg, #eaf6fb 0%, #bfe3f0 22%, #6fb3d2 48%, #2f6f92 78%, #1c4f6c 100%);
  clip-path: polygon(0% 100%, 0% 46%, 10% 30%, 20% 42%, 30% 14%, 40% 36%, 50% 8%, 60% 30%, 70% 10%, 80% 32%, 90% 16%, 100% 34%, 100% 100%);
  transform-origin: bottom center;
  animation: waveSwell 1.1s ease-in-out calc(var(--wi, 0) * 90ms) infinite;
  pointer-events: none;
  z-index: 2;
}
.cell.win-cell.po::after {
  content: '';
  position: absolute;
  left: -15%;
  right: -15%;
  bottom: 66%;
  height: 16%;
  background: radial-gradient(ellipse at center, #ffffff 0%, rgba(255,255,255,0.75) 45%, transparent 78%);
  border-radius: 50%;
  animation: foamPulse 1.1s ease-in-out calc(var(--wi, 0) * 90ms) infinite;
  pointer-events: none;
  z-index: 3;
}
@keyframes waveCellGlow {
  0%, 100% { box-shadow: 0 0 10px var(--o-glow) inset, 0 0 10px 2px rgba(47,111,146,0.25); }
  50% { box-shadow: 0 0 16px var(--o-glow) inset, 0 0 22px 6px rgba(47,111,146,0.5); }
}
@keyframes waveSwell {
  0%, 100% { transform: translateY(5px) scaleY(0.88); }
  50% { transform: translateY(-7px) scaleY(1.1); }
}
@keyframes foamPulse {
  0%, 100% { opacity: 0.5; transform: translateY(3px) scaleX(0.9); }
  50% { opacity: 1; transform: translateY(-5px) scaleX(1.08); }
}
.droplet {
  position: absolute;
  top: 30%;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #eaf6fb;
  box-shadow: 0 0 4px 1px rgba(111,179,210,0.8);
  pointer-events: none;
  z-index: 3;
  opacity: 0;
  animation: dropletArc 1.3s ease-in infinite;
}
@keyframes dropletArc {
  0% { opacity: 0; transform: translate(0, 0) scale(1); }
  20% { opacity: 1; }
  100% { opacity: 0; transform: translate(var(--ex, 6px), 30px) scale(0.3); }
}
.cell.illegal-cell {
  background: rgba(191,83,64,0.3) !important;
  box-shadow: 0 0 0 2px var(--x-color) inset;
  animation: shake 0.3s;
}
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}
/* ── Intermediary 2/3/4-in-a-row flash (2s, non-repeating) ── */
.cell.intermediary-cell { overflow: visible; z-index: 1; }
.cell.intermediary-cell::after {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 6px;
  border: 2px solid currentColor;
  opacity: 0;
  pointer-events: none;
  z-index: 2;
  animation: midRing 2s ease-out forwards;
}
.cell.intermediary-cell.mid-x { color: var(--x-color); animation: midPulseX 2s ease-out forwards; }
.cell.intermediary-cell.mid-o { color: var(--o-color); animation: midPulseO 2s ease-out forwards; }
@keyframes midPulseX {
  0% { background: rgba(191,83,64,0.35); box-shadow: 0 0 14px 4px rgba(191,83,64,0.5) inset; }
  70% { background: rgba(191,83,64,0.12); box-shadow: 0 0 6px 1px rgba(191,83,64,0.2) inset; }
  100% { background: transparent; box-shadow: none; }
}
@keyframes midPulseO {
  0% { background: rgba(63,119,151,0.35); box-shadow: 0 0 14px 4px rgba(63,119,151,0.5) inset; }
  70% { background: rgba(63,119,151,0.12); box-shadow: 0 0 6px 1px rgba(63,119,151,0.2) inset; }
  100% { background: transparent; box-shadow: none; }
}
@keyframes midRing {
  0% { opacity: 0.9; transform: scale(0.85); }
  60% { opacity: 0.25; transform: scale(1.15); }
  100% { opacity: 0; transform: scale(1.3); }
}

/* ── Enclosure capture: preview flash (1s) ── */
.cell.preview-cell { overflow: visible; z-index: 1; }
.cell.preview-cell::after {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 6px;
  border: 2px dashed #a83b26;
  box-shadow: 0 0 16px 4px rgba(168,59,38,0.55) inset;
  pointer-events: none;
  z-index: 2;
  animation: previewFlash 1s ease-out forwards;
}
@keyframes previewFlash {
  0% { opacity: 1; transform: scale(0.9); }
  70% { opacity: 0.8; }
  100% { opacity: 0; transform: scale(1.15); }
}

.cell.grown-cell {
  background: rgba(63,119,151,0.18) !important;
  animation: growIn 0.5s ease;
}
@keyframes growIn {
  0% { opacity: 0; transform: scale(0.5); }
  100% { opacity: 1; transform: scale(1); }
}

.stone {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  animation: pop 0.18s cubic-bezier(0.34,1.56,0.64,1);
}
.stone.sx {
  background: radial-gradient(circle at 35% 35%, #e28a76, #b04a37);
  color: rgba(255,255,255,0.92);
  box-shadow: 0 2px 6px rgba(61,58,51,0.2), 0 0 10px var(--x-glow);
}
.stone.so {
  background: radial-gradient(circle at 35% 35%, #83b6d2, #336f92);
  color: rgba(255,255,255,0.92);
  box-shadow: 0 2px 6px rgba(61,58,51,0.2), 0 0 10px var(--o-glow);
}
@keyframes pop {
  0% { transform: scale(0.4); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* ── Controls ── */
.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.btn {
  padding: 9px 22px;
  background: transparent;
  border: 1px solid rgba(191,83,64,0.35);
  color: var(--x-color);
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.62rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s;
}
.btn:hover { background: rgba(191,83,64,0.08); border-color: var(--x-color); box-shadow: 0 0 16px rgba(191,83,64,0.12); }
.btn.secondary { border-color: #d8d2c1; color: #9c9585; }
.btn.secondary:hover { background: #ece5d5; border-color: #c7bfa9; color: #6b6455; }

/* ── Coord labels ── */
.coords-row {
  font-family: 'Share Tech Mono', monospace;
  font-size: 0.5rem;
  color: #b7ae9a;
  text-align: center;
}
.coords-row { display: flex; gap: 2px; padding: 0 8px; margin-bottom: 2px; }
.coords-row span { width: var(--cell); }

@media (max-width: 520px) {
  :root { --cell: 34px; }
  .stone { width: 24px; height: 24px; font-size: 0.65rem; }
}
