/* Mancala, a carved wooden board and polished stones on a calm sand-coloured page. Fixed palette. */
:root {
  color-scheme: light;
  --bg: #f2ebe1;
  --bg-2: #e9dfd1;
  --ink: #3b2a1f;
  --muted: #8a7563;
  --card: #fffaf3;
  --line: #e0d3c1;
  --wood-hi: #d39a5e;
  --wood: #bd7c43;
  --wood-lo: #97592a;
  --wood-edge: #74411c;
  --pit: #6a3a1c;
  --pit-deep: #47230e;
  --p0: #e3a33b;       /* bottom player: amber */
  --p1: #6fa79a;       /* top player: sage */
  --glow: #ffe2a3;
  --capture: #f08a64;
  /* tutorial kit (tutorial.css) */
  --tut-accent: #e3a33b;
  --tut-accent-ink: #3b2a1f;
  --tut-bubble: #fffaf3;
  --tut-ink: #3b2a1f;
  --tut-muted: #8a7563;
  --tut-dim: rgba(52, 34, 22, .5);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(ellipse at 50% 35%, #f8f3ec 0%, var(--bg) 60%, var(--bg-2) 100%) fixed;
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- bar ---------- */
.app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  max-width: 1000px; margin: 0 auto;
  padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(14px, env(safe-area-inset-left));
}
.bar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 60px; }
.back { color: var(--muted); text-decoration: none; font-size: 14px; white-space: nowrap; padding: 12px 2px; }
.back:hover, .back:focus-visible { color: var(--ink); }
.bar-actions { display: flex; gap: 8px; }

.btn {
  min-height: 44px; min-width: 44px; padding: 0 18px; border-radius: 22px;
  border: 0; background: var(--card); color: var(--ink);
  font-weight: 650; cursor: pointer; box-shadow: 0 1px 2px rgba(59, 42, 31, .12), 0 0 0 1px var(--line);
  transition: transform .12s, background .15s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn.icon { padding: 0; width: 44px; font-size: 22px; line-height: 1; letter-spacing: .04em; }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.primary { background: var(--ink); color: var(--card); box-shadow: none; }
.btn.wide { width: 100%; min-height: 50px; font-size: 17px; }
.btn:disabled, .row:disabled { opacity: .35; cursor: default; transform: none; }
.btn:focus-visible, .slot:focus-visible, .seg button:focus-visible, .row:focus-visible, .link:focus-visible {
  outline: 3px solid var(--p0); outline-offset: 2px;
}

/* ---------- game ---------- */
.game {
  flex: 1; min-height: 0;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px; padding-bottom: 14px;
}
.turn {
  justify-self: center; display: inline-flex; align-items: center; gap: 9px;
  padding: 8px 18px; border-radius: 999px; background: var(--card);
  box-shadow: 0 0 0 1px var(--line); font-weight: 650; font-size: 16px; min-height: 40px;
}
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; transition: background .2s; }
.dot.p0 { background: var(--p0); }
.dot.p1 { background: var(--p1); }
.turn.thinking #turn-text::after { content: ''; display: inline-block; width: 1.1em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
.toast {
  justify-self: center; min-height: 24px; font-size: 15px; color: var(--muted); text-align: center;
  opacity: 0; transform: translateY(4px); transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: none; }

/* ---------- board ---------- */
.board-wrap { min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; }
.board {
  --ar: 2.35;
  width: min(100%, calc(56vh * var(--ar)), 900px);
  aspect-ratio: var(--ar);
  display: grid;
  grid-template-columns: 0.95fr repeat(6, minmax(0, 1fr)) 0.95fr;
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 3.2% 1.2%;
  padding: 3.2% 2.4%;
  border-radius: 999px / 46%;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(255, 225, 180, .3), rgba(255, 225, 180, 0) 55%),
    repeating-linear-gradient(88deg, rgba(90, 44, 14, 0) 0 9px, rgba(90, 44, 14, .1) 9px 11px, rgba(90, 44, 14, 0) 11px 23px, rgba(255, 210, 150, .07) 23px 25px),
    linear-gradient(180deg, var(--wood-hi), var(--wood) 45%, var(--wood-lo));
  box-shadow: 0 16px 34px rgba(90, 55, 25, .28), 0 3px 0 var(--wood-edge), inset 0 2px 0 rgba(255, 232, 195, .35);
  user-select: none; -webkit-user-select: none;
}
@supports (container-type: size) {
  .board-wrap { container-type: size; }
  .board { width: min(100cqw, calc(100cqh * var(--ar)), 900px); }
}

.slot {
  position: relative; border: 0; padding: 0; margin: 0; min-width: 0; min-height: 0;
  background: radial-gradient(ellipse at 50% 38%, var(--pit-deep) 0%, var(--pit) 62%, #7a4523 100%);
  border-radius: 50%;
  box-shadow: inset 0 6px 12px rgba(0, 0, 0, .5), inset 0 -2px 3px rgba(255, 210, 160, .18), 0 1px 0 rgba(255, 228, 190, .35);
  cursor: default;
  transition: box-shadow .25s, transform .18s, filter .25s;
}
.slot.store { border-radius: 999px; }
.slot .well { position: absolute; inset: 0; border-radius: inherit; overflow: hidden; pointer-events: none; }
.stone {
  position: absolute; width: 22%; aspect-ratio: 1; height: auto;
  transform: translate(-50%, -50%); border-radius: 50%;
  box-shadow: 0 1.5px 1.5px rgba(0, 0, 0, .5), inset 0 -1.5px 2px rgba(0, 0, 0, .25);
}
.slot.store .stone { width: 19%; }
.stone.c0 { background: radial-gradient(circle at 34% 30%, #eef5f1 0 12%, #9dbcaf 40%, #5d8272 100%); }  /* jade */
.stone.c1 { background: radial-gradient(circle at 34% 30%, #fff3da 0 12%, #e2b672 42%, #a26f2c 100%); }  /* amber */
.stone.c2 { background: radial-gradient(circle at 34% 30%, #ffe8e0 0 12%, #cf9585 42%, #8e5445 100%); }  /* carnelian */
.stone.c3 { background: radial-gradient(circle at 34% 30%, #ffffff 0 14%, #ece4d2 44%, #ada084 100%); }  /* quartz */
.stone.c4 { background: radial-gradient(circle at 34% 30%, #eceef7 0 12%, #8e97b3 42%, #535a78 100%); }  /* slate */
.stone.c5 { background: radial-gradient(circle at 34% 30%, #f3eaf5 0 12%, #b39bb9 42%, #6f5677 100%); }  /* lilac */

.count {
  position: absolute; left: 50%; transform: translateX(-50%);
  min-width: 26px; padding: 1px 7px; border-radius: 999px;
  font-size: 14px; font-weight: 750; line-height: 20px; text-align: center; font-variant-numeric: tabular-nums;
  background: rgba(52, 28, 12, .78); color: #fbf1e2; pointer-events: none; z-index: 2;
}
.slot.bottom .count { bottom: -10px; }
.slot.top .count { top: -10px; }
.slot.store .count { bottom: 7%; font-size: 18px; line-height: 26px; min-width: 38px; }
.slot.store.own0 .count { background: var(--p0); color: var(--ink); }
.slot.store.own1 .count { background: var(--p1); color: var(--ink); }

/* states */
.slot.playable { cursor: pointer; box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 3px var(--glow), 0 0 16px rgba(255, 226, 163, .6); }
@media (hover: hover) { .slot.playable:hover { transform: translateY(-2px); } }
.slot.playable:active { transform: scale(.97); }
.slot.empty-own { filter: brightness(.94); }
.slot.last-from { box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 2px rgba(255, 246, 228, .7); }
.slot.playable.last-from { box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 3px var(--glow), 0 0 16px rgba(255, 226, 163, .6); }
.slot.lifted { filter: brightness(1.12); box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 3px #fff6e4; }
.slot.hot { box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 3px #fff6e4, 0 0 14px rgba(255, 246, 228, .7); }
.slot.hot .stone:last-child { animation: drop .2s ease-out; }
.slot.captured { box-shadow: inset 0 6px 12px rgba(0, 0, 0, .45), 0 0 0 4px var(--capture), 0 0 16px rgba(240, 138, 100, .6); }
@keyframes drop { from { transform: translate(-50%, -140%) scale(1.25); opacity: .4; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(59, 42, 31, .38);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px;
  animation: fade .2s ease-out;
}
.overlay.soft { background: rgba(242, 235, 225, .55); align-items: center; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet, .card {
  background: var(--card); color: var(--ink);
  width: 100%; max-width: 440px; max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  overflow: auto; overscroll-behavior: contain;
  border-radius: 24px; padding: 8px 20px 20px;
  box-shadow: 0 20px 50px rgba(59, 42, 31, .25);
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(16px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 600px) { .overlay { align-items: center; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; min-height: 52px; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.row {
  display: block; width: 100%; text-align: left; min-height: 50px; padding: 0 4px;
  border: 0; border-bottom: 1px solid var(--line); background: transparent; font-weight: 600; cursor: pointer;
}
.group { padding: 12px 0 4px; }
.label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: var(--bg); }
.seg button {
  flex: 1 1 0; min-height: 44px; border: 0; border-radius: 12px; background: transparent;
  font-weight: 600; cursor: pointer; color: var(--muted); transition: background .15s, color .15s;
}
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(59, 42, 31, .18); }
.note { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.menu-sheet .row:first-of-type { border-top: 1px solid var(--line); }
.menu-sheet #menu-help { margin-top: 10px; border-top: 1px solid var(--line); }
.menu-sheet .btn.wide { margin-top: 16px; }

.over-card { max-width: 340px; text-align: center; padding: 26px 22px 14px; }
.over-title { font-size: 28px; font-weight: 800; }
.over-score { display: flex; justify-content: center; gap: 18px; margin: 10px 0 20px; color: var(--muted); font-size: 15px; }
.over-score .side { display: inline-flex; align-items: center; gap: 6px; }
.over-score b { color: var(--ink); font-size: 20px; }
.over-score i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.over-score .p0 i { background: var(--p0); }
.over-score .p1 i { background: var(--p1); }
.link { display: block; margin: 8px auto 0; min-height: 44px; padding: 0 16px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }

.help-sheet { max-width: 560px; }
.help-body { padding-bottom: 6px; }
.help-body h3 { font-size: 16px; margin: 18px 0 4px; }
.help-body p, .help-body li { font-size: 15px; margin: 4px 0; }
.help-body ul { padding-left: 20px; }

/* ---------- small phones ---------- */
@media (max-width: 380px) {
  .app { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .btn { padding: 0 14px; }
  .back { font-size: 13px; }
  .count { font-size: 13px; min-width: 22px; padding: 0 5px; }
}

/* Portrait: taller pits so every pit stays a comfortable tap target (≥ 40 px wide at 360 px). */
@media (max-aspect-ratio: 1/1) {
  .board {
    --ar: 1.5; border-radius: 48px; gap: 4.5% 0.8%; padding: 5% 2%;
    grid-template-columns: 0.75fr repeat(6, minmax(0, 1fr)) 0.75fr;
  }
  .stone { width: 30%; }
  .slot.store .stone { width: 26%; }
}
@media (max-aspect-ratio: 1/1) and (min-width: 600px) {
  .board { --ar: 2; }
  .stone { width: 26%; }
}
@media (min-aspect-ratio: 1/1) and (max-height: 480px) {
  .bar { min-height: 50px; }
  .game { gap: 6px; padding-bottom: 8px; }
  .board { --ar: 2.9; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* Online play (online.js): no New game while a friend is playing; a presence line under the turn. */
body.is-online #btn-new { display: none; }
.turn-row { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.net { color: var(--muted); }
#over-leave { margin-top: 8px; }
/* The online rows now come first after the settings, so they take over How to play's gap and top line. */
.menu-sheet #menu-online, .menu-sheet #menu-leave { margin-top: 10px; border-top: 1px solid var(--line); }
.menu-sheet #menu-online ~ #menu-help { margin-top: 0; border-top: 0; }
