/* Showcase page-curl viewer. Scoped to body.curl-viewer so other pages are unaffected;
   overrides the legacy fixed topbar/bottombar/stage from public-viewer.css without editing it. */
body.curl-viewer {
  --curl-accent: var(--viewer-primary, #6b4fa0);
  --curl-accent-soft: #ece4f7;
  --curl-paper: #fffdf9;
  --curl-bg: #fbf8f3;
  --curl-text: #3a2f4a;
  --curl-danger: #a1302f;
  --curl-page-a: #f6f1fb;
  --curl-page-b: #ebe3f5;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
  background: var(--curl-bg);
  color: var(--curl-text);
}
@supports (color: color-mix(in srgb, red 50%, white)) {
  body.curl-viewer {
    --curl-accent-soft: color-mix(in srgb, var(--curl-accent) 16%, #fff);
    --curl-page-a: color-mix(in srgb, var(--curl-accent) 6%, #fff);
    --curl-page-b: color-mix(in srgb, var(--curl-accent) 14%, #fff);
  }
}
html:has(> body.curl-viewer) { height: 100%; overflow: hidden; }
body.curl-viewer [hidden] { display: none !important; }

/* Existing header, now in document flow. */
body.curl-viewer .viewer-topbar {
  position: relative;
  inset: auto;
  transform: none;
  z-index: 3;
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 12px;
  height: auto;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--curl-accent-soft);
}
body.curl-viewer .viewer-brand { display: flex; align-items: center; gap: 10px; flex: 1 1 14rem; min-width: 0; }
body.curl-viewer .viewer-brand img { flex: 0 0 auto; max-height: 40px; width: auto; }
body.curl-viewer .viewer-brand > div { min-width: 0; }
body.curl-viewer .viewer-brand h1 {
  margin: 0;
  font-size: clamp(.95rem, 2.4vw, 1.15rem);
  line-height: 1.25;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
body.curl-viewer .viewer-brand p { margin: 2px 0 0; font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.curl-viewer .viewer-locales { display: flex; flex-wrap: wrap; gap: 4px; flex: 0 1 auto; min-width: 0; }
body.curl-viewer .viewer-locales a,
body.curl-viewer .viewer-locales-current {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 8px;
}

/* Stage takes the remaining height; the book is sized from it in JS. */
body.curl-viewer #viewer-stage {
  position: relative;
  inset: auto;
  transform: none;
  flex: 1 1 auto;
  width: auto;
  height: auto;
  min-height: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
  overflow: hidden;
  background: transparent;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
body.curl-viewer #viewer-book-wrap { position: relative; flex: 0 0 auto; transform-origin: 50% 50%; }
body.curl-viewer #viewer-book-root { margin: 0 auto; }
/* Placeholder is tinted, never blank white, while a page is idle/loading/failed. */
body.curl-viewer #viewer-book-root .page { background: linear-gradient(160deg, var(--curl-page-a), var(--curl-page-b)); overflow: hidden; }
body.curl-viewer #viewer-book-root .page::after {
  content: "Trang " attr(data-label);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  text-align: center;
  font-size: .9rem;
  color: var(--curl-accent);
}
body.curl-viewer #viewer-book-root .page[data-state="loading"]::after { content: "Đang tải trang " attr(data-label) "…"; }
body.curl-viewer #viewer-book-root .page[data-state="error"]::after { content: "Không tải được trang " attr(data-label); color: var(--curl-danger); }
body.curl-viewer #viewer-book-root .page[data-state="ready"]::after { content: none; }
body.curl-viewer #viewer-book-root .page img {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

/* Loading sits inside the stage and never intercepts input. */
body.curl-viewer #viewer-loading {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: auto;
  height: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: transparent;
  pointer-events: none;
}
body.curl-viewer #viewer-loading p { margin: 0; padding: 6px 12px; border-radius: 999px; background: var(--curl-paper); color: var(--curl-text); }

/* Error banner in flow between stage and toolbar; never covers the controls. */
body.curl-viewer #viewer-error {
  position: relative;
  inset: auto;
  transform: none;
  z-index: 3;
  flex: 0 0 auto;
  width: auto;
  max-width: none;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px 12px;
  padding: 6px max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  border: 0;
  border-top: 1px solid #f1c9c6;
  border-radius: 0;
  box-shadow: none;
  background: #fdf1f0;
  color: var(--curl-danger);
}
body.curl-viewer #viewer-error-message { margin: 0; min-width: 0; text-align: center; overflow-wrap: anywhere; }

body.curl-viewer .curl-toolbar {
  position: relative;
  z-index: 3;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--curl-accent-soft);
  background: var(--curl-bg);
}
body.curl-viewer .curl-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px 12px; width: 100%; }
body.curl-viewer .curl-group { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 0 0 auto; }
body.curl-viewer .curl-group-nav { flex: 1 1 15rem; max-width: 30rem; }
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry) {
  position: static;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: auto;
  height: auto;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 0 14px;
  border: 1px solid var(--curl-accent);
  border-radius: 999px;
  background: var(--curl-paper);
  color: var(--curl-text);
  box-shadow: none;
  font: inherit;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry):hover { background: var(--curl-accent-soft); }
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry):active,
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry)[aria-pressed="true"] { background: var(--curl-accent); color: #fff; }
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry)[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry):focus-visible { outline: 3px solid var(--curl-accent); outline-offset: 2px; }
body.curl-viewer #viewer-retry { border-color: var(--curl-danger); color: var(--curl-danger); }
body.curl-viewer #viewer-page-range {
  position: static;
  flex: 1 1 6rem;
  width: auto;
  min-width: 72px;
  max-width: 18rem;
  min-height: 44px;
  margin: 0;
  accent-color: var(--curl-accent);
}
body.curl-viewer #viewer-page-range:focus-visible { outline: 3px solid var(--curl-accent); outline-offset: 2px; }
body.curl-viewer #viewer-page-label {
  flex: 0 0 auto;
  min-width: 4.5em;
  text-align: center;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--curl-text);
}
body.curl-viewer #viewer-status {
  max-width: 100%;
  min-height: 1.2em;
  font-size: .85rem;
  text-align: center;
  overflow-wrap: anywhere;
  color: var(--curl-accent);
}
body.curl-viewer .curl-noscript { margin: auto; padding: 16px; text-align: center; }

@media (max-width: 600px) {
  body.curl-viewer .curl-toolbar { padding-top: 6px; }
  body.curl-viewer .curl-controls { gap: 6px 8px; }
  body.curl-viewer :is(#viewer-prev, #viewer-next, #viewer-zoom-out, #viewer-fit, #viewer-zoom-in, #viewer-sound, #viewer-fullscreen, #viewer-retry) { padding: 0 10px; }
}
@media (max-height: 500px) {
  body.curl-viewer .viewer-topbar { padding-top: max(2px, env(safe-area-inset-top)); padding-bottom: 2px; }
  body.curl-viewer .viewer-brand img { max-height: 28px; }
  body.curl-viewer .viewer-brand h1 { font-size: .9rem; -webkit-line-clamp: 1; }
  body.curl-viewer .viewer-brand p { display: none; }
  body.curl-viewer #viewer-stage { padding-top: 6px; padding-bottom: 6px; }
  body.curl-viewer .curl-toolbar { flex-direction: row; flex-wrap: wrap; justify-content: center; padding-top: 4px; padding-bottom: max(4px, env(safe-area-inset-bottom)); }
  body.curl-viewer .curl-controls { width: auto; flex: 0 1 auto; }
  body.curl-viewer #viewer-status { flex: 1 1 8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}
@media (prefers-reduced-motion: reduce) {
  body.curl-viewer .viewer-spinner { animation: none; }
}
