:root {
  --bg: #0f0f10;
  --panel: #1a1b1e;
  --panel-2: #24262b;
  --border: #2f3238;
  --fg: #f2f2f3;
  --muted: #9b9fa8;
  --accent: #5b8def;
  --accent-2: #7aa2ff;
  --danger: #ff6b7a;
  --ok: #3dd68c;
  --shadow: 0 12px 40px rgba(0,0,0,.5);
  --radius: 12px;
  --topbar-h: 56px;
  font-family: Inter, "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
img { -webkit-user-drag: none; user-select: none; }
a { color: var(--accent-2); }

#app { min-height: 100%; }

/* ========== Auth ========== */
.auth-wrap {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(900px 420px at 15% 0%, rgba(91,141,239,.22), transparent 60%),
    radial-gradient(700px 360px at 90% 100%, rgba(125,90,200,.12), transparent 55%),
    var(--bg);
}
.auth-card {
  width: min(420px, 100%);
  background: rgba(26,27,30,.92);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}
.auth-card h1 { margin: 0 0 4px; font-size: 1.55rem; letter-spacing: .01em; }
.auth-card .sub { margin: 0 0 22px; color: var(--muted); font-size: .92rem; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: .82rem; color: var(--muted); }
.field input {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  outline: none;
  color: var(--fg);
  -webkit-text-fill-color: var(--fg);
  caret-color: var(--fg);
  font-size: 16px;
  line-height: 1.4;
  opacity: 1;
}
.field input::placeholder { color: var(--muted); -webkit-text-fill-color: var(--muted); }
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(91,141,239,.18); }
.field input:-webkit-autofill,
.field input:-webkit-autofill:hover,
.field input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--fg);
  caret-color: var(--fg);
  box-shadow: 0 0 0 1000px var(--panel-2) inset;
  transition: background-color 99999s ease-in-out 0s;
}
.pw-row { display: flex; gap: 8px; align-items: stretch; }
.pw-row input { flex: 1; min-width: 0; }
.pw-toggle {
  flex: 0 0 auto;
  padding: 0 12px !important;
  font-size: .85rem !important;
  white-space: nowrap;
}
.btn {
  border: 0; border-radius: 10px; padding: 11px 14px; font-weight: 600;
  background: linear-gradient(180deg, var(--accent-2), var(--accent)); color: #0b1020;
}
.btn.secondary {
  background: var(--panel-2); color: var(--fg); border: 1px solid var(--border);
}
.btn.ghost {
  background: transparent; border: 1px solid transparent; color: var(--fg);
}
.btn.ghost:hover { background: rgba(255,255,255,.06); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.icon {
  width: 38px; height: 38px; display: inline-grid; place-items: center;
  border-radius: 10px; padding: 0; font-size: 1.05rem;
}
.err { color: var(--danger); font-size: .9rem; min-height: 1.2em; margin-top: 10px; }

/* ========== Library ========== */
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 16px;
  background: rgba(15,15,16,.88);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}
.brand {
  font-weight: 750; letter-spacing: .02em;
  display: flex; align-items: center; gap: 8px; white-space: nowrap;
}
.brand .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-2), #a78bfa);
  box-shadow: 0 0 12px rgba(91,141,239,.6);
}
.spacer { flex: 1; }
.search {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--fg);
  border-radius: 999px; padding: 8px 14px; min-width: min(280px, 36vw);
  outline: none;
}
.search:focus { border-color: var(--accent); }
.sort-select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--fg);
  border-radius: 999px; padding: 8px 12px; outline: none; cursor: pointer;
  max-width: 150px;
}
.sort-select:focus { border-color: var(--accent); }
.meta { color: var(--muted); font-size: .88rem; white-space: nowrap; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(158px, 1fr));
  gap: 18px;
  padding: 20px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  cursor: pointer;
  transition: transform .15s ease, border-color .15s, box-shadow .15s;
}
.card:hover {
  transform: translateY(-3px);
  border-color: #4a5f99;
  box-shadow: 0 10px 28px rgba(0,0,0,.35);
}
.cover {
  aspect-ratio: 2/3;
  background: #09090b center/cover no-repeat;
  position: relative;
}
.cover::after {
  content: "";
  position: absolute; inset: auto 0 0 0; height: 40%;
  background: linear-gradient(to top, rgba(0,0,0,.55), transparent);
  pointer-events: none;
}
.card-body { padding: 11px 12px 12px; }
.card-title {
  font-size: .9rem; line-height: 1.35; font-weight: 600;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; min-height: 2.5em;
}
.card-sub { margin-top: 6px; font-size: .78rem; color: var(--muted); }
.progress-bar {
  margin-top: 9px; height: 3px; background: #2c2f36; border-radius: 99px; overflow: hidden;
}
.progress-bar > i { display: block; height: 100%; background: var(--accent); width: 0; border-radius: inherit; }
.empty {
  grid-column: 1 / -1; text-align: center; color: var(--muted);
  padding: 90px 20px; line-height: 1.6;
}

/* ========== Reader (Komga-like) ========== */
.reader {
  position: fixed; inset: 0; z-index: 100;
  background: #000;
  color: #fff;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  --chrome-opacity: 1;
}
.reader.immersive {
  --chrome-opacity: 0;
}
.reader.immersive .r-top,
.reader.immersive .r-bottom {
  pointer-events: none;
}

.r-top, .r-bottom {
  opacity: var(--chrome-opacity);
  transition: opacity .2s ease;
  z-index: 5;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 12px;
}
.r-top {
  background: linear-gradient(to bottom, rgba(0,0,0,.82) 0%, rgba(0,0,0,.35) 70%, transparent 100%);
  min-height: 56px;
}
.r-bottom {
  background: linear-gradient(to top, rgba(0,0,0,.88) 0%, rgba(0,0,0,.4) 70%, transparent 100%);
  min-height: 64px;
  gap: 10px;
}
.r-title {
  font-size: .95rem; font-weight: 600;
  max-width: min(48vw, 520px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.r-chip {
  border: 1px solid rgba(255,255,255,.14);
  background: rgba(255,255,255,.08);
  color: #fff;
  border-radius: 999px;
  padding: 7px 11px;
  font-size: .82rem;
  line-height: 1;
  white-space: nowrap;
}
.r-chip:hover { background: rgba(255,255,255,.14); }
.r-chip.on {
  background: var(--accent);
  border-color: transparent;
  color: #0b1020;
  font-weight: 700;
}
.r-icon {
  width: 38px; height: 38px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.08);
  display: inline-grid; place-items: center;
  font-size: 1.05rem;
}
.r-icon:hover { background: rgba(255,255,255,.14); }

.r-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: pan-y;
  user-select: none;
}
.r-stage.mode-vertical {
  overflow: auto;
  align-items: flex-start;
  justify-content: center;
  overscroll-behavior: contain;
}
.r-view {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 0;
}
.r-stage.mode-vertical .r-view {
  height: auto;
  min-height: 100%;
  flex-direction: column;
  gap: 0;
  padding: 0 0 80px;
}
.r-view img.page {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #000;
  display: block;
}
.r-stage.fit-width .r-view img.page { width: 100%; height: auto; max-height: none; }
.r-stage.fit-height .r-view img.page { height: 100%; width: auto; max-width: 100%; }
.r-stage.fit-contain .r-view img.page { max-width: 100%; max-height: 100%; width: auto; height: auto; }
.r-stage.mode-vertical .r-view img.page {
  width: min(920px, 100%);
  height: auto;
  max-height: none;
  margin: 0 auto;
}

.spread {
  display: flex;
  height: 100%;
  max-width: 100%;
  align-items: center;
  justify-content: center;
  gap: 1px;
}
.spread img.page {
  max-height: 100%;
  width: auto;
  height: 100%;
  object-fit: contain;
}

/* click zones like Komga */
.zone {
  position: absolute; top: 0; bottom: 0; z-index: 2;
}
.zone.prev { left: 0; width: 28%; cursor: w-resize; }
.zone.next { right: 0; width: 28%; cursor: e-resize; }
.zone.menu { left: 28%; width: 44%; cursor: pointer; }
.r-stage.mode-vertical .zone { display: none; }

.scrub-wrap {
  flex: 1; display: flex; align-items: center; gap: 10px; min-width: 140px;
}
.scrub {
  flex: 1;
  appearance: none;
  height: 4px;
  border-radius: 99px;
  background: rgba(255,255,255,.18);
  outline: none;
}
.scrub::-webkit-slider-thumb {
  appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; border: 2px solid var(--accent); cursor: pointer;
}
.page-ind {
  font-variant-numeric: tabular-nums;
  font-size: .88rem;
  color: rgba(255,255,255,.85);
  min-width: 5.5em;
  text-align: center;
}

/* settings drawer */
.drawer-mask {
  position: fixed; inset: 0; z-index: 120;
  background: rgba(0,0,0,.45);
  opacity: 0; pointer-events: none; transition: opacity .18s;
}
.drawer-mask.show { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 121;
  width: min(360px, 92vw);
  background: #15171b;
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(104%);
  transition: transform .2s ease;
  display: flex; flex-direction: column;
}
.drawer.show { transform: translateX(0); }
.drawer h3 {
  margin: 0; padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 1rem;
}
.drawer .body { padding: 14px 18px; overflow: auto; flex: 1; }
.opt-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,.06);
}
.opt-row label { color: var(--muted); font-size: .9rem; }
.seg {
  display: inline-flex; background: var(--panel-2); border-radius: 10px; padding: 3px; gap: 2px;
  border: 1px solid var(--border);
}
.seg button {
  border: 0; background: transparent; color: var(--muted);
  padding: 7px 10px; border-radius: 8px; font-size: .8rem;
}
.seg button.on { background: var(--accent); color: #0b1020; font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  background: rgba(30,32,38,.92); border: 1px solid var(--border);
  color: #fff; padding: 8px 14px; border-radius: 999px;
  font-size: .85rem; z-index: 130;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }

@media (max-width: 720px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; padding: 12px; }
  .search { min-width: 0; flex: 1; }
  .r-title { max-width: 36vw; font-size: .88rem; }
  .r-chip.hide-sm { display: none; }
}


/* ========== Infinite scroll footer ========== */
.list-foot {
  padding: 8px 20px 28px;
  text-align: center;
}
.list-status {
  display: inline-block;
  color: var(--muted);
  font-size: .88rem;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--border);
}
