* {
  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. */
.pref[data-mark="correct"] {
  fill: var(--color-correct);
}

.pref[data-mark="wrong"] {
  fill: url(#mark-wrong-pattern);
}

.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;
}

.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: 2px;
  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;
}

.setting-error {
  margin: 4px 0 0;
  color: var(--color-selected);
  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;
  overflow-wrap: anywhere;
}

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

.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;
  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);
}

/* 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-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;
}
