:root {
  --paper: #efe8d8;
  --ink: #2c2824;
  --pencil: #4a433c;
  --pencil-soft: #8a8174;
  --panel: rgba(239, 232, 216, 0.94);
  --space: #efe8d8;
  /* The title's two sizes, kept here because the random link below is placed from them. */
  --title-size: clamp(26px, 3.2vw, 36px);
  --sub-size: clamp(15px, 1.6vw, 18px);
}

/* Night is the same paper, lit by lamplight: one palette, two moods. */
body[data-light="dawn"] { --space: #ecdcc3; }
body[data-light="dusk"],
body[data-light="night"] {
  --paper: #1b2433;
  --ink: #f1e9d8;
  --pencil: #d9d0be;
  --pencil-soft: #8793aa;
  --panel: rgba(27, 36, 51, 0.94);
  --space: #16191f;
}
body[data-light="dusk"] { --space: #2a2420; }

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: "Barlow Semi Condensed", "DIN Alternate", "Arial Narrow", sans-serif;
  overflow: hidden;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--paper);
}

/* Vignette + faint paper grain — no multiply (that made 3D look see-through) */
#paper-veil {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.2 0 0 0 0 0.16 0 0 0 0 0.1 0 0 0 0.55 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(
      ellipse at center,
      transparent 50%,
      rgba(70, 55, 35, 0.1) 100%
    );
  background-size: 180px 180px, auto;
  opacity: 0.75;
}

#hint {
  position: absolute;
  bottom: 86px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  margin: 0;
  padding: 0 16px;
  /* Without a width the box is capped at half the window, and this long hint would stack into a tall column. */
  width: max-content;
  max-width: min(92vw, 960px);
  font-size: 12.5px;
  font-weight: 500;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--pencil-soft);
  text-align: center;
  pointer-events: none;
}

/* Globe space is plain paper (or night sky) so the sphere does the talking. */
/* Dark presets: light grain on dark paper */
body[data-light="dusk"] #paper-veil,
body[data-light="night"] #paper-veil { filter: invert(1); opacity: 0.4; }

body.is-globe #map { background: var(--space); }

/* Phones and other lite devices skip the blur: a soft paper-coloured haze at the same edges gives
   a similar depth cue for almost nothing. tiltshift.js drives its opacity. */
#tilt-shift.is-lite { --haze: #efe8d8; }
body[data-light="dawn"] #tilt-shift.is-lite { --haze: #f3dcc0; }
body[data-light="dusk"] #tilt-shift.is-lite { --haze: #6b5a4c; }
body[data-light="night"] #tilt-shift.is-lite { --haze: #262a33; }
#tilt-shift.is-lite i {
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  -webkit-mask-image: none;
  mask-image: none;
}
#tilt-shift.is-lite .far-top, #tilt-shift.is-lite .near-top { background: linear-gradient(to bottom, var(--haze), transparent); }
#tilt-shift.is-lite .far-bottom, #tilt-shift.is-lite .near-bottom { background: linear-gradient(to top, var(--haze), transparent); }
#tilt-shift.is-lite .near-top, #tilt-shift.is-lite .near-bottom { display: none; }

/* Tilt-shift: soft blur at the top and bottom edges when zoomed in and tilted, so the view reads
   like a miniature. Pure CSS backdrop blur with a gradient mask. The strength is the --tilt variable that tiltshift.js sets. The overlay must never get an opacity below 1: opacity makes it a backdrop root, and the blur would then see nothing behind it. */
#tilt-shift {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  visibility: hidden;
  --tilt: 0;
  --reach: 0.7;
}
#tilt-shift i {
  position: absolute;
  left: 0;
  right: 0;
  -webkit-backdrop-filter: blur(calc(var(--blur) * var(--tilt)));
  backdrop-filter: blur(calc(var(--blur) * var(--tilt)));
}
#tilt-shift .far-top { top: 0; height: calc(20% * var(--reach)); --blur: 5px; -webkit-mask-image: linear-gradient(to bottom, #000, transparent); mask-image: linear-gradient(to bottom, #000, transparent); }
#tilt-shift .near-top { top: 0; height: calc(8% * var(--reach)); --blur: 10px; -webkit-mask-image: linear-gradient(to bottom, #000, transparent); mask-image: linear-gradient(to bottom, #000, transparent); }
#tilt-shift .far-bottom { bottom: 0; height: calc(17% * var(--reach)); --blur: 5px; -webkit-mask-image: linear-gradient(to top, #000, transparent); mask-image: linear-gradient(to top, #000, transparent); }
#tilt-shift .near-bottom { bottom: 0; height: calc(7% * var(--reach)); --blur: 10px; -webkit-mask-image: linear-gradient(to top, #000, transparent); mask-image: linear-gradient(to top, #000, transparent); }

#title {
  position: absolute;
  top: 18px;
  left: 22px;
  z-index: 2;
  margin: 0;
  pointer-events: none;
  color: var(--ink);
  line-height: 1;
}
#title b {
  display: block;
  font-family: "IM Fell English", Georgia, serif;
  font-weight: 400;
  font-size: var(--title-size);
  font-weight: 600;
  letter-spacing: 0.02em;
  /* A crisp paper-coloured outline, like a map label halo: a ring of shadows, so it works in every browser. */
  text-shadow:
    -2.5px 0 var(--paper), 2.5px 0 var(--paper), 0 -2.5px var(--paper), 0 2.5px var(--paper),
    -2px -2px var(--paper), 2px -2px var(--paper), -2px 2px var(--paper), 2px 2px var(--paper),
    0 0 6px var(--paper);
}
#title span {
  display: block;
  /* A soft halo in the paper colour keeps the small text readable over any land or sea behind it. */
  text-shadow: 0 0 2px var(--paper), 0 0 5px var(--paper), 0 0 10px var(--paper), 0 0 16px var(--paper);
  font-family: "IM Fell English", Georgia, serif;
  font-style: italic;
  margin-top: 6px;
  font-size: var(--sub-size);
  color: var(--pencil);
}

#share-warn {
  position: absolute;
  bottom: 78px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  margin: 0;
  padding: 6px 14px;
  max-width: min(92vw, 420px);
  font-size: 18px;
  font-weight: 600;
  color: #6a4030;
  background: var(--paper);
  border: 1.5px solid #a07050;
  border-radius: 2px;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#share-warn.is-visible {
  opacity: 1;
}

#share.is-oversized {
  border-color: #a07050 !important;
  color: #6a4030 !important;
}

/* A chart legend rather than an app toolbar: square corners, a double rule, tracked capitals
   in the same face as the map labels, and hexagons wherever a mark is needed. */
#toolbar {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  /* Stretch so every button in a row ends at the same line and the active underlines align. */
  align-items: stretch;
  gap: 2px;
  padding: 7px 10px;
  background: var(--panel);
  border: 1px solid var(--pencil);
  box-shadow: inset 0 0 0 3px var(--panel), inset 0 0 0 4px color-mix(in srgb, var(--pencil) 55%, transparent);
  white-space: nowrap;
}

#toolbar button {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--pencil-soft);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  line-height: 1;
  padding: 9px 10px 7px;
  cursor: pointer;
  border-radius: 0;
  transition: color 0.15s ease, border-color 0.15s ease;
}

#random {
  position: absolute;
  /* Just below the title block: its top, the title line, a 6px gap, the subtitle line, then a gap of about the same. */
  top: calc(18px + var(--title-size) + 6px + var(--sub-size) + 9px);
  left: 22px;
  z-index: 2;
  appearance: none;
  margin: 0;
  padding: 0;
  background: none;
  border: 0;
  /* A text underline, not a border: the glow below would paint over a border and wash it out. */
  text-decoration: underline dotted var(--pencil);
  text-decoration-thickness: 1px;
  text-underline-offset: 4px;
  color: var(--pencil);
  text-shadow: 0 0 2px var(--paper), 0 0 5px var(--paper), 0 0 10px var(--paper), 0 0 16px var(--paper);
  font-family: "IM Fell English", Georgia, serif;
  font-style: italic;
  font-size: clamp(15px, 1.6vw, 18px);
  line-height: 1.2;
  cursor: pointer;
  transition: color 0.15s ease;
}
#random:hover { color: var(--ink); text-decoration-color: var(--ink); }
#random:focus-visible { outline: 1px dashed var(--pencil); outline-offset: 3px; }

#toolbar button:hover { color: var(--ink); }

#toolbar button.is-active {
  color: var(--ink);
  border-bottom-color: var(--ink);
}

#toolbar button:focus-visible { outline: 1px dashed var(--pencil); outline-offset: 2px; }

/* A tiny hexagon between groups, like a printer's rule ornament */
#toolbar .toolbar-sep {
  align-self: center;
  width: 8px;
  height: 9px;
  margin: 0 7px;
  background: var(--pencil-soft);
  opacity: 0.6;
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}

/* Biomes: a hexagon each, the active one names itself */
#toolbar .swatch { display: inline-flex; align-items: center; gap: 7px; padding: 7px 6px 5px; }
#toolbar .swatch i {
  position: relative;
  width: 18px;
  height: 20px;
  background: var(--pencil);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
#toolbar .swatch i::after {
  content: "";
  position: absolute;
  inset: 2.5px;
  background: var(--swatch);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
#toolbar .swatch span { display: none; }
#toolbar .swatch.is-active span { display: inline; }

#light-preset {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--pencil-soft);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  line-height: 1;
  padding: 9px 20px 7px 10px;
  border-radius: 0;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--pencil-soft) 50%),
    linear-gradient(135deg, var(--pencil-soft) 50%, transparent 50%);
  background-position: calc(100% - 11px) 55%, calc(100% - 7px) 55%;
  background-size: 4px 4px, 4px 4px;
  background-repeat: no-repeat;
}
#light-preset:hover { color: var(--ink); }
#light-preset option { color: #2c2824; background: #efe8d8; text-transform: none; letter-spacing: 0; }

#undo:disabled { opacity: 0.3; cursor: default; }
/* Its own section at the end of the toolbar: it deletes the whole world. */
#toolbar #clear { display: none; color: #b3402f; font-weight: 600; }
#toolbar #clear-sep { display: none; }
body.has-islands #toolbar #clear { display: inline-block; }
body.has-islands #toolbar #clear-sep { display: block; }
body[data-light="dusk"] #toolbar #clear, body[data-light="night"] #toolbar #clear { color: #ec8b78; }
#toolbar #clear:hover, #toolbar #clear.is-armed { background: #b3402f; color: #fff; }

#share.is-copied { color: var(--ink); }
#share.is-oversized { color: #c07a50; }

#share-warn {
  position: absolute;
  bottom: 86px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  margin: 0;
  padding: 6px 14px;
  max-width: min(92vw, 420px);
  font-size: 17px;
  font-weight: 500;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid #c07a50;
  border-radius: 8px;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#share-warn.is-visible { opacity: 1; }

@media (max-width: 760px) {
  #title { top: 12px; left: 14px; }
  /* One line that shrinks with the screen, so it never wraps onto the random link below it. */
  :root { --sub-size: min(14px, 3.5vw); }
  #title span { margin-top: 5px; line-height: 1.2; white-space: nowrap; }
  #random { top: calc(12px + var(--title-size) + 5px + 1.2 * var(--sub-size) + 8px); left: 14px; font-size: 14px; }
  #toolbar {
    flex-wrap: wrap;
    justify-content: center;
    width: min(96vw, 420px);
    bottom: 16px;
  }
  /* Roomy, finger-sized buttons that share each row's width instead of huddling in the middle. */
  #toolbar { gap: 2px; padding: 8px 10px; }
  #toolbar button, #light-preset {
    flex: 1 1 auto;
    min-height: 44px;
    font-size: 12.5px;
    letter-spacing: 0.12em;
    padding: 10px 10px 8px;
    text-align: center;
  }
  #toolbar .swatch { justify-content: center; padding: 10px 8px 8px; }
  /* Room for the dropdown arrow so it never sits on the word. */
  #light-preset {
    padding-right: 30px;
    background-position: calc(100% - 14px) 55%, calc(100% - 10px) 55%;
  }
  #toolbar .toolbar-sep, body.has-islands #toolbar #clear-sep { display: none; }
  /* Lift the logo, attribution and hint above the toolbar, whatever height it wraps to. */
  .mapboxgl-ctrl-bottom-left, .mapboxgl-ctrl-bottom-right { margin-bottom: calc(var(--toolbar-h, 100px) + 8px); }
  #hint, #share-warn { bottom: calc(var(--toolbar-h, 100px) + 30px); }
}

.mapboxgl-canvas { cursor: crosshair; }

body.is-draw-mode .mapboxgl-canvas { cursor: crosshair; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* About panel */
#about {
  position: absolute;
  z-index: 3;
  left: 50%;
  bottom: 124px;
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 28px));
  max-height: calc(100vh - 170px);
  overflow: auto;
  padding: 20px 24px 18px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--pencil);
  box-shadow: inset 0 0 0 3px var(--panel), inset 0 0 0 4px color-mix(in srgb, var(--pencil) 55%, transparent);
  font-family: "Barlow Semi Condensed", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}
#about[hidden] { display: none; }
#about h2 {
  margin: 0 0 8px;
  font-family: "IM Fell English", Georgia, serif;
  font-weight: 400;
  font-size: 26px;
  line-height: 1.1;
}
#about p { margin: 0 0 10px; }
#about ul { margin: 0 0 10px; padding-left: 18px; }
#about li { margin: 0 0 4px; }
#about b { font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; font-size: 12px; }
#about .about-credits { margin: 12px 0 0; color: var(--pencil-soft); font-size: 12px; }
#about-close {
  position: absolute;
  top: 6px;
  right: 10px;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--pencil-soft);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}
#about-close:hover { color: var(--ink); }

/* Shown while a big world is being restored or built. Covers the page so edits cannot land mid-build. */
#busy {
  position: fixed;
  inset: 0;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  cursor: progress;
  font-family: "Barlow Semi Condensed", system-ui, sans-serif;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink);
  animation: busy-in 0.25s ease both;
}
#busy[hidden] { display: none; }
#busy::before {
  content: "";
  position: absolute;
  z-index: -1;
  width: 260px;
  height: 64px;
  background: var(--panel);
  border: 1px solid var(--pencil);
  box-shadow: inset 0 0 0 3px var(--panel), inset 0 0 0 4px color-mix(in srgb, var(--pencil) 55%, transparent);
}
#busy svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.5;
  stroke-linejoin: round;
  will-change: transform, opacity;
  animation: busy-pulse 1.1s ease-in-out infinite;
}
#busy.is-failed { cursor: pointer; }
#busy.is-failed svg { animation: none; opacity: 1; }
@keyframes busy-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes busy-pulse {
  0%, 100% { transform: scale(0.8) rotate(0deg); opacity: 0.45; }
  50% { transform: scale(1.05) rotate(30deg); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  #busy svg { animation: none; }
}

/* Lighthouse keeper's notes and treasure finds: a small paper card on the map */
.keeper-note .mapboxgl-popup-content {
  padding: 10px 14px 11px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--pencil);
  border-radius: 0;
  box-shadow: inset 0 0 0 3px var(--panel), inset 0 0 0 4px color-mix(in srgb, var(--pencil) 55%, transparent);
  font-family: "IM Fell English", Georgia, serif;
  font-style: italic;
  font-size: 15px;
  line-height: 1.35;
}
.keeper-note .mapboxgl-popup-content b {
  display: block;
  margin-bottom: 3px;
  font-family: "Barlow Semi Condensed", system-ui, sans-serif;
  font-style: normal;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.keeper-note .mapboxgl-popup-content p { margin: 0; }
.keeper-note .mapboxgl-popup-content b { padding-right: 18px; }
.keeper-note .mapboxgl-popup-content p { padding-right: 14px; }
.keeper-note .mapboxgl-popup-close-button {
  top: 2px;
  right: 2px;
  width: 26px;
  height: 26px;
  padding: 0;
  font-size: 18px;
  line-height: 26px;
  color: var(--pencil);
  background: none;
  border: 0;
  border-radius: 0;
}
.keeper-note .mapboxgl-popup-close-button:hover { color: var(--ink); background: none; }
.keeper-note .mapboxgl-popup-close-button:focus-visible { outline: 1px dashed var(--pencil); outline-offset: -3px; }
.keeper-note .mapboxgl-popup-tip { display: none; }

/* The word a sea monster says as it fades */
.monster-says-root { pointer-events: none; }
.monster-says {
  display: block;
  font-family: "IM Fell English", Georgia, serif;
  font-style: italic;
  font-size: 16px;
  color: var(--ink);
  text-shadow: 0 0 3px var(--panel), 0 0 3px var(--panel), 0 0 6px var(--panel);
  opacity: 0;
  animation: monster-says 1.5s ease-out forwards;
}
/* A villager's scream or a penguin's squawk: smaller and quicker than a monster's word */
.monster-says.is-small { font-size: 12px; animation-duration: 1.1s; }
@keyframes monster-says {
  0% { opacity: 0; transform: translateY(4px); }
  20% { opacity: 0.9; }
  100% { opacity: 0; transform: translateY(-6px); }
}

/* After a minute with no mouse, key or map movement the page furniture fades away (idle.js): the title, the hint, the
   toolbar and the zoom buttons. The map's logo and attribution stay. Any movement brings it all back. */
#title, #toolbar, .mapboxgl-ctrl-top-right { transition: opacity 0.8s ease, visibility 0s; }
#random { transition: color 0.15s ease, opacity 0.8s ease, visibility 0s; }
body.is-idle #title,
body.is-idle #hint,
body.is-idle #random,
body.is-idle #toolbar,
body.is-idle .mapboxgl-ctrl-top-right {
  opacity: 0 !important;
  pointer-events: none !important;
  /* Once faded, out of the tab order and away from screen readers too. */
  visibility: hidden !important;
  transition-delay: 0s, 0.8s;
}

/* The hexagon compass (compass.js): it turns and leans with the map. */
.hex-compass button { display: flex; align-items: center; justify-content: center; perspective: 70px; }
.hex-compass-stage {
  display: block;
  width: 21px;
  height: 21px;
  transform-style: preserve-3d;
}
.hex-compass svg { display: block; width: 100%; height: 100%; }
.hex-compass .hc-body { fill: #f4efe2; stroke: #2c2824; stroke-width: 1.6; stroke-linejoin: round; }
.hex-compass .hc-north { fill: #c4533b; }
.hex-compass .hc-south { fill: #8a8174; }
.hex-compass .hc-hub { fill: #2c2824; }
