/* ==========================================================================
   Layout: top bar, sidebar, main column, table-of-contents rail, footer
   ========================================================================== */

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--gutter) 0 calc(var(--gutter) * .75);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: .7rem;
  text-decoration: none;
  color: var(--text);
  min-width: 0;
  flex: none;
}
.brand:hover { color: var(--text); }
.brand__logo { width: 36px; height: 36px; color: var(--text); }
.brand__text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.brand__name { font-family: var(--font-display); font-weight: 650; font-size: 1.14rem; letter-spacing: -.01em; }
.brand__tag { font-size: .74rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

.search-trigger {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: .6rem;
  width: min(420px, 38vw);
  height: 40px;
  padding: 0 .55rem 0 .85rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: .92rem;
  box-shadow: var(--shadow-sm);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.search-trigger:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }
.search-trigger__label { flex: 1; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-trigger__kbd { font-size: .75rem; color: var(--muted); }

.topbar__actions { display: flex; align-items: center; gap: .25rem; }
.topbar .topbar__menu { display: none; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px; height: 40px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-2);
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn[aria-expanded="true"] { background: var(--surface-2); border-color: var(--border); color: var(--text); }

.theme-toggle .icon { display: none; }
[data-theme-pref="system"] .theme-toggle__system,
[data-theme-pref="light"] .theme-toggle__light,
[data-theme-pref="dark"] .theme-toggle__dark { display: block; }

/* ---------- Shell ---------- */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: calc(100vh - var(--topbar-h));
}

.sidebar {
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
  border-right: 1px solid var(--border);
  background: var(--bg);
  overflow: hidden;
}
.sidebar__inner {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.4rem .9rem 2rem 1.1rem;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
  display: flex;
  flex-direction: column;
}

.sidenav__group + .sidenav__group { margin-top: 1.35rem; }
.sidenav__title {
  font-size: .72rem;
  font-weight: 650;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0 .7rem;
  margin-bottom: .4rem;
}
.sidenav ul { list-style: none; margin: 0; padding: 0; }
.sidenav a {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .5rem .7rem;
  border-radius: 10px;
  color: var(--text-2);
  text-decoration: none;
  font-size: .95rem;
  line-height: 1.3;
  position: relative;
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}
.sidenav a .icon { width: 1.15rem; height: 1.15rem; color: var(--muted); transition: color var(--dur) var(--ease); }
.sidenav a:hover { background: var(--surface-2); color: var(--text); }
.sidenav a:hover .icon { color: var(--text); }
.sidenav a[aria-current="page"] {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: var(--shadow-sm), inset 0 0 0 1px var(--border);
}
.sidenav a[aria-current="page"] .icon { color: var(--accent); }
.sidenav a[aria-current="page"]::before {
  content: "";
  position: absolute;
  left: -1.1rem; top: 22%; bottom: 22%;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}

.sidebar__foot {
  margin-top: auto;
  padding: 1.5rem .7rem 0;
  font-size: .82rem;
  color: var(--muted);
  line-height: 1.5;
}
.sidebar__foot a { color: var(--text-2); word-break: break-all; }

.scrim {
  position: fixed;
  inset: 0;
  z-index: 44;
  background: var(--scrim);
  opacity: 0;
  transition: opacity 240ms var(--ease);
}

/* ---------- Main column ---------- */
.main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail-w);
  gap: clamp(1.5rem, 3vw, 3.5rem);
  padding: clamp(1.5rem, 3vw, 3rem) var(--gutter) var(--sp-8);
  max-width: 1760px;
  width: 100%;
  outline: none;
}

.page { min-width: 0; }

.page-head { padding-bottom: clamp(1.5rem, 3vw, 2.5rem); margin-bottom: clamp(1.5rem, 3vw, 2.75rem); border-bottom: 1px solid var(--border); }
.eyebrow {
  font-size: .78rem;
  font-weight: 650;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: .8rem;
}
.page-head h1 { max-width: 22ch; }
.page-head__lede {
  margin-top: 1rem;
  max-width: 64ch;
  font-size: var(--fs-lg);
  color: var(--text-2);
  line-height: 1.6;
}
.page-head__meta {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem 1.25rem;
  margin-top: 1.1rem;
  font-size: .85rem;
  color: var(--muted);
}
.page-head__meta span + span::before { content: "·"; margin-right: 1.25rem; }

/* ---------- Table of contents rail ---------- */
.rail { min-width: 0; }
.toc {
  position: sticky;
  top: calc(var(--topbar-h) + 2rem);
  max-height: calc(100vh - var(--topbar-h) - 4rem);
  overflow-y: auto;
  padding-left: 1rem;
  border-left: 1px solid var(--border);
  scrollbar-width: thin;
}
.toc__title {
  font-size: .72rem;
  font-weight: 650;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: .6rem;
}
.toc ol { list-style: none; margin: 0; padding: 0; }
.toc li + li { margin-top: .1rem; }
.toc a {
  display: block;
  padding: .3rem 0 .3rem .1rem;
  font-size: .86rem;
  line-height: 1.4;
  color: var(--muted);
  text-decoration: none;
  position: relative;
  transition: color var(--dur) var(--ease);
}
.toc a:hover { color: var(--text); }
.toc a.is-active { color: var(--text); font-weight: 600; }
.toc a.is-active::before {
  content: "";
  position: absolute;
  left: calc(-1rem - 1px); top: .35rem; bottom: .35rem;
  width: 2px;
  background: var(--accent);
}

/* Inline "On this page" for medium screens (built by app.js) */
.toc-inline { display: none; }

/* ---------- Pager ---------- */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-top: var(--sp-8);
  padding-top: var(--sp-6);
  border-top: 1px solid var(--border);
}
.pager__link {
  display: flex;
  flex-direction: column;
  gap: .2rem;
  padding: 1rem 1.2rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.pager__link:hover { border-color: var(--border-strong); box-shadow: var(--shadow); color: var(--text); transform: translateY(-1px); }
.pager__link small { color: var(--muted); font-size: .8rem; }
.pager__link span { font-family: var(--font-display); font-weight: 600; font-size: 1.08rem; }
.pager__next { text-align: right; grid-column: 2; }

/* ---------- Footer ---------- */
.site-footer {
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  padding: var(--sp-7) var(--gutter) var(--sp-5);
}
.site-footer__inner {
  max-width: 1500px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1.3fr 1.2fr .6fr;
  gap: var(--sp-7);
}
.site-footer__brand { display: flex; gap: 1rem; align-items: flex-start; }
.site-footer__brand img { color: var(--text); flex: none; }
.site-footer__name { font-family: var(--font-display); font-weight: 650; font-size: 1.2rem; }
.site-footer__verse { margin-top: .75rem; }
.site-footer__ar { margin: 0 0 .85rem; text-align: left; font-family: var(--font-quran); font-size: 1.35rem; line-height: 2.1; color: var(--text); }
.site-footer__en { margin: 0; color: var(--text-2); font-size: .92rem; line-height: 1.65; }
.site-footer__h { font-size: .74rem; font-weight: 650; letter-spacing: .09em; text-transform: uppercase; color: var(--muted); margin-bottom: .6rem; }
.site-footer__contact p:not(.site-footer__h) { color: var(--text-2); font-size: .92rem; line-height: 1.6; margin-bottom: 1rem; }
.site-footer__links { display: flex; flex-direction: column; gap: .35rem; font-size: .92rem; }
.site-footer__links a { color: var(--text-2); text-decoration: none; }
.site-footer__links a:hover { color: var(--text); text-decoration: underline; }
.site-footer__legal {
  max-width: 1500px;
  margin: var(--sp-7) auto 0;
  padding-top: var(--sp-5);
  border-top: 1px solid var(--border);
  font-size: .8rem;
  color: var(--muted);
}

.to-top {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 30;
  background: var(--surface);
  border-color: var(--border);
  box-shadow: var(--shadow);
}

/* ---------- Home layout: no page header, no rail ---------- */
.layout-home .page-head,
.layout-home .rail { display: none; }
.layout-home .main { grid-template-columns: minmax(0, 1fr); padding-top: clamp(1rem, 2vw, 1.75rem); }

/* ==========================================================================
   Responsive
   ========================================================================== */

/* Wide screens: give the reading column room and the rail a little more */
@media (min-width: 1600px) {
  :root { --rail-w: 272px; --sidebar-w: 308px; }
}

/* Medium: hide the rail, show inline TOC */
@media (max-width: 1279px) {
  .main { grid-template-columns: minmax(0, 1fr); }
  .rail { display: none; }
  .toc-inline { display: block; }
}

/* Small: sidebar becomes an off-canvas drawer */
@media (max-width: 1023px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .topbar .topbar__menu { display: inline-grid; margin-left: -.4rem; }
  .sidebar {
    position: fixed;
    z-index: 45;
    top: 0; left: 0; bottom: 0;
    height: 100vh; height: 100dvh;
    width: min(320px, 86vw);
    transform: translateX(-102%);
    transition: transform 280ms var(--ease), box-shadow 280ms var(--ease);
    border-right: 1px solid var(--border);
    background: var(--bg);
  }
  .sidebar__inner { padding-top: 1.25rem; }
  body.nav-open .sidebar { transform: none; box-shadow: var(--shadow-lg); }
  body.nav-open .scrim { opacity: 1; }
  body.nav-open { overflow: hidden; }
  .site-footer__inner { grid-template-columns: 1fr 1fr; }
  .site-footer__brand { grid-column: 1 / -1; }
}

@media (max-width: 720px) {
  :root { --topbar-h: 58px; }
  .brand__tag { display: none; }
  .search-trigger { width: 40px; height: 40px; padding: 0; justify-content: center; margin-left: auto; border-radius: 999px; }
  .search-trigger__label, .search-trigger__kbd { display: none; }
  .topbar { gap: .35rem; }
  .pager { grid-template-columns: 1fr; }
  .pager__next { grid-column: 1; }
  .site-footer__inner { grid-template-columns: 1fr; gap: var(--sp-6); }
}

@media (max-width: 380px) {
  .brand__name { font-size: 1.02rem; }
  .brand__logo { width: 32px; height: 32px; }
}

@media print {
  .topbar, .sidebar, .rail, .pager, .to-top, .site-footer__contact, .search, .settings { display: none !important; }
  .shell, .main { display: block; }
}

/* Inline logo sizing */
.brand__logo { width: 36px; height: 36px; flex: none; }
.site-footer__logo { width: 40px; height: 40px; flex: none; color: var(--text); }

/* Top-bar buttons keep their size; on very small phones the brand gets more compact instead */
.topbar .icon-btn, .topbar .search-trigger { flex: none; }
.brand { min-width: 0; flex: 0 1 auto; }
.brand__text { overflow: hidden; }
.brand__name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 380px) {
  .topbar { gap: .15rem; padding-left: .5rem; padding-right: .6rem; }
  .brand { gap: .4rem; }
  .brand__logo { width: 28px; height: 28px; }
  .brand__name { font-size: .95rem; }
  .topbar .icon-btn, .topbar .search-trigger { width: 38px; height: 38px; }
  .topbar__actions { gap: 0; }
}
