* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: system-ui, sans-serif;
  overscroll-behavior: none;
}

#app {
  height: 100dvh;
  max-width: 100vw;
  overflow: hidden;
}

[hidden] {
  display: none !important;
}

/* One container per screen; only one is visible. */
[data-screen] {
  position: relative;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

/* Screens that host the map stage: stage above, info (lesson) below. */
[data-screen="lesson"],
[data-screen="quiz"] {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
}

[data-screen="home"],
[data-screen="quiz-choice"] {
  display: flex;
  flex-direction: column;
  justify-content: safe center;
  overflow-y: auto;
  gap: 16px;
  padding: 16px;
}

[data-quiz-choice-buttons] {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

[data-screen="home"] h1,
[data-screen="quiz-choice"] h1 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 1.75rem;
}

.home-action,
.home-control {
  touch-action: manipulation;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
}

.home-action {
  width: 100%;
  min-height: 72px;
  font-size: 1.25rem;
  font-weight: 600;
}

/* Top-left corner: the map's sea side, clear of #controls (bottom-right). */
.home-control {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 1;
  min-width: 48px;
  min-height: 48px;
  padding: 0 16px;
}

/* Settings and History are plain flow screens: the control sits in the flow. */
[data-screen="settings"] .home-control,
[data-screen="history"] .home-control,
[data-screen="quiz-choice"] .home-control {
  position: static;
  margin: 8px;
}

#stage {
  position: relative;
  min-height: 0;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: var(--color-sea);
  touch-action: none;
}

#map svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* Enlarged tap zone: 6px transparent stroke, in a layer below all fills so it
   only works over the sea (a prefecture's own fill always wins). */
.pref-tap {
  fill: none;
  stroke: transparent;
  stroke-width: 6px;
  vector-effect: non-scaling-stroke;
  pointer-events: stroke;
}

/* Fill carries the state and the hit test inside the shape. */
.pref {
  fill: var(--color-prefecture);
  stroke: none;
  pointer-events: fill;
}

/* Region colouring (opt-in via data-region); placed before the selection and
   mark rules so those win. */
.pref[data-region="hokkaido"] {
  fill: var(--color-region-hokkaido);
}

.pref[data-region="tohoku"] {
  fill: var(--color-region-tohoku);
}

.pref[data-region="kanto"] {
  fill: var(--color-region-kanto);
}

.pref[data-region="chubu"] {
  fill: var(--color-region-chubu);
}

.pref[data-region="kinki"] {
  fill: var(--color-region-kinki);
}

.pref[data-region="chugoku"] {
  fill: var(--color-region-chugoku);
}

.pref[data-region="shikoku"] {
  fill: var(--color-region-shikoku);
}

.pref[data-region="kyushu"] {
  fill: var(--color-region-kyushu);
}

.region-label-layer,
.region-label {
  pointer-events: none;
}

.region-label {
  fill: var(--color-region-label);
  stroke: var(--color-region-label-halo);
  stroke-width: 3px;
  stroke-linejoin: round;
  paint-order: stroke;
  vector-effect: non-scaling-stroke;
  font-weight: 700;
  user-select: none;
}

.pref[data-selected="true"] {
  fill: var(--color-selected);
}

/* Quiz marks: colour plus a second cue (stripes / outline style), FR-006.
   They fade in over --motion-colour-fade (0ms under reduced motion): the
   correct fill as a colour transition; the striped wrong fill and both mark
   outlines from 0.35 to full opacity (a paint server cannot be interpolated,
   research R2). Only the marked state carries the fade, so clearing the marks
   for the next question stays instant. Played once, never infinite. */
@keyframes mark-fill-fade {
  from {
    fill-opacity: 0.35;
  }

  to {
    fill-opacity: 1;
  }
}

@keyframes mark-stroke-fade {
  from {
    stroke-opacity: 0.35;
  }

  to {
    stroke-opacity: 1;
  }
}

.pref[data-mark="correct"] {
  fill: var(--color-correct);
  transition-property: fill;
  transition-duration: var(--motion-colour-fade, 0ms);
  transition-timing-function: ease-out;
}

.pref[data-mark="wrong"] {
  fill: url(#mark-wrong-pattern);
  animation-name: mark-fill-fade;
  animation-duration: var(--motion-colour-fade, 0ms);
  animation-timing-function: ease-out;
  animation-iteration-count: 1;
}

.mark-pattern-bg {
  fill: var(--color-prefecture);
}

.mark-pattern-stripe {
  fill: var(--color-wrong);
}

/* Visible borders, drawn above the fills and never hit-tested. */
.pref-outline {
  fill: none;
  stroke: var(--color-prefecture-stroke);
  stroke-width: 1px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.pref-outline.is-selected {
  stroke: var(--color-selected-stroke);
  stroke-width: 3px;
}

.pref-outline.is-correct {
  stroke: var(--color-correct-stroke);
  stroke-width: 3px;
}

.pref-outline.is-wrong {
  stroke: var(--color-wrong-stroke);
  stroke-width: 2px;
  stroke-dasharray: 5 3;
}

.pref-outline.is-correct,
.pref-outline.is-wrong {
  animation-name: mark-stroke-fade;
  animation-duration: var(--motion-colour-fade, 0ms);
  animation-timing-function: ease-out;
  animation-iteration-count: 1;
}

.capital-layer,
.capital-dot,
.capital-dot-halo {
  pointer-events: none;
}

.capital-dot-halo {
  fill: var(--color-capital-dot-halo);
  stroke: var(--color-capital-dot);
  stroke-width: 1.5px;
  vector-effect: non-scaling-stroke;
}

.capital-dot {
  fill: var(--color-capital-dot);
  stroke: var(--color-capital-dot-halo);
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
}

.inset-frame {
  fill: none;
  stroke: var(--color-inset-frame);
  stroke-width: 1.5px;
  stroke-dasharray: 4 3;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

#info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px 12px;
  overflow: hidden;
  background: var(--color-surface);
  line-height: 1.2;
}

/* Two lines reserved: the empty-state hint wraps, and the panel height must
   not change with the selection (it would resize the map). */
#info-prefecture {
  margin: 0;
  min-height: 2.4em;
  font-size: 1rem;
  font-weight: 600;
}

#info-capital,
#info-region {
  margin: 0;
  min-height: 1.2em;
  font-size: 0.9375rem;
}

/* One reserved line, never wrapping, so the panel height stays fixed. */
#info-region {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Floats over a sea corner of the map, outside #map (gestures, measuring). */
#controls {
  position: absolute;
  right: 8px;
  bottom: 8px;
  display: flex;
  gap: 8px;
}

#reset-view,
#regions-toggle {
  min-width: 48px;
  touch-action: manipulation;
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
}

/* Pressed: thicker border and a filled background, not hue alone. */
#regions-toggle[aria-pressed="true"] {
  border: 3px solid var(--color-text);
  background: var(--color-text);
  color: var(--color-surface);
  font-weight: 600;
}

/* Regions legend: one column in the right sea strip (72 px wide), over the
   map but outside #map, and it never takes a touch. */
#regions-legend {
  position: absolute;
  top: 160px;
  right: 0;
  width: 72px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 4px;
  border-radius: 6px;
  /* Translucent surface: the legend stays readable over the map (FR-031). */
  background: var(--color-legend-bg);
  pointer-events: none;
  font-size: 0.625rem;
  line-height: 1.1;
  color: var(--color-text);
}

#regions-legend[hidden] {
  display: none;
}

.legend-entry {
  display: flex;
  align-items: flex-start;
  gap: 3px;
}

.legend-swatch {
  flex: none;
  width: 10px;
  height: 10px;
  margin-top: 1px;
  border: 1px solid var(--color-text);
  box-sizing: border-box;
}

.legend-name {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-wrap: anywhere;
}

/* Settings: a scrolling flow screen. */
[data-screen="settings"] {
  overflow-y: auto;
}

.settings-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 8px 16px 24px;
}

.settings-body h2 {
  margin: 0;
  font-size: 1.25rem;
}

.setting label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 600;
}

.setting input,
.setting select {
  min-width: 48px;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-weight: 400;
}

/* Two-option toggle (src/ui/toggle.js): options side by side over a sliding
   red thumb; the selected option is also bold (non-colour cue). Labels may
   wrap at 360 px; both options keep the same height (grid row). */
.toggle {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-toggle-off);
}

.toggle-thumb {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 50%;
  background: var(--color-toggle-on);
  transition-property: transform;
  transition-duration: var(--motion-toggle, 0ms);
  transition-timing-function: ease-out;
  pointer-events: none;
}

.toggle.is-second .toggle-thumb {
  transform: translateX(100%);
}

.toggle-option {
  position: relative;
  min-width: 0;
  min-height: 48px;
  padding: 8px 12px;
  touch-action: manipulation;
  border: none;
  background: transparent;
  color: var(--color-toggle-off-text);
  font: inherit;
  font-weight: 400;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.toggle-option[aria-checked="true"] {
  color: var(--color-toggle-on-text);
  font-weight: 700;
}

/* Heading of a toggle setting (a toggle is not wrapped in a <label>). */
.setting-label {
  margin: 0 0 4px;
  font-weight: 600;
}

.setting-error {
  margin: 4px 0 0;
  color: var(--color-error);
  font-size: 0.875rem;
}

.settings-note,
.settings-source {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.875rem;
  line-height: 1.3;
}

/* History: a scrolling flow screen; the Accueil control stays reachable. */
[data-screen="history"] {
  overflow-y: auto;
}

[data-screen="history"] .home-control {
  position: sticky;
  top: 8px;
}

.history-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0 16px 24px;
}

.history-body h2 {
  margin: 0;
  font-size: 1.25rem;
}

.history-body h3 {
  margin: 0 0 6px;
  font-size: 1.0625rem;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.history-game,
.history-rank {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 2px 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--color-surface);
  overflow-wrap: anywhere;
}

.history-type,
.history-rate,
.history-duration {
  color: var(--color-text-muted);
}

.history-empty {
  margin: 0;
  color: var(--color-text-muted);
}

.history-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-confirm {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-confirm p,
.history-error {
  margin: 0;
}

.history-button {
  width: 100%;
  min-height: 48px;
  touch-action: manipulation;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
}

/* Quiz: bar above the map stage. */
[data-screen="quiz"] {
  grid-template-rows: auto minmax(0, 1fr);
}

.quiz-bar {
  /* The shared stage is prepended on every visit: keep the bar above it. */
  order: -1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px 8px;
  padding: 6px 8px 6px 12px;
  background: var(--color-surface);
}

.quiz-question {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 600;
  line-height: 1.2;
  /* Three lines reserved (the longest region name wraps to three at 360 px) so
     the map keeps the same size, hence the same start view, on every question. */
  min-height: 3.6em;
  overflow-wrap: anywhere;
}

.quiz-progress {
  grid-column: 1;
  grid-row: 2;
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

/* "Tap to continue" hint (next question on tap): same cell and line height as
   the progress, which it replaces during the reveal, so the bar keeps its
   height and the map is never covered nor resized. */
.quiz-continue-hint {
  grid-column: 1;
  grid-row: 2;
  margin: 0;
  min-width: 0;
  overflow: hidden;
  color: var(--color-text);
  font-size: 0.875rem;
  font-style: italic;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.quiz-bar.has-hint .quiz-progress {
  visibility: hidden;
}

.quiz-quit {
  grid-column: 2;
  grid-row: 1 / span 2;
  min-width: 48px;
  min-height: 48px;
  padding: 0 16px;
  touch-action: manipulation;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
}

.quiz-countdown {
  grid-column: 1 / -1;
  grid-row: 3;
  height: 10px;
  overflow: hidden;
  border-radius: 5px;
  background: var(--color-bg);
}

.quiz-countdown-fill {
  height: 100%;
  background: var(--color-countdown);
}

.quiz-countdown.is-low .quiz-countdown-fill {
  background: var(--color-countdown-low);
}

/* Choice quiz: four large answer buttons under the bar; the map is hidden. */
#app[data-quiz-map="off"] #stage {
  display: none;
}

.quiz-choices {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  gap: 12px;
  padding: 12px;
  overflow-y: auto;
}

.quiz-choice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  min-height: 56px;
  padding: 8px 16px;
  touch-action: manipulation;
  border: 2px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  text-align: left;
}

.quiz-choice-label {
  min-width: 0;
  overflow-wrap: anywhere;
}

.quiz-choice-mark {
  flex: none;
  font-weight: 600;
}

/* Marked choices fade to their colours (FR-008); unmarking stays instant. */
.quiz-choice[data-mark] {
  transition-property: background-color, border-color, color;
  transition-duration: var(--motion-colour-fade, 0ms);
  transition-timing-function: ease-out;
}

.quiz-choice[data-mark="correct"] {
  border-color: var(--color-correct-stroke);
  background: var(--color-correct);
  color: var(--color-surface);
}

.quiz-choice[data-mark="wrong"] {
  border-color: var(--color-wrong-stroke);
  background: var(--color-wrong);
  color: var(--color-surface);
}

/* Result: a centred, scrolling flow screen. */
[data-screen="result"] {
  display: flex;
  flex-direction: column;
  justify-content: safe center;
  overflow-y: auto;
  gap: 16px;
  padding: 16px;
  text-align: center;
}

[data-screen="result"] h2 {
  margin: 0;
  font-size: 1.5rem;
}

.result-score {
  margin: 0;
  font-size: 2.5rem;
  font-weight: 700;
}

.result-duration {
  margin: 0;
  color: var(--color-text-muted);
}

.result-best,
.result-compare {
  margin: 0;
  color: var(--color-text-muted);
}

.result-new-best {
  color: var(--color-correct-stroke);
  font-weight: 700;
  white-space: nowrap;
}

.result-message {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}

.result-action {
  width: 100%;
  min-height: 56px;
  touch-action: manipulation;
  border: 1px solid var(--color-text-muted);
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-size: 1.125rem;
  font-weight: 600;
}

/* Landscape: the info panel becomes a right-hand column. */
@media (orientation: landscape) {
  [data-screen="lesson"] {
    grid-template-columns: minmax(0, 1fr) 230px;
    grid-template-rows: minmax(0, 1fr);
  }

  [data-screen="home"] {
    overflow-y: auto;
  }

  /* The map is narrow here: stack the buttons to keep clear of the inset. */
  #controls {
    flex-direction: column;
    align-items: flex-end;
  }

  #info {
    min-height: 0;
    overflow-y: auto;
  }

  /* Landscape: top of the right edge, above the stacked buttons. */
  #regions-legend {
    top: 8px;
  }
}

:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}
