/*
 * Design tokens. Three layers: primitives (per theme) → semantic roles → components.
 * Components use ONLY semantic roles. A theme is one block of values; nothing else changes.
 * Identity follows the approved intro: parchment, walnut ink, cinnabar seal, restrained antique gold.
 */
:root {
  /* type scale for the Arabic base text: 5 steps (decision 4) */
  --text-px: 22px;
  --ui-px: 14px;
  --space: 1;
  --font-ui: 'Vazirmatn', system-ui, sans-serif;
  --font-text: 'Scheherazade New', 'Noto Naskh Arabic', serif;
  --font-note: 'Noto Naskh Arabic', 'Amiri', serif;
  /* Motion (shared): strong ease-out for enter/press, ease for colour; UI stays under 300ms. */
  --ease-out: cubic-bezier(.23, 1, .32, 1);
  --ease-in-out: cubic-bezier(.77, 0, .175, 1);
  --ease-drawer: cubic-bezier(.32, .72, 0, 1);
  --dur-press: 160ms;
  --dur-hover: 180ms;
  --dur-ui: 220ms;
  /* Shared semantic roles. Intro artwork stays independent and unchanged. */
  --brand-paper: #fff5df;
  --brand-gold: #e3c47f;
  --dash-bg: var(--bg);
  --dash-card: var(--surface);
  --dash-raised: var(--card);
  --dash-line: var(--rule);
  --dash-glass: color-mix(in srgb, var(--surface) 88%, transparent);
  --dash-shadow: var(--shadow);
  --overlay: color-mix(in srgb, var(--viewer-bg) 52%, transparent);
  --section-rijal: var(--info);
  --section-collation: var(--rubric);
  --section-text: var(--review);
  --section-fiqh: var(--gold);
  --section-takhrij: var(--info);
  --section-archive: var(--ink-2);
  /* One minimal scroll indicator: no track, a muted thumb, stronger on hover. */
  --scroll-track: transparent;
  --scroll-thumb: color-mix(in srgb, var(--ink-3) 30%, transparent);
  --scroll-thumb-hover: color-mix(in srgb, var(--ink-2) 60%, transparent);
  /* rounded, iOS / One UI-like geometry */
  --r-xs: 8px;
  --r-sm: 12px;
  --r-md: 16px;
  --r-lg: 24px;
  --r-xl: 28px;
  --r-pill: 999px;
  --radius: var(--r-sm);
  --s1: calc(4px * var(--space));
  --s2: calc(8px * var(--space));
  --s3: calc(12px * var(--space));
  --s4: calc(16px * var(--space));
  --s5: calc(24px * var(--space));
  --s6: calc(32px * var(--space));
}
/* The product is RTL; keeping this explicit also places viewport scrollbars on the left. */
html { direction: rtl; }
/* Match the browser canvas to the selected workspace, including navigation paints. */
html[data-workspace-page] { background-color:var(--bg); }
html[data-workspace-page]:not([data-ui-ready]) body { visibility: hidden; }
html[data-workspace-page][data-native-workspace='pending'] body { visibility: hidden; }
/* Firefox and engines without WebKit scrollbar pseudo-elements. Chromium must
   keep using ::-webkit-scrollbar, otherwise the native arrows and tapered ends
   override the suite scrollbar geometry. */
@supports not selector(::-webkit-scrollbar) {
  * {
    scrollbar-width: thin;
    scrollbar-color: var(--scroll-thumb) transparent;
  }
}
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-button {
  display: none;
  width: 0;
  height: 0;
}
*::-webkit-scrollbar-track {
  margin: 0;
  background: transparent;
  border: 0;
}
*::-webkit-scrollbar-thumb {
  min-height: 28px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--scroll-thumb);
  background-clip: padding-box;
}
*::-webkit-scrollbar-thumb:hover { background: var(--scroll-thumb-hover); background-clip: padding-box; }
*::-webkit-scrollbar-corner { background: transparent; }
@media (forced-colors: active) {
  * { scrollbar-color: ButtonText transparent; }
  *::-webkit-scrollbar-track { background: transparent; }
  *::-webkit-scrollbar-thumb { background: ButtonText; }
}
:root[data-size='1'] { --text-px: 18px; }
:root[data-size='2'] { --text-px: 20px; }
:root[data-size='3'] { --text-px: 22px; }
:root[data-size='4'] { --text-px: 25px; }
:root[data-size='5'] { --text-px: 28px; }
:root[data-density='compact'] { --ui-px: 13px; --space: 0.78; }
:root[data-density='normal'] { --ui-px: 14px; --space: 1; }
:root[data-density='comfortable'] { --ui-px: 15.5px; --space: 1.22; }

/* ── paper: intro-derived manuscript palette ── */
:root, :root[data-theme='paper'], .theme-preview[data-theme='paper'] {
  --bg: #eee5d3;
  --surface: #faf4e7;
  --surface-2: #e8dcc5;
  --card: #fffaf0;
  --sunken: #ded0b5;
  --ink: #35251c;
  --ink-2: #594533;
  --ink-3: #705b46;
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M7 10L12 15L17 10' stroke='%23705b46' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #ccb99a;
  --rule-strong: #907554;
  --accent: #6c291b;
  --accent-ink: #fff5df;
  --accent-soft: #eed9c9;
  --accent-hover: #572014;
  --rubric: #80271e;
  --rubric-soft: #f3ddd1;
  --gold: #795521;
  --selection: #e6c995;
  --warn: #795000;
  --warn-soft: #f5e5bd;
  --review: #67436b;
  --review-soft: #ecdfeb;
  --error: #a02421;
  --error-soft: #f8dfd7;
  --success: #365c42;
  --success-soft: #e2ebd9;
  --info: #315e64;
  --info-soft: #deebe6;
  --viewer-bg: #30271f;
  --focus: #6c291b;
  --nav-bg: #e4d5b8;
  --nav-ink: #35251c;
  --nav-muted: #5e4935;
  --nav-mark: #6c291b;
  --nav-active: #f8e9d3;
  --nav-active-line: #8b4b31;
  --nav-line: #89704f;
  --hero-1: #6c291b;
  --hero-2: #8b4b31;
  --hero-ink: #fff5df;
  --guide: #f2c45f;
  --guide-soft: rgba(242,196,95,.16);
  --glow-1: rgba(108,41,27,.10);
  --glow-2: rgba(166,119,57,.12);
  --card-border: #ccb99a;
  --shadow: 0 1px 2px rgba(53,37,28,.04), 0 8px 24px -12px rgba(53,37,28,.20);
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .35 0 0 0 0 .27 0 0 0 0 .15 0 0 0 .07 0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
}
/* ── light: intro-derived manuscript palette ── */
:root[data-theme='light'], .theme-preview[data-theme='light'] {
  --bg: #f4f0e8;
  --surface: #fffdf8;
  --surface-2: #eee6d8;
  --card: #ffffff;
  --sunken: #e5dac7;
  --ink: #30231c;
  --ink-2: #554336;
  --ink-3: #6f5d4e;
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M7 10L12 15L17 10' stroke='%236f5d4e' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #d4c5af;
  --rule-strong: #947a5b;
  --accent: #6c291b;
  --accent-ink: #fffaf0;
  --accent-soft: #f3e3d8;
  --accent-hover: #572014;
  --rubric: #80271e;
  --rubric-soft: #f6e4db;
  --gold: #795521;
  --selection: #ecd5ae;
  --warn: #795000;
  --warn-soft: #fbefd1;
  --review: #67436b;
  --review-soft: #f0e6ef;
  --error: #a02421;
  --error-soft: #fbe6e0;
  --success: #365c42;
  --success-soft: #e8f0e2;
  --info: #315e64;
  --info-soft: #e6f0eb;
  --viewer-bg: #30271f;
  --focus: #6c291b;
  --nav-bg: #eee5d5;
  --nav-ink: #30231c;
  --nav-muted: #5e4935;
  --nav-mark: #6c291b;
  --nav-active: #fffaf0;
  --nav-active-line: #8b4b31;
  --nav-line: #907658;
  --hero-1: #6c291b;
  --hero-2: #8b4b31;
  --hero-ink: #fffaf0;
  --guide: #f2c45f;
  --guide-soft: rgba(242,196,95,.16);
  --glow-1: rgba(108,41,27,.08);
  --glow-2: rgba(166,119,57,.08);
  --card-border: #d4c5af;
  --shadow: 0 1px 2px rgba(48,35,28,.04), 0 8px 24px -12px rgba(48,35,28,.16);
  --grain: none;
}
/* ── dark: intro-derived manuscript palette ── */
:root[data-theme='dark'], .theme-preview[data-theme='dark'] {
  --bg: #191613;
  --surface: #231e19;
  --surface-2: #2e261f;
  --card: #28211b;
  --sunken: #14110e;
  --ink: #f3e7d3;
  --ink-2: #d4c1a5;
  --ink-3: #bfa88a;
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M7 10L12 15L17 10' stroke='%23bfa88a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #594735;
  --rule-strong: #9c8162;
  --accent: #efb28f;
  --accent-ink: #321b12;
  --accent-soft: #4b3024;
  --accent-hover: #f6c9a8;
  --rubric: #f0a58c;
  --rubric-soft: #482a23;
  --gold: #d9b46c;
  --selection: #62472c;
  --warn: #edc779;
  --warn-soft: #42341d;
  --review: #d4b0d6;
  --review-soft: #3c2c3e;
  --error: #ffa89c;
  --error-soft: #4c2722;
  --success: #b0d0a4;
  --success-soft: #263a27;
  --info: #a3cdcb;
  --info-soft: #263b3b;
  --viewer-bg: #110e0b;
  --focus: #efc87b;
  --nav-bg: #211b16;
  --nav-ink: #f3e7d3;
  --nav-muted: #d4c1a5;
  --nav-mark: #e6bd86;
  --nav-active: #493225;
  --nav-active-line: #d4a36c;
  --nav-line: #a18663;
  --hero-1: #4b3024;
  --hero-2: #654331;
  --hero-ink: #f3e7d3;
  --guide: #f2c45f;
  --guide-soft: rgba(242,196,95,.16);
  --glow-1: rgba(239,178,143,.08);
  --glow-2: rgba(217,180,108,.08);
  --card-border: #594735;
  --shadow: 0 1px 2px rgba(0,0,0,.2), 0 8px 24px -12px rgba(0,0,0,.45);
  --grain: none;
}
/* ── contrast: intro-derived manuscript palette ── */
:root[data-theme='contrast'], .theme-preview[data-theme='contrast'] {
  --bg: #000000;
  --surface: #000000;
  --surface-2: #0d0d0d;
  --card: #000000;
  --sunken: #000000;
  --ink: #ffffff;
  --ink-2: #ffffff;
  --ink-3: #e6e6e6;
  --select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M7 10L12 15L17 10' stroke='%23e6e6e6' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #ffffff;
  --rule-strong: #ffffff;
  --accent: #ffd48a;
  --accent-ink: #000000;
  --accent-soft: #332500;
  --accent-hover: #ffe5b3;
  --rubric: #ffbba8;
  --rubric-soft: #3b160d;
  --gold: #ffffff;
  --selection: #594000;
  --warn: #ffd23f;
  --warn-soft: #2b2400;
  --review: #e4c8ff;
  --review-soft: #241a4d;
  --error: #ffb0a6;
  --error-soft: #3b1510;
  --success: #bce9aa;
  --success-soft: #142b10;
  --info: #b5e9f1;
  --info-soft: #092c32;
  --viewer-bg: #000000;
  --focus: #ffd23f;
  --nav-bg: #000000;
  --nav-ink: #ffffff;
  --nav-muted: #ffffff;
  --nav-mark: #ffd48a;
  --nav-active: #332500;
  --nav-active-line: #ffd48a;
  --nav-line: #ffffff;
  --hero-1: #000000;
  --hero-2: #000000;
  --hero-ink: #ffffff;
  --guide: #ffd23f;
  --guide-soft: rgba(255,210,63,.20);
  --glow-1: transparent;
  --glow-2: transparent;
  --card-border: #ffffff;
  --shadow: none;
  --grain: none;
}

/* -- classic manuscript rail material (palette values only) -- */
:root[data-design='manuscript'][data-theme='paper'],
:root[data-design='manuscript'][data-theme='light']{
  --rail-paper-base:#d8c69e;
  --rail-paper-ink:#3b2b20;
  --rail-paper-muted:#513b29;
  --rail-paper-mark:#8b5e2e;
  --rail-paper-active:#eedfbd;
  --rail-paper-line:transparent;
  --rail-paper-veil:rgba(255,247,222,.24);
  --rail-paper-opacity:.72;
}
:root[data-design='manuscript'][data-theme='dark']{
  --rail-paper-base:#1d1511;
  --rail-paper-ink:#f3e7d3;
  --rail-paper-muted:#c8b494;
  --rail-paper-mark:#e5bd78;
  --rail-paper-active:#3d2a20;
  --rail-paper-line:transparent;
  --rail-paper-veil:transparent;
  --rail-paper-opacity:0;
}
:root[data-design='manuscript'][data-theme='contrast']{
  --rail-paper-base:#000000;
  --rail-paper-ink:#ffffff;
  --rail-paper-muted:#ffffff;
  --rail-paper-mark:#ffd48a;
  --rail-paper-active:#332500;
  --rail-paper-line:#ffffff;
  --rail-paper-veil:transparent;
  --rail-paper-opacity:0;
}
:root[data-design='manuscript']{
  --suite-label:#3e2d21;
  --suite-icon:#65492f;
  --suite-active-label:#38271d;
  --suite-active-icon:#8b5e2e;
  --suite-active:#eedfbd;
  --suite-hover-bg:#e5d2ab;
}

/* Shared native-select chrome: one inset chevron, never the engine arrow at the edge.
   The gradient uses currentColor so all four colour modes keep a legible mark. */
select:not([multiple]):not([size]) {
  appearance: none !important;
  -webkit-appearance: none !important;
  background-image: var(--select-arrow) !important;
  background-position: left 12px center !important;
  background-size: 24px 24px !important;
  background-repeat: no-repeat !important;
  padding-inline-end: 44px !important;
}

/* One designed picker for every single select. Chromium supports the
   customizable picker; other engines keep the native list and the chevron above. */
@supports (appearance: base-select) {
  select:not([multiple]):not([size]) {
    appearance: base-select !important;
    -webkit-appearance: base-select !important;
  }
  select::picker-icon {
    display: none !important;
  }
  select::picker(select) {
    margin-block: 6px;
    padding: 6px;
    border: 1px solid var(--rule-strong);
    border-radius: 14px;
    background: var(--surface);
    color: var(--ink);
    box-shadow: 0 18px 44px -18px color-mix(in srgb, var(--ink) 42%, transparent);
    max-block-size: min(360px, 60vh);
    min-inline-size: min(220px, calc(100vw - 24px));
    inline-size: max(anchor-size(width), min(280px, calc(100vw - 24px)));
    max-inline-size: min(440px, calc(100vw - 24px));
    overflow-y: auto;
    font: 400 16px/1.8 var(--font-ui);
    z-index: 100;
  }
  select::picker(select)::-webkit-scrollbar { width: 6px; height: 6px; }
  select::picker(select)::-webkit-scrollbar-track {
    margin: 0;
    background: transparent;
    border: 0;
  }
  select::picker(select)::-webkit-scrollbar-thumb {
    min-height: 28px;
    border: 1px solid transparent;
    border-radius: 999px;
    background: var(--scroll-thumb);
    background-clip: padding-box;
  }
  select::picker(select)::-webkit-scrollbar-thumb:hover {
    background: var(--scroll-thumb-hover);
    background-clip: padding-box;
  }
  select:not([multiple]):not([size]) option {
    min-height: 44px;
    padding: 8px 12px;
    border-radius: 10px;
    background: transparent;
    color: var(--ink);
    font: 400 16px/1.9 var(--font-ui);
    text-align: start;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  select:not([multiple]):not([size]) option:checked {
    background: color-mix(in srgb, var(--gold) 16%, var(--surface));
    color: var(--ink);
    font-weight: 600;
  }
  select:not([multiple]):not([size]) option:checked:disabled {
    background: transparent;
    font-weight: 400;
  }
  select:not([multiple]):not([size]) option:hover,
  select:not([multiple]):not([size]) option:focus {
    background: color-mix(in srgb, var(--accent) 12%, var(--surface));
    color: var(--ink);
  }
  select:not([multiple]):not([size]) option:disabled {
    color: var(--ink-3);
    opacity: .6;
  }
  select:not([multiple]):not([size]) option:disabled:hover {
    background: transparent;
  }
  select:not([multiple]):not([size]) option::checkmark {
    color: var(--accent);
    font-size: 1.05em;
  }
  select:not([multiple]):not([size]):open {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 14%, transparent);
  }
}

/* The shared JS listbox used on every engine, including browsers without
   customizable select. It is appended to body so popups never clip it. */
.ui-select-menu {
  position: fixed;
  z-index: 1000;
  box-sizing: border-box;
  padding: 6px;
  border: 1px solid var(--rule-strong);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 18px 44px -18px color-mix(in srgb, var(--ink) 42%, transparent);
  overflow-y: auto;
  overscroll-behavior: contain;
  font: 400 16px/1.8 var(--font-ui);
  /* Radix modal dialogs disable pointer events on the body; the portal menu
     lives outside the dialog, so it must opt back in to receive clicks. */
  pointer-events: auto;
  outline: none;
}
.ui-select-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  color: var(--ink);
  cursor: pointer;
  text-align: start;
  white-space: normal;
  overflow-wrap: anywhere;
}
.ui-select-option.is-active {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
}
.ui-select-option[aria-selected='true'] {
  background: color-mix(in srgb, var(--gold) 16%, var(--surface));
  font-weight: 600;
}
.ui-select-option[aria-selected='true']::after {
  content: '✓';
  margin-inline-start: auto;
  color: var(--accent);
  font-size: 1.05em;
}
.ui-select-option[aria-disabled='true'] {
  color: var(--ink-3);
  opacity: .6;
  cursor: default;
}
.ui-select-option[aria-disabled='true'].is-active {
  background: transparent;
}
@media (forced-colors: active) {
  .ui-select-menu {
    border: 1px solid CanvasText;
    box-shadow: none;
    background: Canvas;
    color: CanvasText;
  }
  .ui-select-option.is-active {
    background: Highlight;
    color: HighlightText;
  }
  .ui-select-option[aria-selected='true']::after {
    color: HighlightText;
  }
}
@media (forced-colors: active) {
  select:not([multiple]):not([size]) {
    appearance: auto !important;
    -webkit-appearance: auto !important;
    background-image: none !important;
  }
}
