:root {
  --ink: #111;
  --ink-soft: #444;
  --ink-faint: #888;
  --on-ink: #fff;          /* text placed on top of --ink surfaces */
  --paper: #fafaf8;
  --card: #ffffff;
  --line: #e2e2de;
  --good: #1a7f37;
  --bad: #b42318;
  --flash: #f2c94c;
  --map-bg: #efefec;
  --map-land: #ffffff;
  --map-stroke: #555;
  --map-guessed: #1c1c1c;
  --map-solid: #cfcec7;    /* land on the borderless (streak) map */
  --kb-bg: #e2e1dc;        /* in-app keyboard: tray, letter keys, ⌫/return */
  --kb-key: #ffffff;
  --kb-special: #c4c3bb;
  --display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #ecebe3;
    --ink-soft: #c2c1b8;
    --ink-faint: #8b8a81;
    --on-ink: #131311;
    --paper: #131311;
    --card: #1c1c1a;
    --line: #32322e;
    --good: #5cbd7d;
    --bad: #e5695c;
    --map-bg: #151513;
    --map-land: #1c1c1a;
    --map-stroke: #9b9a90;
    --map-guessed: #ecebe3;
    --map-solid: #4a4943;
    --kb-bg: #232321;
    --kb-key: #57564f;
    --kb-special: #383834;
  }
}
:root[data-theme="dark"] {
  --ink: #ecebe3; --ink-soft: #c2c1b8; --ink-faint: #8b8a81; --on-ink: #131311;
  --paper: #131311; --card: #1c1c1a; --line: #32322e;
  --good: #5cbd7d; --bad: #e5695c;
  --map-bg: #151513; --map-land: #1c1c1a; --map-stroke: #9b9a90; --map-guessed: #ecebe3;
  --map-solid: #4a4943;
  --kb-bg: #232321; --kb-key: #57564f; --kb-special: #383834;
}
:root[data-theme="light"] {
  --ink: #111; --ink-soft: #444; --ink-faint: #888; --on-ink: #fff;
  --paper: #fafaf8; --card: #ffffff; --line: #e2e2de;
  --good: #1a7f37; --bad: #b42318;
  --map-bg: #efefec; --map-land: #ffffff; --map-stroke: #555; --map-guessed: #1c1c1c;
  --map-solid: #cfcec7;
  --kb-bg: #e2e1dc; --kb-key: #ffffff; --kb-special: #c4c3bb;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
  min-height: 100vh;
  /* clear the notch / home indicator (viewport-fit=cover, and the iOS app) */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.wrap { max-width: 1200px; margin: 0 auto; padding: 16px 20px 40px; }

header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 6px 0 14px; }
.letters-progress { font-family: var(--display); font-size: 24px; letter-spacing: 0.2px; font-variant-numeric: tabular-nums; }
.letters-progress small {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 12px; color: var(--ink-faint); text-transform: uppercase; letter-spacing: 1.5px; margin-left: 8px;
}
header .total { margin-left: auto; font-size: 14px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.menu { position: relative; flex-shrink: 0; }
#menuBtn {
  width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--card); font-size: 18px; line-height: 1; color: var(--ink);
  display: grid; place-items: center;
}
#menuBtn:hover { border-color: var(--ink-soft); }
.menu-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 30; min-width: 190px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25); overflow: hidden; padding: 4px;
}
.menu-dropdown button, .menu-dropdown a {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 10px 12px; border: none; border-radius: 7px; background: none;
  font-family: inherit; font-size: 14px; color: var(--ink); text-decoration: none;
}
.menu-dropdown button:hover, .menu-dropdown a:hover { background: var(--ink); color: var(--on-ink); }
.menu-dropdown a.checked { font-weight: 700; }
.menu-dropdown a.checked::after { content: "✓"; margin-left: auto; }
.menu-label {
  padding: 8px 12px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--ink-faint);
}
.menu-dropdown hr { border: none; border-top: 1px solid var(--line); margin: 4px 6px; }

/* Alphabet strip — clickable letter states:
   (default) untouched · .partial some found · .done all found · .given gave up
   .current marks the letter being played and combines with the others */
.letters { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 16px; }
.letters button {
  width: 30px; height: 30px; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 6px;
  font-family: inherit; font-size: 13px; font-weight: 600; color: var(--ink-faint);
  background: var(--card); user-select: none; cursor: pointer; padding: 0;
  transition: transform 0.12s, border-color 0.12s;
}
.letters button:hover { border-color: var(--ink-soft); color: var(--ink); }
.letters button.done { background: var(--good); border-color: var(--good); color: var(--on-ink); }
.letters button.partial { background: var(--line); border-color: var(--line); color: var(--ink); }
.letters button.given { background: var(--bad); border-color: var(--bad); color: var(--on-ink); text-decoration: line-through; }
.letters button.current { border: 2px solid var(--ink); border-style: solid; color: var(--ink); transform: scale(1.12); }
.letters button.done.current { outline: 2px solid var(--good); outline-offset: 1px; color: var(--on-ink); }
.letters button.given.current { outline: 2px solid var(--bad); outline-offset: 1px; color: var(--on-ink); }

.layout { display: grid; grid-template-columns: 390px 1fr; gap: 20px; align-items: start; }
@media (max-width: 950px) { .layout { grid-template-columns: 1fr; } }

.panel {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.letter-row { display: flex; align-items: center; gap: 16px; }
.big-letter {
  font-family: var(--display);
  font-size: 60px; font-weight: 700; line-height: 1; width: 84px; height: 84px;
  display: grid; place-items: center; border: 2px solid var(--ink); border-radius: 14px;
  flex-shrink: 0;
}
.counts { font-size: 15px; color: var(--ink-soft); line-height: 1.5; }
.counts b { color: var(--ink); font-size: 17px; }
.counts .nums { display: flex; gap: 14px; margin-top: 6px; font-variant-numeric: tabular-nums; }
.counts .nums span b { display: block; font-size: 22px; }

.inputrow { display: flex; gap: 8px; margin-top: 18px; position: relative; }
#guess {
  flex: 1; font-size: 17px; padding: 11px 14px; border: 1.5px solid var(--ink-soft);
  border-radius: 10px; outline: none; background: var(--card); color: var(--ink); min-width: 0;
}
#guess:focus { border-color: var(--ink); box-shadow: 0 0 0 3px rgba(0,0,0,0.07); }
#guess.shake { animation: shake 0.3s; }
@keyframes shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-6px)} 75%{transform:translateX(6px)} }

button { font-family: inherit; cursor: pointer; }
#micBtn {
  width: 46px; border: 1.5px solid var(--ink-soft); border-radius: 10px; background: var(--card);
  font-size: 20px; flex-shrink: 0;
}
#micBtn.listening { background: var(--ink); border-color: var(--ink); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100%{box-shadow: 0 0 0 0 rgba(0,0,0,0.25)} 50%{box-shadow: 0 0 0 6px rgba(0,0,0,0.08)} }
#micBtn[hidden] { display: none; }

/* Autocomplete dropdown */
#suggestions {
  position: absolute; top: calc(100% + 4px); left: 0; right: 54px; z-index: 20;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25); overflow: hidden; display: none;
}
#suggestions.open { display: block; }
#suggestions div { padding: 10px 14px; font-size: 15px; cursor: pointer; }
#suggestions div.active, #suggestions div:hover { background: var(--ink); color: var(--on-ink); }
#suggestions div small { color: inherit; opacity: 0.6; margin-left: 6px; }

#feedback { min-height: 24px; margin-top: 10px; font-size: 14px; }
#feedback.ok { color: var(--good); }
#feedback.err { color: var(--bad); }
#feedback.info { color: var(--ink-faint); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chips span {
  font-size: 13px; padding: 4px 10px; border-radius: 999px;
  background: var(--ink); color: var(--on-ink);
}
.chips span.missed { background: var(--card); color: var(--bad); border: 1px solid var(--bad); text-decoration: line-through; }

.actions { margin-top: 16px; display: flex; gap: 8px; }
.actions button {
  font-size: 14px; padding: 9px 14px; border-radius: 10px;
  border: 1.5px solid var(--ink-soft); background: var(--card); color: var(--ink);
}
.actions #skipBtn { flex: 1; }
.actions button.nav { width: 44px; padding: 9px 0; font-size: 16px; flex-shrink: 0; }
.actions button:hover:not(:disabled) { background: var(--ink); color: var(--on-ink); border-color: var(--ink); }
.actions button:disabled { opacity: 0.45; cursor: default; }

/* Map */
.mapbox { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }
#map { width: 100%; height: auto; display: block; background: var(--map-bg); border-radius: 8px; }
#map .land { fill: var(--map-land); stroke: var(--map-stroke); stroke-width: 0.6; stroke-linejoin: round; transition: fill 0.5s; }
#map .land.guessed { fill: var(--map-guessed); }
#map .land.flash { fill: var(--flash); transition: none; }
#map .dot { fill: var(--map-land); stroke: var(--map-stroke); stroke-width: 0.8; transition: fill 0.5s; }
#map .dot.guessed { fill: var(--map-guessed); }
#map .dot.flash { fill: var(--flash); transition: none; }

/* End screen */
#endscreen { text-align: center; }
#endscreen h2 { font-family: var(--display); font-size: 26px; margin: 4px 0 2px; }
#endscreen .score { font-family: var(--display); font-size: 56px; font-weight: 700; margin: 10px 0 0; font-variant-numeric: tabular-nums; }
#endscreen .pct { color: var(--ink-faint); margin-bottom: 16px; }
#breakdown { display: flex; flex-wrap: wrap; gap: 4px; justify-content: center; margin: 14px 0; }
#breakdown span {
  width: 34px; padding: 4px 0 5px; border: 1px solid var(--line); border-radius: 6px;
  font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums;
}
#breakdown span b { display: block; font-size: 14px; color: var(--ink); }
#breakdown span.full { background: var(--ink); border-color: var(--ink); }
#breakdown span.full b, #breakdown span.full { color: var(--on-ink); }
#missedlist { text-align: left; font-size: 13px; color: var(--ink-soft); max-height: 180px; overflow-y: auto; border-top: 1px solid var(--line); padding-top: 10px; margin-top: 6px; }
#playagain { margin-top: 16px; font-size: 16px; padding: 12px 26px; border-radius: 10px; border: none; background: var(--ink); color: var(--on-ink); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ---------- Streak mode ---------- */
.clock {
  position: relative; margin-left: auto;
  font-family: var(--display); font-size: 30px; font-weight: 700; line-height: 1;
  font-variant-numeric: tabular-nums; transition: color 0.2s;
}
.clock.low { color: var(--bad); }
.delta {
  position: absolute; right: 0; top: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px; font-weight: 700; opacity: 0; pointer-events: none; white-space: nowrap;
}
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.delta.go { animation: delta-pop 1s ease-out forwards; }
@keyframes delta-pop {
  0% { opacity: 0; transform: translateY(6px); }
  15% { opacity: 1; transform: translateY(0); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-8px); }
}

.panel-title { font-family: var(--display); font-size: 26px; margin: 0 0 8px; }
.rules { margin: 0 0 10px; color: var(--ink-soft); line-height: 1.5; }
.rules-list { margin: 0 0 14px; padding-left: 20px; line-height: 1.8; color: var(--ink-soft); }
.rules-list b { color: var(--ink); }
.rules-list b.good { color: var(--good); }
.rules-list b.bad { color: var(--bad); }
.best-line { font-size: 14px; color: var(--ink-faint); margin-bottom: 12px; min-height: 1em; }
button.primary {
  width: 100%; font-size: 17px; padding: 13px 26px; border-radius: 10px; border: none;
  background: var(--ink); color: var(--on-ink); font-weight: 600;
}
.streak-mode .inputrow { margin-top: 0; }
/* the mic sits in the header, left of the menu, styled like the menu button */
.streak-mode header #micBtn {
  width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 9px;
  font-size: 17px; line-height: 1; display: grid; place-items: center; padding: 0;
}
.streak-mode header #micBtn.listening { border-color: var(--ink); }
.streak-mode header #micBtn.idle { visibility: hidden; }
.streak-mode #suggestions { right: 0; }
.streak-mode #skipBtn {
  flex-shrink: 0; padding: 0 14px; border: 1.5px solid var(--ink-soft); border-radius: 10px;
  background: var(--card); color: var(--ink); font-size: 15px;
}
.streak-mode #skipBtn:disabled { opacity: 0.45; cursor: default; }
/* one line, so the box under the map never changes height mid-run */
.streak-mode #feedback { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.streak-mode #endscreen .chips { justify-content: center; max-height: 220px; overflow-y: auto; margin: 6px 0 4px; }

/* Map without borders: every country is stroked in its own fill colour, so
   only coastlines show. Microstate dots are hidden except the mystery one. */
/* the 1.5px non-scaling stroke also hides anti-aliasing seams that would hint at borders */
#map.borderless .land {
  fill: var(--map-solid); stroke: var(--map-solid); stroke-width: 1.5px; vector-effect: non-scaling-stroke;
  transition: fill 0.35s, stroke 0.35s;
}
#map.borderless .dot { display: none; }
#map.borderless .dot { fill: var(--map-solid); stroke: var(--map-stroke); }
#map.borderless .dot.target { display: inline; }
#map .land.reveal-good, #map .dot.reveal-good { fill: var(--good); stroke: var(--good); }
#map .land.reveal-bad, #map .dot.reveal-bad { fill: var(--bad); stroke: var(--bad); }
#map .land.reveal-wrong, #map .dot.reveal-wrong { fill: var(--bad); stroke: var(--bad); opacity: 0.45; }
#map .dot.reveal-wrong { display: inline; }
#map .pin { pointer-events: none; }
#map .pin-head { fill: var(--bad); stroke: var(--card); stroke-width: 1.4; }
#map .pin-hole { fill: var(--card); }
#map .pin-pulse {
  fill: none; stroke: var(--bad); stroke-width: 1.5;
  transform-box: fill-box; transform-origin: center; animation: pin-pulse 1.6s ease-out infinite;
}
@keyframes pin-pulse {
  0% { transform: scale(0.4); opacity: 0.9; }
  100% { transform: scale(2.6); opacity: 0; }
}

.hint { margin-top: 14px; font-size: 12.5px; color: var(--ink-faint); line-height: 1.5; }
[hidden] { display: none !important; }

/* ---------- Home ---------- */
.home .wrap { max-width: 560px; padding-top: 48px; }
.home-head h1 { font-family: var(--display); font-size: 44px; margin: 0; letter-spacing: 0.3px; }
.home-head p { margin: 6px 0 0; color: var(--ink-soft); font-size: 16px; line-height: 1.5; }
.modes { display: grid; gap: 12px; margin-top: 28px; }
.mode-card {
  display: flex; align-items: center; gap: 16px; padding: 18px 18px 18px 20px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04); color: var(--ink); text-decoration: none;
  transition: border-color 0.12s, transform 0.12s;
}
.mode-card:hover { border-color: var(--ink-soft); }
.mode-card:active { transform: scale(0.985); }
.mode-icon { font-size: 30px; flex-shrink: 0; }
.mode-text { flex: 1; display: grid; gap: 3px; line-height: 1.4; }
.mode-text b { font-family: var(--display); font-size: 22px; }
.mode-text span { color: var(--ink-soft); font-size: 14.5px; }
.mode-text small { color: var(--ink-faint); font-size: 13px; font-variant-numeric: tabular-nums; }
.mode-text small:empty { display: none; }
.mode-go { font-size: 28px; color: var(--ink-faint); flex-shrink: 0; }

/* ---------- In-app keyboard (iOS app only; see setupKeyboard) ----------
   Sized like Apple's iPhone letter keys: 42px keys, 6px apart, 12px between
   rows — but without the predictive bar or emoji/dictation row. */
.kb {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  padding: 8px calc(3px + env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) calc(3px + env(safe-area-inset-left));
  background: var(--kb-bg);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none;
}
.kb.off { opacity: 0.45; pointer-events: none; }
.kb-row { display: flex; justify-content: center; gap: 6px; }
.kb-row + .kb-row { margin-top: 12px; }
.kb-key {
  position: relative; flex: none; width: calc((100% - 54px) / 10); height: 42px; padding: 0;
  border: none; border-radius: 7px; background: var(--kb-key); color: var(--ink);
  box-shadow: 0 1px 0 rgba(0,0,0,0.3);
  font: 400 22px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.kb-key.down { background: var(--kb-special); }
.kb-key.special { background: var(--kb-special); font-size: 16px; }
.kb-key.special.down { background: var(--kb-key); }
.kb-side { flex: 1; margin: 0 8px; }
.kb-row .kb-side:first-child { margin-left: 0; }
.kb-key.kb-side { width: auto; margin-right: 0; font-size: 20px; }
.kb-key.space { flex: 1; width: auto; font-size: 16px; }
.kb-key.return { width: calc((100% - 54px) / 10 * 2.5 + 9px); }
/* the letter pops up above your finger, as on Apple's keyboard */
.kb-key.letter.down::after {
  content: attr(data-k); position: absolute; left: -5px; right: -5px; bottom: calc(100% + 4px);
  height: 52px; display: grid; place-items: center; border-radius: 9px;
  background: var(--kb-key); box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  font-size: 32px; text-transform: uppercase; pointer-events: none;
}
/* keep the page's end clear of the keyboard */
body.kb-open .wrap { padding-bottom: calc(var(--kb-h, 260px) + 16px); }
html { scroll-padding-bottom: var(--kb-h, 0px); }
@media (max-height: 500px) {  /* landscape phone */
  .kb { padding-top: 5px; }
  .kb-key { height: 32px; font-size: 19px; }
  .kb-row + .kb-row { margin-top: 6px; }
  .kb-key.letter.down::after { height: 40px; font-size: 26px; }
}
@media (min-width: 700px) and (min-height: 600px) {  /* iPad */
  .kb { padding: 8px calc(8px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left)); }
  .kb-row { gap: 10px; }
  .kb-key { width: calc((100% - 90px) / 10); height: 56px; font-size: 24px; }
  .kb-key.return { width: calc((100% - 90px) / 10 * 2 + 10px); }
}

/* Phones */
@media (max-width: 600px) {
  .wrap { padding: 10px 12px 28px; }
  header { padding-bottom: 10px; }
  header h1 { font-size: 19px; }
  header .total { font-size: 13px; }
  .letters { gap: 3px; margin-bottom: 12px; }
  .letters button { width: 25px; height: 25px; font-size: 11px; border-radius: 5px; }
  .letters-progress { font-size: 20px; }
  .letters-progress small { margin-left: 5px; letter-spacing: 1px; }
  .layout { gap: 14px; }
  .panel { padding: 14px; border-radius: 12px; }
  .letter-row { gap: 12px; }
  .big-letter { width: 64px; height: 64px; font-size: 44px; border-radius: 11px; }
  .counts { font-size: 13.5px; }
  .counts b { font-size: 14.5px; }
  .counts .nums span b { font-size: 19px; }
  .inputrow { margin-top: 14px; }
  #guess { font-size: 16px; padding: 12px 12px; } /* ≥16px avoids iOS zoom-on-focus */
  #micBtn { width: 48px; }
  #suggestions div { padding: 12px 14px; }
  .actions button { padding: 12px 10px; }
  .actions button.nav { width: 46px; }
  .mapbox { padding: 6px; border-radius: 12px; }
  #endscreen .score { font-size: 44px; }
  .clock { font-size: 26px; }
}
@media (max-width: 950px) {
  .streak-mode .mapbox { order: -1; }
}
