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

/* ── Dark theme (default) ── */
:root, [data-theme="dark"] {
  --bg: #1e1e2e;
  --surface: #181825;
  --border: #313244;
  --accent: #89b4fa;
  --accent-glow: rgba(137, 180, 250, 0.2);
  --on-accent: #1e1e2e;
  --correct: #a6e3a1;
  --correct-glow: rgba(166, 227, 161, 0.25);
  --missed: #f38ba8;
  --on-missed: #fff;
  --text: #cdd6f4;
  --text-dim: #a6adc8;
  --country-default: #939ab7;
  --country-not-in-set: #24273a;
  --country-hover: #8087a2;
  --country-guessed: #a6da95;
  --country-missed: #ed8796;
  --country-stroke: #24273a;
  --ocean: #11111b;
}

/* ── Light theme ── */
[data-theme="light"] {
  --bg: #eff1f5;
  --surface: #e6e9ef;
  --border: #ccd0da;
  --accent: #1e66f5;
  --accent-glow: rgba(30, 102, 245, 0.15);
  --on-accent: #eff1f5;
  --correct: #40a02b;
  --correct-glow: rgba(64, 160, 43, 0.2);
  --missed: #d20f39;
  --on-missed: #fff;
  --text: #4c4f69;
  --text-dim: #6c6f85;
  --country-default: #f2d5cf;
  --country-not-in-set: #c6d0f5;
  --country-hover: #eebebe;
  --country-guessed: #a6d189;
  --country-missed: #e78284;
  --country-stroke: #24273a;
  --ocean: #8caaee;
}

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: row;
  justify-content: center;
}

#app {
  max-width: 1000px;
  display: flex;
  flex-direction: column;
}

/* ── Top bar ── */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 1rem;
}

#topbar #right-menu {
  display: flex;
  flex-direction: row;
  gap: 1rem;
}

#lang-toggle {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.lang-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 0.2rem 0.55rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.lang-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}
.lang-btn:not(.active):hover { border-color: var(--accent); color: var(--accent); }
.lang-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.lang-sep { color: var(--border); font-size: 0.9rem; }

#topbar-title {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: 0.04em;
  text-decoration: none;
}

/* ── Score bar ── */
#score-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#timer-display {
  font-size: 1.6rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--accent);
  transition: color 0.3s;
}
#timer-display.urgent { color: var(--missed); }

#score-display {
  font-weight: 600;
  color: var(--text-dim);
}
#score-display #score-value { color: var(--text); font-size: 1.1rem; }

/* ── Theme toggle ── */
#btn-theme {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 0.2rem 0.6rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
  line-height: 1;
}
#btn-theme:hover { border-color: var(--accent); color: var(--accent); }

#btn-github {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.5rem;
  color: var(--text-dim);
  text-decoration: none;
  transition: border-color 0.15s, color 0.15s;
}
#btn-github:hover { border-color: var(--accent); color: var(--accent); }
#btn-github svg { width: 1rem; height: 1rem; }

/* ── Map ── */
#map-container {
  flex: 1;
  overflow: hidden;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

#map-tooltip {
  position: absolute;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.55rem;
  font-size: 0.82rem;
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  white-space: nowrap;
  transform: translate(-50%, calc(-100% - 8px));
  z-index: 10;
}

#world-map {
  width: 100%;
  height: 400px;
  cursor: grab;
}
#world-map:active { cursor: grabbing; }

#ocean-bg { fill: var(--ocean); }

#world-map path {
  fill: var(--country-not-in-set);
}

#world-map path.in-set,
#world-map circle.dot-marker {
  fill: var(--country-default);
  stroke: var(--country-stroke);
  stroke-width: 0.4px;
  cursor: default;
  transition: fill 0.25s;
}
#world-map .in-set:hover {
  fill: var(--country-hover) !important;
}
#world-map .guessed {
  fill: var(--country-guessed) !important;
  cursor: pointer !important;
}
#world-map .missed  {
  fill: var(--country-missed) !important;
  cursor: pointer !important;
}
#world-map .out-of-mode,
#world-map .out-of-mode:hover {
  fill: var(--country-not-in-set) !important;
  cursor: default !important;
}

/* ── Input row ── */
#input-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1.25rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#input-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
}

#country-input {
  width: 100%;
  padding: 0.55rem 0.85rem;
  font-size: 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#country-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-glow); }
#country-input:disabled { opacity: 0.4; cursor: not-allowed; }
#country-input.flash-correct { border-color: var(--correct); box-shadow: 0 0 0 2px var(--correct-glow); }
#country-input.flash-wrong   { animation: shake 0.35s ease; border-color: var(--missed); }

@keyframes shake {
  0%,100% { transform: translateX(0); }
  20%      { transform: translateX(-6px); }
  40%      { transform: translateX(6px); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(4px); }
}

#feedback {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.78rem;
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
#feedback.visible { opacity: 1; }
#feedback.correct { color: var(--correct); }
#feedback.duplicate { color: var(--text-dim); }
#feedback.wrong { color: var(--missed); }

.mode-select {
  padding: 0.55rem 0.7rem;
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  cursor: pointer;
}
.mode-select:disabled { opacity: 0.4; cursor: not-allowed; }

button.primary {
  padding: 0.55rem 1.2rem;
  font-size: 0.95rem;
  font-weight: 700;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s, transform 0.1s;
}
button.primary:hover  { opacity: 0.85; }
button.primary:active { transform: scale(0.97); }
button.primary.danger { background: var(--missed); color: var(--on-missed); }

button.secondary {
  padding: 0.55rem 1.2rem;
  font-size: 0.95rem;
  font-weight: 700;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: none;
  color: var(--accent);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
button.secondary:hover  { background: var(--accent-glow); }
button.secondary:active { transform: scale(0.97); }

/* ── Continent table ── */
#table-wrap {
  overflow-x: auto;
  overflow-y: auto;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#continent-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
  table-layout: fixed;
}

#continent-table th {
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
  padding: 0.45rem 0.5rem;
  border-bottom: 2px solid var(--border);
  text-align: center;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

#continent-table th .count {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-dim);
  margin-top: 0.1rem;
}

#continent-table th.continent-done { color: var(--correct); }
#continent-table th.continent-done .count { color: var(--correct); opacity: 0.7; }

#continent-table th.col-disabled { color: var(--text-dim); opacity: 0.45; }
#continent-table th.col-disabled .count { color: var(--text-dim); }
#continent-table td.col-disabled { opacity: 0.4; }

@keyframes continent-complete {
  0%   { transform: scale(1); }
  25%  { transform: scale(1.15); color: var(--correct); }
  55%  { transform: scale(1.07); }
  100% { transform: scale(1); }
}
#continent-table th.continent-flash {
  animation: continent-complete 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

#continent-table td {
  padding: 0.2rem 0.5rem;
  height: 2rem;
  color: var(--text);
  text-align: center;
  border-right: 1px solid var(--border);
}

#continent-table tbody tr td[data-iso2] { 
  border: 1px solid var(--border);
  background: var(--bg);
}

#continent-table td.cell-hidden { color: transparent; }

#continent-table td.guessed-cell {
  color: var(--correct);
  font-weight: 700;
}

.cell-aliases { display: none; }
#continent-table td:not(.cell-hidden) .cell-aliases {
  display: block;
  font-size: 0.65rem;
  color: var(--text-dim);
  font-style: italic;
  margin-top: 0.1rem;
}

#continent-table td.missed-cell {
  color: var(--missed);
  font-style: italic;
  opacity: 0.75;
}

/* ── Toast ── */
#toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%) translateY(0.5rem);
  background: var(--correct);
  border: 1px solid var(--correct);
  color: var(--on-accent);
  padding: 0.6rem 1rem;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 1100;
}
#toast[hidden] { display: none; }
#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Text popup (share/summary) ── */
#text-popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 1000;
}
#text-popup-overlay[hidden] { display: none; }

#text-popup {
  position: relative;
  max-width: 32rem;
  width: 100%;
  max-height: 80vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.5rem 1.25rem 1.25rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

#text-popup-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 1.75rem;
  height: 1.75rem;
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 4px;
}
#text-popup-close:hover { background: var(--accent-glow); color: var(--text); }

#text-popup-content {
  margin: 0;
  max-height: calc(80vh - 3rem);
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
}

@media (max-width: 600px) {
  #app { width: 100%; }
  #world-map { width: 600px; height: 250px; }
  #continent-table { min-width: 700px; }

  #input-row {
    flex-wrap: wrap;
    padding: 0.6rem 0.75rem;
    row-gap: 0.5rem;
  }
  #input-wrap {
    flex-basis: 100%;
  }
}
