:root {
  --bg: #f4f1ea;
  --panel: #fbfaf6;
  --panel-border: #ddd6c8;
  --text: #2a2620;
  --muted: #776e60;
  --accent: #2f7a5b;
  --accent-soft: #2f7a5b22;
  --warn: #b0612e;
  --bad: #b03a2e;
  --void: #0d1013;
  --overlay: #fbfaf6e6;
  --shadow: 0 1px 3px #0000001f;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171b;
    --panel: #1b1f24;
    --panel-border: #2c323a;
    --text: #e7e2d8;
    --muted: #9a9387;
    --accent: #5fbf94;
    --accent-soft: #5fbf9426;
    --warn: #e09a5a;
    --bad: #e0705f;
    --void: #0b0d10;
    --overlay: #1b1f24e6;
    --shadow: 0 1px 3px #00000066;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14171b;
  --panel: #1b1f24;
  --panel-border: #2c323a;
  --text: #e7e2d8;
  --muted: #9a9387;
  --accent: #5fbf94;
  --accent-soft: #5fbf9426;
  --warn: #e09a5a;
  --bad: #e0705f;
  --void: #0b0d10;
  --overlay: #1b1f24e6;
  --shadow: 0 1px 3px #00000066;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Component display rules must not resurrect elements marked hidden. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--sans);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

button {
  font: inherit;
  color: inherit;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 4px 10px;
  min-width: 32px;
  min-height: 32px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--panel); font-weight: 600; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }

kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 0 4px;
  border: 1px solid var(--panel-border);
  border-radius: 3px;
}

/* ---- header ---- */

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--panel-border);
  background: var(--panel);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-name { font-weight: 700; font-size: 17px; letter-spacing: 0.02em; }
.brand-mark {
  width: 12px; height: 12px;
  align-self: center;
  border-radius: 50%;
  border: 2px solid var(--accent);
  box-shadow: inset 0 0 0 2px var(--panel), inset 0 0 0 6px var(--accent);
}
.realm { color: var(--muted); font-style: italic; }

.stats { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; }

.pill {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid currentColor;
}
.pill[data-state="live"] { color: var(--accent); }
.pill[data-state="connecting"] { color: var(--muted); }
.pill[data-state="down"] { color: var(--bad); }
.pill-paused { color: var(--warn); }

.controls { display: flex; align-items: center; gap: 8px; }
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; min-width: 38px; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.access { font-size: 12px; color: var(--muted); }

/* ---- layout ---- */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
}

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--void);
  overflow: hidden;
}

#map {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}
#map.dragging { cursor: grabbing; }

.overlay {
  position: absolute;
  /* Above both map canvases. */
  z-index: 2;
  background: var(--overlay);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.overlay-bottom {
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 6px 8px;
  flex-wrap: wrap;
}

.overlay-top {
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  color: var(--warn);
}

.overlay-hint {
  top: 12px;
  right: 12px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--muted);
  transition: opacity 0.6s;
}
.overlay-hint.faded { opacity: 0; pointer-events: none; }

.zctl, .tools, .orders { display: flex; align-items: center; gap: 6px; }
#map[data-mode="dig"], #map[data-mode="undig"] { cursor: crosshair; }

.toolset { display: inline-flex; align-items: center; gap: 6px; }
button.god[aria-pressed="true"] { background: rgba(176, 132, 255, 0.22); border-color: rgb(176, 132, 255); }
.viewport[data-control-mode="god"] { box-shadow: inset 0 0 0 2px rgba(176, 132, 255, 0.75); }
.viewport[data-control-mode="god"] .toolset button[aria-pressed="true"] { border-color: rgb(176, 132, 255); background: rgba(176, 132, 255, 0.18); }
.log .god { color: rgb(150, 110, 230); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .log .god { color: rgb(196, 168, 255); }
}

.overlay-god {
  top: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  flex-wrap: wrap;
  font-size: 13px;
  max-width: calc(100% - 24px);
  border-color: rgba(176, 132, 255, 0.6);
}
.overlay-god .god-title { font-weight: 600; color: rgb(176, 132, 255); text-transform: uppercase; font-size: 11px; letter-spacing: 0.06em; }
.overlay-god label { display: flex; align-items: center; gap: 4px; }
.overlay-god select, .overlay-god input {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 3px 6px;
}
.overlay-god input { width: 96px; }
/* With the 3D panel also open (top left), drop below it so neither hides the other. */
.viewport:has(#view3d-panel:not([hidden])) .overlay-god { top: 60px; }

.overlay-3d {
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  flex-wrap: wrap;
  font-size: 13px;
  max-width: calc(100% - 24px);
}
.overlay-3d .view3d-title { font-weight: 600; color: var(--muted); text-transform: uppercase; font-size: 11px; letter-spacing: 0.06em; }
/* The 3D canvas sits under the 2D one, which carries the overlay and input. */
#map3d {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#map { position: relative; z-index: 1; }
.zctl input[type="range"] { width: 160px; accent-color: var(--accent); }
.zlabel { min-width: 120px; }

/* ---- creature sheet ---- */

.overlay-creature {
  top: 12px;
  left: 12px;
  max-height: calc(100% - 96px);
  width: 340px;
  max-width: calc(100% - 24px);
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  font-size: 13px;
  gap: 6px;
}
/* With the 3D panel open (also top left), sit below it. */
.viewport:has(#view3d-panel:not([hidden])) .overlay-creature { top: 60px; max-height: calc(100% - 144px); }
.cr-head { display: flex; align-items: flex-start; gap: 10px; }
.cr-head .glyph { width: 28px; height: 28px; font-size: 15px; flex: none; }
.cr-title { flex: 1; min-width: 0; }
.cr-name { font-weight: 600; font-size: 15px; line-height: 1.25; }
.cr-sub { font-size: 12px; }
#cr-close { min-width: 0; padding: 0 8px; font-size: 16px; line-height: 1.4; }
.cr-activity { color: var(--accent); font-size: 12.5px; }
.cr-tabs { display: flex; }
.cr-tabs button { flex: 1; min-width: 0; padding: 3px 6px; }
.cr-tabs button[aria-selected="true"] { background: var(--accent-soft); border-color: var(--accent); }
.cr-body { flex: 0 1 auto; min-height: 0; overflow-y: auto; padding-right: 2px; }
.cr-body h3 {
  margin: 10px 0 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.cr-body p { margin: 6px 0; }
.cr-label { color: var(--muted); margin-right: 6px; }
.cr-list { list-style: none; margin: 0; padding: 0; }
.cr-list li { padding: 3px 0; border-bottom: 1px dashed var(--panel-border); }
.cr-list li:last-child { border-bottom: 0; }

.meter {
  position: relative;
  display: inline-block;
  height: 8px;
  border-radius: 4px;
  background: var(--panel-border);
  overflow: hidden;
  vertical-align: middle;
}
.meter i { position: absolute; inset: 0 auto 0 0; border-radius: 4px; background: var(--accent); }
.meter[data-tone="mid"] i { background: var(--warn); }
.meter[data-tone="low"] i { background: var(--bad); }
.meter.thin { height: 4px; }

.cr-mood { display: flex; align-items: center; gap: 8px; margin: 4px 0 6px; }
.cr-mood .meter { flex: 1; }
.cr-mood-label { font-weight: 600; min-width: 72px; text-align: right; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.chip {
  font-size: 11.5px;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
}
.chip[data-sign="neg"] { color: var(--bad); border-color: currentColor; }
.chip[data-sign="pos"] { color: var(--accent); border-color: currentColor; }

/* Maslow's pyramid: narrow at the top (awen), wide at the base (body). */
.pyramid { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-top: 4px; }
.tier {
  width: var(--w);
  min-width: 190px;
  padding: 4px 8px 5px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: var(--panel);
}
.tier-name {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-align: center;
}
.need { display: grid; grid-template-columns: 56px 1fr auto; align-items: center; gap: 6px; }
.need .meter { width: 100%; }
.need-name { font-size: 12px; }
.need-state { font-size: 11px; color: var(--muted); min-width: 0; white-space: nowrap; }

.delta { display: inline-block; min-width: 26px; font-family: var(--mono); font-size: 11.5px; }
.delta[data-sign="pos"] { color: var(--accent); }
.delta[data-sign="neg"] { color: var(--bad); }

.rel { display: grid; grid-template-columns: 1fr 64px 30px; grid-template-rows: auto auto; column-gap: 8px; align-items: center; }
.rel .link { grid-column: 1; text-align: left; }
.rel .muted { grid-column: 1; grid-row: 2; font-size: 11.5px; }
.rel .opinion { grid-column: 2; grid-row: 1 / span 2; }
.rel .rel-op { grid-column: 3; grid-row: 1 / span 2; font-size: 11.5px; text-align: right; }
button.link {
  border: 0;
  background: none;
  padding: 0;
  min-width: 0;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}
button.link:hover { color: var(--accent); text-decoration: underline; }
.opinion {
  position: relative;
  display: block;
  height: 8px;
  border-radius: 4px;
  background: var(--panel-border);
}
.opinion::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }
.opinion i { position: absolute; top: 0; bottom: 0; border-radius: 4px; background: var(--accent); }
.opinion[data-sign="neg"] i { background: var(--bad); }

.attrs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.attr {
  display: flex;
  justify-content: space-between;
  padding: 3px 8px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  font-size: 12px;
}
.looks { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; }
.looks dt { color: var(--muted); }
.looks dd { margin: 0; }
.skill { display: grid; grid-template-columns: 1fr 24px; align-items: center; column-gap: 8px; }
.skill b { text-align: right; }
.skill .meter { grid-column: 1 / span 2; width: 100%; margin-top: 2px; }

/* ---- sidebar ---- */

.side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--panel-border);
  background: var(--bg);
  overflow: hidden;
}

.panel {
  padding: 10px 14px;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel-grow { flex: 1 1 0; min-height: 120px; }

.panel h2 {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.count { font-weight: 400; }

.inspector { min-height: 108px; }
.inspector dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 12px;
  margin: 0;
}
.inspector dt { color: var(--muted); }
.inspector dd { margin: 0; }

.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  vertical-align: -1px;
  margin-right: 6px;
  border: 1px solid #0000004d;
}
.glyph {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  font: 700 12px var(--mono);
  color: #14110d;
  border: 1px solid #0000004d;
  flex: none;
}

.entities, .log, .legend {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  min-height: 0;
}

.entities li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 6px;
  border-radius: 6px;
  cursor: pointer;
}
.entities li:hover { background: var(--accent-soft); }
.entities li[aria-selected="true"] { background: var(--accent-soft); outline: 1px solid var(--accent); }
.entities .ename { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entities .epos { color: var(--muted); font-size: 12px; }
.entities .ewho { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.entities .eact { color: var(--muted); font-size: 11.5px; }
.entities .eact[data-kind="digging"] { color: var(--warn); }
.entities .eact[data-kind="walking"] { color: var(--accent); }
.log .dug { color: var(--text); }

.log li {
  font-size: 12.5px;
  padding: 2px 0;
  border-bottom: 1px dashed var(--panel-border);
}
.log .t { color: var(--muted); margin-right: 6px; }
.log .warn { color: var(--warn); }

.legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px 8px;
  font-size: 12.5px;
}
.legend li { display: flex; align-items: center; gap: 4px; }
.legend .glyph { width: 16px; height: 16px; font-size: 10px; }

/* ---- sign-in dialog ---- */

dialog {
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  padding: 18px 20px;
  width: min(380px, calc(100vw - 32px));
  box-shadow: 0 8px 30px #00000059;
}
dialog::backdrop { background: #0000008c; }
dialog h2 { margin: 0 0 6px; font-size: 16px; }
dialog p { margin: 0 0 12px; font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.field input {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 6px 8px;
}
.check { display: flex; align-items: center; gap: 6px; margin-top: 10px; font-size: 13px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ---- saves dialog ---- */

#saves-dialog { width: min(480px, calc(100vw - 32px)); }
.save-row { display: flex; align-items: flex-end; gap: 8px; }
.field.grow { flex: 1; }
.saves-list {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  max-height: 280px;
  overflow-y: auto;
  border-top: 1px solid var(--panel-border);
}
.saves-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--panel-border);
  font-size: 13px;
}
.saves-list .sname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); }
.saves-list .smeta { color: var(--muted); font-size: 12px; white-space: nowrap; }

/* ---- narrow screens ---- */

@media (max-width: 760px) {
  body { overflow: auto; height: auto; }
  .layout { grid-template-columns: 1fr; }
  .viewport { height: 62vh; }
  .side { border-left: 0; border-top: 1px solid var(--panel-border); overflow: visible; }
  .panel-grow { flex: none; max-height: 240px; }
  .hide-narrow { display: none; }
  .bar { padding: 8px 16px; }
  .zctl input[type="range"] { width: 100px; }
  .overlay-creature { width: auto; right: 12px; max-height: calc(100% - 120px); }
}
