/* Flag Day — 1970s World's Fair.
 * Light cream ground, harvest-gold / burnt-orange / avocado / teal accents,
 * rainbow stripe bands, soft chunky serif headings, Futura-style body. */

:root {
  --paper: #fbf6ea;
  --surface: #f3ead6;
  --card: #fffdf7;
  --ink: #3a2618;
  --ink-soft: #7a5f48;
  --line: #e8dcc6;
  --edge: #3a2618;        /* outlines on buttons and cards */
  --ledge: #3a2618;       /* hard offset shadow on panels */
  --rust: #b24a2c;
  --orange: #e2632a;
  --gold: #efa834;
  --avocado: #5e7a22;
  --teal: #20716b;
  --cocoa: #8b6a4f;
  --correct: var(--avocado);
  --second: var(--gold);
  --miss: var(--cocoa);
  --on-correct: #fbf5e7;
  --on-second: #3a2618;
  --on-miss: #fbf5e7;
  --flag-ring: rgba(58, 38, 24, 0.18);
  --wrong: #c8372b;
  --on-wrong: #fffaf0;
  --on-accent: #fbf5e7;
  --backdrop: rgba(58, 38, 24, 0.35);
  --danger: #b24a2c;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1d1510;
    --surface: #2c2119;
    --card: #251b14;
    --ink: #f3e6cb;
    --ink-soft: #b89f83;
    --line: #47372a;
    --edge: #a88d70;
    --ledge: #0c0806;
    --rust: #d0613f;
    --orange: #f07a3e;
    --gold: #f2b347;
    --avocado: #8ba83b;
    --teal: #3a9a92;
    --cocoa: #6e5440;
    --on-correct: #1d1510;
    --on-second: #1d1510;
    --on-miss: #f3e6cb;
    --flag-ring: rgba(243, 230, 203, 0.18);
    --wrong: #e5604f;
    --on-wrong: #1d1510;
    --on-accent: #1d1510;
    --backdrop: rgba(0, 0, 0, 0.5);
    --danger: #e07a5a;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --paper: #1d1510;
  --surface: #2c2119;
  --card: #251b14;
  --ink: #f3e6cb;
  --ink-soft: #b89f83;
  --line: #47372a;
  --edge: #a88d70;
  --ledge: #0c0806;
  --rust: #d0613f;
  --orange: #f07a3e;
  --gold: #f2b347;
  --avocado: #8ba83b;
  --teal: #3a9a92;
  --cocoa: #6e5440;
  --on-correct: #1d1510;
  --on-second: #1d1510;
  --on-miss: #f3e6cb;
  --flag-ring: rgba(243, 230, 203, 0.18);
  --wrong: #e5604f;
  --on-wrong: #1d1510;
  --on-accent: #1d1510;
  --backdrop: rgba(0, 0, 0, 0.5);
  --danger: #e07a5a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;   /* no rubber-band bounce: feels like an app */
  touch-action: manipulation;  /* no double-tap zoom on fast taps */
  background: var(--paper);
  color: var(--ink);
  font-family: 'Jost', 'Futura', 'Century Gothic', 'Avenir Next', sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.display {
  font-family: 'Fraunces', 'Cooper Black', 'Georgia', serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 800;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

#app {
  max-width: 500px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Rainbow stripe band: rust / orange / gold / avocado with paper gaps. */
.stripes {
  height: 22px;
  flex: none;
  background: linear-gradient(to bottom,
    var(--rust) 0 4px, transparent 4px 6px,
    var(--orange) 6px 10px, transparent 10px 12px,
    var(--gold) 12px 16px, transparent 16px 18px,
    var(--avocado) 18px 22px);
}

/* ---------- Header ---------- */

.play, .top { -webkit-user-select: none; user-select: none; }
img { -webkit-touch-callout: none; }

.top {
  margin-top: env(safe-area-inset-top);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: 60px;
  padding: 0 10px;
  flex: none;
}
.top-side { display: flex; gap: 4px; }
.top-side.right { justify-content: flex-end; }

.title { margin: 0; }
.title button {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: 30px;
  letter-spacing: -0.01em;
  line-height: 1;
}

.icon-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  transition: background-color 0.15s;
}
@media (hover: hover) { .icon-btn:hover { background: var(--surface); } }
.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn svg .fill { fill: currentColor; stroke: none; }
.icon-btn:focus-visible, .key:focus-visible, .btn:focus-visible, .flag-choice:focus-visible, .tab:focus-visible, .atlas-item:focus-visible {
  outline: 3px solid var(--orange);
  outline-offset: 2px;
}

#screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0 16px calc(14px + env(safe-area-inset-bottom));
}

/* ---------- Home ---------- */

.home {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 18px;
  padding-bottom: 28px;
}
.emblem { width: 220px; height: auto; overflow: visible; }
.emblem .orbit { transform-origin: 110px 108px; animation: spin 14s linear infinite; }
.emblem .flaglet { transform-origin: 110px 64px; animation: wave 2.8s ease-in-out infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes wave {
  0%, 100% { transform: skewY(0deg); }
  50% { transform: skewY(-6deg) scaleX(0.94); }
}
.home .kicker {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-wrap: balance;
  text-transform: uppercase;
  color: var(--orange);
  margin: 0 0 -10px;
}
.home h2 {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: 52px;
  margin: 0;
  line-height: 0.95;
  letter-spacing: -0.02em;
}
.home .lede {
  font-size: 19px;
  line-height: 1.35;
  margin: 0;
  max-width: 300px;
}
.home .known {
  width: 100%;
  max-width: 310px;
  font-size: 15px;
  color: var(--ink-soft);
}
.home .known strong { color: var(--ink); }
.home .actions { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 260px; }
.home .meta { font-size: 13px; color: var(--ink-soft); margin: 0; letter-spacing: 0.04em; }

.btn {
  height: 50px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 17px;
  letter-spacing: 0.04em;
  padding: 0 26px;
  border: 2px solid var(--edge);
  background: var(--card);
  transition: transform 0.08s;
}
.btn:active { transform: scale(0.97); }
.btn.go { background: var(--orange); color: var(--on-accent); }
.btn.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.danger { color: var(--danger); border-color: var(--danger); }

/* Mastery meter */
.meter {
  height: 16px;
  border-radius: 999px;
  border: 2px solid var(--edge);
  background: var(--card);
  overflow: hidden;
  display: flex;
  margin-top: 8px;
}
.meter span {
  display: block;
  height: 100%;
  transition: width 0.9s cubic-bezier(.2, .8, .2, 1);
}
.meter .m-known { background: var(--avocado); }
.meter .m-familiar { background: var(--gold); }

/* ---------- Play ---------- */

.play {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tiles {
  display: flex;
  justify-content: center;
  gap: 7px;
  padding: 14px 0 6px;
  flex-wrap: wrap;
}
.tile {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--line);
  transition: border-color 0.15s;
}
.tiles.many .tile { width: 11px; height: 11px; }
.tile.current { border-color: var(--orange); box-shadow: 0 0 0 3px color-mix(in srgb, var(--orange) 25%, transparent); }
.tile.correct, .tile.recovered, .tile.missed { border-color: transparent; }
.tile.correct { background: var(--correct); }
.tile.recovered { background: var(--second); }
.tile.missed { background: var(--miss); }
.tile.flip { animation: boing 0.45s cubic-bezier(.3, 1.6, .5, 1); }

@keyframes boing {
  0% { transform: scale(0.3); }
  100% { transform: scale(1); }
}

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  min-height: 0;
  text-align: center;
}
/* Centre vertically with auto margins rather than justify-content: if space runs
   short they collapse to zero, so content never spills up over the progress dots. */
.stage > :first-child { margin-top: auto; }
.stage > :last-child { margin-bottom: auto; }
.prompt {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.24em;
  color: var(--orange);
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.chip {
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--gold);
  color: var(--on-second);
  letter-spacing: 0.12em;
}

/* Just the flag. It takes up to 230px of height, and shrinks to whatever
   space is left on short screens (e.g. iPhone Safari with its toolbar). */
.flag-wrap {
  flex: 0 1 min(36dvh, 230px);
  min-height: 72px;
  width: 100%;
  position: relative;
}
/* Absolutely positioned so the image's max-height has a definite box to resolve against. */
.placard {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.placard img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
}
.flag-missing {
  width: 240px;
  max-width: 100%;
  aspect-ratio: 3 / 2;
  display: grid;
  place-items: center;
  color: var(--ink-soft);
  font-size: 14px;
  padding: 16px;
}

.country-name {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: clamp(30px, 10vw, 44px);
  line-height: 1.05;
  margin: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.reveal {
  min-height: 24px;
  font-weight: 600;
  font-size: 18px;
  margin: 0;
}

/* Answer keys */
.keys {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 8px;
}
.key {
  min-height: 54px;
  border-radius: 999px;
  background: var(--card);
  border: 2px solid var(--edge);
  color: var(--ink);
  font-weight: 500;
  font-size: 18px;
  padding: 8px 20px;
  line-height: 1.2;
  transition: background-color 0.12s, transform 0.08s;
  user-select: none;
}
@media (hover: hover) { .key:not([aria-disabled="true"]):hover { background: var(--surface); } }
.key:not([aria-disabled="true"]):active { transform: scale(0.97); }
.key[aria-disabled="true"] { cursor: pointer; } /* after a miss, tapping moves on */
.key.is-correct { background: var(--correct); color: var(--on-correct); font-weight: 600; animation: correct-pop 0.5s both; }
.key.is-correct.has-tab { animation: kick 0.5s both; }
.key.is-wrong { background: var(--wrong); color: var(--on-wrong); font-weight: 600; animation: shake 0.4s; }
.key.is-dim { opacity: 0.35; transform: scale(0.95); transition: opacity 0.3s ease, transform 0.35s cubic-bezier(.3, 1.4, .5, 1); }

@keyframes bounce {
  0% { transform: scale(0.96); }
  100% { transform: scale(1); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}

/* Pick-the-flag grid */
.flag-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px 16px;
  padding-top: 8px;
}
.flag-choice {
  position: relative;
  display: grid;
  grid-template-rows: 1fr auto;
  justify-items: center;
  align-items: center;
  gap: 8px;
  padding: 0;
  transition: transform 0.08s, opacity 0.2s;
}
.flag-choice .img-box {
  aspect-ratio: 4 / 3;
  width: 100%;
  display: grid;
  place-items: center;
}
.flag-choice:not([aria-disabled="true"]):active { transform: scale(0.96); }
.flag-choice img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
}
.flag-choice .label {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.2;
  min-height: 24px;
  padding: 4px 10px;
  border-radius: 999px;
  color: var(--ink);
  opacity: 0;
  transition: opacity 0.25s;
}
.answered .flag-choice .label { opacity: 1; }
.flag-choice.is-correct { animation: correct-pop 0.5s both; }
.flag-choice.is-correct.has-tab { animation: kick-flag 0.5s both; }
.flag-choice.is-correct .label { background: var(--correct); color: var(--on-correct); }
.flag-choice.is-wrong { animation: shake 0.4s; }
.flag-choice.is-wrong .label { background: var(--wrong); color: var(--on-wrong); }
.flag-choice.is-dim { opacity: 0.35; transform: scale(0.92); transition: opacity 0.3s ease, transform 0.35s cubic-bezier(.3, 1.4, .5, 1); }

.next-row { min-height: 60px; display: flex; padding-top: 12px; }
.next-row .btn { flex: 1; }
.step { margin: auto; font-size: 13px; letter-spacing: 0.16em; color: var(--ink-soft); }

/* ---------- Toasts ---------- */

#toasts {
  position: fixed;
  top: 90px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 32px);
}
.toast {
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  font-size: 15px;
  padding: 10px 18px;
  border-radius: 999px;
  animation: toast-in 0.3s cubic-bezier(.3, 1.6, .5, 1);
  text-align: center;
}
.toast.out { opacity: 0; transition: opacity 0.3s; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px) scale(0.9); }
  to { opacity: 1; transform: none; }
}

/* ---------- Modals ---------- */

.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: var(--backdrop); }
.modal-card {
  position: relative;
  background: var(--paper);
  color: var(--ink);
  width: min(500px, calc(100vw - 24px));
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  border-radius: 24px;
  border: 2px solid var(--edge);
  box-shadow: 8px 8px 0 var(--ledge);
  padding: 0 22px 26px;
  animation: slide-in 0.3s cubic-bezier(.2, 1.2, .4, 1);
}
.modal-card::before {
  content: '';
  display: block;
  margin: 0 -22px 28px;
  height: 22px;
  background: linear-gradient(to bottom,
    var(--rust) 0 4px, transparent 4px 6px,
    var(--orange) 6px 10px, transparent 10px 12px,
    var(--gold) 12px 16px, transparent 16px 18px,
    var(--avocado) 18px 22px);
}
@media (max-width: 500px) {
  .modal { place-items: stretch; }
  .modal-card {
    width: 100vw;
    max-height: none;
    height: 100dvh;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }
}
@keyframes slide-in {
  from { opacity: 0; transform: translateY(30px); }
  to { opacity: 1; transform: none; }
}
.modal-close { position: absolute; top: 30px; right: 10px; }

.modal h2 {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: 32px;
  line-height: 1.05;
  margin: 0 0 6px;
  padding-right: 36px;
}
.modal h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: var(--orange);
  margin: 26px 0 10px;
}
.modal p, .modal li { font-size: 16px; line-height: 1.5; }
.modal ul { padding-left: 20px; margin: 8px 0; }
.modal li::marker { color: var(--orange); }
.modal .sub { font-size: 18px; margin: 0 0 12px; }
.modal hr { border: 0; height: 2px; background: var(--line); margin: 22px 0; }
.small { font-size: 14px !important; color: var(--ink-soft); }
.center { text-align: center; padding-right: 0 !important; }

.legend-row { display: flex; align-items: center; gap: 12px; margin: 10px 0; font-size: 15px; }
.legend-row .tile { width: 24px; height: 24px; flex: none; }

/* Stat numbers */
.numbers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.numbers > div { background: var(--card); border: 2px solid var(--line); border-radius: 16px; padding: 10px 4px; }
.numbers .n {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 800;
  font-size: 30px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.numbers .l { font-size: 12px; line-height: 1.25; color: var(--ink-soft); }

/* Level distribution */
.dist { display: flex; flex-direction: column; gap: 6px; }
.dist-row { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 8px; font-size: 14px; }
.dist-bar {
  min-width: 30px;
  height: 22px;
  background: var(--cocoa);
  color: var(--on-miss);
  font-weight: 600;
  font-size: 13px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 9px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}
.dist-bar.hl { background: var(--avocado); color: var(--on-correct); }
.dist-bar.prov { background: var(--gold); color: var(--on-second); }

/* Progress chart */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart .axis text { fill: var(--ink-soft); font-size: 11px; font-family: inherit; }
.chart .line { fill: none; stroke: var(--orange); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--orange); opacity: 0.14; }
.chart .dot { fill: var(--orange); stroke: var(--paper); stroke-width: 2; }
.chart .cross { stroke: var(--ink-soft); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .tip {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  pointer-events: none;
  white-space: nowrap;
}
.chart-empty { font-size: 14px; color: var(--ink-soft); text-align: center; padding: 20px 0; }

/* Results */
.result-score {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: 76px;
  line-height: 1;
  margin: 6px 0 4px;
  text-align: center;
  color: var(--orange);
}
.result-msg { text-align: center; font-size: 19px; margin: 0 0 8px; }
.results-tiles { margin: 10px 0 4px; }
.results-tiles .tile { width: 22px; height: 22px; }
.results-tiles.many .tile { width: 14px; height: 14px; }
.big-gain {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 16px;
}
.big-gain .n {
  font-family: 'Fraunces', 'Cooper Black', Georgia, serif;
  font-variation-settings: 'SOFT' 100, 'WONK' 1;
  font-weight: 900;
  font-size: 34px;
  line-height: 1;
}
.plus {
  display: inline-block;
  background: var(--avocado);
  color: var(--on-correct);
  font-weight: 600;
  font-size: 13px;
  padding: 2px 9px;
  border-radius: 999px;
  vertical-align: 2px;
}
.mini-flags { display: flex; flex-wrap: wrap; gap: 12px 14px; }
.mini-flag { display: block; padding: 2px; border-radius: 4px; transition: transform 0.08s; }
.mini-flag:active { transform: scale(0.92); }
.mini-flag img { display: block; height: 28px; width: auto; }
.note { font-size: 14px; color: var(--ink-soft); margin: -4px 0 10px; }
.result-actions { display: flex; gap: 12px; margin-top: 28px; }
.result-actions .btn { flex: 1; }
.btn .ico { width: 18px; height: 18px; vertical-align: -3px; margin-left: 6px; fill: none; stroke: currentColor; stroke-width: 2; }

/* Passport (collection) */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 14px 0 18px; }
.tab {
  font-size: 14px;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 999px;
  border: 2px solid var(--edge);
}
.tab[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.atlas { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 16px 10px; }
.atlas-item { position: relative; text-align: center; font-size: 12px; line-height: 1.25; border-radius: 10px; padding: 4px 2px; }
.atlas-item .thumb { height: 52px; display: grid; place-items: center; margin-bottom: 5px; }
.atlas-item img { max-height: 52px; max-width: 100%; width: auto; border-radius: 3px; }
.atlas-item.unseen img { filter: grayscale(1) sepia(0.4); opacity: 0.3; }
.atlas-item.unseen { color: var(--ink-soft); }
.stamp {
  position: absolute;
  top: -6px;
  right: 2px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid var(--rust);
  color: var(--rust);
  display: grid;
  place-items: center;
  font-size: 15px;
  font-weight: 700;
  background: color-mix(in srgb, var(--card) 70%, transparent);
  transform: rotate(-14deg);
}
.stamp.prov { border-style: dashed; border-color: var(--gold); color: var(--gold); }
.pips { display: flex; justify-content: center; gap: 3px; margin-top: 5px; }
.pips i { width: 7px; height: 7px; border-radius: 50%; background: var(--line); }
.pips i.on { background: var(--gold); }
.pips.done i.on { background: var(--avocado); }

/* Settings */
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 16px 0; border-bottom: 2px solid var(--line); }
.setting .t { font-size: 17px; font-weight: 500; }
.setting .d { font-size: 13px; color: var(--ink-soft); margin-top: 2px; }
.seg { display: flex; border: 2px solid var(--edge); border-radius: 999px; overflow: hidden; flex: none; }
.seg button { padding: 7px 12px; font-weight: 500; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--orange); color: var(--on-accent); }

/* Celebration */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.confetti i {
  position: absolute;
  top: -20px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  animation: fall linear forwards;
}
.confetti i:nth-child(3n) { border-radius: 2px; height: 6px; }
@keyframes fall {
  to { transform: translateY(110vh) rotate(720deg); }
}
.won-badge { color: var(--avocado); font-weight: 700; letter-spacing: 0.04em; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
  .confetti { display: none; }
}

@media (max-height: 680px) {
  .key { min-height: 46px; font-size: 16px; }
  .tiles { padding-top: 8px; }
  .emblem { width: 170px; }
  .home h2 { font-size: 42px; }
}

/* Very short screens (small iPhones with Safari's toolbar): tighten everything
   around the flag so it keeps a decent size. */
@media (max-height: 600px) {
  .top { height: 50px; }
  .title button { font-size: 26px; }
  .tiles { padding: 6px 0 2px; }
  .stage { gap: 8px; }
  .reveal { min-height: 20px; font-size: 16px; }
  .keys { gap: 6px; padding-top: 4px; }
  .key { min-height: 40px; font-size: 15px; padding: 4px 16px; }
  .flag-grid { gap: 8px 12px; padding-top: 4px; }
  .next-row { min-height: 46px; padding-top: 6px; }
  .next-row .btn { height: 42px; }
}

/* Add-to-home-screen hint */
.install {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 320px;
  padding: 10px 12px 10px 16px;
  border-radius: 16px;
  background: var(--surface);
  font-size: 14px;
  line-height: 1.35;
  text-align: left;
}
.install .share { width: 18px; height: 18px; vertical-align: -3px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.install .x { flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; color: var(--ink-soft); font-size: 18px; }
.install .btn { height: 38px; padding: 0 16px; font-size: 15px; flex: none; }

/* Country name that pops up directly above a tapped flag */
.flag-tip {
  position: fixed;
  z-index: 70;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  font-size: 14px;
  line-height: 1.2;
  padding: 7px 12px;
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  /* Grows out of the pointer tip. The arrow sits 5px past the bubble's edge. */
  transform-origin: var(--arrow-x, 50%) calc(100% + 5px);
  animation:
    tip-fade 0.25s ease-out both,
    tip-pop 0.5s cubic-bezier(.34, 1.56, .64, 1) both;
  /* Softer, springier overshoot where supported (Safari 17.2+, Chrome 113+). */
  animation-timing-function: ease-out, linear(0, 0.34 8%, 0.8 16%, 1.05 28%, 1.09 36%, 1.04 50%, 0.99 66%, 1.005 82%, 1);
}
.flag-tip.below { transform-origin: var(--arrow-x, 50%) -5px; }
/* Reverse of the pop-in: shrinks back into the pointer tip as it fades. */
.flag-tip.out { animation: tip-out 0.22s ease-in both; }
.flag-tip::after {
  content: '';
  position: absolute;
  left: var(--arrow-x, 50%);
  bottom: -5px;
  width: 10px;
  height: 10px;
  background: var(--ink);
  transform: translateX(-50%) rotate(45deg);
  border-radius: 2px;
}
.flag-tip.below::after { bottom: auto; top: -5px; }
@keyframes tip-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes tip-out {
  from { opacity: 1; transform: translateX(-50%) scale(1); }
  to { opacity: 0; transform: translateX(-50%) scale(0.5); }
}
@keyframes tip-pop {
  from { transform: translateX(-50%) scale(0.5); }
  to { transform: translateX(-50%) scale(1); }
}

/* ---------- Question transitions: quick, springy, subtle ---------- */

/* Right answer, no message: a quick springy hop in place. */
@keyframes correct-pop {
  0%   { transform: scale(1);    animation-timing-function: ease-out; }
  30%  { transform: scale(1.07); animation-timing-function: cubic-bezier(.3, 1.5, .5, 1); }
  100% { transform: scale(1); }
}

/* Right answer WITH a message tab: the button is kicked up with a slight tilt, then drops
   back and bounces to rest like a dropped ball, as the tab springs out from behind it.
   (Easing curve adapted from uiverse.io/roroland/average-horse-2.) */
@keyframes kick {
  from { transform: translateY(-10px) rotate(-1deg); }
  to   { transform: none; }
}
@keyframes kick-flag {
  from { transform: translateY(-9px) rotate(-2deg); }
  to   { transform: none; }
}
.key.is-correct.has-tab, .flag-choice.is-correct.has-tab {
  animation-timing-function: cubic-bezier(.3, 1.5, .5, 1);
}
@supports (animation-timing-function: linear(0, 1)) {
  .key.is-correct.has-tab, .flag-choice.is-correct.has-tab {
    animation-timing-function: linear(0, 0.01566 4.55%, 0.0625 9.091%, 0.25, 0.5625, 1, 0.8125 45.455%, 0.76566 49.995%, 0.75, 0.76566 59.096%, 0.8125 63.636%, 1 72.727%, 0.95316 77.268%, 0.9375 81.818%, 0.95309 86.359%, 1 90.909%, 0.98438 95.45%, 1);
  }
}

/* The message: a tab that springs up from behind the tapped answer. */
.keys, .flag-grid { position: relative; }
/* Stacking: other answers (1) < the tab (2) < the answer it springs from (3). */
.keys > .key, .flag-grid > .flag-choice { position: relative; z-index: 1; }
.keys > .is-correct, .flag-grid > .is-correct { z-index: 3; }
.play .praise-tab {
  position: absolute;
  z-index: 2;
  transform: translateX(-50%);
  padding: 6px 16px 12px;
  background: var(--gold);
  color: var(--on-second);
  border: 2px solid var(--edge);
  border-bottom: 0;
  border-radius: 14px 14px 0 0;
  font-weight: 600;
  font-size: 15px;
  line-height: 1.2;
  white-space: nowrap;
  pointer-events: none;
  animation: tab-rise 0.5s cubic-bezier(.34, 1.56, .64, 1) both, tab-fade 0.12s linear both;
}
@supports (animation-timing-function: linear(0, 1)) {
  .play .praise-tab {
    animation-timing-function: linear(0, 0.009, 0.035 2.1%, 0.141, 0.281 6.7%, 0.723 12.9%, 0.938 16.7%, 1.017, 1.077, 1.121, 1.149 24.3%, 1.159, 1.163, 1.161, 1.154 29.9%, 1.129 32.8%, 1.051 39.6%, 1.017 43.1%, 0.991, 0.977 51%, 0.974 53.8%, 0.975 57.1%, 0.997 69.8%, 1.003 76.9%, 1.004 83.8%, 1), linear;
  }
}
@keyframes tab-rise {
  from { translate: 0 var(--hide, 30px); }
  to   { translate: 0 0; }
}
@keyframes tab-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* New question arrives. "backwards" only (no forwards fill), so tap feedback still works afterwards. */
@keyframes q-in {
  from { opacity: 0; transform: translateY(14px) scale(0.94); }
  to   { opacity: 1; transform: none; }
}
@keyframes flag-in {
  from { opacity: 0; transform: translateY(12px) rotate(-3deg) scale(0.9); }
  to   { opacity: 1; transform: none; }
}
.stage > * { animation: q-in 0.38s cubic-bezier(.2, 1.25, .4, 1) backwards; }
.stage > :nth-child(2) { animation-delay: 40ms; }
.stage > :nth-child(3) { animation-delay: 80ms; }
.stage > .flag-wrap { animation: flag-in 0.42s cubic-bezier(.2, 1.25, .4, 1) 30ms backwards; }
.keys > *, .flag-grid > * { animation: q-in 0.38s cubic-bezier(.2, 1.25, .4, 1) backwards; }
.keys > :nth-child(1), .flag-grid > :nth-child(1) { animation-delay: 90ms; }
.keys > :nth-child(2), .flag-grid > :nth-child(2) { animation-delay: 130ms; }
.keys > :nth-child(3), .flag-grid > :nth-child(3) { animation-delay: 170ms; }
.keys > :nth-child(4), .flag-grid > :nth-child(4) { animation-delay: 210ms; }
.next-row > * { animation: q-in 0.3s ease-out 240ms backwards; }

/* Feedback animations start at once (the entrance stagger delays must not leak into them). */
.play .keys > .is-correct, .play .keys > .is-wrong,
.play .flag-grid > .is-correct, .play .flag-grid > .is-wrong { animation-delay: 0s; }

/* Current question leaves (the buttons drop away a beat after the flag). */
@keyframes q-out      { to { opacity: 0; transform: translateY(-10px) scale(0.95); } }
@keyframes q-out-down { to { opacity: 0; transform: translateY(12px) scale(0.96); } }
.play.leaving .stage > * { animation: q-out 0.2s ease-in forwards; }
.play.leaving .stage > .flag-wrap { animation: q-out 0.2s ease-in forwards; }
.play.leaving .keys > *, .play.leaving .flag-grid > *, .play.leaving .next-row > * { animation: q-out-down 0.2s ease-in forwards; }
.play.leaving .keys > :nth-child(2), .play.leaving .flag-grid > :nth-child(2) { animation-delay: 20ms; }
.play.leaving .keys > :nth-child(3), .play.leaving .flag-grid > :nth-child(3) { animation-delay: 40ms; }
.play.leaving .keys > :nth-child(4), .play.leaving .flag-grid > :nth-child(4) { animation-delay: 60ms; }

/* The fine outline around flags. Nepal's flag isn't a rectangle, so it goes without one. */
.placard img, .flag-choice img, .mini-flag img, .atlas-item img { box-shadow: 0 0 0 1px var(--flag-ring); }
img[data-flag="np"] { box-shadow: none; }

/* ---------- Accomplishment card ---------- */
.acc-kicker {
  margin: 0 0 6px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.28em;
  color: var(--orange);
}
.acc-flags { display: flex; flex-wrap: wrap; justify-content: center; gap: 14px 14px; margin: 22px 0 14px; }
.acc-flags span { animation: q-in 0.45s cubic-bezier(.2, 1.25, .4, 1) calc(var(--i) * 80ms + 200ms) backwards; }
.acc-flags img { display: block; height: 44px; width: auto; }
.acc-emoji {
  text-align: center;
  font-size: 76px;
  line-height: 1.1;
  margin: 18px 0 8px;
  animation: emoji-pop 0.7s cubic-bezier(.34, 1.56, .64, 1) 0.15s backwards;
}
@keyframes emoji-pop {
  from { opacity: 0; transform: scale(0.3) rotate(-14deg); }
  to   { opacity: 1; transform: none; }
}
.acc-blurb { text-align: center; font-size: 17px; line-height: 1.45; margin: 8px 6px 0; }

/* Shown only on staging addresses. */
.env-tag {
  position: fixed;
  left: 8px;
  bottom: calc(6px + env(safe-area-inset-bottom));
  z-index: 90;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--gold);
  color: var(--on-second);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  opacity: 0.9;
  pointer-events: none;
}

/* ---------- Admin (only with ?admin=true) ---------- */
.foot-links { display: flex; justify-content: center; gap: 6px; }
.foot-link,
.admin-link {
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 6px 10px;
}
.admin-actions { display: grid; gap: 12px; margin-top: 18px; }
.admin-list { display: grid; gap: 8px; margin-top: 14px; }
.admin-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  padding: 12px 14px;
  border: 2px solid var(--edge);
  border-radius: 14px;
  background: var(--card);
  font-size: 16px;
}
.admin-row span:last-child { color: var(--ink-soft); }
