:root {
  --bg: #0c1116;
  --bg-elevated: #141a21;
  --bg-subtle: #1c242e;
  --bg-hover: #24303c;
  --fg: #e8e4dc;
  --fg-muted: #a39e94;
  --fg-subtle: #6f6a62;
  --border: color-mix(in oklab, var(--fg) 12%, transparent);
  --border-strong: color-mix(in oklab, var(--fg) 22%, transparent);
  --accent: #4d8a86;
  --accent-fg: #0c1116;
  --danger: #b45a4a;
  --warn: #b08948;
  --ok: #6a8f6e;
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-body: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --text-xs: 0.72rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --motion-quick: 150ms;
  --motion-fast: 250ms;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --hud-pad: 16px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; width: 100%;
  background: var(--bg); color: var(--fg); font-family: var(--font-body);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

button, [role="button"] { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
button { font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
.tabular { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

#world {
  position: fixed; inset: 0; width: 100%; height: 100%;
  display: block; background: #0a1014; touch-action: none;
}

/* ----- Title ----- */
#title {
  position: fixed; inset: 0; z-index: 40;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  background:
    radial-gradient(1200px 600px at 50% 18%, #1a2830 0%, transparent 55%),
    linear-gradient(180deg, #0c1116 0%, #0a1218 100%);
  padding: max(24px, env(safe-area-inset-top, 0px)) 16px max(32px, env(safe-area-inset-bottom, 0px));
  text-align: center;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#title.hidden { display: none !important; }
#title .brand-sub {
  margin: 6px 0 0;
  font-size: 0.95rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}
@media (min-height: 780px) {
  #title { justify-content: center; }
}
#title .mark {
  width: 56px; height: 56px; margin-bottom: var(--space-4);
}
#title h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(3rem, 8vw, 5.5rem);
  letter-spacing: -0.04em;
  line-height: 0.95;
  margin: 0;
}
#title h1 span {
  display: block;
  font-size: 0.32em;
  letter-spacing: 0.42em;
  font-weight: 500;
  color: var(--fg-muted);
  margin-top: var(--space-3);
  text-indent: 0.42em;
}
#title .lede {
  max-width: 38rem;
  color: var(--fg-muted);
  font-size: var(--text-base);
  line-height: 1.55;
  margin: var(--space-5) 0 var(--space-6);
}
#title .facts {
  display: flex; gap: var(--space-6); flex-wrap: wrap; justify-content: center;
  margin-bottom: var(--space-6);
  color: var(--fg-subtle);
  font-size: var(--text-sm);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
#title .facts b { color: var(--fg); font-weight: 600; display: block; text-transform: none; letter-spacing: 0; font-size: var(--text-base); margin-top: 4px; }
#title .row { display: flex; gap: var(--space-3); flex-wrap: wrap; justify-content: center; }
#boot {
  color: var(--fg-muted); font-size: var(--text-sm); min-height: 1.4em; margin-top: var(--space-4);
}

/* ----- Buttons ----- */
.btn {
  appearance: none; border: 1px solid var(--border-strong);
  background: var(--bg-subtle); color: var(--fg);
  border-radius: 999px; padding: 10px 18px;
  font-size: var(--text-sm); font-weight: 600;
  min-height: 44px;
  transition: background var(--motion-quick) var(--ease-out), transform var(--motion-quick) var(--ease-out);
}
.btn:hover { background: var(--bg-hover); }
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--fg); color: var(--accent-fg); border-color: var(--fg);
}
.btn-primary:hover { background: #f3efe6; }
.btn-ghost { background: transparent; }
.btn-danger { border-color: color-mix(in oklab, var(--danger) 50%, var(--border)); color: #e8c4bc; }
.btn-sm { min-height: 36px; padding: 6px 12px; border-radius: var(--radius-sm); }

/* ----- HUD ----- */
#hud { pointer-events: none; position: fixed; inset: 0; z-index: 10; }
#hud > * { pointer-events: auto; }
#topbar {
  position: absolute; top: calc(var(--hud-pad) + var(--safe-t)); left: var(--hud-pad); right: var(--hud-pad);
  display: flex; gap: var(--space-2); align-items: stretch; justify-content: space-between;
}
.panel {
  background: #141a21;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 40px rgba(0,0,0,0.45);
}
.stats {
  display: flex; gap: 0; padding: 10px 6px;
  overflow-x: auto; max-width: min(100%, 920px);
}
.stat {
  padding: 4px 14px; border-right: 1px solid var(--border);
  min-width: 88px;
}
.stat:last-child { border-right: 0; }
.stat .k {
  font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--fg-subtle); font-weight: 600;
}
.stat .v {
  font-family: var(--font-mono); font-size: 13px; margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.stat.bad .v { color: #e0a399; }
.stat.ok .v { color: #b5cbb4; }
.clock-wrap { padding: 10px 16px; display: flex; flex-direction: column; justify-content: center; min-width: 168px; }
.clock-wrap .date { font-family: var(--font-display); font-size: 1.05rem; font-weight: 600; letter-spacing: -0.02em; }
.clock-wrap .meta { color: var(--fg-muted); font-size: var(--text-xs); margin-top: 2px; }

#toolbar {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--hud-pad) + var(--safe-b));
  display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; padding: 6px;
  max-width: calc(100% - 96px); max-height: 132px; overflow-x: auto; overflow-y: auto;
  transition: opacity var(--motion-fast) var(--ease-out), transform var(--motion-fast) var(--ease-out);
}
#hud.tools-hidden #toolbar { opacity: 0; pointer-events: none; transform: translateX(-50%) translateY(20px); }
#toolbar-toggle {
  position: absolute; left: var(--hud-pad);
  bottom: calc(var(--hud-pad) + var(--safe-b));
  min-width: 44px; min-height: 44px; border-radius: var(--radius-md);
  font-size: 18px; line-height: 1; display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--fg);
}
#hud.tools-hidden #toolbar-toggle { color: var(--fg-muted); }
.tool {
  appearance: none; border: 0; background: transparent; color: var(--fg-muted);
  min-width: 64px; min-height: 56px; border-radius: var(--radius-md);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; font-weight: 600;
}
.tool svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.6; }
.tool:hover, .tool.active { background: var(--bg-hover); color: var(--fg); }
.tool.active { color: #d5ecea; }

#speed {
  position: absolute; right: var(--hud-pad);
  bottom: calc(var(--hud-pad) + var(--safe-b) + 72px);
  display: flex; gap: 4px; padding: 6px;
}
#speed button { min-width: 44px; min-height: 36px; border-radius: var(--radius-sm); }

#toast {
  position: absolute; top: calc(var(--hud-pad) + var(--safe-t) + 78px); left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 10px 16px;
  font-size: var(--text-sm); max-width: min(520px, 90vw);
  opacity: 0; pointer-events: none;
  transition: opacity var(--motion-fast) var(--ease-out), transform var(--motion-fast) var(--ease-out);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

#inspect, #sheet {
  position: absolute; top: calc(var(--hud-pad) + var(--safe-t) + 78px);
  right: var(--hud-pad);
  width: min(360px, calc(100vw - 32px));
  max-height: calc(100vh - 200px);
  overflow: auto;
  padding: var(--space-4);
  border-radius: var(--radius-xl);
}
#inspect h2, #sheet h2 {
  font-family: var(--font-display); font-size: 1.25rem; font-weight: 600;
  letter-spacing: -0.02em; margin: 0 0 4px;
}
.muted { color: var(--fg-muted); font-size: var(--text-sm); line-height: 1.45; }
.tiny { color: var(--fg-subtle); font-size: var(--text-xs); letter-spacing: 0.08em; text-transform: uppercase; font-weight: 600; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); }
.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
.meter {
  height: 4px; background: var(--bg-subtle); border-radius: 99px; overflow: hidden; margin-top: 6px;
}
.meter > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.list-item {
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 8px; border-radius: 99px; border: 1px solid var(--border);
  font-size: 11px; color: var(--fg-muted);
}
.chip.on { border-color: color-mix(in oklab, var(--accent) 50%, var(--border)); color: #c5e0dd; }

label.field { display: flex; flex-direction: column; gap: 6px; font-size: var(--text-xs); color: var(--fg-muted); }
input[type="range"] { width: 100%; accent-color: var(--accent); }
select, input[type="number"] {
  background: var(--bg-subtle); color: var(--fg); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 10px; min-height: 40px; font: inherit;
}

#minimap {
  position: absolute; left: var(--hud-pad);
  bottom: calc(var(--hud-pad) + var(--safe-b) + 72px);
  width: 148px; height: 92px; border-radius: var(--radius-md);
  overflow: hidden; border: 1px solid var(--border);
}
#minimap canvas { width: 100%; height: 100%; display: block; }

#legend {
  position: absolute; left: calc(var(--hud-pad) + 160px);
  bottom: calc(var(--hud-pad) + var(--safe-b) + 72px);
  padding: 8px 10px; font-size: 10px; color: var(--fg-subtle);
  letter-spacing: 0.04em; text-transform: uppercase;
  display: none;
}

/* ----- Modal ----- */
#modal-bg {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(8,10,12,0.62);
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-5);
}
.modal {
  width: min(520px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: var(--space-5);
  max-height: 85vh; overflow: auto;
}
.modal h2 { font-family: var(--font-display); margin: 0 0 var(--space-3); font-size: 1.6rem; letter-spacing: -0.03em; }
.choices { display: flex; flex-direction: column; gap: 8px; margin-top: var(--space-4); }
.choice {
  text-align: left; width: 100%;
  padding: 12px 14px; border-radius: var(--radius-md);
  border: 1px solid var(--border); background: var(--bg-subtle); color: var(--fg);
}
.choice:hover { background: var(--bg-hover); }
.choice small { display: block; color: var(--fg-muted); margin-top: 4px; }

.tabs { display: flex; gap: 4px; margin: 8px 0 12px; flex-wrap: wrap; }
.tab {
  appearance: none; border: 1px solid var(--border); background: transparent;
  color: var(--fg-muted); border-radius: 99px; padding: 6px 10px; font-size: 11px;
  letter-spacing: 0.06em; text-transform: uppercase; font-weight: 600; min-height: 32px;
}
.tab.active { background: var(--fg); color: var(--accent-fg); border-color: var(--fg); }

.close-x {
  appearance: none; border: 0; background: transparent; color: var(--fg-muted);
  min-width: 36px; min-height: 36px; border-radius: var(--radius-sm); font-size: 18px;
}

@media (max-width: 720px) {
  .stats .stat:nth-child(n+5) { display: none; }
  #minimap, #legend { display: none; }
  #toolbar {
    max-width: calc(100vw - 16px);
    max-height: 120px;
    left: 8px; right: 8px; transform: none;
    justify-content: flex-start;
  }
  #hud.tools-hidden #toolbar { transform: translateY(24px); }
  #inspect, #sheet {
    top: auto; bottom: calc(140px + var(--safe-b)); right: 8px; left: 8px; width: auto;
    max-height: 52vh; border-radius: var(--radius-lg);
  }
  #speed { bottom: calc(var(--hud-pad) + var(--safe-b) + 128px); }
  #toolbar-toggle { bottom: calc(var(--hud-pad) + var(--safe-b) + 128px); left: 8px; }
  .clock-wrap { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* --- Enhanced life & HUD polish --- */
#toolbar {
  flex-wrap: wrap;
  gap: 4px;
  max-width: min(760px, 94vw);
  justify-content: center;
  z-index: 12;
}
#topbar, #speed { z-index: 14; }
/* Panels always sit above the toolbar and are fully usable */
#inspect, #sheet { z-index: 26; }
#toolbar-toggle { z-index: 27; }
.tool {
  min-width: 64px;
  transition: background 0.15s var(--ease-out), transform 0.12s var(--ease-out);
}
.tool:hover { transform: translateY(-1px); }
.tool.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
#topbar .stats {
  backdrop-filter: blur(12px);
  box-shadow: 0 8px 32px rgba(0,0,0,0.35);
}
#inspect, #sheet {
  backdrop-filter: blur(14px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.45);
  border: 1px solid var(--border-strong);
}
.chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  background: var(--bg-hover);
  font-size: 11px;
  letter-spacing: 0.04em;
}
.meter {
  height: 6px;
  background: var(--bg-subtle);
  border-radius: 99px;
  overflow: hidden;
  margin-top: 4px;
}
.meter i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #6ab0a8);
  border-radius: 99px;
}
#toast.show {
  animation: toastIn 0.25s var(--ease-out);
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.list-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: 0; }

/* Mayor selection & oath */
.mayor-pick { max-width: 720px; margin: 20px auto 8px; }
.mayor-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.mayor-card {
  appearance: none; text-align: left; padding: 14px 16px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elevated); color: var(--fg);
  cursor: pointer; transition: border-color .15s, background .15s, transform .12s;
}
.mayor-card:hover { background: var(--bg-hover); transform: translateY(-1px); }
.mayor-card.selected { border-color: var(--accent); background: color-mix(in oklab, var(--accent) 18%, var(--bg-elevated)); }
.mayor-card strong { display: block; font-size: 1.05rem; margin-bottom: 6px; }
.mayor-card span { display: block; font-size: 0.82rem; color: var(--fg-muted); line-height: 1.35; }
.oath-panel {
  max-width: 560px; margin: 16px auto; padding: 20px 22px; border-radius: 16px;
  background: var(--bg-elevated); border: 1px solid var(--border-strong); text-align: left;
}
.oath-text {
  font-family: var(--font-display); font-size: 1.05rem; line-height: 1.55;
  color: var(--fg); border-left: 3px solid var(--accent); padding-left: 14px; margin: 0;
}

/* ===== v1.5.0 Enhancements: SimCity-grade GUI ===== */
:root {
  --panel-shadow: 0 8px 32px rgba(0,0,0,0.45), 0 0 0 1px rgba(255,255,255,0.04);
  --accent-police: #3a6aa8;
  --accent-fire: #c44a3a;
  --accent-ems: #2a9a6a;
  --accent-transit: #c8a02a;
  --accent-encamp: #a86a2a;
  --danger: #c43a3a;
  --ok: #3a9a6a;
}

#hud .panel {
  box-shadow: var(--panel-shadow);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

/* Data Toggle Panel */
#data-panel {
  position: absolute;
  top: 72px;
  right: 16px;
  width: 280px;
  max-height: calc(100vh - 160px);
  overflow-y: auto;
  z-index: 40;
  transition: transform 0.25s var(--ease-out), opacity 0.2s;
}
#data-panel.hidden {
  transform: translateX(110%);
  opacity: 0;
  pointer-events: none;
}
#data-panel h3 {
  margin: 0 0 10px;
  font-size: 0.85rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--fg-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#data-panel .toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
#data-panel .toggle-row:last-child { border-bottom: 0; }
.toggle-switch {
  width: 36px; height: 20px; border-radius: 99px;
  background: var(--bg-subtle); position: relative; cursor: pointer;
  transition: background 0.15s;
}
.toggle-switch.on { background: var(--accent); }
.toggle-switch i {
  position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: left 0.15s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.toggle-switch.on i { left: 18px; }

/* Ward Story Panel */
#ward-story {
  position: absolute;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  width: min(520px, 92vw);
  z-index: 45;
  transition: opacity 0.25s, transform 0.25s;
}
#ward-story.hidden { opacity: 0; pointer-events: none; transform: translateX(-50%) translateY(20px); }
#ward-story .story-head {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;
}
#ward-story .story-choices { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
#ward-story .story-choices .btn { flex: 1; min-width: 120px; }

/* Encampment Crisis Modal styling */
.encamp-choices { display: grid; gap: 10px; margin-top: 14px; }
.encamp-choice {
  text-align: left; padding: 12px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg-subtle); cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.encamp-choice:hover { border-color: var(--accent); background: var(--bg-hover); }
.encamp-choice strong { display: block; margin-bottom: 4px; }
.encamp-choice span { font-size: 0.82rem; color: var(--fg-muted); line-height: 1.4; }

/* Resident detail card */
.resident-card {
  padding: 12px; border-radius: 10px; background: var(--bg-subtle); margin-top: 8px;
}
.resident-card .rc-row { display: flex; justify-content: space-between; font-size: 0.85rem; padding: 3px 0; }
.resident-card .rc-bar { height: 5px; background: var(--bg); border-radius: 99px; margin-top: 2px; overflow: hidden; }
.resident-card .rc-bar i { display: block; height: 100%; border-radius: 99px; }

/* Service legend */
#service-legend {
  position: absolute;
  left: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom,0px) + 180px);
  padding: 8px 12px;
  font-size: 0.75rem;
  z-index: 30;
  display: flex; flex-direction: column; gap: 4px;
}
#service-legend .leg { display: flex; align-items: center; gap: 6px; }
#service-legend .dot { width: 10px; height: 10px; border-radius: 50%; }

/* Floating action buttons */
#fab-row {
  position: absolute;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom,0px) + 80px);
  display: flex; flex-direction: column; gap: 8px; z-index: 35;
}
#fab-row button {
  width: 44px; height: 44px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elevated);
  color: var(--fg); cursor: pointer; font-size: 1.1rem;
  box-shadow: var(--panel-shadow); transition: background 0.15s, transform 0.12s;
}
#fab-row button:hover { background: var(--bg-hover); transform: scale(1.05); }
#fab-row button.active { border-color: var(--accent); background: color-mix(in oklab, var(--accent) 20%, var(--bg-elevated)); }

/* Toolbar improvements */
#toolbar {
  display: flex; flex-wrap: wrap; gap: 4px; max-width: 420px;
  transition: opacity 0.2s, transform 0.2s;
}
#toolbar.collapsed { opacity: 0; pointer-events: none; transform: translateY(12px); }
.tool {
  min-width: 64px; padding: 8px 6px; font-size: 0.72rem;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tool svg, .tool .ico { width: 20px; height: 20px; }

/* Better inspect panel */
#inspect {
  max-width: 320px; max-height: 70vh; overflow-y: auto;
  transition: opacity 0.2s, transform 0.2s;
}
#inspect.hidden { opacity: 0; pointer-events: none; transform: scale(0.96); }

/* Sheet (side panels) */
#sheet {
  position: absolute; top: 72px; left: 16px; width: 300px;
  max-height: calc(100vh - 160px); overflow-y: auto; z-index: 40;
  transition: opacity 0.2s, transform 0.2s;
}
#sheet.hidden { opacity: 0; pointer-events: none; transform: translateX(-20px); }

/* Bus route overlay colors */
.bus-route-label {
  font-size: 9px; font-weight: 700; letter-spacing: 0.04em;
  fill: #fff; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}

/* Night mode subtle */
body.night #world { filter: brightness(0.82) saturate(0.9); }
body.night .panel { background: color-mix(in oklab, var(--bg-elevated) 90%, #0a0e14); }

/* Responsive */
@media (max-width: 720px) {
  #data-panel { width: 240px; top: 64px; right: 8px; }
  #sheet { width: 260px; left: 8px; }
  #fab-row { right: 8px; }
}


/* v1.7 mobile + unrest */
@media (max-width: 720px) {
  #toolbar { max-width: calc(100vw - 24px); max-height: 40vh; overflow-y: auto; }
  #fab-row { bottom: calc(12px + env(safe-area-inset-bottom,0px) + 64px); right: 8px; }
  #fab-row button { width: 40px; height: 40px; font-size: 1rem; }
  #sheet, #data-panel { width: min(300px, calc(100vw - 16px)); max-height: 55vh; }
  #inspect { max-width: min(300px, calc(100vw - 16px)); }
  #topbar { flex-wrap: wrap; gap: 6px; }
  #topbar .stats { font-size: 0.78rem; }
  .tool { min-width: 56px; padding: 6px 4px; font-size: 0.68rem; }
}
#fab-row { max-height: 50vh; overflow-y: auto; }
body.tools-hidden #toolbar { display: none; }


/* ===== v1.8 Night Mayor — scroll, brand, mobile ===== */
#sheet, #inspect, #data-panel, #ward-story, .modal {
  overflow-x: hidden !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  max-height: min(75vh, calc(100dvh - 120px)) !important;
}
#sheet {
  max-width: min(340px, calc(100vw - 16px));
}
#inspect {
  max-width: min(340px, calc(100vw - 16px));
}
#data-panel {
  max-width: min(300px, calc(100vw - 16px));
  max-height: min(70vh, calc(100dvh - 100px)) !important;
}
#toolbar {
  max-height: min(160px, 28vh) !important;
  overflow-x: auto !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
}
#fab-row {
  max-height: min(50vh, 320px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.modal {
  max-height: min(85vh, calc(100dvh - 48px)) !important;
  overflow-y: auto !important;
}
#modal-bg {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  align-items: flex-start;
  padding: 24px 12px;
}
#modal-bg .modal {
  margin: auto;
}

.powered-by {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  z-index: 50;
  padding: 6px 12px calc(6px + env(safe-area-inset-bottom, 0px));
  text-align: center;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  color: var(--fg-muted);
  background: linear-gradient(transparent, rgba(8,12,18,0.92) 30%);
  pointer-events: none;
}
.powered-by a {
  pointer-events: auto;
  color: var(--accent);
  text-decoration: none;
}
.powered-by a:hover { text-decoration: underline; }

/* When game is running, keep powered-by subtle */
#title.hidden ~ #powered-by,
body.playing #powered-by {
  opacity: 0.75;
  font-size: 0.62rem;
  padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
}

#title .mayor-pick {
  max-width: min(720px, 100%);
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}
#title .mayor-cards {
  max-height: none;
}
#title .oath-panel {
  max-width: min(560px, 100%);
  overflow: visible;
}

@media (max-width: 720px) {
  #title h1 { font-size: clamp(2.4rem, 12vw, 3.2rem); }
  #title .lede { font-size: 0.92rem; margin: 12px 0 16px; padding: 0 4px; }
  #title .facts { gap: 12px; margin-bottom: 16px; }
  #title .mayor-cards { grid-template-columns: 1fr; }
  #topbar { max-width: 100%; overflow-x: auto; }
  #speed { left: 8px; right: auto; }
  #mini { display: none; } /* free vertical space on phones */
  #toolbar-toggle { left: 8px; }
  #hud { /* ensure children can scroll */ }
  .powered-by { font-size: 0.6rem; }
}

@media (max-height: 700px) {
  #title { justify-content: flex-start; padding-top: 16px; }
  #title .mark { width: 40px; height: 40px; margin-bottom: 8px; }
}

/* Prevent canvas from eating scroll on title */
#title.hidden { pointer-events: none; }


/* ===== v1.8.1 critical scroll + crash fix ===== */
#title {
  position: fixed !important;
  inset: 0 !important;
  z-index: 40 !important;
  overflow-x: auto !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior: contain !important;
  justify-content: flex-start !important;
  align-items: center !important;
  padding: max(12px, env(safe-area-inset-top, 0px)) 12px max(48px, env(safe-area-inset-bottom, 0px)) !important;
  max-height: 100dvh !important;
  max-width: 100vw !important;
  box-sizing: border-box !important;
}
#title > * {
  max-width: min(720px, 100%);
  flex-shrink: 0;
}
#title .mayor-pick,
#title .oath-panel,
#title .facts,
#title .lede,
#title .row {
  width: 100%;
  max-width: min(720px, 100%);
  box-sizing: border-box;
}
#title .mayor-cards {
  display: grid !important;
  grid-template-columns: 1fr !important;
  gap: 8px !important;
  width: 100%;
}
@media (min-width: 560px) {
  #title .mayor-cards {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) !important;
  }
}

/* Always-visible scrollbars where supported */
#title,
#sheet,
#inspect,
#data-panel,
#toolbar,
#fab-row,
.modal,
#modal-bg {
  scrollbar-width: thin;
  scrollbar-color: var(--accent, #4d8a86) rgba(0,0,0,0.25);
}
#title::-webkit-scrollbar,
#sheet::-webkit-scrollbar,
#inspect::-webkit-scrollbar,
#data-panel::-webkit-scrollbar,
#toolbar::-webkit-scrollbar,
.modal::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
#title::-webkit-scrollbar-thumb,
#sheet::-webkit-scrollbar-thumb,
#inspect::-webkit-scrollbar-thumb,
#data-panel::-webkit-scrollbar-thumb,
#toolbar::-webkit-scrollbar-thumb,
.modal::-webkit-scrollbar-thumb {
  background: color-mix(in oklab, var(--accent, #4d8a86) 70%, #fff);
  border-radius: 99px;
}
#title::-webkit-scrollbar-track,
#sheet::-webkit-scrollbar-track,
#inspect::-webkit-scrollbar-track {
  background: rgba(0,0,0,0.2);
}

#sheet, #inspect, #data-panel {
  overflow-x: auto !important;
  overflow-y: auto !important;
  max-height: min(72dvh, calc(100dvh - 100px)) !important;
  max-width: min(360px, calc(100vw - 12px)) !important;
  -webkit-overflow-scrolling: touch !important;
}
#toolbar {
  overflow-x: auto !important;
  overflow-y: auto !important;
  max-width: calc(100vw - 24px) !important;
  max-height: min(160px, 30dvh) !important;
  -webkit-overflow-scrolling: touch !important;
}
#fab-row {
  overflow-x: hidden !important;
  overflow-y: auto !important;
  max-height: min(45dvh, 280px) !important;
  -webkit-overflow-scrolling: touch !important;
}
#modal-bg {
  overflow-x: auto !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  align-items: flex-start !important;
  padding: 16px 8px calc(16px + env(safe-area-inset-bottom,0px)) !important;
}
.modal {
  overflow-x: auto !important;
  overflow-y: auto !important;
  max-height: min(88dvh, calc(100dvh - 32px)) !important;
  max-width: min(520px, calc(100vw - 16px)) !important;
  -webkit-overflow-scrolling: touch !important;
}
#topbar {
  overflow-x: auto !important;
  max-width: 100vw !important;
  -webkit-overflow-scrolling: touch;
}
#powered-by {
  z-index: 45;
  max-width: 100vw;
  overflow-x: auto;
  white-space: nowrap;
}

@media (max-width: 720px) {
  #title h1 { font-size: clamp(2rem, 11vw, 2.8rem) !important; }
  #title .brand-sub { font-size: 0.8rem; letter-spacing: 0.12em; }
  #title .lede { font-size: 0.88rem !important; line-height: 1.45 !important; }
  #mini { display: none !important; }
  #speed { transform: scale(0.92); transform-origin: left bottom; }
}

/* When title is shown, ensure it can receive scroll/touch */
#title:not(.hidden) {
  pointer-events: auto !important;
}


/* ===== v1.8.2 title-scroll — reliable mobile scroll ===== */
#title {
  position: fixed !important;
  inset: 0 !important;
  z-index: 40 !important;
  display: block !important; /* not flex — flex+overflow is flaky on iOS */
  overflow: hidden !important;
  padding: 0 !important;
  background:
    radial-gradient(1200px 600px at 50% 18%, #1a2830 0%, transparent 55%),
    linear-gradient(180deg, #0c1116 0%, #0a1218 100%) !important;
}
#title.hidden { display: none !important; }

#title-scroll, .title-scroll {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 100dvh;
  overflow-x: auto !important;
  overflow-y: scroll !important; /* force scrollbar where supported */
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior: contain;
  padding: max(16px, env(safe-area-inset-top, 0px)) 14px max(72px, calc(56px + env(safe-area-inset-bottom, 0px)));
  box-sizing: border-box;
  text-align: center;
  scrollbar-width: thin;
  scrollbar-color: #4d8a86 rgba(0,0,0,0.3);
}
#title-scroll::-webkit-scrollbar { width: 10px; height: 10px; }
#title-scroll::-webkit-scrollbar-thumb {
  background: #4d8a86;
  border-radius: 8px;
}
#title-scroll::-webkit-scrollbar-track { background: rgba(0,0,0,0.25); }

#title-scroll .mark { margin-left: auto; margin-right: auto; }
#title-scroll h1 { margin-left: auto; margin-right: auto; }
#title-scroll .mayor-pick,
#title-scroll .oath-panel,
#title-scroll .lede,
#title-scroll .facts {
  width: 100%;
  max-width: min(640px, 100%);
  margin-left: auto;
  margin-right: auto;
  box-sizing: border-box;
}
#title-scroll .oath-panel {
  text-align: left;
  margin-bottom: 24px;
}
#title-scroll .mayor-cards {
  display: grid !important;
  grid-template-columns: 1fr !important;
  gap: 10px !important;
}
@media (min-width: 560px) {
  #title-scroll .mayor-cards {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* Keep powered-by from covering oath buttons */
.powered-by {
  pointer-events: none;
  z-index: 42;
}
.powered-by a { pointer-events: auto; }

/* In-game panels always scrollable both axes */
#sheet, #inspect, #data-panel, #ward-story, .modal, #toolbar, #fab-row, #topbar {
  overflow-x: auto !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  scrollbar-width: thin;
}


/* Flag library */
.flag-group { margin: 10px 0; }
.flag-group-label { letter-spacing: .06em; text-transform: uppercase; color: var(--fg-muted); margin-bottom: 6px; }
.flag-chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.flag-chip {
  appearance: none; border: 1px solid var(--border);
  background: var(--bg-subtle); color: var(--fg);
  border-radius: 999px; padding: 6px 10px;
  font-size: 0.72rem; font-weight: 600;
  min-height: 32px; cursor: pointer;
}
.flag-chip:hover { background: var(--bg-hover); border-color: var(--accent); }

/* Panel non-overlap / no truncation */
#sheet, #inspect {
  position: fixed !important;
  right: max(8px, env(safe-area-inset-right, 0px)) !important;
  left: auto !important;
  top: max(56px, env(safe-area-inset-top, 0px) + 48px) !important;
  bottom: auto !important;
  width: min(360px, calc(100vw - 16px)) !important;
  max-width: min(360px, calc(100vw - 16px)) !important;
  max-height: min(70dvh, calc(100dvh - 140px)) !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  z-index: 30 !important;
  box-sizing: border-box !important;
}
#data-panel {
  left: max(8px, env(safe-area-inset-left, 0px)) !important;
  right: auto !important;
  top: max(56px, env(safe-area-inset-top, 0px) + 48px) !important;
  width: min(300px, calc(100vw - 16px)) !important;
  max-height: min(65dvh, calc(100dvh - 140px)) !important;
  overflow-y: auto !important;
  z-index: 29 !important;
}
#toolbar {
  z-index: 20 !important;
  max-height: min(140px, 28dvh) !important;
}
#fab-row {
  z-index: 28 !important;
  right: max(8px, env(safe-area-inset-right, 0px));
  bottom: max(100px, env(safe-area-inset-bottom, 0px) + 90px);
  max-height: min(40dvh, 260px) !important;
  overflow-y: auto !important;
}
#topbar {
  z-index: 22 !important;
  max-width: calc(100vw - 16px);
  overflow-x: auto;
}
@media (max-width: 720px) {
  #sheet, #inspect {
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-width: none !important;
    top: auto !important;
    bottom: max(72px, env(safe-area-inset-bottom, 0px) + 64px) !important;
    max-height: min(50dvh, 420px) !important;
  }
  #data-panel {
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    top: max(48px, env(safe-area-inset-top, 0px) + 40px) !important;
    max-height: min(40dvh, 320px) !important;
  }
}


/* GUI polish v1.8.7 */
#topbar .panel.stats {
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  background: rgba(12, 18, 24, 0.82);
  border: 1px solid rgba(255,255,255,0.08);
}
#toolbar {
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  background: rgba(12, 18, 24, 0.88);
}
#fab-row button {
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.1);
}
#sheet, #inspect {
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  background: rgba(14, 20, 28, 0.92);
  border: 1px solid rgba(255,255,255,0.08);
}
.stat .v {
  text-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

/* Live thoughts feed */
.feed-row { border-left: 3px solid rgba(120,160,200,0.35); padding-left: 8px; margin-bottom: 6px; }
.feed-row[data-kind="grievance"] { border-left-color: #c45c4a; }
.feed-row[data-kind="hope"] { border-left-color: #3a9a6a; }
.feed-row[data-kind="city"] { border-left-color: #c4a24a; }
.feed-text { margin-top: 2px; line-height: 1.35; }
#fab-thoughts { font-size: 16px; }

/* Nightmare visual polish v1.9.4 */
#title-screen {
  background:
    radial-gradient(ellipse at 50% 20%, rgba(180,40,20,0.18), transparent 55%),
    radial-gradient(ellipse at 70% 80%, rgba(20,30,50,0.5), transparent 50%),
    linear-gradient(180deg, #0a1018 0%, #121820 40%, #1a1210 100%);
}
#title-screen h1 {
  text-shadow: 0 0 24px rgba(200,40,30,0.45), 0 2px 0 #000;
  letter-spacing: 0.04em;
}
.mayor-card {
  background: rgba(18, 22, 30, 0.88);
  border: 1px solid rgba(255,255,255,0.08);
  transition: border-color 0.15s, transform 0.15s;
}
.mayor-card:hover, .mayor-card:focus {
  border-color: rgba(200, 80, 50, 0.55);
  transform: translateY(-1px);
}
#canvas-wrap canvas {
  image-rendering: auto;
}

@keyframes toast-pop {
  0% { transform: translateX(-50%) translateY(8px); opacity: 0; }
  100% { transform: translateX(-50%) translateY(0); opacity: 1; }
}
#toast.show {
  animation: toast-pop 0.22s ease-out;
}
#fab-row button:active {
  transform: scale(0.92);
}
.stat .v {
  transition: color 0.3s;
}
