/* Repaso: blues + greens, mobile first */

:root {
  --blue-900: #0E2A47;
  --blue-700: #1F5FA8;
  --blue-600: #2A6FBF;
  --blue-200: #BFD7F0;
  --blue-100: #DCEBFA;
  --blue-50:  #EEF5FC;
  --green-700: #187A50;
  --green-500: #22A06B;
  --green-100: #DDF3E8;
  --amber: #E8A317;

  --ink: #12263A;
  --muted: #4D6378;
  --bg: var(--blue-50);
  --card: #FFFFFF;
  --line: #CFE0F0;

  --radius: 24px;
  --font: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 3px solid var(--blue-600); outline-offset: 3px; }

svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

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

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  min-height: 52px; padding: 0 1.4rem;
  border: 0; border-radius: 999px;
  font-weight: 700; font-size: 1.05rem;
  transition: transform .12s ease, background-color .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--blue-700); color: #fff; }
.btn-green { background: var(--green-700); color: #fff; }
.btn-light { background: #fff; color: var(--blue-700); }
.btn-ghost { background: transparent; color: var(--blue-700); }
.btn-outline { background: #fff; color: var(--blue-900); box-shadow: inset 0 0 0 2px var(--line); }
.btn-big { min-height: 60px; padding: 0 2.4rem; font-size: 1.25rem; box-shadow: 0 10px 30px rgba(9, 35, 64, .25); }

.icon-btn {
  position: relative;
  display: inline-grid; place-items: center;
  width: 48px; height: 48px;
  border: 0; border-radius: 16px;
  background: #fff; color: var(--blue-700);
  box-shadow: 0 1px 0 var(--line), 0 4px 14px rgba(31, 95, 168, .08);
  transition: transform .12s ease;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn-primary { background: var(--blue-700); color: #fff; }
.icon-btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.icon-btn.round { width: 56px; height: 56px; border-radius: 50%; }

.bm-btn.has-mark svg path { fill: var(--amber); stroke: #B57A00; }

.link-btn {
  display: inline-flex; align-items: center; gap: .4rem;
  min-height: 44px; padding: 0 .9rem;
  border: 0; border-radius: 999px; background: transparent;
  color: var(--muted); font-size: .95rem;
}
.link-btn svg { width: 18px; height: 18px; }
.link-btn[aria-expanded="true"] { color: var(--blue-700); background: var(--blue-100); }

/* ---------- Welcome ---------- */

.welcome {
  min-height: 100dvh;
  display: grid; place-items: center;
  padding: calc(2rem + var(--safe-top)) 1.5rem calc(2rem + var(--safe-bottom));
  background:
    radial-gradient(120% 80% at 10% 0%, rgba(255,255,255,.14), transparent 60%),
    linear-gradient(160deg, var(--blue-700) 0%, #1B6C8E 55%, var(--green-700) 100%);
  color: #fff; text-align: center;
}
.welcome-inner { max-width: 26rem; }
.welcome-mark {
  width: 88px; height: 88px; margin: 0 auto 1.5rem;
  display: grid; place-items: center;
  border-radius: 28px; background: rgba(255,255,255,.16);
  font-size: 3.2rem; font-weight: 700; line-height: 1;
}
.welcome h1 { margin: 0 0 2.25rem; font-size: clamp(2rem, 9vw, 2.75rem); line-height: 1.1; }
.key-form { display: flex; flex-direction: column; align-items: center; gap: .6rem; }
.key-form label { font-weight: 700; font-size: 1.05rem; }
.key-form input {
  width: min(100%, 18rem); min-height: 52px; padding: 0 1rem;
  border: 2px solid rgba(255,255,255,.5); border-radius: 14px;
  background: rgba(255,255,255,.95); color: var(--ink);
  font: inherit; font-size: 1.15rem; text-align: center;
}
.key-form input:focus { outline: 3px solid #fff; outline-offset: 2px; }
.key-error { min-height: 1.4em; margin: 0 0 .6rem; color: #FFE1DC; font-weight: 700; }

/* ---------- App layout ---------- */

.app {
  height: 100dvh;
  overflow: hidden; /* the card flying off must not cause sideways scrolling */
  display: flex; flex-direction: column;
  max-width: 560px; margin: 0 auto;
  padding: calc(.75rem + var(--safe-top)) .75rem calc(1rem + var(--safe-bottom));
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: .75rem; }
.topbar-actions { display: flex; gap: .5rem; }

.tally {
  display: inline-flex; align-items: center; gap: .45rem;
  min-height: 48px; padding: 0 1rem 0 .6rem;
  border-radius: 999px; background: var(--green-100); color: var(--green-700);
}
.tally-icon { width: 28px; height: 28px; }
.tally-icon circle { fill: var(--green-500); stroke: none; }
.tally-icon path { stroke: #fff; stroke-width: 2.6; }
.tally-count { font-weight: 700; font-size: 1.3rem; font-variant-numeric: tabular-nums; }
.tally-total { color: var(--muted); font-size: .9rem; }
.tally.bump .tally-icon { animation: bump .35s ease; }

.stage {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; justify-content: center;
  padding: 1rem 0 .25rem;
}

/* ---------- Card stack ---------- */

.deck-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: .4rem; }
.side-btn { width: 44px; height: 44px; margin-bottom: 30px; /* centre on the card, not the stack below it */ }
.side-btn svg { width: 22px; height: 22px; }

.deck { position: relative; perspective: 1400px; padding-bottom: 30px; }
.deck-layer {
  position: absolute; left: 0; right: 0; top: 0; bottom: 30px;
  border-radius: var(--radius);
  background: #fff;
  box-shadow: 0 1px 0 var(--line), 0 8px 20px rgba(31, 95, 168, .10);
  transform-origin: 50% 100%;
}
.deck-layer-1 { transform: translateY(10px) scale(.96); background: #F6FAFE; }
.deck-layer-2 { transform: translateY(20px) scale(.92); background: #EDF4FB; }
.deck-layer-3 { transform: translateY(30px) scale(.88); background: #E4EEF9; }

/* ---------- Card ---------- */

.card {
  position: relative;
  display: flex; flex-direction: column;
  z-index: 1;
  min-height: clamp(260px, 52dvh, 420px);
  padding: .75rem .75rem 1.25rem;
  border-radius: var(--radius);
  background: var(--card);
  border: 2px solid transparent;
  box-shadow: 0 2px 0 var(--line), 0 18px 40px rgba(31, 95, 168, .12);
  overflow: hidden;
  cursor: pointer;
  container-type: inline-size; /* lets the word size itself to the card width */
  touch-action: pan-y;
  transition: border-color .2s ease, background-color .2s ease;
}
.card.is-known { border-color: var(--green-500); background: linear-gradient(180deg, var(--green-100) 0, #fff 38%); }

/* The next card rises out of the stack… */
.card.from-stack { animation: from-stack .42s cubic-bezier(.2, .8, .2, 1); }
/* …going back, the previous card slides in from the left… */
.card.from-left { animation: from-left .45s cubic-bezier(.2, .8, .2, 1); }
/* …and the card she just saw is flicked off to the side. */
.card-ghost {
  position: absolute; left: 0; right: 0; top: 0; z-index: 2;
  pointer-events: none;
  transform-origin: 50% 120%;
  animation: fly-left .5s cubic-bezier(.5, 0, .75, .3) forwards;
}

.card-top { display: flex; align-items: center; justify-content: space-between; }

.card-btn {
  display: grid; place-items: center;
  width: 52px; height: 52px;
  border: 0; border-radius: 50%; background: transparent;
  transition: transform .12s ease, color .15s ease;
}
.card-btn:active { transform: scale(.9); }
.card-btn svg { width: 30px; height: 30px; }

.bookmark { color: var(--blue-200); }
.bookmark svg path { stroke: var(--blue-700); }
.bookmark[aria-pressed="true"] svg path { fill: var(--amber); stroke: #B57A00; }

.tick { color: var(--muted); }
.tick svg circle { stroke: #9BB3C9; }
.tick[aria-pressed="true"] svg circle { fill: var(--green-500); stroke: var(--green-500); }
.tick[aria-pressed="true"] svg path { stroke: #fff; stroke-width: 2.6; }
.tick.pop { animation: bump .35s ease; }

.word {
  flex: 1;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; align-content: center;
  gap: 0 .3em;
  margin: .5rem .5rem;
  font-size: clamp(1.5rem, 14cqi, 3.2rem);
  font-weight: 700; line-height: 1.12;
  text-align: center;
  overflow-wrap: anywhere; hyphens: auto;
}
.card.is-phrase .word { font-size: clamp(1.3rem, 9.5cqi, 2.2rem); line-height: 1.22; }
.word .article { font-weight: 400; color: var(--muted); }


.timer { position: absolute; left: 0; right: 0; bottom: 0; height: 6px; background: var(--blue-100); }
.timer-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--blue-600), var(--green-500));
  transform: scaleX(0); transform-origin: left center;
}
.timer-fill.run { animation: fill var(--interval, 2000ms) linear forwards; }
.app.paused .timer-fill.run { animation-play-state: paused; }
/* Invisible twin of the timer that ends 1 s early: that's when the tick turns green. */
.tick-cue { position: absolute; width: 0; height: 0; }
.tick-cue.run { animation: cue var(--tick-at, 3000ms) linear forwards; }
.app.paused .tick-cue.run { animation-play-state: paused; }

/* ---------- Translation ---------- */

.translate { display: flex; flex-direction: column; align-items: center; padding-top: .6rem; min-height: 6.2rem; }
.translation {
  margin: .35rem 0 0; padding: 0 1rem;
  font-size: 1.2rem; text-align: center; color: var(--blue-900);
  visibility: hidden;
}
.translation.show { visibility: visible; animation: fade .2s ease; }
.note {
  margin: .25rem 0 0; padding: 0 1rem;
  font-size: .95rem; font-style: italic; text-align: center; color: var(--muted);
  visibility: hidden;
}
.note:empty { display: none; }
.note.show { visibility: visible; animation: fade .2s ease; }

/* ---------- Controls ---------- */

.controls { display: flex; align-items: center; justify-content: center; gap: .6rem; padding-top: .5rem; }
.controls .icon-btn.round { width: 52px; height: 52px; flex: none; }

/* Seconds per card: a pill that opens the phone's own picker */
.speed {
  position: relative; flex: none;
  display: inline-flex; align-items: center; gap: .3rem;
  height: 52px; padding: 0 .75rem 0 .85rem;
  border-radius: 999px; background: #fff; color: var(--blue-700);
  box-shadow: 0 1px 0 var(--line), 0 4px 14px rgba(31, 95, 168, .08);
  cursor: pointer;
}
.speed svg { width: 20px; height: 20px; }
.speed .chev { width: 18px; height: 18px; color: var(--muted); }
#speed-value { font-weight: 700; font-size: 1.05rem; color: var(--ink); white-space: nowrap; }
.speed select {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; cursor: pointer; font-size: 16px; /* 16px stops iOS zooming in */
}
.speed:focus-within { outline: 3px solid var(--blue-600); outline-offset: 3px; }
.play { min-height: 56px; padding: 0 1.15rem; font-size: 1.05rem; flex: none; }
.play .icon-play { display: none; }
.app.paused .play { background: var(--green-700); }
.app.paused .play .icon-play { display: block; fill: currentColor; }
.app.paused .play .icon-pause { display: none; }

/* ---------- All words (grid page) ---------- */

.grid-view {
  max-width: 900px; margin: 0 auto;
  padding: 0 .75rem calc(1.5rem + var(--safe-bottom));
}
.grid-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; gap: .75rem;
  padding: calc(.75rem + var(--safe-top)) 0 .75rem;
  background: var(--bg);
}
.grid-title h1 { margin: 0; font-size: 1.3rem; line-height: 1.2; }
.grid-title p { margin: 0; color: var(--muted); font-size: .9rem; }
.grid-title .done { color: var(--green-700); font-weight: 700; }

.grid {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: .5rem;
}
.grid li { content-visibility: auto; contain-intrinsic-size: auto 88px; }
.tile {
  position: relative;
  display: grid; place-items: center;
  width: 100%; height: 100%; min-height: 88px;
  padding: 1.4rem .5rem .8rem;
  border: 2px solid transparent; border-radius: 16px;
  background: #fff;
  box-shadow: 0 1px 0 var(--line), 0 4px 12px rgba(31, 95, 168, .06);
  font-size: .95rem; font-weight: 700; line-height: 1.2; text-align: center;
  overflow-wrap: anywhere; hyphens: auto;
}
.tile:active { transform: scale(.97); }
.tile .t-tick, .tile .t-mark { position: absolute; top: 6px; width: 20px; height: 20px; }
.tile .t-tick { left: 6px; display: none; }
.tile .t-tick circle { fill: var(--green-500); stroke: none; }
.tile .t-tick path { stroke: #fff; stroke-width: 2.6; }
.tile .t-mark { right: 6px; display: none; }
.tile .t-mark path { fill: var(--amber); stroke: #B57A00; }
.tile.is-done { background: var(--green-100); border-color: rgba(34, 160, 107, .45); }
.tile.is-done .t-tick { display: block; }
.tile.is-marked .t-mark { display: block; }
.tile.is-current { border-color: var(--blue-700); box-shadow: 0 0 0 3px var(--blue-200); }

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

.modal {
  width: min(100% - 1.5rem, 480px);
  max-height: calc(100dvh - 2rem);
  padding: 1rem 1.25rem 1.25rem;
  border: 0; border-radius: var(--radius);
  background: #fff; color: var(--ink);
  box-shadow: 0 24px 60px rgba(9, 35, 64, .3);
}
.modal::backdrop { background: rgba(14, 42, 71, .45); backdrop-filter: blur(2px); }
.modal[open] { animation: rise .22s ease-out; }
.modal-top { margin: calc(.75rem + var(--safe-top)) auto auto; }
.modal-bottom {
  margin: auto auto 0; width: 100%; max-width: 560px;
  border-radius: var(--radius) var(--radius) 0 0;
  padding-bottom: calc(1.25rem + var(--safe-bottom));
}

.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .5rem; }
.modal h2 { margin: 0; font-size: 1.35rem; }

.modal label { display: block; margin: .9rem 0 .35rem; font-weight: 700; font-size: .95rem; }
.modal input {
  width: 100%; min-height: 54px; padding: 0 1rem;
  border: 2px solid var(--line); border-radius: 14px;
  font: inherit; font-size: 1.2rem; color: var(--ink); background: var(--blue-50);
}
.modal input:focus { outline: none; border-color: var(--blue-600); background: #fff; }

.accents { display: grid; grid-template-columns: repeat(5, 1fr); gap: .45rem; margin-top: .6rem; }
.accents button {
  min-height: 48px;
  border: 0; border-radius: 12px;
  background: var(--blue-100); color: var(--blue-900);
  font-size: 1.35rem; font-weight: 700;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.accents button:active { background: var(--blue-200); }
.accents [data-shift] { background: var(--green-100); color: var(--green-700); }
.accents [data-shift][aria-pressed="true"] { background: var(--green-700); color: #fff; }

.restart-options { display: flex; flex-direction: column; gap: .6rem; margin-top: .75rem; }
.restart-options .btn { width: 100%; }

.form-error { min-height: 1.4em; margin: .6rem 0 0; color: #B3261E; font-size: .95rem; }

.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .5rem; }


/* ---------- Toast ---------- */

.toast {
  position: fixed; left: 50%; top: calc(4.5rem + var(--safe-top));
  max-width: calc(100% - 2rem); white-space: nowrap;
  padding: .7rem 1.1rem;
  border-radius: 999px; background: var(--blue-900); color: #fff;
  font-size: .95rem; text-align: center;
  transform: translate(-50%, -.5rem); opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Motion ---------- */

@keyframes fill  { to { transform: scaleX(1); } }
@keyframes from-stack {
  from { transform: translateY(10px) scale(.96); box-shadow: 0 1px 0 var(--line); }
  to   { transform: none; }
}
@keyframes from-left {
  from { transform: translateX(-115%) rotate(-10deg); }
  to   { transform: none; }
}
@keyframes fly-left {
  0%   { transform: none; opacity: 1; }
  80%  { opacity: 1; }
  100% { transform: translateX(-135%) translateY(24px) rotate(-16deg) rotateY(22deg); opacity: 0; }
}
@keyframes fade  { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise  { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes cue   { to { opacity: 1; } }
@keyframes bump  { 50% { transform: scale(1.18); } }

@media (prefers-reduced-motion: reduce) {
  .card.from-stack, .card.from-left, .modal[open], .translation.show, .tick.pop, .tally.bump .tally-icon { animation: none; }
  .toast { transition: none; }
  .card-ghost { display: none; }
}

/* Narrow phones (iPhone SE / mini): slimmer arrows so the card keeps its width */
@media (max-width: 360px) {
  .deck-row { gap: .2rem; }
  .side-btn { width: 38px; height: 38px; }
}

/* Smallest phones: the play button shows just its icon so the row fits */
@media (max-width: 360px) {
  .play { width: 56px; padding: 0; }
  .play #play-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
}

/* Very short screens (e.g. iPhone SE in landscape-ish heights) */
@media (max-height: 600px) {
  .card { min-height: 230px; }
  .translate { min-height: 5rem; }
  .play { min-height: 52px; }
}
