/* 地図クイズ - Map Quiz */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #eef4fb;
  --panel: #ffffff;
  --text: #2b3a4a;
  --text-sub: #6b7d8f;
  --accent: #2f7fd4;
  --accent-dark: #23639f;
  --sea: #cfe3f5;
  --land: #f2ecdd;
  --land-stroke: #a8bccd;
  --good: #45a05e;
  --good-light: #a5d8b2;
  --warn: #e6a23c;
  --warn-light: #f4d9a8;
  --bad: #d95c5c;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(43, 58, 74, .1);
}

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  min-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
}

/* ---------- ヘッダー ----------
   タイトルだけの帯は縦幅を圧迫するので隠す（音のボタンは共通ヘッダーのメニューへ集約済み）。
   h1 と、共通ヘッダーが click() で押す元ボタンは DOM に残すため、display:none ではなく
   スクリーンリーダー向けの不可視にする */
.header {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}
.title {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--accent-dark);
}
.title::before { content: "🗺️ "; }

/* ---------- ボタン ---------- */
.btn {
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 700;
  padding: 10px 18px;
  cursor: pointer;
  font-family: inherit;
  transition: filter .15s, transform .05s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: #dde7f0; color: var(--text); }
.btn-secondary:hover { filter: brightness(1.03); }
.btn-small { font-size: 13px; padding: 7px 12px; }
.btn-icon {
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:active { transform: scale(.95); }

.screen[hidden] { display: none !important; }

/* ---------- モード選択 ---------- */
.screen-select { display: flex; flex-direction: column; gap: 16px; }
.lead {
  text-align: center;
  color: var(--text-sub);
  font-size: 14px;
  line-height: 1.7;
}
.load-error {
  text-align: center;
  color: var(--bad);
  font-size: 13px;
  font-weight: 700;
}
.mode-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
@media (min-width: 560px) {
  .mode-list { grid-template-columns: 1fr 1fr; }
}
.mode-card {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: border-color .15s, transform .1s;
}
.mode-card:hover { border-color: var(--accent); transform: translateY(-1px); }
/* 地図データを読み込んでいる間だけ薄く見せる */
.mode-card[aria-busy="true"] { opacity: .6; }
.mode-emoji { font-size: 30px; }
.mode-body { display: flex; flex-direction: column; gap: 2px; }
.mode-name { font-size: 16px; font-weight: 800; color: var(--text); }
.mode-desc { font-size: 12px; color: var(--text-sub); }
.mode-best { font-size: 12px; color: var(--accent-dark); font-weight: 700; min-height: 1em; }

.howto {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
}
.howto h2 { font-size: 15px; margin-bottom: 8px; color: var(--accent-dark); }
.howto ul { padding-left: 20px; }
.howto li { font-size: 13px; line-height: 1.8; color: var(--text-sub); }

/* ---------- ゲーム画面 ---------- */
.screen-game { display: flex; flex-direction: column; gap: 8px; flex: 1; }
.game-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.game-stats { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.stat {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
}
.stat-good { color: var(--good); }
.stat-bad { color: var(--bad); }

.question {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
  padding: 10px 12px;
}
.question-sub { font-size: 12px; color: var(--text-sub); }
.question-main { font-size: 24px; font-weight: 800; letter-spacing: .04em; }
.question-main small { font-size: 13px; font-weight: 600; color: var(--text-sub); margin-left: 6px; }

.progress-bar {
  height: 6px;
  border-radius: 3px;
  background: #dbe5ef;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 3px;
  transition: width .3s;
}

.map-wrap {
  position: relative;
  background: var(--sea);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  flex: 1;
  min-height: 320px;
  max-height: 70dvh;
  touch-action: none;
}
.map-wrap svg {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}
.map-wrap svg.dragging { cursor: grabbing; }

.map-zoom {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 2;
}

/* 地図の地域スタイル */
.region {
  fill: var(--land);
  stroke: var(--land-stroke);
  stroke-width: 1;
  cursor: pointer;
  transition: fill .12s;
}
.region polygon, .region path, .region line, .region use {
  vector-effect: non-scaling-stroke;
}
@media (hover: hover) {
  .region:hover { fill: #dfd4b8; }
}
.region.done-good { fill: var(--good-light); }
.region.done-miss { fill: var(--warn-light); }
.region.flash-wrong { fill: var(--bad) !important; }
.region.flash-good { fill: var(--good) !important; }
.region.reveal { animation: reveal-pulse 1s ease-in-out 2; }
@keyframes reveal-pulse {
  0%, 100% { fill: var(--land); }
  50% { fill: var(--warn); }
}
.region-inert { fill: var(--land); stroke: var(--land-stroke); stroke-width: 1; }
.region-inert path, .region-inert { vector-effect: non-scaling-stroke; }
.map-lines { stroke: #9fb6ca; stroke-width: 1.5; fill: none; pointer-events: none; }
.map-lines line, .map-lines path { vector-effect: non-scaling-stroke; }

/* トースト */
.toast {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  background: rgba(43, 58, 74, .92);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  border-radius: 999px;
  padding: 8px 18px;
  /* 1行に収まるときは1行のまま。長い名前（「ニューファンドランド・ラブラドール州」等）が
     狭い画面で地図の枠からはみ出して切れないよう、枠の幅で折り返す */
  width: max-content;
  max-width: 100%;
  text-align: center;
  pointer-events: none;
  z-index: 3;
  animation: toast-in .18s ease-out;
}
.toast.toast-good { background: rgba(69, 160, 94, .95); }
.toast.toast-bad { background: rgba(217, 92, 92, .95); }
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---------- 結果画面 ---------- */
.screen-result {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 18px;
  text-align: center;
}
.result-title { font-size: 18px; color: var(--text-sub); }
.result-rank {
  font-size: 64px;
  font-weight: 900;
  line-height: 1.1;
  color: var(--accent);
}
.result-rank.rank-S { color: #d4a017; }
.result-rank.rank-A { color: var(--good); }
.result-score { font-size: 18px; font-weight: 800; }
.result-time { font-size: 14px; color: var(--text-sub); font-variant-numeric: tabular-nums; }
.result-best { color: #d4a017; font-weight: 800; animation: best-pop .4s ease-out; }
@keyframes best-pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.result-review { width: 100%; text-align: left; margin-top: 6px; }
.result-review h3 { font-size: 14px; color: var(--bad); margin-bottom: 8px; }
.review-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.review-list li {
  background: #fdeeee;
  color: #a94040;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  padding: 5px 12px;
}
.result-actions { display: flex; gap: 10px; margin-top: 10px; }

/* ---------- フッター ---------- */
.footer {
  text-align: center;
  padding: 18px 4px 10px;
  margin-top: auto;
}
.footer-note { font-size: 10px; color: #93a5b5; margin-bottom: 6px; }
.footer-note a { color: #93a5b5; }
.footer-credits { font-size: 12px; color: var(--text-sub); line-height: 1.7; }
.footer-credits a { color: var(--accent-dark); }

@media (max-width: 420px) {
  .question-main { font-size: 20px; }
  .result-rank { font-size: 52px; }
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / js/ad-banner.js が挿入する）
   --ad-h    : 広告そのものの高さ。広告なしのときは 0px
   --ad-space: レイアウトが空けるべき高さ（広告 + セーフエリア）
   ========================================================================== */
:root {
  --ad-h: 0px;
  --ad-space: 0px;
}
html.has-ad-banner {
  --ad-space: calc(var(--ad-h, 0px) + env(safe-area-inset-bottom, 0px));
}

.ad-banner {
  /* height は広告そのものの高さ。セーフエリアぶんの padding は外側に足す */
  box-sizing: content-box;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* ランキング・リザルト等の全画面オーバーレイ（2147483000）よりは下に置く */
  z-index: 2147482000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: var(--ad-h, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.86);
  /* border ではなく inset shadow にして、枠線ぶん高さがずれないようにする */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.ad-banner__frame {
  display: block;
  width: 100%;
  height: var(--ad-h, 0px);
  border: 0;
  color-scheme: light;
}

@media print {
  .ad-banner { display: none; }
}
