/* ============================================================
   Knucklebones — dark occult themed styling
   Dark gothic palette, crimson ritual accent, candle glow.
   ============================================================ */

:root {
  /* Palette */
  --color-void: #0d0a12;
  --color-night: #16111d;
  --color-crypt: #1f1828;
  --color-stone: #2c2438;
  --color-bone: #ece3d0;
  --color-bone-dim: #b6ab97;
  --color-blood: #b5202a;
  --color-blood-bright: #e23744;
  --color-ember: #f0a23b;
  --color-moss: #6f9461;

  /* Typography */
  --font-display: 'Cinzel Decorative', serif;
  --font-body: 'Spectral', Georgia, serif;

  /* Spacing & motion */
  --radius: 10px;
  --duration-fast: 140ms;
  --duration-normal: 320ms;
  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);

  --board-gap: 8px;

  /* Board width is capped by both the viewport width and the height left over
     after the masthead + arena + footer, so two boards always fit on screen. */
  --board-size: min(80vw, 280px, calc((100vh - 320px) / 2));
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-body);
  color: var(--color-bone);
  background:
    radial-gradient(circle at 50% -10%, #2a1d2e 0%, var(--color-void) 60%),
    var(--color-void);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
}

.vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at center, transparent 45%, rgba(0,0,0,0.65) 100%);
  z-index: 1;
}

.sound-toggle {
  position: fixed;
  top: 12px;
  right: 14px;
  z-index: 20;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid #2c2438;
  background: rgba(22, 17, 29, 0.7);
  color: var(--color-bone);
  font-size: 1.05rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--duration-fast) var(--ease-out-expo),
              border-color var(--duration-fast);
}
.sound-toggle:hover {
  transform: scale(1.08);
  border-color: var(--color-blood-bright);
}
.sound-toggle:focus-visible { outline: 2px solid var(--color-ember); outline-offset: 2px; }
.sound-toggle[aria-pressed="false"] { opacity: 0.6; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.app {
  position: relative;
  z-index: 2;
  flex: 1;
  width: min(1080px, 96vw);
  margin: 0 auto;
  padding: clamp(1rem, 2vw, 2rem) 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Painterly red-rimmed splotches in the corners, echoing the game's art. */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(closest-side at 6% 8%, rgba(122, 84, 60, 0.55), rgba(181, 32, 42, 0.22) 70%, transparent 78%),
    radial-gradient(closest-side at 96% 12%, rgba(122, 84, 60, 0.5), rgba(181, 32, 42, 0.2) 70%, transparent 80%),
    radial-gradient(closest-side at 92% 94%, rgba(122, 84, 60, 0.5), rgba(181, 32, 42, 0.22) 70%, transparent 80%),
    radial-gradient(closest-side at 4% 92%, rgba(122, 84, 60, 0.45), rgba(181, 32, 42, 0.18) 70%, transparent 82%);
  filter: blur(6px);
}

/* ---------- Masthead ---------- */
.masthead { text-align: center; margin-bottom: 1.4rem; }

.title {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(2.4rem, 1.5rem + 4vw, 4rem);
  letter-spacing: 0.04em;
  margin: 0;
  color: var(--color-bone);
  text-shadow: 0 0 18px rgba(226, 55, 68, 0.35), 0 2px 0 #000;
}

.subtitle {
  font-style: italic;
  color: var(--color-bone-dim);
  margin: 0.3rem 0 0;
  letter-spacing: 0.08em;
}

/* Decorative dice + flourishes above the title. */
.ornament {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}
.ornament__die { width: 26px; height: 26px; filter: drop-shadow(0 2px 3px rgba(0,0,0,0.6)); }
.flourish {
  font-size: 1.6rem;
  color: var(--color-bone-dim);
  opacity: 0.7;
}

.bone-glyph {
  width: 54px;
  height: 44px;
  margin: 0.5rem auto 0;
  filter: drop-shadow(0 0 10px rgba(226, 55, 68, 0.3));
}

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-display);
  font-size: 0.95rem;
  letter-spacing: 0.05em;
  color: var(--color-bone);
  background: linear-gradient(180deg, var(--color-stone), var(--color-crypt));
  border: 1px solid #3c324c;
  border-radius: var(--radius);
  padding: 0.85rem 1.4rem;
  cursor: pointer;
  transition: transform var(--duration-fast) var(--ease-out-expo),
              box-shadow var(--duration-fast), border-color var(--duration-fast);
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.btn:hover {
  transform: translateY(-2px);
  border-color: var(--color-blood-bright);
  box-shadow: 0 6px 22px rgba(181, 32, 42, 0.35);
}

.btn:active { transform: translateY(0); }

.btn:focus-visible {
  outline: 2px solid var(--color-ember);
  outline-offset: 2px;
}

.btn--primary {
  background: linear-gradient(180deg, var(--color-blood-bright), var(--color-blood));
  border-color: #7a141b;
}

.btn__hint {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 0.72rem;
  text-transform: lowercase;
  color: var(--color-bone-dim);
}

.btn--primary .btn__hint { color: rgba(255,255,255,0.75); }

/* ---------- Start menu ---------- */
.menu {
  margin-top: 2.2rem;
  text-align: center;
}
.menu__nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

/* Vertical menu items; the focused/hovered one gets a crimson ribbon. */
.menu-item {
  position: relative;
  font-family: var(--font-display);
  font-size: 1.25rem;
  letter-spacing: 0.04em;
  color: var(--color-bone-dim);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0.55rem 2.4rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  transition: color var(--duration-fast), transform var(--duration-fast);
}
.menu-item small {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 0.72rem;
  opacity: 0;
  transition: opacity var(--duration-fast);
}
.menu-item:hover,
.menu-item:focus-visible {
  color: #fff;
  outline: none;
  transform: scale(1.02);
}
.menu-item:hover small,
.menu-item:focus-visible small { opacity: 0.85; }

/* The red ribbon banner with notched ends. */
.menu-item::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, var(--color-blood-bright), var(--color-blood));
  clip-path: polygon(4% 0, 96% 0, 100% 50%, 96% 100%, 4% 100%, 0 50%);
  opacity: 0;
  transform: scaleX(0.8);
  transition: opacity var(--duration-fast), transform var(--duration-fast) var(--ease-out-expo);
  z-index: -1;
  box-shadow: 0 6px 20px rgba(181, 32, 42, 0.45);
}
.menu-item:hover::before,
.menu-item:focus-visible::before {
  opacity: 1;
  transform: scaleX(1);
}

/* ---------- Rules ---------- */
.menu__rules {
  margin: 1.8rem auto 0;
  max-width: 440px;
  text-align: left;
}

.rules {
  background: linear-gradient(180deg, var(--color-night), var(--color-crypt));
  border: 1px solid #2c2438;
  border-radius: var(--radius);
  padding: 1.2rem 1.4rem 1.3rem;
  color: var(--color-bone-dim);
  line-height: 1.5;
}
.rules__title {
  font-family: var(--font-display);
  font-size: 1.35rem;
  color: var(--color-bone);
  margin: 0 0 0.7rem;
  letter-spacing: 0.06em;
  text-align: center;
  text-shadow: 0 0 14px rgba(226, 55, 68, 0.3);
}
.rules__intro { margin: 0 0 1.2rem; }
.rules strong { color: var(--color-bone); }
.rules em { color: var(--color-ember); font-style: normal; font-weight: 600; }

.rules__block {
  border-top: 1px solid rgba(255, 255, 255, 0.07);
  padding-top: 0.9rem;
  margin-top: 0.9rem;
}
.rules__heading {
  font-family: var(--font-display);
  font-size: 1.05rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-ember);
  margin: 0 0 0.4rem;
}
.rules__heading--danger { color: var(--color-blood-bright); }
.rules__block p { margin: 0; }

/* Scoring reference table — mirrors the in-game rules card. */
.score-table {
  width: 100%;
  margin-top: 0.9rem;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  text-align: center;
}
.score-table th,
.score-table td {
  padding: 0.4rem 0.3rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.score-table thead th {
  font-family: var(--font-display);
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-bone-dim);
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}
.score-table tbody th {
  color: var(--color-bone);
  font-weight: 700;
  font-size: 1.05rem;
}
.score-table tbody td { color: var(--color-bone-dim); }
/* Highlight the triple column — the big payoffs. */
.score-table th:last-child,
.score-table td:last-child {
  color: var(--color-ember);
  font-weight: 600;
}
.score-table tbody tr:last-child th,
.score-table tbody tr:last-child td { border-bottom: none; }

.rules-card { max-width: 460px; text-align: left; }
.rules-card .rules { border: none; background: transparent; padding: 0.4rem 0.4rem 0; }
.rules-card .btn { align-self: center; margin-top: 1rem; }
.rules-card:has(.rules--mini) { max-width: 560px; }

/* Mini in-game rules: two side-by-side concept panels. */
.rules--mini { text-align: center; }
.rules--mini .rules__intro { text-align: center; margin-bottom: 1.4rem; }
.mini-grid {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 1.2rem;
}
.mini-grid__sep {
  align-self: center;
  color: var(--color-blood-bright);
  font-size: 1.4rem;
}
.mini-rule {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}
.mini-rule p { margin: 0; }
.mini-rule .rules__heading { margin: 0.2rem 0 0; }
.mini-dice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-bottom: 0.3rem;
}
.mini-die {
  width: 46px;
  height: 46px;
  filter: drop-shadow(0 3px 5px rgba(0,0,0,0.5));
}
.mini-rule:first-child .mini-die:last-child { transform: translateX(8px); }
.mini-rule:first-child .mini-die:first-child { transform: translateX(-8px); }

@media (max-width: 480px) {
  .mini-grid { flex-direction: column; gap: 1rem; }
  .mini-grid__sep { display: none; }
}

/* Subtle text button to open rules during a match. */
.rules-link {
  background: none;
  border: none;
  color: var(--color-bone-dim);
  font-family: var(--font-body);
  font-style: italic;
  font-size: 0.82rem;
  letter-spacing: 0.03em;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
  transition: color var(--duration-fast);
}
.rules-link:hover { color: var(--color-ember); }
.rules-link:focus-visible { outline: 2px solid var(--color-ember); outline-offset: 2px; }

/* ---------- Game layout ---------- */
.game {
  width: 100%;
  animation: fade-in var(--duration-normal) var(--ease-out-expo);
}

/* Compact the menu hero once a match is on screen. */
body:has(#game:not([hidden])) .masthead { margin-bottom: 0.4rem; }
body:has(#game:not([hidden])) .title { font-size: clamp(1.6rem, 1rem + 2.5vw, 2.4rem); }
body:has(#game:not([hidden])) .subtitle,
body:has(#game:not([hidden])) .ornament,
body:has(#game:not([hidden])) .bone-glyph { display: none; }
body:has(#game:not([hidden])) .app { padding-top: 0.6rem; padding-bottom: 0.4rem; }
body:has(#game:not([hidden])) .footer { display: none; }

/* Stage: player cards flank the central boards on wide screens. */
.stage {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.8rem;
  justify-items: center;
}
.center {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  width: 100%;
  align-items: center;
}

@media (min-width: 720px) {
  .stage {
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 1.2rem;
  }
  /* Left player owns the bottom board (p1); right player owns the top board (p2).
     Vs AI, p1 is the human — so the human always sits on the left. */
  .player-card[data-player="p1"] { order: 1; }
  .center { order: 2; }
  .player-card[data-player="p2"] { order: 3; }
}

/* Player identity card (avatar, name, total score). */
.player-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  transition: border-color var(--duration-normal), box-shadow var(--duration-normal);
}
.player-card.is-active {
  border-color: var(--color-blood-bright);
  box-shadow: 0 0 24px rgba(226, 55, 68, 0.22);
}
.avatar {
  width: 84px;
  height: 84px;
}
.avatar__svg { width: 100%; height: 100%; display: block; }
.player-card__name {
  font-family: var(--font-display);
  font-size: 1.05rem;
  letter-spacing: 0.05em;
  color: var(--color-bone);
  margin: 0;
}
.player-card__score {
  font-size: 2rem;
  font-weight: 600;
  color: var(--color-ember);
  font-variant-numeric: tabular-nums;
  margin: 0;
  line-height: 1;
}

/* Board + its per-column scores. */
.board-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  border: 1px solid #2c2438;
  background: linear-gradient(180deg, var(--color-night), var(--color-crypt));
  transition: border-color var(--duration-normal), box-shadow var(--duration-normal);
}
.board-wrap.is-active {
  border-color: var(--color-blood-bright);
  box-shadow: 0 0 26px rgba(226, 55, 68, 0.25);
}

.board-scores {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--board-gap);
  width: var(--board-size);
  margin: 0 auto;
}
.col-score {
  text-align: center;
  font-family: var(--font-display);
  font-size: 1rem;
  color: var(--color-ember);
  font-variant-numeric: tabular-nums;
  min-height: 1.2em;
}

/* ---------- Board ---------- */
.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--board-gap);
  width: var(--board-size);
  margin: 0 auto;
}

.column {
  display: flex;
  flex-direction: column;
  gap: var(--board-gap);
  background: rgba(0,0,0,0.25);
  border-radius: 8px;
  padding: var(--board-gap);
  border: 1px solid transparent;
  transition: border-color var(--duration-fast), background var(--duration-fast);
}

/* Opponent board stacks downward so dice meet in the middle arena */
.board-wrap--opponent .column { flex-direction: column-reverse; }

.column.is-playable { cursor: pointer; }
.column.is-playable:hover {
  border-color: var(--color-ember);
  background: rgba(240, 162, 59, 0.08);
}
.column.is-full { opacity: 0.85; }

.cell {
  aspect-ratio: 1 / 1;
  border-radius: 7px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.05);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Dice ---------- */
.pip-die {
  width: 100%;
  height: 100%;
  border-radius: 7px;
  background: linear-gradient(155deg, #f4ecdb, #d8cbb0);
  color: var(--color-void);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 14%;
  gap: 6%;
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.25), 0 2px 5px rgba(0,0,0,0.5);
  animation: drop-in var(--duration-normal) var(--ease-out-expo);
}

/* Two matching dice in a column → amber; three → blue. */
.pip-die.tier-pair {
  background: linear-gradient(155deg, #f4d77a, #d8b13f);
}
.pip-die.tier-triple {
  background: linear-gradient(155deg, #9fcdee, #4f8fc7);
}

/* The active roll's matching value gets a soft ring while choosing a column. */
.pip-die.is-target {
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.25), 0 0 0 2px var(--color-ember), 0 0 12px rgba(240,162,59,0.6);
}

.pip-die.is-destroying {
  animation: shatter 450ms var(--ease-out-expo) forwards;
  z-index: 2;
}

.pip {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: var(--color-void);
  align-self: center;
  justify-self: center;
}

/* pip grid positions per face */
.pip[data-pos="tl"] { grid-area: 1 / 1; }
.pip[data-pos="tr"] { grid-area: 1 / 3; }
.pip[data-pos="ml"] { grid-area: 2 / 1; }
.pip[data-pos="mc"] { grid-area: 2 / 2; }
.pip[data-pos="mr"] { grid-area: 2 / 3; }
.pip[data-pos="bl"] { grid-area: 3 / 1; }
.pip[data-pos="br"] { grid-area: 3 / 3; }

/* ---------- Arena (roll cup + banner) ---------- */
.arena {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0;
}

/* Each player's engraved bone tray where their die is rolled. */
.tray {
  margin-top: 0.5rem;
  width: 116px;
  height: 74px;
  border-radius: 8px;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.025) 0 6px, transparent 6px 12px),
    linear-gradient(180deg, #161019, #0c0910);
  border: 2px solid #2c2438;
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.5), inset 0 0 18px rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color var(--duration-normal), box-shadow var(--duration-normal);
}
/* Highlight the active player's tray. */
.player-card.is-active .tray {
  border-color: var(--color-blood-bright);
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.5), 0 0 18px rgba(226, 55, 68, 0.4);
}

.tray__die {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tray__die .pip-die {
  width: 100%;
  height: 100%;
  border-radius: 11px;
  box-shadow: inset 0 -4px 8px rgba(0,0,0,0.25), 0 4px 12px rgba(0,0,0,0.6);
}
.tray__die .pip-die.is-rolling { animation: roll 0.5s ease-in-out; }

.turn-banner {
  font-family: var(--font-display);
  letter-spacing: 0.05em;
  margin: 0;
  min-height: 1.4em;
  text-align: center;
  color: var(--color-bone-dim);
}

.roll-btn { margin-top: 0.2rem; }

/* ---------- Overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(5, 3, 8, 0.82);
  backdrop-filter: blur(3px);
  animation: fade-in var(--duration-normal) var(--ease-out-expo);
}
.overlay__card {
  text-align: center;
  background: linear-gradient(180deg, var(--color-crypt), var(--color-night));
  border: 1px solid var(--color-blood);
  border-radius: 14px;
  padding: 2rem 2.4rem;
  box-shadow: 0 0 50px rgba(181,32,42,0.4);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  max-width: 90vw;
}
.overlay__result {
  font-family: var(--font-display);
  font-size: 2rem;
  margin: 0;
  text-shadow: 0 0 16px rgba(226,55,68,0.4);
}
.overlay__scores { color: var(--color-bone-dim); margin: 0 0 0.6rem; }

/* ---------- Footer ---------- */
.footer {
  position: relative;
  z-index: 2;
  text-align: center;
  font-size: 0.78rem;
  color: var(--color-bone-dim);
  padding: 1rem;
  opacity: 0.7;
}

/* ---------- Animations ---------- */
@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes drop-in {
  from { opacity: 0; transform: translateY(-14px) scale(0.85); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes shatter {
  0%   { opacity: 1; transform: scale(1) rotate(0); }
  40%  { opacity: 1; transform: scale(1.12) rotate(-4deg); filter: brightness(1.5); }
  100% { opacity: 0; transform: scale(0.4) rotate(12deg); }
}
@keyframes roll {
  0%   { transform: rotate(0) scale(1); }
  30%  { transform: rotate(160deg) scale(1.12); }
  60%  { transform: rotate(300deg) scale(0.94); }
  100% { transform: rotate(360deg) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

@media (max-width: 420px) {
  .tray { width: 100px; height: 64px; }
  .player-card__score { font-size: 1.6rem; }
}
