/* ==========================================================================
   peg solitaire — themes, tilt, and fanfare
   signature: the jump arc. one timing token drives every peg movement.
   ========================================================================== */

/* ---- shared tokens (type, motion, geometry) ---- */
:root {
  --display: "Marcellus", Georgia, serif;
  --body:    "Inter", system-ui, sans-serif;
  --mono:    "IBM Plex Mono", ui-monospace, monospace;

  --dur-jump:  350ms;
  --dur-sink:  180ms;
  --dur-fade:  260ms;
  --ease-jump: cubic-bezier(.42, .0, .28, 1);

  /* geometry — hole/peg radii and lift are consumed by board.js */
  --peg-r: 20;
  --hole-r: 22;

  /* tilt state, written by settings.applyTilt(deg) */
  --tilt-deg: 0;
  --tilt:     0deg;
  --lift:     46px;
}

/* ---- palette themes — swap tokens only, never structure ---- */

/* chalk = default. Also applied via :root so unset data-theme still renders. */
:root,
[data-theme="chalk"] {
  --surface:    #F1EBDB;
  --surface-2:  #E4DCC5;
  --rim:        #3A5978;
  --rim-hi:     #1F3A5A;
  --piece:      #1A2933;
  --piece-hi:   #2C4356;
  --piece-lo:   #0F1B24;
  --text:       #1A2933;
  --text-mute:  #5F6E7B;
  --warn:       #B44A3E;
  --overlay:    rgba(26, 41, 51, 0.08);
}

[data-theme="felt"] {
  --surface:    #264535;
  --surface-2:  #1E3628;
  --rim:        #B78A3E;
  --rim-hi:     #D9A957;
  --piece:      #EDE3CC;
  --piece-hi:   #F6EFDD;
  --piece-lo:   #B7A987;
  --text:       #EDE3CC;
  --text-mute:  #8FA095;
  --warn:       #8B2E2A;
  --overlay:    rgba(0, 0, 0, 0.18);
}

[data-theme="ink"] {
  --surface:    #0A0A0C;
  --surface-2:  #000000;
  --rim:        #F2C744;
  --rim-hi:     #FFE58A;
  --piece:      #FFFFFF;
  --piece-hi:   #FFFFFF;
  --piece-lo:   #BFBFBF;
  --text:       #FFFFFF;
  --text-mute:  #A9AAB2;
  --warn:       #FF6B5B;
  --overlay:    rgba(255, 255, 255, 0.06);
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --dur-jump: 0ms;
    --dur-sink: 0ms;
    --dur-fade: 120ms;
    --lift: 0px;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100dvh;
  background: var(--surface);
  color: var(--text);
  font-family: var(--body);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 32px 28px 48px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  min-height: 100dvh;
  position: relative;
}

/* ------------------------------ topbar --------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  border-bottom: 1px solid var(--overlay);
  padding-bottom: 14px;
}

h1 {
  font-family: var(--display);
  font-weight: 400;
  font-size: 28px;
  letter-spacing: 0.01em;
  margin: 0;
  color: var(--text);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.controls {
  display: flex;
  gap: 4px;
  align-items: center;
}

.controls button {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--text);
  font-family: var(--body);
  font-size: 14px;
  font-weight: 500;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 2px;
  transition: color 120ms ease, background-color 120ms ease;
}

.controls button:hover:not(:disabled),
.controls button:focus-visible {
  color: var(--rim-hi);
  outline: none;
}

.controls button:focus-visible {
  box-shadow: inset 0 -2px 0 var(--rim-hi);
}

.controls button:disabled {
  color: var(--text-mute);
  cursor: not-allowed;
}

.controls button + button {
  border-left: 1px solid var(--overlay);
  padding-left: 16px;
  margin-left: 4px;
}

.cog {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--text-mute);
  cursor: pointer;
  padding: 8px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 120ms ease, background-color 120ms ease;
  margin-left: 8px;
}

.cog:hover,
.cog:focus-visible {
  color: var(--rim-hi);
  background: var(--overlay);
  outline: none;
}

.cog svg {
  width: 18px;
  height: 18px;
}

/* ------------------------------ stage --------------------------------- */

.stage {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(220px, 1fr);
  gap: 40px;
  align-items: start;
}

@media (max-width: 720px) {
  .stage {
    grid-template-columns: 1fr;
  }
}

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  perspective: 1600px;
}

/* tilt-wrap owns the rotateX; separated so we can layer fanfare on top */
.tilt-wrap {
  width: 100%;
  max-width: 560px;
  transform: rotateX(var(--tilt));
  transform-origin: 50% 100%;
  transition: transform 320ms cubic-bezier(.4, 0, .2, 1);
  transform-style: preserve-3d;
}

@media (prefers-reduced-motion: reduce) {
  .tilt-wrap { transition: none; }
}

#board {
  width: 100%;
  height: auto;
  aspect-ratio: 500 / 450;
  overflow: visible;
  display: block;
}

/* ---- board frame (triangle outline drawn by board.js) ---- */

.frame {
  fill: none;
  stroke: var(--rim);
  stroke-opacity: 0.35;
  stroke-width: 1;
  stroke-linejoin: round;
}

/* ---- holes ---- */

.hole {
  fill: var(--surface);
  stroke: var(--rim);
  stroke-opacity: 0.55;
  stroke-width: 1.5;
}

.hole-shadow {
  fill: var(--surface-2);
  opacity: 0.55;
  pointer-events: none;
}

.hole.destination {
  stroke: var(--rim-hi);
  stroke-opacity: 1;
  stroke-width: 2;
  stroke-dasharray: 3 4;
  animation: pulse 1400ms ease-in-out infinite;
}

.hole.hint-dst {
  stroke: var(--rim-hi);
  stroke-opacity: 1;
  stroke-width: 2.5;
}

@keyframes pulse {
  0%, 100% { stroke-opacity: 0.55; }
  50%      { stroke-opacity: 1;    }
}

/* ---- pegs (three-piece cylinder: base shadow, side, cap) ---- */

.peg {
  cursor: pointer;
  transition: transform 120ms ease;
  transform-box: fill-box;
  transform-origin: center;
}

.peg .base {
  fill: var(--surface-2);
  opacity: 0.55;
  pointer-events: none;
}

.peg .side {
  fill: var(--piece-lo);
  pointer-events: none;
}

.peg .cap {
  fill: var(--piece);
  stroke: var(--piece-lo);
  stroke-width: 1;
}

.peg .gloss {
  fill: var(--piece-hi);
  opacity: 0.55;
  pointer-events: none;
}

/* ink theme: strip the gloss for a flat, high-contrast look */
[data-theme="ink"] .peg .gloss { opacity: 0; }
[data-theme="ink"] .peg .cap   { stroke-width: 1.5; }

.peg:hover .cap,
.peg:focus-visible .cap {
  fill: var(--rim-hi);
}

.peg:focus-visible {
  outline: none;
}

.peg:focus-visible .cap {
  stroke: var(--rim);
  stroke-width: 2;
}

.peg.selected .cap {
  fill: var(--rim);
  stroke: var(--rim-hi);
  stroke-width: 2;
}

.peg.hint-src .cap {
  fill: var(--rim-hi);
  animation: pulse-fill 1400ms ease-in-out infinite;
}

@keyframes pulse-fill {
  0%, 100% { fill: var(--rim-hi); }
  50%      { fill: var(--piece);  }
}

.peg.captured .cap {
  fill: var(--warn);
}

.peg.locked {
  pointer-events: none;
}

.peg.winner .cap {
  filter: drop-shadow(0 0 6px var(--rim-hi));
}

/* setup mode: all pegs are ghost pegs — faint, uniform, any can become the empty hole */
.peg.setup .cap {
  fill-opacity: 0.22;
  stroke: var(--piece);
  stroke-opacity: 0.5;
  stroke-width: 1.5;
}
.peg.setup .gloss { opacity: 0; }
.peg.setup .side  { opacity: 0; }
.peg.setup .base  { opacity: 0; }
.peg.setup { cursor: pointer; }

/* the current empty hole in setup mode — looks like a ghost peg */
.hole.empty-start {
  fill: var(--piece);
  fill-opacity: 0.18;
  stroke: var(--piece);
  stroke-opacity: 0.55;
  stroke-width: 2;
  cursor: pointer;
}

/* ------------------------------ hud --------------------------------- */

.hud {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-top: 12px;
}

.hud-line {
  margin: 0;
  font-family: var(--mono);
  font-size: 14px;
  color: var(--text);
  letter-spacing: 0.02em;
}

.hud-line span {
  color: var(--rim-hi);
}

.hud-count { font-size: 16px; }

.forecast[data-visible="false"] { display: none; }

.forecast {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  background: var(--overlay);
  border-left: 2px solid var(--rim);
  border-radius: 0 2px 2px 0;
}

.hud-safe { font-size: 13px; color: var(--text-mute); }
.hud-safe span { color: var(--text); }

/* ---- toggles — plain checkbox list, no lozenge switches ---- */

.toggles {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 4px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-mute);
  cursor: pointer;
  user-select: none;
  padding: 3px 0;
}

.toggle input {
  appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 2px;
  border: 1.5px solid var(--rim);
  background: transparent;
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  margin: 0;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.toggle input:checked {
  background: var(--rim);
  border-color: var(--rim);
}

.toggle input:checked::after {
  content: "";
  position: absolute;
  left: 2px;
  top: 0px;
  width: 5px;
  height: 8px;
  border: 2px solid var(--surface);
  border-top: none;
  border-left: none;
  transform: rotate(40deg);
}

.toggle input:focus-visible {
  outline: 2px solid var(--rim-hi);
  outline-offset: 2px;
}

.toggle:hover span,
.toggle input:checked ~ span { color: var(--text); }

/* ---- speed ---- */

.speed[data-visible="false"] { display: none; }

.speed {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 4px;
}

.speed label {
  font-size: 13px;
  color: var(--text-mute);
  display: flex;
  justify-content: space-between;
}

.speed label span {
  font-family: var(--mono);
  color: var(--rim-hi);
}

.speed input[type="range"] {
  width: 100%;
  accent-color: var(--rim);
}

/* ------------------------------ hint lines ---------------------------- */

.hint-lines {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

.hint-lines p {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
}

.hint-lines p[hidden] { display: none; }

.strategy { color: var(--text); }

.direction {
  font-family: var(--mono);
  color: var(--rim-hi);
}

/* ------------------------------ status ------------------------------- */

.status {
  min-height: 24px;
  font-family: var(--mono);
  font-size: 13px;
  color: var(--rim-hi);
  padding-top: 8px;
  border-top: 1px solid var(--overlay);
}

.status:empty { border-top-color: transparent; }

/* ------------------------------ settings popover ---------------------- */

.settings-popover {
  position: absolute;
  right: 28px;
  top: 76px;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--rim);
  border-radius: 4px;
  padding: 18px 20px;
  min-width: 260px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
  display: none;
  flex-direction: column;
  gap: 18px;
}

.settings-popover[open] {
  display: flex;
}

.settings-popover h2 {
  font-family: var(--body);
  font-weight: 500;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-mute);
  margin: 0 0 8px;
}

.theme-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.theme-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  border-radius: 3px;
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
  transition: background-color 120ms ease;
}

.theme-option:hover { background: var(--overlay); }

.theme-option input {
  appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1.5px solid var(--rim);
  margin: 0;
  cursor: pointer;
  position: relative;
}

.theme-option input:checked::after {
  content: "";
  position: absolute;
  inset: 2px;
  border-radius: 50%;
  background: var(--rim);
}

.theme-option input:focus-visible {
  outline: 2px solid var(--rim-hi);
  outline-offset: 2px;
}

.theme-swatches {
  display: inline-flex;
  gap: 3px;
  margin-left: auto;
}

.theme-swatches span {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

/* Static swatch preview colours — fixed regardless of active theme */
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 2px; border: 1px solid rgba(0,0,0,.15); }
.sw-chalk-a { background: #F1EBDB; }
.sw-chalk-b { background: #3A5978; }
.sw-chalk-c { background: #1A2933; }
.sw-felt-a  { background: #264535; }
.sw-felt-b  { background: #B78A3E; }
.sw-felt-c  { background: #EDE3CC; }
.sw-ink-a   { background: #0A0A0C; border-color: rgba(255,255,255,.15); }
.sw-ink-b   { background: #F2C744; }
.sw-ink-c   { background: #FFFFFF; }

.tilt-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tilt-row label {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-mute);
}

.tilt-row label span {
  font-family: var(--mono);
  color: var(--rim-hi);
}

.tilt-row input[type="range"] {
  width: 100%;
  accent-color: var(--rim);
}

/* ------------------------------ fanfare ------------------------------- */

.fanfare-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
  z-index: 5;
}

.spark {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--rim-hi);
  box-shadow: 0 0 6px var(--rim-hi);
  transform: translate(-50%, -50%);
  animation: spark-arc var(--spark-dur, 1200ms) cubic-bezier(.16, .8, .28, 1) forwards;
  will-change: transform, opacity;
  opacity: 0;
}

@keyframes spark-arc {
  0%   { transform: translate(-50%, -50%) translate(0, 0);                                           opacity: 0; }
  10%  {                                                                                             opacity: 1; }
  70%  { transform: translate(-50%, -50%) translate(var(--dx), calc(var(--dy) - var(--rise, 24px))); opacity: 1; }
  100% { transform: translate(-50%, -50%) translate(var(--dx), var(--dy));                           opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .spark {
    animation: spark-fade 300ms ease-out forwards;
  }
  @keyframes spark-fade {
    0%   { opacity: 0; }
    50%  { opacity: 1; }
    100% { opacity: 0; }
  }
}

.halo {
  position: absolute;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid var(--rim-hi);
  opacity: 0.5;
  animation: halo-breathe 2200ms ease-in-out infinite;
  will-change: transform, opacity;
}

@keyframes halo-breathe {
  0%, 100% { transform: translate(-50%, -50%) scale(0.9); opacity: 0.45; }
  50%      { transform: translate(-50%, -50%) scale(1.1); opacity: 0.15; }
}

.verdict {
  position: absolute;
  left: 50%;
  bottom: -8px;
  transform: translate(-50%, 100%);
  font-family: var(--display);
  font-size: 22px;
  color: var(--text);
  letter-spacing: 0.01em;
  opacity: 0;
  animation: verdict-in 480ms ease-out 200ms forwards;
}

@keyframes verdict-in {
  from { opacity: 0; transform: translate(-50%, 110%); }
  to   { opacity: 1; transform: translate(-50%, 100%); }
}
