:root {
  --bg: #1f2430;
  --panel: #f7f5ef;
  --text: #f2f2f2;
  --text-dark: #1f2430;
  --muted: #b9bfcc;
  --accent: #3d7eff;
  /* Highlight: steady, high-contrast yellow. It moves between the sides but
     never flashes, which keeps it safe for photosensitive users. */
  --highlight: #ffd400;
  --selected: #2fbf71;
  --radius: 1.5rem;
  --gap: clamp(1rem, 3vw, 3rem);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  /* Georgian fonts are listed explicitly because some system UI fonts lack
     Georgian letters: Noto (Linux, Android), Sylfaen (Windows), macOS/iOS fonts. */
  font-family:
    system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Georgian", "Noto Sans", Sylfaen,
    "BPG Arial", "Helvetica Neue", sans-serif;
  font-size: 1.125rem;
  line-height: 1.4;
}

/* ---- Start screen --------------------------------------------------------- */

.start-screen {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 3rem 1rem;
}

.start-screen h1 {
  margin: 0;
  font-size: clamp(2rem, 6vw, 3.5rem);
}

/* ---- Intro screen ---------------------------------------------------------- */

.intro-screen {
  position: relative;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding: 3rem 1rem;
}

.back-button {
  position: absolute;
  top: 1rem;
  left: 1rem;
  font: inherit;
  padding: 0.5rem 1rem;
  border: 2px solid #3a4152;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.back-button:focus-visible {
  outline: 0.2rem solid var(--accent);
  outline-offset: 0.2rem;
}

.intro-icon {
  width: clamp(6rem, 20vw, 9rem);
  margin-top: 1.5rem;
}

.intro-name {
  margin: 0;
  font-size: clamp(2rem, 6vw, 3.5rem);
  text-align: center;
}

.start-button {
  font: inherit;
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 700;
  padding: 1.25rem 4rem;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  cursor: pointer;
}

.start-button:focus-visible {
  outline: 0.2rem solid white;
  outline-offset: 0.3rem;
}

.settings summary {
  cursor: pointer;
  font-weight: 600;
  padding: 0.25rem 0;
}

/* ---- Game menu ------------------------------------------------------------ */

.game-menu {
  width: min(56rem, 100%);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.category-name {
  margin: 0.5rem 0 0;
  font-size: clamp(1.25rem, 3vw, 1.6rem);
  color: var(--muted);
}

.category-games {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
  gap: 1rem;
}

.game-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  padding: 1.25rem 0.75rem;
  border: 0.35rem solid transparent;
  border-radius: 1.5rem;
  background: var(--panel);
  color: var(--text-dark);
  font: inherit;
  text-align: center;
  cursor: pointer;
  transition: transform 150ms ease, border-color 150ms ease;
}

.game-card:hover {
  transform: translateY(-2px);
}

/* Keyboard focus is a thin blue ring; yellow is reserved for the scan. */
.game-card:focus-visible {
  outline: 0.2rem solid var(--accent);
  outline-offset: 0.2rem;
}

.game-card img {
  width: 4.5rem;
  aspect-ratio: 1;
}

.game-name {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.intro-category {
  margin: 0;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.9rem;
}

.hint {
  margin: 0;
  color: var(--muted);
  text-align: center;
}

.language-switch {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
}

.language-switch button {
  font: inherit;
  padding: 0.5rem 1.25rem;
  border: 2px solid #3a4152;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.language-switch button[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

.language-switch button:focus-visible,
.link-button:focus-visible {
  outline: 0.2rem solid var(--accent);
  outline-offset: 0.2rem;
}

.voice-status {
  margin: 0;
  max-width: 40rem;
  text-align: center;
  color: var(--muted);
}

.link-button {
  font: inherit;
  padding: 0;
  border: none;
  background: none;
  color: var(--highlight);
  text-decoration: underline;
  cursor: pointer;
}

.settings {
  width: min(40rem, 100%);
  background: #2a3040;
  border-radius: 1rem;
  padding: 0.75rem 1.25rem;
}

.settings-form fieldset {
  border: none;
  margin: 1rem 0 0;
  padding: 0;
  display: grid;
  gap: 0.75rem;
}

.settings-form legend {
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.85rem;
}

.settings-form label {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.settings-form .field {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.settings-form input[type="number"] {
  width: 5.5rem;
}

.settings-form input,
.settings-form select {
  font: inherit;
  padding: 0.3rem 0.5rem;
  max-width: 100%;
}

.settings-form input[type="checkbox"] {
  width: 1.4rem;
  height: 1.4rem;
}

.note {
  margin: 0;
  color: var(--highlight);
  font-size: 0.95rem;
}

.note-muted {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---- Game screen ---------------------------------------------------------- */

.game-screen {
  position: fixed;
  inset: 0;
  padding: 0.5rem var(--gap) var(--gap);
  display: flex;
  flex-direction: column;
  /* Room under Back for the highlight/selected frames, which spill up to
     0.75rem outside a picture. */
  gap: max(1rem, calc(var(--gap) * 0.5));
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.game-root {
  position: relative; /* game overlays (e.g. "Paused") cover the pictures, not Back */
  flex: 1;
  min-height: 0;
}

/* Top bar: Back on the left, the game's own buttons on the right. Its own
   row above the game, so nothing here covers a picture. */
.game-bar {
  position: relative;
  z-index: 2; /* always tappable, above anything a game draws */
  order: -1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.game-controls {
  display: flex;
  gap: 0.5rem;
}

.game-screen .back-button {
  position: static; /* not the intro screen's absolute offsets */
  min-height: 2.75rem;
  padding: 0.4rem 1.25rem;
  font-size: 1.1rem;
}

.page-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.75rem;
  border: 2px solid #3a4152;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.page-button svg {
  width: 1.1rem;
  height: 1.1rem;
  fill: currentColor;
}

.page-button:focus-visible {
  outline: 0.2rem solid var(--accent);
  outline-offset: 0.2rem;
}

.choices {
  height: 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
}

/* Two pictures: stack them on narrow, portrait screens (phones). */
@media (max-aspect-ratio: 3/4) {
  .choices {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
}

/* Four pictures: always a 2×2 grid, scanned left to right, top to bottom. */
.choices[data-count="4"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: calc(var(--gap) * 0.75);
}

.choices[data-count="4"] .choice {
  gap: 0.5rem;
  padding: clamp(0.5rem, 2vmin, 1.5rem);
}

.choices[data-count="4"] .choice figcaption {
  font-size: clamp(0.8rem, 10cqi, 2.75rem);
}

.choice {
  container-type: inline-size; /* lets the caption size itself with cqi */
  margin: 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: clamp(1rem, 3vw, 2.5rem);
  background: var(--panel);
  color: var(--text-dark);
  border-radius: var(--radius);
  /* The border is always there, just transparent, so highlighting doesn't
     shift the layout. */
  border: 0.9rem solid transparent;
  transition:
    border-color 150ms ease,
    opacity 200ms ease,
    transform 200ms ease;
}

.choice img {
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  pointer-events: none;
}

.choice figcaption {
  max-width: 100%;
  /* Sized by the card's width (cqi), so long Georgian words like
     ფორთოხალი fit on one line even in a small 2×2 cell on a phone. */
  font-size: clamp(0.8rem, 10cqi, 3.5rem);
  font-weight: 700;
  line-height: 1.15;
  text-align: center;
  overflow-wrap: anywhere; /* last resort: never widen the grid cell */
}

/* Set by the game while the caption fits (after shrinking) on one line. */
.choice figcaption.one-line {
  white-space: nowrap;
}

.choice.highlighted {
  border-color: var(--highlight);
  box-shadow: 0 0 0 0.5rem rgb(255 212 0 / 0.35);
}

.choice.selected {
  border-color: var(--selected);
  box-shadow: 0 0 0 0.75rem rgb(47 191 113 / 0.4);
  transform: scale(1.03);
}

.choice.not-selected {
  opacity: 0.35;
}

/* Already chosen on this page: stays in place, dimmed, with a check mark,
   and the scanner skips it. */
.choice.done {
  position: relative;
}

/* Dim the contents, not the card, so the check mark stays bright. */
.choice.done > * {
  opacity: 0.35;
}

.choice.done::after {
  content: "✓";
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  display: grid;
  place-items: center;
  width: clamp(2rem, 14cqi, 4rem);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--selected);
  color: white;
  font-size: clamp(1.25rem, 9cqi, 2.5rem);
  font-weight: 700;
}

/* Page number for the caregiver: small and muted, tucked into the bottom gap
   so it doesn't compete with the pictures. */
.page-indicator {
  position: absolute;
  right: var(--gap);
  bottom: 0;
  height: var(--gap);
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: clamp(0.75rem, 1.5vmin, 1rem);
  pointer-events: none;
}

.pause-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgb(31 36 48 / 0.8);
  text-align: center;
}

.pause-overlay p {
  margin: 0;
  font-size: clamp(2rem, 7vw, 4rem);
  font-weight: 700;
}

.pause-overlay .sub {
  font-size: 0.5em;
  font-weight: 400;
  color: var(--muted);
}

@media (prefers-reduced-motion: reduce) {
  .choice {
    transition: none;
  }

  .choice.selected {
    transform: none;
  }
}

[hidden] {
  display: none !important;
}
