* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  color-scheme: dark;
  --bg: #05060a;
  --panel: rgba(13, 16, 28, 0.78);
  --edge: rgba(255, 255, 255, 0.09);
  --text: #cdd3e1;
  --dim: #78809a;
  --accent: #6ee7ff;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#view {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
}

button, select {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: calc(16px + env(safe-area-inset-top));
  left: calc(20px + env(safe-area-inset-left));
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}
#hud h1 {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.42em;
  color: #eef2fb;
  text-shadow: 0 0 24px rgba(110, 231, 255, 0.35);
}
#stats {
  margin-top: 2px;
  font-size: 11px;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.06em;
}

/* ---------- population chart (ecosystem/evolution modes) ---------- */
#popChart {
  position: fixed;
  top: calc(72px + env(safe-area-inset-top));
  left: calc(20px + env(safe-area-inset-left));
  width: 224px;
  height: 68px;
  z-index: 5;
  display: none;
  background: rgba(13, 16, 28, 0.5);
  border: 1px solid var(--edge);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
body.eco #popChart { display: block; }

/* ---------- mode-dependent visibility ---------- */
#evoNote { display: none; font-size: 11px; color: var(--dim); margin-top: 8px; line-height: 1.5; }
body.evo #evoNote { display: block; }
body.evo #presetRow, body.evo #matrixSec { display: none; }
#ecoSliders { display: none; }
body.eco #ecoSliders { display: block; }
.evo-only { display: none; }
body.evo .evo-only { display: flex; }
#brushRow { flex: 1 1 100%; margin-top: 4px; }
#mode { width: 100%; margin-bottom: 8px; }

/* ---------- toggle button ---------- */
#toggleUI, #surprise {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: calc(16px + env(safe-area-inset-right));
  z-index: 15;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--edge);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  transition: transform 0.1s ease-out, background 0.15s;
}
#toggleUI:hover, #surprise:hover { background: rgba(30, 36, 56, 0.85); }
#toggleUI:active, #surprise:active { transform: scale(0.92); }
#surprise { top: calc(62px + env(safe-area-inset-top)); font-size: 18px; }

/* ---------- panel ---------- */
#panel {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top));
  right: 16px;
  width: 268px;
  max-height: calc(100vh - 80px - env(safe-area-inset-top));
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 14px;
  padding: 14px;
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  transition: opacity 0.25s, transform 0.25s;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.16) transparent;
}
#panel::-webkit-scrollbar { width: 6px; }
#panel::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.16); border-radius: 3px; }
#panel::-webkit-scrollbar-track { background: transparent; }

#panel.hidden { opacity: 0; transform: translateX(12px); pointer-events: none; }

#panel { display: flex; flex-direction: column; }
#panel > * { flex-shrink: 0; }

#panel section + section {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--edge);
}
/* desktop: matrix sits tight under its Rules header */
#matrixSec { margin-top: 0; padding-top: 0; border-top: none; }
#panel h3 {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--dim);
  margin-bottom: 8px;
  flex: 1 1 100%;
}
#panel h3.tgl {
  position: relative;
  cursor: pointer;
  padding-right: 18px;
}
#panel h3.tgl::after {
  content: '⌄';
  position: absolute;
  right: 2px;
  top: -3px;
  font-size: 13px;
  transition: transform 0.15s;
}
#panel section.collapsed h3.tgl::after { transform: rotate(-90deg); }
#panel section.collapsed h3.tgl { margin-bottom: 0; }
#panel section.collapsed > *:not(h3) { display: none; }
#panel h3.tgl:focus-visible { outline: none; color: var(--accent); }

#grip { display: none; }

.row { display: flex; gap: 6px; }
.row select { flex: 1; min-width: 0; }

select, button {
  font: inherit;
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 5px 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
select option { color: var(--text); background: #0d101c; }
select:hover, button:hover { background: rgba(255, 255, 255, 0.1); }
button:active { background: rgba(255, 255, 255, 0.16); }
button.on { border-color: var(--accent); color: var(--accent); }
select:focus-visible, button:focus-visible { outline: none; border-color: rgba(110, 231, 255, 0.5); }

#matrix {
  width: 100%;
  max-width: 320px;
  aspect-ratio: 1;
  display: block;
  margin: 10px auto 0;
  border-radius: 10px;
  cursor: ns-resize;
  touch-action: none;
}
#mread {
  margin-top: 6px;
  font-size: 11px;
  color: var(--dim);
  min-height: 17px;
  font-variant-numeric: tabular-nums;
}
#mread .chip {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 3px;
  margin: 0 3px -1px 0;
}

/* ---------- sliders ---------- */
.ctl {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 7px;
}
.ctl span {
  width: 84px;
  flex: none;
  font-size: 11px;
  color: var(--dim);
  letter-spacing: 0.04em;
}
.ctl input[type="range"] {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
  height: 18px;
}
.ctl output {
  width: 40px;
  flex: none;
  text-align: right;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.ctl select { flex: 1; min-width: 0; }

/* ---------- buttons row ---------- */
.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.buttons button, .buttons select { flex: 1 1 auto; }
#aboutBtn, #snapBtn { flex: 0 0 34px; }
#seedMode { flex: 0 1 88px; }
.world-buttons { flex-wrap: wrap; }
.world-buttons button { flex: 1 1 92px; }
.world-buttons button:disabled { opacity: 0.35; cursor: default; }
.preset-row { margin-top: 6px; }
.preset-row select { min-width: 0; flex: 1 1 130px; }
.preset-row button { flex: 0 0 auto; }
.world-key {
  display: flex;
  flex-wrap: wrap;
  gap: 3px 10px;
  margin-top: 7px;
  color: var(--dim);
  font-size: 9px;
}
.world-key span::before { content: '·'; margin-right: 4px; }
.world-key .chem-pos { color: #35dcec; }
.world-key .chem-neg { color: #f15bb5; }

/* ---------- canvas tool picker ---------- */
.brush-control {
  position: relative;
  width: 100%;
}
#brushTrigger {
  width: 100%;
  min-height: 48px;
  display: grid;
  grid-template-columns: 15px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  text-align: left;
}
.brush-swatch, .brush-option i {
  display: block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--tool-color, var(--accent));
  box-shadow: 0 0 12px color-mix(in srgb, var(--tool-color, var(--accent)) 70%, transparent);
}
#brushTrigger span { min-width: 0; }
#brushTrigger strong, #brushTrigger small { display: block; }
#brushTrigger strong { color: #eef2fb; font-size: 12px; font-weight: 650; }
#brushTrigger small {
  margin-top: 1px;
  overflow: hidden;
  color: var(--dim);
  font-size: 10px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#brushTrigger b { color: var(--dim); font-size: 14px; transition: transform 0.15s; }
#brushTrigger[aria-expanded="true"] b { transform: rotate(180deg); }
#brushMenu {
  margin-top: 6px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 10px;
  background: rgba(8, 10, 18, 0.96);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
}
#brushMenu[hidden] { display: none; }
.brush-option {
  --tool-color: var(--accent);
  width: 100%;
  min-height: 47px;
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: center;
  gap: 10px;
  border: 0;
  border-radius: 0;
  padding: 6px 10px;
  text-align: left;
}
.brush-option + .brush-option { border-top: 1px solid rgba(255, 255, 255, 0.055); }
.brush-group {
  padding: 7px 10px 3px;
  font-size: 8.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--dim);
  border-top: 1px solid rgba(255, 255, 255, 0.055);
}
#brushMenu .brush-group:first-child { border-top: none; }
.brush-option strong, .brush-option small { display: block; }
.brush-option strong { color: #e8edf8; font-size: 11px; }
.brush-option small { margin-top: 1px; color: var(--dim); font-size: 9.5px; line-height: 1.3; }
.brush-option[aria-selected="true"] {
  background: color-mix(in srgb, var(--tool-color) 13%, rgba(255, 255, 255, 0.04));
}
.brush-option[aria-disabled="true"] { opacity: 0.38; }
#brushHelp {
  min-height: 30px;
  margin: 6px 3px 0;
  color: var(--dim);
  font-size: 9.5px;
  line-height: 1.45;
}
.composer-slider {
  display: grid;
  grid-template-columns: 48px 1fr 36px;
  align-items: center;
  gap: 7px;
  margin-top: 5px;
  color: var(--dim);
  font-size: 9.5px;
}
.composer-slider input { width: 100%; min-width: 0; accent-color: var(--tool-color, var(--accent)); }
.composer-slider output { color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }
#brushCursor {
  --tool-color: var(--accent);
  position: fixed;
  z-index: 12;
  display: none;
  width: 72px;
  height: 72px;
  align-items: center;
  justify-content: center;
  border: 1px solid color-mix(in srgb, var(--tool-color) 80%, white);
  border-radius: 50%;
  background: color-mix(in srgb, var(--tool-color) 8%, transparent);
  box-shadow: inset 0 0 24px color-mix(in srgb, var(--tool-color) 9%, transparent),
              0 0 18px color-mix(in srgb, var(--tool-color) 15%, transparent);
  pointer-events: none;
  transform: translate(-50%, -50%);
}
#brushCursor.visible { display: flex; }
#brushCursor span {
  position: absolute;
  top: calc(100% + 5px);
  left: 50%;
  padding: 2px 6px;
  border: 1px solid color-mix(in srgb, var(--tool-color) 35%, transparent);
  border-radius: 5px;
  color: #eef2fb;
  background: rgba(6, 8, 14, 0.86);
  font-size: 9px;
  line-height: 1.3;
  white-space: nowrap;
  transform: translateX(-50%);
}

/* ---------- WebGPU engine ---------- */
#gpuview {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
/* #view stays on top, transparent, so pointer handling is untouched */
body.webgpu #view { opacity: 0; }
/* not ported yet on the WebGPU path */
body.webgpu #worldSec { display: none; }
body.webgpu .brush-option:not([data-brush="force"]),
body.webgpu .brush-group { display: none; }

/* ---------- zen mode ---------- */
body.zen #hud, body.zen #toggleUI, body.zen #surprise,
body.zen #popChart, body.zen #panel {
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.2s ease-out;
}
body.zen #view { cursor: none; }

/* ---------- first-run nudge ---------- */
#firstRun {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  transition: opacity 0.9s ease-out;
}
#firstRun[hidden] { display: none; }
#firstRun.bye { opacity: 0; }
#firstRun span {
  font-size: 15px;
  letter-spacing: 0.4em;
  color: rgba(238, 242, 251, 0.85);
  text-shadow: 0 0 22px rgba(110, 231, 255, 0.45);
  animation: firstpulse 2.8s ease-in-out infinite;
}
@keyframes firstpulse { 0%, 100% { opacity: 0.3; } 50% { opacity: 0.95; } }
@media (prefers-reduced-motion: reduce) { #firstRun span { animation: none; } }

.hint {
  margin-top: 12px;
  font-size: 10.5px;
  color: var(--dim);
  text-align: center;
  line-height: 1.7;
  user-select: none;
  -webkit-user-select: none;
}
.hint-touch { display: none; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 10px;
  padding: 8px 16px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  z-index: 20;
}

/* ---------- overlay ---------- */
#overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(3, 4, 8, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 30;
  transition: opacity 0.2s;
}
#overlay.hidden { opacity: 0; pointer-events: none; }
#overlay .card {
  user-select: text;
  -webkit-user-select: text;
  width: min(560px, calc(100vw - 40px));
  max-height: calc(100dvh - 80px);
  overflow-y: auto;
  background: #0d101c;
  border: 1px solid var(--edge);
  border-radius: 16px;
  padding: 28px 30px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
}
#overlay h2 {
  font-size: 18px;
  color: #eef2fb;
  margin-bottom: 12px;
  letter-spacing: 0.04em;
}
#overlay p { margin-bottom: 10px; }
#overlay .tech { color: var(--dim); font-size: 12px; }
#overlay table {
  margin: 14px 0;
  border-collapse: collapse;
  font-size: 12px;
}
#overlay td { padding: 3px 14px 3px 0; color: var(--dim); }
#overlay td:first-child {
  color: var(--accent);
  font-family: ui-monospace, monospace;
  font-size: 11px;
}
#closeOverlay { margin-top: 6px; }

/* ---------- fatal / noscript ---------- */
#fatal[hidden] { display: none; }
#fatal, .noscript {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  background: var(--bg);
  color: #f2b8c6;
  font-size: 15px;
  z-index: 40;
  white-space: pre-line;
}

/* ---------- mobile: panel becomes a bottom sheet ----------
   (keep this media query in sync with mqMobile in main.js) */
@media (max-width: 720px), (pointer: coarse) and (max-height: 520px) {
  #panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    /* Leave the top controls clear, but let the expanded sheet use the rest of
       the viewport. Its own overflow then keeps the final sliders reachable. */
    max-height: calc(100dvh - 64px - env(safe-area-inset-top));
    border-radius: 18px 18px 0 0;
    border-bottom: none;
    border-top: 1px solid rgba(255, 255, 255, 0.14);
    padding: 0 16px calc(16px + env(safe-area-inset-bottom));
    box-shadow: 0 -12px 48px rgba(0, 0, 0, 0.55);
    transition: none;
    will-change: transform;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
  }
  /* the desktop hide style must not apply to the sheet — JS drives transform */
  #panel.hidden { opacity: 1; transform: none; pointer-events: auto; }

  #grip {
    display: flex;
    justify-content: center;
    padding: 12px 0 8px;
    margin: 0 -16px;
    cursor: grab;
    touch-action: none;
  }
  #grip:active { cursor: grabbing; }
  #grip span {
    width: 44px;
    height: 5px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.22);
  }

  /* peek order: quick controls first, matrix + sliders below the fold */
  #rulesSec { order: 1; }
  .buttons { order: 2; }
  #worldSec { order: 3; }
  #matrixSec { order: 4; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--edge); }
  #sliders { order: 5; }
  #ecoSliders { order: 6; }
  .hint { order: 7; }

  #popChart { width: 176px; height: 56px; }

  #panel section + section { margin-top: 10px; padding-top: 10px; }

  /* 16px stops iOS Safari from auto-zooming focused selects */
  select { font-size: 16px; }
  .ctl input[type="range"] { height: 30px; }
  #matrix { max-width: 240px; }
  #hud h1 { font-size: 13px; }
}

@media (pointer: coarse) {
  .hint-mouse { display: none; }
  .hint-touch { display: block; }
}

/* ---------- accessibility preferences ---------- */
@media (prefers-reduced-transparency: reduce) {
  #panel, #toggleUI, #surprise, #toast {
    background: #0d101c;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}
@media (prefers-reduced-motion: reduce) {
  #panel, #toggleUI, #surprise, #overlay { transition: none; }
}
@media (prefers-contrast: more) {
  :root { --edge: rgba(255, 255, 255, 0.3); --dim: #a8b0c4; }
}
