:root {
  --bg: #050505;
  --fg: #f2f2f2;
  --dim: rgba(242, 242, 242, 0.48);
  --faint: rgba(242, 242, 242, 0.14);
  --line: rgba(242, 242, 242, 0.1);
  --glass: rgba(10, 10, 10, 0.72);
  --accent: #39ff6a;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Inter Tight", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
.mono { font-family: var(--mono); }
button { font: inherit; color: inherit; }

#stage {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  cursor: grab;
  touch-action: none;
}
#stage.dragging { cursor: grabbing; }
#stage.hovering { cursor: pointer; }

.vignette {
  position: fixed; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 45%, transparent 45%, rgba(0, 0, 0, 0.75) 100%);
}
.grain {
  position: fixed; inset: -50%; pointer-events: none; opacity: 0.06;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  animation: grain 1.2s steps(6) infinite;
}
@keyframes grain {
  0% { transform: translate(0, 0); } 20% { transform: translate(-3%, 2%); }
  40% { transform: translate(2%, -3%); } 60% { transform: translate(-2%, -1%); }
  80% { transform: translate(3%, 3%); } 100% { transform: translate(0, 0); }
}

/* ---------- HUD ---------- */
.hud {
  position: fixed;
  z-index: 5;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.9s var(--ease), transform 0.9s var(--ease);
}
body.ready .hud { opacity: 1; transform: none; }
body.ready .hud-tr { transition-delay: 0.1s; }
body.ready .hud-bl { transition-delay: 0.2s; }
body.ready .hud-br { transition-delay: 0.3s; }

.hud-tl { top: 22px; left: 24px; width: min(340px, calc(100vw - 48px)); }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.brand-mark {
  width: 9px; height: 9px; background: var(--accent);
  box-shadow: 0 0 14px var(--accent);
  animation: pulse 2.4s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; box-shadow: 0 0 4px var(--accent); } }
.brand-name { font-family: var(--mono); font-size: 12px; letter-spacing: 0.22em; font-weight: 500; }
.brand-sub { font-size: 11px; color: var(--dim); }

.search {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line);
  background: var(--glass);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 9px 12px;
  transition: border-color 0.3s;
}
.search:focus-within { border-color: rgba(242, 242, 242, 0.4); }
.search-key {
  font-size: 10px; color: var(--dim);
  border: 1px solid var(--faint); padding: 1px 6px;
}
.search input {
  all: unset; flex: 1; font-family: var(--mono); font-size: 12px; color: var(--fg);
}
.search input::placeholder { color: rgba(242, 242, 242, 0.3); }
.results {
  list-style: none; margin: 6px 0 0; padding: 4px 0;
  border: 1px solid var(--line); background: var(--glass);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  max-height: 50vh; overflow: auto;
}
.results li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; font-size: 13px; cursor: pointer;
}
.results li .k { font-family: var(--mono); font-size: 10px; color: var(--dim); margin-left: auto; letter-spacing: 0.08em; }
.results li.active, .results li:hover { background: rgba(255, 255, 255, 0.06); }
.results .empty { color: var(--dim); font-family: var(--mono); font-size: 11px; cursor: default; }
.sq { width: 7px; height: 7px; flex: none; display: inline-block; }
.sq.ring { background: transparent !important; border: 1px solid var(--fg); border-radius: 50%; }

.hud-tr { top: 22px; right: 24px; display: flex; gap: 22px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.stat-k { font-size: 9.5px; letter-spacing: 0.2em; color: var(--dim); }
.stat-v { font-size: 20px; font-weight: 300; font-variant-numeric: tabular-nums; }

.hud-bl { left: 24px; bottom: 22px; }
.legend-h { font-size: 9.5px; letter-spacing: 0.2em; color: var(--dim); margin-bottom: 10px; }
.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.legend li {
  display: flex; align-items: center; gap: 10px; cursor: pointer;
  font-family: var(--mono); font-size: 11px; letter-spacing: 0.06em;
  padding: 4px 8px 4px 0; transition: opacity 0.3s, transform 0.3s var(--ease);
}
.legend li:hover { transform: translateX(4px); }
.legend li .n { color: var(--dim); }
.legend li.off { opacity: 0.28; }
.legend li .sq { box-shadow: 0 0 10px currentColor; }

.hud-br { right: 24px; bottom: 22px; display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
.coords { font-size: 10px; color: var(--dim); letter-spacing: 0.08em; font-variant-numeric: tabular-nums; }
.controls { display: flex; gap: 6px; }
.controls button {
  background: var(--glass); border: 1px solid var(--line);
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.14em;
  padding: 7px 10px; cursor: pointer; color: var(--dim);
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.controls button:hover { color: var(--fg); border-color: rgba(242, 242, 242, 0.4); }
.controls button.on { color: var(--fg); }
.controls button.on::before { content: "● "; color: var(--accent); }
.build { font-size: 9.5px; color: rgba(242, 242, 242, 0.3); letter-spacing: 0.08em; }
.build a { color: inherit; }

.tooltip {
  position: fixed; z-index: 6; pointer-events: none;
  font-size: 10.5px; letter-spacing: 0.06em;
  padding: 6px 9px; background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transform: translate(14px, 14px);
  white-space: nowrap;
}
.tooltip .t-k { color: var(--dim); margin-right: 8px; }

/* ---------- Panel ---------- */
.panel {
  position: fixed; z-index: 8; top: 0; right: 0; bottom: 0;
  width: min(460px, 100vw);
  background: linear-gradient(180deg, rgba(12, 12, 12, 0.92), rgba(6, 6, 6, 0.96));
  border-left: 1px solid var(--line);
  backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
  transform: translateX(104%);
  transition: transform 0.7s var(--ease);
  display: flex; flex-direction: column;
}
.panel.open { transform: none; }
.panel::before {
  content: ""; position: absolute; left: -1px; top: 0; width: 1px; height: 0;
  background: var(--panel-accent, var(--accent));
  box-shadow: 0 0 12px var(--panel-accent, var(--accent));
  transition: height 1.1s var(--ease) 0.15s;
}
.panel.open::before { height: 100%; }
.panel-close {
  position: absolute; top: 18px; right: 18px; z-index: 2;
  background: none; border: 1px solid var(--line); color: var(--dim);
  font-size: 10px; letter-spacing: 0.14em; padding: 6px 9px; cursor: pointer;
}
.panel-close:hover { color: var(--fg); border-color: rgba(242, 242, 242, 0.4); }
.panel-inner { overflow-y: auto; padding: 64px 32px 48px; flex: 1; }
.panel-inner > * { opacity: 0; transform: translateY(10px); }
.panel.open .panel-inner > * { animation: rise 0.8s var(--ease) forwards; }
.panel.open .panel-inner > *:nth-child(2) { animation-delay: 0.06s; }
.panel.open .panel-inner > *:nth-child(3) { animation-delay: 0.12s; }
.panel.open .panel-inner > *:nth-child(4) { animation-delay: 0.18s; }
.panel.open .panel-inner > *:nth-child(5) { animation-delay: 0.24s; }
.panel.open .panel-inner > *:nth-child(n + 6) { animation-delay: 0.3s; }
@keyframes rise { to { opacity: 1; transform: none; } }

.p-kicker {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.18em; color: var(--dim);
  margin-bottom: 16px; text-transform: uppercase;
}
.p-kicker .sq { width: 8px; height: 8px; }
.p-title { font-size: 26px; line-height: 1.18; font-weight: 500; letter-spacing: -0.015em; margin: 0 0 18px; }
.p-meta {
  display: grid; grid-template-columns: auto 1fr; gap: 6px 18px;
  font-family: var(--mono); font-size: 11px; margin: 0 0 18px;
  padding: 14px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.p-meta dt { color: var(--dim); letter-spacing: 0.12em; }
.p-meta dd { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-meta a { color: var(--fg); text-decoration: none; border-bottom: 1px solid var(--faint); }
.p-meta a:hover { border-color: var(--fg); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 22px; }
.chip {
  font-family: var(--mono); font-size: 10.5px; letter-spacing: 0.04em;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.02);
  padding: 4px 8px; cursor: pointer; color: var(--fg);
  transition: border-color 0.2s, background 0.2s, transform 0.2s var(--ease);
  display: inline-flex; align-items: center; gap: 7px;
}
.chip:hover { border-color: rgba(242, 242, 242, 0.45); background: rgba(255, 255, 255, 0.06); transform: translateY(-1px); }
.chip .c { color: var(--dim); }

.md { font-size: 14.5px; line-height: 1.65; color: rgba(242, 242, 242, 0.86); }
.md h2 {
  font-family: var(--mono); font-size: 10px; font-weight: 400; letter-spacing: 0.2em;
  text-transform: uppercase; color: var(--dim); margin: 28px 0 10px;
}
.md h2::before { content: "// "; color: var(--panel-accent, var(--accent)); }
.md h3 { font-size: 15px; font-weight: 500; margin: 22px 0 8px; }
.md p, .md ul, .md ol { margin: 0 0 12px; }
.md ul, .md ol { padding-left: 20px; }
.md li { margin: 3px 0; }
.md a { color: var(--fg); text-decoration: none; border-bottom: 1px solid rgba(242, 242, 242, 0.3); word-break: break-word; }
.md a:hover { border-color: var(--fg); }
.md img, .md video { max-width: 100%; display: block; border: 1px solid var(--line); margin: 12px 0; }
.md code { font-family: var(--mono); font-size: 12px; background: rgba(255, 255, 255, 0.06); padding: 1px 5px; }
.md pre { background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); padding: 12px; overflow: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 0 0 12px; padding-left: 14px; border-left: 1px solid var(--panel-accent, var(--accent)); color: var(--dim); }

.list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.list li {
  display: flex; align-items: center; gap: 12px; padding: 12px 0;
  border-bottom: 1px solid var(--line); cursor: pointer;
  transition: padding 0.3s var(--ease), background 0.3s;
}
.list li:hover { padding-left: 8px; background: rgba(255, 255, 255, 0.025); }
.list .l-t { font-size: 14px; flex: 1; }
.list .l-k { font-family: var(--mono); font-size: 10px; color: var(--dim); letter-spacing: 0.1em; }

/* ---------- Intro ---------- */
.intro {
  position: fixed; inset: 0; z-index: 20; display: grid; place-content: center; gap: 14px;
  background: var(--bg); transition: opacity 0.9s var(--ease), visibility 0.9s;
  font-size: 11px; letter-spacing: 0.3em; text-align: center;
}
.intro.done { opacity: 0; visibility: hidden; }
.intro-bar { width: 180px; height: 1px; background: var(--faint); margin: 0 auto; overflow: hidden; }
.intro-bar span { display: block; height: 100%; width: 0; background: var(--fg); transition: width 0.6s var(--ease); }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .hud-tl { top: 14px; left: 16px; width: calc(100vw - 32px); }
  .hud-tr { top: auto; bottom: 16px; right: 16px; gap: 14px; }
  .stat-v { font-size: 15px; }
  .hud-bl { bottom: 16px; left: 16px; }
  .hud-bl .legend { max-height: 30vh; overflow: auto; }
  .hud-br { top: 112px; bottom: auto; right: 16px; }
  .coords, .build { display: none; }
  body.panel-open .hud-br, body.panel-open .hud-bl, body.panel-open .hud-tr { opacity: 0; pointer-events: none; }
  .panel {
    top: auto; width: 100vw; height: 62vh; border-left: 0; border-top: 1px solid var(--line);
    transform: translateY(104%);
  }
  .panel::before { left: 0; top: -1px; height: 1px !important; width: 0; transition: width 1.1s var(--ease) 0.15s; }
  .panel.open::before { width: 100%; }
  .panel-inner { padding: 56px 20px 40px; }
  .p-title { font-size: 22px; }
}

@media (prefers-reduced-motion: reduce) {
  .grain, .brand-mark { animation: none; }
}
