/* =========================================================================
   Gomoku — presentation layer (the single source of visual truth).
   The renderer only toggles classes and sets a few custom properties; every
   look decision lives here.
   ========================================================================= */

:root {
  /* --- Wood palette -------------------------------------------------------
     A carved-hardwood identity: an oak playing surface framed in walnut, on a
     dark roasted-wood ground. Stones are the same material, told apart by tone
     rather than the usual black/white. */
  --wood-ground:      #241811;   /* space around the board                    */
  --wood-board:       #c69356;   /* oak playing surface                       */
  --wood-board-hi:    #d8ab72;   /* grain highlight                           */
  --wood-frame:       #6d4522;   /* walnut carved frame                       */
  --wood-frame-deep:  #472c15;   /* frame shadow line                         */

  --token-dark-core:  #6a4326;   /* walnut stone                              */
  --token-dark-hi:    #986a3f;
  --token-dark-rim:   #35200f;
  --token-light-core: #e4c592;   /* maple stone                               */
  --token-light-hi:   #f7e7c6;
  --token-light-rim:  #bf9a63;

  --grid-line:        rgba(46, 28, 12, 0.55);
  --grid-w:           1.5px;
  --hoshi:            rgba(46, 28, 12, 0.7);

  --ember:            #e8a13c;    /* last-move + winning glow accent           */
  --ember-strong:     #ffbf5b;

  --ink:              #f3e7d3;    /* primary text on the ground                */
  --ink-muted:        #b89b78;
  --panel:            #31221580;  /* translucent walnut for controls           */
  --panel-solid:      #3b2a1a;

  /* --- Sprite swap --------------------------------------------------------
     Placeholder tokens (the radial gradients below) show until real art loads.
     bridge.js injects the real paths at runtime, resolved through BASE_URL:

        --sprite-dark:  url("<BASE_URL>assets/dark.webp");
        --sprite-light: url("<BASE_URL>assets/light.webp");

     So just drop dark.webp / light.webp into assets/ — no CSS edit needed. The
     image layers over the gradient token; a missing file falls back to it. */
  --sprite-dark:  none;
  --sprite-light: none;

  --token-dark: radial-gradient(circle at 38% 32%,
                  var(--token-dark-hi) 0%,
                  var(--token-dark-core) 42%,
                  var(--token-dark-rim) 100%);
  --token-light: radial-gradient(circle at 38% 32%,
                  var(--token-light-hi) 0%,
                  var(--token-light-core) 46%,
                  var(--token-light-rim) 100%);

  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-body: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --radius: 10px;
  --board-max: 560px;
  --frame: max(8px, 2.4vmin);   /* walnut frame thickness (drawn via box-shadow) */
}

/* Dark theme — toggled by the shell through the bridge's onDark. A dimmer,
   candle-lit reading of the same wood, not a different game. */
:root[data-theme="dark"] {
  --wood-ground:      #130c07;
  --wood-board:       #a8783f;
  --wood-board-hi:    #bb8c53;
  --wood-frame:       #4a2f18;
  --wood-frame-deep:  #29190c;
  --ink:              #ece0cc;
  --ink-muted:        #a98d6b;
  --panel:            #2a1c1099;
  --panel-solid:      #2c1e12;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(120% 90% at 50% 0%, #30201400 0%, #17100a 100%),
    var(--wood-ground);
  min-height: 100dvh;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: clamp(12px, 4vw, 24px);
  display: flex;
  flex-direction: column;
  gap: clamp(12px, 3vw, 20px);
}

/* --- Toolbar ------------------------------------------------------------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
}
.brand { display: none; } /* title comes from the wrapper */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
  align-items: center;
}
.controls label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--ink-muted);
}
.controls select,
.controls button {
  font: inherit;
  font-size: 0.85rem;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid #0004;
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
}
.controls button:hover { background: var(--panel-solid); }
.controls button:disabled { opacity: 0.4; cursor: default; }
.controls select { -webkit-appearance: none; appearance: none; padding-right: 10px; }

/* --- Status bar ---------------------------------------------------------- */
.statusbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 2rem;
  font-size: clamp(0.95rem, 3.4vw, 1.05rem);
}
.turn-token {
  width: 1.15em;
  height: 1.15em;
  border-radius: 50%;
  flex: none;
  box-shadow: inset 0 0 0 1px #0003, 0 1px 2px #0005;
  background-size: cover;
  background-position: center;
}
.turn-token[data-color="dark"]  { background-image: var(--sprite-dark),  var(--token-dark); }
.turn-token[data-color="light"] { background-image: var(--sprite-light), var(--token-light); }
.turn-token[data-color="none"]  { background: transparent; box-shadow: none; }
.status-text { color: var(--ink); }
.status-text.is-result { font-family: var(--font-display); font-size: 1.15em; }

/* --- Board --------------------------------------------------------------- */
/* Space the status bar, board, and panel by at least the frame thickness so
   the board's box-shadow frame never bleeds over its neighbours. */
.gomoku {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: calc(var(--frame) + 6px);
}

.board-wrap { display: flex; justify-content: center; }

.board {
  --cell: calc(100% / var(--n));
  position: relative;
  width: min(94vw, 94vmin, var(--board-max));
  aspect-ratio: 1 / 1;
  padding: 0;
  border-radius: var(--radius);
  /* Walnut carved frame around the oak field. */
  box-shadow:
    0 0 0 var(--frame) var(--wood-frame),
    0 0 0 calc(var(--frame) + 2px) var(--wood-frame-deep),
    0 10px 26px #0008;
  background-color: var(--wood-board);
  /* Layer order: grid lines, then oak grain. */
  background-image:
    linear-gradient(var(--grid-line) var(--grid-w), transparent var(--grid-w)),
    linear-gradient(90deg, var(--grid-line) var(--grid-w), transparent var(--grid-w)),
    repeating-linear-gradient(92deg,
      var(--wood-board) 0, var(--wood-board) 5px,
      var(--wood-board-hi) 5px, var(--wood-board-hi) 6px),
    radial-gradient(130% 100% at 50% 30%, #ffffff14 0%, #00000018 100%);
  background-size:
    100% var(--cell),
    var(--cell) 100%,
    auto,
    auto;
  /* Offset the grid by half a cell so lines run through intersection centers. */
  background-position:
    0 calc(var(--cell) / 2),
    calc(var(--cell) / 2) 0,
    0 0,
    0 0;
  background-blend-mode: normal, normal, soft-light, normal;
}

.grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
}

/* Each intersection is a tap target; the stone sits centered on it. */
.cell {
  position: relative;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.cell:disabled { cursor: default; }
.cell:focus-visible {
  outline: 2px solid var(--ember-strong);
  outline-offset: -3px;
  border-radius: 4px;
}

/* Star points (hoshi), hidden once a stone covers them. */
.cell.hoshi::after {
  content: "";
  position: absolute;
  left: 50%; top: 50%;
  width: 22%; height: 22%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--hoshi);
}
.cell.filled.hoshi::after { display: none; }

/* Stones + ghost preview share a footprint centered on the intersection. */
.stone, .ghost {
  position: absolute;
  left: 50%; top: 50%;
  width: 82%; height: 82%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  pointer-events: none;
}

.stone {
  opacity: 0;
  box-shadow: 0 2px 3px #0006, inset 0 -2px 4px #0004, inset 0 2px 3px #ffffff30;
}
.cell.filled .stone { opacity: 1; }
.stone.is-dark  { background-image: var(--sprite-dark),  var(--token-dark); }
.stone.is-light { background-image: var(--sprite-light), var(--token-light); }

@media (prefers-reduced-motion: no-preference) {
  .cell.filled .stone { animation: settle 160ms ease-out; }
  @keyframes settle {
    from { transform: translate(-50%, -60%) scale(0.7); opacity: 0; }
    to   { transform: translate(-50%, -50%) scale(1);   opacity: 1; }
  }
}

/* Last move: a quiet ember ring. */
.cell.is-last .stone {
  box-shadow: 0 0 0 2px var(--ember), 0 2px 3px #0006, inset 0 -2px 4px #0004;
}
/* Winning five: a warm glow. */
.cell.is-winning .stone {
  box-shadow: 0 0 0 2px var(--ember-strong), 0 0 12px 2px var(--ember);
}
@media (prefers-reduced-motion: no-preference) {
  .cell.is-winning .stone { animation: settle 160ms ease-out, glow 1.1s ease-in-out infinite alternate; }
  @keyframes glow {
    from { box-shadow: 0 0 0 2px var(--ember-strong), 0 0 6px 1px var(--ember); }
    to   { box-shadow: 0 0 0 2px var(--ember-strong), 0 0 16px 4px var(--ember); }
  }
}

/* Ghost preview of the current stone on hover (human's turn only). */
.ghost { opacity: 0; background-image: var(--ghost-image, none); }
.board[data-input="human"] .cell:not(.filled):hover .ghost { opacity: 0.32; }

/* --- Swap2 action panel -------------------------------------------------- */
.action-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid #0004;
}
.action-panel[hidden] { display: none; }
.action-panel .prompt { font-size: 0.9rem; color: var(--ink-muted); }
.action-panel .choices { display: flex; flex-wrap: wrap; gap: 8px; }
.action-panel button {
  font: inherit;
  font-size: 0.9rem;
  color: var(--ink);
  background: var(--panel-solid);
  border: 1px solid #0005;
  border-radius: 8px;
  padding: 9px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.action-panel button:hover { border-color: var(--ember); }
.action-panel .swatch {
  width: 1.1em; height: 1.1em; border-radius: 50%;
  background-size: cover; box-shadow: inset 0 0 0 1px #0003;
}
.action-panel .swatch.is-dark  { background-image: var(--sprite-dark),  var(--token-dark); }
.action-panel .swatch.is-light { background-image: var(--sprite-light), var(--token-light); }
