* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: #1a1a1a;
  color: #eee;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  gap: 12px;
}

/* ---------- Player panels ---------- */
.player {
  /* Match the board's width so panel edges and tray-side spacing align
     visually with the board's wooden frame. */
  width: min(92vw, calc(100vh - 284px));
  min-height: 110px;
  display: flex;
  align-items: center;
  /* 12px horizontal padding + 2px border = 14px visual inset on each side,
     matching the board's 14px wooden frame. 14px gap between icon and
     tray gives the same 14px of space on the tray's left. */
  gap: 14px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #262626;
  border: 2px solid transparent;
  transition: border-color 0.25s, background 0.25s;
  position: relative;
}

.player.active {
  border-color: #f5c34a;
  background: #2f2a1a;
  box-shadow: 0 0 14px rgba(245, 195, 74, 0.25);
}

.player.gameover.winner {
  border-color: #4ad17a;
  background: #1d2f1f;
}

.player-piece-wrap {
  /* Match the board piece diameter so the hand icon reads the same as
     the pieces on the board (on iPad Mini: 0.84 * (board_inner/8) ≈ 70px). */
  width: 70px;
  height: 70px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.piece-mini {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.piece-mini[data-mini-color="black"] {
  background: radial-gradient(circle at 30% 30%, #5a5a5a, #050505 70%);
  box-shadow: inset -2px -3px 6px rgba(255,255,255,0.08), 0 2px 4px rgba(0,0,0,0.5);
}

.piece-mini[data-mini-color="white"] {
  background: radial-gradient(circle at 30% 30%, #ffffff, #c9c9c9 70%);
  box-shadow: inset -2px -3px 6px rgba(0,0,0,0.18), 0 2px 4px rgba(0,0,0,0.5);
}

/* ---------- Hand tray inside player panel ---------- */
/* Tray is a horizontal strip; each .hand-piece is a tall thin vertical bar
   (a disc viewed edge-on). Alternating B/W across the row creates the
   striped "noodle" look from the reference photos. Stripes are sized to be
   noticeably smaller than the pieces on the board. */
.tray {
  flex: 1 1 auto;
  align-self: stretch;
  min-width: 0;
  margin: 4px 0;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: stretch;
  padding: 4px 5px;
  border-radius: 4px;
  background: linear-gradient(180deg, #050505, #1a1a1a 50%, #050505);
  box-shadow:
    inset 0 0 0 1px #000,
    inset 0 2px 3px rgba(0,0,0,0.8),
    0 1px 2px rgba(0,0,0,0.4);
  overflow: hidden;
}

/* Point-symmetric layout: rotating the screen 180° around the board maps
   the bottom (black) panel onto the top (white) panel.
   - Black panel (default row): [icon][tray] with stripes piled at the
     tray's LEFT (flex-start), empty on the right.
   - White panel (row-reverse at both panel and tray level): [tray][icon]
     with stripes piled at the tray's RIGHT, empty on the left.
   row-reverse on the tray itself also ensures the last-appended stripe
   sits at the inner edge of the pile, so removing handEl.lastElementChild
   always takes from the "top of stack" in both trays. */
.player-white {
  flex-direction: row-reverse;
}
.player-white .tray {
  flex-direction: row-reverse;
}

/* Each stripe = one double-sided disc on edge. Left half is the black
   face, right half the white face, with a darker seam in between.
   Width is FIXED so pieces don't get thicker as the hand shrinks — they
   stay the same size and the tray just gains empty space at the end. */
.hand-piece {
  flex: 0 0 15px;
  width: 15px;
  height: 100%;
  border-radius: 1px;
  background:
    linear-gradient(90deg,
      #2a2a2a 0%,
      #0a0a0a 40%,
      #050505 49%,
      #2a2a2a 50%,
      #e6e6e6 51%,
      #fafafa 60%,
      #dcdcdc 100%);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.12),
    inset 0 -1px 0 rgba(0,0,0,0.4),
    0 1px 1px rgba(0,0,0,0.55);
  transition: opacity 0.3s ease-out, transform 0.3s ease-out;
}
.hand-piece + .hand-piece {
  margin-left: 1px;
}

.hand-piece.leaving { opacity: 0; }
.player-white .hand-piece.leaving { transform: scale(0.3) translateY(14px); }
.player-black .hand-piece.leaving { transform: scale(0.3) translateY(-14px); }

/* ---------- Board ---------- */
.board-wrapper {
  position: relative;
  /* panels (110px each) + app paddings + gaps ≈ 284px vertical budget */
  width: min(92vw, calc(100vh - 284px));
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  background: #1f7a3a;
  border: 14px solid #3a210e;
  border-radius: 10px;
  box-shadow:
    inset 0 0 0 2px #0a3015,
    0 10px 30px rgba(0,0,0,0.6),
    0 4px 8px rgba(0,0,0,0.4);
  background-image:
    linear-gradient(135deg, rgba(255,255,255,0.04), rgba(0,0,0,0.06));
  position: relative;
}

/* Wooden grain hint on the frame */
.board::before {
  content: "";
  position: absolute;
  inset: -14px;
  border-radius: 10px;
  pointer-events: none;
  background:
    repeating-linear-gradient(90deg, rgba(255,255,255,0.04) 0 2px, transparent 2px 6px),
    linear-gradient(180deg, #4a2a14 0%, #2c1808 100%);
  border: 14px solid transparent;
  background-clip: border-box;
  z-index: -1;
}

.cell {
  position: relative;
  border-right: 1px solid #0a3015;
  border-bottom: 1px solid #0a3015;
  display: flex;
  align-items: center;
  justify-content: center;
}
.cell:nth-child(8n) { border-right: none; }
.cell:nth-last-child(-n+8) { border-bottom: none; }

/* Star points (Othello marker dots) at corners of cells (2,2)(2,6)(6,2)(6,6) */
.cell.star::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #07251a;
  bottom: -5px;
  right: -5px;
  z-index: 1;
}

.cell.valid::before {
  content: "";
  position: absolute;
  width: 24%;
  height: 24%;
  border-radius: 50%;
  background: rgba(255,255,255,0.22);
  pointer-events: none;
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(0.85); opacity: 0.6; }
  50% { transform: scale(1); opacity: 1; }
}

/* ---------- Pieces ---------- */
.piece {
  width: 84%;
  height: 84%;
  position: relative;
  pointer-events: none;
  z-index: 2;
  border-radius: 50%;
}

/* Two stacked face layers; the visible color is controlled by data-color.
   Flip animation is a coin-flip: scaleX 1 -> 0 -> 1. The script swaps
   data-color at mid-flip so the new face appears as it scales back. */

.piece::before,
.piece::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  backface-visibility: hidden;
}

.piece::before {
  background: radial-gradient(circle at 32% 28%, #6a6a6a, #0a0a0a 65%, #000 100%);
  box-shadow:
    inset -3px -4px 8px rgba(255,255,255,0.10),
    inset 3px 4px 8px rgba(0,0,0,0.5),
    0 3px 6px rgba(0,0,0,0.55);
  opacity: 1;
}

.piece::after {
  background: radial-gradient(circle at 32% 28%, #ffffff, #d6d6d6 65%, #b8b8b8 100%);
  box-shadow:
    inset -3px -4px 8px rgba(0,0,0,0.15),
    inset 3px 4px 8px rgba(255,255,255,0.55),
    0 3px 6px rgba(0,0,0,0.55);
  opacity: 0;
}

.piece[data-color="white"]::before { opacity: 0; }
.piece[data-color="white"]::after  { opacity: 1; }

.piece.flipping {
  animation: coin-flip 0.45s ease-in-out;
}

@keyframes coin-flip {
  0%   { transform: scaleX(1); }
  50%  { transform: scaleX(0.05); }
  100% { transform: scaleX(1); }
}

.piece.appear {
  animation: drop 0.28s ease-out;
}

@keyframes drop {
  0%   { transform: translateY(-18px) scale(0.4); opacity: 0; }
  60%  { transform: translateY(0) scale(1.05); opacity: 1; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

/* No-animation mode for popstate restoration */
.no-anim .piece,
.no-anim .piece-mini,
.no-anim .hand-piece {
  transition: none !important;
  animation: none !important;
}

/* ---------- Game over overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.62);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 100;
  pointer-events: none;
}
.overlay.visible {
  display: flex;
  animation: overlay-fade 0.35s ease-out;
}
.overlay-card {
  width: min(88vw, 520px);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  animation: overlay-pop 0.5s cubic-bezier(0.22, 1.4, 0.4, 1);
}
.overlay-illust {
  width: 100%;
  height: auto;
  filter: drop-shadow(0 10px 24px rgba(0, 0, 0, 0.5));
}
.overlay-illust svg {
  display: block;
  width: 100%;
  height: auto;
}
/* The only interactive element inside the overlay. The overlay itself has
   pointer-events: none so clicks don't hit the hidden board behind it, so
   this button must opt back in explicitly. Pressing it reloads the page —
   that's the only in-app way to reset mid-game state (see SPEC.md §2). */
.overlay-reset {
  pointer-events: auto;
  font: 700 18px/1 -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  letter-spacing: 0.08em;
  color: #1a1a1a;
  background: linear-gradient(180deg, #ffffff 0%, #e8e8e8 100%);
  border: 2px solid #1a1a1a;
  border-radius: 999px;
  padding: 12px 36px;
  cursor: pointer;
  box-shadow:
    0 4px 0 #1a1a1a,
    0 8px 18px rgba(0, 0, 0, 0.45);
  transition: transform 0.08s ease-out, box-shadow 0.08s ease-out;
  -webkit-tap-highlight-color: transparent;
}
.overlay-reset:hover {
  background: linear-gradient(180deg, #ffffff 0%, #f0f0f0 100%);
}
.overlay-reset:active {
  transform: translateY(3px);
  box-shadow:
    0 1px 0 #1a1a1a,
    0 3px 8px rgba(0, 0, 0, 0.45);
}
.overlay-reset:focus-visible {
  outline: 3px solid #ffe27a;
  outline-offset: 3px;
}

@keyframes overlay-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes overlay-pop {
  0%   { transform: scale(0.6) translateY(18px); opacity: 0; }
  70%  { transform: scale(1.03) translateY(0);    opacity: 1; }
  100% { transform: scale(1) translateY(0);       opacity: 1; }
}

/* ---------- Layout per orientation ---------- */
@media (orientation: portrait) {
  .app { padding: 20px 16px; }
}
