:root {
  --ink: #171411;
  --muted: #6b635b;
  --line: #e3e3e3;
  --panel: #f5f5f5;
  --paper: #ffffff;
  --accent: #893b43;
  --accent-soft: rgba(137, 59, 67, 0.1);
  --visited: #3d7c3f;
  --sidebar-bg: #faf8f6;
}

* {
  box-sizing: border-box;
}

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

body {
  color: var(--ink);
  background: #ffffff;
  font-family: Cambria, Georgia, "Palatino Linotype", Palatino, "Times New Roman", serif;
  font-size: 16px;
}

/* UOTI brand block */
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  text-decoration: none;
  padding: 0.6rem 0.9rem;
  margin: -0.9rem -0.9rem 0;
  border-bottom: 1px solid var(--line);
  background: var(--accent);
  transition: background 0.15s;
}

.sidebar-brand:hover {
  background: #7a3039;
}

.brand-logo {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}

.brand-name {
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: #ffffff;
  line-height: 1.1;
}

.brand-tagline {
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 0.7rem;
  color: rgba(255, 255, 255, 0.72);
  letter-spacing: 0.02em;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}


.reader-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  align-items: start;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--line);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 0.9375rem;
}

.sidebar-head {
  padding: 0.9rem 0.9rem 0.75rem;
  border-bottom: 1px solid var(--line);
  background: var(--sidebar-bg);
  display: grid;
  gap: 0.5rem;
}

.toc-header {
  padding: 0.35rem 0.9rem 0.25rem;
  font-size: 0.63rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
  opacity: 0.7;
}

.back-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.8rem;
  opacity: 0.8;
}

.back-link:hover {
  opacity: 1;
  text-decoration: underline;
}

.book-title {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.2;
}

.book-subtitle {
  color: var(--muted);
  font-size: 0.82rem;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 0.55rem;
  color: var(--muted);
  font-size: 1rem;
  pointer-events: none;
  line-height: 1;
}

.search-wrap input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.5rem 0.6rem 0.5rem 1.9rem;
  font: inherit;
  font-size: 0.875rem;
  background: #f5f5f5;
}

.search-wrap input:focus {
  outline: none;
  border-color: rgba(137, 59, 67, 0.4);
  box-shadow: 0 0 0 3px rgba(137, 59, 67, 0.08);
}

.format-badge {
  display: none;
}

.search-wrap:has(input:disabled) {
  display: none;
}

.toc {
  overflow-y: auto;
  min-height: 0;
  padding: 0.4rem 0.4rem 1rem;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-list-root {
  display: grid;
  gap: 0.15rem;
}

.toc-list-nested {
  margin-top: 0.1rem;
  margin-left: 0.6rem;
  padding-left: 0.5rem;
  border-left: 1px solid rgba(137, 59, 67, 0.15);
}

.toc-item {
  display: block;
}

.toc-link {
  display: block;
  text-decoration: none;
  color: var(--ink);
  border-radius: 8px;
  padding: 0.45rem 0.55rem;
  line-height: 1.25;
  border: 1px solid transparent;
}

.toc-link-button {
  width: 100%;
  text-align: left;
  font: inherit;
  background: transparent;
  cursor: pointer;
}

.toc-link-label {
  cursor: default;
}

.toc-link:hover {
  background: rgba(137, 59, 67, 0.06);
  border-color: rgba(137, 59, 67, 0.12);
}

.toc-item.active > .toc-link {
  background: var(--accent-soft);
  border-color: rgba(137, 59, 67, 0.28);
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  padding-left: calc(0.55rem - 2px);
  color: #5a1a22;
  font-weight: 600;
}

.toc-item.search-hit > .toc-link {
  background: rgba(137, 59, 67, 0.06);
}

.toc-page-label {
  float: right;
  color: var(--muted);
  font-size: 0.8em;
  margin-left: 0.5rem;
}

.toc-item.visited:not(.active) > .toc-link::after {
  content: "●";
  float: right;
  color: var(--visited);
  font-size: 0.42rem;
  line-height: 2.6;
  margin-left: 0.3rem;
  opacity: 0.7;
}

.toc-item.toc-depth-0:not(:first-child) {
  margin-top: 0.5rem;
  padding-top: 0.35rem;
  border-top: 1px solid var(--line);
}

.toc-depth-0 > .toc-link {
  font-weight: 700;
  letter-spacing: -0.01em;
}

.toc-depth-1 > .toc-link {
  font-size: 0.94rem;
}

.toc-depth-2 > .toc-link,
.toc-depth-3 > .toc-link {
  font-size: 0.89rem;
  color: #3a3835;
}

.toc-item.hidden {
  display: none;
}

.mainpane {
  min-width: 0;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
}

.topbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(4px);
  box-shadow: 0 1px 0 var(--line), 0 2px 12px rgba(17,17,17,0.06);
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 0.5rem 1rem 0.6rem;
  display: grid;
  gap: 0.1rem;
  font-family: 'DM Sans', system-ui, sans-serif;
}

.topbar-breadcrumb {
  min-height: 1.3rem;
  display: flex;
  align-items: center;
}

.breadcrumb-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.8rem;
  font-family: 'DM Sans', system-ui, sans-serif;
  display: none;
  align-items: center;
  gap: 0.2rem;
  opacity: 0.85;
}

.breadcrumb-link.visible {
  display: inline-flex;
}

.breadcrumb-link:hover {
  opacity: 1;
  text-decoration: underline;
}

.breadcrumb-arrow {
  font-size: 0.9em;
}

.topbar-main {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-shrink: 0;
  border-left: 1px solid var(--line);
  padding-left: 0.75rem;
  margin-left: 0.3rem;
}

.font-size-btn {
  border: 1px solid var(--line);
  background: white;
  border-radius: 8px;
  padding: 0.25rem 0.6rem;
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
  line-height: 1.4;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.font-size-btn:hover {
  background: var(--panel);
  border-color: rgba(137, 59, 67, 0.25);
  color: var(--ink);
}

.font-size-btn[data-size="lg"] { font-size: 0.92rem; }
.font-size-btn[data-size="xl"] { font-size: 1rem; }

.chapter-title {
  font-size: 1.1rem;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.status {
  color: var(--muted);
  font-size: 0.85rem;
}

.nav-btn {
  border: 1px solid var(--line);
  background: white;
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font: inherit;
  cursor: pointer;
}

.nav-btn[disabled] {
  opacity: 0.45;
  cursor: default;
}

.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
  padding: 2rem clamp(1.5rem, 3vw, 2.5rem);
  gap: 1rem;
  font-family: 'DM Sans', system-ui, sans-serif;
}

.chapter-nav-btn {
  border: 1px solid var(--line);
  background: white;
  border-radius: 8px;
  padding: 0.6rem 1.2rem;
  font: inherit;
  cursor: pointer;
  color: var(--accent);
  font-size: 0.95rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 120px;
  max-width: 42%;
}

.chapter-nav-prev {
  align-items: flex-start;
  text-align: left;
}

.chapter-nav-next {
  align-items: flex-end;
  text-align: right;
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.nav-btn-dir {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  opacity: 0.75;
  line-height: 1;
}

.chapter-nav-next .nav-btn-dir {
  opacity: 0.85;
}

.nav-btn-title {
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.chapter-nav-btn:hover:not([disabled]) {
  background: var(--accent-soft);
  border-color: rgba(137, 59, 67, 0.3);
  color: var(--accent);
}

.chapter-nav-next:hover:not([disabled]) {
  background: #7a3039;
  border-color: #7a3039;
  color: #ffffff;
}

.chapter-nav-btn[disabled] {
  opacity: 0.35;
  cursor: default;
}

.content {
  min-width: 0;
  padding: clamp(1rem, 2vw, 1.5rem);
}

.content-inner {
  max-width: 860px;
  margin: 0 auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: clamp(1.75rem, 5vw, 4rem) clamp(1.5rem, 4vw, 3.5rem);
  box-shadow: 0 18px 44px rgba(17, 17, 17, 0.05);
  font-size: var(--content-font-size, 1.05rem);
}

.content h1,
.content h2,
.content h3,
.content h4 {
  font-family: 'Lora', Cambria, Georgia, serif;
  line-height: 1.15;
}

.content p,
.content li {
  line-height: 1.72;
}

.content p {
  margin-bottom: 0.85em;
}

.content code {
  background: rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding: 0.05rem 0.25rem;
}

.content img {
  max-width: 100%;
  height: auto;
}

.content .os-figure {
  margin: 2rem 0 1rem;
}

.content figure {
  margin: 0 auto;
  text-align: center;
}

.content figure.scaled-down {
  max-width: 75%;
}

.content .os-caption-container {
  margin-top: 0.65rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--line);
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--muted);
}

.content .os-title-label,
.content .os-number {
  font-weight: 700;
  color: var(--ink);
}

.content .os-caption {
  color: var(--muted);
}

.content cnx-pi {
  display: none !important;
}

.content .page-title {
  margin-top: 0;
  font-size: clamp(2rem, 4.2vw, 3rem);
}

.content h4,
.content h5,
.content h6 {
  line-height: 1.2;
}

.content ul.os-feature-list {
  list-style: none;
  margin-left: 0;
  padding-left: 0;
  display: grid;
  gap: 0.8rem;
}

.content li.os-feature-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.65rem;
  align-items: start;
  margin: 0;
  padding: 0.15rem 0;
}

.content li.os-feature-item > span[data-type="media"] {
  display: block;
  margin: 0.05rem 0 0;
}

.content li.os-feature-item > span[data-type="media"] img {
  width: auto;
  max-width: 78px;
  height: auto;
}

.content li.os-feature-item > .os-feature-body {
  min-width: 0;
}

.content li.os-feature-item > .os-feature-body > :first-child {
  margin-top: 0;
}

.content li.os-feature-item > .os-feature-body > :last-child {
  margin-bottom: 0;
}

/* ── Inline links ────────────────────────────────────────── */
.content a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-color: rgba(137, 59, 67, 0.4);
}

.content a:hover {
  text-decoration-color: var(--accent);
}

.content a:visited {
  color: var(--visited);
  text-decoration-color: rgba(61, 124, 63, 0.4);
}

/* ── Tables ──────────────────────────────────────────────── */
.content .os-table {
  overflow-x: auto;
  margin: 1.5rem 0;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.content table {
  width: 100%;
  border-collapse: collapse;
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 0.9rem;
  line-height: 1.5;
}

.content thead {
  background: var(--accent);
  color: #fff;
}

.content thead th {
  padding: 0.65rem 0.85rem;
  text-align: left;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.content tbody tr:nth-child(even) {
  background: rgba(137, 59, 67, 0.04);
}

.content tbody tr:hover {
  background: rgba(137, 59, 67, 0.08);
}

.content tbody td,
.content tbody th {
  padding: 0.55rem 0.85rem;
  border-top: 1px solid var(--line);
  vertical-align: top;
}

.content tbody th {
  font-weight: 600;
  background: rgba(137, 59, 67, 0.06);
  white-space: nowrap;
}

/* dark mode */
[data-theme="dark"] .content thead {
  background: #5a2028;
}

[data-theme="dark"] .content tbody tr:nth-child(even) {
  background: rgba(196, 122, 129, 0.06);
}

[data-theme="dark"] .content tbody tr:hover {
  background: rgba(196, 122, 129, 0.12);
}

[data-theme="dark"] .content tbody th {
  background: rgba(196, 122, 129, 0.08);
}

.content [data-type="example"],
.content [data-type="note"],
.content .example,
.content .note {
  border-left: 4px solid rgba(137, 59, 67, 0.6);
  padding: 0.35rem 0.8rem;
  margin: 1rem 0;
  background: rgba(137, 59, 67, 0.05);
  border-radius: 0 8px 8px 0;
}

.empty,
.error-box,
.pdf-panel {
  max-width: 860px;
  margin: 0 auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1rem;
}

.error-box {
  border-color: #d2a7a7;
  background: #fff8f8;
}

.pdf-panel .pdf-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin: 0.5rem 0 1rem;
}

.pdf-panel a {
  text-decoration: none;
  border: 1px solid rgba(137, 59, 67, 0.25);
  color: var(--accent);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
}

.pdf-frame {
  width: 100%;
  min-height: 65vh;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: white;
}

.license {
  padding: 0.75rem 1rem 1rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
  font-size: 0.85rem;
  background: var(--panel);
  font-family: 'DM Sans', system-ui, sans-serif;
}

.license a {
  color: var(--accent);
}

.pdf-reader {
  max-width: 1100px;
  margin: 0 auto;
  display: grid;
  gap: 0.85rem;
}

.pdf-toolbar {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.75rem;
  display: grid;
  gap: 0.55rem;
}

.pdf-toolbar-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.pdf-inline-label {
  color: var(--muted);
  font-size: 0.85rem;
}

.pdf-page-input,
.pdf-search-input {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.35rem 0.5rem;
  font: inherit;
  background: #fff;
}

.pdf-page-input {
  width: 5.5rem;
}

.pdf-search-input {
  min-width: min(22rem, 100%);
  flex: 1 1 14rem;
}

.pdf-page-total,
.pdf-zoom-label,
.pdf-search-status {
  color: var(--muted);
  font-size: 0.85rem;
}

.pdf-search-status {
  min-height: 1.1rem;
}

.pdf-mini-btn {
  padding: 0.32rem 0.55rem;
  line-height: 1.2;
}

.pdf-viewer-stage {
  background: #f8f8f8;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.75rem;
  display: grid;
  gap: 0.6rem;
}

.pdf-banner {
  border-radius: 10px;
  padding: 0.45rem 0.65rem;
  background: rgba(137, 59, 67, 0.07);
  color: #5a1a22;
  font-size: 0.9rem;
}

.pdf-banner[hidden] {
  display: none;
}

.pdf-banner-error {
  background: #fff3f3;
  color: #8b1f1f;
  border: 1px solid rgba(139, 31, 31, 0.15);
}

.pdf-viewer-scroll {
  overflow: auto;
  border-radius: 10px;
  border: 1px solid rgba(137, 59, 67, 0.12);
  background: #f0f0f0;
  min-height: 55vh;
  max-height: 75vh;
  padding: 0.75rem;
}

.pdf-canvas-wrap {
  margin: 0 auto;
  width: fit-content;
  max-width: 100%;
  background: white;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(17, 17, 17, 0.08);
}

.pdf-canvas {
  display: block;
  background: white;
  border-radius: 8px;
}

.pdf-aux-links {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.pdf-aux-links a {
  text-decoration: none;
  border: 1px solid rgba(137, 59, 67, 0.2);
  color: var(--accent);
  border-radius: 8px;
  padding: 0.4rem 0.55rem;
  background: rgba(255, 255, 255, 0.7);
}

/* ── Dark mode ─────────────────────────────────────────── */
[data-theme="dark"] {
  --ink: #e8e4df;
  --muted: #9a9189;
  --line: #2a2724;
  --panel: #1e1c19;
  --paper: #1a1815;
  --accent: #c47a81;
  --accent-soft: rgba(196, 122, 129, 0.18);
  --visited: #6db570;
  --sidebar-bg: #141210;
}

[data-theme="dark"] body {
  background: #0f0e0c;
}

[data-theme="dark"] .topbar {
  background: rgba(20, 18, 16, 0.95);
  box-shadow: 0 1px 0 var(--line), 0 2px 12px rgba(0,0,0,0.25);
}

[data-theme="dark"] .font-size-btn,
[data-theme="dark"] .icon-btn {
  background: var(--panel);
}

[data-theme="dark"] .chapter-nav-btn {
  background: var(--paper);
}

[data-theme="dark"] .chapter-nav-next {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

[data-theme="dark"] .chapter-nav-next:hover:not([disabled]) {
  background: #a85a62;
  border-color: #a85a62;
}

[data-theme="dark"] .search-wrap input {
  background: var(--panel);
  color: var(--ink);
}

[data-theme="dark"] .license {
  background: var(--panel);
}

[data-theme="dark"] .error-box {
  background: #1f1418;
  border-color: #5a3030;
}

[data-theme="dark"] .pdf-frame,
[data-theme="dark"] .pdf-canvas-wrap,
[data-theme="dark"] .pdf-canvas {
  background: var(--panel);
}

[data-theme="dark"] .pdf-viewer-stage {
  background: #181614;
}

[data-theme="dark"] .pdf-viewer-scroll {
  background: #181614;
}

[data-theme="dark"] .pdf-page-input,
[data-theme="dark"] .pdf-search-input {
  background: var(--panel);
  color: var(--ink);
}

[data-theme="dark"] .content-inner {
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] .content code {
  background: rgba(255, 255, 255, 0.07);
}

[data-theme="dark"] .toc-item.active > .toc-link {
  color: #e8a0a7;
}

/* ── Sidebar overlay (mobile) ───────────────────────────── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 49;
  background: rgba(0, 0, 0, 0.45);
}

.sidebar-overlay.active {
  display: block;
}

.sidebar-toggle-btn {
  display: none;
}

/* ── Icon buttons (theme toggle, print) ─────────────────── */
.icon-btn {
  border: 1px solid var(--line);
  background: white;
  border-radius: 8px;
  padding: 0.25rem 0.55rem;
  font-size: 1rem;
  cursor: pointer;
  color: var(--muted);
  line-height: 1.4;
  font-family: 'DM Sans', system-ui, sans-serif;
}

.icon-btn:hover {
  background: var(--panel);
  border-color: rgba(137, 59, 67, 0.25);
  color: var(--ink);
}

/* ── Print styles ───────────────────────────────────────── */
@media print {
  .sidebar,
  .topbar,
  .chapter-nav,
  .license {
    display: none !important;
  }

  .reader-shell {
    display: block;
  }

  .mainpane {
    display: block;
    min-height: unset;
  }

  .content {
    padding: 0;
  }

  .content-inner {
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    max-width: 100%;
  }
}

@media (max-width: 900px) {
  .reader-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 300px;
    z-index: 50;
    border-right: 1px solid var(--line);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    overflow-y: auto;
  }

  .sidebar--open {
    transform: translateX(0);
  }

  .sidebar-toggle-btn {
    display: inline-flex;
    align-items: center;
    font-size: 1.2rem;
    padding: 0.2rem 0.45rem;
  }

  .toc {
    max-height: none;
  }

  .mainpane {
    min-height: 0;
  }

  .topbar-main {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
  }

  .pdf-toolbar {
    padding: 0.65rem;
  }

  .pdf-toolbar-group {
    gap: 0.35rem;
  }

  .pdf-search-form {
    align-items: flex-start;
  }

  .pdf-search-input {
    flex-basis: 100%;
    min-width: 0;
    width: 100%;
  }

  .pdf-viewer-scroll {
    min-height: 45vh;
    max-height: 65vh;
    padding: 0.5rem;
  }
}
