/* ---- 테마: 기본 다크, 자동이면 OS 설정(prefers-color-scheme), 수동 선택은 <html data-theme> ---- */
:root {
  color-scheme: dark;
  --bg: #111418;
  --panel: #1b2027;
  --line: #2b323c;
  --text: #e8ecf1;
  --muted: #98a2b0;
  --accent: #5aa2ff;
  --on-accent: #06121f;
  --danger: #ff7a7a;
  --thumb-bg: #0d1014;
  --reader-bg: #000;
  --bar-bg: rgba(17, 20, 24, 0.74);
  --overlay-bg: rgba(0, 0, 0, 0.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f5f6f8; --panel: #ffffff; --line: #d9dee6; --text: #1c2128; --muted: #5b6675; --accent: #1a64d1; --on-accent: #ffffff;
    --danger: #c62828; --thumb-bg: #e8ebf0; --reader-bg: #f2f2f2; --bar-bg: rgba(255, 255, 255, 0.84); --overlay-bg: rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f6f8; --panel: #ffffff; --line: #d9dee6; --text: #1c2128; --muted: #5b6675; --accent: #1a64d1; --on-accent: #ffffff;
  --danger: #c62828; --thumb-bg: #e8ebf0; --reader-bg: #f2f2f2; --bar-bg: rgba(255, 255, 255, 0.84); --overlay-bg: rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif; -webkit-text-size-adjust: 100%; }
a { color: inherit; text-decoration: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* 터치 영역은 모두 최소 44px */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; min-width: 44px; padding: 0 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.btn.small { padding: 0 12px; font-size: 14px; }
.btn.icon-btn { padding: 0; font-size: 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.btn:active { filter: brightness(1.2); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.error { color: var(--danger); }
.center { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; text-align: center; }

/* 상단 바 / 탐색 */
.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 6px; padding: calc(4px + var(--safe-top)) calc(10px + var(--safe-right)) 4px calc(14px + var(--safe-left)); background: var(--bar-bg); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; font-size: 18px; display: inline-flex; align-items: center; min-height: 44px; }
.spacer { flex: 1; }
.who { color: var(--muted); font-size: 13px; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.browse { padding: 8px calc(14px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(14px + var(--safe-left)); max-width: 1100px; margin: 0 auto; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 0 2px; margin: 0 0 4px; color: var(--muted); }
.crumbs a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 6px; color: var(--accent); }
.tools { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.tools input, .sorttools select { min-height: 44px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; }
.tools input { flex: 1; min-width: 0; }
.sorttools { margin-bottom: 4px; }
.sort-label { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.notice { margin: 2px 2px 8px; color: var(--muted); font-size: 13px; min-height: 1.2em; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }
.errbox { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 32px 0; text-align: center; }

/* 폴더: 한 줄 목록형(그리드 보기에서도 폴더는 조밀하게) */
.dirs { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; margin-bottom: 14px; }
.dir-item { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 6px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; min-width: 0; }
.dir-item:active { filter: brightness(1.15); }
.dir-icon { font-size: 20px; }
.dir-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.dir-text .name { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: unset; }
.dir-item .date { flex: none; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.card { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; min-width: 0; }
.card:active { filter: brightness(1.15); }
.thumb { aspect-ratio: 3 / 4; display: flex; align-items: center; justify-content: center; background: var(--thumb-bg); font-size: 34px; color: var(--muted); overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.nothumb { opacity: 0.5; }
.card.unsupported .thumb { aspect-ratio: 4 / 3; }
.card.unsupported, .row.unsupported { opacity: 0.6; }
.thumb-wrap { position: relative; }
.pbar-track { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.pbar { height: 100%; width: 0; background: var(--accent); }
.thumb-wrap > .pbar-track { position: absolute; left: 0; right: 0; bottom: 0; border-radius: 0; background: rgba(0, 0, 0, 0.45); }
.is-done .thumb img { opacity: 0.55; }
.state { font-size: 12px; color: var(--accent); min-height: 1em; }
.is-done .state { color: var(--muted); }
.meta { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px 10px; min-width: 0; }
.name { font-size: 14px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-word; }
.sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { align-self: flex-start; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: rgba(255, 122, 122, 0.16); color: var(--danger); }

/* 목록 보기 */
.rows { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; align-items: center; gap: 12px; min-height: 64px; padding: 6px 10px 6px 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; min-width: 0; }
.row:active { filter: brightness(1.15); }
.row .thumb-wrap { flex: none; width: 52px; }
.row .thumb { font-size: 22px; border-radius: 6px; }
.row .meta { flex: 1; padding: 0; }
.row .pbar-track { margin-top: 4px; }
.row .state { flex: none; align-self: center; }

.skel .thumb { animation: skel 1.2s ease-in-out infinite; }
.skel-line { display: block; height: 12px; margin: 4px 0; border-radius: 6px; background: var(--line); animation: skel 1.2s ease-in-out infinite; }
.skel-line.short { width: 55%; }
@keyframes skel { 50% { opacity: 0.45; } }

.recent h2 { font-size: 16px; margin: 4px 2px 8px; }
.row-scroll { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 10px; scroll-snap-type: x proximity; }
.recent .card { flex: 0 0 140px; scroll-snap-align: start; }
.recent .name.series { font-weight: 600; -webkit-line-clamp: 1; }

/* 뷰어: 이미지가 화면 전체를 차지하고, 컨트롤은 기본 숨김 → 필요할 때만 이미지 위에 반투명 오버레이로 나타난다. */
.reader { position: fixed; top: 0; left: 0; width: 100%; height: 100vh; height: 100dvh; background: var(--reader-bg); overflow: hidden; }
.stage { position: absolute; inset: 0; overflow: hidden; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
.stage.zoomed { cursor: grab; }
.zoomwrap { position: absolute; top: var(--safe-top); right: var(--safe-right); bottom: var(--safe-bottom); left: var(--safe-left); display: flex; align-items: center; justify-content: center; transform-origin: 0 0; will-change: transform; }
.zoomwrap.animating { transition: transform 0.2s ease; }
.zoomwrap .page { flex: 1 1 0; min-width: 0; width: 100%; height: 100%; object-fit: contain; display: block; -webkit-user-drag: none; }
/* 두 쪽 보기: 우→좌는 첫 쪽이 오른쪽. 책등(가운데) 쪽으로 붙여 펼친 책처럼 보이게 한다. */
.rtl .zoomwrap.spread { flex-direction: row-reverse; }
.zoomwrap.spread .page:nth-child(1) { object-position: right center; }
.zoomwrap.spread .page:nth-child(2) { object-position: left center; }
.rtl .zoomwrap.spread .page:nth-child(1) { object-position: left center; }
.rtl .zoomwrap.spread .page:nth-child(2) { object-position: right center; }
.status { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 8px 14px; border-radius: 10px; background: rgba(0, 0, 0, 0.7); color: #e8ecf1; font-size: 14px; display: flex; flex-direction: column; gap: 8px; align-items: center; text-align: center; max-width: 80vw; pointer-events: auto; z-index: 2; }
.scroller { position: absolute; inset: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; -webkit-tap-highlight-color: transparent; touch-action: pan-y pinch-zoom; }
.scroller .sp { min-height: 60vh; max-width: 900px; margin: 0 auto; background: #0a0a0a; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #98a2b0; }
.scroller .sp img { display: block; width: 100%; height: auto; }
.end-box { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 32px 16px calc(96px + var(--safe-bottom)); color: #98a2b0; text-align: center; }
.mode-page .scroller, .mode-scroll .stage { display: none; }
.reader.loading .status { position: absolute; }

.bar { position: absolute; left: 0; right: 0; z-index: 3; display: flex; align-items: center; gap: 8px; padding: 4px calc(10px + var(--safe-right)) 4px calc(10px + var(--safe-left)); background: var(--bar-bg); color: var(--text); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); transition: transform 0.18s ease, opacity 0.18s ease, visibility 0s; }
.topbar { top: 0; padding-top: calc(4px + var(--safe-top)); }
.topbar .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.bottombar { bottom: 0; flex-direction: column; align-items: stretch; gap: 2px; padding-bottom: calc(6px + var(--safe-bottom)); }
.bottombar input[type="range"] { width: 100%; height: 44px; margin: 0; accent-color: var(--accent); }
.rtl.mode-page .bottombar input[type="range"] { direction: rtl; }
.posrow { display: flex; align-items: center; justify-content: space-between; gap: 6px; color: var(--muted); font-size: 13px; }
.posrow .btn.small { padding: 0 10px; font-size: 13px; flex: 1 1 auto; }
.posrow .btn:disabled { flex: 1 1 auto; }
.posbox { display: inline-flex; align-items: center; gap: 6px; flex: none; }
.posbox .pos { min-width: 64px; text-align: center; }
.jump { width: 56px; min-height: 44px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); text-align: center; font: inherit; font-size: 13px; }
.bars-hidden .bar { opacity: 0; visibility: hidden; pointer-events: none; transition: transform 0.18s ease, opacity 0.18s ease, visibility 0s 0.18s; }
.bars-hidden .topbar { transform: translateY(-100%); }
.bars-hidden .bottombar { transform: translateY(100%); }
/* 가로 화면(낮은 높이): 슬라이더 아래 두 줄을 한 줄로 합쳐 컨트롤이 가리는 높이를 줄인다 */
@media (max-height: 500px) {
  .bottombar { flex-direction: row; flex-wrap: wrap; gap: 0 8px; }
  .bottombar input[type="range"] { flex: 1 1 100%; height: 44px; }
  .posrow { flex: 1 1 0; min-width: 0; }
}

/* 컨트롤이 숨겨졌을 때만 보이는 작은 페이지 번호(이미지 위, 터치 통과) */
.pagenum { position: absolute; left: 50%; bottom: calc(3px + var(--safe-bottom)); transform: translateX(-50%); z-index: 2; padding: 0 7px; border-radius: 999px; background: rgba(0, 0, 0, 0.4); color: rgba(255, 255, 255, 0.75); font-size: 11px; line-height: 16px; pointer-events: none; font-variant-numeric: tabular-nums; }
.reader:not(.bars-hidden) .pagenum, .reader.hide-num .pagenum, .reader.loading .pagenum { display: none; }

/* 오버레이(설정 시트·도움말·다음 권 확인) */
.overlay { position: fixed; inset: 0; z-index: 30; background: var(--overlay-bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
.overlay.bottom { align-items: flex-end; padding: 0; }
.panel { display: flex; flex-direction: column; width: min(560px, 100%); max-height: 88dvh; overflow: hidden; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4); }
.panel.sheet { border-radius: 16px 16px 0 0; padding-bottom: var(--safe-bottom); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 8px 0 16px; }
.panel-head h2 { margin: 0; font-size: 17px; }
.panel-body { overflow-y: auto; padding: 8px 16px; -webkit-overflow-scrolling: touch; }
.panel-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 16px 14px; }
.panel-actions .btn { flex: 1 1 auto; }
.vol-name { margin: 4px 0 8px; word-break: break-word; }
.seg-row { display: flex; flex-direction: column; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.seg-label { font-size: 14px; display: flex; flex-direction: column; }
.seg-label small { color: var(--muted); font-size: 12px; }
.seg-group { display: flex; gap: 6px; flex-wrap: wrap; }
.seg { flex: 1 1 0; min-height: 44px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; background: transparent; color: var(--text); font: inherit; font-size: 14px; cursor: pointer; }
.seg[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.help-sec h3 { margin: 14px 0 4px; font-size: 14px; color: var(--accent); }
.help-sec ul { margin: 0; padding-left: 18px; font-size: 14px; }
.help-sec li { margin: 4px 0; }

#toast { position: fixed; left: 50%; bottom: calc(40px + var(--safe-bottom)); transform: translate(-50%, 20px); padding: 8px 14px; font-size: 14px; border-radius: 999px; background: rgba(40, 46, 56, 0.96); border: 1px solid #2b323c; color: #e8ecf1; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 40; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
}
