:root {
  --bg: #020604;
  --fg: #33ff66;
  --fg-dim: #1d8a3c;
  --fg-faint: #0f4a22;
  --fg-hi: #c4ffd4;
  --amber: #ffb000;
  --cyan: #38e8ff;
  --red: #ff3b4e;
  --magenta: #ff4fd8;
  --line: rgba(51, 255, 102, 0.28);
  --glow: 0 0 6px rgba(51, 255, 102, 0.55);
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code", "Cascadia Code", Menlo, Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono);
  font-size: 14px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

body.yolo { --fg: #ff5b6e; --fg-dim: #a8303e; --fg-faint: #4a1018; --fg-hi: #ffd0d6; --line: rgba(255, 70, 90, 0.35); --glow: 0 0 6px rgba(255, 70, 90, 0.6); }

#rain {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.16;
  z-index: 0;
  pointer-events: none;
  transition: opacity 0.6s;
}
body[data-chaos="3"] #rain { opacity: 0.22; }
body[data-chaos="4"] #rain { opacity: 0.28; }
body[data-chaos="5"] #rain { opacity: 0.34; }

#app {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
  padding: 10px 12px 8px;
  text-shadow: var(--glow);
}

/* ---------- header ---------- */
#top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--line);
  padding-bottom: 6px;
}
.brand { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.logo { font-size: 20px; font-weight: 800; letter-spacing: 0.18em; color: var(--fg-hi); }
.tagline { color: var(--fg-dim); font-size: 12px; }
.tagline::before { content: "// "; }
.top-meta { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 11px;
  border: 1px solid var(--line);
  padding: 2px 6px;
  color: var(--fg-dim);
  white-space: nowrap;
}
.badge-red { color: #fff; background: var(--red); border-color: var(--red); text-shadow: none; animation: blink 0.8s steps(1) infinite; }

/* ---------- HUD ---------- */
#hud {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 6px;
}
.stat {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(51, 255, 102, 0.06), rgba(0, 0, 0, 0.2));
  padding: 5px 8px 6px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
}
.stat .k { font-size: 10px; letter-spacing: 0.12em; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat .v { font-size: 17px; font-weight: 700; color: var(--fg-hi); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.stat .v.small { font-size: 12px; padding-top: 4px; }
.stat .v.amber { color: var(--amber); text-shadow: 0 0 6px rgba(255, 176, 0, 0.5); }
.stat .v.hot { color: var(--red); text-shadow: 0 0 8px rgba(255, 59, 78, 0.7); }
.stat .v.warn { color: var(--amber); }
.stat .bar { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: var(--fg-faint); }
.bar i { display: block; height: 100%; width: 0; background: var(--fg); box-shadow: var(--glow); transition: width 0.2s; }
.stat-ctx.full .bar i { background: var(--red); }

/* ---------- main panels ---------- */
#main {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  gap: 10px;
  min-height: 0;
}
.panel {
  border: 1px solid var(--line);
  background: rgba(2, 10, 5, 0.72);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}
.panel::before, .panel::after {
  content: "";
  position: absolute;
  width: 10px;
  height: 10px;
  border: 2px solid var(--fg);
  pointer-events: none;
}
.panel::before { top: -2px; left: -2px; border-right: 0; border-bottom: 0; }
.panel::after { bottom: -2px; right: -2px; border-left: 0; border-top: 0; }
.panel-hd {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 11px;
  letter-spacing: 0.14em;
  padding: 4px 8px;
  border-bottom: 1px solid var(--line);
  color: var(--fg-dim);
  background: rgba(51, 255, 102, 0.05);
  white-space: nowrap;
  overflow: hidden;
}
.panel-hd span:first-child { color: var(--fg); }

/* console: bottom-anchored, overflow is clipped at the top */
#console {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 8px 10px;
  line-height: 1.5;
  font-size: 14px;
  transition: filter 1.4s, transform 1.4s, opacity 1.4s;
  transform-origin: 50% 100%;
}
#console.compacting { filter: blur(3px); transform: scale(0.82); opacity: 0.25; }
.ln { white-space: pre-wrap; word-break: break-word; flex: none; min-height: 1.5em; }
.ln .ts { color: var(--fg-dim); opacity: 0.7; }
.ln .vb { color: var(--cyan); text-shadow: 0 0 6px rgba(56, 232, 255, 0.45); }
.ln .tg { color: var(--amber); text-shadow: 0 0 6px rgba(255, 176, 0, 0.45); }
.ln .pr { color: var(--fg-hi); }
.ln .fl { color: var(--fg-dim); }
.ln.reply { color: var(--fg-dim); font-style: italic; padding-left: 2ch; }
.ln.sys { color: var(--magenta); text-shadow: 0 0 6px rgba(255, 79, 216, 0.4); }
.ln.blk { color: var(--fg); padding-left: 2ch; border-left: 2px solid var(--fg-faint); }
.ln.blk .cm { color: var(--fg-dim); }
.ln.boot.ok { color: var(--fg); }
.ln.boot.warn { color: var(--amber); }
.ln.boot.fail { color: var(--red); }
.ln.boot.hi { color: var(--fg-hi); font-weight: 700; }
.ln.summary { color: var(--cyan); }

.cur {
  display: inline-block;
  width: 0.62em;
  height: 1.1em;
  margin-left: 1px;
  vertical-align: text-bottom;
  background: var(--fg);
  box-shadow: var(--glow);
  animation: blink 1s steps(1) infinite;
}

/* swarm */
#swarm {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  grid-auto-rows: 104px;
  gap: 6px;
  align-content: start;
}
.tile {
  border: 1px solid var(--line);
  background: rgba(4, 18, 10, 0.85);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  position: relative;
  --c: var(--fg);
}
.tile.new { animation: boot 0.35s ease-out; }
.tile-hd {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 10.5px;
  padding: 3px 6px;
  border-bottom: 1px solid var(--line);
  background: rgba(51, 255, 102, 0.06);
  white-space: nowrap;
  min-width: 0;
}
.tile-id { color: var(--fg-hi); font-weight: 700; flex: none; }
.tile-role { color: var(--fg-dim); overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.tile-st {
  font-size: 10px;
  padding: 2px 6px;
  margin: 2px 6px 0;
  align-self: flex-start;
  color: var(--c);
  border: 1px solid var(--c);
  white-space: nowrap;
  max-width: calc(100% - 12px);
  overflow: hidden;
  text-overflow: ellipsis;
  text-shadow: 0 0 6px var(--c);
}
.tile-body { flex: 1; padding: 2px 6px; font-size: 10.5px; line-height: 1.35; color: var(--fg-dim); overflow: hidden; display: flex; flex-direction: column; justify-content: flex-end; }
.tile-body div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: none; }
.tile-body div:last-child { color: var(--fg); }
.tile-body div.in { color: var(--amber); }
.tile-bar { height: 2px; background: var(--fg-faint); flex: none; }
.tile-bar i { display: block; height: 100%; background: var(--c); width: 0; transition: width 0.3s; }

#swarm[data-density="1"] { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); grid-auto-rows: 80px; gap: 6px; }
#swarm[data-density="1"] .tile-role { display: none; }
#swarm[data-density="1"] .tile-body { font-size: 9.5px; }
#swarm[data-density="2"] { grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); grid-auto-rows: 50px; gap: 6px; }
#swarm[data-density="2"] .tile-role, #swarm[data-density="2"] .tile-body { display: none; }
#swarm[data-density="2"] .tile-hd { font-size: 9.5px; padding: 2px 4px; }
#swarm[data-density="2"] .tile-st { font-size: 9px; margin: 3px 4px 0; padding: 1px 4px; max-width: calc(100% - 8px); }
#swarm[data-density="2"] .tile-bar { margin-top: auto; }
.tile.s-think { --c: var(--fg); }
.tile.s-think .tile-st { animation: pulse 1.2s ease-in-out infinite; }
.tile.s-halluc { --c: var(--magenta); }
.tile.s-halluc .tile-body { animation: glitch-x 0.4s steps(2) infinite; }
.tile.s-perm { --c: var(--amber); border-color: var(--amber); animation: blink-border 0.7s steps(1) infinite; }
.tile.s-right { --c: var(--cyan); }
.tile.s-err { --c: var(--red); border-color: rgba(255, 59, 78, 0.6); }
.tile.s-spawn { --c: var(--fg-hi); }
.tile.s-compact { --c: var(--cyan); }
.tile.s-compact .tile-body { filter: blur(1.2px); }
.tile.s-done { --c: var(--fg-dim); opacity: 0.75; }
.tile.s-wait { --c: #c8a24a; }
.tile.ping { box-shadow: 0 0 0 1px var(--amber), 0 0 14px rgba(255, 176, 0, 0.45); }
.tile.dying { animation: die 0.35s ease-in forwards; }

/* ---------- footer ---------- */
#bottom { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.ticker { overflow: hidden; border: 1px solid var(--line); background: rgba(51, 255, 102, 0.05); white-space: nowrap; font-size: 12px; padding: 3px 0; }
.ticker-track { display: inline-block; padding-left: 100%; animation: ticker 140s linear infinite; }
.ticker-track span { margin-right: 4ch; }
.ticker-track span::before { content: "\25B8  "; color: var(--amber); }
.hints { display: flex; justify-content: space-between; gap: 10px; font-size: 11px; color: var(--fg-dim); flex-wrap: wrap; }
.hints b { color: var(--fg); font-weight: 400; }
.dim { color: var(--fg-dim); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  place-items: center;
  background: rgba(0, 0, 0, 0.55);
  padding: 16px;
  text-shadow: var(--glow);
}
.overlay.show { display: grid; }

#start { background: rgba(0, 0, 0, 0.8); }
.start-box { text-align: center; max-width: 820px; padding: 26px 30px; border: 1px solid var(--line); background: rgba(2, 10, 5, 0.92); box-shadow: 0 0 60px rgba(51, 255, 102, 0.12); }
.start-box h1 { font-size: clamp(30px, 7vw, 72px); letter-spacing: 0.12em; margin: 10px 0 18px; color: var(--fg-hi); }
.robots { color: var(--fg-dim); font-size: clamp(9px, 2.1vw, 15px); line-height: 1.2; margin: 0; }
.start-hint { font-size: clamp(14px, 2.4vw, 20px); color: var(--fg); }

.big-box {
  text-align: center;
  border: 4px double var(--fg);
  padding: 28px 40px;
  background: rgba(2, 16, 7, 0.94);
  box-shadow: 0 0 40px rgba(51, 255, 102, 0.35), inset 0 0 30px rgba(51, 255, 102, 0.12);
  max-width: min(920px, 94vw);
  animation: pop 0.25s ease-out;
}
.big-title { font-size: clamp(30px, 8vw, 92px); font-weight: 900; letter-spacing: 0.06em; color: var(--fg-hi); line-height: 1.05; animation: flash 0.9s steps(1) infinite; }
.big-sub { margin-top: 14px; font-size: clamp(12px, 2vw, 17px); color: var(--fg); }
.big-box.deny { border-color: var(--red); box-shadow: 0 0 50px rgba(255, 59, 78, 0.4), inset 0 0 30px rgba(255, 59, 78, 0.15); background: rgba(22, 2, 6, 0.94); }
.big-box.deny .big-title, .big-box.deny .big-sub, .big-box.deny .rl-count { color: var(--red); text-shadow: 0 0 10px rgba(255, 59, 78, 0.7); }
.big-box.agi { border-color: var(--cyan); box-shadow: 0 0 60px rgba(56, 232, 255, 0.45), inset 0 0 30px rgba(56, 232, 255, 0.15); background: rgba(2, 10, 18, 0.94); }
.big-box.agi .big-title { color: var(--cyan); text-shadow: 0 0 14px rgba(56, 232, 255, 0.8), 3px 0 0 rgba(255, 79, 216, 0.5), -3px 0 0 rgba(51, 255, 102, 0.5); }
.rl-count { margin-top: 18px; font-size: 22px; }
.rl-hold { margin-top: 10px; min-height: 1.4em; font-size: 12px; color: var(--fg-dim) !important; text-shadow: none !important; }

.perm-box, .help-box {
  position: relative;
  border: 2px solid var(--amber);
  background: rgba(14, 10, 0, 0.96);
  color: var(--amber);
  text-shadow: 0 0 6px rgba(255, 176, 0, 0.45);
  padding: 18px 22px;
  width: min(620px, 94vw);
  box-shadow: 0 0 40px rgba(255, 176, 0, 0.25);
  animation: pop 0.2s ease-out;
}
.help-box { border-color: var(--fg); color: var(--fg); background: rgba(2, 12, 6, 0.97); text-shadow: var(--glow); width: min(680px, 94vw); }
.help-box pre { font-size: clamp(10px, 1.8vw, 13px); line-height: 1.45; margin: 10px 0; white-space: pre-wrap; }
.perm-hd { font-weight: 800; letter-spacing: 0.2em; font-size: 12px; border-bottom: 1px solid currentColor; padding-bottom: 6px; }
#perm-q { font-size: 16px; color: #ffe2a0; margin: 14px 0 8px; }
#perm-q b { color: #fff; }
#perm-cmd { background: rgba(255, 176, 0, 0.08); border: 1px dashed rgba(255, 176, 0, 0.5); padding: 8px 10px; white-space: pre-wrap; word-break: break-all; margin: 0 0 12px; font-size: 13px; }
.perm-opts { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.perm-opts li { padding: 3px 8px; border: 1px solid transparent; }
.perm-opts li.disabled { opacity: 0.45; text-decoration: line-through; }
.perm-opts li.sel { background: var(--amber); color: #000; text-shadow: none; }
.perm-foot { margin-top: 12px; font-size: 11px; opacity: 0.75; animation: blink 1.2s steps(1) infinite; }
.perm-foot.done { animation: none; opacity: 1; color: #fff; }
.stamp {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-14deg) scale(2.4);
  font-size: 46px;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: var(--fg);
  border: 6px solid var(--fg);
  padding: 4px 18px;
  opacity: 0;
  pointer-events: none;
  text-shadow: var(--glow);
  background: rgba(0, 0, 0, 0.6);
}
.stamp.show { opacity: 1; transform: translate(-50%, -50%) rotate(-14deg) scale(1); transition: transform 0.15s ease-out, opacity 0.1s; }

#compact { background: rgba(0, 0, 0, 0.25); }
.compact-box { text-align: center; border: 2px solid var(--cyan); background: rgba(0, 10, 16, 0.92); padding: 22px 30px; color: var(--cyan); text-shadow: 0 0 10px rgba(56, 232, 255, 0.6); width: min(560px, 92vw); }
.compact-title { font-size: clamp(18px, 4vw, 30px); font-weight: 800; letter-spacing: 0.1em; animation: flash 0.6s steps(1) infinite; }
.compact-sub { font-size: 12px; margin: 8px 0 14px; opacity: 0.85; }
.big-bar { height: 8px; background: rgba(56, 232, 255, 0.15); position: relative; }
.big-bar i { background: var(--cyan); width: 0; transition: none; }
#compact.show .big-bar i { animation: fill 2.1s linear forwards; }

#toasts { position: fixed; right: 22px; top: 140px; z-index: 60; display: flex; flex-direction: column; gap: 8px; width: min(340px, 80vw); pointer-events: none; }
.toast {
  border: 1px solid var(--cyan);
  background: rgba(0, 12, 18, 0.94);
  color: var(--fg-hi);
  padding: 8px 10px;
  font-size: 12px;
  box-shadow: 0 0 20px rgba(56, 232, 255, 0.2);
  animation: slide-in 0.25s ease-out;
  text-shadow: none;
}
.toast .t-hd { color: var(--cyan); font-size: 10px; letter-spacing: 0.16em; margin-bottom: 3px; }
.toast.out { animation: slide-out 0.3s ease-in forwards; }

#kbd {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  font-size: 16px;
  border: 0;
  padding: 0;
  resize: none;
  pointer-events: none;
  user-select: text;
  -webkit-user-select: text;
}

/* ---------- CRT ---------- */
#crt {
  position: fixed;
  inset: 0;
  z-index: 100;
  pointer-events: none;
  background:
    repeating-linear-gradient(180deg, rgba(0, 0, 0, 0.22) 0, rgba(0, 0, 0, 0.22) 1px, transparent 1px, transparent 3px),
    radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.55) 100%);
  animation: flicker 6s infinite;
}

/* ---------- glitch title ---------- */
.glitch { position: relative; display: inline-block; }
.glitch::before, .glitch::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}
.glitch::before { color: var(--magenta); animation: glitch-a 3.2s infinite steps(1); }
.glitch::after { color: var(--cyan); animation: glitch-b 2.7s infinite steps(1); }
body[data-chaos="4"] .glitch::before, body[data-chaos="5"] .glitch::before { animation-duration: 0.9s; }
body[data-chaos="4"] .glitch::after, body[data-chaos="5"] .glitch::after { animation-duration: 0.7s; }

#app.shake { animation: shake 0.16s linear; }
body[data-chaos="5"] #s-status { color: var(--red); animation: blink 0.5s steps(1) infinite; }

/* ---------- keyframes ---------- */
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 50% { opacity: 0.45; } }
@keyframes blink-border { 50% { border-color: transparent; } }
@keyframes boot { from { opacity: 0; transform: scale(0.85); filter: brightness(3); } to { opacity: 1; transform: none; filter: none; } }
@keyframes die { to { opacity: 0; transform: scaleY(0.05); filter: brightness(4); } }
@keyframes ticker { to { transform: translateX(-100%); } }
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes flash { 50% { opacity: 0.35; } }
@keyframes fill { to { width: 100%; } }
@keyframes slide-in { from { transform: translateX(110%); opacity: 0; } }
@keyframes slide-out { to { transform: translateX(110%); opacity: 0; } }
@keyframes flicker {
  0%, 100% { opacity: 1; }
  4% { opacity: 0.92; }
  5% { opacity: 1; }
  47% { opacity: 0.96; }
  48% { opacity: 1; }
  71% { opacity: 0.9; }
  72% { opacity: 1; }
}
@keyframes shake {
  25% { transform: translate(2px, -1px); }
  50% { transform: translate(-2px, 1px); }
  75% { transform: translate(1px, 2px); }
}
@keyframes glitch-x { 0% { transform: translateX(-1px); } 50% { transform: translateX(2px); text-shadow: -2px 0 var(--cyan), 2px 0 var(--magenta); } }
@keyframes glitch-a {
  0%, 89%, 100% { opacity: 0; transform: none; }
  90% { opacity: 0.8; transform: translate(-3px, 1px); clip-path: inset(10% 0 55% 0); }
  93% { opacity: 0.8; transform: translate(3px, -1px); clip-path: inset(60% 0 8% 0); }
  96% { opacity: 0; }
}
@keyframes glitch-b {
  0%, 84%, 100% { opacity: 0; transform: none; }
  85% { opacity: 0.8; transform: translate(3px, 0); clip-path: inset(40% 0 30% 0); }
  88% { opacity: 0.8; transform: translate(-2px, 1px); clip-path: inset(5% 0 75% 0); }
  91% { opacity: 0; }
}

/* ---------- responsive ---------- */
@media (max-width: 1200px) {
  #hud { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  html, body { font-size: 12px; }
  #app { padding: 6px; gap: 6px; }
  .tagline, .top-meta .badge:not(#clock):not(#yolo-badge) { display: none; }
  .logo { font-size: 16px; }
  #hud { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
  .stat { padding: 3px 5px 5px; }
  .stat .v { font-size: 13px; }
  .stat .k { font-size: 9px; }
  .stat-loc { display: none; }
  #main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1.1fr) minmax(0, 1fr); }
  #toasts { top: auto; bottom: 64px; right: 8px; }
  #console { font-size: 12px; }
  #swarm { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); grid-auto-rows: 92px; }
  #swarm[data-density="1"] { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); grid-auto-rows: 70px; }
  #swarm[data-density="2"] { grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); grid-auto-rows: 46px; }
  .hints .dim { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  #crt { animation: none; }
  .glitch::before, .glitch::after { display: none; }
  .tile, .tile.s-perm, .tile.s-think .tile-st, .tile.s-halluc .tile-body { animation: none; }
  .big-title, .compact-title, .badge-red, .perm-foot, .cur { animation: none; }
  .big-box, .perm-box, .help-box, .toast, .toast.out, #s-status { animation: none !important; }
  .stamp.show, .bar i, .tile-bar i { transition: none; }
  .ticker-track { animation: none; padding-left: 0; }
  #compact.show .big-bar i { animation-duration: 0.85s; }
  #app.shake { animation: none; }
  #console { transition: none; }
}
