/* =========================================================
   Hamza Medical Library
   Palette: porcelain + deep viridian, brass hairlines.
   Type: Bodoni Moda (display) / Hanken Grotesk (text),
         Arabic fallbacks: Noto Naskh Arabic / IBM Plex Sans Arabic
   ========================================================= */

:root {
  --bg: #ECEFEE;
  --bg-deep: #E3E8E6;
  --surface: #F7F9F8;
  --ink: #12201E;
  --ink-soft: #2A3A37;
  --muted: #5D6E6A;
  --line: rgba(18, 32, 30, .13);
  --line-strong: rgba(18, 32, 30, .24);
  --accent: #0E5A50;
  --accent-ink: #F3F7F6;
  --brass: #A47C33;
  --focus: #0E5A50;
  --shadow-vol: 0 1px 1px rgba(10, 30, 26, .08), 0 10px 22px -10px rgba(10, 30, 26, .35);
  --shadow-vol-hover: 0 2px 4px rgba(10, 30, 26, .1), 0 26px 40px -16px rgba(10, 30, 26, .45);
  --cover-blank: #0E5A50;
  --cover-blank-ink: #E9F0EE;

  --f-display: 'Bodoni Moda', 'Noto Naskh Arabic', 'Didot', Georgia, serif;
  --f-text: 'Hanken Grotesk', 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', sans-serif;

  --gutter: clamp(16px, 4.5vw, 64px);
  --maxw: 1360px;
  --ease: cubic-bezier(.2, .7, .2, 1);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #081311;
  --bg-deep: #050D0C;
  --surface: #0F1E1C;
  --ink: #E6EEEB;
  --ink-soft: #C5D3CF;
  --muted: #8DA19C;
  --line: rgba(230, 238, 235, .11);
  --line-strong: rgba(230, 238, 235, .22);
  --accent: #6CC2AE;
  --accent-ink: #06201B;
  --brass: #CDAA5C;
  --focus: #8FD6C5;
  --shadow-vol: 0 1px 1px rgba(0, 0, 0, .4), 0 12px 24px -10px rgba(0, 0, 0, .7);
  --shadow-vol-hover: 0 2px 4px rgba(0, 0, 0, .4), 0 28px 44px -16px rgba(0, 0, 0, .85);
  --cover-blank: #123A34;
  --cover-blank-ink: #D9E7E3;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #081311;
    --bg-deep: #050D0C;
    --surface: #0F1E1C;
    --ink: #E6EEEB;
    --ink-soft: #C5D3CF;
    --muted: #8DA19C;
    --line: rgba(230, 238, 235, .11);
    --line-strong: rgba(230, 238, 235, .22);
    --accent: #6CC2AE;
    --accent-ink: #06201B;
    --brass: #CDAA5C;
    --focus: #8FD6C5;
    --shadow-vol: 0 1px 1px rgba(0, 0, 0, .4), 0 12px 24px -10px rgba(0, 0, 0, .7);
    --shadow-vol-hover: 0 2px 4px rgba(0, 0, 0, .4), 0 28px 44px -16px rgba(0, 0, 0, .85);
    --cover-blank: #123A34;
    --cover-blank-ink: #D9E7E3;
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; overflow-x: clip; }
body { overflow-x: clip; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--f-text);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
main { flex: 1; }
img, svg, canvas { display: block; }
a { color: inherit; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; border-radius: 4px; }
::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }

.theme-anim, .theme-anim *, .theme-anim *::before, .theme-anim *::after {
  transition: background-color .4s var(--ease), color .4s var(--ease), border-color .4s var(--ease), fill .4s, stroke .4s !important;
}

.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;
}
.skip {
  position: absolute; left: 12px; top: -60px; z-index: 50;
  background: var(--accent); color: var(--accent-ink);
  padding: 10px 14px; border-radius: 8px; text-decoration: none; font-weight: 600;
}
.skip:focus { top: 12px; }

/* ---------- top bar ---------- */
.topbar {
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 18px var(--gutter) 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.mark {
  display: inline-flex; align-items: center; gap: 12px;
  text-decoration: none; color: var(--ink);
}
.mark svg { width: 36px; height: 36px; }
.mark rect { fill: var(--accent); }
.mark .mark-cols { fill: var(--bg); }
.mark .mark-beat { fill: none; stroke: var(--brass); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.mark-text { font-size: 14px; font-weight: 500; letter-spacing: .01em; color: var(--muted); }

.icon-btn {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  transition: border-color .2s, background-color .2s, transform .3s var(--ease);
}
.icon-btn:hover { border-color: var(--line-strong); background: color-mix(in srgb, var(--ink) 5%, transparent); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: var(--ink); stroke-width: 1.6; stroke-linecap: round; }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }
.theme-btn svg { transition: transform .5s var(--ease); }
.theme-btn:hover svg { transform: rotate(-18deg); }

/* ---------- hero ---------- */
.hero {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: clamp(48px, 9vw, 120px) var(--gutter) clamp(28px, 4vw, 48px);
  position: relative;
}
.hero-title {
  margin: 0;
  font-family: var(--f-display);
  font-optical-sizing: auto;
  font-weight: 500;
  font-size: clamp(3.1rem, 10.5vw, 9.5rem);
  line-height: .9;
  letter-spacing: -.025em;
  color: var(--ink);
}
.hero-title span { display: block; }
.hero-title span:last-child { padding-left: clamp(0px, 9vw, 150px); }

.ecg {
  width: 100%;
  height: clamp(44px, 6vw, 84px);
  margin: clamp(10px, 2vw, 22px) 0 clamp(18px, 2.4vw, 28px);
  overflow: visible;
}
.ecg path {
  fill: none;
  stroke: var(--brass);
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: round;
  stroke-linecap: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: trace 2.2s .25s cubic-bezier(.55, .1, .3, 1) forwards;
}
@keyframes trace { to { stroke-dashoffset: 0; } }

.hero-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px 40px;
}
.hero-sub {
  margin: 0;
  max-width: 34ch;
  color: var(--muted);
  font-size: clamp(15px, 1.4vw, 17px);
}

.search {
  position: relative;
  flex: 0 1 460px;
  min-width: min(100%, 300px);
  display: flex;
  align-items: center;
  border-bottom: 1.5px solid var(--line-strong);
  transition: border-color .25s;
}
.search:focus-within { border-color: var(--accent); }
.search > svg {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: var(--muted); stroke-width: 1.7; stroke-linecap: round;
}
.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 14px 12px;
  font-size: 18px;
  outline: none;
}
.search input::placeholder { color: var(--muted); opacity: .85; }
.search input::-webkit-search-cancel-button { display: none; }
.search kbd {
  font-family: var(--f-text);
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 1px 7px;
}
.search:focus-within kbd { opacity: 0; }
.search-clear {
  border: 0; background: none; padding: 6px; border-radius: 50%;
  display: grid; place-items: center;
}
.search-clear svg { width: 16px; height: 16px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; }
.search-clear[hidden] { display: none; }
.search-clear:not([hidden]) + kbd { display: none; }

/* ---------- preview notice ---------- */
.notice {
  max-width: calc(var(--maxw) - 2 * var(--gutter));
  margin: 0 var(--gutter) 22px;
  padding: 12px 16px;
  border: 1px solid color-mix(in srgb, var(--brass) 45%, transparent);
  border-radius: 10px;
  background: color-mix(in srgb, var(--brass) 9%, var(--surface));
  color: var(--ink-soft);
  font-size: 14.5px;
}
@media (min-width: 1488px) { .notice { margin-left: auto; margin-right: auto; } }
.notice code { font-size: .92em; background: var(--bg-deep); padding: 1px 6px; border-radius: 5px; color: var(--ink); }
.notice strong { color: var(--ink); font-weight: 600; }

/* ---------- shelf ---------- */
.shelf {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: clamp(20px, 3vw, 36px) var(--gutter) clamp(64px, 8vw, 110px);
  border-top: 1px solid var(--line);
}
.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(200px, 42vw), 1fr));
  gap: clamp(30px, 4vw, 56px) clamp(16px, 2.6vw, 40px);
  padding-top: clamp(14px, 2vw, 24px);
}

.vol { min-width: 0; perspective: 1400px; }
.vol-link { display: block; text-decoration: none; color: inherit; border-radius: 6px; outline-offset: 6px; }

.vol-cover {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 2px 6px 6px 2px;
  overflow: hidden;
  background: var(--cover-blank);
  box-shadow: var(--shadow-vol);
  transform-origin: left center;
  transition: transform .55s var(--ease), box-shadow .55s var(--ease);
}
/* spine: a shaded hinge with a brass rule */
.vol-cover::before {
  content: '';
  position: absolute; inset: 0 auto 0 0;
  width: 14px;
  z-index: 3;
  background:
    linear-gradient(90deg, rgba(0,0,0,.34), rgba(0,0,0,.08) 55%, rgba(255,255,255,.14) 72%, rgba(0,0,0,.12) 100%);
  pointer-events: none;
}
.vol-cover::after {
  content: '';
  position: absolute; inset: 0;
  z-index: 3;
  box-shadow: inset -1px 0 0 rgba(255,255,255,.08), inset 0 0 0 1px rgba(0,0,0,.06);
  background: linear-gradient(105deg, transparent 40%, rgba(255,255,255,.0) 55%, rgba(255,255,255,.12) 70%, transparent 85%);
  background-size: 250% 100%;
  background-position: 100% 0;
  transition: background-position .8s var(--ease);
  pointer-events: none;
}
.vol-cover img {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  object-position: top center;
  z-index: 2;
  opacity: 0;
  transition: opacity .5s var(--ease);
}
.vol-cover img.loaded { opacity: 1; }

/* typographic cover used when no image exists */
.vol-blank {
  position: absolute; inset: 0;
  z-index: 1;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 18px 16px 18px 26px;
  color: var(--cover-blank-ink);
  background:
    radial-gradient(120% 70% at 100% 0%, rgba(255,255,255,.10), transparent 60%),
    var(--cover-blank);
}
.vol-blank::before {
  content: '';
  position: absolute; left: 22px; right: 14px; top: 50%;
  border-top: 1px solid color-mix(in srgb, var(--brass) 70%, transparent);
}
.vol-mono {
  font-family: var(--f-display);
  font-size: clamp(2rem, 4vw, 2.8rem);
  font-weight: 500;
  line-height: 1;
  color: var(--brass);
}
.vol-blank-title {
  font-family: var(--f-display);
  font-size: clamp(.95rem, 1.5vw, 1.1rem);
  line-height: 1.2;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}

.vol-new {
  position: absolute; top: 10px; right: 10px; z-index: 4;
  font-size: 11.5px; font-weight: 600; letter-spacing: .02em;
  padding: 3px 9px;
  border-radius: 99px;
  background: var(--brass);
  color: #1B1406;
}

.vol-title {
  margin: 16px 0 6px;
  font-family: var(--f-display);
  font-weight: 500;
  font-size: clamp(1.02rem, 1.35vw, 1.2rem);
  line-height: 1.25;
  letter-spacing: -.005em;
  color: var(--ink);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  text-wrap: pretty;
}
.vol-meta {
  margin: 0;
  display: flex; justify-content: space-between; gap: 8px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

@media (hover: hover) {
  .vol-link:hover .vol-cover {
    transform: rotateY(-14deg) translateX(4px);
    box-shadow: var(--shadow-vol-hover);
  }
  .vol-link:hover .vol-cover::after { background-position: 0 0; }
  .vol-link:hover .vol-title { color: var(--accent); }
}
.vol-link:active .vol-cover { transform: scale(.98); }
.vol-title { transition: color .25s; }

/* one orchestrated entrance: the shelf fills left to right */
.vol.enter { animation: shelve .8s var(--ease) both; animation-delay: calc(var(--i) * 55ms + 300ms); }
@keyframes shelve {
  from { opacity: 0; transform: translateY(18px); }
  to   { opacity: 1; transform: none; }
}

/* skeleton */
.skel .vol-cover { background: var(--bg-deep); box-shadow: none; }
.skel .vol-cover::before, .skel .vol-cover::after { display: none; }
.skel-line { height: 14px; margin-top: 16px; border-radius: 4px; background: var(--bg-deep); }
.skel-line.short { width: 55%; margin-top: 8px; }
.skel .vol-cover, .skel-line { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }

/* empty / error states */
.state { padding: clamp(40px, 8vw, 90px) 0; max-width: 46ch; }
.state-title {
  margin: 0 0 8px;
  font-family: var(--f-display);
  font-size: clamp(1.6rem, 3vw, 2.2rem);
  line-height: 1.15;
  font-weight: 500;
}
.state-text { margin: 0; color: var(--muted); }
.state[data-kind="error"] .state-title { color: #B4483B; }

/* ---------- footer ---------- */
.footer {
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 26px var(--gutter) calc(26px + env(safe-area-inset-bottom));
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 8px 24px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  color: var(--muted);
}
.footer p { margin: 0; }
.footer-sig {
  font-family: var(--f-display);
  font-style: italic;
  font-size: 18px;
  color: var(--ink);
}

/* ---------- toasts ---------- */
.toasts {
  position: fixed; z-index: 100;
  left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: grid; gap: 8px; justify-items: center;
  width: min(92vw, 440px);
  pointer-events: none;
}
.toast {
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14.5px;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,.4);
  opacity: 0; transform: translateY(10px);
  transition: opacity .3s var(--ease), transform .3s var(--ease);
}
.toast.show { opacity: 1; transform: none; }
.toast-error { background: #9E3B30; color: #fff; }
.toast-success { background: var(--accent); color: var(--accent-ink); }

/* =========================================================
   PDF reader
   ========================================================= */
body.viewer-open { overflow: hidden; }

.viewer {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column;
  background: #0B1614;
  color: #E6EEEB;
  opacity: 0;
  transform: scale(.985);
  transition: opacity .26s var(--ease), transform .26s var(--ease);
}
.viewer.open { opacity: 1; transform: none; }
.viewer[hidden] { display: none; }

.viewer-bar {
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) clamp(10px, 2vw, 22px) 10px;
  background: rgba(11, 22, 20, .92);
  border-bottom: 1px solid rgba(230, 238, 235, .09);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.viewer-title {
  flex: 1; min-width: 0;
  margin: 0;
  font-family: var(--f-display);
  font-weight: 500;
  font-size: clamp(15px, 1.6vw, 19px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.viewer-tools { display: flex; align-items: center; gap: 6px; }

.vbtn {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center;
  border-radius: 10px;
  border: 0;
  background: transparent;
  color: #E6EEEB;
  text-decoration: none;
  transition: background-color .18s;
}
.vbtn:hover { background: rgba(230, 238, 235, .09); }
.vbtn.active { color: #CDAA5C; }
.vbtn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.vbtn .i-exit { display: none; }
.vbtn.active .i-exit { display: block; }
.vbtn.active .i-enter { display: none; }
.viewer :focus-visible { outline-color: #8FD6C5; }

.vpager {
  display: flex; align-items: center; gap: 6px;
  font-size: 14px; color: #9FB3AE;
  font-variant-numeric: tabular-nums;
  margin-right: 6px;
}
.vpager input {
  width: 3.4em;
  padding: 6px 4px;
  text-align: center;
  border-radius: 8px;
  border: 1px solid rgba(230, 238, 235, .16);
  background: rgba(230, 238, 235, .05);
  color: #E6EEEB;
  -moz-appearance: textfield;
  appearance: textfield;
}
.vpager input::-webkit-inner-spin-button, .vpager input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.vzoom {
  display: flex; align-items: center;
  border: 1px solid rgba(230, 238, 235, .12);
  border-radius: 12px;
  padding: 1px;
}
.vzoom .vbtn { width: 36px; height: 36px; }
.vzoom-label { min-width: 3.4em; text-align: center; font-size: 13px; color: #9FB3AE; font-variant-numeric: tabular-nums; }

.viewer-stage {
  position: relative;
  flex: 1;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  outline: none;
  background:
    radial-gradient(80% 50% at 50% 0%, rgba(108, 194, 174, .05), transparent 70%),
    #0B1614;
}
.viewer-pages {
  display: flex; flex-direction: column; align-items: center;
  gap: clamp(10px, 1.6vw, 22px);
  padding: clamp(10px, 2.4vw, 32px) clamp(8px, 2vw, 32px) calc(48px + env(safe-area-inset-bottom));
  width: max-content;
  min-width: 100%;
}
.vpage {
  position: relative;
  flex: none;
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 14px 34px -14px rgba(0,0,0,.8);
}
.vpage:not(.ready)::after {
  content: attr(data-page);
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: #B9C4C1;
  font-family: var(--f-display);
  font-size: 28px;
}
.vpage canvas { width: 100%; height: 100%; border-radius: 2px; animation: pagein .3s ease both; }
@keyframes pagein { from { opacity: 0; } }

.viewer-loading {
  position: absolute; inset: 0;
  display: grid; place-content: center; justify-items: center; gap: 14px;
  padding: 24px;
  text-align: center;
  background: #0B1614;
}
.viewer-loading[hidden] { display: none; }
.viewer-loading p { margin: 0; color: #9FB3AE; font-size: 15px; max-width: 36ch; }
.vl-track { width: min(240px, 60vw); height: 2px; background: rgba(230, 238, 235, .12); border-radius: 2px; overflow: hidden; }
.vl-bar { height: 100%; background: #CDAA5C; transform-origin: left; transform: scaleX(.04); transition: transform .25s ease; }
.viewer-loading.error .vl-track { display: none; }
.viewer-loading.error p { color: #F0B4AA; font-size: 16px; }

:fullscreen.viewer, .viewer:fullscreen { transform: none; }

@media (max-width: 640px) {
  .mark-text { display: none; }
  .hero-title span:last-child { padding-left: 0; }
  .search kbd { display: none; }
  .search { flex-basis: 100%; }
  .hide-sm { display: none; }
  .viewer-bar { gap: 4px; }
  .vpager { margin-right: 0; }
  .vpager span { display: none; }
  .vzoom-label { display: none; }
  .vzoom { border: 0; }
  .viewer-tools { gap: 0; }
  .vbtn { width: 38px; height: 38px; }
}
@media (max-width: 380px) {
  .vzoom { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-delay: 0ms !important; transition-duration: .001ms !important; }
  .ecg path { stroke-dashoffset: 0; }
}
