/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  color-scheme: light;
  --ink: #18201c;
  --muted: #647067;
  --soft: #f5f7f4;
  --paper: #ffffff;
  --line: #dbe3db;
  --line-strong: #c7d2c8;
  --brand: #255c4a;
  --brand-strong: #174535;
  --accent: #b9892d;
  --danger: #9f2f2f;
  --reader: #2e302d;
  --reader-panel: #20231f;
  --reader-panel-soft: #2b302a;
  --reader-line: #40473f;
  --focus: rgba(37, 92, 74, 0.24);
  --shadow-sm: 0 1px 2px rgba(20, 28, 24, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 28, 24, 0.12);
}

html { height: 100%; }

body {
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--soft);
  line-height: 1.5;
}

a { color: inherit; }

.container {
  width: min(1120px, calc(100% - 2rem));
  margin: 0 auto;
}

.size-1 { width: 0.5rem; height: 0.5rem; }
.size-3 { width: 0.75rem; height: 0.75rem; }
.size-4 { width: 1rem; height: 1rem; }
.size-5 { width: 1.25rem; height: 1.25rem; }
.size-6 { width: 1.5rem; height: 1.5rem; }

.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;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2.4rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 0.92rem;
  font-weight: 650;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.btn:focus-visible,
.form-control:focus-visible,
.thumbnail-button:focus-visible,
.related-file:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}

.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn-primary:hover:not(:disabled) { background: var(--brand-strong); border-color: var(--brand-strong); }
.btn-outline { background: var(--paper); color: var(--brand); border-color: var(--line-strong); }
.btn-outline:hover:not(:disabled) { background: #edf4ef; border-color: var(--brand); }
.btn-ghost { background: transparent; color: inherit; border-color: transparent; }
.btn-ghost:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); }
.btn-sm { min-height: 2rem; padding: 0.35rem 0.7rem; font-size: 0.84rem; }
.btn-full { width: 100%; }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 650; margin-bottom: 0.35rem; color: var(--ink); }

.form-control {
  width: 100%;
  min-height: 2.4rem;
  padding: 0.52rem 0.72rem;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-control:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--focus); }
.form-control-sm { min-height: 2rem; padding: 0.3rem 0.5rem; font-size: 0.86rem; width: auto; }
.captcha-group { display: grid; gap: 0.5rem; }
.captcha-group label { margin-bottom: 0; }
.captcha-image {
  display: block;
  width: 100%;
  max-width: 260px;
  height: 82px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #eef3ee;
}
.captcha-input { text-transform: uppercase; letter-spacing: 0.18em; font-weight: 700; }
.captcha-refresh { width: fit-content; color: var(--brand); font-size: 0.84rem; }

/* Alerts */
.alert { padding: 0.75rem 0.9rem; border-radius: 6px; margin-bottom: 1rem; }
.alert-error { background: #fff0f0; color: #7b2323; border: 1px solid #e5a5a5; }
.info-banner {
  background: #eef6f1;
  color: var(--brand-strong);
  border: 1px solid #b8d4c6;
  padding: 0.75rem 0.9rem;
  border-radius: 6px;
  margin-bottom: 1rem;
}

/* Header */
.site-header {
  background: var(--paper);
  color: var(--ink);
  border-bottom: 1px solid var(--line);
}

.site-header .container {
  min-height: 4rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.site-title {
  color: var(--brand-strong);
  text-decoration: none;
  font-weight: 800;
  font-size: 1.05rem;
}

.site-nav { display: flex; align-items: center; gap: 0.75rem; }
.actor-label { font-size: 0.88rem; color: var(--muted); }
.inline-form { display: inline; }

/* Main Content */
.main-content { padding: 2rem 0 2.5rem; }

/* Footer */
.site-footer {
  border-top: 1px solid var(--line);
  padding: 1rem 0;
  text-align: center;
  color: var(--muted);
  background: #edf1ed;
}

.site-footer p { font-size: 0.84rem; }

/* Auth Page */
.auth-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1rem;
  background:
    linear-gradient(180deg, rgba(37, 92, 74, 0.08), rgba(37, 92, 74, 0)),
    var(--soft);
}

.auth-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2rem;
  width: min(100%, 410px);
  box-shadow: var(--shadow-md);
}

.auth-title {
  font-size: 1.45rem;
  margin-bottom: 1.35rem;
  color: var(--brand-strong);
  text-align: center;
}

.login-form { display: flex; flex-direction: column; }
.login-form .btn { margin-top: 0.5rem; }

/* Library and Dashboard */
h2 { font-size: 1.45rem; margin-bottom: 1rem; color: var(--ink); }

.dashboard-welcome {
  padding: 1.25rem 0 1.5rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 1.5rem;
}

.dashboard-welcome h2 { margin-bottom: 0.2rem; font-size: 1.5rem; }
.welcome-name { color: var(--brand); }
.welcome-sub { font-size: 0.9rem; color: var(--muted); }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.85rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.05rem 1.15rem;
  box-shadow: var(--shadow-sm);
}

.stat-value { font-size: 1.8rem; font-weight: 800; color: var(--brand); line-height: 1.1; }
.stat-label { font-size: 0.84rem; color: var(--muted); margin-top: 0.35rem; }

.dashboard-section { margin-bottom: 2rem; }

.section-title {
  font-size: 1rem;
  font-weight: 750;
  color: var(--ink);
  margin-bottom: 0.75rem;
  padding-bottom: 0.45rem;
  border-bottom: 1px solid var(--line);
}

.history-list { list-style: none; display: flex; flex-direction: column; gap: 0.6rem; }

.history-item {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow-sm);
}

.history-title {
  font-weight: 700;
  text-decoration: none;
  color: var(--brand-strong);
  font-size: 0.96rem;
  display: block;
  margin-bottom: 0.1rem;
}

.history-title:hover { text-decoration: underline; }
.history-author { font-size: 0.84rem; color: var(--muted); display: block; margin-bottom: 0.35rem; }
.history-meta { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.history-time { font-size: 0.8rem; color: var(--muted); }
.history-badge { font-size: 0.75rem; background: #edf4ef; color: var(--brand); border-radius: 999px; padding: 0.1rem 0.55rem; font-weight: 650; }

.empty-state-sm { color: var(--muted); font-size: 0.92rem; padding: 0.75rem 0; }
.empty-state-sm .link { color: var(--brand); text-decoration: none; font-weight: 650; }
.empty-state-sm .link:hover { text-decoration: underline; }

.login-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: var(--paper);
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}

.login-table th {
  background: #eef2ee;
  color: var(--muted);
  font-weight: 700;
  padding: 0.65rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
}

.login-table td { padding: 0.65rem 1rem; border-bottom: 1px solid #edf1ed; color: var(--ink); }
.login-table tr:last-child td { border-bottom: none; }
.login-row-current td { background: #f3f8f5; }
.login-ip { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.84rem; }
.badge-current { display: inline-block; background: var(--brand); color: #fff; font-size: 0.72rem; border-radius: 999px; padding: 0.05rem 0.45rem; margin-left: 0.4rem; font-family: sans-serif; vertical-align: middle; }

.search-form { margin-bottom: 1rem; }
.search-row { display: flex; gap: 0.5rem; }
.search-input { flex: 1; }
.result-count { font-size: 0.88rem; color: var(--muted); margin-bottom: 1rem; }
.document-list { list-style: none; display: flex; flex-direction: column; gap: 0.7rem; }
.document-item { background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 1rem; transition: border-color 0.15s ease, box-shadow 0.15s ease; }
.document-item:hover { border-color: var(--brand); box-shadow: var(--shadow-sm); }
.document-title { font-weight: 700; text-decoration: none; color: var(--brand-strong); font-size: 1rem; }
.document-title:hover { text-decoration: underline; }
.document-meta { font-size: 0.84rem; color: var(--muted); margin-top: 0.25rem; }
.empty-state { text-align: center; padding: 3rem; color: var(--muted); }
.pagination { display: flex; align-items: center; gap: 0.75rem; justify-content: center; margin-top: 2rem; }
.page-info { font-size: 0.88rem; color: var(--muted); }

/* Viewer */
.viewer-page {
  height: 100vh;
  overflow: hidden;
  background: var(--reader);
  color: #f7faf5;
}

.viewer-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--reader);
}

/* ── Header ──────────────────────────────────────────────────────────────── */
.viewer-header {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 65;
  gap: 0.5rem;
  min-height: 3.25rem;
  padding: 0.4rem 0.85rem;
  background: #f9fbf8;
  color: var(--ink);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(20, 28, 24, 0.06);
}

.viewer-header-main {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.viewer-icon-btn {
  color: var(--brand-strong);
  min-width: 2rem;
  padding: 0.3rem 0.5rem;
}
.viewer-icon-btn:hover:not(:disabled) { background: #edf4ef; }

.viewer-heading { min-width: 0; flex: 1; }
.viewer-title { font-size: 0.92rem; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
.viewer-subtitle { color: var(--muted); font-size: 0.76rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Center: page navigation */
.viewer-nav-controls {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  justify-content: center;
}

.page-jump-form {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 650;
}

.page-jump-input { width: 3.8rem; text-align: center; }
.page-total { color: var(--muted); white-space: nowrap; font-size: 0.84rem; }

/* Right: tools */
.viewer-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
}

/* Zoom popover */
.zoom-wrapper { position: relative; }

.zoom-btn { gap: 0.3rem; font-size: 0.8rem; font-weight: 750; }

.zoom-popover {
  position: absolute;
  z-index: 35;
  top: calc(100% + 0.3rem);
  right: 0;
  display: flex;
  flex-direction: column;
  min-width: 7rem;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
.zoom-popover[hidden] { display: none; }

.zoom-preset-btn {
  display: block;
  width: 100%;
  padding: 0.52rem 0.9rem;
  text-align: left;
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--ink);
  background: none;
  border: none;
  cursor: pointer;
}
.zoom-preset-btn:hover { background: var(--soft); }
.zoom-preset-btn.active { color: var(--brand); font-weight: 750; background: #edf4ef; }

/* Bookmark icon-only */
.bookmark-toggle { padding: 0.3rem 0.5rem; }
.bookmark-toggle.is-active { background: #fff7d8; border-color: #d9b947; color: #6f5400; }

/* More menu */
.more-menu-wrapper { position: relative; }

.more-menu-dropdown {
  position: absolute;
  z-index: 35;
  top: calc(100% + 0.3rem);
  right: 0;
  min-width: 10rem;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
.more-menu-dropdown[hidden] { display: none; }

.more-menu-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.65rem 0.9rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink);
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
}
.more-menu-item:hover { background: var(--soft); }

/* ── Body layout ──────────────────────────────────────────────────────────── */
.viewer-body {
  min-height: 0;
  flex: 1;
  display: flex;
  position: relative;
}

/* ── Nav Rail ─────────────────────────────────────────────────────────────── */
.viewer-nav-rail {
  flex-shrink: 0;
  width: 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.65rem 0;
  background: var(--reader-panel);
  border-right: 1px solid var(--reader-line);
  z-index: 10;
}

.nav-rail-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: #8a9c8a;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
  position: relative;
}
.nav-rail-btn:hover:not(:disabled) { background: rgba(255,255,255,0.1); color: #f0f5ef; }
.nav-rail-btn.is-active { background: rgba(255,255,255,0.12); color: #f0f5ef; }
.nav-rail-btn:disabled { opacity: 0.32; cursor: not-allowed; }
.nav-rail-btn:focus-visible { outline: none; box-shadow: 0 0 0 2px rgba(237,244,239,0.3); }

/* Tooltip via CSS */
.nav-rail-btn::after {
  content: attr(title);
  position: absolute;
  left: calc(100% + 0.6rem);
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  background: rgba(20, 28, 24, 0.92);
  color: #fff;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 0.3rem 0.55rem;
  border-radius: 5px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease 0.5s;
  z-index: 100;
}
.nav-rail-btn:hover:not(:disabled)::after { opacity: 1; }

/* ── Offcanvas Panel ──────────────────────────────────────────────────────── */
.offcanvas-panel {
  position: absolute;
  left: 48px;
  top: 0;
  bottom: 0;
  width: 300px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: var(--reader-panel);
  border-right: 1px solid var(--reader-line);
  color: #e8eee7;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.2);
  visibility: hidden;
  pointer-events: none;
}
.offcanvas-panel.is-open {
  transform: translateX(0);
  visibility: visible;
  pointer-events: auto;
}

.panel-content {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
}
.panel-content[hidden] { display: none; }

.panel-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 2.6rem;
  padding: 0 0.65rem 0 0.95rem;
  border-bottom: 1px solid var(--reader-line);
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #f7faf5;
  font-size: 0.8rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.panel-close-btn { color: #8a9c8a; padding: 0.25rem; min-height: auto; }
.panel-close-btn:hover { color: #f0f5ef; background: rgba(255,255,255,0.1); }

.panel-search-bar { padding: 0.6rem 0.75rem 0.25rem; }

.offcanvas-backdrop {
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(0, 0, 0, 0.28);
}
.offcanvas-backdrop[hidden] { display: none; }

/* ── Sidebar count badge ─────────────────────────────────────────────────── */
.sidebar-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.55rem;
  height: 1.55rem;
  border-radius: 999px;
  padding: 0 0.4rem;
  background: var(--reader-panel-soft);
  color: #d6dfd5;
  font-size: 0.75rem;
  font-weight: 750;
  text-transform: none;
}

/* ── Related file list (inside panel) ────────────────────────────────────── */
.related-file-list {
  min-height: 0;
  overflow: auto;
  padding: 0.45rem;
}

.related-file-search {
  width: 100%;
  background: #f9fbf8;
}

.related-file {
  display: grid;
  grid-template-columns: 1.65rem minmax(0, 1fr);
  gap: 0.55rem;
  align-items: start;
  padding: 0.62rem;
  border-radius: 6px;
  color: #dce5dc;
  text-decoration: none;
  border: 1px solid transparent;
}
.related-file:hover { background: rgba(255, 255, 255, 0.06); }
.related-file.active { background: #edf4ef; color: var(--brand-strong); border-color: #cbd9ce; }

.related-file-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.65rem;
  height: 1.65rem;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 0.78rem;
  font-weight: 800;
}
.related-file.active .related-file-index { background: var(--brand); color: #fff; }
.related-file-text { min-width: 0; display: block; }
.related-file-title { display: block; font-size: 0.86rem; font-weight: 700; line-height: 1.3; overflow-wrap: anywhere; }
.related-file-meta { display: block; margin-top: 0.15rem; font-size: 0.76rem; color: inherit; opacity: 0.72; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Thumbnail list ──────────────────────────────────────────────────────── */
.thumbnail-list {
  min-height: 0;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
  padding: 0.75rem;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.thumbnail-button {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: #d6dfd5;
  padding: 0.35rem;
  cursor: pointer;
}

.thumbnail-button:hover { background: rgba(255, 255, 255, 0.06); }
.thumbnail-button.active { background: #edf4ef; color: var(--brand-strong); border-color: #cbd9ce; }

.thumbnail-bookmark-marker {
  display: none;
  margin-left: auto;
  color: #b08900;
  font-size: 0.75rem;
  line-height: 1;
}

.thumbnail-button.is-bookmarked .thumbnail-bookmark-marker {
  display: inline-flex;
}

.document-search-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.45rem;
  padding: 0.75rem;
}

.document-search-input {
  width: 100%;
}

.document-search-status {
  min-height: 1.1rem;
  padding: 0 0.75rem 0.4rem;
  color: #a9b7aa;
  font-size: 0.78rem;
}

.document-search-results {
  flex: 1;
  min-height: 0;
  display: grid;
  align-content: start;
  gap: 0.45rem;
  padding: 0 0.75rem 0.85rem;
  overflow-y: auto;
}

.document-search-result {
  display: grid;
  gap: 0.25rem;
  width: 100%;
  padding: 0.55rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
  color: #f3f7f2;
  text-align: left;
  cursor: pointer;
}

.document-search-result:hover {
  background: rgba(255, 255, 255, 0.08);
}

.document-search-result.is-active {
  border-color: rgba(215, 193, 137, 0.8);
  background: rgba(215, 193, 137, 0.2);
}

.document-search-result-page {
  color: #d7c189;
  font-size: 0.76rem;
  font-weight: 750;
}

.document-search-result-snippet {
  display: -webkit-box;
  overflow: hidden;
  color: #e8eee7;
  font-size: 0.8rem;
  line-height: 1.35;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.document-search-empty {
  margin: 0;
  color: #a9b7aa;
  font-size: 0.82rem;
}

.page-notes-section {
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.note-form {
  display: grid;
  gap: 0.5rem;
  padding: 0.75rem;
}

.note-input {
  width: 100%;
  resize: vertical;
  min-height: 5rem;
  font-size: 0.85rem;
}

.page-notes-list {
  display: grid;
  gap: 0.55rem;
  padding: 0 0.75rem 0.9rem;
}

.page-notes-empty {
  margin: 0;
  color: #a9b7aa;
  font-size: 0.82rem;
}

.page-note-item {
  display: grid;
  gap: 0.35rem;
  padding: 0.6rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
}

.page-note-text {
  margin: 0;
  color: #f3f7f2;
  font-size: 0.84rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.page-note-meta {
  color: #a9b7aa;
  font-size: 0.74rem;
}

.page-note-delete {
  justify-self: start;
  min-height: 1.7rem;
  padding: 0.2rem 0.45rem;
  color: #f2d1d1;
}

.thumbnail-frame {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: 4px;
  overflow: hidden;
  background: #3b4039;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.thumbnail-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  -webkit-user-drag: none;
  user-select: none;
}

.thumbnail-loader {
  width: 1.15rem;
  height: 1.15rem;
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-top-color: #d6dfd5;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.thumbnail-loader[hidden] { display: none; }
.thumbnail-label { font-size: 0.76rem; font-weight: 750; text-align: center; }

.viewer-scroll {
  flex: 1;
  min-width: 0;
  overflow: auto;
  background: var(--reader);
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.document-pages {
  position: relative;
  min-width: 100%;
  width: max-content;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 1.75rem 2rem 3rem;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.pdf-page {
  width: fit-content;
  max-width: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.55rem;
}

.pdf-page-frame {
  position: relative;
  display: grid;
  place-items: center;
  min-width: min(760px, calc(100vw - 4rem));
  min-height: 68vh;
  background: #f7f7f3;
  border-radius: 4px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.28);
  overflow: hidden;
}

.pdf-page-frame.is-search-target {
  box-shadow: 0 0 0 3px rgba(214, 173, 77, 0.95), 0 12px 36px rgba(0, 0, 0, 0.28);
}

.pdf-page-img {
  display: block;
  max-width: none;
  height: auto;
  background: #fff;
  -webkit-user-drag: none;
  user-select: none;
}

.pdf-highlight-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.pdf-search-hit-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.pdf-highlight-rect {
  position: absolute;
  border-radius: 2px;
  mix-blend-mode: multiply;
  opacity: 0.45;
}

.pdf-search-hit-rect {
  position: absolute;
  border-radius: 2px;
  background: rgba(255, 214, 92, 0.62);
  box-shadow: 0 0 0 1px rgba(143, 96, 0, 0.25);
  animation: search-hit-pulse 1.35s ease-out;
}

@keyframes search-hit-pulse {
  0% { opacity: 0; }
  15% { opacity: 1; }
  100% { opacity: 0.55; }
}

.highlight-yellow { background: #f7d94c; }
.highlight-green { background: #7ddc93; }
.highlight-blue { background: #75b7ff; }
.highlight-pink { background: #ff91c2; }
.highlight-purple { background: #c8a3ff; }

.page-loader {
  position: absolute;
  z-index: 1;
  color: #5e665f;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid #e1e5df;
  border-radius: 6px;
  padding: 0.55rem 0.8rem;
  font-size: 0.86rem;
  font-weight: 650;
}

.page-loader[hidden] { display: none; }
.pdf-page.has-error .page-loader { color: var(--danger); border-color: #e0adad; background: #fff2f2; }
.pdf-page-number { color: #c7d0c5; font-size: 0.82rem; font-weight: 750; }

.loading-indicator {
  position: fixed;
  top: 4.5rem;
  left: 50%;
  z-index: 20;
  transform: translateX(-50%);
  background: rgba(20, 28, 24, 0.92);
  color: #fff;
  padding: 0.65rem 1rem;
  border-radius: 6px;
  font-size: 0.9rem;
  box-shadow: var(--shadow-md);
}

.loading-indicator[hidden] { display: none; }
.loading-indicator.error { background: rgba(127, 32, 32, 0.95); }

/* Progress bar (bottom of shell) */
.viewer-progress {
  flex-shrink: 0;
  height: 3px;
  background: #dfe6df;
}

.viewer-progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.15s ease;
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.58); }

.modal-content {
  position: relative;
  background: var(--paper);
  color: var(--ink);
  border-radius: 8px;
  padding: 1.5rem;
  max-width: 500px;
  width: min(100%, 500px);
  box-shadow: var(--shadow-md);
}

.modal-small { max-width: 310px; text-align: center; }
.modal h2 { margin-bottom: 0.65rem; font-size: 1.16rem; color: var(--ink); }
.modal p { margin-bottom: 0.75rem; }

.password-display {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 1rem 0;
  background: #f2f5f1;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0.75rem;
}

.password-code {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1rem;
  word-break: break-all;
}

.warning-text { font-size: 0.85rem; color: #7c5318; background: #fff7df; border: 1px solid #ead499; padding: 0.55rem 0.75rem; border-radius: 6px; }
.modal-actions { display: flex; gap: 0.75rem; margin-top: 1.25rem; justify-content: flex-end; flex-wrap: wrap; }
.processing-text { margin-bottom: 1rem; color: var(--ink); }
.spinner { width: 36px; height: 36px; border: 4px solid #e2e8e0; border-top-color: var(--brand); border-radius: 50%; animation: spin 0.7s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Error Page */
.error-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1rem;
  background: var(--soft);
}

.error-card {
  text-align: center;
  background: var(--paper);
  padding: 2rem;
  border-radius: 8px;
  width: min(100%, 430px);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
}

.error-code { font-size: 3.5rem; font-weight: 850; color: var(--danger); line-height: 1; }
.error-message { font-size: 1rem; margin: 0.75rem 0 0.5rem; color: var(--ink); }
.error-ref { font-size: 0.82rem; color: var(--muted); margin-bottom: 1.5rem; }

@media (max-width: 820px) {
  /* Header stacks: title row + nav controls row */
  .viewer-header {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    padding: 0.4rem 0.65rem;
    gap: 0.25rem;
  }
  .viewer-header-main { grid-row: 1; grid-column: 1; }
  .viewer-actions { grid-row: 1; grid-column: 2; }
  .viewer-nav-controls { grid-row: 2; grid-column: 1 / -1; justify-content: flex-start; padding-bottom: 0.2rem; }

  /* Nav rail → bottom tab bar */
  .viewer-nav-rail {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 52px;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: 0 0.5rem;
    border-right: none;
    border-top: 1px solid var(--reader-line);
    z-index: 60;
  }
  .nav-rail-btn { width: 44px; height: 44px; }
  /* Tooltips don't work on touch — disable */
  .nav-rail-btn::after { display: none; }

  /* Offcanvas panel slides up from bottom */
  .offcanvas-panel {
    left: 0;
    right: 0;
    top: auto;
    bottom: 52px;
    width: 100%;
    height: 65vh;
    transform: translateY(100%);
    transition: transform 0.22s ease;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
    border-right: none;
    border-top: 1px solid var(--reader-line);
  }
  .offcanvas-panel.is-open { transform: translateY(0); }

  /* Body: add bottom padding for tab bar */
  .viewer-body { padding-bottom: 52px; }

  .document-pages { padding: 1rem 1rem 2.5rem; gap: 1rem; }
  .pdf-page-frame { min-width: calc(100vw - 2rem); min-height: 62vh; }
}

@media (max-width: 620px) {
  .container { width: min(100% - 1rem, 1120px); }
  .site-header .container { min-height: auto; padding: 0.7rem 0; align-items: flex-start; flex-direction: column; }
  .site-nav { width: 100%; justify-content: space-between; }
  .main-content { padding: 1.25rem 0 2rem; }
  .auth-card { padding: 1.4rem; }
  .stat-grid { grid-template-columns: 1fr; }
  .search-row { flex-direction: column; }
  .login-table { display: block; overflow-x: auto; }
  .modal-actions { justify-content: stretch; }
  .modal-actions .btn { flex: 1; }
  .password-display { align-items: stretch; flex-direction: column; }
}
