/* ============================================================
   reader.css — the graphic-novel reader.
   Same look as the music player, but "midnight": the player's two modes, made darker.
     data-theme="blue"   ← derived from the player's light mode  (navy + cyan, yellow buttons)
     data-theme="amber"  ← derived from the player's dark mode   (black + amber, mint buttons)
   Every text/background and border pair was checked against WCAG AA.
   ============================================================ */

@font-face {
  font-family: 'Press Start 2P';
  font-style: normal; font-weight: 400; font-display: swap;
  src: url('fonts/press-start-2p-v16-latin-regular.woff2') format('woff2'),
       url('fonts/press-start-2p-v16-latin-regular.ttf') format('truetype');
}
@font-face {
  font-family: 'VT323';
  font-style: normal; font-weight: 400; font-display: swap;
  src: url('fonts/vt323-v18-latin-regular.woff2') format('woff2');
}

:root {
  --pixel: 'Press Start 2P', ui-monospace, Menlo, Consolas, monospace;
  --ui: 'VT323', ui-monospace, Menlo, Consolas, monospace;
  --col: 920px;                       /* width of the reading column */
  color-scheme: dark;
}

:root[data-theme="blue"] {
  --bg: #00001a;  --surface: #07072e;  --surface-2: #0e0e45;
  --border: #00c9a3;  --text: #00e6b8;  --dim: #7fd1c0;
  --accent: #ffcc00;  --hot: #ff00cc;  --focus: #00aaff;
  --btn-bg: #0c0c14;  --btn-fg: #ffcc00;  --btn-bd: #d9ad00;
  --glow: rgba(0, 170, 255, .55);
  --ph-a: #0a0a2c;  --ph-b: #0d0d38;
}
:root[data-theme="amber"] {
  --bg: #000000;  --surface: #0b0b0b;  --surface-2: #161616;
  --border: #b38f00;  --text: #e6b800;  --dim: #b89c3a;
  --accent: #00ffaa;  --hot: #ff00cc;  --focus: #00ffaa;
  --btn-bg: #05050f;  --btn-fg: #00e69a;  --btn-bd: #00b87a;
  --glow: rgba(0, 255, 170, .45);
  --ph-a: #0c0c0c;  --ph-b: #131313;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background: var(--bg); scroll-behavior: auto; }
body {
  margin: 0; min-height: 100vh; min-height: 100dvh;
  background: var(--bg) repeating-linear-gradient(0deg, rgba(255, 255, 255, .03) 0 1px, transparent 1px 3px);  /* scanlines show only in the side gutters */
  color: var(--text);
  font: 22px/1.25 var(--ui);
  -webkit-font-smoothing: none;
}
a { color: var(--accent); }
h2, h3 { font-family: var(--pixel); font-weight: 400; line-height: 1.5; margin: 0; }
p { margin: 0 0 .6em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
main:focus { outline: none; }

.sr-only {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
.skip-link {
  position: absolute; left: 8px; top: -80px; z-index: 100; padding: 10px 14px;
  background: var(--btn-fg); color: var(--btn-bg); font: 11px var(--pixel); text-decoration: none;
}
.skip-link:focus { top: 8px; }

/* ---------- buttons, inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 10px;
  font: 11px/1 var(--pixel); text-transform: uppercase; letter-spacing: .02em; white-space: nowrap;
  color: var(--btn-fg); background: var(--btn-bg);
  border: 1px solid var(--btn-bd); border-radius: 0;
  text-decoration: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { color: var(--hot); border-color: var(--hot); text-shadow: 0 0 8px var(--glow); }
.btn:active { color: var(--btn-bg); background: var(--btn-fg); text-shadow: none; }
.btn[aria-pressed="true"] { color: var(--btn-bg); background: var(--btn-fg); }
.btn[disabled] { opacity: .4; cursor: not-allowed; text-shadow: none; }
.btn[disabled]:hover { color: var(--btn-fg); border-color: var(--btn-bd); }
.btn-sq { min-width: 38px; padding: 0 6px; }
.btn-mini { min-height: 30px; padding: 0 8px; font-size: 10px; margin-left: 8px; }
.theme-name b { font-weight: 400; }

select, input[type="search"] {
  width: 100%; min-height: 38px; padding: 0 12px;
  font: 21px var(--ui); color: var(--text); background-color: var(--surface-2);
  border: 1px solid var(--border); border-radius: 0;
}
select {
  appearance: none; -webkit-appearance: none; padding-right: 34px; text-overflow: ellipsis;
  background-image: linear-gradient(45deg, transparent 50%, var(--text) 50%), linear-gradient(135deg, var(--text) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 12px) 50%;
  background-size: 6px 6px, 6px 6px; background-repeat: no-repeat;
}
select:disabled { opacity: .5; }
select option { background: var(--surface); color: var(--text); }
input[type="search"]::placeholder { color: var(--dim); opacity: 1; }

input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 120px; height: 28px; margin: 0; background: transparent; cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track { height: 8px; background: var(--surface-2); border: 1px solid var(--btn-bd); }
input[type="range"]::-moz-range-track { height: 8px; background: var(--surface-2); border: 1px solid var(--btn-bd); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 18px; margin-top: -6px; background: var(--btn-fg); border: 2px solid var(--btn-fg); border-radius: 0; }
input[type="range"]::-moz-range-thumb { width: 12px; height: 14px; background: var(--btn-fg); border: 2px solid var(--btn-fg); border-radius: 0; }
input[type="range"]:hover::-webkit-slider-thumb { background: var(--hot); border-color: var(--hot); }
input[type="range"]:hover::-moz-range-thumb { background: var(--hot); border-color: var(--hot); }

/* ---------- control bar ---------- */
.bar {
  position: sticky; top: 0; z-index: 40;
  padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 3px double var(--border);
  transition: transform .22s ease;
  will-change: transform;
}
.bar.is-hidden { transform: translateY(-105%); }
.bar-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px; padding-bottom: 8px; }
.bar-music { padding-top: 8px; border-top: 1px dashed var(--border); gap: 8px 12px; }
.grp { display: flex; align-items: center; gap: 6px; min-width: 0; }
.grp-wrap { flex-wrap: wrap; }
.grp-chapters { flex: 1 1 340px; }
.pick { flex: 1 1 auto; min-width: 0; display: block; }
.page-ind { font: 10px var(--pixel); color: var(--accent); white-space: nowrap; min-width: 14ch; text-align: right; }   /* fixed width: the bar never shifts as the numbers change */
.grp-actions { margin-left: auto; }

.mp-now { flex: 1 1 200px; min-width: 0; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.mp-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 23px; }
.mp-time { flex: none; color: var(--dim); font-size: 21px; }
.btn-play { font-size: 13px; }

.progress { height: 5px; margin: 0 calc(-1 * max(10px, env(safe-area-inset-left))); background: var(--surface-2); overflow: hidden; }
.progress i {
  display: block; height: 100%; transform-origin: 0 50%; transform: scaleX(0);
  background: linear-gradient(90deg, #ff00cc, #ff6600, #ffcc00, #00ffcc, #00aaff, #cc00ff);   /* the player's rainbow */
}

/* ---------- the comic ---------- */
#reader { max-width: var(--col); margin: 0 auto; position: relative; }
@media (min-width: 1000px) { #reader { border-inline: 3px double var(--border); } }

.status { padding: 48px 16px; text-align: center; font-size: 26px; }
.status .btn { margin-top: 14px; }

.chapter-card, .end-card {
  padding: clamp(26px, 6vw, 52px) 16px; text-align: center;
  background: var(--surface); border-block: 3px double var(--border);
}
.chapter-card .kicker, .end-card .kicker { font: 10px var(--pixel); color: var(--accent); letter-spacing: .2em; text-transform: uppercase; }
.chapter-card h2, .end-card h2 { margin-top: 14px; font-size: clamp(14px, 3.2vw, 20px); color: var(--text); text-shadow: 0 0 10px var(--glow); }
.chapter-card .count { margin-top: 12px; color: var(--dim); }
.end-card .grp { justify-content: center; flex-wrap: wrap; margin-top: 18px; }
.end-card p { margin-top: 10px; color: var(--dim); }

.page {
  position: relative; margin: 0; width: 100%;
  aspect-ratio: var(--w) / var(--h);                       /* space is reserved before the image arrives → nothing ever jumps */
  background: repeating-linear-gradient(135deg, var(--ph-a) 0 14px, var(--ph-b) 14px 28px);
  overflow: hidden; contain: layout paint style;
}
@supports not (aspect-ratio: 1 / 1) {
  .page::after { content: ''; display: block; padding-top: calc(var(--h) / var(--w) * 100%); }
}
.page::before {
  content: attr(data-label); position: absolute; inset: 0; z-index: 0; display: grid; place-items: center;
  font: 10px var(--pixel); letter-spacing: .2em; color: var(--dim);
}
.page.is-loaded::before { content: none; }
.page img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
  opacity: 0; transition: opacity .25s ease; -webkit-user-drag: none; user-select: none;
}
.page.is-loaded img { opacity: 1; }
.page img:not([src]) { visibility: hidden; }
.page[data-state="error"]::before { content: 'PAGE FAILED TO LOAD'; color: var(--hot); }
.page .retry { position: absolute; left: 50%; top: calc(50% + 28px); z-index: 2; transform: translateX(-50%); }

/* ---------- dialogs ---------- */
dialog {
  width: min(680px, calc(100vw - 20px)); max-height: min(88vh, 760px);
  padding: 0; overflow: hidden;
  color: var(--text); background: var(--surface); border: 3px double var(--border);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(0, 0, 0, .75); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 0; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.dlg-head h2 { font-size: 13px; color: var(--accent); }
.dlg-body { padding: 12px 14px 16px; overflow: auto; overscroll-behavior: contain; }
.dlg-body h3 { margin: 0 0 8px; font-size: 11px; color: var(--accent); }
.dlg-body section + section { margin-top: 24px; }
.dlg-body .grp-wrap { margin-top: 10px; }
.note { color: var(--dim); }

.pl { list-style: none; margin: 0 0 10px; padding: 0; border-top: 1px dashed var(--border); }
.pl li { display: flex; align-items: center; gap: 6px; padding: 5px 0; border-bottom: 1px dashed var(--border); }
.pl .t { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pl .t small { color: var(--dim); font-size: 20px; }
.pl li.is-current .t { color: var(--accent); }
.pl li.is-missing .t { color: var(--dim); text-decoration: line-through; }
.pl .btn { min-height: 34px; min-width: 36px; padding: 0 6px; font-size: 9px; }
.search { display: block; margin-bottom: 8px; }

.donate-card { margin-top: 14px; padding: 14px; text-align: center; background: var(--bg); border: 1px dashed var(--border); }
.qr { display: inline-block; padding: 14px; background: #fff; border: 2px solid var(--border); line-height: 0; }
.qr img, .qr canvas { display: block; width: 200px; height: 200px; }
.qr-fallback { display: block; max-width: 200px; padding: 10px 4px; color: #222; font: 18px/1.2 var(--ui); line-height: 1.2; }
.addr-label { margin: 12px 0 4px; color: var(--dim); }
.addr { display: block; margin: 0 0 12px; padding: 8px; font: 14px/1.5 ui-monospace, Menlo, Consolas, monospace; word-break: break-all; color: var(--text); background: var(--surface); border: 1px solid var(--border); user-select: all; }
.donate-card .grp { justify-content: center; }

.toast {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); z-index: 90; transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 24px)); padding: 10px 14px; text-align: center;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: 0 0 0 3px var(--bg);
}

/* ---------- smaller screens: the same bar, stacked ---------- */
@media (max-width: 860px) {
  .grp-chapters { flex: 1 1 100%; order: 3; }
  .grp-actions { margin-left: auto; }
  .bar-nav { justify-content: space-between; }
}
@media (max-width: 560px) {
  body { font-size: 22px; }
  .bar { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .progress { margin-left: calc(-1 * max(8px, env(safe-area-inset-left))); margin-right: calc(-1 * max(8px, env(safe-area-inset-right))); }
  .bar-row { gap: 6px; padding-bottom: 6px; }
  .bar .btn { font-size: 9px; padding: 0 8px; min-height: 40px; }
  .bar .btn-sq { min-width: 40px; padding: 0; }
  .bar select { min-height: 40px; font-size: 20px; }
  .theme-name, .ind-ch, .mp-time, .btn-mute { display: none; }
  .page-ind { min-width: 5ch; }
  .bar-music { flex-wrap: nowrap; padding-top: 6px; gap: 6px; }
  .mp-now { flex: 1 1 0; }
  .mp-title { font-size: 20px; }
  .grp-vol input[type="range"] { width: 56px; }
}
@media (max-width: 360px) {
  .grp-vol { display: none; }
}

/* touch devices: comfortable targets */
@media (pointer: coarse) {
  .btn, select, input[type="search"] { min-height: 44px; }
  .btn-sq { min-width: 44px; }
  .pl .btn { min-height: 44px; min-width: 44px; }
  input[type="range"] { height: 40px; }
}

html:has(dialog[open]) { overflow: hidden; }

/* ---------- accessibility preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
@media (prefers-contrast: more) {
  :root[data-theme="blue"]  { --border: #33ffd6; --dim: #b4f0e4; --btn-bd: #ffe066; }
  :root[data-theme="amber"] { --border: #ffd633; --dim: #ffe580; --btn-bd: #66ffc4; }
  .page { outline: 1px solid var(--border); }
}
@media (forced-colors: active) {
  .btn, select, dialog, .bar, .qr { border: 1px solid CanvasText; }
  .progress i { background: Highlight; }
  .page.is-loaded::before { content: none; }
}
