/* ============================================================
   Augustin Bresset — Procedural Island Portfolio
   A 3D low-poly diorama world (Three.js) seen at an oblique angle.
   Aesthetic: Miyazaki / illustrated diorama / inventor's atlas.
   ============================================================ */

:root {
  --parchment: #EDE4D2;
  --parchment-dark: #D9CDB8;
  --ink: #2C1A0E;
  --ink-mid: #5C3D20;
  --ink-light: #8B6845;
  --rust: #C4763A;
  --sage: #7A9A6E;
  --slate: #6B8FA8;
  --amber: #D4A84B;
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--parchment);
  font-family: 'EB Garamond', serif;
}

/* ── Viewport ─────────────────────────────────────────────── */
#viewport {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--parchment);
  cursor: grab;
  user-select: none;
  /* claim ALL touch gestures for the map: without this the browser hijacks
     one-finger drags (scroll / pull-to-refresh) and pinches (page zoom), which is
     what makes touch navigation feel broken on phones. */
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

#viewport canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* ── Painterly overlay ────────────────────────────────────── */
/* Coarse paper "tooth": low-frequency fibres, soft-light blend so it reads like
   the grain of a sketchbook page rather than a darkening film. */
#paper {
  position: fixed;
  inset: 0;
  z-index: 39;
  pointer-events: none;
  opacity: 0.5;
  mix-blend-mode: soft-light;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='320'%3E%3Cfilter id='p'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.012 0.016' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23p)'/%3E%3C/svg%3E");
  background-size: 520px 520px;
}
/* Fine pencil grain: high-frequency tooth, multiply for the faint graphite speckle. */
#grain {
  position: fixed;
  inset: 0;
  z-index: 40;
  pointer-events: none;
  opacity: 0.10;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

#vignette {
  position: fixed;
  inset: 0;
  z-index: 41;
  pointer-events: none;
  background: radial-gradient(ellipse 75% 75% at 50% 45%,
    rgba(44, 26, 14, 0) 55%, rgba(40, 28, 16, 0.26) 100%);
}

/* ── UI Overlay ───────────────────────────────────────────── */
#ui {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 1.4rem 1.8rem;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  z-index: 100;
  pointer-events: none;
}

.ui-btn {
  pointer-events: all;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  background: rgba(237, 228, 210, 0.9);
  border: 1.5px solid rgba(44, 26, 14, 0.28);
  color: var(--ink);
  font-family: 'EB Garamond', serif;
  font-size: 0.95rem;
  line-height: 1;
  padding: 0.45rem 1rem;
  cursor: pointer;
  letter-spacing: 0.02em;
  backdrop-filter: blur(6px);
  transition: background 0.2s, border-color 0.2s, opacity 0.2s;
}

.ui-btn:hover {
  background: rgba(237, 228, 210, 1);
  border-color: rgba(44, 26, 14, 0.55);
}

.ui-btn--theme {
  margin-left: auto;               /* push the theme + lang pair to the right */
  font-family: 'Caveat', cursive;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 0.45rem 0.85rem;
}

.ui-btn--lang {
  margin-left: 0;
  font-family: 'Caveat', cursive;
  font-size: 0.88rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 0.45rem 0.8rem;
}

.ui-btn--help {
  margin-left: 0;
  font-family: 'EB Garamond', serif;
  font-weight: 700;
  font-size: 1rem;
  padding: 0.45rem 0.85rem;
}

/* ── Sketch / croquis theme: heavier paper tooth + graphite grain, lighter
   vignette so the page reads like a pencil sketchbook leaf. ─────────────── */
body.theme-sketch #paper { opacity: 0.82; background-size: 430px 430px; }
body.theme-sketch #grain { opacity: 0.2; }
body.theme-sketch #vignette {
  background: radial-gradient(ellipse 82% 82% at 50% 45%,
    rgba(80, 70, 52, 0) 60%, rgba(96, 84, 62, 0.13) 100%);
}

/* ── Hint ─────────────────────────────────────────────────── */
.ui-hint {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  z-index: 60;
  transform: translateX(-50%);
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.9rem;
  color: rgba(44, 26, 14, 0.45);
  pointer-events: none;
  transition: opacity 0.5s;
  white-space: nowrap;
}

/* ── Settlement legend (clickable list, overview only) ────────── */
.legend {
  position: fixed;
  top: 4.6rem;
  left: 1.8rem;
  z-index: 80;
  background: rgba(237, 228, 210, 0.9);
  border: 1.5px solid rgba(44, 26, 14, 0.28);
  backdrop-filter: blur(6px);
  padding: 0.7rem 0.7rem 0.55rem;
  width: 188px;
  box-shadow: 4px 5px 0 rgba(44, 26, 14, 0.12);
  transition: opacity 0.4s, transform 0.4s;
}
.legend.hidden {
  opacity: 0;
  transform: translateX(-12px);
  pointer-events: none;
}
.legend-title {
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.7rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-light);
  padding: 0 0.35rem 0.5rem;
  margin-bottom: 0.2rem;
  border-bottom: 1px solid rgba(44, 26, 14, 0.14);
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  background: none;
  border: none;
  padding: 0.4rem 0.35rem;
  cursor: pointer;
  text-align: left;
  border-radius: 3px;
  transition: background 0.15s;
}
.legend-item:hover { background: rgba(44, 26, 14, 0.08); }
.legend-dot {
  flex: 0 0 auto;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 1.5px solid rgba(44, 26, 14, 0.4);
  box-shadow: 0 0 7px currentColor;
}
.legend-text { display: flex; flex-direction: column; line-height: 1.1; }
.legend-name {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.18rem;
  color: var(--ink);
}
.legend-sub {
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.7rem;
  color: var(--ink-light);
}

/* ── City links (the focused settlement's front doors: repo/demo/site) ──────
   Takes the legend's top-left slot while in a city (the legend is hidden then). */
.citylinks {
  --accent: var(--rust);
  position: fixed;
  top: 4.6rem;
  left: 1.8rem;
  z-index: 80;
  background: rgba(237, 228, 210, 0.9);
  border: 1.5px solid rgba(44, 26, 14, 0.28);
  border-left: 4px solid var(--accent);
  backdrop-filter: blur(6px);
  padding: 0.7rem 0.7rem 0.55rem;
  width: 188px;
  box-shadow: 4px 5px 0 rgba(44, 26, 14, 0.12);
  transition: opacity 0.4s, transform 0.4s;
}
.citylinks.hidden {
  opacity: 0;
  transform: translateX(-12px);
  pointer-events: none;
}
.citylinks-head {
  display: flex;
  flex-direction: column;
  line-height: 1.05;
  padding: 0 0.35rem 0.5rem;
  margin-bottom: 0.25rem;
  border-bottom: 1px solid rgba(44, 26, 14, 0.14);
}
.citylinks-city {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--ink);
}
.citylinks-title {
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.66rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-light);
}
.citylinks-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.4rem 0.35rem;
  text-decoration: none;
  border-radius: 3px;
  transition: background 0.15s;
}
.citylinks-item:hover { background: rgba(44, 26, 14, 0.08); }
.citylinks-dot {
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  border: 1.5px solid rgba(44, 26, 14, 0.4);
  box-shadow: 0 0 7px currentColor;
}
.citylinks-label {
  flex: 1 1 auto;
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.12rem;
  color: var(--ink);
}
.citylinks-arrow {
  flex: 0 0 auto;
  font-family: 'EB Garamond', serif;
  font-size: 0.9rem;
  color: var(--ink-light);
  transition: transform 0.15s, color 0.15s;
}
.citylinks-item:hover .citylinks-arrow {
  color: var(--accent);
  transform: translate(1px, -1px);
}

/* ── Field note (a slip pinned beside a landmark) ─────────────── */
.note-card {
  --accent: var(--rust);
  position: fixed;
  left: 0; top: 0;
  width: min(332px, calc(100vw - 28px));
  background:
    linear-gradient(170deg, rgba(255, 253, 246, 0.7), rgba(237, 228, 210, 0) 130px),
    var(--parchment);
  border: 2px solid var(--ink);
  border-left: 6px solid var(--accent);
  padding: 1.2rem 1.45rem 1.3rem;
  z-index: 200;
  box-shadow: 6px 8px 0 rgba(44, 26, 14, 0.16);
  will-change: transform, opacity;
}

.note-card.hidden { display: none; }

/* a small accent "gem" notch on the edge that faces the marker */
.note-card::after {
  content: '';
  position: absolute;
  top: var(--notch-y, 42px);
  width: 13px; height: 13px;
  background: var(--accent);
  border: 2px solid var(--ink);
  transform: translateY(-50%) rotate(45deg);
}
.note-card--right::after { left: -8px; }
.note-card--left::after  { right: -8px; }
/* keep the thick accent edge on the side that faces the marker */
.note-card--left { border-left-width: 2px; border-right: 6px solid var(--accent); }

.note-close {
  position: absolute;
  top: 0.55rem;
  right: 0.7rem;
  background: none;
  border: none;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--ink-light);
  font-family: 'EB Garamond', serif;
  line-height: 1;
  padding: 0.2rem 0.35rem;
  transition: color 0.15s;
}
.note-close:hover { color: var(--ink); }

/* note body typography */
.note-kicker {
  display: block;
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-light);
  margin-bottom: 0.15rem;
}

.note-title {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.7rem;
  line-height: 1.05;
  color: var(--ink);
  margin: 0 0 0.5rem;
  padding-right: 1rem;
}

.note-text {
  font-size: 0.92rem;
  line-height: 1.62;
  color: var(--ink-mid);
  margin: 0;
}

.note-meta {
  margin-top: 0.8rem;
  padding-top: 0.7rem;
  border-top: 1px solid rgba(44, 26, 14, 0.14);
}
.note-meta-row + .note-meta-row { margin-top: 0.55rem; }
.note-meta-k {
  display: block;
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.64rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink);
}
.note-meta-v {
  display: block;
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--ink-mid);
}

.note-links {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}
.note-link {
  font-family: 'Caveat', cursive;
  font-size: 1.1rem;
  color: var(--ink);
  text-decoration: none;
  border-bottom: 1.5px solid var(--accent);
  padding-bottom: 1px;
  transition: color 0.2s;
}
.note-link:hover { color: var(--accent); }

/* ── Note code snippet: monospace, ink on a faint paper tint ──── */
.note-code {
  margin-top: 0.85rem;
  padding: 0.65rem 0.75rem;
  background: rgba(44, 26, 14, 0.055);
  border: 1px solid rgba(44, 26, 14, 0.14);
  border-left: 3px solid var(--accent);
  border-radius: 3px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.note-code code {
  font-family: ui-monospace, 'SFMono-Regular', 'Menlo', 'Consolas', monospace;
  font-size: 0.68rem;
  line-height: 1.55;
  color: var(--ink);
  white-space: pre;
}
.note-code .tok-c { color: var(--ink-light); font-style: italic; }

/* ── Note figure: inline SVG diagram, or a "pinned" framed image ── */
.note-figure { margin: 0.9rem 0 0; padding: 0; }
.note-figure svg { display: block; width: 100%; height: auto; }
.note-figure figcaption {
  margin-top: 0.35rem;
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-size: 0.66rem;
  color: var(--ink-light);
  text-align: center;
}
/* framed variant — a photo/crest pinned like a polaroid, tinted to sit on paper */
.note-figure--framed {
  width: max-content;
  max-width: 100%;
  margin: 0.9rem auto 0.2rem;
  padding: 0.4rem 0.4rem 0.5rem;
  background: #fbf8f1;
  border: 1px solid rgba(44, 26, 14, 0.18);
  box-shadow: 2px 3px 0 rgba(44, 26, 14, 0.12);
  transform: rotate(-1.3deg);
}
.note-figure--framed img,
.note-figure--framed svg {
  display: block;
  width: auto;
  max-width: 100%;
  max-height: 120px;
}

/* ── How-it-works popup (centred, dimmed backdrop) ────────────── */
.help-modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(20, 14, 8, 0.45);
  backdrop-filter: blur(2px);
}
.help-modal.hidden { display: none; }

.help-card {
  position: relative;
  width: min(420px, calc(100vw - 40px));
  background:
    linear-gradient(170deg, rgba(255, 253, 246, 0.7), rgba(237, 228, 210, 0) 130px),
    var(--parchment);
  border: 2px solid var(--ink);
  padding: 1.6rem 1.8rem 1.7rem;
  box-shadow: 8px 10px 0 rgba(44, 26, 14, 0.2);
  animation: noteDrop 0.32s cubic-bezier(0.2, 0.9, 0.25, 1) both;
}

.help-title {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.7rem;
  color: var(--ink);
  margin: 0 0 0.9rem;
  padding-right: 1.2rem;
}

.help-list {
  margin: 0 0 1.3rem;
  padding-left: 1.15rem;
  font-family: 'EB Garamond', serif;
  font-size: 0.95rem;
  line-height: 1.55;
  color: var(--ink-mid);
}
.help-list li { margin-bottom: 0.55rem; }
.help-list li:last-child { margin-bottom: 0; }

.help-gotit {
  display: block;
  margin: 0 auto;
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.1rem;
}

@media (prefers-reduced-motion: reduce) {
  .help-card { animation: none !important; }
}

/* entrance: the slip drops in, its lines rise in a quick stagger */
@keyframes noteDrop {
  from { opacity: 0; transform: translateY(10px) scale(0.96) rotate(-1.2deg); }
  to   { opacity: 1; transform: none; }
}
@keyframes noteRise {
  from { opacity: 0; transform: translateY(7px); }
  to   { opacity: 1; transform: none; }
}
.note-card.note-in { animation: noteDrop 0.42s cubic-bezier(0.2, 0.9, 0.25, 1) both; }
.note-card.note-in .note-kicker { animation: noteRise 0.4s both 0.05s; }
.note-card.note-in .note-title  { animation: noteRise 0.4s both 0.11s; }
.note-card.note-in .note-text   { animation: noteRise 0.4s both 0.17s; }
.note-card.note-in .note-figure { animation: noteRise 0.4s both 0.21s; }
.note-card.note-in .note-code   { animation: noteRise 0.4s both 0.25s; }
.note-card.note-in .note-meta   { animation: noteRise 0.4s both 0.29s; }
.note-card.note-in .note-links  { animation: noteRise 0.4s both 0.31s; }

@media (prefers-reduced-motion: reduce) {
  .note-card.note-in,
  .note-card.note-in * { animation: none !important; }
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 600px) {
  /* the slip becomes a bottom sheet; JS positioning is overridden */
  .note-card {
    left: 0 !important;
    right: 0;
    top: auto !important;
    bottom: 0;
    width: 100%;
    border-left-width: 2px;
    border-bottom: none;
    border-top: 4px solid var(--accent);
    box-shadow: 0 -6px 18px rgba(44, 26, 14, 0.18);
  }
  .note-card::after { display: none; }

  #ui {
    padding: 1rem 1.2rem;
  }

  .ui-hint {
    font-size: 0.8rem;
    bottom: 1.2rem;
    max-width: 90vw;
    white-space: normal;
    text-align: center;
  }

  .legend {
    top: 4rem;
    left: 0.8rem;
    width: 150px;
    padding: 0.5rem 0.5rem 0.4rem;
  }
  .legend-name { font-size: 1.05rem; }

  .citylinks {
    top: 4rem;
    left: 0.8rem;
    width: 150px;
    padding: 0.5rem 0.5rem 0.4rem;
  }
  .citylinks-city { font-size: 1.1rem; }
}

/* Touch devices: finger-sized targets (44px Apple HIG floor) regardless of screen
   width — a tablet is wide but still needs fat buttons. */
@media (pointer: coarse) {
  .ui-btn {
    min-height: 44px;
    padding: 0.55rem 1rem;
    font-size: 1rem;
  }
  .legend-item {
    padding: 0.55rem 0.6rem;
    min-height: 44px;
  }
  .citylinks-item {
    padding: 0.55rem 0.6rem;
    min-height: 44px;
  }
  .note-close {
    width: 44px;
    height: 44px;
    font-size: 1.6rem;
  }
  .note-link {
    display: inline-block;
    padding: 0.5rem 0.2rem;
  }
}

/* ── WebGL fallback ───────────────────────────────────────── */
.webgl-fallback {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  text-align: center;
  color: var(--ink-mid);
  font-family: 'EB Garamond', serif;
  font-size: 1.1rem;
  padding: 2rem;
}
