/* Fredoka (SIL Open Font License), self-hosted so the page works offline and loads nothing from third parties */
@font-face {
  font-family: 'Fredoka'; font-style: normal; font-weight: 400 700; font-display: swap;
  src: url('fredoka-latin.woff2') format('woff2');
}
:root {
  --navy: #1b1750;
  --navy-2: #2b2470;
  --pink: #e0457b;
  --pink-deep: #b82a5f;
  --yellow: #ffe94a;
  --cream: #fff8e6;
  --font: 'Fredoka', ui-rounded, 'Trebuchet MS', system-ui, sans-serif;
  --vh: 100vh;
  --peek: 70px;            /* how far the held Railroad Switch sticks out above the card */
  --tail: 68px;            /* empty space under the card, keeps it centred between the badges and the counter */
  --pile-step: 12px;       /* each discard sits this much lower than the previous one */
  --small-w: min(14vw, 150px);
  --splash-ms: 1600;
  --bg-opacity: .5;        /* how strongly the background shows; the rest fades into the navy page colour */
}
@supports (height: 100dvh) { :root { --vh: 100dvh; } }
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { height: 100%; }
body { height: var(--vh); }
body {
  font-family: var(--font); color: var(--cream); background: var(--navy);
  overflow: hidden; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
.bg {
  position: fixed; inset: 0; z-index: -2; opacity: var(--bg-opacity);
  background: url('bg.svg?v=42') center / cover no-repeat;
}
.bg::after { content: ''; position: absolute; inset: 0; background: radial-gradient(ellipse at center, transparent 40%, rgba(15,12,60,.55) 100%); }

.stage, .footer-hint, .disclaimer { transition: opacity .5s ease; }
body.loading .stage, body.loading .footer-hint, body.loading .disclaimer { opacity: 0; }
/* Failsafe: if the script never removes .loading (it failed or is blocked), reveal the page anyway after 3 seconds */
body.loading .stage, body.loading .footer-hint, body.loading .disclaimer { animation: failsafe .3s 3s forwards; }
@keyframes failsafe { to { opacity: 1; } }
.fatal { position: fixed; inset: 0; z-index: 100; align-items: center; justify-content: center; padding: 24px; text-align: center; background: var(--navy); }
.fatal:not([hidden]) { display: flex; }
.fatal p { max-width: 420px; font-size: 1.25rem; font-weight: 500; line-height: 1.4; color: var(--cream); }

.stage {
  height: 100%; display: flex; flex-direction: column; align-items: center; gap: 6px; overflow-y: auto; overflow-x: hidden;
  padding: calc(18px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(54px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

header { text-align: center; }
h1 {
  font-size: clamp(1.6rem, 4.5vw, 3rem); font-weight: 700; text-transform: uppercase; letter-spacing: .12em;
  text-shadow: 0 3px 0 var(--navy), 0 6px 18px rgba(0,0,0,.5);
}
h1 span { color: var(--yellow); }
.badges { display: flex; gap: 10px; justify-content: center; margin-top: 8px; }
.badge {
  padding: 4px 18px; border-radius: 999px; font-weight: 600; font-size: 1rem; letter-spacing: .06em; text-transform: uppercase;
  background: rgba(27,23,80,.85); border: 2px solid var(--yellow); color: var(--yellow);
  box-shadow: 0 4px 10px rgba(0,0,0,.35); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}

main { flex: 1; display: flex; align-items: center; justify-content: center; width: 100%; min-height: 0; }
.deal { display: flex; flex-direction: column; align-items: center; gap: 14px; perspective: 1400px; padding-top: calc(var(--peek) + 6px); padding-bottom: var(--tail); }
.row { display: flex; align-items: center; justify-content: center; gap: 28px; }

/* Cards: portrait, with the symbol rings from the real cards */
.stack { position: relative; }
.card {
  --reserve: 420px;   /* vertical space taken by everything that is not the card */
  --cap: 70vw;
  --wmin: 140px;
  --w: clamp(var(--wmin), min(var(--cap), 340px, calc((var(--vh) - var(--reserve)) / 1.4)), 340px);
  width: var(--w); aspect-ratio: 5 / 7;
  border-radius: 18px; border: 4px solid #fff; position: relative; cursor: pointer; overflow: hidden;
  box-shadow: 0 18px 40px rgba(0,0,0,.55);
  background: var(--navy-2); touch-action: manipulation;
}
.card > svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.card.dealt { animation: deal .55s cubic-bezier(.2,.8,.2,1); }
@keyframes deal {
  from { transform: translateY(-60px) rotateY(100deg) scale(.9); }
  to   { transform: none; }
}
#card { z-index: 1; }
/* Held Railroad Switch: tucked behind the current card, peeking out above it */
.card.held { position: absolute; left: 0; top: calc(var(--peek) * -1); z-index: 0; cursor: default; pointer-events: none; transform: rotate(3deg); transform-origin: bottom left; border-color: var(--yellow); box-shadow: 0 0 0 3px var(--navy), 0 0 30px rgba(255,233,74,.8), 0 14px 30px rgba(0,0,0,.5); }
.card.held.peek { animation: peek .5s ease-out; }
@keyframes peek { from { transform: translate(0, var(--peek)) rotate(0); } }
.held-cap {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); white-space: nowrap; display: flex; align-items: center; gap: 6px;
  background: var(--yellow); color: var(--navy); border: 3px solid var(--navy); border-radius: 999px;
  padding: 3px 14px 3px 5px; font-weight: 700; font-size: clamp(.62rem, calc(var(--w) / 15), 1.1rem); letter-spacing: .04em; text-transform: uppercase;
  box-shadow: 0 3px 8px rgba(0,0,0,.35);
}
.held-cap svg { width: clamp(20px, calc(var(--w) / 8), 34px); height: clamp(20px, calc(var(--w) / 8), 34px); border-radius: 6px; display: block; flex: none; }
.card.small { --w: var(--small-w); cursor: default; pointer-events: none; border-width: 3px; border-radius: 12px; }
.pile { --w: var(--small-w); position: relative; width: var(--w); height: 0; flex: none; align-self: center; }
.pile .card { position: absolute; left: 0; top: calc(var(--w) * -0.7 - 40px); transform: translate(var(--dx), calc(var(--n) * var(--pile-step))) rotate(var(--rot));  box-shadow: 0 8px 18px rgba(0,0,0,.45); }
@media (max-width: 900px) { .pile { display: none; } }
.card.back { background: var(--yellow); display: flex; flex-direction: column; color: var(--navy); text-align: center; }
.back-top {
  flex: 0 0 52%; background: var(--pink); display: grid; place-items: center;
  border-top: 7px solid var(--navy); border-bottom: 7px solid var(--navy);
}
.back-top svg { width: 56%; height: auto; filter: drop-shadow(0 5px 0 rgba(27,23,80,.3)); }
.back-bottom { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 0 10px; }
.back-title { font-size: clamp(1.2rem, 2.2vw, 1.7rem); font-weight: 700; text-transform: uppercase; letter-spacing: .06em; line-height: 1.1; }
.back-sub { font-size: 1rem; font-weight: 500; }
.back-sub kbd { background: #fff; border: 3px solid var(--navy); border-radius: 8px; padding: 0 10px; font-family: inherit; font-weight: 700; box-shadow: 0 3px 0 var(--navy); }
.card[hidden] { display: none; }


/* Underground line progress, like the bar in Harmonies */
.line { width: min(92vw, 620px); text-align: center; margin-top: 4px; }
.stops {
  position: relative; display: flex; justify-content: space-between; align-items: center; padding: 8px 26px;
  background: rgba(27,23,80,.82); border: 4px solid var(--cream); border-radius: 999px; box-shadow: 0 6px 18px rgba(0,0,0,.4);
}
.stops::before { content: ''; position: absolute; left: 40px; right: 40px; top: 50%; height: 8px; margin-top: -4px; background: var(--pink); border-radius: 4px; opacity: .45; }
.stop {
  position: relative; width: 34px; height: 34px; border-radius: 50%; border: 5px solid var(--pink);
  background: var(--navy); transition: background .4s, transform .4s;
}
.stop.on { background: var(--pink); transform: scale(1.12); }
.stop.on::after { content: ''; position: absolute; left: -9px; right: -9px; top: 50%; height: 7px; margin-top: -3.5px; background: var(--cream); border-radius: 2px; }
.stop.final::before { content: ''; position: absolute; inset: -11px; border-radius: 50%; border: 6px solid rgba(255,233,74,.95); animation: pulse-layer .9s ease-in-out infinite; }
.line-label { display: inline-block; margin-top: 4px; padding: 2px 12px; border-radius: 999px; background: transparent; font-size: .75rem; letter-spacing: .2em; text-transform: uppercase; opacity: .85; text-shadow: 0 2px 4px rgba(0,0,0,.6); }

/* Splash */
.splash { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(27,23,80,.72); z-index: 50; animation: fade calc(var(--splash-ms) * 1ms) ease forwards; pointer-events: none; }
.splash[hidden] { display: none; }
#splash-text { font-size: clamp(2.4rem, 9vw, 6rem); font-weight: 700; text-transform: uppercase; letter-spacing: .12em; color: var(--yellow); text-align: center; text-shadow: 0 5px 0 var(--navy), 0 10px 30px rgba(0,0,0,.6); }
#splash-text small { display: block; font-size: .3em; letter-spacing: .2em; color: var(--cream); margin-top: 8px; }
@keyframes fade { 0% { opacity: 0; } 15% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; } }

/* Past cards overlay */
.overlay { position: fixed; inset: 0; background: rgba(15,12,60,.97); z-index: 60; display: flex; align-items: center; overflow-y: auto;
  padding: calc(20px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(20px + env(safe-area-inset-left)); }
.overlay > * { margin: auto; }   /* centres, and still lets tall content scroll */
.overlay[hidden] { display: none; }
.overlay-box { text-align: center; max-width: 900px; }
.overlay h2 { font-size: 1.8rem; letter-spacing: .12em; text-transform: uppercase; color: var(--yellow); margin-bottom: 18px; }
.past-grid .card { --w: min(28vw, 150px); cursor: default; }
.past-grid { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; }
.hint { margin-top: 18px; opacity: .7; }
#past-close { margin-top: 22px; }
.overlay .sub { margin: -8px 0 16px; opacity: .8; }

.footer-hint {
  position: fixed; bottom: calc(14px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); white-space: nowrap; font-size: .85rem; letter-spacing: .04em;
  background: rgba(27,23,80,.8); padding: 5px 16px; border-radius: 999px; border: 1px solid rgba(255,233,74,.5);
}
.footer-hint strong { color: var(--yellow); font-weight: 600; }
.disclaimer { position: fixed; bottom: calc(8px + env(safe-area-inset-bottom)); left: calc(10px + env(safe-area-inset-left)); font-size: .72rem; max-width: 260px; line-height: 1.25; background: rgba(27,23,80,.78); padding: 4px 10px; border-radius: 10px; }

/* Confirm dialog */
.dialog {
  width: min(92vw, 400px); background: var(--yellow); color: var(--navy); border: 5px solid #fff; border-radius: 20px;
  overflow: hidden; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.6); animation: pop .25s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes pop { from { transform: scale(.85); } to { transform: none; } }
.dialog-band { height: 22px; background: var(--pink); border-top: 6px solid var(--navy); border-bottom: 6px solid var(--navy); box-sizing: content-box; }
.dialog h2 { font-size: 1.6rem; text-transform: uppercase; letter-spacing: .06em; margin: 20px 16px 6px; color: var(--navy); }
.dialog p { font-size: 1.05rem; margin: 0 20px 20px; font-weight: 500; }
.dialog-buttons { display: flex; gap: 12px; justify-content: center; padding: 0 20px 22px; }
.btn {
  font-family: inherit; font-size: 1.05rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; cursor: pointer;
  padding: 10px 22px; border-radius: 999px; border: 3px solid var(--navy); box-shadow: 0 4px 0 var(--navy); transition: transform .1s, box-shadow .1s; touch-action: manipulation;
}
.btn:disabled { background: #8d89b5; color: #e4e2f3; border-color: #3d3880; box-shadow: 0 4px 0 #3d3880; cursor: not-allowed; }
.btn:disabled:active { transform: none; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--navy); }
.btn.no { background: #fff; color: var(--navy); }
.btn.yes { background: var(--pink-deep); color: #fff; }   /* white on --pink was only ~4:1, this is ~6:1 */
.btn:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }
.dialog .btn:focus-visible { outline-color: var(--navy); }
#card:focus-visible { outline: 4px solid var(--yellow); outline-offset: 6px; }

/* Touch buttons (phones have no keyboard) */
.tools { display: none; gap: 8px 10px; justify-content: center; flex-wrap: wrap; margin-top: 10px; }
.tools .btn { font-size: .8rem; padding: 8px 12px; min-height: 44px; box-shadow: 0 4px 0 var(--navy), 0 8px 14px rgba(0,0,0,.35); }

/* Phones, and any touch device (including landscape): show buttons, hide the keyboard hint */
@media (max-width: 760px), (hover: none) and (pointer: coarse) {
  .tools { display: flex; }
  .footer-hint { display: none; }
}
@media (max-width: 760px) {
  .disclaimer { display: none; }
  .stage { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .card { --reserve: 400px; --cap: 80vw; }
}
/* Very narrow phones: keep the three buttons on one row */
@media (max-width: 360px) {
  .tools { gap: 6px; }
  .tools .btn { font-size: .7rem; padding: 8px 9px; letter-spacing: .02em; }
}
/* Small portrait phones (iPhone SE and similar) */
@media (max-width: 760px) and (max-height: 640px) {
  h1 { font-size: 1.3rem; }
  .badges { margin-top: 4px; }
  :root { --peek: 52px; --tail: 52px; }
  .deal { gap: 8px; }
  .card { --reserve: 360px; }
}
/* Short screens, mostly landscape phones: card on the left, status and buttons on the right */
@media (max-height: 520px) {
  h1, .line-label, .disclaimer, .footer-hint { display: none; }
  .stage {
    display: grid; grid-template-columns: 1fr minmax(230px, 42%); grid-template-rows: auto 1fr auto auto;
    align-items: center; justify-items: center; column-gap: 16px; row-gap: 6px;
    padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
  }
  header { grid-column: 2; grid-row: 1; }
  .badges { margin-top: 0; }
  main { grid-column: 1; grid-row: 1 / 5; height: 100%; }
  .line { grid-column: 2; grid-row: 3; width: 100%; margin-top: 0; }
  .tools { display: flex; grid-column: 2; grid-row: 4; margin-top: 4px; }   /* keyboard hint is hidden here, so mouse users get the buttons too */
  .stops { padding: 5px 18px; border-width: 3px; }
  .stop { width: 26px; height: 26px; border-width: 4px; }
  :root { --peek: 34px; --tail: 48px; }
  .deal { gap: 8px; }
  .card { --reserve: 120px; --cap: 60vw; --wmin: 110px; }
}
/* Last card of the round: unmissable */
.card.final { box-shadow: 0 0 0 6px var(--yellow), 0 0 28px 6px rgba(255,233,74,.7), 0 18px 40px rgba(0,0,0,.55); }
/* Pulse: a layer behind the card whose opacity animates (cheap), instead of animating box-shadow */
.stack.is-final::before {
  content: ''; position: absolute; inset: 0; border-radius: 18px; z-index: 0; pointer-events: none;
  box-shadow: 0 0 0 9px var(--yellow), 0 0 60px 20px rgba(255,233,74,.95);
  animation: pulse-layer 1s ease-in-out infinite;
}
@keyframes pulse-layer { 0%, 100% { opacity: 0; } 50% { opacity: 1; } }
.sash {
  position: absolute; top: 24px; left: -50px; width: 200px; text-align: center; transform: rotate(-38deg);
  background: var(--yellow); color: var(--navy); border-top: 3px solid var(--navy); border-bottom: 3px solid var(--navy);
  padding: 3px 0; font-weight: 700; font-size: .95rem; letter-spacing: .14em; text-transform: uppercase;
  box-shadow: 0 4px 10px rgba(0,0,0,.4); z-index: 5;
}

/* Past cards: turn numbers, latest highlighted */
.num {
  position: absolute; top: 6px; left: 6px; z-index: 5; width: 30px; height: 30px; border-radius: 50%;
  background: var(--navy); color: #fff; border: 2px solid #fff; display: grid; place-items: center; font-weight: 700; font-size: .95rem;
}
.past-grid .card.latest { box-shadow: 0 0 0 4px var(--yellow), 0 0 22px 4px rgba(255,233,74,.8); }
/* in the grid the cards are small: shrink the turn badge */
.past-grid .num { width: 24px; height: 24px; top: 4px; left: 4px; font-size: .8rem; border-width: 2px; }

/* Visually hidden, still read by screen readers */
.sr { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }


/* ---------- Themes: one button (or the T key) cycles them; dusk is the default ---------- */
html[data-theme="day"]   .bg { background-image: url('bg-day.svg?v=42'); }
html[data-theme="night"] .bg { background-image: url('bg-night.svg?v=42'); }
html[data-theme="calm"]  .bg { background-image: url('bg-calm.svg?v=42'); }
/* the day backgrounds are light: lighter vignette, and the small captions get a navy pill so they stay readable */
html[data-theme="day"] .bg::after, html[data-theme="calm"] .bg::after { background: radial-gradient(ellipse at center, transparent 60%, rgba(27,23,80,.22) 100%); }
html[data-theme="day"] .line-label, html[data-theme="calm"] .line-label { background: var(--navy); opacity: 1; text-shadow: none; }   /* colours only: no theme may change a size */

.badges { align-items: center; }
.theme-btn {
  width: 36px; height: 36px; padding: 0; border-radius: 50%; display: grid; place-items: center; cursor: pointer; touch-action: manipulation;
  background: rgba(27,23,80,.85); color: var(--yellow); border: 2px solid var(--yellow); box-shadow: 0 4px 10px rgba(0,0,0,.35);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.theme-btn svg { width: 20px; height: 20px; }
.theme-btn:active { transform: scale(.92); }
.theme-btn:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }




/* Respect reduced-motion: no animation, keep static highlights */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  body.loading .stage, body.loading .footer-hint, body.loading .disclaimer { animation: failsafe .01s 3s forwards !important; }   /* keep the failsafe */
  .stack.is-final::before { opacity: .55; }
  .stop.final::before { opacity: 1; }
}
