/* ---------- Colours ----------
   Light mode is the default. Dark mode swaps in the second set of values
   when <html data-theme="dark"> (the moon/sun button sets this). */

:root {
  --font-body: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Fraunces", Georgia, serif;

  --radius-xl: 32px;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-pill: 999px;

  --bg: #c4f3de;
  --bg-glow-1: #9fe9ca;
  --bg-glow-2: #e6fcf2;
  --surface: rgba(255, 255, 255, 0.5);
  --surface-strong: rgba(255, 255, 255, 0.78);
  --surface-border: rgba(255, 255, 255, 0.85);
  --shadow: 0 12px 32px rgba(18, 92, 66, 0.12);
  --text: #0c2a23;
  --muted: #44695e;
  --chip-bg: rgba(255, 255, 255, 0.72);
  --chip-hover: #ffffff;
  --chip-active-bg: #0f2e27;
  --chip-active-text: #c8f7e2;
  --accent: #8fe3c2;
  --accent-text: #0c2a23;
  --scroll-thumb: rgba(15, 46, 39, 0.28);
  --focus: #0f2e27;
  --error: #b3261e; /* dark red: error messages and the Clear all text */
  --clear-bg: rgba(12, 42, 35, 0.07);
  --clear-bg-hover: rgba(12, 42, 35, 0.12);

  /* Map and list */
  --ocean: #effcf6;
  --not-visited: #cdd5d2;
  --visited: #1b5e20;
  --country-stroke: #ffffff;
  --dot-stroke: #7e8c87;
  --hover-outline: #0c2a23;
  --row-hover: rgba(255, 255, 255, 0.85);
  --visited-row: #c9ecd1;
  --visited-row-hover: #b5e2c0;
}

:root[data-theme="dark"] {
  --bg: #050c0a;
  --bg-glow-1: #0c3b2b;
  --bg-glow-2: #081511;
  --surface: rgba(16, 30, 26, 0.72);
  --surface-strong: rgba(22, 40, 35, 0.92);
  --surface-border: rgba(120, 255, 200, 0.09);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  --text: #eafff6;
  --muted: #8db5a7;
  --chip-bg: rgba(255, 255, 255, 0.06);
  --chip-hover: rgba(255, 255, 255, 0.12);
  --chip-active-bg: #3dffb0;
  --chip-active-text: #04140e;
  --accent: #3dffb0;
  --accent-text: #04140e;
  --scroll-thumb: rgba(61, 255, 176, 0.35);
  --focus: #3dffb0;
  --error: #ff8a80; /* a lighter red, as dark red is hard to read on near-black */
  --clear-bg: rgba(255, 255, 255, 0.1);
  --clear-bg-hover: rgba(255, 255, 255, 0.16);

  --ocean: #0a1613;
  --not-visited: #34413c;
  --visited: #23a35a; /* a brighter green, so visited countries stand out on the dark map */
  --country-stroke: #0a1613;
  --dot-stroke: #74877f;
  --hover-outline: #eafff6;
  --row-hover: rgba(255, 255, 255, 0.06);
  --visited-row: rgba(35, 163, 90, 0.24);
  --visited-row-hover: rgba(35, 163, 90, 0.34);
}

/* ---------- Page ---------- */

* {
  box-sizing: border-box;
}

html {
  color-scheme: light;
}

:root[data-theme="dark"] {
  color-scheme: dark;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-body);
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 15% 0%, var(--bg-glow-2), transparent 45%),
    radial-gradient(circle at 90% 30%, var(--bg-glow-1), transparent 50%);
  background-attachment: fixed;
  transition: background-color 0.3s, color 0.3s;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Rounded scrollbars (Chrome, Edge, Safari) */
::-webkit-scrollbar {
  width: 10px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  background-clip: padding-box;
  border: 2px solid transparent;
  border-radius: var(--radius-pill);
}

/* Firefox has no rounded-scrollbar option, so it gets thin, tinted ones */
@supports (-moz-appearance: none) {
  * {
    scrollbar-width: thin;
    scrollbar-color: var(--scroll-thumb) transparent;
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

/* The scrolling row of continent chips, between the intro card and the map */
.chip-bar {
  min-width: 0;
  padding: 8px;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.chips {
  position: relative; /* so each chip's offsetLeft is measured from the row */
  min-width: 0;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px 16px;
  /* Fade the chips out at both ends, a hint that the row scrolls */
  mask-image: linear-gradient(to right, transparent, #000 16px, #000 calc(100% - 16px), transparent);
  -webkit-mask-image: linear-gradient(to right, transparent, #000 16px, #000 calc(100% - 16px), transparent);
}

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  font-size: 0.92rem;
  font-weight: 600;
  white-space: nowrap;
  background: var(--chip-bg);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.2s;
}

.chip:hover {
  background: var(--chip-hover);
  transform: translateY(-1px);
}

.chip.active {
  background: var(--chip-active-bg);
  color: var(--chip-active-text);
  border-color: transparent;
}

.chip-count {
  padding: 2px 8px;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  background: rgba(127, 127, 127, 0.16);
  border-radius: var(--radius-pill);
}

.icon-button {
  flex: none;
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  background: var(--chip-bg);
  border: 1px solid var(--surface-border);
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}

.icon-button:hover {
  background: var(--chip-hover);
  transform: rotate(-12deg);
}

.icon-sun,
:root[data-theme="dark"] .icon-moon {
  display: none;
}

:root[data-theme="dark"] .icon-sun {
  display: block;
}

/* ---------- Main content ---------- */

.page {
  display: grid;
  gap: 16px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 8px 16px 32px;
}

/* ---------- Title (sits straight on the background) ---------- */

.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: clamp(12px, 3vw, 28px) 8px 4px;
}

/* Options link and dark mode switch, on the far right of the title */
.hero-actions {
  flex: none;
  display: flex;
  gap: 10px;
}

/* Stretched to the same height as the dark mode circle beside it */
.options-link {
  display: flex;
  align-items: center;
  padding-block: 0;
  text-decoration: none;
}

.hero h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.4rem, 6vw, 4rem);
  font-weight: 700;
  line-height: 1.02;
  letter-spacing: -0.03em;
}

/* ---------- Save your list (keys), below the map ---------- */

.save {
  padding: clamp(20px, 3vw, 32px);
}

.key-panel {
  display: grid;
  gap: 10px;
  max-width: 800px;
}

.key-panel h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-weight: 600;
}

.key-help {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--muted);
}

.key-row {
  display: flex;
  gap: 8px;
  margin: 0;
}

/* Wide enough to show the longest key (41 characters, with every place ticked) plus the padding */
.key-row .text-input {
  min-width: 46ch;
}

/* On narrower screens the key box gets its own full-width row, buttons below */
@media (max-width: 760px) {
  .key-row {
    flex-wrap: wrap;
  }

  .key-row .text-input {
    order: -1; /* move it before the buttons */
    flex-basis: 100%;
    min-width: 0;
  }

  .key-row .pill-button {
    flex: 1;
  }
}

.text-input {
  flex: 1;
  min-width: 0;
  padding: 11px 16px;
  font: inherit;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; /* keeps l, 1 and I easy to tell apart */
  font-size: 0.9rem;
  color: var(--text);
  background: var(--chip-bg);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-pill);
  outline: none;
  transition: box-shadow 0.2s;
}

.text-input::placeholder {
  font-family: var(--font-body);
  color: var(--muted);
}

.text-input:focus {
  box-shadow: 0 0 0 3px var(--accent);
}

.pill-button {
  flex: none;
  padding: 11px 18px;
  font-weight: 600;
  white-space: nowrap;
  color: var(--chip-active-text);
  background: var(--chip-active-bg);
  border: none;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: transform 0.2s, opacity 0.2s;
}

.pill-button:hover {
  transform: translateY(-1px);
  opacity: 0.9;
}

/* A lighter button, so Load stands out as the main action next to it */
.pill-button.secondary {
  color: var(--text);
  background: var(--chip-bg);
  border: 1px solid var(--surface-border);
}

.pill-button.small {
  padding: 6px 14px;
  font-size: 0.85rem;
}

/* ---------- Country name that follows the cursor ---------- */

.tooltip {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  color: var(--text);
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  pointer-events: none; /* clicks go straight through to the map */
}

.tooltip[hidden] {
  display: none;
}

.key-message {
  min-height: 1.4em; /* holds the space, so the page doesn't jump when a message appears */
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.key-message.error {
  color: var(--error);
}

.how-to {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: step;
}

.how-to li {
  position: relative;
  padding: 18px 18px 18px 60px;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--muted);
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-lg);
  counter-increment: step;
}

.how-to li::before {
  content: counter(step);
  position: absolute;
  top: 16px;
  left: 16px;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  font-weight: 700;
  color: var(--accent-text);
  background: var(--accent);
  border-radius: 50%;
}

.how-to strong {
  display: block;
  margin-bottom: 2px;
  font-size: 1rem;
  color: var(--text);
}

/* ---------- Map and list ---------- */

.explorer {
  display: flex;
  gap: 12px;
  height: clamp(520px, calc(100vh - 120px), 820px);
  padding: 12px;
}

#map-container {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: var(--ocean);
  border-radius: var(--radius-lg);
  transition: background 0.3s;
}

#map {
  width: 100%;
  height: 100%;
  display: block;
  cursor: default;
}

#map.dragging {
  cursor: move;
}

/* Maps that don't wrap (Equal Earth) are a rounded shape: the area around
   them is tinted, and the globe itself is filled with sea */
#map-container.whole-globe {
  background: var(--clear-bg);
}

.globe {
  fill: var(--ocean);
  stroke: var(--not-visited);
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
  transition: fill 0.3s;
}

.country {
  fill: var(--not-visited);
  stroke: var(--country-stroke);
  stroke-width: 0.6px;
  vector-effect: non-scaling-stroke; /* outlines stay thin when zoomed in */
  cursor: pointer;
  transition: fill 0.2s;
}

.dot {
  fill: var(--not-visited);
  stroke: var(--dot-stroke);
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: fill 0.2s;
}

/* Countries get their hover outline from .hover-outline instead, which sits
   on top so neighbouring countries can't cover part of it */
.dot:hover {
  stroke: var(--hover-outline);
  stroke-width: 1.5px;
}

.hover-outline {
  fill: none;
  stroke: var(--hover-outline);
  stroke-width: 1.5px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  pointer-events: none; /* clicks pass through to the country underneath */
}

.country.visited,
.dot.visited {
  fill: var(--visited);
}

/* ---------- Sidebar ---------- */

#sidebar {
  width: 300px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-lg);
}

/* The row above the search box */
.sidebar-actions {
  margin: 12px 12px 0;
}

/* Full width, so it lines up with the search box below it */
#clear-all {
  width: 100%;
  padding: 10px 16px;
  font-size: 0.92rem;
  color: var(--error);
  background: var(--clear-bg);
}

#clear-all:hover {
  background: var(--clear-bg-hover);
  opacity: 1;
}

.search-box {
  position: relative;
  margin: 12px;
}

.search-box svg {
  position: absolute;
  top: 50%;
  left: 16px;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

#search {
  width: 100%;
  padding: 12px 16px 12px 44px;
  font: inherit;
  color: var(--text);
  background: var(--chip-bg);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-pill);
  outline: none;
  transition: box-shadow 0.2s;
}

#search::placeholder {
  color: var(--muted);
}

#search:focus {
  box-shadow: 0 0 0 3px var(--accent);
}

/* The continent filter between the search box and the list. Smaller chips
   than the row above the map, to suit the narrow sidebar. */
.list-chips {
  flex: none;
  margin-top: -6px;
  padding: 0 12px 10px;
}

.list-chips .chip {
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
}

#country-list {
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding: 0 8px 12px;
  list-style: none;
}

#country-list li + li {
  margin-top: 2px;
}

#country-list button {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px 12px;
  text-align: left;
  background: none;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 0.15s;
}

#country-list button:hover {
  background: var(--row-hover);
}

#country-list button.visited {
  font-weight: 600;
  background: var(--visited-row);
}

#country-list button.visited:hover {
  background: var(--visited-row-hover);
}

.flag {
  width: 26px;
  height: 19px;
  flex: none;
  object-fit: cover;
  border-radius: 5px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15); /* keeps white flags visible */
}

#no-results {
  margin: 0;
  padding: 16px 20px;
  color: var(--muted);
}

/* ---------- Options page ---------- */

/* Hidden from view but still read out by screen readers and reachable with
   the keyboard, for the radio buttons behind the projection tiles */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.settings-panel {
  display: grid;
  gap: 10px;
  padding: clamp(20px, 3vw, 32px);
}

.settings-panel h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-weight: 600;
}

.projection-options {
  min-width: 0;
  margin: 6px 0 0;
  padding: 0;
  border: none;
}

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

.projection-option {
  display: grid;
  align-content: start;
  gap: 6px;
  padding: 12px 12px 18px;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.projection-option:hover {
  transform: translateY(-2px);
}

/* The picked tile gets a mint ring, like the focused search box */
.projection-option:has(input:checked) {
  box-shadow: 0 0 0 3px var(--accent);
}

.projection-option:has(input:focus-visible) {
  outline: 2px solid var(--focus);
  outline-offset: 4px;
}

/* All previews share one shape, so the row of tiles lines up; each map sits
   in the middle of its frame */
.projection-preview {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 2;
  margin-bottom: 6px;
  padding: 10px;
  background: var(--clear-bg);
  border-radius: var(--radius-md);
}

.preview-sea {
  fill: var(--ocean);
  transition: fill 0.3s;
}

.preview-land {
  fill: var(--not-visited);
  transition: fill 0.2s;
}

/* The picked projection's land turns green, like a visited country */
.projection-option:has(input:checked) .preview-land {
  fill: var(--visited);
}

.projection-name {
  padding: 0 6px;
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
}

.projection-summary {
  padding: 0 6px;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--muted);
}

/* Extra places: one panel per group, each with a tick box for every place */
.place-pickers {
  display: grid;
  gap: 12px;
  margin-top: 6px;
}

.place-picker {
  padding: 16px 18px;
  background: var(--surface-strong);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-lg);
}

/* The group's name and summary, with the Select all button on the right */
.picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.picker-text {
  display: grid;
  gap: 4px;
}

.picker-name {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
}

.picker-summary {
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--muted);
}

.place-choices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 6px;
  min-width: 0;
  margin: 12px 0 0;
  padding: 0;
  border: none;
}

.place-choice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  font-size: 0.92rem;
  line-height: 1.3;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 0.15s;
}

.place-choice:hover {
  background: var(--row-hover);
}

/* Ticked rows are shaded like visited rows in the country list */
.place-choice:has(.tick:checked) {
  font-weight: 600;
  background: var(--visited-row);
}

.place-choice:has(.tick:checked):hover {
  background: var(--visited-row-hover);
}

/* A rounded tick box, built from a checkbox. Ticked, it uses the same
   colours as the selected chip. */
.tick {
  flex: none;
  position: relative;
  width: 22px;
  height: 22px;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  background: var(--chip-bg);
  border: 1.5px solid var(--muted);
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.tick:checked {
  background: var(--chip-active-bg);
  border-color: var(--chip-active-bg);
}

/* The tick mark: two sides of a box, turned to make a check */
.tick:checked::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 6.5px;
  width: 5px;
  height: 10px;
  border: solid var(--chip-active-text);
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg);
}

/* ---------- Phones: map on top, list underneath ---------- */

@media (max-width: 700px) {
  .chip-bar {
    padding: 4px;
  }

  .chip-bar .chips {
    padding: 4px 12px;
  }

  .icon-button {
    width: 42px;
    height: 42px;
  }

  .page {
    padding: 4px 8px 24px;
    gap: 12px;
  }

  .card {
    border-radius: var(--radius-lg);
  }

  .explorer {
    flex-direction: column;
    height: auto;
    padding: 8px;
  }

  #map-container {
    flex: none;
    height: 55vh;
  }

  #sidebar {
    width: auto;
    height: 60vh;
  }
}
