:root {
  --bg: #1c1c1e;
  --panel: #2a2a2d;
  --panel-2: #353539;
  --line: #4a4a4f;
  --ink: #f2f2f2;
  --ink-sub: #a8a8ad;
  --stage: #121214;
  --reveal-ms: 1100ms;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 480px;
  min-height: 100%;
  margin: 0 auto;
  padding-block: 16px 20px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
}

.app { position: relative; }
.corner-btn {
  position: absolute;
  top: 12px;
  z-index: 2;
  font: inherit;
  font-size: 0.85rem;
  color: var(--ink-sub);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 36px;
  cursor: pointer;
}
.sound-toggle { right: max(16px, env(safe-area-inset-right, 0px)); }
.home-btn { left: max(16px, env(safe-area-inset-left, 0px)); }
.sound-toggle[aria-pressed="true"] { color: var(--ink); }
.home-btn.is-armed { color: var(--bg); background: var(--ink); border-color: var(--ink); }
.corner-btn:focus-visible { outline: 3px solid #8ab4ff; outline-offset: 2px; }
/* 角のボタンと重ならないよう、出題と結果の画面は上をあける */
.quiz-head { padding-top: 44px; }
#result { padding-top: 44px; }
#title { padding-top: 44px; }

.screen { display: none; flex: 1; flex-direction: column; gap: 14px; }
.screen.is-active { display: flex; }

/* ---------- 国旗 ---------- */
/* 3:2 の枠の中に、国旗を本来の縦横比で収める */
.flag-box {
  container-type: size;
  width: 100%;
  aspect-ratio: 3 / 2;
  display: grid;
  place-items: center;
}
.flag {
  position: relative;
  width: min(100cqw, calc(100cqh * var(--ar, 1.5)));
  aspect-ratio: var(--ar, 1.5);
  outline: 1px solid rgba(255, 255, 255, 0.3);
}
.flag img {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: fill;
  display: block;
}
.flag-gray { filter: grayscale(1); }
.flag-color { clip-path: inset(0 0 100% 0); }

/* 色の広がり方：横縞＝上から、縦縞＝左から、その他＝中心から */
.flag.reveal-horizontal .flag-color { animation: wipe-down var(--reveal-ms) ease-in-out forwards; }
.flag.reveal-vertical   .flag-color { animation: wipe-right var(--reveal-ms) ease-in-out forwards; }
.flag.reveal-center     .flag-color { animation: grow-circle var(--reveal-ms) ease-in-out forwards; }
.flag.is-colored .flag-color { clip-path: none; animation: none; }

@keyframes wipe-down  { from { clip-path: inset(0 0 100% 0); } to { clip-path: inset(0 0 0 0); } }
@keyframes wipe-right { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
@keyframes grow-circle { from { clip-path: circle(0% at 50% 50%); } to { clip-path: circle(71% at 50% 50%); } }

@media (prefers-reduced-motion: reduce) {
  :root { --reveal-ms: 300ms; }
  .flag.reveal-horizontal .flag-color,
  .flag.reveal-vertical .flag-color,
  .flag.reveal-center .flag-color { clip-path: none; animation: fade-in var(--reveal-ms) ease-out forwards; }
  @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
}

/* ---------- タイトル ---------- */
#title { justify-content: center; text-align: center; }
.title-flag { width: 72%; margin: 0 auto 12px; }
.subtitle { margin: 0; font-size: 0.95rem; color: var(--ink-sub); letter-spacing: 0.08em; }
.logo-group { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.logo-sub {
  margin: 0;
  padding: 3px 14px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--ink);
}
.logo { margin: 0; font-size: clamp(2rem, 11vw, 3rem); line-height: 1.15; letter-spacing: 0.06em; font-weight: 800; text-wrap: balance; }
:root[lang="en"] .logo { font-size: min(8vw, 2.5rem); letter-spacing: 0.01em; white-space: nowrap; }
.lead { margin: 14px 0 20px; color: var(--ink-sub); line-height: 1.8; }

/* ---------- ボタン ---------- */
.btn {
  font: inherit;
  border: none;
  border-radius: 14px;
  min-height: 56px;
  padding: 12px 16px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn-main { background: var(--ink); color: var(--bg); font-size: 1.15rem; font-weight: 700; }
.btn-main:active { transform: scale(0.98); }
.btn-sub { background: transparent; color: var(--ink-sub); border: 1px solid var(--line); }
.modes { display: flex; flex-direction: column; gap: 10px; }
.back-link { align-self: center; font-size: 0.85rem; color: var(--ink-sub); text-underline-offset: 3px; padding: 4px 8px; }
.back-link:hover, .back-link:focus-visible { color: var(--ink); }
.mode-btn { display: flex; flex-direction: column; align-items: center; gap: 2px; line-height: 1.3; }
.mode-btn small { font-size: 0.8rem; font-weight: 400; opacity: 0.75; }
.btn-sub.mode-btn { color: var(--ink); font-size: 1.05rem; font-weight: 700; border-width: 2px; }
.btn:focus-visible { outline: 3px solid #8ab4ff; outline-offset: 2px; }

/* ---------- 出題 ---------- */
.quiz-head { display: flex; justify-content: space-between; align-items: baseline; color: var(--ink-sub); }
.progress { font-size: 1.05rem; font-weight: 700; color: var(--ink); }
.score-mini { font-size: 0.95rem; }

.flag-stage {
  background: var(--stage);
  border-radius: 16px;
  padding: 18px;
}

.verdict { min-height: 3.6em; text-align: center; }
.verdict .mark { font-size: 1.5rem; font-weight: 800; }
.verdict .official { display: block; margin-top: 2px; color: var(--ink-sub); font-size: 0.9rem; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice {
  background: var(--panel);
  color: var(--ink);
  border: 2px solid var(--line);
  font-size: 1.05rem;
  font-weight: 700;
  position: relative;
}
.choice:not(:disabled):active { background: var(--panel-2); }
/* 長い国名（例：セントクリストファー・ネービス）はボタンに収まるよう小さめに */
.choice.is-long { font-size: 0.85rem; line-height: 1.3; padding: 8px 10px; }
.choice:disabled { cursor: default; opacity: 0.45; }
.choice.is-answer { opacity: 1; border-color: var(--ink); background: var(--panel-2); }
.choice.is-wrong { opacity: 1; border-style: dashed; }
.choice .tag { display: block; font-size: 0.8rem; font-weight: 400; color: var(--ink-sub); }

.compare { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.compare figure { margin: 0; background: var(--panel); border-radius: 12px; padding: 10px; text-align: center; }
.compare figcaption { margin-top: 6px; font-size: 0.85rem; color: var(--ink-sub); }
.compare figcaption b { display: block; color: var(--ink); font-size: 0.95rem; }

#next { margin-top: auto; }

/* 背の低いスマホでも、外れたときの画面が1画面に収まるように詰める */
@media (max-height: 740px) {
  .screen { gap: 10px; }
  .flag-stage { padding: 10px; }
  .verdict { min-height: 3.2em; }
  .verdict .mark { font-size: 1.3rem; }
  .compare figure { padding: 8px; }
  .compare figcaption { line-height: 1.3; }
  .compare figcaption b { font-size: 0.88rem; }
  .verdict .official { line-height: 1.3; font-size: 0.8rem; }
  .verdict { min-height: 2.9em; }
  .flag-stage .flag-box { width: 94%; margin: 0 auto; }
  .title-flag { width: 58%; margin-bottom: 4px; }
  .lead { margin: 8px 0 12px; line-height: 1.6; }
  .btn { min-height: 52px; }
}

/* ---------- 結果 ---------- */
#result { text-align: center; }
.final-score { margin: 0; font-size: 3rem; font-weight: 800; }
.final-score small { font-size: 1rem; font-weight: 400; color: var(--ink-sub); margin-left: 4px; }
.result-flags {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;
}
.result-flags li { background: var(--panel); border-radius: 12px; padding: 8px; font-size: 0.85rem; }
.result-flags .flag { margin-bottom: 4px; }
.result-flags .ok { font-weight: 700; }
.result-flags .ng { color: var(--ink-sub); }
#again { margin-top: 6px; }
