:root {
  --bg: #f4f6fb;
  --bg-alt: #ffffff;
  --text: #1a2233;
  --text-dim: #5b6478;
  --border: #e1e5ee;
  --accent: #3355ff;
  --accent-dim: #e8ecff;
  --good: #1a9e5c;
  --good-bg: #e3f7ec;
  --bad: #e0473d;
  --bad-bg: #fdecea;
  --shadow: 0 2px 10px rgba(20, 30, 60, 0.06);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131c;
    --bg-alt: #171d2b;
    --text: #eef1f8;
    --text-dim: #9aa3b8;
    --border: #262e42;
    --accent: #6c85ff;
    --accent-dim: #1c2545;
    --good: #35c281;
    --good-bg: #113325;
    --bad: #ff6b61;
    --bad-bg: #3a1717;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  min-height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

button, input, select {
  font-family: inherit;
  color: inherit;
}

a { color: var(--accent); }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  padding: 6px 4px;
}
.brand-emoji { font-size: 1.3rem; }

.tabs { display: flex; gap: 4px; }
.tab {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.92rem;
}
.tab:hover { background: var(--accent-dim); color: var(--text); }
.tab.active { background: var(--accent); color: #fff; }

.topbar-score {
  margin-left: auto;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

/* Layout */
.app {
  flex: 1;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 18px 48px;
}

.footer {
  text-align: center;
  padding: 14px;
  font-size: 0.78rem;
  color: var(--text-dim);
}
.footer a { color: var(--text-dim); }

/* Home */
.hero {
  text-align: center;
  margin: 8px 0 28px;
}
.hero h1 {
  font-size: clamp(1.6rem, 4vw, 2.3rem);
  margin: 0 0 8px;
}
.hero p {
  color: var(--text-dim);
  margin: 0;
  font-size: 1.02rem;
}

.section-title {
  font-size: 1rem;
  font-weight: 700;
  margin: 28px 0 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}

.mode-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: left;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.mode-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.mode-card .emoji { font-size: 1.8rem; }
.mode-card h3 { margin: 0; font-size: 1.05rem; }
.mode-card p { margin: 0; color: var(--text-dim); font-size: 0.87rem; line-height: 1.35; }
.mode-card .best {
  margin-top: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--accent);
}

.filters-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 6px;
}

.pill-group { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  border: 1px solid var(--border);
  background: var(--bg-alt);
  color: var(--text-dim);
  padding: 6px 13px;
  border-radius: 999px;
  font-size: 0.83rem;
  font-weight: 600;
  cursor: pointer;
}
.pill.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Quiz screen */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  gap: 10px;
  flex-wrap: wrap;
}
.progress-bar {
  flex: 1;
  height: 7px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  min-width: 120px;
}
.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.25s ease;
}
.quiz-stat { font-size: 0.85rem; color: var(--text-dim); font-weight: 600; white-space: nowrap; }

.chrono-timer {
  font-size: 1rem;
  font-weight: 800;
  color: var(--accent);
  background: var(--accent-dim);
  padding: 4px 12px;
  border-radius: 999px;
  white-space: nowrap;
}
.chrono-timer.low {
  color: #fff;
  background: var(--bad);
}

.question-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 30px 20px;
  text-align: center;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}
.question-label {
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 14px;
}
.question-flag {
  width: min(260px, 80vw);
  border-radius: 8px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.question-text {
  font-size: clamp(1.4rem, 5vw, 2rem);
  font-weight: 800;
}
.question-sub { color: var(--text-dim); margin-top: 6px; }

.options-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.option-btn {
  background: var(--bg-alt);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  box-shadow: var(--shadow);
}
.option-btn:hover:not(:disabled) { border-color: var(--accent); }
.option-btn:disabled { cursor: default; }
.option-btn.correct { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.option-btn.wrong { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }
.option-btn.dim { opacity: 0.5; }

.feedback-banner {
  text-align: center;
  font-weight: 700;
  margin-top: 14px;
  min-height: 1.4em;
}
.feedback-banner.good { color: var(--good); }
.feedback-banner.bad { color: var(--bad); }

.actions-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}

.btn {
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  padding: 11px 22px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95rem;
}
.btn:hover { filter: brightness(1.08); }
.btn.secondary {
  background: var(--bg-alt);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn:disabled { opacity: 0.5; cursor: default; }

/* Map */
#map-canvas {
  width: 100%;
  height: 460px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #cfe3f5;
  box-shadow: var(--shadow);
}
@media (prefers-color-scheme: dark) {
  #map-canvas { background: #10243d; }
}
.leaflet-container { border-radius: var(--radius); font-family: inherit; }

/* Result screen */
.result-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 24px;
  text-align: center;
  box-shadow: var(--shadow);
}
.result-emoji { font-size: 3rem; }
.result-score {
  font-size: 2.6rem;
  font-weight: 800;
  margin: 6px 0;
}
.result-sub { color: var(--text-dim); margin-bottom: 18px; }
.result-newbest {
  display: inline-block;
  background: var(--good-bg);
  color: var(--good);
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
  margin-bottom: 14px;
  font-size: 0.85rem;
}

/* Explore */
.explore-toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  align-items: center;
}
.search-input {
  flex: 1;
  min-width: 180px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-alt);
  color: var(--text);
  font-size: 0.95rem;
}
.search-input:focus { outline: 2px solid var(--accent); }

.country-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}
.country-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
}
.country-card:hover { border-color: var(--accent); }
.country-card img { width: 34px; height: 24px; object-fit: cover; border-radius: 3px; flex-shrink: 0; }
.country-card .cname { font-weight: 700; font-size: 0.92rem; }
.country-card .ccap { color: var(--text-dim); font-size: 0.78rem; }

.detail-back {
  background: none;
  border: none;
  color: var(--accent);
  font-weight: 700;
  cursor: pointer;
  padding: 6px 0;
  margin-bottom: 10px;
  font-size: 0.9rem;
}

.detail-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.detail-header img { width: 96px; border-radius: 8px; border: 1px solid var(--border); box-shadow: var(--shadow); }
.detail-header h2 { margin: 0 0 4px; }
.detail-header .official { color: var(--text-dim); font-size: 0.9rem; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.detail-item {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.detail-item .label { color: var(--text-dim); font-size: 0.75rem; text-transform: uppercase; font-weight: 700; letter-spacing: 0.03em; margin-bottom: 4px; }
.detail-item .value { font-weight: 600; }
.detail-item .value a { font-weight: 600; text-decoration: none; }

#detail-map { width: 100%; height: 300px; border-radius: var(--radius); border: 1px solid var(--border); margin-bottom: 10px; }

.empty-state { text-align: center; color: var(--text-dim); padding: 40px 10px; }

.map-hint { text-align: center; color: var(--text-dim); font-size: 0.85rem; margin-top: 10px; }

@media (max-width: 560px) {
  .topbar { padding: 8px 10px; }
  .brand-name { display: none; }
  #map-canvas { height: 360px; }
}
