/* Trình đọc truyện */
html { scroll-behavior: auto; scroll-padding-top: 0; }
body.reader { background: #182225; color: #F3EDDD; min-height: 100vh; }

.r-top, .r-bottom {
  position: fixed; left: 0; right: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  background: #223135; color: #F3EDDD;
  transition: transform .2s ease;
}
.r-top { top: 0; height: 56px; padding: 0 8px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
.r-bottom { bottom: 0; height: calc(52px + env(safe-area-inset-bottom)); padding: 0 8px env(safe-area-inset-bottom); gap: 10px; }
.ui-hidden .r-top { transform: translateY(-100%); }
.ui-hidden .r-bottom { transform: translateY(100%); }

.r-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: rgba(243, 237, 221, .16); }
.r-progress i { display: block; width: 0; height: 100%; background: #E08A5F; transition: width .15s linear; }

.r-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; flex: none;
  min-width: 44px; height: 44px; padding: 0 9px;
  background: transparent; color: inherit; border: 0; border-radius: 4px;
  font: 500 .9rem/1 var(--ui); text-decoration: none; cursor: pointer;
}
.r-btn:hover { background: rgba(243, 237, 221, .14); }
.r-btn[disabled] { opacity: .35; cursor: default; background: transparent; }
.r-btn:focus-visible, .r-zoom-val:focus-visible, .r-page input:focus-visible, .r-slider:focus-visible { outline-color: #F2B48F; }

.r-heading { flex: 1 1 auto; min-width: 0; padding: 0 6px; line-height: 1.15; }
.r-label { display: block; font-size: .72rem; letter-spacing: .05em; text-transform: uppercase; color: #A9C2BC; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-title { font: 400 1.22rem/1.1 var(--hand); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-tools { display: flex; align-items: center; gap: 2px; flex: none; }

.r-zoom { display: inline-flex; align-items: center; margin-inline: 4px; border-inline: 1px solid rgba(243, 237, 221, .18); padding-inline: 2px; }
.r-zoom-val { min-width: 54px; height: 44px; background: transparent; color: inherit; border: 0; font: 500 .84rem/1 var(--ui); cursor: pointer; border-radius: 4px; font-variant-numeric: tabular-nums; }
.r-zoom-val:hover { background: rgba(243, 237, 221, .14); }
.r-survey { background: #E6F2F1; color: #223135; border-radius: 999px; padding: 0 14px 0 10px; height: 38px; font-weight: 600; margin-left: 4px; }
.r-survey:hover { background: #cfe5e3; }

.r-page { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; white-space: nowrap; flex: none; font-variant-numeric: tabular-nums; }
.r-page input {
  width: 3.4em; height: 36px; text-align: center; font: 500 .95rem/1 var(--ui);
  background: #182225; color: inherit; border: 1px solid rgba(243, 237, 221, .4); border-radius: 4px;
  -moz-appearance: textfield; appearance: textfield;
}
.r-page input::-webkit-outer-spin-button, .r-page input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.r-slider { flex: 1 1 auto; min-width: 40px; height: 36px; accent-color: #E08A5F; margin: 0; cursor: pointer; }
.r-bottom .r-spacer { flex: 1 1 auto; }

@media (max-width: 720px) {
  .r-zoom, .r-full { display: none; }
  .r-survey { padding: 0 10px; min-width: 40px; }
  .r-survey span { display: none; }
  .r-title { font-size: 1.1rem; }
}
@media (max-width: 460px) { .r-slider { display: none; } .r-bottom { justify-content: space-between; } }

/* Trang truyện */
#pages {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  width: max-content; min-width: 100%;
  padding: 68px 0 16px;
}
.pg {
  position: relative; margin: 0; width: var(--pw, 860px); aspect-ratio: var(--ratio, 0.6663);
  background: var(--paper); color: #c9c3ae; flex: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.pg::before { content: attr(data-n); position: absolute; inset: 0; display: grid; place-items: center; font: 400 2.2rem/1 var(--hand); }
.pg img { position: relative; display: block; width: 100%; height: 100%; pointer-events: none; -webkit-user-drag: none; -webkit-user-select: none; user-select: none; }
.pg:not(.ready) img { visibility: hidden; }
.pg.failed { cursor: pointer; }
.pg.failed::before { content: "Không tải được trang này. Chạm để thử lại."; font: 500 1rem/1.4 var(--ui); padding: 24px; text-align: center; color: var(--ink); }

/* Chế độ lật trang */
body.flip { overflow: hidden; }
body.flip #stage { position: fixed; top: 56px; bottom: calc(52px + env(safe-area-inset-bottom)); left: 0; right: 0; overflow: auto; display: flex; }
body.flip.ui-hidden #stage { top: 0; bottom: 0; }
body.flip #pages { flex-direction: row; gap: 0; margin: auto; padding: 8px; width: auto; min-width: 0; cursor: default; }
body.flip .pg { display: none; }
body.flip .pg.on { display: block; }
body.flip .pg.on + .pg.on { margin-left: 2px; }
body.flip #pages[hidden] { display: none; }
body.flip .endcard { display: none; margin: auto; }
body.flip .endcard.on { display: block; }

/* Cuối chương */
.endcard {
  width: min(100% - 24px, 620px); margin: 10px auto calc(76px + env(safe-area-inset-bottom));
  padding: clamp(22px, 4vw, 36px); background: var(--paper); color: var(--ink);
  border: 2px solid #000; text-align: center;
}
.end-kicker { margin: 0; font: 400 1.1rem/1.2 var(--hand); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-soft); }
.endcard h2 { font-size: clamp(1.8rem, 5vw, 2.5rem); margin: .1em 0 .7em; }
.end-actions { display: flex; flex-direction: column; gap: 10px; align-items: stretch; max-width: 440px; margin-inline: auto; }
.end-survey { background: var(--bubble); color: var(--ink); }
.end-survey:hover { background: #d3e8e6; }
.end-link { display: inline-block; margin-top: 14px; color: var(--ink-soft); font-weight: 500; }

/* Lưới xem nhanh */
.thumbs {
  width: min(1040px, 94vw); max-height: 88vh; padding: 0;
  background: #223135; color: #F3EDDD; border: 1px solid rgba(243, 237, 221, .3);
}
.thumbs::backdrop { background: rgba(10, 16, 18, .7); }
.thumbs-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 18px; background: #223135; }
.thumbs-head h2 { font-size: 1.5rem; }
.thumbs-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); padding: 4px 18px 18px; }
.thumb { padding: 0; background: none; border: 0; color: inherit; cursor: pointer; font: 500 .84rem/1.8 var(--ui); font-variant-numeric: tabular-nums; }
.thumb img { display: block; width: 100%; aspect-ratio: var(--ratio, 0.6663); background: var(--paper); pointer-events: none; -webkit-user-drag: none; outline: 2px solid transparent; outline-offset: 2px; }
.thumb:hover img { outline-color: rgba(243, 237, 221, .6); }
.thumb.current img { outline-color: #E08A5F; }

/* Gợi ý đọc tiếp */
.toast {
  position: fixed; left: 50%; bottom: calc(66px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 15;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px 8px;
  width: max-content; max-width: calc(100vw - 24px);
  padding: 6px 8px 6px 14px; background: var(--paper); color: var(--ink);
  border: 2px solid #000; font-size: .95rem;
}
.toast[hidden] { display: none; }
.toast button { min-height: 38px; padding: 4px 12px; border: 0; border-radius: 4px; font: 600 .9rem/1 var(--ui); cursor: pointer; background: var(--accent); color: var(--paper); }
.toast button.plain { background: transparent; color: var(--ink-soft); font-weight: 500; }

.noscript { padding: 96px 20px; text-align: center; }

/* Không hỗ trợ in */
@media print {
  body.reader > * { display: none !important; }
  body.reader::before { content: "Bản đọc trực tuyến không hỗ trợ in."; display: block; padding: 40px; color: #000; font-size: 16pt; }
}
