:root {
  --bg: #0b0b0d;
  --bg-elev: #17171b;
  --bg-elev2: #202024;
  --fg: #eceef0;
  --fg-dim: #9a9aa2;
  --accent: #5b8cff;
  --danger: #ff5a5a;
  --border: #2a2a30;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

body {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

.icon { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; display: block; }

/* ---------- 상단바 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  background: rgba(11,11,13,0.9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar .title { font-size: 17px; font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iconbtn { background: none; border: none; color: var(--fg); padding: 6px; border-radius: 10px; display: flex; align-items: center; justify-content: center; }
.iconbtn:active { background: var(--bg-elev2); }

/* ---------- 홈 ---------- */
.home { padding: 12px 14px 40px; max-width: 880px; margin: 0 auto; }
.search-wrap { margin-bottom: 18px; }
.search-input {
  width: 100%; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elev);
  color: var(--fg); font-size: 15px;
}
.search-input::placeholder { color: var(--fg-dim); }

.section-title { font-size: 14px; color: var(--fg-dim); margin: 18px 0 10px; font-weight: 600; }

.continue-row { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.continue-row::-webkit-scrollbar { display: none; }
.continue-card {
  flex: 0 0 auto; width: 132px; background: var(--bg-elev); border-radius: 12px; overflow: hidden;
  border: 1px solid var(--border);
}
.continue-card .cov { width: 132px; height: 176px; object-fit: cover; background: var(--bg-elev2); display: block; }
.continue-card .meta { padding: 8px; }
.continue-card .name { font-size: 12.5px; line-height: 1.3; max-height: 2.6em; overflow: hidden; margin-bottom: 6px; }
.progress-bar { height: 4px; background: var(--bg-elev2); border-radius: 2px; overflow: hidden; }
.progress-bar > i { display: block; height: 100%; background: var(--accent); }
.continue-card .pages { font-size: 11px; color: var(--fg-dim); margin-top: 4px; }

.series-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 14px;
}
.series-card { display: block; }
.series-card .cov {
  width: 100%; aspect-ratio: 3/4; object-fit: cover; border-radius: 10px; background: var(--bg-elev);
  border: 1px solid var(--border);
}
.series-card .name { font-size: 13px; margin-top: 6px; line-height: 1.3; }
.series-card .count { font-size: 11.5px; color: var(--fg-dim); margin-top: 2px; }

.empty-hint { color: var(--fg-dim); font-size: 14px; padding: 30px 4px; text-align: center; }

/* ---------- 시리즈 상세 ---------- */
.series-page { padding: 8px 14px 40px; max-width: 880px; margin: 0 auto; }
.series-header { display: flex; gap: 14px; margin: 10px 0 18px; align-items: flex-start; }
.series-header .cov { width: 92px; aspect-ratio: 3/4; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); flex: 0 0 auto; }
.series-header h1 { font-size: 19px; margin: 0 0 6px; }
.series-header .sub { color: var(--fg-dim); font-size: 13px; }

.pref-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
.pref-btn {
  background: var(--bg-elev); border: 1px solid var(--border); color: var(--fg-dim);
  padding: 7px 12px; border-radius: 999px; font-size: 12.5px; display: flex; align-items: center; gap: 6px;
}
.pref-btn.active { color: var(--fg); border-color: var(--accent); background: rgba(91,140,255,0.12); }

.book-list { display: flex; flex-direction: column; gap: 2px; }
.book-row { display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.book-row .cov { width: 52px; aspect-ratio: 3/4; object-fit: cover; border-radius: 6px; background: var(--bg-elev); flex: 0 0 auto; }
.book-row .info { flex: 1; min-width: 0; }
.book-row .name { font-size: 14.5px; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-row .status { font-size: 12px; color: var(--fg-dim); display: flex; align-items: center; gap: 6px; }
.book-row .done-badge { color: var(--accent); }
.book-row .bar { width: 100px; }

/* ---------- 손상 파일 표시 ---------- */
.cov-broken {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-elev2); color: var(--fg-dim);
  font-size: 20px; font-weight: 700;
}
.book-row.corrupt .name { color: var(--fg-dim); }
.corrupt-badge { color: #e0777a; }
.corrupt-screen {
  min-height: 70vh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px; text-align: center;
}
.corrupt-screen .cov-broken { width: 92px; aspect-ratio: 3/4; border-radius: 10px; font-size: 32px; }
.corrupt-screen h1 { font-size: 17px; margin: 0; }
.corrupt-screen .sub { color: var(--fg-dim); font-size: 13px; max-width: 420px; }

/* ---------- 로그인 ---------- */
.login-screen {
  min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; gap: 18px;
}
.login-screen .logo { width: 56px; height: 56px; stroke: var(--fg); }
.login-screen h1 { font-size: 18px; margin: 0; }
.pin-input {
  font-size: 26px; letter-spacing: 10px; text-align: center; width: 220px;
  padding: 14px 10px; border-radius: 14px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--fg);
}
.login-btn {
  width: 220px; padding: 12px; border-radius: 14px; border: none; background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 600;
}
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- 리더 ---------- */
.reader {
  position: fixed; inset: 0; background: #000; z-index: 100;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  touch-action: none;
}
.reader-stage { position: relative; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
.reader-stage img.page {
  max-width: 100%; max-height: 100%; object-fit: contain; user-select: none; -webkit-user-drag: none;
}
.reader-stage.pair { gap: 0; }
.reader-stage.pair img.page { max-width: 50%; }

/* 펼침 나누기: overflow hidden 컨테이너 + 200% 폭 이미지를 이동시켜 반쪽만 보여줌 */
.split-wrap { position: relative; overflow: hidden; max-width: 100%; max-height: 100%; }
.split-wrap img.split-half {
  position: absolute; top: 0; height: 100%; width: 200%; max-width: none; max-height: none; object-fit: contain;
}
.split-wrap img.split-half.side-left { left: 0; }
.split-wrap img.split-half.side-right { left: -100%; }

/* 보기 방식: 폭 맞춤(세로 스크롤) */
.reader.fit-width { touch-action: pan-y; }
.reader.fit-width .tap-zones .zone { touch-action: pan-y; }
.reader.fit-width .reader-stage {
  align-items: flex-start; justify-content: flex-start; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; touch-action: pan-y;
}
.reader.fit-width .reader-stage img.page { width: 100%; max-width: 100%; height: auto; max-height: none; object-fit: contain; display: block; }
.reader.fit-width .reader-stage.pair img.page { width: 50%; max-width: 50%; height: auto; }
.reader.fit-width .reader-stage .split-wrap { max-width: none; max-height: none; }

.tap-zones { position: absolute; inset: 0; display: flex; z-index: 5; }
.tap-zones .zone { height: 100%; }
.tap-zones .zone.left { width: 30%; }
.tap-zones .zone.mid { width: 40%; }
.tap-zones .zone.right { width: 30%; }

.reader-menu-top, .reader-menu-bottom {
  position: absolute; left: 0; right: 0; z-index: 10; background: linear-gradient(rgba(0,0,0,0.85), transparent);
  padding: 10px 14px; display: flex; align-items: center; gap: 10px;
  transition: transform 0.18s ease, opacity 0.18s ease;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.reader-menu-top { top: 0; }
.reader-menu-bottom {
  bottom: 0; top: auto; background: linear-gradient(to top, rgba(0,0,0,0.85), transparent);
  flex-direction: column; align-items: stretch; padding-bottom: calc(14px + env(safe-area-inset-bottom));
}
.reader-menu-bottom .row { display: flex; align-items: center; gap: 10px; }
.reader.menu-hidden .reader-menu-top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader.menu-hidden .reader-menu-bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }

.reader-menu-top .name { flex: 1; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-slider { width: 100%; -webkit-appearance: none; appearance: none; height: 26px; background: transparent; }
.page-slider.flip { transform: scaleX(-1); }
.page-slider::-webkit-slider-runnable-track { height: 4px; background: var(--bg-elev2); border-radius: 2px; }
.page-slider::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: var(--accent); margin-top: -7px; }
.page-num { font-size: 12.5px; color: var(--fg-dim); text-align: center; }

.end-card {
  position: absolute; inset: 0; z-index: 8; background: rgba(0,0,0,0.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 20px;
  text-align: center;
}
.end-card .cov { width: 140px; aspect-ratio: 3/4; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.end-card .msg { font-size: 15px; color: var(--fg-dim); }
.end-card .name { font-size: 17px; font-weight: 700; }
.end-card button { padding: 10px 22px; border-radius: 999px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--fg); font-size: 14px; }

/* ---------- 토스트 ---------- */
#toast-root { position: fixed; left: 0; right: 0; bottom: 0; z-index: 200; display: flex; flex-direction: column; align-items: center; pointer-events: none; }
.toast {
  margin-bottom: calc(18px + env(safe-area-inset-bottom));
  background: var(--bg-elev2); border: 1px solid var(--border); color: var(--fg);
  padding: 12px 16px; border-radius: 14px; font-size: 13.5px; display: flex; align-items: center; gap: 12px;
  pointer-events: auto; box-shadow: 0 6px 24px rgba(0,0,0,0.4);
  max-width: 90vw;
}
.toast button { background: var(--accent); color: #fff; border: none; padding: 6px 12px; border-radius: 999px; font-size: 12.5px; white-space: nowrap; }
.toast .close { background: none; color: var(--fg-dim); padding: 6px; }

.loading-hint { color: var(--fg-dim); font-size: 13px; text-align: center; padding: 40px 0; }
