:root {
  --bg: #05080d;
  --ar: #46f0e1;
  --ar-soft: rgba(70, 240, 225, .16);
  --ar-line: rgba(70, 240, 225, .55);
  --ar-hot: #bffcf6;
  --warn: #ffb547;
  --ink: #e8fbff;
  --ink-dim: #8fb3b8;
  --panel: rgba(6, 18, 24, .78);
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Space Grotesk", system-ui, -apple-system, Segoe UI, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--sans); overflow: hidden; }
button, input { font: inherit; color: inherit; }

/* ---------- stage / world ---------- */
#stage {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: crosshair;
  background: radial-gradient(ellipse at 50% 40%, #0c1820 0%, var(--bg) 70%);
}
#stage.dragging { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; width: 4785px; height: 4300px; transform-origin: 0 0; will-change: transform; }
#shelf, #overlay { position: absolute; inset: 0; width: 4785px; height: 4300px; user-select: none; -webkit-user-drag: none; }
#shelf { filter: brightness(.92) saturate(.95); transition: filter .4s; }
#stage.has-active #shelf { filter: brightness(.82) saturate(.8); }
#overlay { overflow: visible; }

/* hit polygons */
#hits polygon { fill: transparent; stroke: none; cursor: pointer; pointer-events: all; }
#stage.dragging #hits polygon { cursor: grabbing; }
#hits polygon.flash { animation: flash 1.1s ease-out both; }
@keyframes flash {
  0% { fill: rgba(70, 240, 225, 0); stroke: rgba(70, 240, 225, 0); }
  25% { fill: rgba(70, 240, 225, .22); stroke: rgba(191, 252, 246, .9); }
  100% { fill: rgba(70, 240, 225, 0); stroke: rgba(70, 240, 225, 0); }
}
#hits polygon { stroke-width: 1.5; vector-effect: non-scaling-stroke; }

/* search matches */
#matches polygon { fill: rgba(70, 240, 225, .12); stroke: var(--ar); stroke-width: 1.5; vector-effect: non-scaling-stroke; pointer-events: none; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { fill: rgba(70, 240, 225, .28); } }

/* dim mask with a hole for the active book */
#dim { fill: rgba(2, 6, 10, 0); transition: fill .35s; pointer-events: none; }
#stage.has-active #dim { fill: rgba(2, 6, 10, .5); }
#stage.searching #dim { fill: rgba(2, 6, 10, .55); }

/* active book */
#focus { pointer-events: none; opacity: 0; transition: opacity .18s; }
#stage.has-active #focus { opacity: 1; }
#lifted, #liftedFx { transform: scale(1); transition: transform .45s cubic-bezier(.2, 1.4, .4, 1); }
#stage.has-active #lifted, #stage.has-active #liftedFx { transform: scale(var(--lift, 1.12)); }
#outline { fill: none; stroke: var(--ar); stroke-width: 2; stroke-dasharray: var(--per, 1000); stroke-dashoffset: var(--per, 1000); filter: drop-shadow(0 0 6px var(--ar)); }
#stage.has-active #outline { animation: draw .5s .05s cubic-bezier(.6, 0, .2, 1) forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
#brackets { fill: none; stroke: var(--ar-hot); stroke-width: 2.5; stroke-linecap: square; opacity: 0; }
#stage.has-active #brackets { animation: lock .35s .3s ease-out forwards; }
@keyframes lock { 0% { opacity: 0; } 40% { opacity: 1; } 55% { opacity: .3; } 100% { opacity: 1; } }
#scan { opacity: 0; }
#stage.has-active #scan { animation: scan 1.1s .1s cubic-bezier(.5, 0, .5, 1) 1 both; }
@keyframes scan { 0% { opacity: 1; transform: translateY(0); } 85% { opacity: 1; } 100% { opacity: 0; transform: translateY(var(--scanH, 300px)); } }

/* intro sweep */
#sweepBar { fill: url(#scanGrad); opacity: 0; pointer-events: none; }
#sweepBar.go { animation: sweep 2.2s cubic-bezier(.45, 0, .55, 1) forwards; }
@keyframes sweep { 0% { opacity: 1; transform: translateY(0); } 92% { opacity: 1; } 100% { opacity: 0; transform: translateY(4500px); } }

/* ---------- leader line ---------- */
#leader { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
#leaderLine { fill: none; stroke: var(--ar); stroke-width: 1.5; filter: drop-shadow(0 0 4px var(--ar)); stroke-dasharray: var(--len, 400); stroke-dashoffset: var(--len, 400); opacity: 0; }
#anchorDot { fill: var(--ar-hot); opacity: 0; filter: drop-shadow(0 0 6px var(--ar)); }
#anchorRing { fill: none; stroke: var(--ar); stroke-width: 1.5; opacity: 0; transform-box: fill-box; transform-origin: center; }
#leader.on #leaderLine { opacity: 1; animation: draw .35s .12s ease-out forwards; }
#leader.on #anchorDot { opacity: 1; }
#leader.on #anchorRing { animation: ring 1.6s ease-out infinite; }
@keyframes ring { 0% { opacity: .9; transform: scale(.3); } 100% { opacity: 0; transform: scale(1.8); } }

/* ---------- info card ---------- */
#card {
  position: absolute; left: 0; top: 0; width: 340px; z-index: 5; pointer-events: auto;
  --x: 0px; --y: 0px; transform: translate(var(--x), var(--y));
}
#card[hidden] { display: block; visibility: hidden; pointer-events: none; }
.card-inner {
  position: relative; padding: 14px 16px 14px; background: var(--panel);
  border: 1px solid var(--ar-line); border-radius: 4px;
  backdrop-filter: blur(14px) saturate(1.3); -webkit-backdrop-filter: blur(14px) saturate(1.3);
  box-shadow: 0 0 0 1px rgba(70, 240, 225, .08), 0 0 28px rgba(70, 240, 225, .18), inset 0 0 40px rgba(70, 240, 225, .05);
  clip-path: inset(50% 0 50% 0); opacity: 0;
  background-image: repeating-linear-gradient(0deg, rgba(70, 240, 225, .035) 0 1px, transparent 1px 3px);
}
#card.open .card-inner { animation: open .42s .3s cubic-bezier(.2, .9, .3, 1) forwards; }
@keyframes open {
  0% { clip-path: inset(49% 0 49% 0); opacity: 1; }
  45% { clip-path: inset(49% 0 49% 0); opacity: 1; }
  100% { clip-path: inset(0 0 0 0); opacity: 1; }
}
.card-inner::before, .card-inner::after {
  content: ""; position: absolute; width: 14px; height: 14px; border: 2px solid var(--ar-hot); pointer-events: none;
}
.card-inner::before { left: -5px; top: -5px; border-right: 0; border-bottom: 0; }
.card-inner::after { right: -5px; bottom: -5px; border-left: 0; border-top: 0; }
#card.open .card-body > *, #card.open .links { animation: rise .4s both; }
#card.open .meta { animation-delay: .6s; }
#card.open .cover { animation-delay: .55s; }
#card.open .links { animation-delay: .75s; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.card-head { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; }
.tag { padding: 2px 6px; border: 1px solid var(--ar-line); color: var(--ar); border-radius: 2px; }
.tag.dim { color: var(--ink-dim); border-color: rgba(143, 179, 184, .35); }
.tag.warn { color: var(--warn); border-color: rgba(255, 181, 71, .5); }
.close { margin-left: auto; background: none; border: 0; color: var(--ink-dim); cursor: pointer; padding: 2px 4px; font-size: 13px; visibility: hidden; }
#card.pinned .close { visibility: visible; }
.close:hover { color: var(--ar); }

.card-body { display: flex; gap: 14px; }
.cover { position: relative; flex: none; width: 92px; aspect-ratio: 2 / 3; border: 1px solid var(--ar-line); border-radius: 2px; overflow: hidden; background: #0b1a20; box-shadow: 0 0 18px rgba(70, 240, 225, .2); }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-fallback { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; font-family: var(--mono); font-size: 10px; color: var(--ink-dim); letter-spacing: .1em; }
.cover.has-img .cover-fallback { display: none; }
.cover::after { content: ""; position: absolute; inset: 0; background: linear-gradient(180deg, transparent 40%, rgba(70, 240, 225, .25) 50%, transparent 60%); background-size: 100% 240%; animation: coverScan 2.6s linear infinite; mix-blend-mode: screen; }
@keyframes coverScan { from { background-position: 0 120%; } to { background-position: 0 -120%; } }

.meta { min-width: 0; flex: 1; }
.meta h2 { margin: 0 0 4px; font-size: 18px; line-height: 1.2; font-weight: 700; text-shadow: 0 0 12px rgba(70, 240, 225, .35); overflow-wrap: anywhere; }
.author { margin: 0 0 4px; font-size: 13.5px; color: var(--ar-hot); }
.series { margin: 0 0 8px; font-family: var(--mono); font-size: 11px; color: var(--ink-dim); }
.series:empty { display: none; }
.rating { display: flex; align-items: center; gap: 8px; font-family: var(--mono); }
.stars { position: relative; display: inline-block; font-size: 15px; letter-spacing: 2px; line-height: 1; }
.stars-bg { color: rgba(143, 179, 184, .28); }
.stars-fg { position: absolute; left: 0; top: 0; overflow: hidden; white-space: nowrap; color: var(--ar); text-shadow: 0 0 8px var(--ar); width: 0; transition: width .9s .7s cubic-bezier(.2, .8, .2, 1); }
.num { font-size: 15px; font-weight: 600; color: var(--ink); }
.count { margin: 4px 0 0; font-family: var(--mono); font-size: 10.5px; color: var(--ink-dim); }
.rating.none .stars, .rating.none .num { display: none; }

.links { display: flex; gap: 8px; margin-top: 12px; }
.links a {
  flex: 1; text-align: center; padding: 7px 8px; font-family: var(--mono); font-size: 12px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--ar); text-decoration: none; border: 1px solid var(--ar-line); border-radius: 2px; background: var(--ar-soft);
  transition: background .15s, color .15s, box-shadow .15s;
}
.links a:hover { background: var(--ar); color: #032; box-shadow: 0 0 16px var(--ar); }
.links a[hidden] { display: none; }

/* ---------- HUD ---------- */
.hud { position: fixed; left: 0; right: 0; z-index: 10; display: flex; align-items: center; gap: 16px; padding: 12px 16px; pointer-events: none; }
.hud > * { pointer-events: auto; }
.hud-top { top: 0; justify-content: space-between; background: linear-gradient(180deg, rgba(5, 8, 13, .85), transparent); padding-top: max(12px, env(safe-area-inset-top)); }
.hud-bottom { bottom: 0; background: linear-gradient(0deg, rgba(5, 8, 13, .85), transparent); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.brand { display: flex; align-items: baseline; gap: 10px; font-family: var(--mono); font-weight: 600; letter-spacing: .18em; font-size: 13px; color: var(--ar); text-shadow: 0 0 10px rgba(70, 240, 225, .5); white-space: nowrap; }
.brand .sub { color: var(--ink-dim); font-weight: 400; letter-spacing: .06em; font-size: 11px; text-shadow: none; }
.blip { width: 8px; height: 8px; border-radius: 50%; background: var(--ar); box-shadow: 0 0 10px var(--ar); align-self: center; animation: blink 1.4s steps(2) infinite; }
@keyframes blink { 50% { opacity: .2; } }

.search { position: relative; width: min(360px, 46vw); }
.search input {
  width: 100%; padding: 8px 12px; background: rgba(6, 18, 24, .7); border: 1px solid var(--ar-line); border-radius: 2px; outline: none;
  font-family: var(--mono); font-size: 13px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.search input:focus { border-color: var(--ar); box-shadow: 0 0 14px rgba(70, 240, 225, .35); }
.search input::placeholder { color: var(--ink-dim); }
#results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; max-height: 50vh; overflow: auto; margin: 0; padding: 4px; list-style: none;
  background: var(--panel); border: 1px solid var(--ar-line); border-radius: 2px; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
#results li { padding: 6px 8px; cursor: pointer; border-radius: 2px; font-size: 13px; }
#results li small { display: block; color: var(--ink-dim); font-family: var(--mono); font-size: 10.5px; }
#results li:hover, #results li.sel { background: var(--ar-soft); color: var(--ar-hot); }
#results li.empty { color: var(--ink-dim); cursor: default; }

.readout { font-family: var(--mono); font-size: 11px; color: var(--ink-dim); letter-spacing: .08em; min-width: 140px; }
.hint { flex: 1; text-align: center; font-family: var(--mono); font-size: 11px; color: var(--ink-dim); letter-spacing: .04em; }
.zoom { display: flex; gap: 6px; margin-right: 170px; }
.zoom button { width: 34px; height: 34px; border: 1px solid var(--ar-line); background: rgba(6, 18, 24, .7); color: var(--ar); border-radius: 2px; cursor: pointer; font-size: 16px; }
.zoom button:hover { background: var(--ar-soft); box-shadow: 0 0 12px rgba(70, 240, 225, .35); }

#minimap { position: fixed; right: 16px; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 11; width: 150px; border: 1px solid var(--ar-line); border-radius: 2px; overflow: hidden; background: #000; box-shadow: 0 0 16px rgba(70, 240, 225, .2); cursor: pointer; }
#minimap img { display: block; width: 100%; opacity: .7; }
#mmView { position: absolute; border: 1px solid var(--ar-hot); background: rgba(70, 240, 225, .12); box-shadow: 0 0 8px var(--ar); pointer-events: none; }

@media (max-width: 720px) {
  .brand .sub, .hint, .readout { display: none; }
  .search { width: auto; flex: 1; }
  .zoom { margin-right: 0; }
  #minimap { display: none; }
  #card { width: min(340px, calc(100vw - 32px)); }
  .cover { width: 76px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-delay: 0s !important; transition-duration: .01ms !important; }
}
