/* GeoPop — field-atlas design system.
 *
 * Idea: every screen is a plate from a survey atlas. Warm map paper, deep
 * slate ink, and ONE mark colour — terracotta — used the way a surveyor uses a
 * red pencil: it marks where you answer (the guess slip, the circle on the
 * map), what is selected, and the primary action. Nothing else is orange.
 *
 * Files: style.css (tokens, base, game screen) · start.css (landing plate) ·
 * sheets.css (archive, sign-in, duels, leaderboard).
 */

* { box-sizing: border-box; }

:root {
  /* Paper & ink */
  --paper: #E9E6DA;        /* map paper: the page */
  --sheet: #F4F2EA;        /* a sheet laid on the paper: panels, overlays */
  --sunk: #DDD9CB;         /* recessed: tracks, hovers */
  --ink: #17213A;          /* text and linework */
  --slate: #2D3C59;        /* brand Deep Slate */
  --ink-2: #3E4C68;
  --ink-3: #596477;        /* secondary text (4.8:1 on paper) */
  --ink-4: #8A919E;        /* decoration only */
  --rule: rgba(23, 33, 58, 0.16);
  --rule-strong: rgba(23, 33, 58, 0.4);

  /* The mark colour */
  --terra: #D1855C;        /* brand Terracotta: fills, marks */
  --terra-deep: #BC6E44;
  --terra-ink: #9C4A22;    /* terracotta as TEXT (5:1 on paper) */
  --terra-soft: rgba(209, 133, 92, 0.16);

  /* Meaning colours, only where they carry meaning */
  --moss: #94A378;         /* brand sage: fills */
  --moss-ink: #56683E;     /* sage as text */
  --brick: #9A4535;        /* errors, running out of time */

  /* Type */
  --font-display: "Sitka Display", "Sitka Heading", "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-ui: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "SF Pro Text", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, "Liberation Mono", monospace;

  /* Space, shape, motion */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px; --s8: 72px;
  --radius: 3px;
  --radius-lg: 6px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --ease-std: cubic-bezier(0.4, 0, 0.2, 1);
  --dur-fast: 140ms;
  --dur: 260ms;
  --dur-slow: 560ms;

  /* Layout, kept in sync by app.js (syncPanelHeight) */
  --topbar-h: 53px;
  --panel-height: 0px;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { overflow: hidden; }

h1, h2, h3 { margin: 0; }

:focus-visible {
  outline: 2px solid var(--terra-ink);
  outline-offset: 2px;
}

.hidden { display: none !important; }

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Buttons ---------- */

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

button:disabled { cursor: not-allowed; opacity: 0.45; }

/* Primary: the one action on a screen. A raised key with real travel: its
   ink body shows 3px under the cap; the cap's top edge catches the light and
   its lower edge is in shade; a soft contact shadow sits on the paper. On press
   it goes down at once (the :active state has almost no transition), the body
   and shadow close up, the highlight dims; on release it comes straight back. */
.primaryBtn {
  --key-light: rgba(255, 255, 255, 0.36);
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  padding: 13px 28px;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: 0.1px;
  color: var(--ink);
  background: linear-gradient(180deg, #DA916A 0%, var(--terra) 52%, #C97C53 100%);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  box-shadow:
    inset 0 1px 0 var(--key-light),
    inset 0 -1px 0 rgba(23, 33, 58, 0.16),
    0 3px 0 var(--ink),
    0 5px 6px -1px rgba(23, 33, 58, 0.24);
  top: 0;                     /* the travel is done with top, not transform: a transform would make the key a
                                 stacking context and lift its mounting (::before, z-index -1) above it */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: top 90ms var(--ease-std), box-shadow 90ms var(--ease-std), background var(--dur-fast) var(--ease-std);
}

.primaryBtn:hover { --key-light: rgba(255, 255, 255, 0.46); background: linear-gradient(180deg, #DE9B76 0%, #D78D65 52%, #CC825A 100%); }
.primaryBtn:active {
  top: 2px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    inset 0 2px 3px rgba(23, 33, 58, 0.16),
    0 1px 0 var(--ink),
    0 1px 2px rgba(23, 33, 58, 0.2);
  transition-duration: 25ms;
}
.primaryBtn:disabled { background: var(--terra); box-shadow: none; top: 2px; }
.primaryBtn.busy, .primaryBtn.busy:disabled { cursor: progress; opacity: 0.7; }

/* The main actions sit in a mounting: a shallow recess cut into the plate,
   a little larger than the key. It belongs to the plate, so it does not travel
   with the key (it is moved back up by exactly the key's travel). */
.startBtnBig::before,
#playAgainBtn::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: -5px -5px -8px;
  border-radius: 5px;
  background: rgba(23, 33, 58, 0.045);
  box-shadow: inset 0 1px 2px rgba(23, 33, 58, 0.2), 0 1px 0 rgba(255, 255, 255, 0.6);
  pointer-events: none;
  transition: transform 90ms var(--ease-std);
}
.startBtnBig:active::before,
#playAgainBtn:active::before { transform: translateY(-2px); transition-duration: 25ms; }
.startBtnBig:disabled::before,
#playAgainBtn:disabled::before { transform: translateY(-2px); }

.ghostBtn {
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: transparent;
  transition: background var(--dur-fast) var(--ease-std), border-color var(--dur-fast) var(--ease-std);
}

.ghostBtn:hover { background: rgba(23, 33, 58, 0.07); border-color: var(--ink); }
/* a plain key: nearly flat, it only presses in a hair */
.ghostBtn:active { background: rgba(23, 33, 58, 0.13); box-shadow: inset 0 1px 2px rgba(23, 33, 58, 0.16); transform: translateY(1px); }

/* ---------- Top bar: an instrument readout ---------- */

#topbar {
  position: relative;
  z-index: 600;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 52px;
  padding: 0 20px;
  background: var(--sheet);
  border-bottom: 1px solid var(--ink);
}

.topbarBrand {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-self: start;
  padding: 4px 6px;
  margin: -4px -6px;
  border-radius: var(--radius);
}

.topbarBrand:hover { background: rgba(23, 33, 58, 0.06); }
.topbarLogo { display: block; flex-shrink: 0; }

#topbar h1 {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.2px;
  color: var(--ink);
}

.stats { display: flex; }

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 96px;
  padding: 5px 22px 4px;
  border-left: 1px solid var(--rule);
}

.stat:last-child { border-right: 1px solid var(--rule); }

.stat .label {
  font-size: 11.5px;
  color: var(--ink-3);
  line-height: 1.2;
}

.stat span:last-child {
  display: inline-block;
  font-family: var(--font-display);
  font-size: 21px;
  font-weight: 700;
  line-height: 1.15;
  color: var(--ink);
  font-variant-numeric: lining-nums tabular-nums;
  transform-origin: left center;
  transition: transform var(--dur) var(--ease);
}

.stat span:last-child.bump { transform: scale(1.1); }

.topbarActions {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-self: end;
}

.langSwitch {
  display: flex;
  gap: 0;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  overflow: hidden;
}

.langSwitch button {
  padding: 5px 9px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--ink-3);
  transition: background var(--dur-fast) var(--ease-std), color var(--dur-fast) var(--ease-std);
}

.langSwitch button + button { border-left: 1px solid var(--rule); }
.langSwitch button:hover { color: var(--ink); background: rgba(23, 33, 58, 0.06); }
.langSwitch button.active { background: var(--ink); color: var(--sheet); box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35); }
.langSwitch button:active { transform: translateY(1px); }

/* The sound button: just a speaker, the same height as the language keys. On,
   it shows its waves; muted, it is crossed out and goes quieter in tone. */
.soundSwitch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 28px;
  padding: 0;
  color: var(--ink);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--dur-fast) var(--ease-std), background var(--dur-fast) var(--ease-std);
}
.soundSwitch:hover { background: rgba(23, 33, 58, 0.06); border-color: var(--ink); }
.soundSwitch:active { background: rgba(23, 33, 58, 0.12); transform: translateY(1px); }
.soundSwitch[aria-checked="false"] { color: var(--ink-3); }
.soundIcon { display: block; flex-shrink: 0; }
.soundSwitch[aria-checked="true"] .soundMute,
.soundSwitch[aria-checked="false"] .soundWaves { display: none; }

.authControl { display: flex; align-items: center; }

.userChip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 3px 3px 12px;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
}

.userChipEmail {
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.userChipLogout {
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--sheet);
  background: var(--ink);
  border-radius: 2px;
  white-space: nowrap;      /* the nickname gives way (ellipsis), the key never breaks in two */
  flex-shrink: 0;
}

.userChipLogout:hover { background: var(--slate); }

/* ---------- Map ---------- */

#map {
  position: absolute;
  top: calc(var(--topbar-h) + 1px);
  bottom: var(--panel-height);
  left: 0;
  right: 0;
  background: var(--paper);
}

/* The OSM raster is toned toward paper so the map reads as a printed plate
   and the terracotta circle is the loudest thing on it. Colours are only
   desaturated, never altered: geography stays accurate. */
.leaflet-tile-pane {
  filter: grayscale(0.5) sepia(0.28) saturate(0.85) contrast(0.95) brightness(1.03);
}

#map .leaflet-top,
#map .leaflet-bottom { z-index: 400; }

.leaflet-bar {
  border: 1px solid var(--ink) !important;
  border-radius: var(--radius) !important;
  box-shadow: none !important;
  overflow: hidden;
}

.leaflet-bar a,
.leaflet-bar a:hover {
  background-color: var(--sheet);
  color: var(--ink);
  border-bottom: 1px solid var(--rule);
}

.leaflet-bar a:hover { background-color: var(--sunk); }

.leaflet-control-attribution {
  background: rgba(244, 242, 234, 0.8) !important;
  color: var(--ink-3) !important;
  prefix: false;
  font-size: 10px;
}

#minimapWrap {
  position: absolute;
  right: 16px;
  bottom: calc(var(--panel-height) + 16px);
  z-index: 500;
}

/* The mercator world raster is square, so the minimap must be too, or it
   clips longitude at the sides (or latitude top and bottom). */
#minimap {
  width: clamp(140px, 17vw, 240px);
  height: clamp(140px, 17vw, 240px);
  overflow: hidden;
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  background: var(--sunk);
  box-shadow: 4px 4px 0 rgba(23, 33, 58, 0.18);
  transition: width var(--dur) var(--ease-std), height var(--dur) var(--ease-std);
}

#minimapWrap.expanded #minimap {
  width: min(60vw, calc(100vh - 305px), 560px);
  height: min(60vw, calc(100vh - 305px), 560px);
}

#minimapToggle {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 10;
  width: 28px;
  height: 28px;
  font-size: 15px;
  line-height: 1;
  color: var(--ink);
  background: var(--sheet);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
}

#minimapToggle:hover { background: var(--sunk); }

/* Result / summary / loading sheet along the bottom edge */

#panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 520;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 28px 20px;
  background: var(--sheet);
  border-top: 1px solid var(--ink);
}

/* During a round the guess slip floats over the map instead. */
#panel.collapsed { padding: 0; border-top: 0; background: transparent; }

.row {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
}

.row:not(.hidden) { animation: rowIn var(--dur) var(--ease); }

@keyframes rowIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.error { color: var(--brick); font-size: 14px; }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--rule-strong);
  border-top-color: var(--terra);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
  vertical-align: middle;
}

@keyframes spin { to { transform: rotate(360deg); } }

#loadingRow {
  flex-direction: row;
  align-items: center;
  justify-content: center;
  padding: 6px 0;
  color: var(--ink-3);
  font-family: var(--font-display);
  font-style: italic;
  font-size: 17px;
}

#errorRow { align-items: center; gap: var(--s3); text-align: center; }
.errorText { max-width: 60ch; margin: 0; font-size: 14.5px; line-height: 1.5; overflow-wrap: anywhere; }
.errorBtnRow { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }

/* ---------- Guess slip (the focal point of a round) ---------- */

.guessHud {
  position: absolute;
  left: 50%;
  bottom: 28px;
  z-index: 550;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: min(94vw, 470px);
  padding: 14px 16px 12px;
  background: var(--sheet);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  /* the terracotta offset is the single "stamp" in the interface */
  box-shadow: 6px 6px 0 var(--terra), 0 22px 40px -14px rgba(23, 33, 58, 0.5);
  transform: translateX(-50%);
  animation: slipIn var(--dur) var(--ease);
}

@keyframes slipIn {
  from { opacity: 0; transform: translateX(-50%) translateY(14px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.guessHudRow { display: flex; flex-direction: column; gap: 6px; }

/* The circle's radius: its own plate, marked like the other things you answer
   with (the terracotta edge), the value set large. */
.guessRadius {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 5px 12px 5px 10px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-3);
  background: var(--sunk);
  border-left: 3px solid var(--terra-deep);
  border-radius: 2px;
  font-variant-numeric: tabular-nums;
}

.grValue {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.2px;
  color: var(--ink);
  font-variant-numeric: lining-nums tabular-nums;
  transform-origin: left center;
}

/* a new round: the value lifts in the mark colour for a moment, then settles */
.guessRadius.isNew .grValue { animation: radiusFlag 1100ms var(--ease) both; }

@keyframes radiusFlag {
  0%, 35% { color: var(--terra-ink); transform: scale(1.12); }
  100% { color: var(--ink); transform: scale(1); }
}

/* a surveyor's scale bar in front of the radius */
.guessRadius::before {
  content: "";
  flex: 0 0 26px;
  height: 7px;
  border: 1.5px solid var(--ink);
  border-top: 0;
  border-bottom-width: 1.5px;
  border-radius: 0;
  background:
    linear-gradient(var(--ink), var(--ink)) 0 0 / 1.5px 100% no-repeat,
    linear-gradient(var(--ink), var(--ink)) 100% 0 / 1.5px 100% no-repeat;
  border-left: 0;
  border-right: 0;
}

.guessRadius:empty { display: none; }

.duelBadge {
  align-self: flex-start;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--sheet);
  background: var(--slate);
  border-radius: 2px;
}

.guessInputWrap {
  position: relative;
  display: flex;
  align-items: center;
  border-bottom: 2px solid var(--ink);
  transition: border-color var(--dur) var(--ease-std);
}

.guessInputWrap:focus-within { border-color: var(--terra-deep); }

#guessInput {
  flex: 1;
  min-width: 0;
  padding: 4px 0 6px;
  font-family: var(--font-ui);
  font-size: 40px;
  font-weight: 650;
  letter-spacing: -0.5px;
  color: var(--ink);
  background: transparent;
  border: 0;
  outline: 0;
  font-variant-numeric: tabular-nums;
}

#guessInput::placeholder { color: var(--ink-4); opacity: 0.7; }

.guessSuffix {
  margin: 0 12px 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-3);
  pointer-events: none;
}

.guessConfirmBtn {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 40px;
  margin-bottom: 4px;
  color: var(--ink);
  background: var(--terra);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 0 2px 0 var(--ink);
  transition: transform var(--dur-fast) var(--ease-std), box-shadow var(--dur-fast) var(--ease-std);
}

.guessConfirmBtn:hover { background: #D99468; }
.guessConfirmBtn:active { transform: translateY(1px); box-shadow: 0 1px 0 var(--ink), inset 0 2px 3px rgba(23, 33, 58, 0.18); transition-duration: 25ms; }

/* nothing typed yet: the stamp waits, uninked */
.guessHud:not(.hasGuess) .guessConfirmBtn {
  color: var(--ink-3);
  background: var(--sheet);
  border-color: var(--rule-strong);
  box-shadow: 0 2px 0 var(--rule-strong);
}

.guessHint { font-size: 12px; color: var(--ink-3); transition: color var(--dur-fast) var(--ease-std); }
.guessHint.isWarning { color: var(--terra-ink); font-weight: 650; }

/* Enter on an empty slip: a short sideways shake, like a stamp that didn't take */
.guessHud.nudge { animation: slipNudge 360ms var(--ease-std); }

@keyframes slipNudge {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 6px)); }
  60% { transform: translateX(calc(-50% + 4px)); }
}

.guessError {
  padding: 5px 10px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: var(--brick);
  border-radius: 2px;
}

/* Time left: a band across the top of the slip, with the seconds on a tab
   hanging from its right end. It escalates in three steps (data-time-stage,
   set by app.js): warn at 7 s, low at 5 s, critical at 3 s. */
.timerBarWrap {
  --bar-h: 5px;
  position: relative;
  height: var(--bar-h);
  margin: -14px -16px 18px;
  background: var(--sunk);
  box-shadow: inset 0 1px 1px rgba(23, 33, 58, 0.18);
  transition: height 220ms var(--ease), margin-bottom 220ms var(--ease);
}

.timerBar {
  height: 100%;
  background: var(--ink);
  transition: background 220ms var(--ease-std);
}

.timerText {
  position: absolute;
  right: 10px;
  top: 100%;
  padding: 2px 8px 3px;
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 700;
  line-height: 1.1;
  color: var(--sheet);
  background: var(--ink);
  border-radius: 0 0 3px 3px;
  font-variant-numeric: lining-nums tabular-nums;
  transform-origin: top center;
  transition: background 220ms var(--ease-std), font-size 220ms var(--ease), padding 220ms var(--ease);
}

/* 7 s: the band thickens and takes the mark colour */
.guessHud[data-time-stage="warn"] .timerBarWrap { --bar-h: 7px; }
.guessHud[data-time-stage="warn"] .timerBar,
.guessHud[data-time-stage="warn"] .timerText { background: var(--terra-deep); }

/* 5 s: brick, thicker, the seconds larger */
.guessHud[data-time-stage="low"] .timerBarWrap,
.guessHud[data-time-stage="critical"] .timerBarWrap { --bar-h: 9px; }
.guessHud[data-time-stage="low"] .timerBar,
.guessHud[data-time-stage="low"] .timerText,
.guessHud[data-time-stage="critical"] .timerBar,
.guessHud[data-time-stage="critical"] .timerText { background: var(--brick); }
.guessHud[data-time-stage="low"] .timerText { font-size: 18px; }

/* 3 s, the most: the band at its fullest, the seconds largest, the slip's own
   stamp and edge turn brick, and the edges of the screen glow faintly */
.guessHud[data-time-stage="critical"] .timerBarWrap { --bar-h: 12px; margin-bottom: 26px; }
.guessHud[data-time-stage="critical"] .timerText { font-size: 22px; padding: 3px 10px 4px; }
.guessHud[data-time-stage="critical"] {
  border-color: var(--brick);
  box-shadow: 6px 6px 0 var(--brick), 0 22px 40px -14px rgba(23, 33, 58, 0.5);
}
body.timeCritical::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 540;              /* over the map, under the slip */
  pointer-events: none;
  box-shadow: inset 0 0 0 3px rgba(154, 69, 53, 0.6), inset 0 0 60px rgba(154, 69, 53, 0.22);
  animation: edgeIn 300ms var(--ease) both;
}
@keyframes edgeIn { from { opacity: 0; } }

/* every one of the last seconds: the tab and the band give a beat with the sound */
.timerText.beat { animation: secondBeat 420ms var(--ease); }
.timerBarWrap.beat { animation: bandBeat 420ms var(--ease); }
@keyframes secondBeat {
  0% { transform: scale(1.18); }
  100% { transform: scale(1); }
}
@keyframes bandBeat {
  0% { filter: brightness(1.35); }
  100% { filter: none; }
}

/* ---------- Round reveal ---------- */

#resultRow {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr) auto;
  grid-template-areas:
    "actual guess score next"
    "ref ref ref ref"
    "axis axis axis axis";
  column-gap: var(--s6);
  row-gap: var(--s4);
  align-items: end;
}

.resBlock { display: flex; flex-direction: column; min-width: 0; }
.resActualBlock { grid-area: actual; }
#resGuessBlock { grid-area: guess; }
#resScoreBlock { grid-area: score; }
#nextBtn { grid-area: next; align-self: center; }
.resAxis { grid-area: axis; }

.resBlock .label {
  font-size: 12.5px;
  color: var(--ink-3);
  margin-bottom: 2px;
}

.resBlock > span:not(.label):not(.resSub) {
  font-family: var(--font-display);
  font-weight: 700;
  line-height: 1.05;
  color: var(--ink);
  font-variant-numeric: lining-nums tabular-nums;
}

.resActualBlock > #resActual { font-size: clamp(34px, 5vw, 56px); letter-spacing: -1px; color: var(--terra-ink); }
#resGuessBlock > span:not(.label):not(.resSub),
#resScoreBlock > span:not(.label):not(.resSub) { font-size: clamp(22px, 2.6vw, 30px); }

.resSub { margin-top: 6px; font-size: 13px; font-weight: 600; color: var(--ink-3); font-variant-numeric: tabular-nums; }

.resLabel {
  margin-top: 6px;
  font-family: var(--font-display);
  font-style: italic;
  font-size: 17px;
  font-weight: 600;
  color: var(--ink-3);
}

.resLabel.pop { animation: labelPop var(--dur) var(--ease); }

@keyframes labelPop {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}

/* praise earns colour; misses just go quiet */
.resLabel.tier-perfect,
.resLabel.tier-great { color: var(--moss-ink); }
.resLabel.tier-good { color: var(--ink); }

/* Log-scale comparison: where the guess landed vs. the truth. The distance
   between the two marks is the error, in orders of magnitude. */
.axis { position: relative; height: 62px; margin: 0 10px; }
.axTrack { position: absolute; left: 0; right: 0; top: 30px; height: 2px; background: var(--ink); }
.axTickMark { position: absolute; top: 26px; width: 1px; height: 10px; background: var(--ink); }
.axTickLabel {
  position: absolute;
  top: 42px;
  transform: translateX(-50%);
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-3);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.axErr { position: absolute; top: 28px; height: 6px; background: var(--terra); transform-origin: left center; }
.axActual, .axGuess {
  position: absolute;
  top: 31px;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border: 2px solid var(--ink);
  border-radius: 50%;
}
.axActual { background: var(--terra); }
.axGuess { background: var(--sheet); }
.axTag {
  position: absolute;
  top: 2px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
}
.axTagActual { color: var(--terra-ink); }
.axTag.toLeft { transform: translateX(-100%); }
.axTag.toRight { transform: none; }
.resAxis.reveal .axErr { animation: axDraw 700ms var(--ease) both 200ms; }
.resAxis.reveal .axActual { animation: axPop 420ms var(--ease) both 600ms; }
.resAxis.reveal .axGuess { animation: axPop 360ms var(--ease) both 80ms; }

@keyframes axDraw { from { transform: scaleX(0); } to { transform: scaleX(1); } }
@keyframes axPop { from { transform: scale(0); } to { transform: scale(1); } }

/* the circle on the map draws itself */
.ringDraw { stroke-dasharray: 1; animation: ringDraw 1.1s var(--ease) both; }
@keyframes ringDraw { from { stroke-dashoffset: 1; fill-opacity: 0; } to { stroke-dashoffset: 0; } }

/* ---------- Summary ---------- */

#summaryRow {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "score strip actions";
  column-gap: var(--s6);
  align-items: end;
}

.summaryText { grid-area: score; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; color: var(--ink-3); font-size: 14px; }
.summaryText > span:first-child { flex-basis: 100%; font-family: var(--font-display); font-style: italic; font-size: 18px; color: var(--ink); }

.summaryText b {
  font-family: var(--font-display);
  font-size: clamp(38px, 5vw, 60px);
  line-height: 1;
  letter-spacing: -1px;
  color: var(--ink);
  font-variant-numeric: lining-nums tabular-nums;
}

.summaryText b + span + b { font-size: 24px; color: var(--ink-3); letter-spacing: 0; }

.summaryStrip {
  grid-area: strip;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 64px;
  min-width: 0;
  border-bottom: 1.5px solid var(--ink);
}

.stripBar {
  position: relative;
  flex: 1 1 0;
  max-width: 34px;
  min-height: 2px;
  background: var(--slate);
  transform-origin: bottom;
  animation: barUp 520ms var(--ease) both;
  animation-delay: calc(var(--i) * 45ms);
}

.stripBar.strong { background: var(--terra); }

@keyframes barUp { from { transform: scaleY(0); } to { transform: scaleY(1); } }

.summaryBtnRow { grid-area: actions; display: flex; flex-direction: column; align-items: stretch; gap: 10px; }
.summaryMinorBtns { display: flex; gap: 8px; }
.summaryMinorBtns .ghostBtn { flex: 1 1 auto; white-space: nowrap; }

/* ---------- Narrow screens ---------- */

@media (max-width: 860px) {
  #topbar { grid-template-columns: 1fr auto; row-gap: 0; padding: 6px 12px 0; }
  #topbar .stats { grid-column: 1 / -1; order: 3; justify-content: stretch; margin: 6px -12px 0; border-top: 1px solid var(--rule); }
  .stat { flex: 1; min-width: 0; padding: 4px 12px; }
  .stat:first-child { border-left: 0; }
  .stat:last-child { border-right: 0; }
  .topbarActions { flex-wrap: wrap; justify-content: flex-end; gap: 6px; }

  #panel { padding: 14px 16px 16px; }
  #resultRow {
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "actual actual" "guess score" "ref ref" "axis axis" "next next";
    row-gap: var(--s3);
  }
  #nextBtn { width: 100%; }
  #summaryRow { grid-template-columns: 1fr; grid-template-areas: "score" "strip" "actions"; row-gap: var(--s4); }
  .summaryBtnRow { flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center; }
  /* on a phone the guess slip owns the bottom edge, so the overview map moves up */
  #minimapWrap { right: 10px; top: calc(var(--topbar-h) + 10px); bottom: auto; }
  #minimap { width: 104px; height: 104px; }
  #minimapWrap.expanded #minimap { width: min(86vw, 60vh); height: min(86vw, 60vh); }
  .axTickLabel.minor { display: none; }
  .guessHud { bottom: 16px; box-shadow: 4px 4px 0 var(--terra), 0 18px 30px -12px rgba(23, 33, 58, 0.5); }
  #guessInput { font-size: 32px; }
}

@media (max-width: 520px) {
  /* a phone's top bar is one row of actions; the wordmark yields to them */
  #topbar h1 { display: none; }
  .topbarActions { flex-wrap: nowrap; gap: 5px; }
  .topbarActions .ghostBtn { padding: 5px 8px; font-size: 12px; }
  .userChip { padding-left: 8px; font-size: 12px; }
  .userChipEmail { max-width: 70px; }
  /* in play, the nickname yields so the bar fits a phone (the menu still shows it) */
  #topbar { grid-template-columns: auto minmax(0, 1fr); }
  #topbar .userChipEmail { display: none; }
  #topbar .userChip { padding-left: 3px; }
  .topbarActions { flex-wrap: wrap; min-width: 0; }
  .langSwitch button { padding: 5px 7px; }
  /* in play the bar must stay one row on a phone (a second row eats the map):
     the keys tighten a little to make room for the sound button */
  #topbar .topbarActions { gap: 4px; }
  #topbar .langSwitch button { padding: 5px 5px; }
  #topbar .topbarActions .ghostBtn { padding: 5px 7px; }
  #topbar .soundSwitch { width: 26px; }
}

/* the narrowest phones (320 px): in play the language switch yields its place -
   it is still on the landing plate, and the bar stays one row */
@media (max-width: 350px) {
  #topbar .langSwitch { display: none; }
  .summaryBtnRow { flex-direction: column; align-items: stretch; }
}


/* A phone on its side (or any short window): the map is under 300 px tall, so
   the slip lies down to one row - the radius plate beside the answer, no hint
   line - and leaves the circle the room above it. */
@media (max-height: 500px) and (min-width: 560px) {
  .guessHud {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 14px;
    row-gap: 6px;
    width: min(94vw, 580px);
    padding: 10px 14px 8px;
    bottom: 12px;
  }
  .timerBarWrap { flex: 0 0 calc(100% + 28px); margin: -10px -14px 14px; }
  .duelBadge { flex: 0 0 auto; }
  .guessRadius { flex: 0 0 auto; }
  .guessHudRow { flex: 1 1 220px; min-width: 0; }
  .guessHint:not(.isWarning) { display: none; }   /* the "type a number first" warning still shows */
  #guessInput { font-size: 30px; }
  /* the last seconds still escalate, but the slip grows only a little, not over the circle */
  .guessHud[data-time-stage="low"] .timerBarWrap { --bar-h: 8px; }
  .guessHud[data-time-stage="critical"] .timerBarWrap { --bar-h: 10px; margin-bottom: 16px; }
  .guessHud[data-time-stage="critical"] .timerText { font-size: 19px; padding: 2px 9px 3px; }
}

/* ---------- "For scale": something well known with about as many people ---------- */

.refBox {
  grid-area: ref;
  display: grid;
  grid-template-columns: auto minmax(0, 1.3fr) auto minmax(0, 1fr) auto;
  column-gap: var(--s5);
  row-gap: 4px;
  align-items: center;
  padding: 10px 0 10px 14px;
  border-top: 1px solid var(--rule-strong);
  border-left: 3px solid var(--terra);
  animation: rowIn var(--dur) var(--ease);
}

.refCaption { font-family: var(--font-display); font-style: italic; font-size: 15px; color: var(--ink-3); }

.refMain { display: flex; flex-direction: column; min-width: 0; }

.refName {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.3px;
  overflow-wrap: anywhere;
}

.refDesc { font-size: 12.5px; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.refNum { display: flex; flex-direction: column; align-items: flex-start; }

.refValue {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  line-height: 1.1;
  color: var(--terra-ink);
  font-variant-numeric: lining-nums tabular-nums;
}

.refWhat { font-size: 12px; color: var(--ink-3); }

.refRel { font-size: 14px; font-weight: 650; color: var(--ink); }
.refRel.same { color: var(--moss-ink); }

.refSrc { font-size: 12.5px; color: var(--ink-3); text-align: right; }
.refSrcLabel { color: var(--ink-3); }

.refBox a { color: var(--ink); text-decoration: none; border-bottom: 1px solid var(--rule-strong); }
.refBox a:hover { color: var(--terra-ink); border-color: var(--terra-ink); }

.refSrc details { position: relative; display: inline-block; }
.refSrc summary { display: inline; cursor: pointer; color: var(--ink-3); border-bottom: 1px dotted var(--ink-4); list-style: none; }
.refSrc summary::-webkit-details-marker { display: none; }
.refSrc summary::before { content: "· "; }
.refSrc details[open] summary { color: var(--ink); }

.refSrcList {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  z-index: 30;
  width: min(440px, 86vw);
  margin: 0;
  padding: 10px 14px;
  list-style: none;
  text-align: left;
  background: var(--sheet);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 4px 4px 0 rgba(23, 33, 58, 0.2);
}

.refSrcList li { display: grid; grid-template-columns: 1fr auto; gap: 0 12px; padding: 6px 0; border-bottom: 1px solid var(--rule); }
.refSrcList li:last-child { border-bottom: 0; }
.refSrcName { font-weight: 650; color: var(--ink); }
.refSrcVal { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.refSrcLic { grid-column: 1 / -1; font-size: 11.5px; color: var(--ink-3); }

@media (max-width: 860px) {
  .refBox { grid-template-columns: 1fr auto; padding-left: 12px; }
  .refCaption { grid-column: 1 / -1; }
  .refMain { grid-column: 1; }
  .refNum { grid-column: 2; align-items: flex-end; }
  .refRel, .refSrc { grid-column: 1 / -1; text-align: left; }
  .refSrcList { right: auto; left: 0; }
}


/* ---------- Touch screens: the same look, fingertip-sized hit areas ---------- */

@media (pointer: coarse) {
  .ghostBtn,
  .userChipLogout,
  .langSwitch button { min-height: 40px; }
  .userChip { min-height: 40px; }
  #minimapToggle { width: 36px; height: 36px; }
  .leaflet-touch .leaflet-bar a { width: 36px; height: 36px; line-height: 36px; }
}

/* a touch keyboard has no Enter/space shortcut to explain; keep only the warnings */
@media (pointer: coarse) {
  .guessHint:not(.isWarning) { display: none; }
}

/* the sheets take focus themselves; their own outline would frame the whole card */
.overlay > [tabindex="-1"]:focus { outline: none; }
