:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1c232d;
  --line: #2a3340;
  --text: #dde3ea;
  --muted: #8b96a5;
  --accent: #e0a24a;
  --accent-2: #5fa8e8;
  --good: #5cc28a;
  --bad: #e8665f;
  --warn: #e0c04a;
  font-family: "Segoe UI", system-ui, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 14px; overscroll-behavior: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
button, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 12px; cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); color: #1a1206; border-color: var(--accent); font-weight: 600; }
button.danger:hover { border-color: var(--bad); color: var(--bad); }
input {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
}
input:focus { outline: none; border-color: var(--accent-2); }

#boot-error {
  max-width: 720px; margin: 10vh auto; padding: 24px; background: var(--panel);
  border: 1px solid var(--bad); border-radius: 10px; line-height: 1.5;
}

/* ---------- world list ---------- */
#screen-worlds { padding: 40px 24px; max-width: 1100px; margin: 0 auto; }
.brand h1 { margin: 0; font-size: 34px; letter-spacing: 1px; }
.brand p { margin: 4px 0 28px; color: var(--muted); }
.cards { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; }
@media (max-width: 820px) { .cards { grid-template-columns: 1fr; } }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; }
.card h2 { margin: 0 0 14px; font-size: 16px; color: var(--accent); }
.world-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.world-list li {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
.world-list .title { font-weight: 600; font-size: 15px; }
.world-list .meta { grid-column: 1; color: var(--muted); font-size: 12px; }
.world-list .actions { grid-row: 1 / span 2; grid-column: 2; display: flex; gap: 6px; }
form#new-world { display: flex; flex-direction: column; gap: 12px; }
form#new-world label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
form#new-world input { color: var(--text); }
.row { display: flex; gap: 6px; }
.row input { flex: 1; }
.packs { font-size: 12px; color: var(--muted); }
.packs b { color: var(--text); }
.problems {
  margin-top: 14px; padding: 12px; border: 1px solid var(--bad); border-radius: 8px;
  background: #2a1517; font-size: 13px; line-height: 1.45;
}
.problems h4 { margin: 0 0 6px; color: var(--bad); }
.problems code { color: var(--warn); }

/* ---------- game ---------- */
#screen-game { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.world-name { font-weight: 600; color: var(--accent); }
.sep { width: 1px; height: 22px; background: var(--line); }
.date { font-weight: 600; min-width: 190px; }
.time { font-variant-numeric: tabular-nums; color: var(--accent-2); min-width: 46px; }
.icon { padding: 4px 10px; }
#speed { width: 180px; accent-color: var(--accent); }
.speed-label { min-width: 54px; font-variant-numeric: tabular-nums; }
.presets { display: flex; gap: 4px; }
.presets button { padding: 2px 7px; font-size: 12px; }
.presets button.on { border-color: var(--accent); color: var(--accent); }
.jump input { width: 190px; }
.save-state { margin-left: auto; font-size: 12px; color: var(--muted); }
.save-state.err { color: var(--bad); }

.game-main { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) clamp(240px, 26vw, 340px); min-height: 0; }
.map-wrap { position: relative; min-height: 0; min-width: 0; display: flex; flex-direction: column; }
#world-map { flex: 1; width: 100%; min-height: 0; display: block; cursor: grab; touch-action: none; background: #0b0e13; }
#world-map:active { cursor: grabbing; }
.map-toolbar {
  display: flex; gap: 10px; flex-wrap: wrap; align-items: center; padding: 6px 10px;
  background: var(--panel-2); border-bottom: 1px solid var(--line); font-size: 12px;
}
.seg { display: inline-flex; gap: 3px; align-items: center; }
.seg button { padding: 3px 9px; font-size: 12px; }
.seg button.on { border-color: var(--accent); color: var(--accent); }
.seg label { display: inline-flex; gap: 4px; align-items: center; padding: 3px 7px; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; user-select: none; }
.seg label:has(input:checked) { border-color: var(--accent-2); color: var(--accent-2); }
.seg input[type=checkbox] { display: none; }
.hover-info {
  position: absolute; left: 10px; bottom: 10px; pointer-events: none; font-size: 12px;
  background: rgba(8,10,14,0.82); border: 1px solid var(--line); border-radius: 6px; padding: 5px 9px; min-height: 26px;
  font-variant-numeric: tabular-nums;
}
.hover-info:empty { display: none; }
.gen-info table { width: 100%; border-collapse: collapse; }
.gen-info td { padding: 1px 4px; }
.gen-info td:last-child { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.gen-progress {
  position: fixed; inset: 0; z-index: 40; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(10, 12, 16, 0.92);
}
.gen-progress ol { min-width: 360px; line-height: 1.7; color: var(--muted); }
.gen-progress li.done { color: var(--text); }
.gen-progress li.done span { color: var(--good); margin-left: 8px; font-size: 12px; }
.gen-progress li.note { list-style: none; font-size: 12px; color: var(--muted); margin-left: -6px; }
.side { background: var(--panel); border-left: 1px solid var(--line); padding: 12px 10px; overflow: auto; }
.side h3 { margin: 6px 0 8px; font-size: 13px; color: var(--accent); text-transform: uppercase; letter-spacing: 0.5px; }
.side canvas { display: block; width: 100%; border-radius: 6px; border: 1px solid var(--line); }
.info { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 10px 0 14px; font-size: 13px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; font-variant-numeric: tabular-nums; }
.chronicle { list-style: none; margin: 8px 0 0; padding: 0; }
.chronicle li { margin: 2px 0; }
.person-card { position: absolute; right: 12px; top: 12px; width: 260px; padding: 10px 12px; border-radius: 8px; background: rgba(12, 15, 20, 0.82); color: var(--text, #e6e6e6); font-size: 13px; line-height: 1.45; pointer-events: none; }
.person-card .relation { color: var(--accent-2); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
.person-card h4 { margin: 0 0 2px; font-size: 15px; color: var(--accent); }
.person-card .muted { font-size: 12px; }
#village-life .life { list-style: none; margin: 4px 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px; }
#village-life .life button { font-size: 12px; padding: 2px 8px; }
.act-card { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); max-width: min(560px, 92%); padding: 8px 12px; border-radius: 8px; background: rgba(12, 15, 20, 0.82); color: var(--text, #e6e6e6); font-size: 13px; line-height: 1.5; pointer-events: none; }
.act-card .said { color: var(--muted); font-size: 12px; }
.act-card .said div:last-child { color: var(--text, #e6e6e6); }
.act-card .keys { margin-top: 4px; }
.act-card kbd { display: inline-block; min-width: 1.4em; text-align: center; padding: 0 4px; margin-right: 3px; border: 1px solid var(--muted); border-radius: 3px; font-size: 11px; }
.act-card .keys span { margin-right: 12px; white-space: nowrap; }
.person-card .thoughts { font-style: italic; margin: 2px 0; }
.person-card .diary { font-size: 12px; color: var(--muted); margin: 2px 0 4px; max-height: 9em; overflow-y: auto; border-left: 2px solid var(--muted); padding-left: 6px; }
.chronicle li.link { cursor: pointer; border-radius: 3px; }
.chronicle li.link:hover { background: rgba(255, 255, 255, 0.06); }
.chronicle.scroll { max-height: 260px; overflow-y: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; }
.chips button.on { border-color: var(--accent, #e0a24a); color: var(--accent, #e0a24a); }
.chronicle .muted { display: inline-block; min-width: 70px; font-variant-numeric: tabular-nums; }

/* ---------- overlay ---------- */
.overlay {
  position: fixed; top: 54px; right: 8px; z-index: 50; min-width: 380px;
  background: rgba(8, 10, 14, 0.88); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font: 12px/1.45 Consolas, "Cascadia Mono", monospace; pointer-events: none;
}
.overlay table { border-collapse: collapse; width: 100%; }
.overlay td, .overlay th { padding: 1px 6px; text-align: right; white-space: nowrap; }
.overlay th { color: var(--muted); font-weight: normal; }
.overlay td:first-child, .overlay th:first-child { text-align: left; }
.overlay .bar { display: inline-block; height: 7px; background: var(--good); vertical-align: middle; }
.overlay .over { color: var(--bad); }
.overlay .ok { color: var(--good); }
.overlay .dim { color: var(--muted); }
.overlay h5 { margin: 6px 0 2px; color: var(--accent); font-size: 11px; font-weight: normal; text-transform: uppercase; }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--panel-2); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 16px;
}

/* ---------- touch controls (3D) ---------- */
.touch3d { position: absolute; inset: 0; pointer-events: none; }

/* Full screen: pull the whole 3D view out of the page and over everything. The canvas and every
   touch control are children of #view3d, so they all travel with it — nothing else to move. The
   z-index clears the sidebar sheet (30) and the hover card (40) but stays under the debug overlay
   (50) and the toast (60). */
.view3d.fs { position: fixed; inset: 0; z-index: 45; }
/* Top centre: the only part of the view no control or card wants. */
.touch-fs {
  position: absolute; top: calc(6px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  z-index: 20; width: 46px; height: 32px; padding: 0; border-radius: 16px; pointer-events: auto;
  background: rgba(20, 24, 30, 0.55); border: 1px solid rgba(255, 255, 255, 0.3);
  color: var(--text); font-size: 15px; touch-action: manipulation;
}
.touch-fs.on { background: rgba(224, 162, 74, 0.5); border-color: var(--accent); }
.stick {
  position: absolute; left: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); width: 128px; height: 128px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.08); border: 1px solid rgba(255, 255, 255, 0.25); pointer-events: auto; touch-action: none;
}
.stick .knob {
  position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.28); border: 1px solid rgba(255, 255, 255, 0.5);
}
.touch-buttons {
  position: absolute; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; pointer-events: auto;
}
.touch-buttons button {
  width: 56px; height: 56px; border-radius: 50%; font-size: 20px; touch-action: none; user-select: none;
  background: rgba(20, 24, 30, 0.55); border-color: rgba(255, 255, 255, 0.3);
}
.touch-buttons button.held { background: rgba(224, 162, 74, 0.5); }
.touch-speed { position: absolute; right: 12px; top: 12px; display: flex; gap: 8px; pointer-events: auto; }
.touch-speed button { width: 44px; height: 44px; padding: 0; border-radius: 50%; background: rgba(20, 24, 30, 0.55); border-color: rgba(255, 255, 255, 0.3); }
.touch-hint { position: absolute; left: 0; right: 0; bottom: calc(150px + env(safe-area-inset-bottom)); margin: 0; text-align: center; font-size: 12px; color: rgba(255, 255, 255, 0.6); }

/* ---------- 3D view ---------- */
/* touch-action: none is what stops the browser stealing the first drag as a scroll, pinch or
   pull-to-refresh. Without it, putting a finger down on a still screen looks like a page gesture
   and the camera only starts moving once the browser has decided it isn't one. */
.view3d { position: absolute; inset: 0; z-index: 5; background: #000; touch-action: none; }
.view3d-canvas { width: 100%; height: 100%; display: block; cursor: crosshair; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.hud3d {
  position: absolute; left: 10px; top: 10px; pointer-events: none; font-size: 12px; line-height: 1.5;
  background: rgba(8,10,14,0.6); border-radius: 6px; padding: 6px 10px; font-variant-numeric: tabular-nums;
}
.look { position: absolute; left: 50%; top: calc(50% + 14px); transform: translateX(-50%); max-width: 46%; padding: 3px 9px; border-radius: 6px; background: rgba(10,12,16,0.62); color: #f2efe6; font-size: 12.5px; line-height: 1.35; text-align: center; pointer-events: none; }
.crosshair { position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; margin: -3px 0 0 -3px; border-radius: 50%; background: rgba(255,255,255,0.7); pointer-events: none; }
.view-switch button { padding: 4px 12px; }
.view-switch button.on { border-color: var(--accent); color: var(--accent); }
.fly-here { margin: 4px 0 10px; }

/* ---------- phones and small tablets ---------- */
.mobile-only { display: none; }
@media (max-width: 820px) {
  .mobile-only { display: inline-flex; }
  #screen-worlds { padding: 20px 12px; }
  .brand h1 { font-size: 26px; }
  .world-list li { grid-template-columns: 1fr; }
  .world-list .actions { grid-row: auto; grid-column: 1; }

  #screen-game { height: 100dvh; }
  .topbar { gap: 6px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); }
  .topbar .world-name, .topbar .sep, .topbar .presets, .topbar .save-state { display: none; }
  .date { min-width: 0; font-size: 13px; }
  #speed { flex: 1 1 60px; width: auto; min-width: 0; }
  .speed-label { min-width: 0; }
  .jump { flex: 1 1 100%; }
  .jump input { width: 100%; }

  .game-main { grid-template-columns: minmax(0, 1fr); }
  .map-toolbar { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .map-toolbar .seg { flex: none; }
  .seg label { white-space: nowrap; }

  /* The sidebar becomes a bottom sheet. */
  .side {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; height: 58dvh; border-left: none;
    border-top: 1px solid var(--line); border-radius: 14px 14px 0 0; box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.5);
    padding: 34px 14px calc(14px + env(safe-area-inset-bottom)); transform: translateY(105%); transition: transform 0.2s ease;
  }
  .side.open { transform: none; }
  .sheet-close { position: absolute; top: 4px; left: 50%; transform: translateX(-50%); padding: 0 22px; min-height: 26px; font-size: 16px; }

  .person-card { left: 8px; right: 8px; width: auto; top: auto; bottom: calc(160px + env(safe-area-inset-bottom)); }
  .act-card { bottom: calc(160px + env(safe-area-inset-bottom)); }
  .hud3d { font-size: 11px; max-width: calc(100% - 130px); }
  .hover-info { right: 10px; }
  .gen-progress ol { min-width: 0; width: 88vw; }
  .overlay { left: 8px; right: 8px; min-width: 0; }
}
@media (pointer: coarse) {
  button { min-height: 36px; }
  .seg button, .seg label { padding: 6px 10px; }
  .chronicle li { padding: 4px 0; }
}

/* The action wheel (hold E, or hold ✋ on touch): what you can do here, round the crosshair. */
.act-wheel { position: absolute; inset: 0; pointer-events: none; z-index: 6; }
.act-wheel .hub { position: absolute; left: 50%; top: 50%; width: 46px; height: 46px; margin: -23px 0 0 -23px; border-radius: 50%; border: 2px solid rgba(230, 230, 230, 0.5); background: rgba(12, 15, 20, 0.45); }
.act-wheel button { position: absolute; transform: translate(-50%, -50%); pointer-events: auto; max-width: 180px; padding: 6px 10px; border-radius: 16px; border: 1px solid rgba(230, 230, 230, 0.25); background: rgba(12, 15, 20, 0.82); color: var(--text, #e6e6e6); font-size: 13px; line-height: 1.3; cursor: pointer; }
.act-wheel button.on, .act-wheel button:hover { background: rgba(214, 170, 90, 0.92); color: #15120c; border-color: transparent; }
/* A step that takes time fills a bar. */
.act-card .progress { margin: 6px 0 2px; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.15); overflow: hidden; }
.act-card .progress i { display: block; height: 100%; background: rgb(214, 170, 90); transition: width 0.3s linear; }
