:root {
  --ink: #e8e3d4;
  --panel: rgba(18, 24, 16, 0.93);
  --edge: rgba(200, 220, 180, 0.16);
  --oak: #4f7a45;
  --maple: #b0503a;
  --warm: #f0b25c;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #16210f;
  color: var(--ink);
  font: 15px/1.55 ui-serif, Georgia, 'Iowan Old Style', serif;
  cursor: default;
  user-select: none;
}

#stage {
  position: fixed;
  inset: 0;
  display: block;
  filter: saturate(calc(1 - var(--grim, 0) * 0.55)) brightness(calc(1 - var(--grim, 0) * 0.18));
  transition: filter 1.2s linear;
}

body.grim #stage { filter: saturate(0.15) brightness(0.72) contrast(1.05); }

/* ---------- proximity prompt ---------- */
#prompt {
  position: fixed;
  left: 50%;
  bottom: 56px;
  transform: translate(-50%, 14px);
  padding: 10px 18px;
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 10px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s, transform 0.22s;
  backdrop-filter: blur(6px);
}
#prompt.show { opacity: 1; transform: translate(-50%, 0); }
#prompt b { display: block; font-size: 17px; letter-spacing: 0.02em; }
#prompt span { display: block; font-size: 12px; opacity: 0.6; margin-bottom: 5px; }

kbd {
  display: inline-block;
  padding: 1px 7px;
  margin: 0 1px;
  font: 11px/1.6 ui-monospace, monospace;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--edge);
  border-bottom-width: 2px;
  border-radius: 4px;
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 16px;
  left: 16px;
  width: 208px;
  padding: 14px 16px 12px;
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 12px;
  backdrop-filter: blur(6px);
}
#hud h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  opacity: 0.55;
}
#hud label {
  display: block;
  font-size: 11.5px;
  letter-spacing: 0.06em;
  opacity: 0.7;
  margin-bottom: 4px;
}
#hud label span { float: right; opacity: 0.9; font-variant-numeric: tabular-nums; }
.meter {
  height: 7px;
  background: rgba(0, 0, 0, 0.45);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 12px;
}
.meter i {
  display: block;
  height: 100%;
  width: 22%;
  border-radius: 4px;
  transition: width 0.7s cubic-bezier(.2,.7,.3,1), background 0.7s;
}
.lightbar { background: linear-gradient(90deg, #8a6a2e, #ffd98a); }

.badge {
  padding: 5px 9px;
  font-size: 11.5px;
  text-align: center;
  border: 1px dashed var(--edge);
  border-radius: 6px;
  opacity: 0.55;
  margin-bottom: 10px;
}
.badge.on {
  border: 1px solid var(--maple);
  background: rgba(176, 80, 58, 0.22);
  color: #ffbda9;
  opacity: 1;
  font-weight: 600;
}

#hud dl { margin: 0 0 10px; font-size: 12px; }
#hud dl div { display: flex; justify-content: space-between; padding: 1.5px 0; }
#hud dt { opacity: 0.55; }
#hud dd { margin: 0; font-variant-numeric: tabular-nums; }

#minimap {
  width: 100%;
  height: 110px;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 7px;
}
.hint { margin: 9px 0 0; font-size: 10.5px; opacity: 0.45; line-height: 1.9; }

/* ---------- dialogue ---------- */
#dialogue {
  position: fixed;
  top: 0;
  right: 0;
  width: min(430px, 92vw);
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--edge);
  backdrop-filter: blur(10px);
  transform: translateX(103%);
  transition: transform 0.42s cubic-bezier(.2,.8,.25,1);
  user-select: text;
}
#dialogue.open { transform: none; }

#dialogue header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--edge);
}
#d-portrait { flex: none; }
.who { flex: 1; min-width: 0; }
.who h3 { margin: 0; font-size: 19px; font-weight: 600; }
.who p { margin: 2px 0 0; font-size: 11.5px; opacity: 0.55; letter-spacing: 0.03em; }
#d-close {
  flex: none;
  width: 30px; height: 30px;
  font-size: 20px;
  color: var(--ink);
  background: none;
  border: 1px solid var(--edge);
  border-radius: 7px;
  cursor: pointer;
}
#d-close:hover { background: rgba(255,255,255,0.08); }

#d-log {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
}
.msg {
  max-width: 92%;
  padding: 10px 13px;
  border-radius: 12px;
  white-space: pre-wrap;
  animation: rise 0.3s ease;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg-tree {
  align-self: flex-start;
  background: rgba(94, 122, 69, 0.2);
  border: 1px solid rgba(140, 180, 110, 0.2);
  border-bottom-left-radius: 3px;
  font-size: 15.5px;
}
.msg-you {
  align-self: flex-end;
  background: rgba(240, 178, 92, 0.14);
  border: 1px solid rgba(240, 178, 92, 0.24);
  border-bottom-right-radius: 3px;
}
.msg-error { background: rgba(180, 60, 45, 0.2); border-color: rgba(220, 90, 70, 0.35); font-size: 13px; }
.msg.typing:empty::after {
  content: '⋯';
  opacity: 0.5;
  animation: pulse 1.1s infinite;
}
@keyframes pulse { 50% { opacity: 0.15; } }

.sway {
  align-self: center;
  font-size: 11.5px;
  letter-spacing: 0.1em;
  text-transform: lowercase;
  opacity: 0.6;
  font-style: italic;
}

#d-form { display: flex; gap: 8px; padding: 12px 16px 16px; border-top: 1px solid var(--edge); }
#d-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--edge);
  border-radius: 9px;
}
#d-input:focus { outline: none; border-color: rgba(240, 178, 92, 0.5); }
#d-send {
  padding: 0 16px;
  color: #1b2412;
  font: inherit;
  font-weight: 600;
  background: var(--warm);
  border: none;
  border-radius: 9px;
  cursor: pointer;
}
#d-send:disabled, #d-input:disabled { opacity: 0.45; cursor: wait; }

/* ---------- title ---------- */
#title {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at 50% 40%, rgba(30,46,24,0.9), rgba(8,12,6,0.97));
  cursor: pointer;
  transition: opacity 0.9s;
  z-index: 20;
}
#title.gone { opacity: 0; pointer-events: none; }
#title .card { max-width: 480px; padding: 0 28px; text-align: center; }
#title h1 {
  margin: 0;
  font-size: 68px;
  font-weight: 400;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}
.verse { margin: 10px 0 26px; font-style: italic; opacity: 0.72; font-size: 17px; }
/* Balanced wrapping: this is display copy in a narrow column, where the default
   greedy wrap leaves a one-word last line ("owed.") at some widths. Balancing
   evens the lines out and re-does it at every viewport, so the phone gets the
   same treatment as the desktop. Unsupported browsers just wrap as before. */
.blurb { margin: 0 0 20px; font-size: 14.5px; line-height: 1.75; opacity: 0.72; text-wrap: balance; }
#t-status { margin: 22px 0 4px; font: 12px/1.5 ui-monospace, monospace; opacity: 0.6; }
#t-status.ok { color: #9ec97e; }
#t-status.bad { color: #e08b72; }
.go { font-size: 12px; letter-spacing: 0.2em; text-transform: uppercase; opacity: 0.4; animation: pulse 2.4s infinite; }

/* ---------- endings ---------- */
.ending {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 40px;
  text-align: center;
  z-index: 15;
  pointer-events: none;
}
.ending.show { display: flex; }
.ending.law { background: linear-gradient(rgba(10,8,6,0.5), rgba(10,8,6,0.82)); }
.ending.dappled { background: radial-gradient(ellipse at 50% 30%, rgba(255,231,170,0.18), rgba(10,16,8,0.7)); }
.ending h1 {
  margin: 0 0 18px;
  font-size: 42px;
  font-weight: 400;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  opacity: 0.9;
}
.ending .lines { margin: 0; font-size: 19px; line-height: 2; font-style: italic; }
.ending .line { display: block; min-height: 1.9em; }
.ending .final { margin-top: 26px; font-size: 23px; font-style: normal; letter-spacing: 0.02em; }
.ending.cutting { animation: shake 0.5s infinite; }
@keyframes shake {
  25% { transform: translate(1.5px, -1px); }
  75% { transform: translate(-1.5px, 1px); }
}
.restart {
  margin-top: 34px;
  padding: 11px 22px;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  letter-spacing: 0.1em;
  background: none;
  border: 1px solid var(--edge);
  border-radius: 9px;
  cursor: pointer;
  pointer-events: auto;
  animation: rise 1s ease;
}
.restart:hover { background: rgba(255,255,255,0.08); }

/* ==========================================================================
   Touch + small screens
   ========================================================================== */

/* Stop the browser treating drags on the forest as scroll/pinch gestures. */
#stage { touch-action: none; }
html, body { overscroll-behavior: none; }

/* The thumbstick appears wherever a thumb lands, so it costs no screen space
   and works equally well left- or right-handed. */
#stick-base, #stick-knob {
  position: fixed;
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: opacity 0.18s;
  z-index: 8;
}
#stick-base {
  width: 116px; height: 116px;
  background: rgba(232, 227, 212, 0.07);
  border: 1.5px solid rgba(232, 227, 212, 0.22);
}
#stick-knob {
  width: 52px; height: 52px;
  background: rgba(240, 178, 92, 0.34);
  border: 1.5px solid rgba(240, 178, 92, 0.6);
}
#stick-base.on, #stick-knob.on { opacity: 1; }

#hud-toggle { display: none; }

/* The prompt is a button. Always clickable: mouse users get click-to-knock
   too, and it avoids a dead first tap before touch mode is detected. */
#prompt { pointer-events: auto; cursor: pointer; }
body.touch #prompt { padding: 12px 22px; }
body.touch #prompt em {
  display: inline-block;
  margin-top: 2px;
  padding: 5px 14px;
  font-style: normal;
  font-size: 13px;
  letter-spacing: 0.08em;
  color: #1b2412;
  background: var(--warm);
  border-radius: 8px;
}

@media (max-width: 760px) {
  /* Ledger collapses to a strip across the top, clear of notches. */
  #hud {
    top: 0; left: 0; right: 0;
    width: auto;
    padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
    border: none;
    border-bottom: 1px solid var(--edge);
    border-radius: 0;
  }
  #hud h2, #hud dl, .hint { display: none; }
  #hud label { margin-bottom: 3px; font-size: 10.5px; }
  .meter { margin-bottom: 7px; }
  .badge { margin-bottom: 0; padding: 3px 8px; font-size: 10.5px; }

  #hud-toggle {
    display: block;
    position: absolute;
    top: calc(6px + env(safe-area-inset-top));
    right: 10px;
    width: 34px; height: 34px;
    color: var(--ink);
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid var(--edge);
    border-radius: 8px;
    cursor: pointer;
  }
  /* a little map glyph */
  #hud-toggle::before {
    content: '';
    display: block;
    width: 14px; height: 11px;
    margin: 0 auto;
    background: currentColor;
    opacity: 0.7;
    clip-path: polygon(0 15%, 33% 0, 66% 15%, 100% 0, 100% 85%, 66% 100%, 33% 85%, 0 100%);
  }

  #minimap { display: none; height: 96px; margin-top: 8px; }
  #hud.expanded #minimap { display: block; }
  #hud.expanded dl { display: block; margin-top: 6px; }

  /* Conversations take the whole screen — a side panel is unreadable here. */
  #dialogue {
    width: 100%;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #dialogue header { padding: calc(12px + env(safe-area-inset-top)) 14px 10px; }
  .who h3 { font-size: 17px; }
  .msg { max-width: 96%; font-size: 15px; }
  #d-input { font-size: 16px; } /* 16px stops iOS zooming the page on focus */
  #d-close { width: 38px; height: 38px; }

  /* Keep the knock button clear of the thumb zone. The bottom ~110px is where
     the steering thumb lives; a button there gets pressed by accident. */
  #prompt { bottom: calc(112px + env(safe-area-inset-bottom)); max-width: 84vw; }

  #title .card { padding: 0 22px; }
  #title h1 { font-size: 44px; }
  .verse { font-size: 15px; margin-bottom: 18px; }
  .blurb { font-size: 13.5px; }

  .ending { padding: 24px; }
  .ending h1 { font-size: 30px; }
  .ending .lines { font-size: 16px; line-height: 1.85; }
  .ending .final { font-size: 19px; }
}

/* Landscape phones are short — claw back vertical space. Deliberately not
   gated on pointer:coarse, since some mobile browsers report otherwise and a
   short desktop window benefits identically. */
@media (max-height: 460px) {
  #hud label, #hud .badge { display: none; }
  #hud { padding-bottom: 4px; }
  #title h1 { font-size: 34px; }
  .blurb { display: none; }
}

/* ---------- saving: resume / start over ---------- */
#t-resume { margin: 18px 0 6px; }
#t-resume .found {
  margin: 0 0 14px;
  font-size: 13.5px;
  line-height: 1.7;
  opacity: 0.78;
  text-wrap: balance; /* same reason as .blurb — its length varies with the save */
}
#t-resume .found b { color: var(--warm); font-weight: 600; }

#t-continue, #t-new, .ghost {
  display: inline-block;
  margin: 4px 5px;
  padding: 11px 20px;
  font: inherit;
  font-size: 13.5px;
  letter-spacing: 0.04em;
  border-radius: 9px;
  cursor: pointer;
}
#t-continue {
  color: #1b2412;
  background: var(--warm);
  border: 1px solid var(--warm);
  font-weight: 600;
}
#t-continue:hover { filter: brightness(1.08); }

.ghost {
  color: var(--ink);
  background: none;
  border: 1px solid var(--edge);
}
.ghost:hover { background: rgba(255, 255, 255, 0.08); }

/* the in-game "leave this forest" control */
.ghost.small {
  display: block;
  width: 100%;
  margin: 10px 0 0;
  padding: 7px 10px;
  font-size: 11px;
  letter-spacing: 0.08em;
  opacity: 0.5;
}
.ghost.small:hover { opacity: 1; }
.ghost.small.armed {
  opacity: 1;
  color: #ffbda9;
  border-color: var(--maple);
  background: rgba(176, 80, 58, 0.2);
}

/* ---------- sound toggle ---------- */
/* Sits above the title card (z-index 20) so the forest can be silenced before
   it ever makes a sound. */
#sound {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 34px; height: 34px;
  padding: 0;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 8px;
  opacity: 0.45;
  cursor: pointer;
  transition: opacity 0.2s;
  z-index: 21;
}
#sound:hover { opacity: 1; }
#sound svg {
  display: block;
  width: 22px; height: 18px;
  margin: 0 auto;
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
}
#sound .waves { fill: none; }
#sound .slash { display: none; }
#sound.off .waves { display: none; }
#sound.off .slash { display: block; }

@media (max-width: 760px) {
  /* the ledger is a strip; the restart control lives with the map */
  #restart { display: none; }
  #hud.expanded #restart { display: block; }
  #t-continue, #t-new { display: block; width: 100%; margin: 6px 0; padding: 13px 20px; }

  /* tuck in beside the map button on the strip */
  #sound {
    top: calc(6px + env(safe-area-inset-top));
    right: 52px;
    opacity: 1;
  }
}
