:root {
  --bg: #f8f7f4; --surface: #fff; --ink: #292825; --muted: #88847e; --line: #e8e5df;
  --accent: #8B789D; --shadow: 0 12px 45px #0002; --scroll-thumb: #c8c2cc;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
}
[data-theme="dark"] {
  --bg: #191918; --surface: #242422; --ink: #f2f0eb; --muted: #a5a199; --line: #3b3935;
  --shadow: 0 12px 45px #0006; --scroll-thumb: #57525a;
}
body[data-reader-bg="black"], body[data-reader-bg="dark"] { --topbar-ink: #fff; }
body[data-reader-bg="light"], body[data-reader-bg="white"] { --topbar-ink: #171615; }

* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: var(--scroll-thumb) transparent; }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 20px; }
*::-webkit-scrollbar-thumb:hover { background: var(--accent); }

html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; }
body { background: var(--bg); color: var(--ink); font-family: "Montserrat", sans-serif; }
button, input, select { font: inherit; }
button { color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar { position: fixed; z-index: 40; inset: 0 0 auto; height: calc(68px + var(--safe-top)); padding: var(--safe-top) 30px 0; display: flex; justify-content: space-between; align-items: center; pointer-events: none; }
.topbar > * { pointer-events: auto; }
.logo { font: 600 29px "Lora", serif; letter-spacing: -1.5px; text-decoration: none; color: var(--topbar-ink, var(--ink)); transition: color .2s; }
.logo span { color: var(--accent); }
.icon-button { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; display: grid; place-items: center; font-size: 25px; }
.icon-button:hover { background: var(--line); }
#themeToggle { color: var(--topbar-ink, var(--ink)); }
#themeToggle:hover { background: transparent; }
#themeIcon { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* Home */
#home { height: 100%; overflow-y: auto; padding: 145px 20px 60px; }
.welcome { max-width: 590px; margin: auto; text-align: center; }
.eyebrow { color: var(--muted); text-transform: uppercase; letter-spacing: 2px; font-size: 10px; font-weight: 600; }
h1 { font: 500 clamp(38px, 6vw, 60px) "Lora", serif; letter-spacing: -2px; line-height: 1.13; margin: 17px 0 13px; }
.subtitle { color: var(--muted); font-size: 13px; margin-bottom: 46px; line-height: 1.8; }
.subtitle a { color: var(--ink); text-decoration-color: var(--accent); text-underline-offset: 4px; }
.subtitle a:hover { color: var(--accent); }
.dropzone { min-height: 170px; display: flex; flex-direction: column; justify-content: center; align-items: center; cursor: pointer; }
#fileInput:focus-visible + .dropzone { outline: 2px solid var(--accent); outline-offset: 6px; }
.upload-icon { font-size: 35px; line-height: 1; color: var(--accent); margin-bottom: 18px; transition: color .2s, transform .2s; }
.dropzone.dragging .upload-icon { color: #c2b7cc; transform: scale(1.12); }
.drop-title { font-size: 13px; font-weight: 600; }
.drop-subtitle { color: var(--muted); font-size: 11px; margin-top: 7px; }
.drop-note { color: var(--muted); font-size: 10px; margin-top: 24px; }

.library { max-width: 850px; margin: 70px auto 0; }
.library-heading { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 17px; }
.library-heading h2 { font: 500 23px "Lora", serif; margin: 0; }
.library-heading span { font-size: 10px; color: var(--muted); }
.library-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.library-card { background: var(--surface); border: 1px solid var(--line); padding: 16px; min-height: 110px; display: flex; flex-direction: column; justify-content: space-between; border-radius: 3px; }
.library-card:hover { border-color: var(--accent); }
.card-open { background: none; border: 0; padding: 0; text-align: left; width: 100%; display: flex; flex-direction: column; }
.card-open strong { font-size: 12px; overflow-wrap: anywhere; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
.card-cover { width: 100%; aspect-ratio: 2 / 3; object-fit: cover; background: var(--bg); border-radius: 2px; margin-bottom: 10px; }
.card-open small { color: var(--muted); margin-top: 8px; font-size: 10px; }
.remove-card { background: none; border: 0; color: var(--muted); font-size: 10px; padding: 10px 0 0; text-align: right; }

/* Reader */
#reader { position: fixed; inset: 0; }
#stage { position: absolute; inset: 0; overflow: hidden; display: flex; justify-content: center; align-items: center; touch-action: none; user-select: none; }
#stage.bg-black { background: #000; } #stage.bg-dark { background: #232023; }
#stage.bg-light { background: #f8f6f0; } #stage.bg-white { background: #fff; }
#page { position: absolute; left: 50%; top: 50%; transform-origin: center center; will-change: transform; cursor: grab; }
#page.dragging { cursor: grabbing; }
#page img { display: block; max-width: none; max-height: none; width: auto; height: auto; pointer-events: none; -webkit-user-drag: none; }

#menuButton { position: absolute; z-index: 20; right: 24px; bottom: calc(22px + var(--safe-bottom)); width: 47px; height: 47px; border: 0; border-radius: 50%; background: var(--surface); box-shadow: var(--shadow); display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 5px; }
#menuButton span { width: 17px; height: 1.5px; background: var(--ink); }

#menu { position: fixed; z-index: 50; right: 20px; bottom: calc(80px + var(--safe-bottom)); width: min(370px, calc(100vw - 25px)); max-height: calc(100dvh - 100px - var(--safe-bottom)); overflow-y: auto; overscroll-behavior: contain; background: var(--surface); border: 1px solid var(--line); border-radius: 5px; box-shadow: var(--shadow); padding: 22px; }
.menu-eyebrow { font-size: 10px; color: var(--muted); letter-spacing: 1.5px; margin: 0 0 7px; }
#comicTitle { font-size: 16px; font-weight: 600; overflow-wrap: anywhere; margin: 0; }
.progress { margin: 20px 0 10px; }
.progress-label { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-bottom: 8px; }
.progress-label strong { color: var(--ink); }
.progress-track { height: 2px; background: var(--line); }
#progressFill { display: block; height: 100%; width: 0; background: var(--accent); }
.reading-hint { font-size: 10px; color: var(--muted); line-height: 1.6; margin: 0 0 18px; }

.group { border-top: 1px solid var(--line); padding: 16px 0; }
.group h3 { margin: 0 0 12px; font-size: 11px; text-transform: uppercase; letter-spacing: 1.3px; color: var(--muted); }
.nav-row, .zoom-row, .button-row { display: flex; align-items: center; gap: 7px; }
.button-row { margin-top: 10px; }
.control { min-width: 37px; height: 36px; border: 1px solid var(--line); background: transparent; border-radius: 3px; display: grid; place-items: center; font-size: 14px; }
.control:hover, .soft:hover { border-color: var(--accent); }
.page-jump { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--muted); margin: auto; }
.page-jump input { width: 48px; padding: 6px 3px; text-align: center; background: var(--bg); color: var(--ink); border: 1px solid var(--line); appearance: textfield; -moz-appearance: textfield; }
.page-jump input::-webkit-inner-spin-button, .page-jump input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.page-jump input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); caret-color: var(--accent); }
.zoom-label { font-size: 12px; min-width: 48px; text-align: center; color: var(--muted); }
.text-button { background: none; border: 0; font-size: 11px; padding: 8px 3px; }
.zoom-row .text-button { margin-left: auto; }
.soft { flex: 1; min-height: 37px; background: transparent; border: 1px solid var(--line); border-radius: 3px; font-size: 11px; }
.soft.full { width: 100%; }

.setting { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 14px; font-size: 12px; }
.setting label { color: var(--muted); }
.setting select { max-width: 160px; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 3px; padding: 7px 8px; font-size: 11px; }
.toggle-switch { position: relative; width: 62px; height: 30px; flex-shrink: 0; border: 1px solid var(--line); border-radius: 30px; background: var(--bg); padding: 0; transition: background .2s, border-color .2s; }
.toggle-switch[aria-checked="true"] { background: var(--accent); border-color: var(--accent); }
.toggle-thumb { position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px #0002; transition: transform .22s ease; }
.toggle-switch[aria-checked="true"] .toggle-thumb { transform: translateX(32px); }
.toggle-state { position: absolute; top: 0; right: 8px; height: 28px; display: flex; align-items: center; font-size: 8px; font-weight: 700; letter-spacing: .5px; color: var(--muted); }
.toggle-switch[aria-checked="true"] .toggle-state { right: auto; left: 8px; color: #fff; }

#controlsPanel { border-top: 1px solid var(--line); padding: 15px 0; }
.controls-intro { font-size: 11px; color: var(--muted); line-height: 1.7; margin: 0 0 13px; }
.controls-intro.spaced { margin-top: 14px; }
.shortcut-list { display: grid; grid-template-columns: 1fr auto; gap: 10px 12px; align-items: center; font-size: 11px; }
.shortcut-key { color: var(--muted); font-size: 10px; text-align: right; }
.key-setting { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 12px; font-size: 11px; }
.key-setting button { min-width: 75px; min-height: 30px; background: transparent; border: 1px solid var(--line); border-radius: 3px; font-size: 10px; }
.key-setting button.listening { border-color: var(--accent); color: var(--accent); }
.menu-footer { border-top: 1px solid var(--line); padding-top: 12px; display: flex; flex-direction: column; align-items: flex-start; }
.danger { color: #bd5149; }

#thumbnails { position: absolute; z-index: 29; top: calc(75px + var(--safe-top)); right: 20px; bottom: calc(80px + var(--safe-bottom)); width: min(300px, calc(100vw - 25px)); background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); padding: 14px; display: flex; flex-direction: column; }
.thumb-heading { display: flex; justify-content: space-between; align-items: center; font-size: 12px; padding-bottom: 12px; }
.thumb-grid { overflow: auto; overscroll-behavior: contain; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.thumb-item { border: 1px solid var(--line); background: var(--bg); padding: 4px; }
.thumb-item img { width: 100%; height: 95px; object-fit: contain; display: block; }
.thumb-item small { display: block; color: var(--muted); font-size: 9px; margin-top: 4px; }

#toast { position: fixed; z-index: 60; left: 50%; bottom: calc(25px + var(--safe-bottom)); transform: translate(-50%, 10px); opacity: 0; pointer-events: none; background: #292825; color: #fff; padding: 9px 16px; border-radius: 30px; font-size: 11px; transition: .2s; }
#toast.visible { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 600px) {
  .topbar { height: calc(58px + var(--safe-top)); padding-left: 17px; padding-right: 17px; }
  #home { padding: 115px 17px 50px; }
  h1 { font-size: 43px; }
  .dropzone { min-height: 190px; }
  #menuButton { right: 15px; bottom: calc(15px + var(--safe-bottom)); }
  #menu { right: 10px; bottom: calc(70px + var(--safe-bottom)); padding: 18px; }
  #thumbnails { right: 10px; top: calc(65px + var(--safe-top)); bottom: calc(70px + var(--safe-bottom)); }
}
