/* 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: #231a52;
  --navy-2: #352a78;
  --ink: #2f2b55;          /* the navy ink of the cards */
  --purple: #8a4ec6;       /* Crown cards */
  --lemon: #fdf1b8;        /* the yellow of the card backs */
  --orange: #ee8a3c;       /* Bear cards */
  --kerb: #ee8a3c;         /* the orange stripe under the card back title */
  --pink: #e0383e;          /* red */
  --pink-deep: #c42a30;
  --blue: #2f8fe0;
  --yellow: #f6de36;
  --cream: #fff8e6;
  --font: 'Fredoka', ui-rounded, 'Trebuchet MS', system-ui, sans-serif;
  --vh: 100vh;
  --back-art: url('card-back.svg');   /* the real deck's back: rising white lines, red M roundels and stations on royal blue */
  --m-roundel: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><circle cx='10' cy='10' r='10' fill='%232f8fe0'/><path d='M5.8 13.4V8M5.8 9Q5.8 6.6 7.9 6.6T10 9V13.4M10 9Q10 6.6 12.1 6.6T14.2 9V13.4' fill='none' stroke='white' stroke-width='2.1' stroke-linecap='round'/></svg>");
  --tail: 6px;             /* empty space under the card (the same as above it, so the card is centred) */
  --pile-step: 12px;       /* each discard sits this much lower than the previous one */
    --small-w: min(13vw, 190px);
  --splash-ms: 1600;
  --bg-opacity: .7;        /* 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') center 62% / cover no-repeat;   /* Berlin at dusk: the TV tower, the Brandenburg Gate, the Reichstag and the skyline */
}
.bg::after { content: ''; position: absolute; inset: 0; background: radial-gradient(ellipse at 50% 45%, rgba(35,26,82,.1) 25%, rgba(22,15,58,.7) 100%); }

.stage, .footer-hint { transition: opacity .5s ease; }
body.loading .stage, body.loading .footer-hint { 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 { 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; position: relative; z-index: 10; }   /* above the cards, which can overflow upward on short windows and would cover the theme button */
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; align-items: center; }
.badge {
  padding: 4px 18px; border-radius: 999px; font-weight: 600; font-size: 1rem; letter-spacing: .06em; text-transform: uppercase;
  background: rgba(35,26,82,.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 { position: relative; 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: 6px; padding-bottom: var(--tail); }
.row { position: relative; display: flex; align-items: center; justify-content: center; }

/* Cards: portrait, with the symbol rings from the real cards */
.stack { position: relative; }
.card {
  --reserve: 330px;   /* vertical space taken by everything that is not the card */
  --cap: 70vw;
  --wmin: 200px;
  --wmax: 560px;
  --w: clamp(var(--wmin), min(var(--cap), var(--wmax), calc((var(--vh) - var(--reserve)) * 1.4)), var(--wmax));   /* Paris cards are landscape: height = width x 5/7 */
  width: var(--w); aspect-ratio: 7 / 5;
  border-radius: 18px; border: 4px solid #fff; position: relative; cursor: pointer; overflow: hidden;
  box-shadow: 0 2px 0 #d9dce8, 0 18px 40px rgba(0,0,0,.55);
  background: var(--navy-2); touch-action: manipulation;
}
/* a soft sheen and a fine inner edge on every card face, like printed card stock */
.card::after { content: ''; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; z-index: 4;
  background: linear-gradient(155deg, rgba(255,255,255,.16), transparent 38%); box-shadow: inset 0 0 0 1px rgba(27,33,82,.18); }
.card > svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.card.dealt { animation: deal .55s cubic-bezier(.2,.8,.2,1); will-change: transform; }   /* stays on its own layer after the flip, so the art does not snap sideways by a subpixel when the animation ends */
@keyframes deal {
  from { transform: translateY(-60px) rotateY(100deg) scale(.9); }
  to   { transform: var(--rest, none); }   /* ends where the card really rests, so it does not jump when the animation finishes */
}
#card { z-index: 1; }
.card.small { --w: var(--small-w); cursor: default; pointer-events: none; border-width: 3px; border-radius: 12px; }
.pile { --w: var(--small-w); position: absolute; top: 50%; left: calc(100% + 70px); width: var(--w); height: 0; }   /* out of the flow, so the drawn card alone is centred */
.pile .card { position: absolute; left: 0; top: calc(var(--w) * -0.357 - 20px); transform: translate(var(--dx), calc(var(--n) * var(--pile-step) + var(--dy, 0px))) rotate(var(--rot));  box-shadow: 0 8px 18px rgba(0,0,0,.45); pointer-events: auto; cursor: pointer; }   /* click the discard pile to see the past cards */
/* The draw deck, on the left: a tidy stack of card backs */
.deck { --w: var(--small-w); position: absolute; top: 50%; right: calc(100% + 70px); width: var(--w); height: 0; cursor: pointer; }
.deck:focus-visible { outline: 3px solid var(--yellow); outline-offset: 8px; border-radius: 12px; }
.deck .card { position: absolute; left: 0; top: calc(var(--w) * -0.357 - 20px); transform: translate(calc(var(--n) * -3px), calc(var(--n) * -3px)); box-shadow: 0 4px 10px rgba(0,0,0,.45); pointer-events: auto; cursor: pointer; }
@media (max-width: 1150px) {
  .pile, .deck { display: none; }
}
.card.back {
  background: var(--back-art) center / 100% 100% no-repeat, var(--purple); color: var(--ink); text-align: center;
}   /* the card back: purple, orange, navy and yellow stripes with the blue m roundel, like the real deck */
.card.back::before { content: ''; position: absolute; inset: 0; pointer-events: none; box-shadow: inset 0 0 calc(var(--w) / 9) rgba(10,20,80,.45); }
.back-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 2;
  background: linear-gradient(180deg, var(--lemon), #e2e97c); border-top: 3px solid var(--ink);
  box-shadow: 0 -7px 0 var(--kerb), 0 -10px 0 var(--ink);   /* the red and navy kerb of the Street cards */
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; padding: 10px 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(--ink); border-radius: 8px; padding: 0 10px; font-family: inherit; font-weight: 700; box-shadow: 0 3px 0 var(--ink); }
.back-title, .back-sub { color: var(--ink); }
.card[hidden] { display: none; }


/* Progress: two tracks, purple (Crown) and orange (Bear), one station per card; whichever fills first ends the round */
.line { width: min(92vw, 620px); text-align: center; margin-top: 4px; }
.stops { display: flex; flex-direction: column; gap: 8px; }
.trk {
  position: relative; display: flex; justify-content: space-between; align-items: center; padding: 6px 26px;
  border: 4px solid #fff; border-radius: 999px; box-shadow: inset 0 0 0 2px rgba(35,26,82,.3), 0 6px 18px rgba(0,0,0,.4);
}
.trk.crown { background: linear-gradient(180deg, var(--purple) 0 22%, var(--ink) 22% 30%, var(--purple) 30% 70%, var(--ink) 70% 78%, var(--purple) 78%); }
.trk.bear { background: linear-gradient(180deg, var(--orange) 0 22%, var(--ink) 22% 30%, var(--orange) 30% 70%, var(--ink) 70% 78%, var(--orange) 78%); }
.trk::before { content: ''; position: absolute; left: 40px; right: 40px; top: 50%; height: 8px; margin-top: -4px; background: var(--ink); border-radius: 4px; box-shadow: 0 0 0 2px #fff; }
.stop {
  position: relative; width: 30px; height: 30px; border-radius: 50%; border: 5px solid var(--ink);
  background: #fff; transition: background-color .4s, transform .4s, border-color .4s;
}
.stop.on { background: var(--m-roundel) center / 100% no-repeat, #fff; border-width: 2px; border-color: #fff; box-shadow: 0 0 0 2px var(--ink), 0 3px 6px rgba(0,0,0,.35); transform: scale(1.14); }
.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; }

/* Splash */
.splash { position: fixed; inset: 0; display: grid; place-items: center; background: radial-gradient(ellipse at center, rgba(138,78,198,.55), rgba(35,26,82,.82) 70%); 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: #8fd3ff; 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: radial-gradient(ellipse at 50% 30%, rgba(36,44,120,.97), rgba(22,15,58,.98) 75%); 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; text-shadow: 0 3px 0 var(--navy), 0 6px 16px rgba(0,0,0,.45); }
.overlay h2::after { content: ''; display: block; width: 120px; height: 8px; margin: 10px auto 0; border-radius: 4px; background: linear-gradient(90deg, #111 0 33.4%, #dd0000 33.4% 66.7%, #ffce00 66.7%); box-shadow: 0 0 0 2px var(--ink); }
.past-grid .card { --w: min(40vw, 210px); 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(35,26,82,.85); padding: 5px 16px; border-radius: 999px; border: 1px solid rgba(255,255,255,.45); box-shadow: 0 4px 12px rgba(0,0,0,.3);
}
.footer-hint strong { color: var(--yellow); font-weight: 600; }

/* Confirm dialog */
.dialog {
  width: min(92vw, 400px); background: var(--lemon); color: var(--ink); 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; } }
/* the header of the dialog is a tiny Street card: teal skyline over the red and navy kerb */
.dialog-band { height: 76px; background: url('skyline.svg') center bottom / auto 72px no-repeat, var(--orange); border-bottom: 7px solid var(--kerb); box-shadow: 0 3px 0 var(--ink); box-sizing: content-box; }
.dialog h2 { font-size: 1.6rem; text-transform: uppercase; letter-spacing: .06em; margin: 20px 16px 10px; color: var(--ink); text-shadow: none; }
.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, filter .15s; touch-action: manipulation;
  background-image: linear-gradient(180deg, rgba(255,255,255,.22), rgba(255,255,255,0) 55%);
}
@media (hover: hover) { .btn:not(:disabled):hover { filter: brightness(1.06); transform: translateY(-1px); } }
.btn:disabled { background-color: #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-color: #fff; color: var(--navy); }
.btn.yes { background-color: 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: flex; 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) {
  .badges { margin-top: 8px; }
  .stage { padding-bottom: calc(14px + env(safe-area-inset-bottom)); padding-left: calc(4px + env(safe-area-inset-left)); padding-right: calc(4px + env(safe-area-inset-right)); }
  .card { --reserve: 270px; --cap: calc(100vw - 8px); --wmax: 520px; }
}
/* 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; }
  .deal { gap: 8px; }
  .card { --reserve: 250px; }
}
/* Short screens, mostly landscape phones: card on the left, status and buttons on the right */
@media (max-height: 520px) {
  h1, .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 { gap: 4px; }
  .trk { padding: 3px 16px; border-width: 3px; }
  .stop { width: 20px; height: 20px; border-width: 3px; }
  .deal { gap: 8px; }
  .card { --reserve: 90px; --cap: 62vw; --wmin: 150px; }
}
/* 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; }


/* The table: click the pile and the cards still in the deck are laid out over the play area (desktop only) */
.table {
  position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column; gap: 14px; padding: 18px 22px; cursor: pointer;
  background: linear-gradient(rgba(35,26,82,.9), rgba(35,26,82,.9)), var(--back-art) 0 0 / 220px auto; border: 3px solid #fff; border-radius: 24px;
  box-shadow: inset 0 0 0 3px rgba(35,26,82,.9), inset 0 0 0 5px rgba(255,255,255,.35), 0 18px 50px rgba(0,0,0,.55);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); transform-origin: 30% 50%;
}
.table[hidden] { display: none; }
.table.open { animation: table-in .35s cubic-bezier(.2,.8,.2,1); }
.table.closing { animation: table-out .22s ease-in forwards; }
@keyframes table-in { from { opacity: 0; transform: scale(.9); } }
@keyframes table-out { to { opacity: 0; transform: scale(.94); } }
.table-head { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; color: var(--yellow); font-weight: 700; font-size: 1.3rem; letter-spacing: .1em; text-transform: uppercase; }
.table-hint { font-size: .8rem; font-weight: 500; letter-spacing: .04em; text-transform: none; color: var(--cream); opacity: .7; }
.table-grid { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; align-content: flex-start; padding: 6px; }
.table-grid .card { --w: clamp(150px, 14vw, 220px); cursor: pointer; pointer-events: none; box-shadow: 0 6px 14px rgba(0,0,0,.45);   /* a bit smaller than the drawn card, but far from tiny */
  animation: table-deal .5s cubic-bezier(.2,.8,.2,1) backwards; animation-delay: calc(var(--i) * 18ms + 120ms); }
/* each card slides in from the deck (or the discard pile) and settles */
.table.from-right { --from: 45vw; }
@keyframes table-deal { from { opacity: 0; transform: translate(var(--from, -45vw), 8vh) rotate(-14deg) scale(.6); } }
@media (max-width: 1150px) { .table { display: none !important; } }

/* 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 { animation: failsafe .01s 3s forwards !important; }   /* keep the failsafe */
  .stack.is-final::before { opacity: .55; }
  .stop.final::before { opacity: 1; }
}

.badges { align-items: center; }

/* Phones held upright: tall cards, like the London helper */
@media (max-width: 760px) and (orientation: portrait) {
  .card { --reserve: 300px; --cap: calc(100vw - 40px); --wmin: 150px; --wmax: 380px; aspect-ratio: 5 / 7; --w: clamp(var(--wmin), min(var(--cap), var(--wmax), calc((var(--vh) - var(--reserve)) / 1.4)), var(--wmax)); }
}
