:root {
  color-scheme: light;
  --ink: #172b2f;
  --muted: #607176;
  --brand: #e66d62;
  --brand-strong: #bd5149;
  --mint: #2fae9b;
  --paper: #fff;
  --surface: #eef4f3;
  --line: rgba(23, 43, 47, 0.14);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body { width: 100%; height: 100%; margin: 0; }

button,
input,
textarea { font: inherit; }

button,
a { -webkit-tap-highlight-color: transparent; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.exam-reader-page {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  overflow: hidden;
  background: var(--surface);
  color: var(--ink);
}

.reader-header {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: auto auto minmax(160px, 1fr) auto;
  min-height: 64px;
  padding: 6px clamp(12px, 2vw, 26px);
  align-items: center;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.97);
}

.reader-header__back,
.reader-header__actions button {
  display: inline-flex;
  min-height: 42px;
  padding: 8px 13px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
  text-decoration: none;
}

.reader-header__brand { display: flex; align-items: center; }
.reader-header__brand img { display: block; width: 78px; height: auto; }

.reader-header__document { min-width: 0; display: grid; gap: 2px; }
.reader-header__document strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.93rem; }
.reader-header__document span { color: var(--muted); font-size: 0.72rem; }
.reader-header__document span.is-saving { color: #9a6712; }
.reader-header__document span.is-saved { color: #177569; }

.reader-header__actions { display: flex; gap: 8px; }
.reader-header__actions .reader-header__primary { border-color: var(--brand); background: var(--brand); color: #fff; }
.reader-header__actions .reader-header__primary:hover { background: var(--brand-strong); }

.reader-audio {
  position: relative;
  z-index: 18;
  border-bottom: 1px solid rgba(47, 174, 155, 0.24);
  background: #eaf8f5;
}

.reader-audio__toggle {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  width: 100%;
  min-height: 54px;
  padding: 7px clamp(12px, 2vw, 26px);
  align-items: center;
  gap: 11px;
  border: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.reader-audio__toggle:hover,
.reader-audio__toggle:focus-visible { background: rgba(47, 174, 155, 0.08); }
.reader-audio__icon { display: grid; width: 34px; height: 34px; place-items: center; border-radius: 50%; background: var(--mint); color: #fff; font-size: 0.8rem; }
.reader-audio__copy { min-width: 0; display: grid; gap: 1px; }
.reader-audio__copy small { color: #237f72; font-size: 0.68rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.035em; }
.reader-audio__copy strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.86rem; }
.reader-audio__action { padding: 7px 10px; border: 1px solid rgba(47, 174, 155, 0.35); border-radius: 9px; background: #fff; color: #177569; font-size: 0.75rem; font-weight: 850; }

.reader-audio__panel {
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(180px, 1fr);
  padding: 10px clamp(12px, 2vw, 26px) 14px;
  align-items: center;
  gap: 12px 18px;
  border-top: 1px solid rgba(47, 174, 155, 0.18);
}
.reader-audio__tabs { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; }
.reader-audio__tabs button { min-height: 36px; padding: 6px 10px; border: 1px solid rgba(47, 174, 155, 0.3); border-radius: 8px; background: #fff; color: var(--ink); cursor: pointer; font-size: 0.75rem; font-weight: 800; }
.reader-audio__tabs button.is-active { border-color: var(--mint); background: #dff5f0; color: #176d62; }
.reader-audio__player { width: 100%; overflow: hidden; border-radius: 11px; background: #10272b; box-shadow: 0 8px 22px rgba(23, 43, 47, 0.16); }
.reader-audio__player iframe { display: block; width: 100%; aspect-ratio: 16 / 9; border: 0; }
.reader-audio__player audio { display: block; width: 100%; min-height: 48px; background: #fff; }
.reader-audio__panel > p { max-width: 560px; margin: 0; color: var(--muted); font-size: 0.76rem; line-height: 1.45; }

.reader-toolbar {
  position: relative;
  z-index: 15;
  display: flex;
  min-height: 58px;
  padding: 7px clamp(10px, 2vw, 24px);
  align-items: center;
  gap: 12px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
  background: rgba(248, 251, 250, 0.97);
  scrollbar-width: thin;
}

.reader-toolbar__group { display: flex; flex: 0 0 auto; align-items: center; gap: 5px; }
.reader-toolbar__group + .reader-toolbar__group { padding-left: 12px; border-left: 1px solid var(--line); }

.reader-toolbar button {
  min-width: 38px;
  min-height: 40px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}

.reader-toolbar button:disabled { opacity: 0.4; cursor: default; }
.reader-toolbar button:hover:not(:disabled),
.reader-toolbar button:focus-visible { border-color: var(--mint); }

.reader-toolbar__tools button { display: inline-flex; align-items: center; gap: 6px; }
.reader-toolbar__tools button > span:first-child { font-size: 1.05rem; }
.reader-toolbar__tools button.is-active { border-color: var(--brand); background: #fff0ed; color: #a8443d; }

.reader-toolbar__pages label { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 0.8rem; }
.reader-toolbar__pages input { width: 52px; min-height: 38px; padding: 5px; border: 1px solid var(--line); border-radius: 8px; text-align: center; }
.reader-toolbar__edit output { min-width: 44px; color: var(--muted); font-size: 0.75rem; text-align: center; }
.reader-color { display: flex; }
.reader-color input { width: 38px; height: 38px; padding: 3px; border: 1px solid var(--line); border-radius: 9px; background: #fff; }
.reader-size { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.72rem; }
.reader-size input { width: 72px; }

.reader-main {
  position: relative;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  scrollbar-gutter: stable;
}

.reader-welcome,
.reader-error {
  min-height: 100%;
  padding: 42px 20px;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
}

.reader-welcome h1,
.reader-error h1 { margin: 18px 0 6px; font-size: clamp(1.5rem, 4vw, 2.3rem); }
.reader-welcome p,
.reader-error p { max-width: 520px; margin: 0; color: var(--muted); line-height: 1.5; }
.reader-error button,
.reader-error a { margin-top: 14px; min-height: 42px; padding: 9px 14px; border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); cursor: pointer; font-weight: 800; text-decoration: none; }

.reader-spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(47, 174, 155, 0.2);
  border-top-color: var(--mint);
  border-radius: 50%;
  animation: reader-spin 0.8s linear infinite;
}

@keyframes reader-spin { to { transform: rotate(360deg); } }

.reader-pages {
  display: grid;
  justify-items: center;
  gap: 22px;
  min-height: 100%;
  padding: 24px clamp(10px, 3vw, 34px) 60px;
}

.reader-page {
  position: relative;
  max-width: 100%;
  border-radius: 2px;
  background: var(--paper);
  box-shadow: 0 10px 34px rgba(23, 43, 47, 0.16);
}

.reader-page::before {
  content: attr(data-page-label);
  position: absolute;
  z-index: 3;
  top: 8px;
  right: 8px;
  padding: 4px 7px;
  border-radius: 999px;
  background: rgba(23, 43, 47, 0.7);
  color: #fff;
  font-size: 0.65rem;
  pointer-events: none;
}

.reader-page canvas { display: block; }
.reader-page__pdf { position: relative; z-index: 1; width: 100%; height: 100%; background: #fff; }
.reader-page__annotations { position: absolute; z-index: 2; inset: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: pan-y pinch-zoom; }
.exam-reader-page[data-tool="pen"] .reader-page__annotations,
.exam-reader-page[data-tool="highlight"] .reader-page__annotations,
.exam-reader-page[data-tool="circle"] .reader-page__annotations { touch-action: none; }
.exam-reader-page[data-tool="eraser"] .reader-page__annotations { cursor: cell; }

.reader-page__placeholder {
  position: absolute;
  z-index: 0;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 0.8rem;
}

.reader-mobile-tools { display: none; }

.reader-text-dialog {
  width: min(520px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: 16px;
  box-shadow: 0 26px 80px rgba(23, 43, 47, 0.3);
}
.reader-text-dialog::backdrop { background: rgba(23, 43, 47, 0.58); backdrop-filter: blur(2px); }
.reader-text-dialog form { display: grid; gap: 14px; padding: 20px; }
.reader-text-dialog h2 { margin: 0; font-size: 1.3rem; }
.reader-text-dialog textarea { width: 100%; min-height: 96px; padding: 12px; resize: vertical; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); }
.reader-text-dialog form > div { display: flex; justify-content: flex-end; gap: 8px; }
.reader-text-dialog button { min-height: 42px; padding: 9px 14px; border: 1px solid var(--line); border-radius: 9px; background: #fff; cursor: pointer; font-weight: 800; }
.reader-text-dialog .reader-dialog-primary { border-color: var(--brand); background: var(--brand); color: #fff; }

.reader-toast {
  position: fixed;
  z-index: 80;
  left: 50%;
  bottom: 22px;
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-size: 0.82rem;
  font-weight: 750;
  text-align: center;
  transform: translateX(-50%);
  box-shadow: 0 12px 30px rgba(23, 43, 47, 0.25);
}

[hidden] { display: none !important; }

@media (max-width: 900px) {
  .reader-header { grid-template-columns: auto minmax(0, 1fr) auto; }
  .reader-header__brand { display: none; }
  .reader-header__actions button:first-child { display: none; }
  .reader-toolbar__tools button span:last-child { display: none; }
}

@media (max-width: 650px) {
  .exam-reader-page { grid-template-rows: auto auto auto minmax(0, 1fr) auto; }
  .reader-header { min-height: 56px; padding: 6px 9px; gap: 8px; }
  .reader-header__back { min-width: 44px; padding: 7px 10px; }
  .reader-header__back span:last-child { display: none; }
  .reader-header__document strong { font-size: 0.8rem; }
  .reader-header__document span { font-size: 0.65rem; }
  .reader-header__actions .reader-header__primary { min-height: 42px; padding: 7px 10px; font-size: 0.72rem; }

  .reader-audio__toggle { min-height: 48px; padding: 5px 9px; gap: 8px; }
  .reader-audio__icon { width: 31px; height: 31px; }
  .reader-audio__copy small { font-size: 0.58rem; }
  .reader-audio__copy strong { font-size: 0.72rem; }
  .reader-audio__action { padding: 6px 8px; font-size: 0.65rem; }
  .reader-audio__panel { grid-template-columns: minmax(0, 1fr); max-height: min(310px, 46vh); padding: 8px 9px 10px; overflow: auto; }
  .reader-audio__tabs { grid-column: 1; }
  .reader-audio__player { max-width: 420px; margin-inline: auto; }
  .reader-audio__panel > p { font-size: 0.67rem; }

  .reader-toolbar { min-height: 50px; padding: 5px 8px; gap: 8px; }
  .reader-toolbar__tools,
  .reader-toolbar__edit .reader-color,
  .reader-toolbar__edit .reader-size,
  .reader-toolbar__edit [data-action="clear"] { display: none; }
  .reader-toolbar__group + .reader-toolbar__group { padding-left: 8px; }
  .reader-toolbar button { min-height: 40px; }
  .reader-toolbar__edit { margin-left: auto; }
  .reader-toolbar__edit output { min-width: 38px; }

  .reader-pages { gap: 12px; padding: 12px 5px 28px; }
  .reader-page { max-width: calc(100vw - 10px); box-shadow: 0 4px 18px rgba(23, 43, 47, 0.14); }
  .reader-page::before { display: none; }

  .reader-mobile-tools {
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    padding: 6px max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    gap: 4px;
    border-top: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.98);
  }
  .reader-mobile-tools button { min-height: 48px; padding: 4px; display: grid; place-items: center; gap: 0; border: 1px solid transparent; border-radius: 9px; background: transparent; color: var(--muted); }
  .reader-mobile-tools button span { font-size: 1.05rem; font-weight: 850; }
  .reader-mobile-tools button small { font-size: 0.58rem; font-weight: 750; }
  .reader-mobile-tools button.is-active { border-color: rgba(230, 109, 98, 0.35); background: #fff0ed; color: #a8443d; }
  .reader-toast { bottom: calc(68px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  .reader-main { scroll-behavior: auto; }
  .reader-spinner { animation-duration: 1.8s; }
}
