:root {
  --bar: #14324a;
  --bar-ink: #f3f6f9;
  --ocean: #b9d7ea;
  --land: #f2ead3;
  --land-hover: #e6d9b0;
  --other: #d9d6cc;
  --dim: #e8e4d8;
  --border: #8b8170;
  --selected: #2f7de1;
  --right: #3aa655;
  --close: #f0b429;
  --wrong: #d64545;
  --ink: #1d2733;
  --card: #ffffff;
  --muted: #5d6b7a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--ocean);
}

body { display: flex; flex-direction: column; position: fixed; inset: 0; }

button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Top bar ---------- */
.bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: calc(8px + var(--safe-top)) 16px 8px;
  background: var(--bar);
  color: var(--bar-ink);
  z-index: 10;
}
.brand { font-weight: 700; font-size: 1.15rem; letter-spacing: .02em; }
.field select {
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.25);
  border-radius: 8px;
  padding: 6px 10px;
  color: var(--bar-ink);
  min-height: 36px;
}
.field select option { color: var(--ink); }

.seg { display: inline-flex; border: 1px solid rgba(255,255,255,.3); border-radius: 8px; overflow: hidden; }
.seg button {
  background: transparent; border: 0; padding: 6px 14px; color: var(--bar-ink); min-height: 36px;
}
.seg button[aria-checked="true"] { background: var(--bar-ink); color: var(--bar); font-weight: 600; }

.stats { display: flex; align-items: center; gap: 10px; margin-left: auto; font-variant-numeric: tabular-nums; }
#progress { font-weight: 600; }
#timer { opacity: .8; min-width: 3.2em; text-align: right; }

.pill { display: inline-flex; align-items: center; gap: 5px; }
.pill i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.pill.right i { background: var(--right); }
.pill.close i { background: var(--close); }
.pill.wrong i { background: var(--wrong); }

.ghost {
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 8px;
  padding: 6px 12px;
  min-height: 36px;
  opacity: .9;
}
.primary {
  background: var(--selected);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 600;
  min-height: 44px;
}

/* ---------- Map ---------- */
#stage { position: relative; flex: 1; min-height: 0; }
#map { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }

.country, .marker {
  fill: var(--land);
  stroke: var(--border);
  stroke-width: .6px;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: round;
}
.country.other { fill: var(--other); pointer-events: none; }
.country.dim, .marker.dim { fill: var(--dim); stroke: #b8b1a1; }
.marker { stroke-width: 1px; }
.hit { fill: transparent; stroke: none; }
.marker.dim + .hit, .hit.dim { pointer-events: none; }

@media (hover: hover) {
  .country.playable:hover, .marker.playable:hover { fill: var(--land-hover); }
}
.country.right, .marker.right { fill: var(--right); }
.country.close, .marker.close { fill: var(--close); }
.country.wrong, .marker.wrong { fill: var(--wrong); }
#map .selected { fill: var(--selected); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { fill-opacity: .55; } }

/* ---------- Answer panel ---------- */
.panel {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: min(520px, calc(100% - 32px));
  background: var(--card);
  border-radius: 14px;
  box-shadow: 0 8px 30px rgba(20, 50, 74, .25);
  padding: 12px 14px;
  z-index: 5;
}
.panel.hidden { display: none; }
.prompt { font-size: .9rem; color: var(--muted); margin-bottom: 8px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.small { margin-top: 8px; font-size: .9rem; }
.row.small .ghost { color: var(--muted); min-height: 32px; padding: 4px 10px; }
#answer {
  flex: 1;
  min-width: 0;
  font-size: 1.2rem;
  padding: 10px 12px;
  border: 2px solid #cfd8e1;
  border-radius: 10px;
  outline: none;
}
#answer:focus { border-color: var(--selected); }
.feedback { flex: 1; text-align: center; font-weight: 600; min-height: 1.3em; }
.feedback.right { color: var(--right); }
.feedback.close { color: #b07d00; }
.feedback.wrong { color: var(--wrong); }
.panel.shake { animation: shake .35s; }
@keyframes shake {
  20%, 60% { transform: translateX(calc(-50% - 8px)); }
  40%, 80% { transform: translateX(calc(-50% + 8px)); }
}

.toast {
  position: absolute;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 50, 74, .92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  pointer-events: none;
  z-index: 6;
  white-space: nowrap;
}
.toast.hidden { display: none; }

.zoom {
  position: absolute;
  right: 16px;
  bottom: calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.zoom button {
  width: 44px; height: 44px;
  border-radius: 10px;
  border: 0;
  background: var(--card);
  box-shadow: 0 2px 8px rgba(0,0,0,.18);
  font-size: 1.3rem;
}

.legend {
  position: absolute;
  left: 16px;
  bottom: calc(16px + var(--safe-bottom));
  display: flex;
  gap: 12px;
  background: rgba(255,255,255,.85);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: .85rem;
}

/* ---------- Result dialog ---------- */
dialog {
  border: 0;
  border-radius: 16px;
  padding: 20px 22px;
  width: min(440px, calc(100% - 32px));
  max-height: 80vh;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(20, 50, 74, .45); }
dialog h2 { margin: 0 0 8px; }
#missed { max-height: 40vh; overflow: auto; padding-left: 0; list-style: none; margin: 10px 0; }
#missed li { padding: 3px 0; display: flex; gap: 8px; align-items: center; }
.actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }
.actions .ghost { color: var(--ink); }

@media (max-width: 640px) {
  .bar { padding-left: 12px; padding-right: 12px; gap: 6px 10px; }
  .brand { display: none; }
  .stats { margin-left: 0; width: 100%; justify-content: space-between; }
  .legend { display: none; }
}
