/* Holiday Chain — vintage holiday-card look: cream paper, evergreen and cranberry ink. */
:root {
  --cream: #f5ead3;
  --paper: #fffaf0;
  --edge: #dccba3;
  --evergreen: #1f4d3a;
  --evergreen-deep: #163a2c;
  --cranberry: #9b1b30;
  --cranberry-deep: #7d1426;
  --gold: #c9962b;
  --ink: #2c2a25;
  --cols: 8;
  --rows: 8;
  /* Header banner height. It shrinks on short screens so the board keeps its room. */
  --head: clamp(44px, 9vh, 80px);
  /* The board fits the width, and also the height left after the banner, HUD and legend. */
  --board: min(100vw - 20px, 500px, (100vh - 258px - var(--head)) * var(--cols) / var(--rows));
}
@supports (height: 100dvh) {
  :root { --board: min(100vw - 20px, 500px, (100dvh - 258px - var(--head)) * var(--cols) / var(--rows)); }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--cream);
  background-image: radial-gradient(circle at 1px 1px, rgba(31, 77, 58, 0.07) 1px, transparent 0);
  background-size: 14px 14px;
  color: var(--ink);
  font-family: Georgia, 'Iowan Old Style', 'Times New Roman', serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation; /* no double-tap zoom on buttons */
  overflow-x: hidden;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 580px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
}
.screen { flex: 1; display: flex; flex-direction: column; align-items: center; }

/* --- Start and results cards --- */
.card-screen, #screen-game { justify-content: center; }
.greeting {
  width: 100%;
  max-width: 420px;
  text-align: center;
  background: var(--paper);
  border: 2px solid var(--evergreen);
  outline: 1px solid var(--cranberry);
  outline-offset: -9px;
  border-radius: 6px;
  padding: 36px 26px 34px;
  box-shadow: 0 3px 0 var(--edge), 0 14px 30px rgba(44, 42, 37, 0.12);
}
.garland { display: flex; justify-content: center; gap: 10px; margin-bottom: 14px; }
.garland svg { width: 40px; height: 40px; }
.eyebrow {
  margin: 0;
  font-size: 0.8rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--cranberry);
}
h1, h2 { margin: 6px 0 0; color: var(--evergreen); font-weight: 700; line-height: 1.05; }
h1 { font-size: clamp(2.4rem, 11vw, 3.3rem); font-style: italic; }
h2 { font-size: 1.7rem; font-style: italic; }
.rule { margin: 10px 0; color: var(--gold); font-size: 1.1rem; }
.instruction { margin: 0 auto 24px; max-width: 30ch; font-size: 1.08rem; line-height: 1.45; }
.big-score { margin: 6px 0 0; font-size: 4rem; font-weight: 700; color: var(--cranberry); line-height: 1; font-variant-numeric: tabular-nums; }
.new-best { margin: 10px 0 0; font-style: italic; color: var(--evergreen); font-weight: 700; }
.best { margin: 0 0 24px; font-size: 1.1rem; }
.btn {
  font: inherit;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--paper);
  background: var(--cranberry);
  border: 0;
  border-radius: 999px;
  min-width: 190px;
  min-height: 52px;
  padding: 12px 34px;
  box-shadow: 0 4px 0 var(--cranberry-deep);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--cranberry-deep); }
.btn:focus-visible { outline: 3px solid var(--evergreen); outline-offset: 3px; }
/* The quieter second choice under the main button */
.btn-link {
  display: block;
  margin: 14px auto 0;
  min-height: 44px;
  padding: 8px 12px;
  font: inherit;
  font-size: 0.95rem;
  color: var(--evergreen);
  background: none;
  border: 0;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}
.btn-link:focus-visible { outline: 3px solid var(--evergreen); outline-offset: 2px; border-radius: 6px; }

/* --- High scores --- */
.btn-small { min-width: 0; min-height: 48px; padding: 8px 22px; font-size: 1.05rem; }
.btn-link:not([hidden]) + .btn-link { margin-top: 0; }
.initials { margin: 0 0 14px; }
#screen-results .greeting { padding-top: 26px; padding-bottom: 26px; }
#screen-results .best { margin-bottom: 16px; }
.initials label { display: block; margin-bottom: 10px; font-weight: 700; color: var(--evergreen); }
.initials-row { display: flex; justify-content: center; align-items: center; gap: 12px; }
.initials input {
  width: 5.2em;
  min-height: 48px;
  padding: 4px 0 4px 0.3em;
  font: inherit;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.3em;
  text-align: center;
  text-transform: uppercase;
  color: var(--ink);
  background: #fff;
  border: 2px solid var(--evergreen);
  border-radius: 8px;
}
.initials input:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.initials-note { margin: 8px 0 0; font-size: 0.9rem; color: var(--cranberry); }
.initials-note:empty { display: none; }
.scores { margin: 0 0 18px; }
.scores h3 { margin: 0 0 8px; font-size: 1.05rem; font-style: italic; color: var(--evergreen); }
/* Two columns, read down: 1-5 on the left, 6-10 on the right. */
.score-list {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  max-width: 330px;
  display: grid;
  grid-template-rows: repeat(5, auto);
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  column-gap: 22px;
  font-variant-numeric: tabular-nums;
}
.score-list li { display: flex; gap: 8px; padding: 4px 2px; border-bottom: 1px dotted var(--edge); text-align: left; }
.score-list .place { width: 1.6em; text-align: right; color: #5c5546; }
.score-list .who { font-weight: 700; letter-spacing: 0.08em; }
.score-list .pts { margin-left: auto; }
.score-list li.you { color: var(--cranberry); background: #fbeef0; border-radius: 4px; }
.score-list li.you .place { color: inherit; }
.score-list .none { grid-row: 1 / -1; display: block; border: 0; text-align: center; color: #5c5546; }

/* --- Header banner --- */
.banner {
  display: block;
  flex: none;
  width: var(--board);
  height: var(--head);
  margin-bottom: 10px;
  filter: drop-shadow(0 2px 0 var(--edge));
}

/* --- HUD --- */
.hud {
  width: var(--board);
  display: flex;
  align-items: stretch;
  gap: 8px;
  min-height: 62px;
}
.stat {
  position: relative;
  min-width: 92px;
  padding: 7px 12px 8px;
  background: var(--paper);
  border: 1.5px solid var(--evergreen);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.stat .label { font-size: 0.68rem; letter-spacing: 0.14em; text-transform: uppercase; color: var(--evergreen); white-space: nowrap; }
.stat .value { font-size: 1.6rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
#hud-moves { color: var(--cranberry); }
.star-stat { flex: 1; background: var(--evergreen); border-color: var(--gold); }
.star-stat .label, .star-stat .value { color: #f6d67a; }
.star-stat .bar { position: absolute; left: 0; right: 0; bottom: 0; height: 5px; background: rgba(255, 255, 255, 0.18); }
.star-stat .bar span { display: block; height: 100%; width: 100%; background: #f0c04a; transform-origin: left center; }
.stat.pulse { animation: pulse 0.6s ease-out; }
#hud-moves-box { margin-left: auto; } /* Score sits left; Moves and the sound button sit right */
.sound-btn {
  flex: none;
  width: 48px;
  padding: 0;
  display: grid;
  place-items: center;
  color: var(--evergreen);
  background: var(--paper);
  border: 1.5px solid var(--evergreen);
  border-radius: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sound-btn svg { width: 26px; height: 26px; }
.sound-btn .slash, .sound-btn[aria-pressed="false"] .waves { display: none; }
.sound-btn[aria-pressed="false"] { color: #7a705c; }
.sound-btn[aria-pressed="false"] .slash { display: inline; }
.sound-btn:focus-visible { outline: 3px solid var(--cranberry); outline-offset: 2px; }

.preview {
  margin: 6px 0 4px;
  height: 1.5em;
  line-height: 1.5em;
  font-size: 1rem;
  font-weight: 700;
  color: var(--evergreen);
  text-align: center;
  white-space: nowrap;
}
.preview.short { color: #7a705c; font-weight: 400; font-style: italic; }

/* --- Board --- */
.board-wrap { position: relative; width: var(--board); }
.board {
  position: relative;
  width: var(--board);
  aspect-ratio: var(--cols) / var(--rows);
  background: var(--evergreen);
  border-radius: 12px;
  box-shadow: 0 0 0 3px var(--evergreen), 0 0 0 5px var(--gold), 0 8px 18px rgba(44, 42, 37, 0.18);
  overflow: hidden;
  touch-action: none; /* the board never scrolls or zooms the page */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
body.star-on .board { box-shadow: 0 0 0 3px var(--evergreen), 0 0 0 6px #f0c04a, 0 0 22px 6px rgba(240, 192, 74, 0.55); }

.piece {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% / var(--cols));
  height: calc(100% / var(--rows));
  /* Position comes from the physics in ui.js, frame by frame, so transform has no transition. */
  transition: opacity 0.2s linear;
  will-change: transform;
  pointer-events: none;
}
.piece.out { opacity: 0; }
.piece.out .face { transform: scale(0.2); transition-duration: 0.22s; }
.face {
  position: absolute;
  inset: 5%;
  border-radius: 50%;
  background: var(--paper);
  border: 1.5px solid var(--edge);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.25);
  display: grid;
  place-items: center;
  transition: transform 0.14s ease-out, background-color 0.14s, border-color 0.14s;
}
.face > svg { width: 68%; height: 68%; display: block; }
.spin { position: absolute; inset: 0; will-change: transform; }
.piece.flip .face { animation: flip 0.35s ease-in-out; }
.piece.sel .face { transform: scale(0.86); background: #ffe9a8; border-color: var(--cranberry); border-width: 2.5px; }

.piece.booster .face { background: var(--evergreen-deep); border: 2px solid #f0c04a; box-shadow: 0 0 0 2px rgba(240, 192, 74, 0.3); }
.piece.booster .face { animation: glow 1.6s ease-in-out infinite; }
.piece.born .face { animation: born 0.6s cubic-bezier(0.3, 1.5, 0.5, 1), glow 1.6s ease-in-out 0.6s infinite; }
.piece.pressed .face { transform: scale(0.88); }
.badge {
  position: absolute;
  right: -6%;
  bottom: -6%;
  width: 50%;
  height: 50%;
  border-radius: 50%;
  background: var(--paper);
  border: 1.5px solid var(--cranberry);
  display: grid;
  place-items: center;
}
.badge svg { width: 78%; height: 78%; display: block; }

#chain-line { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#chain-line polyline { stroke: var(--cranberry); stroke-width: 0.17; stroke-linecap: round; stroke-linejoin: round; opacity: 0.8; }

/* --- Feedback --- */
.toasts {
  position: absolute;
  left: 0;
  right: 0;
  top: 18%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  z-index: 5;
}
.toast {
  padding: 8px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1.15rem;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.28);
  animation: toast 1.5s ease-out forwards;
}
.toast.points { background: var(--paper); color: var(--evergreen); border: 2px solid var(--evergreen); font-size: 1.4rem; }
.toast.move { background: var(--cranberry); color: var(--paper); border: 2px solid var(--paper); }
.toast.booster { background: #f0c04a; color: var(--evergreen-deep); border: 2px solid var(--evergreen-deep); display: flex; align-items: center; gap: 8px; }
.toast.booster svg { width: 26px; height: 26px; }
.toast.info { background: var(--evergreen); color: var(--paper); border: 2px solid var(--paper); font-size: 0.98rem; }

.legend {
  width: var(--board);
  margin: 12px 0 0;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.74rem;
  color: var(--evergreen);
}
.legend span { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.legend svg { width: 20px; height: 20px; padding: 2px; border-radius: 50%; background: var(--evergreen-deep); flex: none; }

/* --- Credit line, on every screen --- */
.credit {
  flex: none;
  margin-top: 10px;
  text-align: center;
  font-size: 0.75rem;
  line-height: 1.5;
  color: #5c5546;
}
.credit .by { display: block; }
.credit a { color: var(--evergreen); font-weight: 700; text-underline-offset: 2px; white-space: nowrap; }
.credit a:hover { color: var(--cranberry); }
.credit a:focus-visible { outline: 2px solid var(--cranberry); outline-offset: 2px; border-radius: 2px; }

.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

@keyframes toast {
  0% { opacity: 0; transform: translateY(14px) scale(0.8); }
  14% { opacity: 1; transform: translateY(0) scale(1.06); }
  22% { transform: scale(1); }
  78% { opacity: 1; transform: translateY(-6px); }
  100% { opacity: 0; transform: translateY(-22px); }
}
@keyframes flip {
  0% { transform: scale(1); }
  45% { transform: scale(0.1, 1); }
  100% { transform: scale(1); }
}
@keyframes pulse {
  0% { transform: scale(1); }
  35% { transform: scale(1.14); background: #ffe9a8; }
  100% { transform: scale(1); }
}
@keyframes born {
  0% { transform: scale(0.2) rotate(-25deg); }
  100% { transform: scale(1) rotate(0); }
}
@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 2px rgba(240, 192, 74, 0.3); }
  50% { box-shadow: 0 0 0 2px rgba(240, 192, 74, 0.3), 0 0 9px 3px rgba(240, 192, 74, 0.75); }
}

/* Reduced motion: no falling, bouncing, pulsing, or drifting. Feedback stays, as plain fades. */
@media (prefers-reduced-motion: reduce) {
  .piece, .face { transition: none; }
  .piece.booster .face, .piece.born .face, .piece.flip .face, .stat.pulse { animation: none; }
  .toast { animation: toast-still 1.5s linear forwards; }
  .btn:active { transform: none; }
}
@keyframes toast-still {
  0%, 85% { opacity: 1; }
  100% { opacity: 0; }
}
