:root {
  --bg: #111214;
  --bg2: #1b1d21;
  --bg3: #26292e;
  --fg: #eceef1;
  --fg2: #9aa0a8;
  --accent: #ff8a3d;
  --ok: #4cc38a;
  --danger: #ff5b5b;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --tabs-h: calc(56px + var(--safe-bot));
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", system-ui, sans-serif;
  font-size: 15px; line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

/* ---------- 상단바 */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: rgba(17,18,20,.92); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid #22252a;
}
.top h1 { flex: 1; min-width: 0; margin: 0; font-size: 17px; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { min-width: 40px; height: 40px; border-radius: 10px; display: inline-flex;
  align-items: center; justify-content: center; font-size: 20px; padding: 0 8px; }
.icon-btn:active { background: var(--bg3); }
.pill { padding: 7px 12px; border-radius: 999px; background: var(--bg3); font-size: 13px; font-weight: 600; white-space: nowrap; }
.pill.accent { background: var(--accent); color: #1a1a1a; }
.pill:disabled { opacity: .5; }

main { padding: 12px 12px calc(var(--tabs-h) + 20px); max-width: 1400px; margin: 0 auto; }

/* ---------- 하단 탭 */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: var(--tabs-h); padding-bottom: var(--safe-bot);
  display: flex; background: rgba(17,18,20,.95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid #22252a;
}
#tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 11px; color: var(--fg2); position: relative; }
#tabs a span { font-size: 20px; line-height: 1.2; }
#tabs a.on { color: var(--accent); }
#dl-badge:not(:empty) { position: absolute; top: 6px; left: 55%; background: var(--accent); color: #111;
  font-style: normal; font-size: 10px; font-weight: 700; border-radius: 9px; padding: 0 5px; min-width: 16px; text-align: center; }
body.reading #tabs { display: none; }

/* ---------- 그리드 */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 14px 10px; }
@media (min-width: 700px) { .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px 14px; } }
.card { display: block; min-width: 0; }
.cover { position: relative; aspect-ratio: 0.7; border-radius: 8px; overflow: hidden; background: var(--bg2);
  display: flex; align-items: center; justify-content: center; font-size: 34px; color: #444; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .2s; }
.cover img.ok { opacity: 1; }
.cover .badge { position: absolute; top: 5px; right: 5px; z-index: 2; background: rgba(0,0,0,.7); border-radius: 6px;
  padding: 1px 6px; font-size: 11px; font-weight: 700; color: var(--ok); }
.cover .kind { position: absolute; top: 5px; left: 5px; z-index: 2; background: rgba(0,0,0,.7); border-radius: 6px;
  padding: 1px 6px; font-size: 11px; color: #ddd; }
.cover .bar { position: absolute; left: 0; bottom: 0; height: 4px; background: var(--accent); z-index: 2; }
.card .name { margin-top: 6px; font-size: 13px; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden; word-break: break-all; }
.card .sub { font-size: 11px; color: var(--fg2); margin-top: 2px; }

.list .row { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid #22252a; }
.list .row .cover { width: 52px; flex: none; font-size: 20px; }
.list .row .meta { flex: 1; min-width: 0; }
.list .row .meta b { display: block; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .row .meta small { color: var(--fg2); font-size: 12px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty { text-align: center; color: var(--fg2); padding: 60px 20px; }
.section-title { font-size: 13px; color: var(--fg2); margin: 18px 2px 8px; font-weight: 600; }
.search-box { width: 100%; padding: 12px 14px; font-size: 16px; border-radius: 12px; border: 0; background: var(--bg2); color: var(--fg); outline: none; }
.progress { height: 6px; border-radius: 3px; background: var(--bg3); overflow: hidden; margin-top: 6px; }
.progress > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.storage { background: var(--bg2); border-radius: 12px; padding: 12px 14px; font-size: 13px; color: var(--fg2); }
.offline-banner { display: none; background: #3a2a14; color: #ffc58f; font-size: 12px; text-align: center; padding: 6px; }

#toast { position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px); transform: translateX(-50%) translateY(20px);
  background: #2d3036; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0;
  pointer-events: none; transition: all .2s; z-index: 100; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 뷰어 */
.reader { position: fixed; inset: 0; background: #000; z-index: 30; overflow: hidden; touch-action: pan-x pan-y pinch-zoom; }
.reader .stage { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.reader .stage.rtl { flex-direction: row-reverse; }
.reader .stage img { max-width: 100%; max-height: 100%; object-fit: contain; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; display: block; }
.reader .stage.two img { max-width: 50%; }
.reader .stage.fitw { overflow-y: auto; align-items: flex-start; display: block; text-align: center; }
.reader .stage.fitw img { max-width: 100%; max-height: none; width: 100%; height: auto; margin: 0 auto; }
@media (min-width: 900px) { .reader .stage.fitw img { width: auto; max-width: 100%; } }
.reader .scroll { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.reader .scroll img { display: block; width: 100%; max-width: 960px; margin: 0 auto; min-height: 40px; }
.reader .scroll img.ph { background: #0b0b0b; }
.reader .scroll .end { padding: 40px 20px calc(80px + var(--safe-bot)); text-align: center; }
.reader .loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #555; pointer-events: none; }

.reader .ui { position: absolute; left: 0; right: 0; z-index: 5; background: rgba(15,16,18,.94); transition: transform .2s, opacity .2s; }
.reader .ui.topbar { top: 0; padding: calc(var(--safe-top) + 6px) 8px 6px; display: flex; align-items: center; gap: 4px; }
.reader .ui.topbar .t { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader .ui.botbar { bottom: 0; padding: 10px 14px calc(var(--safe-bot) + 10px); }
.reader.hide-ui .ui.topbar { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader.hide-ui .ui.botbar { transform: translateY(100%); opacity: 0; pointer-events: none; }
.reader .slider-row { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--fg2); }
.reader .slider-row input { flex: 1; accent-color: var(--accent); }
.reader .slider-row input.rtl { direction: rtl; }
.reader .vol-row { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; }
.reader .vol-row .pill { max-width: 48%; overflow: hidden; text-overflow: ellipsis; }
.reader .pageno { position: absolute; bottom: calc(var(--safe-bot) + 6px); right: 10px; font-size: 11px; color: #777; z-index: 2; pointer-events: none; }
.reader.hide-ui .pageno { display: block; }
.reader:not(.hide-ui) .pageno { display: none; }

.endcard { position: absolute; inset: 0; z-index: 4; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: rgba(0,0,0,.85); text-align: center; padding: 20px; }
.endcard h3 { margin: 0; font-size: 18px; }
.endcard .pill { font-size: 15px; padding: 12px 20px; max-width: 90vw; overflow: hidden; text-overflow: ellipsis; }

/* ---------- 설정 시트 */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; background: var(--bg2); border-radius: 16px 16px 0 0;
  padding: 16px 16px calc(var(--safe-bot) + 16px); max-width: 560px; margin: 0 auto; }
.sheet h4 { margin: 0 0 12px; font-size: 16px; }
.opt { margin-bottom: 14px; }
.opt label { display: block; font-size: 12px; color: var(--fg2); margin-bottom: 6px; }
.seg { display: flex; background: var(--bg3); border-radius: 10px; padding: 3px; }
.seg button { flex: 1; padding: 8px 4px; border-radius: 8px; font-size: 13px; }
.seg button.on { background: var(--accent); color: #111; font-weight: 700; }
body.offline .offline-banner { display: block; }
