:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-2: #ececea;
  --text: #1c1c1e;
  --muted: #6d6d72;
  --line: #e0e0dc;
  --accent: #2f6df6;
  --accent-text: #ffffff;
  --danger: #d6453b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .08), 0 4px 14px rgba(0, 0, 0, .08);
  --chrome-bg: rgba(24, 24, 26, .86);
  --chrome-text: #f2f2f2;
  --radius: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141416;
    --surface: #1f1f22;
    --surface-2: #2a2a2e;
    --text: #ececee;
    --muted: #9b9ba1;
    --line: #313135;
    --accent: #5b8cff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.pad { padding: 24px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.screen { position: fixed; inset: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface);
  white-space: nowrap; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .35; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px;
  border: 0; border-radius: 8px; background: transparent;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 20px; height: 20px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 5px 12px; border-radius: 7px;
  color: var(--muted); font-weight: 500; white-space: nowrap;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

select {
  height: 32px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); padding: 0 8px;
}

/* ---------- library ---------- */
#library { display: flex; flex-direction: column; padding-top: var(--safe-top); }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 20px; border-bottom: 1px solid var(--line);
  background: var(--surface);
}
[data-platform="mac"] .topbar { padding-top: 14px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 16px; }
.logo {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px;
  background: var(--accent); color: #fff; font-weight: 800; font-size: 15px;
}
.search {
  flex: 1; max-width: 420px; display: flex; align-items: center; gap: 8px;
  height: 34px; padding: 0 10px; border-radius: 8px; background: var(--surface-2);
  color: var(--muted);
}
.search input { flex: 1; border: 0; outline: 0; background: transparent; color: var(--text); min-width: 0; }
.actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

#lib-main { flex: 1; overflow-y: auto; padding: 8px 20px calc(40px + var(--safe-bottom)); }
#lib-main h2 { font-size: 15px; margin: 0; font-weight: 700; }
.shelf { margin: 14px 0 6px; }
.shelf h2 { margin-bottom: 10px !important; }
.shelf-row {
  display: grid; grid-auto-flow: column; grid-auto-columns: 112px; gap: 14px;
  overflow-x: auto; padding-bottom: 8px; scrollbar-width: thin;
}
.grid-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 18px 0 12px; }
.crumb { display: flex; align-items: center; gap: 10px; margin: 14px 0 16px; }
.crumb h2 { font-size: 18px !important; }
.crumb .btn { margin-left: auto; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(138px, 1fr)); gap: 22px 18px; }
.card { cursor: pointer; outline: none; min-width: 0; }
.card:focus-visible .cover { box-shadow: 0 0 0 3px var(--accent); }
.cover {
  position: relative; aspect-ratio: 2 / 3; border-radius: 6px; overflow: hidden;
  background: var(--surface-2); box-shadow: var(--shadow);
  transition: transform .15s ease;
}
.card:hover .cover { transform: translateY(-2px); }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cover img:not([src]), .cover img.none { visibility: hidden; }
.cover .ph {
  position: absolute; inset: 0; padding: 12px; display: flex; align-items: flex-end;
  font-weight: 600; font-size: 13px; line-height: 1.3; color: var(--muted);
  background: linear-gradient(160deg, var(--surface) 0%, var(--surface-2) 100%);
  overflow: hidden; word-break: break-all;
}
.cover .badge {
  position: absolute; right: 6px; bottom: 6px; padding: 2px 7px; border-radius: 10px;
  background: rgba(0, 0, 0, .72); color: #fff; font-size: 11px; font-weight: 600;
}
.cover.stacked { overflow: visible; }
.cover.stacked::before, .cover.stacked::after {
  content: ""; position: absolute; inset: 0; border-radius: 6px; background: var(--surface-2);
  box-shadow: var(--shadow); z-index: -1;
}
.cover.stacked::before { transform: translate(5px, -5px); opacity: .8; }
.cover.stacked::after { transform: translate(10px, -10px); opacity: .5; z-index: -2; }
.cover.stacked > img, .cover.stacked > .ph { border-radius: 6px; }
.series .cover { margin-right: 10px; margin-top: 10px; }
.meta { padding: 8px 2px 0; }
.meta .title {
  font-size: 13px; font-weight: 600; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.meta .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.bar { height: 3px; border-radius: 2px; background: var(--line); margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.card.done .bar i { background: var(--muted); }
.card.compact .meta .title { -webkit-line-clamp: 1; }

.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 18px !important; margin: 14px 0 6px !important; }
.empty-art svg { width: 64px; height: 64px; stroke-width: 1.6; }
.empty-actions { display: flex; gap: 10px; justify-content: center; margin-top: 22px; }

/* ---------- modal, menu, toast ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: rgba(0, 0, 0, .4); padding: 16px; }
.modal-box {
  width: min(520px, 100%); max-height: 86vh; overflow-y: auto; border-radius: 14px;
  background: var(--surface); padding: 20px 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.modal-box h3 { margin: 0 0 14px; font-size: 17px; }
.set-group { margin-bottom: 18px; }
.set-label { font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .03em; }
.set-group .btn { margin: 0 6px 6px 0; }
.folder-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.folder-row span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-list { margin-bottom: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

.menu {
  position: fixed; z-index: 60; min-width: 180px; padding: 5px; border-radius: 10px;
  background: var(--surface); box-shadow: 0 10px 30px rgba(0, 0, 0, .25); border: 1px solid var(--line);
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 12px; border-radius: 6px; }
.menu button:hover { background: var(--surface-2); }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); z-index: 70;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 9px 16px; border-radius: 20px; max-width: 86vw;
  background: rgba(20, 20, 22, .9); color: #fff; font-size: 13px;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- reader ---------- */
#reader { background: var(--reader-bg, #111); color: var(--reader-fg, #ccc); overflow: hidden; }
#stage { position: absolute; inset: 0; overflow: hidden; touch-action: pan-x pan-y pinch-zoom; user-select: none; -webkit-user-select: none; }
#reader[data-mode="text"] #stage { user-select: text; -webkit-user-select: text; }

.chrome {
  position: absolute; left: 0; right: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  background: var(--chrome-bg); color: var(--chrome-text);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  transition: transform .2s ease, opacity .2s ease; opacity: 0; pointer-events: none;
}
.chrome.top { top: 0; transform: translateY(-100%); padding: calc(8px + var(--safe-top)) 10px 8px; }
.chrome.bottom { bottom: 0; transform: translateY(100%); padding: 10px 14px calc(10px + var(--safe-bottom)); }
#reader.ui .chrome { transform: none; opacity: 1; pointer-events: auto; }
.chrome .icon-btn:hover, .chrome .btn.ghost:hover { background: rgba(255, 255, 255, .12); }
.chrome .btn.ghost { color: var(--chrome-text); }
.r-title { flex: 1; min-width: 0; }
#r-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#r-sub { font-size: 12px; opacity: .65; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-quick { display: flex; gap: 6px; }
.chip {
  height: 28px; padding: 0 11px; border-radius: 14px; border: 1px solid rgba(255, 255, 255, .22);
  background: rgba(255, 255, 255, .06); color: var(--chrome-text); font-size: 12.5px; white-space: nowrap;
}
.chip:hover { background: rgba(255, 255, 255, .16); }
#r-slider { flex: 1; accent-color: var(--accent); min-width: 60px; }
#r-label { min-width: 74px; text-align: center; font-variant-numeric: tabular-nums; font-size: 13px; }
#r-bottom.rtl { flex-direction: row-reverse; }

/* paged comic */
.paged { position: absolute; inset: 0; overflow: hidden; }
.paged.fit-width { overflow-y: auto; }
.spread { position: relative; width: 100%; height: 100%; display: flex; align-items: stretch; justify-content: center; }
.spread.rtl { flex-direction: row-reverse; }
.page { position: relative; flex: 0 0 100%; height: 100%; overflow: hidden; }
.spread.two .page { flex-basis: 50%; }
.page img { display: block; width: 100%; height: 100%; object-fit: contain; object-position: center; }
.page.to-right img { object-position: right center; }
.page.to-left img { object-position: left center; }
.fit-width .spread { height: auto; min-height: 100%; align-items: flex-start; }
.fit-width .page { height: auto; }
.fit-width .page img { height: auto; }
.fit-width .page.html { height: 100vh; }
.page iframe.htmlpage { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }
.page iframe.htmlpage.fixed { inset: auto; left: 0; top: 0; transform-origin: 0 0; }

/* webtoon / scroll strip */
.strip { position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden; overflow-anchor: none; -webkit-overflow-scrolling: touch; }
.strip-col { margin: 0 auto; width: 100%; }
.slot { position: relative; width: 100%; overflow: hidden; }
.slot.img { margin-bottom: var(--gap, 0); }
.slot img { display: block; width: 100%; height: 100%; }
.slot iframe.docframe { display: block; width: 100%; height: 100%; border: 0; }
.slot iframe.docframe.fixed { position: absolute; left: 0; top: 0; transform-origin: 0 0; height: auto; background: #fff; }
.strip.comic .slot.doc { background: #fff; }
.book-divider {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 48px 16px; color: var(--reader-fg); opacity: .8; text-align: center;
}
.book-divider span { font-size: 12px; letter-spacing: .1em; opacity: .7; }
.book-divider b { font-size: 16px; }

/* paged text */
.textpaged { position: absolute; top: var(--safe-top); left: 0; right: 0; bottom: calc(26px + var(--safe-bottom)); overflow: hidden; }
.textpaged iframe.chapter { position: absolute; left: 0; top: 0; border: 0; background: transparent; }
.textpaged iframe.pending { visibility: hidden; }
.page-info {
  position: absolute; left: 0; right: 0; bottom: calc(6px + var(--safe-bottom)); text-align: center;
  font-size: 11px; opacity: .5; font-variant-numeric: tabular-nums; pointer-events: none;
}

/* sheets */
.sheet {
  position: absolute; z-index: 30; top: calc(60px + var(--safe-top)); right: 12px; bottom: calc(74px + var(--safe-bottom));
  width: min(360px, calc(100% - 24px)); overflow-y: auto;
  background: var(--surface); color: var(--text); border-radius: 14px; padding: 6px 16px 16px;
  box-shadow: 0 16px 50px rgba(0, 0, 0, .35); user-select: none; -webkit-user-select: none;
}
.sheet-head h3 { margin: 12px 0 8px; font-size: 15px; }
.set-row { display: flex; flex-direction: column; gap: 7px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.set-row:last-of-type { border-bottom: 0; }
.set-row > label { font-size: 12.5px; color: var(--muted); font-weight: 600; display: flex; justify-content: space-between; }
.set-row > label b { color: var(--text); font-weight: 600; }
.set-row .seg { flex-wrap: wrap; }
.set-row input[type="range"] { width: 100%; accent-color: var(--accent); }
.set-row.toggle { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; cursor: pointer; }
.set-row.toggle .t { font-weight: 600; font-size: 13.5px; }
.set-row.toggle small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.set-row.toggle input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.hint { font-size: 12px; color: var(--muted); margin: 12px 0 0; }
.toc-list { display: flex; flex-direction: column; }
.toc-item { text-align: left; border: 0; background: transparent; padding: 9px 14px; border-radius: 8px; }
.toc-item:hover { background: var(--surface-2); }

.endcard {
  position: absolute; z-index: 25; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(380px, calc(100% - 32px)); padding: 22px; border-radius: 16px; text-align: center;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .4);
}
.endcard h3 { margin: 0 0 6px; }
.endcard p { margin: 0 0 16px; color: var(--muted); word-break: break-all; }
.endcard .row { justify-content: center; }

.busy { position: absolute; inset: 0; z-index: 15; display: grid; place-items: center; pointer-events: none; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid rgba(128, 128, 128, .3); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- selection ---------- */
.grid-tools { display: flex; align-items: center; gap: 8px; }
.crumb .btn.small { margin-left: auto; }
.crumb .btn.small + .btn { margin-left: 0; }
.card .check {
  position: absolute; left: 7px; top: 7px; z-index: 2; width: 24px; height: 24px; border-radius: 50%;
  display: none; place-items: center; background: rgba(255, 255, 255, .92);
  border: 2px solid rgba(0, 0, 0, .25); box-shadow: 0 1px 4px rgba(0, 0, 0, .25);
}
.card .check svg { width: 14px; height: 14px; stroke-width: 3; color: #fff; display: none; }
.selecting .card .check { display: grid; }
.selecting .card .cover { transition: transform .12s ease, outline-color .12s ease; outline: 3px solid transparent; outline-offset: 2px; }
.selecting .card:hover .cover { transform: none; }
.selecting .card.selected .cover { outline-color: var(--accent); transform: scale(.96); }
.selecting .card.selected .check { background: var(--accent); border-color: var(--accent); }
.selecting .card.selected .check svg { display: block; }
.selecting .series .cover.stacked::before, .selecting .series .cover.stacked::after { outline: none; }
.selbar {
  position: fixed; left: 50%; bottom: calc(18px + var(--safe-bottom)); transform: translateX(-50%); z-index: 40;
  display: flex; align-items: center; gap: 6px; padding: 8px 10px 8px 16px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--line); box-shadow: 0 12px 40px rgba(0, 0, 0, .22);
  max-width: calc(100% - 24px); white-space: nowrap;
}
#sel-count { font-weight: 600; margin-right: 6px; }
.btn.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-fill:hover { filter: brightness(1.06); background: var(--danger); }
.btn.danger-fill:disabled { opacity: .4; }
#library.selecting #lib-main { padding-bottom: calc(96px + var(--safe-bottom)); }
.remove-box p { color: var(--muted); margin: 0 0 18px; line-height: 1.55; }
.remove-box .names { margin: 0 0 14px; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); font-size: 13px; max-height: 140px; overflow-y: auto; word-break: break-all; }
.remove-box .row { justify-content: flex-end; }
.toast.actionable { pointer-events: auto; }
.toast-action { margin-left: 14px; border: 0; background: none; color: #8fb6ff; font-weight: 700; padding: 0; }

/* iPad/iPhone (app and home-screen web app): no image callouts or text selection when long-pressing the shelf. */
[data-platform="ios"] #library, [data-platform="pwa"] #library { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
[data-platform="ios"] #library input, [data-platform="pwa"] #library input { -webkit-user-select: text; user-select: text; }
/* Under 16px, focusing a field makes iPhone WebKit zoom the page in and leave it zoomed. */
[data-platform="ios"] input, [data-platform="ios"] select,
[data-platform="pwa"] input, [data-platform="pwa"] select { font-size: 16px; }
[data-platform="ios"] .page img, [data-platform="ios"] .slot img,
[data-platform="pwa"] .page img, [data-platform="pwa"] .slot img { -webkit-touch-callout: none; }

/* Installable web app: no folder access (books are imported into its storage), no window to fullscreen once installed. */
.hint-import { display: none; }
[data-platform="pwa"] .hint-import { display: block; }
[data-platform="pwa"] .hint-folder,
[data-platform="pwa"] #btn-folder,
[data-platform="pwa"] #empty-folder,
[data-platform="pwa"] .drop-box .folder-hint { display: none; }
@media (display-mode: standalone), (display-mode: fullscreen) {
  [data-platform="pwa"] #r-full-btn { display: none; }
}
.storage-info { font-size: 13px; line-height: 1.6; margin-bottom: 10px; }
.storage-info small { display: block; color: var(--muted); font-size: 12px; margin-top: 4px; }

/* ---------- drag and drop ---------- */
.drop {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: 24px;
  background: rgba(20, 24, 40, .45); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  pointer-events: none;
}
.drop-box {
  display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center;
  width: min(460px, 100%); padding: 36px 28px; border-radius: 18px;
  border: 2px dashed var(--accent); background: var(--surface); color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.drop-box svg { width: 40px; height: 40px; color: var(--accent); }
.drop-box b { font-size: 17px; }
.drop-box span { color: var(--muted); font-size: 13px; }

/* ---------- small screens ---------- */
@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 14px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .actions .btn span { display: none; }
  .actions .btn { padding: 0 10px; }
  #lib-main { padding-left: 14px; padding-right: 14px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 18px 12px; }
  .shelf-row { grid-auto-columns: 96px; }
  .r-quick { display: none; }
  .selbar { left: 12px; right: 12px; transform: none; justify-content: space-between; }
  #sel-all { display: none; }
  .sheet { left: 0; right: 0; bottom: 0; top: auto; width: 100%; max-height: 72vh; border-radius: 16px 16px 0 0; padding-bottom: calc(16px + var(--safe-bottom)); }
  .chrome.bottom .btn.small { padding: 0 6px; font-size: 12px; }
}

/* Android: no window to fullscreen, no keyboard shortcuts. */
[data-platform="ios"] .drop-box .folder-hint,
[data-platform="ios"] #r-full-btn,
[data-platform="android"] .drop-box .folder-hint,
[data-platform="android"] #r-full-btn,
[data-platform="android"] .sheet .hint.keys { display: none; }

/* Paged modes handle horizontal swipes themselves (pointer events must not be cancelled by panning). */
#reader[data-layout="paged"] #stage { touch-action: pinch-zoom; }
#reader[data-layout="paged"] .paged.fit-width { touch-action: pan-y pinch-zoom; }
