/* Peg Hop! — "Playful Paper" design system (spec §11).
   All tokens live here; components reference them exclusively. */

:root {
  /* surfaces */
  --paper: #FDF7EE;
  --card: #FFFFFF;
  --edge: #EADFCE;

  /* ink */
  --ink: #3F3D56;
  --ink-soft: #77738F;

  /* hero + accents */
  --coral: #FF6F61;
  --coral-deep: #D94A3D;
  --sun: #FFC145;
  --sun-deep: #D99A1F;
  --lilac: #A78BFA;
  --lilac-deep: #7C5CE0;
  --teal: #2EC4B6;

  /* board mats (cycled per level: ordinal % 4) */
  --mat-mint: #DCEFE2;   --mat-mint-edge: #BFDCCA;
  --mat-sky: #D8E9F9;    --mat-sky-edge: #B9D2EC;
  --mat-butter: #FBEFCF; --mat-butter-edge: #E7D6A8;
  --mat-blush: #FBDFD6;  --mat-blush-edge: #EFC3B6;

  /* geometry & motion */
  --radius-lg: 24px;
  --radius-md: 16px;
  --radius-sm: 10px;
  --shadow-card: 0 6px 18px rgba(63, 61, 86, 0.10);
  --shadow-pop: 0 10px 28px rgba(63, 61, 86, 0.16);
  --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out: cubic-bezier(0.22, 0.61, 0.36, 1);

  --font: 'Nunito', system-ui, 'Segoe UI', sans-serif;
}

/* ---- fonts (spec §11.3): one self-hosted variable file covers 400-900 ---- */

@font-face {
  font-family: 'Nunito';
  font-style: normal;
  font-weight: 400 900;
  font-display: swap;
  src: url('../assets/fonts/nunito-variable-latin.woff2') format('woff2');
}

/* ---- base ---- */

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden; /* home's decor blobs bleed past the viewport edge */
}

h1, h2, h3, p, table { margin: 0; }

button { font: inherit; color: inherit; }

a { color: inherit; text-decoration: none; }

#view { min-height: 100vh; }

/* ---- shared layout ---- */

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  max-width: 640px;
  margin: 0 auto;
}

.view-title {
  font-weight: 900;
  font-size: 28px;
}

.header-stars {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-weight: 700;
  color: var(--ink-soft);
}
.header-stars svg { width: 18px; height: 18px; }

/* ---- buttons (spec §11.4: "sticker" style) ---- */

.btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 14px 28px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  transition: transform 120ms var(--ease-out);
}
.btn:active { transform: translateY(2px); }

.btn-primary {
  background: var(--coral);
  color: #fff;
  border-bottom: 4px solid var(--coral-deep);
}
.btn-primary:hover { transform: translateY(-1px); }
.btn-primary:active { border-bottom-width: 2px; }

.btn-secondary {
  background: var(--card);
  color: var(--ink);
  border-bottom: 4px solid var(--edge);
}
.btn-secondary:hover { transform: translateY(-1px); }
.btn-secondary:active { border-bottom-width: 2px; }

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--card);
  border: 1px solid var(--edge);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 120ms var(--ease-out);
}
.icon-btn:hover { transform: translateY(-1px); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.bulb-neutral { color: var(--ink-soft); cursor: default; }

:focus-visible {
  outline: 3px solid var(--lilac);
  outline-offset: 2px;
}

/* ---- cards ---- */

.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--edge);
  box-shadow: var(--shadow-card);
}

/* ================= Home (spec §5.1, §12.2) ================= */

.decor {
  position: fixed;
  inset: 0;
  overflow: hidden;
  z-index: -1;
  pointer-events: none;
}
.decor-blob {
  position: absolute;
  border-radius: 50%;
  opacity: 0.4;
}

.home-shell {
  max-width: 420px;
  margin: 0 auto;
  padding: 48px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 8px;
  min-height: 100vh;
}

.wordmark {
  font-weight: 900;
  font-size: clamp(40px, 9vw, 56px);
  margin: 0;
  letter-spacing: -0.01em;
}
.wordmark .peg-dot { color: var(--coral); }

.home-logo { display: flex; align-items: center; justify-content: center; gap: 10px; }

.peg-accent {
  width: 0.7em;
  height: 0.7em;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 32%, #FF8A76, #F45D4E);
  border: 2px solid var(--coral-deep);
  display: inline-block;
  animation: peg-float 3s ease-in-out infinite;
}
@keyframes peg-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
@media (prefers-reduced-motion: reduce) {
  .peg-accent { animation: none; }
}

.tagline {
  color: var(--ink-soft);
  font-weight: 700;
  margin-bottom: 16px;
}

.home-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 280px;
}

.progress-teaser {
  margin-top: 20px;
  color: var(--ink-soft);
  font-weight: 700;
}

.home-footer {
  margin-top: auto;
  padding-top: 32px;
  color: var(--ink-soft);
  font-size: 13px;
}

/* ================= Levels (spec §5.2, §12.3) ================= */

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px 32px;
}

.level-card {
  background: var(--card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--edge);
  box-shadow: var(--shadow-card);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  transition: transform 120ms var(--ease-out), box-shadow 120ms var(--ease-out);
  position: relative;
}
.level-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-pop); }

.level-card.locked {
  cursor: default;
  filter: grayscale(0.6);
  opacity: 0.6;
}
.level-card.locked:hover { transform: none; box-shadow: var(--shadow-card); }

.level-card.next-up {
  border: 2px solid var(--coral);
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-ordinal { font-weight: 700; color: var(--ink-soft); font-size: 13px; }
.card-stars { display: inline-flex; gap: 2px; }
.card-stars svg { width: 14px; height: 14px; }
.card-lock { color: var(--ink-soft); }
.card-lock svg { width: 16px; height: 16px; }

.card-thumb {
  display: flex;
  justify-content: center;
  align-items: center;
}
.level-thumb { width: 120px; height: 90px; }

.card-name {
  font-weight: 700;
  font-size: 17px;
  text-align: center;
}

.card-chip {
  position: absolute;
  top: -8px;
  right: 10px;
  background: var(--coral);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 999px;
}

/* ================= How to Play (spec §5.3) ================= */

.howto-body {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 20px 48px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.step-diagrams {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--edge);
  border-radius: var(--radius-lg);
  padding: 16px;
}
.step { display: flex; flex-direction: column; align-items: center; gap: 4px; flex: 1; }
.step-diagram { width: 100%; max-width: 130px; height: 56px; }
.step-label {
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
}

.howto-section h2 {
  font-weight: 900;
  font-size: 19px;
  margin-bottom: 6px;
}
.howto-section p { color: var(--ink); }

.controls-table {
  width: 100%;
  border-collapse: collapse;
}
.controls-table tr:not(:last-child) td { border-bottom: 1px solid var(--edge); }
.controls-table td { padding: 8px 4px; }
.controls-table td.key {
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
  width: 1%;
  padding-right: 16px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  font-weight: 700;
}
.setting-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--coral);
}

/* ================= Play (spec §5.4, §12.4) ================= */

.play-shell {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: 96px; /* room for the sticky control bar on mobile */
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Narrow screens: the wrappers dissolve so their contents share one column,
   sequenced by `order` rather than by DOM position. */
.play-body {
  display: flex;
  flex-direction: column;
  width: 100%;
}
.play-main, .play-side { display: contents; }
.play-hud { order: 1; }
.play-result, .coach-mark { order: 2; }
.play-board-wrap { order: 3; }
.play-controls { order: 4; }

.play-topbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
}
.play-title { font-weight: 700; }
.play-title::before { content: ''; }
.play-topbar-icons { display: flex; gap: 8px; }

.play-hud {
  display: flex;
  gap: 10px;
  padding: 0 16px 12px;
}
.hud-chip {
  background: var(--card);
  border: 1px solid var(--edge);
  border-radius: 999px;
  padding: 6px 14px;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.hud-chip b {
  font-weight: 900;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
}
.hud-chip small {
  font-weight: 700;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
}
.hud-chip b.pop { animation: hud-pop 220ms var(--ease-bounce); }
@keyframes hud-pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.25); }
  100% { transform: scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .hud-chip b.pop { animation: none; }
}

.play-result {
  font-weight: 700;
  color: var(--ink-soft);
  text-align: center;
  padding: 0 16px 8px;
}

.play-board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  /* Boards keep a minimum peg size (see geometry.js); on very narrow screens
     the widest boards scroll sideways instead of shrinking further (§6.6). */
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

svg.board {
  width: 100%;
  height: auto;
  max-height: min(62vh, 560px);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.peg { cursor: pointer; }
.peg-inner { transform-box: fill-box; transform-origin: center; }

/* legal-landing ring (§11.5): rotating dashed coral circle */
.cup-hi {
  pointer-events: none;
  animation: cup-dash 4s linear infinite;
}
@keyframes cup-dash { to { stroke-dashoffset: -26; } }
.cup-warm { fill: rgba(255, 111, 97, 0.14); }
@media (prefers-reduced-motion: reduce) {
  .cup-hi { animation: none; }
}

.play-controls {
  width: 100%;
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  justify-content: center;
  position: sticky;
  bottom: 0;
  background: var(--card);
  border-top: 1px solid var(--edge);
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.control-btn {
  background: var(--card);
  color: var(--ink);
  border: none;
  border-bottom: 4px solid var(--edge);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  flex: 1;
  transition: transform 120ms var(--ease-out), opacity 120ms var(--ease-out);
}
.control-btn svg { width: 20px; height: 20px; }
.control-btn:not(:disabled):hover { transform: translateY(-1px); }
.control-btn:not(:disabled):active { transform: translateY(2px); border-bottom-width: 2px; }
.control-btn:disabled { opacity: 0.5; cursor: default; }

.control-btn-hint {
  background: var(--lilac);
  color: #fff;
  border-bottom-color: var(--lilac-deep);
}
.control-btn-hint:disabled { opacity: 0.5; }

/* Wide screens: board on the left, HUD and controls in a right-hand
   sidebar (spec §5.4). The wrappers become real boxes here. */
@media (min-width: 900px) {
  .play-shell {
    max-width: 1040px;
    padding: 0 24px 40px;
  }
  .play-body {
    flex-direction: row;
    align-items: flex-start;
    gap: 28px;
  }
  .play-main {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 1;
    min-width: 0; /* let the board shrink instead of overflowing the row */
  }
  .play-side {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 0 0 260px;
    position: sticky;
    top: 24px;
  }
  .play-hud { padding: 0; }
  .play-result { padding: 0; }
  .play-board-wrap { padding: 0; }
  .play-controls {
    position: static;
    flex-direction: column;
    border-top: none;
    background: transparent;
    padding: 0;
  }
  .control-btn {
    flex-direction: row;
    justify-content: flex-start;
    gap: 10px;
    font-size: 14px;
    padding: 12px 18px;
  }
  .coach-mark { margin: 0; }
}

/* ---- persistent shell nodes (populated in later milestones) ---- */

.toast-root {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 50;
  pointer-events: none;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 60;
}

/* ---- dragging peg (spec §6.5) ---- */

.peg.dragging {
  filter: drop-shadow(0 6px 8px rgba(63, 61, 86, 0.28));
}

/* ================= Modal (spec §11.4, §12.5) ================= */

.modal-scrim {
  position: fixed;
  inset: 0;
  background: rgba(63, 61, 86, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 70;
  animation: scrim-in 160ms var(--ease-out);
}
@keyframes scrim-in { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  position: relative;
  background: var(--card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--edge);
  box-shadow: var(--shadow-pop);
  padding: 28px 24px 24px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  animation: modal-in 260ms var(--ease-bounce);
}
@keyframes modal-in {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.modal-close svg { width: 18px; height: 18px; }

.modal-headline {
  font-weight: 900;
  font-size: 24px;
  margin-bottom: 12px;
}

.modal-stars {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 12px;
}
.modal-star { display: inline-flex; }
.modal-star svg { width: 44px; height: 44px; }
.modal-star.earned { animation: star-pop 350ms var(--ease-bounce) backwards; }
.modal-star:nth-child(1).earned { animation-delay: 0ms; }
.modal-star:nth-child(2).earned { animation-delay: 150ms; }
.modal-star:nth-child(3).earned { animation-delay: 300ms; }
@keyframes star-pop {
  from { transform: scale(0); }
  60% { transform: scale(1.3); }
  to { transform: scale(1); }
}

.modal-stats {
  font-weight: 700;
  color: var(--ink-soft);
}

.chip-best {
  display: inline-block;
  background: var(--teal);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 999px;
  margin-left: 6px;
}

.modal-reminder {
  color: var(--ink-soft);
  font-size: 14px;
  margin-top: 6px;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
}

@media (prefers-reduced-motion: reduce) {
  .modal-scrim, .modal-card, .modal-star.earned { animation: none; }
}

/* The dialog takes focus on open (see modal.js); it is a container, not a
   control, so it should not paint a focus ring of its own. */
.modal-card:focus { outline: none; }

/* ================= Solver features (spec §9) ================= */

/* Dead-end bulb: lit = a path to one peg survives, dead = proven stuck,
   neutral = no claim (still searching, or the budget ran out). */
.bulb { cursor: default; }
.bulb-lit { color: var(--sun); border-color: var(--sun); background: #FFF9EC; }
.bulb-dead { color: #A9A5B8; background: #F2F0F3; }
.bulb-neutral { color: var(--ink-soft); }

/* Hint ring (§11.5): same dashed circle as a legal move, in lilac, pulsing. */
.cup-hint {
  pointer-events: none;
  transform-box: fill-box;
  transform-origin: center;
  animation: hint-pulse 900ms var(--ease-out) 3;
}
@keyframes hint-pulse {
  0% { transform: scale(1); opacity: 1; }
  70% { transform: scale(1.25); opacity: 0.35; }
  100% { transform: scale(1); opacity: 1; }
}

/* Hint button spinner, shown only once a search is visibly slow. */
.control-btn-hint.loading svg {
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .cup-hint, .control-btn-hint.loading svg { animation: none; }
}

/* ---- toasts (spec §11.4) ---- */

.toast {
  background: var(--card);
  border: 1px solid var(--edge);
  border-radius: 999px;
  box-shadow: var(--shadow-pop);
  padding: 10px 20px;
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
  text-align: center;
  max-width: min(90vw, 420px);
  animation: toast-in 200ms var(--ease-out);
}
@keyframes toast-in {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
}

/* ================= Accessibility & polish (spec §12.4, §15) ============ */

/* Visible only to screen readers: the live region that narrates play. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* The board is one tab stop; the ring inside it marks the roving cell, so
   the element itself does not need a second outline. */
svg.board:focus { outline: none; }
svg.board:focus-visible { outline: none; }
.cell-focus { pointer-events: none; }

.coach-mark {
  margin: 0 16px 12px;
  background: var(--card);
  border: 2px solid var(--coral);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: 10px 16px;
  font-weight: 700;
  font-size: 14px;
  text-align: center;
  cursor: pointer;
  animation: coach-in 260ms var(--ease-bounce);
}
@keyframes coach-in {
  from { transform: translateY(-6px) scale(0.96); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .coach-mark { animation: none; }
}
