/* ============================================================
   Measurement-First Medical Science — bilingual notebook
   Light + dark, LTR + RTL, print/PDF ready
   ============================================================ */

:root {
  --bg: #f6f7f9;
  --bg-elev: #ffffff;
  --bg-sunken: #eef1f5;
  --ink: #12181f;
  --ink-soft: #454f5b;
  --ink-faint: #77828f;
  --line: #dfe4ea;
  --line-soft: #eaeef3;
  --accent: #0d7ea8;
  --accent-2: #0f9d8f;
  --accent-ink: #ffffff;
  --hi: #1a8f5a;
  --md: #b8791b;
  --lo: #b23b3b;
  --warm: #c2410c;
  --cool: #1d6fa5;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 8px 24px -12px rgba(16,24,40,.18);
  --sidebar-w: 292px;
  --topbar-h: 62px;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Lora", Georgia, "Times New Roman", serif;
  --ar: "IBM Plex Sans Arabic", "SF Arabic", "Geeza Pro", "Noto Naskh Arabic", Tahoma, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0b1220;
  --bg-elev: #121b2c;
  --bg-sunken: #0e1626;
  --ink: #e8edf4;
  --ink-soft: #b3bfcf;
  --ink-faint: #8395ab;
  --line: #22304a;
  --line-soft: #1a2537;
  --accent: #4cc9f0;
  --accent-2: #48d6b8;
  --accent-ink: #06202e;
  --hi: #4ade80;
  --md: #fbbf24;
  --lo: #f87171;
  --warm: #fb923c;
  --cool: #60a5fa;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px -14px rgba(0,0,0,.7);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar-h) + 16px); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16.5px;
  line-height: 1.72;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

html[dir="rtl"] body { font-family: var(--ar); font-size: 17px; line-height: 1.95; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { transition: none !important; animation: none !important; }
}

.skip-link {
  position: absolute; inset-inline-start: -9999px; top: 0;
  background: var(--accent); color: var(--accent-ink);
  padding: 10px 16px; border-radius: 0 0 var(--radius-sm) 0; z-index: 100;
}
.skip-link:focus { inset-inline-start: 0; }

/* ---------- reading progress ---------- */
.progress {
  position: fixed; inset-inline: 0; top: 0; height: 3px; z-index: 60;
  background: transparent; pointer-events: none;
}
.progress span {
  display: block; height: 100%; width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .12s linear;
}

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 14px;
  padding: 0 18px;
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 11px; text-decoration: none; color: inherit; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border-radius: 11px; color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 26%, transparent);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.22; min-width: 0; }
.brand-text strong { font-size: 15px; font-weight: 650; letter-spacing: -.01em; white-space: nowrap; }
.brand-text small { font-size: 11.5px; color: var(--ink-faint); white-space: nowrap; }

.top-actions { display: flex; align-items: center; gap: 9px; margin-inline-start: auto; }

.search-wrap {
  position: relative; display: flex; align-items: center;
  background: var(--bg-sunken); border: 1px solid var(--line);
  border-radius: 999px; padding: 0 12px; height: 38px; width: 260px;
  transition: border-color .15s, box-shadow .15s;
}
.search-wrap:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.search-wrap svg { color: var(--ink-faint); flex: none; }
.search-wrap input {
  border: 0; background: transparent; outline: 0; color: var(--ink);
  font: inherit; font-size: 14px; padding-inline-start: 9px; width: 100%;
}
.search-wrap input::-webkit-search-cancel-button { -webkit-appearance: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 15px; border-radius: 999px; cursor: pointer;
  font: inherit; font-size: 13.5px; font-weight: 600; letter-spacing: .01em;
  border: 1px solid var(--line); background: var(--bg-elev); color: var(--ink);
  transition: transform .12s, background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.btn:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }

.lang-btn { padding: 0 12px; gap: 8px; font-variant-numeric: tabular-nums; }
.lang-current { color: var(--accent); font-weight: 700; }
.lang-other { color: var(--ink-faint); font-weight: 600; }
.lang-arrow { color: var(--ink-faint); font-size: 12px; }

.icon-btn {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border-radius: 10px; cursor: pointer; color: var(--ink-soft);
  border: 1px solid var(--line); background: var(--bg-elev);
  transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
#themeBtn .ico-moon { display: none; }
[data-theme="dark"] #themeBtn .ico-sun { display: none; }
[data-theme="dark"] #themeBtn .ico-moon { display: block; }

.burger, .burger::before, .burger::after {
  display: block; width: 17px; height: 2px; border-radius: 2px; background: currentColor;
  position: relative; transition: transform .2s;
}
.burger::before { content: ""; position: absolute; top: -5.5px; }
.burger::after { content: ""; position: absolute; top: 5.5px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); align-items: start; }

.sidebar {
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto; overscroll-behavior: contain;
  border-inline-end: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 20px 14px 30px;
  display: flex; flex-direction: column; gap: 16px;
}

.toc { display: flex; flex-direction: column; gap: 3px; }
.toc-title {
  font-size: 11px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-faint); padding: 0 10px 8px;
}
.toc-group { border-radius: var(--radius-sm); }
.toc a {
  display: flex; gap: 10px; align-items: baseline;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--ink-soft); text-decoration: none; font-size: 13.6px; line-height: 1.42;
  border-inline-start: 2px solid transparent;
  transition: background .13s, color .13s, border-color .13s;
}
.toc a:hover { background: var(--bg-sunken); color: var(--ink); }
.toc a.active {
  background: color-mix(in srgb, var(--accent) 11%, transparent);
  color: var(--ink); font-weight: 600;
  border-inline-start-color: var(--accent);
}
.toc .num {
  font-family: var(--mono); font-size: 11px; color: var(--accent);
  flex: none; padding-top: 1px; opacity: .95;
}
.toc .sub { display: flex; flex-direction: column; gap: 1px; margin: 1px 0 6px; }
.toc .sub a {
  font-size: 12.6px; padding: 5px 10px 5px 34px; color: var(--ink-faint);
  border-inline-start: 2px solid transparent;
}
html[dir="rtl"] .toc .sub a { padding: 5px 34px 5px 10px; }
.toc .sub a.active { color: var(--accent); background: transparent; }

.side-foot { margin-top: auto; border-top: 1px solid var(--line-soft); padding-top: 14px; }
.legend {
  display: flex; align-items: center; flex-wrap: wrap; gap: 5px 8px;
  font-size: 11.5px; color: var(--ink-faint); padding: 0 10px 8px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-inline-start: 4px; }
.d-hi { background: var(--hi); } .d-md { background: var(--md); } .d-lo { background: var(--lo); }
.side-foot .small { padding: 0 10px; color: var(--ink-faint); font-size: 11.5px; line-height: 1.55; margin: 4px 0 0; }

.scrim { display: none; }

/* ---------- content ---------- */
.content { min-width: 0; padding: 0 clamp(18px, 4vw, 56px) 70px; }
.content > * { max-width: 880px; margin-inline: auto; }

.hero { padding: 54px 0 34px; }
.eyebrow {
  font-size: 12px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase;
  color: var(--accent); margin: 0 0 14px;
}
.hero h1 {
  font-family: var(--serif); font-size: clamp(29px, 4.4vw, 45px);
  line-height: 1.16; letter-spacing: -.02em; margin: 0 0 18px; font-weight: 600;
}
html[dir="rtl"] .hero h1 { font-family: var(--ar); font-weight: 700; line-height: 1.34; }
.lede { font-size: 18px; color: var(--ink-soft); margin: 0 0 28px; max-width: 74ch; }
html[dir="rtl"] .lede { font-size: 17.5px; }

.hero-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(178px, 1fr));
  gap: 12px; margin-bottom: 28px;
}
.hero-card {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 15px 16px; box-shadow: var(--shadow);
}
.hero-card .n {
  font-family: var(--mono); font-size: 11px; color: var(--accent);
  letter-spacing: .08em; display: block; margin-bottom: 6px;
}
.hero-card .t { font-size: 14px; font-weight: 620; line-height: 1.4; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- chapter ---------- */
.chapters { display: flex; flex-direction: column; }
.chapter {
  padding: 46px 0 40px;
  border-top: 1px solid var(--line);
  scroll-margin-top: calc(var(--topbar-h) + 18px);
}
.chapter-head { margin-bottom: 26px; }
.chapter-num {
  display: inline-flex; align-items: center; gap: 9px;
  font-family: var(--mono); font-size: 11.5px; font-weight: 600;
  letter-spacing: .14em; color: var(--accent); text-transform: uppercase;
  margin-bottom: 10px;
}
.chapter-num::after {
  content: ""; width: 34px; height: 1px; background: currentColor; opacity: .4;
}
.chapter h2 {
  font-family: var(--serif); font-size: clamp(24px, 3.1vw, 33px);
  line-height: 1.22; letter-spacing: -.015em; margin: 0 0 10px; font-weight: 600;
}
html[dir="rtl"] .chapter h2 { font-family: var(--ar); font-weight: 700; line-height: 1.35; }
.chapter-sub { color: var(--ink-soft); font-size: 16.5px; margin: 0; max-width: 76ch; }

/* ---------- rich text ---------- */
.rich > h3 {
  font-size: 19.5px; font-weight: 650; letter-spacing: -.01em;
  margin: 38px 0 12px; padding-inline-start: 13px;
  border-inline-start: 3px solid var(--accent);
  scroll-margin-top: calc(var(--topbar-h) + 18px);
}
html[dir="rtl"] .rich > h3 { font-size: 19px; }
.rich > h4 {
  font-size: 16px; font-weight: 650; margin: 26px 0 8px; color: var(--ink);
}
.rich p { margin: 0 0 15px; color: var(--ink-soft); }
.rich > p { max-width: 78ch; }
.rich strong { color: var(--ink); font-weight: 640; }
.rich em { font-style: italic; }
.rich a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

.rich ul, .rich ol { margin: 0 0 16px; padding-inline-start: 22px; color: var(--ink-soft); }
.rich li { margin: 0 0 7px; }
.rich li::marker { color: var(--accent); }
.rich li > strong:first-child { color: var(--ink); }

.rich code, .rich .mono {
  font-family: var(--mono); font-size: .875em;
  background: var(--bg-sunken); border: 1px solid var(--line-soft);
  padding: 1.5px 6px; border-radius: 6px; color: var(--ink);
}

.eq {
  display: block; overflow-x: auto; text-align: center;
  font-family: var(--mono); font-size: 14.5px; color: var(--ink);
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 15px 18px; margin: 16px 0 20px;
  direction: ltr; unicode-bidi: isolate;
}
.eq small { display: block; font-family: var(--sans); font-size: 12px; color: var(--ink-faint); margin-top: 7px; }

.rich blockquote {
  margin: 18px 0; padding: 13px 18px; border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--accent) 7%, transparent);
  border-inline-start: 3px solid var(--accent);
  color: var(--ink-soft); font-size: 15.6px;
}
.rich blockquote p:last-child { margin-bottom: 0; }

.cascade {
  font-family: var(--mono); font-size: 13px; line-height: 1.85; white-space: pre;
  overflow-x: auto; direction: ltr; text-align: left;
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 17px 19px; margin: 16px 0 22px;
  color: var(--ink-soft);
}

/* tables */
.tablewrap { overflow-x: auto; margin: 18px 0 24px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg-elev); }
table { border-collapse: collapse; width: 100%; font-size: 13.8px; }
html[dir="rtl"] table { font-size: 14px; }
th, td { padding: 10px 13px; text-align: start; vertical-align: top; border-bottom: 1px solid var(--line-soft); }
th {
  background: var(--bg-sunken); font-weight: 650; color: var(--ink);
  font-size: 12.4px; letter-spacing: .02em; white-space: nowrap;
  position: sticky; top: 0;
}
td { color: var(--ink-soft); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in srgb, var(--accent) 4%, transparent); }
td strong { color: var(--ink); }

/* grade chips */
.g { display: inline-block; font-family: var(--mono); font-size: 11px; font-weight: 700;
     padding: 1px 7px; border-radius: 999px; line-height: 1.7; }
.g-h { color: var(--hi); background: color-mix(in srgb, var(--hi) 15%, transparent); border: 1px solid color-mix(in srgb, var(--hi) 35%, transparent); }
.g-m { color: var(--md); background: color-mix(in srgb, var(--md) 15%, transparent); border: 1px solid color-mix(in srgb, var(--md) 35%, transparent); }
.g-l { color: var(--lo); background: color-mix(in srgb, var(--lo) 15%, transparent); border: 1px solid color-mix(in srgb, var(--lo) 35%, transparent); }

.callout {
  border: 1px solid var(--line); border-inline-start: 3px solid var(--accent-2);
  background: var(--bg-elev); border-radius: var(--radius-sm);
  padding: 14px 17px; margin: 18px 0 22px;
}
.callout p:last-child { margin-bottom: 0; }
.callout.warn { border-inline-start-color: var(--md); }
.callout.stop { border-inline-start-color: var(--lo); }

.pill { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
        background: var(--bg-sunken); border: 1px solid var(--line); color: var(--ink-soft); margin: 0 4px 4px 0; }
html[dir="rtl"] .pill { margin: 0 0 4px 4px; }

.rail { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(230px,1fr)); margin: 18px 0 24px; }
.rail-card { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 13px 15px; background: var(--bg-elev); }
.rail-card.warm { border-inline-start: 3px solid var(--warm); }
.rail-card.cool { border-inline-start: 3px solid var(--cool); }
.rail-card h4 { margin: 0 0 6px; font-size: 14px; }
.rail-card p { margin: 0; font-size: 13.6px; color: var(--ink-soft); }

.rich hr { border: 0; border-top: 1px solid var(--line); margin: 34px 0; }

.docfoot { margin-top: 30px; padding-top: 22px; border-top: 1px solid var(--line); }
.docfoot .small { color: var(--ink-faint); font-size: 13px; }
.small { font-size: 13px; }

/* search hit */
mark.hit { background: color-mix(in srgb, var(--md) 34%, transparent); color: inherit; border-radius: 3px; }
.is-hidden { display: none !important; }
.no-results { padding: 30px 0; color: var(--ink-faint); }

/* ---------- overlay / diagram ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: color-mix(in srgb, #000 55%, transparent); padding: 22px;
}
.overlay[hidden] { display: none; }
.overlay-inner {
  position: relative; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-width: 940px; width: 100%; max-height: 88vh; overflow: auto; padding: 26px 26px 8px;
}
.overlay .close { position: absolute; inset-inline-end: 12px; top: 12px; }
.overlay-inner h3 { margin-top: 6px; }
.overlay-inner p { color: var(--ink-soft); }

.to-top {
  position: fixed; inset-inline-end: 20px; bottom: 20px; z-index: 55;
  width: 42px; height: 42px; border-radius: 50%; cursor: pointer;
  border: 1px solid var(--line); background: var(--bg-elev); color: var(--accent);
  font-size: 17px; box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.to-top.show { opacity: 1; pointer-events: auto; }
.to-top:hover { transform: translateY(-2px); }

/* ---------- responsive ---------- */
.only-mobile { display: none; }

@media (max-width: 1020px) {
  :root { --sidebar-w: 262px; }
  .search-wrap { width: 190px; }
  .brand-text small { display: none; }
}

@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .only-desktop { display: none; }
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset-block: var(--topbar-h) 0; inset-inline-start: 0;
    width: min(320px, 86vw); z-index: 70; transform: translateX(-102%);
    transition: transform .25s ease; border-inline-end: 1px solid var(--line);
  }
  html[dir="rtl"] .sidebar { transform: translateX(102%); }
  .sidebar.open { transform: translateX(0); }
  .scrim { display: block; position: fixed; inset: 0; z-index: 65; background: rgba(0,0,0,.45); opacity: 0; pointer-events: none; transition: opacity .25s; }
  .scrim.show { opacity: 1; pointer-events: auto; }
  .search-wrap { width: 44px; padding: 0 13px; }
  .search-wrap input { display: none; }
  .search-wrap:focus-within { width: 100%; position: absolute; inset-inline: 12px; z-index: 5; background: var(--bg-elev); }
  .search-wrap:focus-within input { display: block; }
  .brand-text strong { font-size: 13.5px; white-space: normal; }
  .topbar { padding: 0 12px; gap: 9px; }
  .content { padding: 0 18px 54px; }
  .hero { padding: 34px 0 24px; }
  .chapter { padding: 34px 0 30px; }
}

/* ---------- print ---------- */
@media print {
  :root { --bg: #fff; --bg-elev: #fff; --ink: #000; --ink-soft: #222; --line: #bbb; --line-soft: #ddd; }
  .topbar, .sidebar, .scrim, .to-top, .progress, .hero-actions, .search-wrap { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; }
  .content > * { max-width: none; }
  .chapter { break-inside: auto; page-break-inside: auto; border-top: 1px solid #ccc; }
  .chapter-head { break-after: avoid; }
  .rich > h3 { break-after: avoid; }
  .tablewrap, .eq, .cascade, .callout { break-inside: avoid; }
  table { font-size: 10.5px; }
  .overlay { display: none !important; }
  a { color: #000; text-decoration: none; }
}
