/* ============================================================
   القضاء الإداري — reader stylesheet
   Academic Arabic typography, RTL first, light/dark.
   ============================================================ */

:root {
  --bg:          #f6f3ec;
  --page:        #fffdf8;
  --ink:         #1d1b16;
  --ink-soft:    #4d483d;
  --ink-faint:   #7d7668;
  --rule:        #ddd5c4;
  --rule-soft:   #eae3d5;
  --accent:      #7a1f1f;
  --accent-soft: #a44a3f;
  --mark:        #ffe08a;
  --mark-live:   #ffb64d;
  --chrome:      #fbf8f1;
  --chrome-line: #e2dacb;
  --shadow:      0 1px 2px rgba(35,28,12,.06), 0 8px 26px rgba(35,28,12,.07);

  --measure: 850px;
  --lead: 2.05;
  --fscale: 1;

  --fs-body: calc(1.135rem * var(--fscale));
  --fs-note: calc(.95rem  * var(--fscale));

  --serif-ar: 'Amiri', 'Noto Naskh Arabic', 'Geeza Pro', 'Al Bayan', 'Baghdad', serif;
  --sans-ar:  'Noto Naskh Arabic', 'Amiri', 'SF Arabic', 'Geeza Pro', system-ui, sans-serif;
  --ui: system-ui, -apple-system, 'Segoe UI', 'Noto Naskh Arabic', sans-serif;
}

html[data-theme="dark"] {
  --bg:          #14161a;
  --page:        #1b1e23;
  --ink:         #e6e2d8;
  --ink-soft:    #b9b3a6;
  --ink-faint:   #8b8579;
  --rule:        #343941;
  --rule-soft:   #262a30;
  --accent:      #e2a17a;
  --accent-soft: #caa08a;
  --mark:        #6a5312;
  --mark-live:   #9a760f;
  --chrome:      #181b20;
  --chrome-line: #2a2f36;
  --shadow:      0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans-ar);
  line-height: 1.6;
  overflow-wrap: break-word;
}

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8;
      stroke-linecap: round; stroke-linejoin: round; }

.skip-link {
  position: absolute; inset-inline-start: -9999px; top: 0; z-index: 200;
  background: var(--accent); color: #fff; padding: .6rem 1rem; border-radius: 0 0 8px 8px;
}
.skip-link:focus { inset-inline-start: 1rem; }

:focus-visible { outline: 2px solid var(--accent-soft); outline-offset: 2px; border-radius: 4px; }

/* ---------------------------------------------------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 60;
  background: color-mix(in srgb, var(--chrome) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--chrome-line);
}
.topbar-row {
  display: flex; align-items: center; gap: .55rem;
  padding: .5rem clamp(.6rem, 2vw, 1.1rem);
}
.brand { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-title { font-family: var(--serif-ar); font-size: 1.12rem; font-weight: 700; letter-spacing: .01em; }
.brand-sub { font-size: .72rem; color: var(--ink-faint); white-space: nowrap;
             overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; flex: none;
  border: 1px solid transparent; border-radius: 9px;
  background: transparent; color: var(--ink-soft); cursor: pointer;
}
.icon-btn:hover { background: var(--rule-soft); color: var(--ink); }
.icon-btn.tiny { width: 30px; height: 30px; }
.icon-btn.tiny svg { width: 16px; height: 16px; }

#themeToggle .ico-sun { display: none; }
html[data-theme="dark"] #themeToggle .ico-moon { display: none; }
html[data-theme="dark"] #themeToggle .ico-sun { display: block; }

.search-wrap {
  position: relative; flex: 1 1 auto; max-width: 560px; margin-inline: auto;
  display: flex; align-items: center; gap: .25rem;
  background: var(--page); border: 1px solid var(--chrome-line);
  border-radius: 11px; padding: .2rem .55rem;
}
.search-wrap:focus-within { border-color: var(--accent-soft); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-soft) 18%, transparent); }
.search-icon { width: 17px; height: 17px; color: var(--ink-faint); flex: none; }
#q {
  flex: 1 1 auto; min-width: 0; border: 0; background: transparent; color: inherit;
  font: inherit; font-size: .96rem; padding: .42rem .15rem;
}
#q::-webkit-search-cancel-button { display: none; }
#q:focus { outline: none; }
.search-nav { display: flex; align-items: center; gap: .1rem; }
.match-counter { font-size: .78rem; color: var(--ink-faint); white-space: nowrap;
                 font-variant-numeric: tabular-nums; padding-inline: .25rem; }

.tools { display: flex; align-items: center; gap: .35rem; flex: none; }
.seg { display: inline-flex; background: var(--rule-soft); border-radius: 10px; padding: 2px; gap: 2px; }
.seg-btn {
  border: 0; background: transparent; color: var(--ink-soft); cursor: pointer;
  font: inherit; font-size: .82rem; padding: .34rem .62rem; border-radius: 8px; white-space: nowrap;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn.is-on { background: var(--page); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.seg-out { align-self: center; font-size: .8rem; padding-inline: .5rem; color: var(--ink-soft);
           font-variant-numeric: tabular-nums; }

.btn {
  border: 1px solid var(--chrome-line); background: var(--page); color: var(--ink);
  font: inherit; font-size: .85rem; padding: .4rem .8rem; border-radius: 9px; cursor: pointer;
}
.btn:hover { border-color: var(--accent-soft); }
.btn.ghost { background: transparent; }

/* ---------------------------------------------------------- panels */
.panel { background: var(--chrome); border-bottom: 1px solid var(--chrome-line); }
.settings { padding: .7rem clamp(.6rem, 2vw, 1.1rem) .9rem; display: grid; gap: .65rem; }
.set-row { display: flex; align-items: center; gap: .7rem; flex-wrap: wrap; }
.set-label { font-size: .84rem; color: var(--ink-soft); min-width: 6.5rem; }
.switch { display: inline-flex; align-items: center; gap: .4rem; font-size: .84rem; color: var(--ink-soft); cursor: pointer; }
.goto { display: flex; gap: .4rem; flex-wrap: wrap; }
.goto input, .goto select {
  font: inherit; font-size: .85rem; padding: .35rem .6rem; border-radius: 9px;
  border: 1px solid var(--chrome-line); background: var(--page); color: var(--ink);
}
.goto input { width: 11rem; }
.set-note { margin: 0; font-size: .78rem; color: var(--ink-faint); }

/* ---------------------------------------------------------- layout */
.layout { display: flex; align-items: flex-start; }

.sidebar {
  position: sticky; top: 56px; align-self: flex-start;
  width: 310px; flex: none; max-height: calc(100vh - 56px);
  overflow-y: auto; overscroll-behavior: contain;
  border-inline-start: 1px solid var(--chrome-line);
  background: var(--chrome);
  padding: .8rem .8rem 3rem;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.sidebar-title { margin: .1rem 0 .5rem; font-family: var(--serif-ar); font-size: 1.05rem; }
.toc-filter input {
  width: 100%; font: inherit; font-size: .85rem; padding: .38rem .6rem; margin-bottom: .5rem;
  border: 1px solid var(--chrome-line); border-radius: 9px; background: var(--page); color: var(--ink);
}
.only-mobile { display: none; }

.toc, .toc ol { list-style: none; margin: 0; padding: 0; }
.toc ol { padding-inline-start: .75rem; border-inline-start: 1px solid var(--rule-soft); margin-inline-start: .3rem; }
.toc li { margin: .06rem 0; }
.toc-item { display: flex; align-items: flex-start; gap: .15rem; }
.toc-twist {
  flex: none; width: 20px; height: 22px; border: 0; background: transparent; cursor: pointer;
  color: var(--ink-faint); display: grid; place-items: center; padding: 0;
}
.toc-twist svg { width: 13px; height: 13px; transition: transform .16s ease; }
.toc-twist[aria-expanded="false"] svg { transform: rotate(90deg); }
.toc-link {
  flex: 1 1 auto; display: block; text-decoration: none; color: var(--ink-soft);
  font-size: .855rem; line-height: 1.45; padding: .2rem .35rem; border-radius: 7px;
}
.toc-link:hover { background: var(--rule-soft); color: var(--ink); }
.toc-link .lab { color: var(--accent); font-weight: 600; }
.toc-link .pg { float: inline-end; color: var(--ink-faint); font-size: .76rem; padding-inline-start: .4rem;
                font-variant-numeric: tabular-nums; }
.toc-link.is-current { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--ink); }
.toc > li > .toc-item > .toc-link { font-weight: 700; font-family: var(--serif-ar); font-size: .98rem; color: var(--ink); }
.toc li[hidden] { display: none; }
.toc-empty { color: var(--ink-faint); font-size: .85rem; padding: .5rem .3rem; }

.sidebar-scrim { display: none; }

/* ---------------------------------------------------------- reader */
.reader { flex: 1 1 auto; min-width: 0; padding: 1.4rem clamp(.65rem, 3vw, 2rem) 6rem; }
.book { max-width: var(--measure); margin-inline: auto; }

.loading { max-width: var(--measure); margin: 4rem auto; color: var(--ink-faint);
           display: flex; align-items: center; gap: .6rem; justify-content: center; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--rule); border-top-color: var(--accent);
           border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- page -------------------------------------------------------- */
.book-page {
  background: var(--page);
  border: 1px solid var(--rule-soft);
  border-radius: 12px;
  box-shadow: var(--shadow);
  margin-block: 1.1rem;
  padding: clamp(1.1rem, 3.2vw, 2.4rem) clamp(1rem, 3.4vw, 2.6rem) 1.2rem;
  scroll-margin-top: 78px;
}
.book-page[data-kind="divider"],
.book-page[data-kind="title"] { text-align: center; }

.page-marker {
  display: flex; align-items: center; gap: .6rem;
  font-size: .74rem; color: var(--ink-faint);
  margin-bottom: 1.1rem; padding-bottom: .5rem;
  border-bottom: 1px solid var(--rule-soft);
}
.page-marker .bp { font-weight: 700; color: var(--ink-soft); font-family: var(--serif-ar); font-size: .95rem; }
.page-marker .pdfno { font-variant-numeric: tabular-nums; }
.page-marker .spacer { flex: 1 1 auto; }
.page-marker .est { color: var(--accent-soft); }
.page-tools { display: flex; gap: .15rem; }
.page-tools .icon-btn.tiny { width: 28px; height: 28px; color: var(--ink-faint); }

.page-foot { margin-top: 1rem; text-align: center; font-size: .72rem; color: var(--ink-faint);
             border-top: 1px solid var(--rule-soft); padding-top: .5rem; }

/* --- text -------------------------------------------------------- */
.book-body { font-family: var(--serif-ar); font-size: var(--fs-body); line-height: var(--lead); }

.book-body p {
  margin: 0 0 .35em;
  text-indent: 2.1em;
  text-align: justify;
  text-justify: inter-word;
  hyphens: none;
  scroll-margin-top: 90px;
}
.book-body p.cont { text-indent: 0; }
.book-body p.li { text-indent: 0; padding-inline-start: 1.6em; margin-bottom: .2em; }
.book-body ol.src-list { list-style: none; margin: .2em 0 .5em; padding: 0; }
.book-body ol.src-list > li { text-indent: 0; padding-inline-start: 1.6em; margin-bottom: .2em;
                              text-align: justify; }
.book-body p.ltr, .fn-item.ltr { direction: ltr; text-align: left; text-indent: 0;
                                 font-family: 'Amiri', Georgia, serif; }
.book-body p.display { text-indent: 0; text-align: center; font-weight: 600; margin-block: .8em; }

h2.bh, h3.bh, h4.bh, h5.bh, h6.bh {
  font-family: var(--serif-ar); text-align: center; text-indent: 0;
  margin: 1.5em 0 .7em; line-height: 1.45; scroll-margin-top: 90px;
  font-weight: 700;
}
h2.bh { font-size: 1.5em; color: var(--accent); }
h3.bh { font-size: 1.3em; }
h4.bh { font-size: 1.16em; }
h5.bh { font-size: 1.06em; }
h6.bh { font-size: 1em; font-weight: 600; color: var(--ink-soft); }
.bh .lab { display: block; font-size: .8em; color: var(--accent); letter-spacing: .02em; margin-bottom: .12em; }
h2.bh .lab { color: var(--accent-soft); }
.bh .ttl { display: block; }
.bh:hover .anchor { opacity: 1; }
.anchor { opacity: 0; font-size: .6em; color: var(--ink-faint); text-decoration: none;
          margin-inline-start: .4em; vertical-align: middle; }
.anchor:focus { opacity: 1; }

.book-h1 {
  font-family: var(--serif-ar); text-align: center; margin: .2rem 0 1.4rem;
  font-size: clamp(1.6rem, 4vw, 2.2rem); color: var(--accent); line-height: 1.3;
}
.book-h1-sub { display: block; font-size: .46em; color: var(--ink-faint);
               font-weight: 400; margin-top: .35em; }

.title-block { font-family: var(--serif-ar); text-align: center; }
.book-body .title-block p.tl { margin: .5em 0; text-indent: 0; text-align: center; }
.book-body .title-block p.tl-1 { font-size: 2.05em; font-weight: 700; letter-spacing: .01em;
                                 margin-bottom: .7em; color: var(--accent); }
.book-body .title-block p.tl-2 { font-size: 1.12em; }

/* --- footnote references and notes -------------------------------- */
sup.footnote-ref { font-size: .62em; line-height: 0; vertical-align: super; }
sup.footnote-ref a { color: var(--accent); text-decoration: none; padding: 0 .12em;
                     border-radius: 3px; font-variant-numeric: tabular-nums; }
sup.footnote-ref a:hover, sup.footnote-ref a:focus { background: color-mix(in srgb, var(--accent) 16%, transparent); }

.footnotes {
  margin-top: 1.5rem; padding-top: .7rem;
  border-top: 1px solid var(--rule);
  font-family: var(--serif-ar); font-size: var(--fs-note); line-height: 1.85; color: var(--ink-soft);
}
.footnotes ol { list-style: none; margin: 0; padding: 0; counter-reset: none; }
.fn-item { margin-bottom: .45em; text-align: justify; scroll-margin-top: 90px; }
.fn-item .fn-num { color: var(--accent); font-weight: 700; margin-inline-end: .25em;
                   font-variant-numeric: tabular-nums; }
.fn-item .fn-back { color: var(--ink-faint); text-decoration: none; margin-inline-start: .3em; font-size: .85em; }
.fn-item .fn-back:hover { color: var(--accent); }
.fn-item.inferred .fn-num::after { content: '؟'; font-size: .8em; color: var(--ink-faint); }
body.no-footnotes .footnotes { display: none; }
:target.fn-item, :target.book-page > .page-marker { background: color-mix(in srgb, var(--mark) 45%, transparent); }
.flash { animation: flash 1.6s ease-out; border-radius: 6px; }
@keyframes flash { from { background: color-mix(in srgb, var(--mark) 70%, transparent); } to { background: transparent; } }

.para-copy {
  position: absolute; z-index: 40;
  width: 26px; height: 26px; display: grid; place-items: center;
  border: 1px solid var(--rule); border-radius: 7px;
  background: var(--page); color: var(--ink-faint);
  cursor: pointer; opacity: .55; transition: opacity .12s ease;
}
.para-copy:hover, .para-copy:focus-visible { opacity: 1; color: var(--accent); border-color: var(--accent-soft); }
.para-copy svg { width: 14px; height: 14px; }
@media (hover: none) { .para-copy { display: none; } }

/* --- search marks -------------------------------------------------- */
mark.hit { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 .06em; }
mark.hit.live { background: var(--mark-live); box-shadow: 0 0 0 2px var(--mark-live); }

/* --- OCR confidence (review mode only) ----------------------------- */
.ocr-uncertain { border-radius: 3px; }
body.review .ocr-uncertain[data-confidence="medium"] { background: color-mix(in srgb, #f5c542 34%, transparent); }
body.review .ocr-uncertain[data-confidence="low"]    { background: color-mix(in srgb, #e05a5a 22%, transparent);
                                                       border-bottom: 2px solid #d64545; }
body.review .book-page[data-flags]:not([data-flags=""]) { outline: 2px dashed var(--accent-soft); }
.review-badge { display: none; }
body.review .review-badge { display: inline-flex; gap: .3rem; font-size: .7rem; color: var(--accent);
                            border: 1px solid var(--accent-soft); border-radius: 6px; padding: 0 .35rem; }

/* --- original scan -------------------------------------------------- */
.scan { margin-top: 1.2rem; display: none; }
.scan img { width: 100%; height: auto; display: block; border: 1px solid var(--rule);
            border-radius: 8px; background: #fff; }
.scan figcaption { font-size: .72rem; color: var(--ink-faint); text-align: center; margin-top: .4rem; }

body[data-view="both"] .scan { display: block; }
body[data-view="scan"] .scan { display: block; }
body[data-view="scan"] .book-body,
body[data-view="scan"] .footnotes { display: none; }

@media (min-width: 1100px) {
  body[data-view="both"] .page-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.4rem; align-items: start; }
  body[data-view="both"] .scan { margin-top: 0; position: sticky; top: 80px; }
  body[data-view="both"] .book { max-width: min(1500px, 96vw); }
}

.scan-toggle { font-size: .72rem; }

/* ---------------------------------------------------------- search panel */
.search-panel {
  position: fixed; inset-block-start: 0; inset-inline-end: 0; z-index: 90;
  width: min(430px, 100vw); height: 100vh; overflow-y: auto;
  border-inline-start: 1px solid var(--chrome-line); box-shadow: var(--shadow);
  padding: .8rem;
}
.sp-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem;
           padding-bottom: .5rem; border-bottom: 1px solid var(--chrome-line); margin-bottom: .5rem; }
.sp-summary { font-size: .85rem; color: var(--ink-soft); }
.results { list-style: none; margin: 0; padding: 0; }
.results li { margin-bottom: .3rem; }
.res {
  display: block; width: 100%; text-align: start; background: var(--page);
  border: 1px solid var(--chrome-line); border-radius: 10px; padding: .55rem .7rem;
  color: inherit; cursor: pointer; font: inherit;
}
.res:hover, .res.is-current { border-color: var(--accent-soft); background: color-mix(in srgb, var(--accent) 7%, var(--page)); }
.res-head { display: block; font-size: .76rem; color: var(--accent); margin-bottom: .3rem;
            overflow: hidden; }
.res-page { float: inline-end; color: var(--ink-faint); font-variant-numeric: tabular-nums; }
.res-snip { display: block; font-family: var(--serif-ar); font-size: .95rem; line-height: 1.7;
            color: var(--ink-soft); }
.res-more { text-align: center; color: var(--ink-faint); font-size: .8rem; padding: .5rem; }

/* ---------------------------------------------------------- toast */
.toast {
  position: fixed; inset-block-end: 1.1rem; inset-inline-start: 50%; transform: translateX(50%);
  background: var(--ink); color: var(--page); padding: .55rem 1rem; border-radius: 10px;
  font-size: .85rem; z-index: 200; box-shadow: var(--shadow);
}

/* ---------------------------------------------------------- responsive */
@media (max-width: 1000px) {
  .brand-sub { display: none; }
  .tools .seg-btn { padding: .34rem .45rem; font-size: .78rem; }
}
@media (max-width: 860px) {
  /* The drawer sits on the inline-start edge (the right, in RTL) and slides out
     to the physical right, because `transform` is always physical. */
  .sidebar {
    position: fixed; inset-block: 0; inset-inline-start: 0; top: 0; max-height: none; height: 100dvh;
    width: min(320px, 86vw); z-index: 100; transform: translateX(100%);
    transition: transform .22s ease; padding-top: .8rem;
    border-inline-start: 0; border-inline-end: 1px solid var(--chrome-line);
  }
  @media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
  body.toc-open .sidebar { transform: none; }
  body.toc-open .sidebar-scrim { display: block; position: fixed; inset: 0; z-index: 95;
                                 background: rgba(0,0,0,.4); }
  .only-mobile { display: inline-grid; }
  .brand { display: none; }
  .reader { padding-inline: .55rem; }
  .book-page { padding: 1rem .9rem 1rem; border-radius: 10px; }
  .search-panel { width: 100vw; height: 100dvh; }
  .tools .seg { display: none; }
  .icon-btn { width: 40px; height: 40px; }
}
@media (max-width: 620px) {
  .topbar-row { gap: .3rem; padding: .4rem .5rem; }
  :root { --fs-body: calc(1.06rem * var(--fscale)); }
  .set-label { min-width: 100%; }
  /* a short measure plus justification opens rivers of white space in Arabic */
  .book-body p, .book-body ol.src-list > li, .fn-item { text-align: start; }
  .book-body p { text-indent: 1.4em; }
}

/* ---------------------------------------------------------- print */
@media print {
  .topbar, .sidebar, .search-panel, .toast, .page-tools, .sidebar-scrim,
  .skip-link, .anchor, .scan-toggle, .para-copy { display: none !important; }
  .book-h1 { color: #000; margin-top: 0; }
  body { background: #fff; color: #000; }
  .layout { display: block; }
  .reader { padding: 0; }
  .book { max-width: none; }
  .book-page { box-shadow: none; border: 0; border-radius: 0; margin: 0;
               padding: 0 0 1.2rem; break-after: page; background: #fff; }
  .page-marker { border-bottom: 1px solid #bbb; color: #444; }
  .book-body { font-size: 12pt; line-height: 1.85; }
  .footnotes { font-size: 9.5pt; border-top: 1px solid #999; }
  h2.bh, h3.bh, h4.bh, h5.bh { break-after: avoid; color: #000; }
  .bh .lab { color: #444; }
  mark.hit { background: none; box-shadow: none; }
  .ocr-uncertain { background: none !important; border: 0 !important; }
  body:not([data-view="scan"]) .scan { display: none !important; }
  .book-body p { orphans: 3; widows: 3; }
}
