:root {
  --ink: #2B2118;
  --ink-soft: #6B5D4F;
  --parchment: #F1E6D3;
  --parchment-raised: #E8D9BF;
  --wood-dark: #3E2A1E;
  --wood-mid: #5C3E29;
  --wood-hollow: #2A1B12;
  --seed-1: #8B5E34;
  --seed-2: #7A4E2A;
  --seed-3: #96693D;
  --brass: #C99A3E;
  --brass-bright: #E0B854;
  --clay: #A8452F;
  --rule: #D8C6A4;
  --shadow: 0 10px 30px -8px rgba(43, 33, 24, 0.45);
  --radius: 10px;
  font-size: 16px;
}

* { box-sizing: border-box; }

/* The native `hidden` attribute only works if no later, equally-or-more
   specific rule sets its own `display` — several elements in this file
   do (flex/grid containers especially), which silently defeats `hidden`
   for exactly those elements. Caught via a live test of the reverse-move
   count panel not actually hiding on cancel. This one rule guarantees
   `hidden` always wins, regardless of what any other selector sets. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--parchment);
  background-image:
    radial-gradient(circle at 15% 10%, rgba(201, 154, 62, 0.08), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(60, 40, 25, 0.06), transparent 45%);
  color: var(--ink);
  font-family: "Source Sans 3", sans-serif;
  min-height: 100vh;
}

h1, h2 {
  font-family: "Fraunces", serif;
  font-weight: 600;
  margin: 0;
}

/* ---------- Header ---------- */

.top {
  padding: 2.5rem 1.5rem 1.5rem;
  text-align: center;
}

.top-inner {
  max-width: 640px;
  margin: 0 auto;
}

.top h1 {
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  color: var(--wood-dark);
  letter-spacing: -0.01em;
}

.also-known {
  display: block;
  font-family: "Source Sans 3", sans-serif;
  font-weight: 400;
  font-size: 0.95rem;
  color: var(--ink-soft);
  margin-top: 0.35rem;
  letter-spacing: 0.01em;
}

.back-to-dictionary {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
}

.back-to-dictionary a {
  color: var(--ink-soft);
  text-decoration: none;
}

.back-to-dictionary a:hover {
  color: var(--wood-dark);
  text-decoration: underline;
}

.subhead {
  color: var(--ink-soft);
  font-size: 1rem;
  line-height: 1.5;
  margin: 0.75rem 0 0;
}

/* ---------- Game frame ---------- */

main {
  display: flex;
  justify-content: center;
  padding: 0.5rem 1rem 3rem;
}

.game-frame {
  width: 100%;
  max-width: 780px;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.player-strip {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 1rem;
  background: var(--parchment-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.player-strip.is-active {
  border-color: var(--brass);
  box-shadow: 0 0 0 3px rgba(201, 154, 62, 0.18);
}

.player-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--ink-soft);
  flex-shrink: 0;
}

.player-strip.is-active .player-dot {
  background: var(--brass-bright);
  box-shadow: 0 0 0 4px rgba(224, 184, 84, 0.25);
}

.player-name {
  font-weight: 600;
  flex: 1;
}

.player-score {
  font-family: "Space Mono", monospace;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

/* ---------- Board ---------- */

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  padding: 1.5rem 1.25rem;
  background: var(--wood-dark);
  background-image:
    repeating-linear-gradient(180deg, rgba(255,255,255,0.02) 0px, rgba(255,255,255,0.02) 2px, transparent 2px, transparent 5px);
  border-radius: 22px;
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(0,0,0,0.3), inset 0 2px 10px rgba(0,0,0,0.35);
}

.board-peg {
  position: absolute;
  left: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--wood-hollow);
  box-shadow: inset 0 2px 3px rgba(0,0,0,0.6), 0 1px 0 rgba(255,255,255,0.06);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 0.4rem;
  width: 100%;
}

.hole {
  position: relative;
  aspect-ratio: 1 / 0.82;
  border-radius: 8px;
  background: var(--wood-hollow);
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), inset 0 -1px 0 rgba(255,255,255,0.04);
  border: none;
  padding: 0;
  cursor: default;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Grid items default to min-width:auto, which refuses to shrink below
     the content's natural size — a hole with a large seed cluster (a
     named grouping's big hole, or a pile built up mid-game) could then
     force the whole 8-column row wider than a narrow viewport, pushing
     later columns off-screen entirely. Caught on a live mobile-viewport
     test, not just guessed at. */
  min-width: 0;
  overflow: hidden;
  transition: box-shadow 0.15s ease, transform 0.1s ease;
}

.hole.is-legal {
  cursor: pointer;
}

/* Seeds within a hole are arranged in a roughly square cluster rather
   than a single row — the number of columns and the seed size are both
   computed per-hole in JS from the seed count (--seed-cols, --seed-w,
   --seed-h custom properties) so a hole with 2 seeds and one with 18
   both read as a compact clump rather than a spilling line. */
.seed-cluster {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 1.5px;
  max-width: min(calc(var(--seed-cols, 2) * (var(--seed-w, 13px) + 1.5px)), 100%);
  /* Also a flex item (of .hole/.arrange-hole) with the same implicit
     min-width:auto problem — without this, the cluster's own "ideal"
     width could still force its parent hole wider than available space
     on a narrow viewport. The 100% cap above lets it wrap into more
     rows instead when there genuinely isn't room for the ideal shape. */
  min-width: 0;
}

.hole.is-legal:hover {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 2px var(--brass);
}

.hole.is-legal:focus-visible {
  outline: none;
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--brass-bright);
}

.hole.is-active-drop {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 2px var(--brass-bright);
  transform: scale(1.06);
}

.hole.is-capture-flash {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--clay);
  animation: capture-pulse 0.5s ease;
}

@keyframes capture-pulse {
  0% { transform: scale(1); }
  40% { transform: scale(0.85); }
  100% { transform: scale(1); }
}

.hole.is-manual-target {
  cursor: pointer;
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--brass-bright);
  animation: manual-target-pulse 1.1s ease-in-out infinite;
}

@keyframes manual-target-pulse {
  0%, 100% { box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--brass-bright); }
  50% { box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 6px rgba(224, 184, 84, 0.35); }
}

.hole.is-reverse-target {
  cursor: pointer;
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--clay);
  animation: reverse-target-pulse 1.8s ease-in-out infinite;
}

@keyframes reverse-target-pulse {
  0%, 100% { box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--clay); }
  50% { box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 7px rgba(168, 69, 47, 0.3); }
}

.seed {
  width: var(--seed-w, 13px);
  height: var(--seed-h, 11px);
  flex-shrink: 0;
  border-radius: 60% 40% 55% 45% / 55% 60% 40% 45%;
  box-shadow: inset -1px -1px 2px rgba(0,0,0,0.3), inset 1px 1px 1px rgba(255,255,255,0.15);
}

.seed:nth-child(3n) { background: var(--seed-1); }
.seed:nth-child(3n+1) { background: var(--seed-2); }
.seed:nth-child(3n+2) { background: var(--seed-3); }

.hole-count {
  position: absolute;
  bottom: -1.1rem;
  left: 50%;
  transform: translateX(-50%);
  font-family: "Space Mono", monospace;
  font-size: 0.68rem;
  color: var(--ink-soft);
  opacity: 0;
  transition: opacity 0.15s ease;
}

.hole:hover .hole-count,
.hole.is-legal .hole-count {
  opacity: 0.75;
}

/* ---------- Status bar ---------- */

.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.4rem 0.2rem;
}

.status-message {
  margin: 0;
  font-size: 0.95rem;
  min-height: 1.3em;
}

.status-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.move-count {
  font-family: "Space Mono", monospace;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.reverse-count-panel {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  background: var(--parchment-raised);
  border: 1.5px solid var(--brass);
  border-radius: var(--radius);
  padding: 0.6rem 0.9rem;
  margin-top: -0.3rem;
}

.reverse-count-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--wood-dark);
}

.reverse-count-options {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.reverse-count-option {
  font-family: "Space Mono", monospace;
  font-size: 0.85rem;
  font-weight: 700;
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  border: 1.5px solid var(--wood-mid);
  background: var(--parchment);
  color: var(--wood-dark);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.reverse-count-option:hover {
  background: var(--wood-dark);
  color: var(--parchment);
}

.btn-ghost, .btn-primary {
  font-family: "Source Sans 3", sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
  border: 1.5px solid var(--wood-mid);
  background: transparent;
  color: var(--wood-dark);
  transition: background 0.15s ease, color 0.15s ease;
}

.btn-ghost:hover {
  background: var(--wood-dark);
  color: var(--parchment);
}

.btn-primary {
  background: var(--wood-dark);
  color: var(--parchment);
  border-color: var(--wood-dark);
}

.btn-primary:hover {
  background: var(--brass);
  border-color: var(--brass);
  color: var(--wood-dark);
}

/* ---------- Dialogs ---------- */

dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  box-shadow: var(--shadow);
  max-width: 480px;
  width: 90vw;
}

#rules-dialog {
  max-width: 560px;
}

dialog::backdrop {
  background: rgba(20, 15, 10, 0.6);
}

.rules-panel, .win-panel {
  padding: 1.75rem 1.75rem 1.5rem;
  background: var(--parchment);
  max-height: 82vh;
  overflow-y: auto;
}

.rules-panel h2, .win-panel h2 {
  color: var(--wood-dark);
  margin-bottom: 0.75rem;
}

.rules-panel h3 {
  font-family: "Fraunces", serif;
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--wood-dark);
  margin: 1.25rem 0 0.35rem;
}

.rules-subnote {
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin: 0 0 0.6rem;
  line-height: 1.45;
}

.rules-panel ol {
  padding-left: 1.2rem;
  margin: 0 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  font-size: 0.92rem;
  line-height: 1.5;
}

.rules-panel ul {
  padding-left: 1.2rem;
  margin: 0 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--ink);
}

.rules-note {
  font-size: 0.82rem;
  color: var(--ink-soft);
  border-top: 1px solid var(--rule);
  padding-top: 0.75rem;
  line-height: 1.5;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 1rem;
}

.win-panel {
  text-align: center;
}

.win-eyebrow {
  font-family: "Space Mono", monospace;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--brass);
  margin: 0 0 0.35rem;
}

.win-panel h2 {
  font-size: 1.6rem;
}

.win-detail {
  color: var(--ink-soft);
  margin: 0.5rem 0 0;
}

.win-panel .dialog-actions {
  justify-content: center;
}

/* ---------- Setup dialog (choosing an opening grouping) ---------- */

#setup-dialog {
  max-width: 560px;
}

.setup-panel {
  padding: 1.75rem 1.75rem 1.5rem;
  background: var(--parchment);
}

.setup-eyebrow {
  font-family: "Space Mono", monospace;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--brass);
  margin: 0 0 0.35rem;
}

.setup-panel h2 {
  color: var(--wood-dark);
  font-size: 1.4rem;
  margin-bottom: 0.5rem;
}

.setup-intro {
  font-size: 0.88rem;
  color: var(--ink-soft);
  line-height: 1.5;
  margin: 0 0 1rem;
}

/* ---------- Online play (create/join/waiting) ---------- */

.online-code-input {
  width: 100%;
  font-family: "Space Mono", monospace;
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.3em;
  text-align: center;
  text-transform: uppercase;
  padding: 0.7rem 0.5rem;
  border-radius: var(--radius);
  border: 1.5px solid var(--wood-mid);
  background: var(--parchment-raised);
  color: var(--wood-dark);
  margin-bottom: 0.5rem;
}

.online-code-input:focus {
  outline: none;
  border-color: var(--brass);
}

.online-join-error {
  font-size: 0.85rem;
  color: var(--clay);
  margin: 0 0 0.5rem;
}

.online-room-code {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  background: var(--parchment-raised);
  border: 1.5px solid var(--brass);
  border-radius: var(--radius);
  padding: 0.9rem;
  margin-bottom: 0.75rem;
}

.online-room-code-value {
  font-family: "Space Mono", monospace;
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  color: var(--wood-dark);
}

#online-choice-dialog .dialog-actions,
#online-join-dialog .dialog-actions,
#online-wait-dialog .dialog-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.grouping-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 46vh;
  overflow-y: auto;
  padding-right: 0.25rem;
}

.grouping-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  border: 1.5px solid var(--rule);
  background: var(--parchment-raised);
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  font-family: "Source Sans 3", sans-serif;
}

.grouping-option:hover {
  border-color: var(--brass);
}

.grouping-option.is-selected {
  border-color: var(--brass);
  box-shadow: 0 0 0 2px rgba(201, 154, 62, 0.25);
  background: #F4E9D3;
}

.grouping-option-name {
  font-weight: 600;
  color: var(--wood-dark);
  font-size: 0.95rem;
}

.grouping-option-runyoro {
  font-family: "Space Mono", monospace;
  font-size: 0.68rem;
  color: var(--brass);
  margin-left: 0.5rem;
}

/* ---------- Movement-mode picker (in the setup dialog) ---------- */

.move-mode-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--wood-dark);
  margin: 1.1rem 0 0.5rem;
}

.move-mode-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.move-mode-option {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  text-align: left;
  padding: 0.65rem 0.8rem;
  border-radius: var(--radius);
  border: 1.5px solid var(--rule);
  background: var(--parchment-raised);
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  font-family: "Source Sans 3", sans-serif;
}

.move-mode-option:hover {
  border-color: var(--brass);
}

.move-mode-option.is-selected {
  border-color: var(--brass);
  box-shadow: 0 0 0 2px rgba(201, 154, 62, 0.25);
  background: #F4E9D3;
}

.move-mode-name {
  font-weight: 600;
  color: var(--wood-dark);
  font-size: 0.88rem;
}

.move-mode-desc {
  font-size: 0.76rem;
  color: var(--ink-soft);
  line-height: 1.4;
}

.grouping-option-desc {
  font-size: 0.82rem;
  color: var(--ink-soft);
  margin-top: 0.2rem;
  line-height: 1.4;
}

.grouping-option.is-custom {
  border-style: dashed;
}

/* ---------- Free-arrangement tray ---------- */

#arrange-dialog {
  max-width: 640px;
}

.arrange-panel {
  padding: 1.75rem 1.75rem 1.5rem;
  background: var(--parchment);
  max-height: 88vh;
  overflow-y: auto;
}

.arrange-panel h2 {
  color: var(--wood-dark);
  font-size: 1.3rem;
  margin-bottom: 0.4rem;
}

.tray-bar {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  background: var(--wood-dark);
  color: var(--parchment);
  border-radius: 999px;
  padding: 0.5rem 1.1rem;
  margin: 0.75rem 0 1.1rem;
  width: fit-content;
}

.tray-count {
  font-family: "Space Mono", monospace;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--brass-bright);
}

.tray-label {
  font-size: 0.82rem;
  opacity: 0.85;
}

.arrange-row-label {
  font-size: 0.78rem;
  color: var(--ink-soft);
  margin: 0.6rem 0 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.arrange-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.35rem;
  background: var(--wood-dark);
  border-radius: 10px;
  padding: 0.6rem;
}

.arrange-hole {
  aspect-ratio: 1 / 0.82;
  border-radius: 8px;
  background: var(--wood-hollow);
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55);
  border: none;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0; /* see the matching note on .hole -- same overflow problem, same fix */
  overflow: hidden;
  transition: box-shadow 0.15s ease;
}

.arrange-hole:hover {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 2px var(--brass);
}

.arrange-hole:disabled {
  cursor: not-allowed;
}

.arrange-hole:disabled:hover {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55);
}

.arrange-hole.is-locked {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 2px var(--brass);
  cursor: grab;
  touch-action: none;
}

.arrange-hole.is-locked:active {
  cursor: grabbing;
}

.arrange-hole.is-drop-target {
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.55), 0 0 0 3px var(--brass-bright);
  transform: scale(1.05);
}

.drag-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 62px;
  height: 50px;
  border-radius: 8px;
  background: var(--wood-hollow);
  box-shadow: 0 8px 20px rgba(0,0,0,0.5), 0 0 0 2px var(--brass-bright);
  opacity: 0.92;
  transform: translate(-50%, -50%);
}

.arrange-controls {
  display: flex;
  gap: 0.6rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

/* ---------- Responsive ---------- */

@media (max-width: 640px) {
  .board-wrap { padding: 1rem 0.6rem; }
  .board { gap: 0.25rem; }
  .seed { width: 9px; height: 8px; }
  .hole-count { display: none; }
  .top { padding: 1.75rem 1rem 1rem; }
  .status-bar { justify-content: center; text-align: center; }
}

@media (prefers-reduced-motion: reduce) {
  .hole, .hole.is-capture-flash, .hole.is-manual-target, .hole.is-reverse-target { animation: none !important; transition: none !important; }
}
