:root {
  --bg: #0f172a;
  --card: #111827;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --app-gutter: 16px;
  --swipe-bg-opacity: 0;
  --swipe-progress: 0;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Inter, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top, 0px)) var(--app-gutter) calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
  background: transparent;
  transition: background-color 140ms ease;
}

.app.swipe-right { background: rgba(34, 197, 94, 0.32); }
.app.swipe-left { background: rgba(239, 68, 68, 0.32); }

.topbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.topbar h1 { margin: 0; font-size: 1.6rem; }
.topbar p { margin: 4px 0 0; color: var(--muted); }

.icon-btn {
  border: 1px solid #374151;
  background: #1f2937;
  color: #fff;
  border-radius: 10px;
  padding: 8px 10px;
}

.stats { display: flex; justify-content: space-between; color: var(--muted); }

.search-wrap { position: relative; }

.search-wrap input {
  width: 100%;
  border: 1px solid #374151;
  background: #0b1220;
  color: #e5e7eb;
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 1rem;
}

.search-wrap input:focus { outline: 2px solid #475569; outline-offset: 1px; }

.search-results {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  background: #111827;
  border: 1px solid #374151;
  border-radius: 10px;
  max-height: 240px;
  overflow: auto;
  margin: 0;
  padding: 6px;
  list-style: none;
  z-index: 20;
}

.search-results.hidden { display: none; }

.search-results li {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.search-results li.active,
.search-results li:hover { background: #1f2937; }

.search-results .code { color: #93c5fd; font-weight: 700; }

.card-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  perspective: 1000px;
  position: relative;
  border-radius: 22px;
  overflow: visible;
}


.card {
  width: 100%;
  min-height: min(56dvh, 560px);
  border-radius: 20px;
  background: transparent;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 220ms ease;
  touch-action: pan-y pinch-zoom;
  user-select: none;
  will-change: transform;
  z-index: 1;
}

.card.flipped { transform: rotateY(180deg); }

.card.dragging { cursor: grabbing; }

.card.dragging .face {
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.42);
}

.face {
  position: absolute;
  inset: 0;
  border-radius: 20px;
  background: var(--card);
  border: 1px solid #1f2937;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.38);
  backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  padding: 18px;
  transition: background-color 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
}

.card.correct-flash .face {
  background: #22c55e;
  border-color: #4ade80;
  box-shadow: 0 16px 40px rgba(34, 197, 94, 0.38);
}

.card.wrong-flash .face {
  background: #b91c1c;
  border-color: #ef4444;
  box-shadow: 0 16px 40px rgba(239, 68, 68, 0.35);
}

.back { transform: rotateY(180deg); }

.visual { flex: 1; display: grid; place-items: center; overflow: hidden; }

.visual img { max-width: 100%; max-height: 34dvh; filter: invert(1) brightness(1.05); }

.visual img.flag-img {
  max-width: 100%;
  max-height: 36dvh;
  border-radius: 10px;
  object-fit: contain;
  filter: none;
  border: 1px solid #374151;
}

.label { text-align: center; font-size: 1.3rem; font-weight: 700; margin-top: 8px; min-height: 3.4rem; display: flex; align-items: center; justify-content: center; }
.label.top { margin-top: 0; margin-bottom: 8px; }
.sub { text-align: center; color: var(--muted); margin-top: 4px; font-weight: 600; }

.actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 10px;
  color: #fff;
  font-weight: 700;
  transition: transform 120ms ease, filter 120ms ease, box-shadow 120ms ease;
}

.btn:active { transform: scale(0.98); }

.btn.wrong { background: #991b1b; }
.btn.right { background: #166534; }
.btn.neutral { background: #334155; }

.app.swipe-left .btn.wrong,
.app.swipe-right .btn.right {
  filter: brightness(1.1);
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(15, 23, 42, 0.35);
}

.hint { text-align: center; color: var(--muted); margin: 4px 0 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: rgba(17, 24, 39, 0.95);
  border: 1px solid #374151;
  border-radius: 999px;
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.toast.show { opacity: 1; }

.header-selects { display: flex; gap: 8px; margin-top: 4px; }

.header-selects select {
  background: #1f2937;
  color: #e5e7eb;
  border: 1px solid #374151;
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%239ca3af'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 22px;
}

.header-selects select:focus { outline: 2px solid #475569; outline-offset: 1px; }

/* Continent multi-select dropdown */
.continent-dropdown { position: relative; }

.continent-btn {
  background: #1f2937;
  color: #e5e7eb;
  border: 1px solid #374151;
  border-radius: 8px;
  padding: 5px 22px 5px 8px;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%239ca3af'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

.continent-btn:focus { outline: 2px solid #475569; outline-offset: 1px; }

.continent-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: #1f2937;
  border: 1px solid #374151;
  border-radius: 10px;
  padding: 6px 0;
  z-index: 30;
  min-width: 180px;
  box-shadow: 0 12px 28px rgba(0,0,0,0.4);
}

.continent-panel.hidden { display: none; }

.continent-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.continent-option:hover { background: #374151; }

.continent-option input[type="checkbox"] {
  accent-color: #3b82f6;
  width: 15px;
  height: 15px;
  cursor: pointer;
}

.continent-divider {
  height: 1px;
  background: #374151;
  margin: 4px 10px;
}

.score-tap.flash-right {
  animation: flashGreen 400ms ease;
}
.score-tap.flash-wrong {
  animation: flashRed 400ms ease;
}

@keyframes flashGreen {
  0%, 100% { background: rgba(255,255,255,0.06); }
  30% { background: rgba(34, 197, 94, 0.45); }
}
@keyframes flashRed {
  0%, 100% { background: rgba(255,255,255,0.06); }
  30% { background: rgba(239, 68, 68, 0.45); }
}

@media (max-width: 768px) {
  :root {
    --app-gutter: 12px;
  }

  .app {
    max-width: none;
    gap: 10px;
  }

  .card-wrap {
    margin-inline: calc(var(--app-gutter) * -1);
    border-radius: 0;
  }

  .card {
    min-height: 58dvh;
    border-radius: 16px;
  }

  .face {
    border-radius: 16px;
  }

  .actions {
    gap: 7px;
  }

  .btn {
    padding: 13px 10px;
  }
}

.score-tap {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,0.06);
  transition: background 120ms ease;
}
.score-tap:hover { background: rgba(255,255,255,0.14); }

/* Score modal (dialog) */
.score-modal {
  border: 1px solid #374151;
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  padding: 0;
  max-width: 420px;
  width: calc(100% - 32px);
  max-height: 80dvh;
  overflow: hidden;
}

.score-modal-inner {
  display: flex;
  flex-direction: column;
  max-height: 80dvh;
  margin: 0;
}

.score-modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.score-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid #1f2937;
}
.score-modal-header h2 { margin: 0; font-size: 1.1rem; }
.score-modal-header .icon-btn { font-size: 1.3rem; line-height: 1; padding: 4px 8px; cursor: pointer; }

.score-modal-list {
  list-style: none;
  margin: 0;
  padding: 8px 16px;
  overflow-y: auto;
  flex: 1;
}

.score-modal-list li {
  padding: 10px 0;
  border-bottom: 1px solid #1f2937;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  gap: 10px;
}
.score-modal-list li:last-child { border-bottom: none; }
.score-modal-list .flag-emoji { font-size: 1.4rem; }
.score-modal-list .empty-msg {
  color: var(--muted);
  text-align: center;
  padding: 24px 0;
  display: block;
}

.score-modal-clear {
  margin: 8px 16px 14px;
}

@media (max-width: 768px) {
  .score-modal {
    max-width: none;
    width: 100%;
    max-height: 100%;
    height: 100%;
    border-radius: 0;
    border: none;
  }
  .score-modal-inner {
    max-height: 100dvh;
    height: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app,
  .card,
  .face,
  .btn,
  .toast {
    transition: none !important;
  }
}
