:root {
  --ink: #0a1214;
  --ink-2: #111d20;
  --glass: rgba(16, 28, 31, 0.72);
  --glass-strong: rgba(14, 24, 27, 0.9);
  --line: rgba(233, 220, 195, 0.14);
  --text: #f1e9da;
  --muted: #a9b3ad;
  --faint: #6f7d79;
  --accent: #6fd3c1;
  --accent-ink: #062320;
  --amber: #f2b45a;
  --good: #7fe0a0;
  --bad: #ff7a6b;
  --radius: 18px;
  --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --ink: #0a1214; } }
:root[data-theme="dark"] { --ink: #0a1214; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--ink);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  transition: background 0.8s ease;
}
body[data-stage="studio"] { background: radial-gradient(120% 90% at 50% 38%, #1d2f31 0%, #0e1a1c 55%, #070d0e 100%); }
body[data-stage="fluid"]  { background: radial-gradient(110% 90% at 50% 40%, #12384a 0%, #0a2230 50%, #050f17 100%); }
body[data-stage="lab"]    { background: radial-gradient(120% 100% at 50% 30%, #23302c 0%, #121b19 60%, #080d0c 100%); }
body[data-stage="world"]  { background: #9cc7d8; }

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
#ui { position: fixed; inset: 0; pointer-events: none; }
#ui > * { pointer-events: none; }
#ui .live { pointer-events: auto; }

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* top bar */
.topbar {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  display: flex; align-items: center; gap: 10px;
  background: linear-gradient(to bottom, rgba(6, 12, 13, 0.6), rgba(6, 12, 13, 0));
}
.topbar .title { font-family: var(--serif); font-size: 19px; font-weight: 600; letter-spacing: 0.01em; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title small { display: block; font-family: var(--sans); font-size: 12px; font-weight: 400; color: var(--muted); letter-spacing: 0; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; flex: none;
  background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-size: 18px;
}

/* bottom sheet */
.sheet {
  position: absolute; left: 50%; bottom: 0; transform: translateX(-50%);
  width: min(600px, 100%);
  padding: 14px 16px calc(14px + var(--safe-b));
  background: var(--glass);
  border: 1px solid var(--line); border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  backdrop-filter: blur(16px) saturate(1.2); -webkit-backdrop-filter: blur(16px) saturate(1.2);
  display: flex; flex-direction: column; gap: 12px;
  max-height: 52vh; overflow-y: auto;
}
@media (min-width: 900px) {
  .sheet.side { left: auto; right: 16px; bottom: 16px; transform: none; width: 380px; border-radius: var(--radius); border-bottom: 1px solid var(--line); max-height: calc(100vh - 100px); }
}
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.nowrap { flex-wrap: nowrap; }
.grow { flex: 1; min-width: 0; }
.hint { color: var(--muted); font-size: 13px; }
.label { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--faint); }

.btn {
  padding: 10px 14px; border-radius: 12px;
  background: rgba(241, 233, 218, 0.07); border: 1px solid var(--line);
  font-weight: 500; font-size: 14px; white-space: nowrap;
  transition: background 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn:hover:not(:disabled) { background: rgba(241, 233, 218, 0.12); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: #8ae0d0; }
.btn.on { background: rgba(111, 211, 193, 0.18); border-color: rgba(111, 211, 193, 0.55); color: #c9f3ea; }
.btn.small { padding: 7px 10px; font-size: 13px; border-radius: 10px; }
.btn.wide { width: 100%; }

.seg { display: flex; background: rgba(0, 0, 0, 0.25); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; padding: 8px 10px; border-radius: 9px; font-size: 13px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.seg button.on { background: rgba(241, 233, 218, 0.12); color: var(--text); }

.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 6px 11px; border-radius: 999px; font-size: 12.5px; font-weight: 500;
  border: 1px solid var(--line); color: var(--muted); background: rgba(0, 0, 0, 0.2);
  display: inline-flex; gap: 6px; align-items: center;
}
.chip.on { color: var(--text); border-color: rgba(242, 180, 90, 0.6); background: rgba(242, 180, 90, 0.14); }
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.chip .dot.done { background: var(--good); }
.chip.locked { opacity: 0.55; }
.chip.planned { opacity: 0.4; border-style: dashed; }

/* info card */
.card h2 { font-family: var(--serif); font-weight: 600; font-size: 22px; margin: 0; line-height: 1.15; }
.card .kicker { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--amber); margin-bottom: 4px; }
.card p { margin: 8px 0 0; color: #d9d2c3; }
.card p b { color: var(--text); font-weight: 600; }
.card .pull { margin-top: 8px; font-size: 13px; color: var(--accent); }

/* question banner */
.banner {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(64px + var(--safe-t));
  width: min(560px, calc(100% - 32px));
  padding: 12px 16px; border-radius: 14px;
  background: var(--glass-strong); border: 1px solid var(--line);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  text-align: center;
}
.banner .q { font-family: var(--serif); font-size: 19px; line-height: 1.25; }
.banner .q em { font-style: normal; color: var(--amber); }
.banner .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.banner.good { border-color: rgba(127, 224, 160, 0.7); }
.banner.bad { border-color: rgba(255, 122, 107, 0.7); }
.dots { display: flex; gap: 4px; justify-content: center; margin-top: 8px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: rgba(255, 255, 255, 0.15); }
.dots i.good { background: var(--good); }
.dots i.bad { background: var(--bad); }
.dots i.cur { background: var(--amber); }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choices .btn { white-space: normal; text-align: center; min-height: 46px; }
.choices .btn.good { background: rgba(127, 224, 160, 0.25); border-color: var(--good); }
.choices .btn.bad { background: rgba(255, 122, 107, 0.2); border-color: var(--bad); }

/* floating 3D label */
.tag3d {
  position: absolute; left: 0; top: 0; transform: translate(-50%, -130%);
  padding: 4px 9px; border-radius: 8px; font-size: 12.5px; font-weight: 600;
  background: rgba(10, 18, 20, 0.85); border: 1px solid rgba(242, 180, 90, 0.5); color: var(--text);
  white-space: nowrap; transition: opacity 0.2s;
}

/* home */
.home {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  padding: 24px 16px calc(28px + var(--safe-b));
  background: linear-gradient(to top, rgba(5, 15, 23, 0.92) 0%, rgba(5, 15, 23, 0.45) 45%, rgba(5, 15, 23, 0) 70%);
}
.home .brand { text-align: center; margin-bottom: 22px; }
.home h1 { font-family: var(--serif); font-weight: 400; font-size: clamp(44px, 11vw, 76px); margin: 0; letter-spacing: -0.01em; line-height: 1; }
.home .tagline { color: var(--muted); margin-top: 10px; font-size: 15px; }
.menu { width: min(560px, 100%); display: grid; gap: 10px; }
.menu-card {
  display: flex; gap: 14px; align-items: center; text-align: left;
  padding: 16px; border-radius: var(--radius);
  background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  transition: border-color 0.2s, transform 0.1s;
}
.menu-card:hover:not(:disabled) { border-color: rgba(111, 211, 193, 0.5); }
.menu-card:active:not(:disabled) { transform: scale(0.99); }
.menu-card .glyph { width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center; flex: none; font-size: 22px; background: rgba(111, 211, 193, 0.12); color: var(--accent); }
.menu-card .glyph.amber { background: rgba(242, 180, 90, 0.12); color: var(--amber); }
.menu-card h3 { margin: 0; font-family: var(--serif); font-weight: 600; font-size: 19px; }
.menu-card p { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.menu-card .meta { margin-left: auto; font-size: 12px; color: var(--faint); text-align: right; flex: none; }
.menu-card:disabled { opacity: 0.45; }

/* progress + bars */
.bar { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.3s; }
.stat { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.stat b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.legend { display: grid; gap: 6px; }
.legend .sp { display: grid; grid-template-columns: 12px 1fr auto 70px; gap: 8px; align-items: center; font-size: 13px; }
.legend .sw { width: 12px; height: 12px; border-radius: 4px; }
.legend canvas { width: 70px; height: 20px; }
.ledger { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; font-size: 13px; color: var(--muted); }
.ledger b { color: var(--text); font-variant-numeric: tabular-nums; text-align: right; }
.ledger .plus { color: var(--good); }
.ledger .minus { color: var(--bad); }

input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; }
.slider-ends { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--faint); margin-top: -4px; }

.toast {
  position: absolute; left: 50%; top: calc(70px + var(--safe-t)); transform: translate(-50%, -8px);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px);
  background: var(--glass-strong); border: 1px solid rgba(242, 180, 90, 0.5);
  opacity: 0; transition: opacity 0.25s, transform 0.25s; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay-center {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 16px;
  background: rgba(5, 10, 11, 0.55);
}
.panel {
  width: min(460px, 100%); padding: 22px; border-radius: var(--radius);
  background: var(--glass-strong); border: 1px solid var(--line); text-align: center;
  display: grid; gap: 12px;
}
.panel h2 { font-family: var(--serif); font-weight: 600; margin: 0; font-size: 26px; }
.panel .big { font-family: var(--serif); font-size: 48px; line-height: 1; }

.list { display: grid; gap: 8px; }
.kbd { font-size: 11px; color: var(--faint); }

/* Create: tool rail */
.toolbar {
  position: absolute; left: 12px; top: calc(76px + var(--safe-t));
  display: flex; flex-direction: column; gap: 4px; padding: 6px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 16px;
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.toolbar .tool {
  width: 62px; padding: 7px 2px 6px; border-radius: 11px;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  font-size: 11px; font-weight: 500; color: var(--muted); white-space: nowrap;
}
.toolbar .tool .glyph { font-size: 19px; line-height: 1.2; color: var(--text); }
.toolbar .tool:hover:not(:disabled) { background: rgba(241, 233, 218, 0.08); }
.toolbar .tool.on { background: rgba(111, 211, 193, 0.18); color: #c9f3ea; }
.toolbar .tool.off { opacity: 0.45; }
.toolbar .sep { height: 1px; margin: 3px 6px; background: var(--line); }
@media (max-width: 700px) {
  .toolbar {
    flex-direction: row; left: 8px; right: 8px; top: calc(64px + var(--safe-t));
    overflow-x: auto; scrollbar-width: none; padding: 4px; gap: 2px;
  }
  .toolbar::-webkit-scrollbar { display: none; }
  .toolbar .tool { width: auto; min-width: 0; flex: 1 1 0; padding: 5px 1px; font-size: 10px; }
  .toolbar .tool .glyph { font-size: 15px; }
  .toolbar .sep { width: 1px; height: auto; margin: 6px 2px; }
}

/* World: settings */
.knob { display: flex; flex-direction: column; gap: 2px; }
.knob .hint { font-size: 12px; line-height: 1.35; }
.knob input[type="range"] { height: 22px; }
.legend button.sp { width: 100%; text-align: left; padding: 4px 6px; margin: 0 -6px; border-radius: 8px; }
.legend button.sp:hover { background: rgba(241, 233, 218, 0.06); }
.legend button.sp.on { background: rgba(111, 211, 193, 0.14); }

/* Phone: a handle tucks the panel away */
.sheet-handle { display: none; }
@media (max-width: 899px) {
  .sheet-handle { display: block; flex: none; width: 64px; height: 22px; margin: -10px auto -4px; border-radius: 8px; position: relative; }
  .sheet-handle::after { content: ""; position: absolute; left: 14px; right: 14px; top: 8px; height: 5px; border-radius: 3px; background: var(--muted); opacity: 0.6; }
  .sheet.collapsed { max-height: 92px; overflow: hidden; }
}

/* Phone: one thin strip at the top, and a denser panel */
.toolbar .tool.back { display: none; }
@media (max-width: 700px) {
  .topbar.create-top { display: none; }
  .toolbar { top: calc(4px + var(--safe-t)); left: 4px; right: 4px; border-radius: 12px; padding: 3px; }
  .toolbar .tool { padding: 3px 1px; gap: 0; font-size: 9.5px; border-radius: 9px; }
  .toolbar .tool .glyph { font-size: 14px; line-height: 1.15; }
  .toolbar .tool.back { display: flex; color: var(--faint); }
  .sheet { padding: 8px 12px calc(10px + var(--safe-b)); gap: 8px; font-size: 13px; max-height: 44vh; }
  .sheet .hint { font-size: 12px; line-height: 1.35; }
  .sheet .label { font-size: 10px; }
  .seg { padding: 2px; border-radius: 10px; }
  .seg button { padding: 5px 6px; font-size: 12px; border-radius: 8px; }
  .btn.small { padding: 4px 8px; font-size: 12px; border-radius: 8px; }
  .btn { padding: 8px 12px; font-size: 13px; }
  .row { gap: 6px; }
  .ledger { font-size: 12px; }
  .sheet-handle { height: 16px; margin: -6px auto -6px; }
  .sheet-handle::after { top: 5px; height: 4px; }
}

/* Create: secondary controls beside the selection */
.float-ctl {
  position: absolute; left: 0; top: 0; display: flex; align-items: center; gap: 4px; padding: 4px;
  background: var(--glass-strong); border: 1px solid var(--line); border-radius: 14px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}
.float-ctl button { min-width: 38px; height: 38px; border-radius: 10px; background: rgba(241, 233, 218, 0.08); font-size: 20px; line-height: 1; }
.float-ctl button:hover:not(:disabled) { background: rgba(241, 233, 218, 0.14); }
.float-ctl button.go { font-size: 13px; font-weight: 600; padding: 0 12px; background: rgba(111, 211, 193, 0.18); color: #c9f3ea; }
.float-ctl button.go.on { background: var(--accent); color: var(--accent-ink); }
.float-ctl .val { font-size: 11px; line-height: 1.15; text-align: center; color: var(--muted); min-width: 54px; font-variant-numeric: tabular-nums; }

/* World: a rail of categories down the side, each with its own pop-out */
.rail {
  position: absolute; left: 12px; top: calc(76px + var(--safe-t));
  display: flex; flex-direction: column; gap: 2px; padding: 5px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 16px;
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.rail .tool {
  width: 62px; padding: 7px 2px 6px; border-radius: 11px;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  font-size: 11px; font-weight: 500; color: var(--muted); white-space: nowrap;
}
.rail .tool .glyph { font-size: 18px; line-height: 1.2; color: var(--text); }
.rail .tool:hover { background: rgba(241, 233, 218, 0.08); }
.rail .tool.on { background: rgba(111, 211, 193, 0.18); color: #c9f3ea; }
.popout {
  position: absolute; left: 92px; top: calc(76px + var(--safe-t));
  width: 370px; max-height: calc(100vh - 92px - var(--safe-t)); overflow-y: auto;
  padding: 12px 14px; border-radius: var(--radius);
  background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(16px) saturate(1.2); -webkit-backdrop-filter: blur(16px) saturate(1.2);
  display: flex; flex-direction: column; gap: 10px;
}
@media (max-width: 899px) {
  .rail { left: 6px; top: calc(62px + var(--safe-t)); padding: 3px; border-radius: 12px; }
  .rail .tool { width: 50px; padding: 4px 1px; font-size: 9.5px; border-radius: 9px; }
  .rail .tool .glyph { font-size: 15px; }
  .popout {
    left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 44vh;
    padding: 10px 12px calc(10px + var(--safe-b)); gap: 8px; font-size: 13px;
    border-radius: var(--radius) var(--radius) 0 0; border-bottom: 0;
  }
}

/* Create: the creature library */
input.text {
  flex: 1; min-width: 0; font: inherit; font-size: 13px; color: var(--text);
  background: rgba(0, 0, 0, 0.25); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px;
}
input.text:focus { outline: none; border-color: var(--accent); }
.library { display: flex; flex-direction: column; gap: 4px; max-height: 168px; overflow-y: auto; }
.lib-item { display: flex; gap: 6px; align-items: stretch; }
.lib-load {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 1px; text-align: left; cursor: pointer;
  font: inherit; color: var(--text); background: rgba(241, 233, 218, 0.05); border: 1px solid var(--line); border-radius: 10px; padding: 5px 10px;
}
.lib-load:hover { border-color: var(--accent); }
.lib-load b { font-weight: 600; font-size: 13.5px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-load small { color: var(--muted); font-size: 11.5px; }
.btn.danger { color: var(--bad); border-color: var(--bad); }
@media (max-width: 700px) {
  input.text { font-size: 16px; padding: 4px 8px; } /* 16px stops phones zooming in on focus */
  .library { max-height: 120px; }
}
