/* ============ 国旗クイズ ============ */

:root {
  --bg: #0d1428;
  --bg2: #101a33;
  --panel: #1b2745;
  --panel-border: rgba(255, 255, 255, 0.14);
  --text: #eef2fa;
  --text-dim: #9aa7c4;
  --accent: #4da3ff;
  --accent2: #ffd166;
  --ok: #35d07f;
  --danger: #ff5c74;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { height: 100%; }

body {
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 50% -200px, #1d2c55 0%, transparent 60%),
    linear-gradient(180deg, var(--bg2), var(--bg));
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  max-width: 560px;
  min-height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  min-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  /* 上端はタイトル帯を隠したぶん、共通ヘッダーとの間を少しだけ空ける */
  padding: 10px 14px 0;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
}

button { user-select: none; -webkit-user-select: none; }

.hidden { display: none !important; }

/* ---------- ヘッダー ---------- */

/* タイトルだけの帯は縦幅を圧迫するので隠す（ボタンは共通ヘッダーのメニューへ集約済み）。
   h1 と、共通ヘッダーが click() で押す元ボタンは DOM に残すため、display:none ではなく
   スクリーンリーダー向けの不可視にする */
#topbar {
  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;
}

#logo {
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.02em;
}

#topbar-buttons { display: flex; gap: 8px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
}
.icon-btn:active { transform: scale(0.94); }

/* ---------- 画面共通 ---------- */

#main { flex: 1; display: flex; flex-direction: column; }

.screen { display: flex; flex-direction: column; padding-bottom: 16px; }

.btn {
  border: none;
  border-radius: 14px;
  padding: 13px 18px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(180deg, #5db1ff, #2f7fe0);
  color: #fff;
  box-shadow: 0 6px 18px rgba(77, 163, 255, 0.35);
}

.btn-sub {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
}

.mini-btn {
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 7px 10px;
  background: var(--panel);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.mini-btn:active { transform: scale(0.96); }

/* ---------- ホーム画面 ---------- */

.home-lead {
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 700;
  margin: 6px 0 14px;
}

.home-caption {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  margin: 14px 2px 8px;
}

#type-cards { display: flex; gap: 10px; }

.type-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 10px 14px;
  border: 2px solid var(--panel-border);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.type-card[aria-checked="true"] {
  border-color: var(--accent);
  background: rgba(77, 163, 255, 0.14);
  box-shadow: 0 0 0 1px var(--accent) inset;
}
.type-card:active { transform: scale(0.97); }

.type-emoji { font-size: 30px; }
.type-name { font-size: 16px; font-weight: 900; }
.type-desc { font-size: 11px; color: var(--text-dim); font-weight: 700; }

#diff-seg { display: flex; gap: 10px; }

.seg-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: center;
  padding: 11px 8px;
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.seg-btn small { font-size: 11px; font-weight: 700; color: var(--text-dim); }
.seg-btn[aria-checked="true"] {
  border-color: var(--accent2);
  background: rgba(255, 209, 102, 0.12);
}
.seg-btn:active { transform: scale(0.97); }

#home-best {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
  font-size: 14px;
  font-weight: 800;
}
#home-best .best-label { color: var(--text-dim); }
#home-best-value { color: var(--accent2); }

.btn-big {
  margin-top: 16px;
  padding: 16px;
  font-size: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.btn-big small { font-size: 12px; font-weight: 700; opacity: 0.85; }

#btn-zukan { margin-top: 12px; }

/* ---------- クイズ画面 ---------- */

#quiz-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 4px 0 10px;
}

#quiz-progress { font-size: 15px; font-weight: 900; letter-spacing: 0.05em; }

#quiz-score {
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
  min-width: 52px;
  text-align: right;
}

#timer-track {
  height: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
  margin-bottom: 16px;
}

#timer-bar {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #35d07f, #4da3ff);
  transform-origin: left center;
  will-change: transform;
}
#timer-bar.warn { background: linear-gradient(90deg, #ffb648, #ff5c74); }

#q-area { text-align: center; }

#q-text {
  font-size: 15px;
  font-weight: 800;
  color: var(--text-dim);
  margin-bottom: 12px;
}

#q-flag-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 18px;
}

#q-flag {
  width: min(76vw, 320px);
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.18);
}

#q-name {
  font-size: clamp(24px, 7vw, 34px);
  font-weight: 900;
  margin: 24px 0 30px;
  min-height: 1.3em;
  line-height: 1.3;
}

/* 選択肢: 国名（縦4つ） */
.choices-text { display: flex; flex-direction: column; gap: 10px; }

.choice {
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.choices-text .choice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 14px;
  font-size: 16px;
  font-weight: 800;
  text-align: left;
  min-height: 52px;
}

.choice-key {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 選択肢: 国旗（2×2） */
.choices-flag {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.choices-flag .choice {
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.choices-flag .choice img {
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  border-radius: 6px;
  pointer-events: none;
}

@media (hover: hover) {
  .choice:not(:disabled):hover { border-color: var(--accent); }
}

.choice:disabled { cursor: default; }

.choice.correct {
  border-color: var(--ok);
  background: rgba(53, 208, 127, 0.16);
  box-shadow: 0 0 0 1px var(--ok) inset;
}

.choice.wrong {
  border-color: var(--danger);
  background: rgba(255, 92, 116, 0.14);
  animation: shake 0.3s;
}

.choice.dim { opacity: 0.45; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* ---------- 結果画面 ---------- */

#result-box {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 18px 18px;
  text-align: center;
  margin-top: 6px;
}

#result-title { font-size: 24px; font-weight: 900; }

#result-sub {
  margin-top: 6px;
  color: var(--accent2);
  font-size: 14px;
  font-weight: 800;
  animation: pulse 1.2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

#result-main {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  margin: 10px 0 14px;
}

#result-correct { font-size: 52px; font-weight: 900; color: var(--accent); line-height: 1; }

.result-unit { font-size: 15px; font-weight: 800; color: var(--text-dim); }

#result-stats {
  display: flex;
  justify-content: center;
  gap: 22px;
  margin-bottom: 16px;
}

.rstat { display: flex; flex-direction: column; gap: 2px; }
.rstat-label { font-size: 11px; font-weight: 800; color: var(--text-dim); }
.rstat > span:last-child { font-size: 16px; font-weight: 900; color: var(--accent2); }

#result-buttons { display: flex; gap: 10px; justify-content: center; }
#result-buttons .btn { flex: 1; max-width: 200px; }

#review-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.review-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  padding: 7px 10px;
}

.review-row img {
  width: 44px;
  height: 33px;
  object-fit: contain;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  flex-shrink: 0;
}

.review-mark { font-size: 16px; flex-shrink: 0; }

.review-name { font-size: 14px; font-weight: 800; flex: 1; min-width: 0; }

.review-sub {
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 700;
  display: block;
}

.review-miss { font-size: 11px; color: var(--danger); font-weight: 700; flex: 0 1 auto; max-width: 45%; text-align: right; }

/* ---------- 図鑑画面 ---------- */

#zukan-top {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0 12px;
}

#zukan-title { font-size: 17px; font-weight: 900; }

#zukan-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
  scrollbar-width: none;
}
#zukan-tabs::-webkit-scrollbar { display: none; }

.zukan-tab {
  flex-shrink: 0;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 14px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}
.zukan-tab[aria-selected="true"] {
  background: rgba(77, 163, 255, 0.18);
  border-color: var(--accent);
  color: var(--accent);
}

#zukan-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin-top: 6px;
}

.zukan-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px;
  text-align: center;
}

.zukan-card img {
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.05);
}

.zukan-ja { display: block; font-size: 13px; font-weight: 800; margin-top: 6px; }
.zukan-en { display: block; font-size: 10px; font-weight: 700; color: var(--text-dim); margin-top: 1px; }

/* ---------- モーダル ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(8, 12, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-box {
  width: min(420px, 100%);
  max-height: min(84vh, 640px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #223154, #16203a);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 20px 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-title { font-size: 20px; font-weight: 900; text-align: center; }

.howto {
  margin: 0;
  padding-left: 1.3em;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
}
.howto b { color: var(--accent2); }

.credit-note { font-size: 11px; color: var(--text-dim); text-align: center; }
.credit-note a { color: var(--text-dim); }

/* ---------- フッター ---------- */

#footer {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  padding: 18px 0;
  padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  line-height: 1.8;
}
#footer a { color: var(--text-dim); }

@media (min-width: 700px) {
  #logo { font-size: 24px; }
  .choices-text .choice { font-size: 17px; }
}

/* ---------- 全国ランキング（SGRanking 共通モジュール） ---------- */
.sgr-hidden {
  display: none !important;
}

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000; /* 既存モーダル(60)より上 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(8, 12, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, #223154, #16203a);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 20px 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.sgr-title {
  margin: 0;
  font-size: 20px;
  font-weight: 900;
  text-align: center;
}

.sgr-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.sgr-tab {
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 12px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

.sgr-tab-active {
  background: rgba(77, 163, 255, 0.18);
  border-color: var(--accent);
  color: var(--accent);
}

.sgr-register-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
  text-align: center;
}

.sgr-form {
  display: flex;
  gap: 8px;
}

.sgr-name {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
}

.sgr-name:focus {
  outline: none;
  border-color: var(--accent);
}

.sgr-submit {
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #5db1ff, #2f7fe0);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(77, 163, 255, 0.35);
}

.sgr-submit:hover {
  filter: brightness(1.08);
}

.sgr-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

.sgr-error {
  margin: 4px 0 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--danger);
}

.sgr-status {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
}

.sgr-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.sgr-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 14px;
}

.sgr-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.05);
}

.sgr-row.sgr-row-me {
  background: rgba(77, 163, 255, 0.25);
  color: #fff;
}

.sgr-rank {
  width: 2em;
  text-align: right;
  font-weight: 800;
  flex-shrink: 0;
}

.sgr-row-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sgr-row-score {
  font-weight: 800;
  color: var(--accent2);
  flex-shrink: 0;
}

.sgr-row.sgr-row-me .sgr-row-score {
  color: #fff;
}

.sgr-close {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.sgr-close:active { transform: scale(0.97); }

.sgr-cta {
  margin: 0 0 14px;
  text-align: center;
}

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
}

.sgr-cta-btn {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #ffe08a, #ffb648);
  color: #4a3200;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(255, 209, 102, 0.35);
}
.sgr-cta-btn:active { transform: scale(0.97); }

.sgr-cta-link {
  border: none;
  background: none;
  padding: 6px;
  font-family: inherit;
  font-size: 13px;
  color: var(--text-dim);
  text-decoration: underline;
  cursor: pointer;
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / 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; }
}
