@font-face {
  font-family: "Caveat";
  src: url("../assets/fonts/Caveat-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Caveat";
  src: url("../assets/fonts/Caveat-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

:root {
  /* Atlas Paper — warm parchment, iron-gall ink, wax-seal red, brass */
  --bg: #ece5d3;
  --bg-elevated: #f7f2e4;
  --text: #2b2620;
  --text-muted: #786e5c;
  --border: #d6c9ac;
  --accent: #a8371f;
  --accent-contrast: #f7f2e4;
  --visited: #a8371f;
  --visited-hover: #8a2c18;
  --partial: #b08628;
  --partial-hover: #8f6c1e;
  --unvisited: #d9cfb8;
  --unvisited-hover: #c7bb9e;
  --danger: #7a2418;
  --shadow: 0 8px 24px rgba(43, 38, 32, 0.16);
  --radius: 10px;
  --font-display: ui-monospace, "SF Mono", "Cascadia Code", "Courier New", Consolas, monospace;
  --font-hand: "Caveat", "Segoe Script", cursive;
}

[data-theme="dark"] {
  /* Night Voyage — ink navy, parchment text, coral stamp, aged gold */
  --bg: #171b24;
  --bg-elevated: #1f2530;
  --text: #ede7d8;
  --text-muted: #8b93a3;
  --border: #2e3646;
  --accent: #e2694a;
  --accent-contrast: #171b24;
  --visited: #e2694a;
  --visited-hover: #c85539;
  --partial: #d4a94c;
  --partial-hover: #b78e37;
  --unvisited: #262c38;
  --unvisited-hover: #323a48;
  --danger: #c6553a;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.55);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  /* Self-contained vector paper-grain texture (SVG feTurbulence, no image
     asset) laid over the flat theme color — subtle fiber/mottling instead
     of a flat fill, and always crisp since it's generated, not photographic. */
  background-color: var(--bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.045' numOctaves='4' seed='7' stitchTiles='stitch' result='big'/%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.7' numOctaves='3' seed='3' stitchTiles='stitch' result='fine'/%3E%3CfeColorMatrix in='big' type='matrix' values='0 0 0 0 0.16  0 0 0 0 0.13  0 0 0 0 0.09  0 0 0 0.16 0' result='bigInk'/%3E%3CfeColorMatrix in='fine' type='matrix' values='0 0 0 0 0.16  0 0 0 0 0.13  0 0 0 0 0.09  0 0 0 0.14 0' result='fineInk'/%3E%3CfeMerge%3E%3CfeMergeNode in='bigInk'/%3E%3CfeMergeNode in='fineInk'/%3E%3C/feMerge%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

[data-theme="dark"] body {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.045' numOctaves='4' seed='7' stitchTiles='stitch' result='big'/%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.7' numOctaves='3' seed='3' stitchTiles='stitch' result='fine'/%3E%3CfeColorMatrix in='big' type='matrix' values='0 0 0 0 0.85  0 0 0 0 0.87  0 0 0 0 0.94  0 0 0 0.13 0' result='bigInk'/%3E%3CfeColorMatrix in='fine' type='matrix' values='0 0 0 0 0.85  0 0 0 0 0.87  0 0 0 0 0.94  0 0 0 0.1 0' result='fineInk'/%3E%3CfeMerge%3E%3CfeMergeNode in='bigInk'/%3E%3CfeMergeNode in='fineInk'/%3E%3C/feMerge%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding: 1.1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.app-header__brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.app-header__mark {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

/* Funky hand-drawn compass logo */
.app-logo {
  width: 2.75rem;
  height: 2.75rem;
  flex-shrink: 0;
  overflow: visible;
}

.app-logo__ring {
  stroke: var(--text);
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app-logo line {
  stroke: var(--text-muted);
  stroke-width: 1.6;
  stroke-linecap: round;
}

.app-logo__needle {
  transform-origin: 32px 32px;
  animation: compass-wobble 7s ease-in-out infinite;
}

.app-logo__needle-n {
  fill: var(--accent);
}

.app-logo__needle-s {
  fill: var(--text-muted);
}

.app-logo__pivot {
  fill: var(--bg-elevated);
  stroke: var(--text);
  stroke-width: 1.4;
}

.app-logo__spark {
  fill: var(--partial);
}

.app-logo__spark--sm {
  fill: var(--accent);
  opacity: 0.85;
}

@keyframes compass-wobble {
  0%,
  100% {
    transform: rotate(-8deg);
  }
  50% {
    transform: rotate(10deg);
  }
}

.app-header h1 {
  font-family: var(--font-hand);
  font-size: 2.3rem;
  font-weight: 700;
  line-height: 1;
  margin: 0;
  border-bottom: 3px solid var(--partial);
  padding-bottom: 0.1rem;
  width: fit-content;
  transform: rotate(-1.5deg);
}

.app-header__tagline {
  margin: 0.15rem 0 0;
  font-family: var(--font-hand);
  font-size: 1.1rem;
  color: var(--text-muted);
  letter-spacing: 0.01em;
  transform: rotate(-1deg);
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: 999px;
  width: 2.5rem;
  height: 2.5rem;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-toggle:hover {
  border-color: var(--accent);
}

/* Main layout */
#app {
  flex: 1;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem;
}

/* Map heading (title + back button) */
.map-heading {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.map-heading h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.back-button {
  font-family: var(--font-display);
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  cursor: pointer;
}

.back-button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.back-button[hidden] {
  display: none;
}

/* Stats bar — styled like a boarding-pass stub: two ticket fields split by
   a perforated divider, with a stamped-ink progress line underneath. */
.stats-bar {
  display: flex;
  align-items: stretch;
  flex-wrap: wrap;
  row-gap: 0.5rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  margin-bottom: 1rem;
  gap: 1.1rem;
  position: relative;
}

.stats-bar__field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding-right: 1.1rem;
  border-right: 1px dashed var(--border);
}

.stats-bar__field--pct {
  border-right: none;
  padding-right: 0;
}

.stats-bar__label {
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.stats-bar__value {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text);
}

.stats-bar__field--pct .stats-bar__value {
  color: var(--accent);
}

.stats-bar__track {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: var(--unvisited);
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: hidden;
}

.stats-bar__fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.25s ease;
}

/* Search */
.map-toolbar {
  position: relative;
  margin-bottom: 1rem;
}

.region-search {
  width: 100%;
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 0.95rem;
}

.region-search:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.region-search__results {
  list-style: none;
  margin: 0.25rem 0 0;
  padding: 0.25rem;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 240px;
  overflow-y: auto;
}

.region-search__results[hidden] {
  display: none;
}

.region-search__result {
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.region-search__result:hover,
.region-search__result--active {
  background: var(--unvisited);
}

.region-search__result-badge {
  font-size: 0.75rem;
  color: var(--visited);
  font-weight: 600;
}

/* Map hover tooltip */
.map-tooltip {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  background: var(--text);
  color: var(--bg-elevated);
  font-family: var(--font-display);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  white-space: nowrap;
  transform: translate(-50%, -135%);
  box-shadow: var(--shadow);
}

.map-tooltip[hidden] {
  display: none;
}

/* Legend — wax-seal dots instead of generic square swatches */
.legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  margin: 0 0 1rem;
  padding: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.legend__swatch {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 999px;
  display: inline-block;
  box-shadow: inset 0 0 0 1px rgba(43, 38, 32, 0.15);
}

.legend__swatch--visited {
  background: var(--visited);
}

.legend__swatch--partial {
  background: var(--partial);
}

.legend__swatch--dashed {
  background: var(--unvisited);
  border: 1px dashed var(--partial);
  box-shadow: none;
}

.legend.legend--country .legend__item-partial,
.legend.legend--country .legend__item-dashed {
  display: none;
}

/* Map */
.map-wrap {
  position: relative;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1.5rem;
  max-height: 75vh;
  overflow: auto;
  overscroll-behavior: contain;
}

/* Zoom controls, pinned inside the map container */
.map-zoom-controls {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.map-zoom-controls button {
  width: 2.5rem;
  height: 2.5rem;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 999px;
  font-family: var(--font-display);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.map-zoom-controls button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#map-zoom-reset {
  width: auto;
  height: auto;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.75rem;
}

#map-zoom-reset[hidden] {
  display: none;
}

/* Flag markers planted on visited regions. Sized in screen pixels (not SVG
   units) so a flag on tiny Andorra looks the same size as one on Russia. */
.map-markers {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.map-marker {
  position: absolute;
  transform: translate(-50%, -95%);
  font-size: 1.15rem;
  line-height: 1;
  filter: drop-shadow(0 1px 1.5px rgba(0, 0, 0, 0.45));
  animation: stamp-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes stamp-in {
  0% {
    opacity: 0;
    transform: translate(-50%, -95%) scale(0.3) rotate(-18deg);
  }
  60% {
    opacity: 1;
    transform: translate(-50%, -95%) scale(1.15) rotate(4deg);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -95%) scale(1) rotate(0deg);
  }
}

.map-svg {
  width: 100%;
  line-height: 0;
  opacity: 1;
  transform: scale(var(--zoom, 1));
  transform-origin: center center;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.map-svg--transitioning {
  opacity: 0;
  transform: scale(calc(var(--zoom, 1) * 1.05));
}

.map-svg svg {
  width: 100%;
  height: auto;
  display: block;
}

.map-svg path.region {
  fill: var(--unvisited);
  stroke: var(--bg-elevated);
  stroke-width: 0.5;
  cursor: pointer;
  transition: fill 0.15s ease;
}

.map-svg path.region:hover {
  fill: var(--unvisited-hover);
}

.map-svg path.region:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Countries with a drill-down map get a dashed outline so it's clear
   clicking them opens their regions instead of just marking visited. */
.map-svg path.region.has-regions {
  stroke: var(--partial);
  stroke-width: 0.8;
  stroke-dasharray: 2 1.3;
}

.map-svg path.region.partial {
  fill: var(--partial);
}

.map-svg path.region.partial:hover {
  fill: var(--partial-hover);
}

.map-svg path.region.visited {
  fill: var(--visited);
}

.map-svg path.region.visited:hover {
  fill: var(--visited-hover);
}

/* Visited list */
.visited-list h2 {
  font-family: var(--font-display);
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  margin-bottom: 0.5rem;
}

.visited-list__empty {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.visited-list__items {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.75rem;
}

.visited-list__item {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 0.9rem;
  cursor: pointer;
}

.visited-list__item:hover {
  border-color: var(--accent);
}

.visited-list__item-name {
  font-weight: 600;
  display: block;
  margin-bottom: 0.25rem;
}

.visited-list__item-date {
  font-family: var(--font-display);
  font-size: 0.75rem;
  color: var(--text-muted);
  display: block;
  margin-bottom: 0.25rem;
}

.visited-list__item-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Side panel */
.side-panel {
  position: fixed;
  inset: 0;
  z-index: 50;
  visibility: hidden;
}

.side-panel.is-open {
  visibility: visible;
}

.side-panel__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 17, 12, 0.4);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.side-panel.is-open .side-panel__backdrop {
  opacity: 1;
}

.side-panel__content {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: min(400px, 100%);
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  overflow-y: auto;
}

.side-panel.is-open .side-panel__content {
  transform: translateX(0);
}

.side-panel__close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 2.5rem;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text-muted);
}

#side-panel-title {
  font-family: var(--font-display);
  letter-spacing: 0.02em;
}

#journal-form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 1.5rem;
}

#journal-form label {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.75rem;
}

#journal-form input,
#journal-form textarea {
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  resize: vertical;
}

#journal-form input:focus,
#journal-form textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.side-panel__actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1.25rem;
}

.btn {
  flex: 1;
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}

.btn--danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}

/* Footer */
.app-footer {
  border-top: 1px solid var(--border);
  padding: 1rem 1.5rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.app-footer p {
  margin: 0;
}

.app-footer a {
  color: var(--accent);
}

.app-footer__credits {
  margin-top: 0.4rem;
}

.app-footer__credits summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.74rem;
  list-style: none;
}

.app-footer__credits summary::-webkit-details-marker {
  display: none;
}

.app-footer__credits summary::before {
  content: "▸ ";
  font-size: 0.7em;
}

.app-footer__credits[open] summary::before {
  content: "▾ ";
}

.app-footer__credits summary:hover {
  color: var(--accent);
}

.app-footer__credits p {
  margin: 0.4rem auto 0;
  max-width: 46rem;
}

/* Enlarge touch targets for touch-primary input, regardless of viewport
   width (also applies to touchscreen laptops at desktop widths). */
@media (pointer: coarse) {
  .theme-toggle {
    width: 2.75rem;
    height: 2.75rem;
  }

  .back-button {
    padding: 0.65rem 1.1rem;
    font-size: 0.85rem;
  }

  .side-panel__close {
    width: 2.75rem;
    height: 2.75rem;
    font-size: 1.3rem;
  }

  .region-search__result {
    padding: 0.85rem 0.7rem;
  }

  .btn {
    padding: 0.85rem 0.9rem;
  }

  .map-svg path.region {
    stroke-width: 1.5;
  }

  .map-svg path.region.has-regions {
    stroke-width: 1.8;
  }

  .map-zoom-controls button {
    width: 2.75rem;
    height: 2.75rem;
  }
}

@media (max-width: 480px) {
  .app-header h1 {
    font-size: 1.85rem;
  }

  .app-header__tagline {
    font-size: 0.95rem;
  }

  .app-logo {
    width: 2.25rem;
    height: 2.25rem;
  }

  #app {
    padding: 1rem 0.75rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .map-svg,
  .stats-bar__fill,
  .side-panel__backdrop,
  .side-panel__content,
  .map-svg path.region {
    transition: none !important;
  }

  .map-marker,
  .app-logo__needle {
    animation: none !important;
  }
}
