:root {
  --bg: #0c1014;
  --panel: #141a21;
  --panel-2: #1a222c;
  --line: #27313d;
  --line-soft: #1e2731;
  --text: #e9eff5;
  --text-dim: #8593a3;
  --text-faint: #5b6878;
  --phosphor: #36e2a0;
  --armed: #45b8f5;
  --hold: #f5b13f;
  --danger: #ff5d8f;
  --radius: 14px;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --display: "Space Grotesk", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(1200px 600px at 80% -10%, #11202a 0%, transparent 55%),
    radial-gradient(900px 500px at -10% 110%, #161226 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: var(--display);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}
a { color: inherit; text-decoration: none; }
button { font-family: var(--display); cursor: pointer; }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 14px;
  padding: 12px 18px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line-soft);
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 18px; white-space: nowrap; }
.brand .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--phosphor); box-shadow: 0 0 14px var(--phosphor); }
.brand .en { font-family: var(--mono); font-weight: 600; font-size: 10px; letter-spacing: .3em; color: var(--text-faint); text-transform: uppercase; }
.nav { display: flex; gap: 4px; }
.nav a { font-size: 13px; font-weight: 600; color: var(--text-dim); padding: 7px 12px; border-radius: 9px; }
.nav a:hover { color: var(--text); background: var(--panel); }
.nav a.active { color: var(--phosphor); background: color-mix(in srgb, var(--phosphor) 12%, transparent); }
.searchbox { margin-left: auto; display: flex; gap: 6px; min-width: 0; }
.searchbox input {
  font-family: var(--mono); font-size: 13px; color: var(--text);
  background: var(--panel); border: 1px solid var(--line); border-radius: 9px;
  padding: 8px 12px; width: min(280px, 46vw);
}
.searchbox input::placeholder { color: var(--text-faint); }
.searchbox button { font-size: 13px; font-weight: 600; color: var(--bg); background: var(--phosphor); border: none; border-radius: 9px; padding: 0 14px; }

/* ---- layout ---- */
.wrap { width: 100%; max-width: none; margin: 0 auto; padding: 18px clamp(14px, 1.5vw, 28px) 60px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.chip {
  font-size: 13px; font-weight: 600; color: var(--text-dim);
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 7px 15px; transition: color .15s, border-color .15s, background .15s;
}
.chip:hover { color: var(--text); }
.chip.active { color: var(--bg); background: var(--phosphor); border-color: transparent; }
.chip.savedchip { display: inline-flex; align-items: center; gap: 7px; padding-right: 10px; }
.chip.savedchip .delx { font-size: 15px; line-height: 1; color: var(--text-faint); }
.chip.savedchip:hover .delx { color: var(--danger); }
.chip.savedchip.active .delx { color: color-mix(in srgb, var(--bg) 55%, transparent); }
.chip.addchip { border-style: dashed; color: var(--text-faint); }
.chip.addchip:hover { color: var(--text); border-color: color-mix(in srgb, var(--phosphor) 45%, var(--line)); }

.sectitle { display: flex; align-items: baseline; gap: 10px; margin: 4px 0 14px; }
.sectitle b { font-size: 16px; font-weight: 600; }
.sectitle span { font-family: var(--mono); font-size: 11px; color: var(--text-faint); }

/* ---- card grid ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; transition: border-color .15s, transform .15s; }
.card:hover { border-color: color-mix(in srgb, var(--phosphor) 45%, var(--line)); transform: translateY(-2px); }
.card .thumb { position: relative; aspect-ratio: 16 / 9; background: #07090c center/cover no-repeat; display: block; }
.card .thumb.tall { aspect-ratio: 3 / 4; }
.card .thumb .ph { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-faint); font-family: var(--mono); font-size: 11px; }
.card .cap { padding: 9px 10px; font-size: 13px; font-weight: 500; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.7em; }

/* ---- buttons / states ---- */
.btn { font-size: 13px; font-weight: 600; color: var(--text-dim); background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 9px 16px; }
.btn:hover { color: var(--text); border-color: color-mix(in srgb, var(--phosphor) 45%, var(--line)); }
.btn.solid { background: var(--phosphor); color: var(--bg); border-color: transparent; }
.btn.danger:hover { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 50%, var(--line)); }
.center { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; text-align: center; padding: 70px 20px; color: var(--text-dim); }
.center h2 { margin: 0; color: var(--text); font-size: 18px; }
.center p { margin: 0; max-width: 42ch; font-size: 14px; }
.loading { color: var(--text-faint); font-family: var(--mono); font-size: 13px; padding: 40px; text-align: center; }
.error { color: var(--danger); font-family: var(--mono); font-size: 13px; padding: 18px; background: color-mix(in srgb, var(--danger) 8%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 30%, var(--line)); border-radius: 10px; }
.morewrap { display: flex; justify-content: center; padding: 26px; }

/* ---- detail ---- */
.detail { display: grid; grid-template-columns: 340px 1fr; gap: 24px; margin-bottom: 26px; }
.detail .cover { aspect-ratio: 16 / 9; border-radius: 12px; background: #07090c center/cover no-repeat; border: 1px solid var(--line); }
.detail h1 { margin: 0 0 10px; font-size: 22px; }
.detail .intro { color: var(--text-dim); font-size: 14px; white-space: pre-wrap; max-height: 9.5em; overflow: auto; }
.eps { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 9px; }
.eps button { font-size: 13px; font-weight: 500; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: 11px 8px; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.eps button:hover { color: var(--phosphor); border-color: color-mix(in srgb, var(--phosphor) 45%, var(--line)); }
.eps button:disabled { opacity: .5; cursor: progress; }
.backlink { display: inline-flex; align-items: center; gap: 6px; font-family: var(--mono); font-size: 12px; color: var(--text-dim); margin-bottom: 16px; }
.backlink:hover { color: var(--phosphor); }

/* ---- rules manager ---- */
.rule-row { display: flex; align-items: center; gap: 12px; padding: 13px 15px; background: var(--panel); border: 1px solid var(--line); border-radius: 11px; margin-bottom: 10px; }
.rule-row .meta { min-width: 0; }
.rule-row .meta b { font-size: 14px; }
.rule-row .meta div { font-family: var(--mono); font-size: 11px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rule-row .spacer { margin-left: auto; }
.importer { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-top: 18px; }
.importer h3 { margin: 0 0 12px; font-size: 15px; }
.importer textarea { width: 100%; min-height: 160px; font-family: var(--mono); font-size: 12px; color: var(--text); background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 11px; resize: vertical; }
.importer .row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.importer input[type=text] { flex: 1; min-width: 220px; font-family: var(--mono); font-size: 12px; color: var(--text); background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; }
.note { font-size: 12px; color: var(--text-faint); margin-top: 10px; line-height: 1.6; }

/* ---- image grid (booru) — row-major masonry (fills left→right→down), natural orientation ---- */
.imagegrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); grid-auto-rows: 8px; column-gap: 14px; }
.icard { position: relative; padding: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--panel); cursor: zoom-in; transition: border-color .15s; }
.icard:hover { border-color: color-mix(in srgb, var(--phosphor) 45%, var(--line)); }
.icard .ithumb { display: block; width: 100%; height: auto; object-fit: contain; background: #07090c; }
.icard .ithumb.noimg, .icard .ithumb.broken { aspect-ratio: 3 / 4; }
.icard .ithumb.noimg { display: grid; place-items: center; color: var(--text-faint); font-family: var(--mono); font-size: 11px; }
.icard .ivid { position: absolute; right: 7px; bottom: 7px; width: 26px; height: 26px; display: grid; place-items: center; font-size: 10px; color: #fff; background: rgba(0,0,0,.6); border-radius: 50%; }

/* ---- lightbox viewer ---- */
.lightbox { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(5,7,10,.92); backdrop-filter: blur(4px); }
.lightbox .lb-stage { flex: 1; display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; padding: 56px 70px 96px; cursor: zoom-out; }
.lightbox .lb-media { max-width: 92vw; max-height: 84vh; object-fit: contain; border-radius: 6px; box-shadow: 0 20px 60px -20px #000; }
.lightbox .lb-loading { color: var(--text-dim); font-family: var(--mono); font-size: 13px; letter-spacing: .04em; }
.lightbox .lb-x { position: absolute; top: 14px; right: 16px; width: 40px; height: 40px; font-size: 17px; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.lightbox .lb-fs { position: absolute; top: 14px; right: 64px; width: 40px; height: 40px; font-size: 18px; line-height: 1; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.lightbox .lb-fs:hover { color: var(--phosphor); border-color: color-mix(in srgb, var(--phosphor) 50%, var(--line)); }
.lightbox .lb-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 46px; height: 74px; font-size: 30px; color: var(--text); background: color-mix(in srgb, var(--panel) 70%, transparent); border: 1px solid var(--line); border-radius: 12px; cursor: pointer; }
.lightbox .lb-prev { left: 12px; } .lightbox .lb-next { right: 12px; }
.lightbox .lb-x:hover, .lightbox .lb-nav:hover { color: var(--phosphor); border-color: color-mix(in srgb, var(--phosphor) 50%, var(--line)); }
.lightbox .lb-info { position: absolute; left: 0; right: 0; bottom: 0; padding: 12px 18px; max-height: 30vh; overflow: auto; background: linear-gradient(to top, rgba(5,7,10,.96), transparent); }
.lightbox .lb-meta { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }
.lightbox .lb-meta a { color: var(--armed); }
.lightbox .lb-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.lightbox .lb-tags span { font-size: 12px; color: var(--text-dim); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }

/* immersive / fullscreen reading — the image fills the screen height (object-fit:contain scales
   it UP, not just down), everything else is hidden. Tap the right half → next page, left → prev.
   100dvh adapts to the iOS browser chrome. */
.lightbox.immersive { background: #000; }
.lightbox.immersive .lb-stage { padding: 0; height: 100vh; height: 100dvh; cursor: pointer; }
.lightbox.immersive .lb-media { width: 100vw; height: 100vh; height: 100dvh; max-width: none; max-height: none; object-fit: contain; border-radius: 0; box-shadow: none; }
.lightbox.immersive .lb-info, .lightbox.immersive .lb-prev, .lightbox.immersive .lb-next { display: none; } /* hide all non-image UI */
.lightbox.immersive .lb-x, .lightbox.immersive .lb-fs { top: max(12px, env(safe-area-inset-top)); opacity: .22; } /* faint escape hatch */
.lightbox.immersive .lb-x:hover, .lightbox.immersive .lb-fs:hover { opacity: 1; }

@media (max-width: 620px) {
  .detail { grid-template-columns: 1fr; gap: 16px; }
  .searchbox input { width: 40vw; }
  .nav a { padding: 7px 9px; }
  .lightbox .lb-stage { padding: 48px 10px 90px; }
}

@media (min-width: 1200px) {
  .imagegrid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
}
