:root {
  --bg: #0a0e16;
  --panel: #141b27;
  --panel2: #1c2533;
  --line: #283448;
  --text: #e7edf5;
  --muted: #8a99ad;
  --accent: #5db0ff;
  --good: #46c98b;
  --warn: #f0b53a;
  --bad: #f06a5a;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

#app { display: flex; height: 100vh; width: 100vw; position: relative; overflow: hidden; }

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 50% 38%, rgba(93, 176, 255, .24), transparent 34%),
    linear-gradient(140deg, rgba(5, 8, 14, .98), rgba(10, 18, 32, .98));
  color: var(--text);
  opacity: 1;
  visibility: visible;
  transition: opacity .25s ease, visibility .25s ease;
}
.loading-overlay.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.loading-panel {
  display: grid;
  place-items: center;
  gap: 12px;
}
.loading-spinner {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 6px solid rgba(231, 237, 245, .22);
  border-top-color: var(--accent);
  border-right-color: var(--good);
  animation: loadingSpin .9s linear infinite;
}
.loading-panel p {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .02em;
}

@keyframes loadingSpin {
  to { transform: rotate(360deg); }
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 280px;
  flex: 0 0 280px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  transition: margin-left .22s ease, margin-bottom .22s ease, transform .22s ease;
  position: relative;
  z-index: 6;
}
body.sidebar-collapsed #sidebar { margin-left: -246px; }

#sidebar h1 { font-size: 22px; letter-spacing: 0.5px; }
.tagline { color: var(--muted); font-size: 12px; margin-top: -8px; }
.back-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  color: var(--text);
  text-decoration: none;
  background: #0f1622;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
}
.back-link:hover { border-color: var(--accent); }

.panel {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}
.panel h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-bottom: 10px;
}
.panel-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.panel-title-row h2 { margin-bottom: 0; }
.mini-button {
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 11px;
}
.hint { font-size: 11px; color: var(--muted); margin: 6px 0 4px; }
.panel-warning,
.inspect-notice {
  margin-top: 10px;
  padding: 8px;
  border-radius: 7px;
  border: 1px solid rgba(240,181,58,.45);
  background: rgba(240,181,58,.12);
  color: #ffd98a;
  font-size: 11.5px;
  line-height: 1.4;
}
.inspect-notice {
  margin: 0 0 8px;
  border-color: rgba(240,106,90,.5);
  background: rgba(240,106,90,.12);
  color: #ffaaa0;
}
.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 12px;
}
.toggle-row input { accent-color: var(--accent); }

/* Tool grid */
.tools {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.tool {
  background: #0f1622;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 9px 6px;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all .12s ease;
}
.tool:hover { border-color: var(--accent); transform: translateY(-1px); }
.tool.active {
  background: var(--accent);
  color: #05111f;
  border-color: var(--accent);
  font-weight: 600;
}

/* Sliders */
.slider {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-top: 10px;
}
.slider span { color: var(--text); float: right; font-variant-numeric: tabular-nums; }
.slider input { width: 100%; margin-top: 6px; accent-color: var(--accent); }

/* Buttons */
.row { display: flex; gap: 6px; }
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 9px;
  font-size: 13px;
  color: var(--text);
  background: #0f1622;
  border: 1px solid var(--line);
  transition: filter .12s ease;
}
button:hover { filter: brightness(1.25); }
button:active { transform: translateY(1px); }
.wide { width: 100%; }
.third { flex: 1; }
.primary { background: var(--accent); color: #05111f; font-weight: 600; border-color: transparent; }
.good { background: rgba(70,201,139,.15); border-color: var(--good); color: var(--good); }
.warn { background: rgba(240,181,58,.15); border-color: var(--warn); color: var(--warn); }
.bad  { background: rgba(240,106,90,.15); border-color: var(--bad);  color: var(--bad); }

/* Stats */
.stats { list-style: none; display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.stats li { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.stats b { color: var(--text); font-variant-numeric: tabular-nums; }

/* Species list */
.species-list { display: flex; flex-direction: column; gap: 7px; }
.species-subhead {
  margin-top: 4px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.species-subhead:first-child { margin-top: 0; }
.sp { display: grid; grid-template-columns: 12px 1fr auto auto; align-items: center; gap: 6px; font-size: 12px; }
.sp .dot { width: 10px; height: 10px; border-radius: 50%; }
.sp.plant .dot {
  border-radius: 3px;
  background: linear-gradient(135deg, #46c98b, #9bd968);
}
.sp .nm { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sp .ct { color: var(--muted); font-variant-numeric: tabular-nums; }
.sp .tk { font-size: 11px; }
.sp .iq { grid-column: 2 / 5; height: 3px; background: #0c121c; border-radius: 2px; overflow: hidden; }
.sp .iq i { display: block; height: 100%; background: linear-gradient(90deg, #5db0ff, #a06bff); }
.sp.plant .iq i { background: linear-gradient(90deg, #46c98b, #d7d66b); }

/* Inspect */
#inspectBody { font-size: 12px; color: var(--muted); line-height: 1.55; }
#inspectBody b { color: var(--text); }
.ins-head { font-size: 13px; margin-bottom: 6px; }
.ins-head .dot, #inspectBody .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; margin-right: 5px; }
.ins-stats { margin-bottom: 8px; }
.trait { display: grid; grid-template-columns: 74px 1fr 42px; align-items: center; gap: 6px; margin: 3px 0; font-size: 11px; }
.trait > div { height: 6px; background: #0c121c; border-radius: 3px; overflow: hidden; }
.trait i { display: block; height: 100%; background: linear-gradient(90deg, #46c98b, #5db0ff); }
.trait-val {
  color: var(--text);
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.ins-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
  margin-bottom: 8px;
}
.ins-grid span { color: var(--muted); }
.ins-grid b {
  color: var(--text);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Guide */
.guide summary { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); cursor: pointer; list-style: none; }
.guide summary::-webkit-details-marker { display: none; }
.guide[open] summary { margin-bottom: 8px; }
.guide-body { font-size: 11.5px; color: var(--muted); line-height: 1.5; }
.guide-body p { margin-bottom: 7px; }
.guide-body b { color: var(--text); }

.footer { font-size: 11px; color: var(--muted); text-align: center; margin-top: auto; padding-top: 8px; }

/* Event log overlay (top-right of the world) */
#eventLog {
  position: absolute;
  top: 12px; right: 12px;
  width: min(330px, calc(100vw - 24px));
  max-height: min(62vh, 520px);
  display: flex; flex-direction: column; gap: 6px;
  pointer-events: auto;
  background: rgba(12,16,24,.68);
  border: 1px solid rgba(40,52,72,.72);
  border-radius: 8px;
  padding: 8px;
  backdrop-filter: blur(3px);
}
#eventLog[hidden] { display: none; }
#feedSearch {
  width: 100%;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(8,12,20,.88);
  color: var(--text);
  padding: 7px 9px;
  font: inherit;
  font-size: 12px;
  outline: none;
}
#feedSearch:focus { border-color: var(--accent); }
#feedItems {
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 2px;
}
.ev {
  background: rgba(12,16,24,.82);
  border-left: 3px solid var(--line);
  padding: 5px 9px;
  border-radius: 5px;
  font-size: 12px;
  color: var(--text);
  backdrop-filter: blur(2px);
}
.ev b { font-weight: 600; }
.ev.muted { color: var(--muted); }
.ev.invention { border-left-color: var(--warn); }
.ev.speciation { border-left-color: #a06bff; }
.ev.extinction { border-left-color: var(--bad); }
.ev.kill { border-left-color: #d98a5a; }
.ev.spawn { border-left-color: var(--good); }

/* ---------- Stage ---------- */
#stage { position: relative; flex: 1; overflow: hidden; }
#glcanvas { position: absolute; inset: 0; display: block; z-index: 0; cursor: crosshair; }
#eventLog, #zoomControls, #toast { z-index: 5; }

.era {
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  padding: 6px;
  margin-bottom: 8px;
  background: #0c121c;
  border-radius: 6px;
  border: 1px solid var(--line);
}

/* Zoom controls (bottom-left of the world) */
#zoomControls {
  position: absolute;
  left: 12px; bottom: 12px;
  display: flex; flex-direction: column; gap: 4px;
}
#zoomControls button {
  width: 34px; height: 34px;
  font-size: 18px; line-height: 1;
  background: rgba(20,27,39,.92);
  border: 1px solid var(--line);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
}
#zoomControls button:hover { border-color: var(--accent); }
#zoomControls button.active {
  background: #94f2b8;
  color: #06140d;
  border-color: #94f2b8;
}

#toast {
  position: absolute;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(20,27,39,.95);
  border: 1px solid var(--line);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: all .25s ease;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.panel-handle {
  position: absolute;
  top: 50%;
  left: 279px;
  z-index: 8;
  width: 28px;
  height: 58px;
  padding: 0;
  transform: translateY(-50%);
  border-radius: 0 8px 8px 0;
  background: rgba(20,27,39,.96);
  border: 1px solid var(--line);
  border-left: 0;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  cursor: ew-resize;
  touch-action: none;
}
body.sidebar-collapsed .panel-handle {
  left: 33px;
}

@media (max-width: 860px) {
  html, body { overflow: hidden; }
  #app { flex-direction: column; }
  #sidebar {
    width: 100%;
    flex: 0 0 42vh;
    min-height: 260px;
    max-height: 46vh;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  body.sidebar-collapsed #sidebar {
    margin-left: 0;
    margin-bottom: calc(-42vh + 42px);
    transform: translateY(calc(-100% + 42px));
  }
  #stage {
    flex: 1 1 auto;
    min-height: 54vh;
  }
  #eventLog {
    width: min(300px, calc(100vw - 24px));
    max-height: 42vh;
  }
  .panel-handle {
    left: 50%;
    top: min(42vh, 46vh);
    width: 58px;
    height: 28px;
    transform: translate(-50%, -50%) rotate(90deg);
    border-left: 1px solid var(--line);
    border-radius: 8px 8px 0 0;
    cursor: ns-resize;
  }
  body.sidebar-collapsed .panel-handle {
    left: 50%;
    top: 42px;
  }
}
