/* ==========================================================================
   src/styles/base.css
   Design System Tokens, Color Palettes (Light, Dark, Sepia, High-Contrast),
   Global Motion System Variables, Zero-Lag GPU View Transitions & Surface
   Transitions, SVG Crisp Rendering, and Global Resets
   ========================================================================== */

:root, [data-theme="light"] {
  color-scheme: light;
  --bg-primary: #F8FAFC;
  --bg-secondary: #F1F5F9;
  --bg-surface: #FFFFFF;
  --bg-elevated: #FFFFFF;
  --bg-hover: #E2E8F0;
  --bg-active: #CBD5E1;
  --text-primary: #0F172A;
  --text-secondary: #475569;
  --text-muted: #64748B;
  --border-color: #E2E8F0;
  --border-strong: #CBD5E1;
  --accent: #D97706;
  --accent-hover: #B45309;
  --accent-soft: rgba(217, 119, 6, 0.13);
  --verse-num: #B45309;
  --hl-yellow: rgba(234, 179, 8, 0.24);
  --hl-green: rgba(34, 197, 94, 0.22);
  --hl-blue: rgba(59, 130, 246, 0.20);
  --hl-rose: rgba(244, 63, 94, 0.20);
  --focus-ring: #D97706;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg-primary: #0D1117;
  --bg-secondary: #161B26;
  --bg-surface: #161B26;
  --bg-elevated: #1C2431;
  --bg-hover: #263244;
  --bg-active: #334155;
  --text-primary: #E2E8F0;
  --text-secondary: #CBD5E1;
  --text-muted: #64748B;
  --border-color: #273449;
  --border-strong: #475569;
  --accent: #D97706;
  --accent-hover: #F59E0B;
  --accent-soft: rgba(217, 119, 6, 0.18);
  --verse-num: #FBBF24;
  --hl-yellow: rgba(234, 179, 8, 0.28);
  --hl-green: rgba(34, 197, 94, 0.26);
  --hl-blue: rgba(59, 130, 246, 0.26);
  --hl-rose: rgba(244, 63, 94, 0.26);
  --focus-ring: #D97706;
}

[data-theme="sepia"] {
  color-scheme: light;
  --bg-primary: #F5EEDC;
  --bg-secondary: #EADBC0;
  --bg-surface: #FBF8F1;
  --bg-elevated: #FBF8F1;
  --bg-hover: #DEC49A;
  --bg-active: #D6C19A;
  --text-primary: #3B2F2F;
  --text-secondary: #5C4B37;
  --text-muted: #806B55;
  --border-color: #EADBC0;
  --border-strong: #DEC49A;
  --accent: #B45309;
  --accent-hover: #92400E;
  --accent-soft: rgba(180, 83, 9, 0.13);
  --verse-num: #92400E;
  --hl-yellow: rgba(217, 155, 26, 0.28);
  --hl-green: rgba(46, 150, 78, 0.24);
  --hl-blue: rgba(54, 115, 189, 0.22);
  --hl-rose: rgba(204, 71, 94, 0.22);
  --focus-ring: #B45309;
}

[data-contrast="high"][data-theme="light"] {
  --bg-primary: #FFFFFF;
  --bg-secondary: #F0F0F0;
  --bg-surface: #FFFFFF;
  --text-primary: #000000;
  --text-secondary: #1F1F1F;
  --border-color: #888888;
  --accent: #3D2713;
}

[data-contrast="high"][data-theme="dark"] {
  --bg-primary: #000000;
  --bg-secondary: #111111;
  --bg-surface: #0A0A0A;
  --text-primary: #FFFFFF;
  --text-secondary: #E0E0E0;
  --border-color: #666666;
  --accent: #F0D098;
}

[data-contrast="high"][data-theme="sepia"] {
  --bg-primary: #FAF3E3;
  --bg-secondary: #EFE4CA;
  --bg-surface: #FFFDF7;
  --text-primary: #1F140A;
  --text-secondary: #382514;
  --border-color: #7D684E;
  --accent: #593312;
}

:root {
  --ui-font: 'IBM Plex Sans Arabic', -apple-system, BlinkMacSystemFont, sans-serif;
  --reading-font: 'Amiri', Georgia, serif;
  --reading-size: 24px;
  --reading-line-height: 2.05;
  --reading-para-gap: 14px;
  --reading-max-width: 740px;

  /* Global Motion System Tokens (Durations & Easing Curves) */
  --motion-micro: 140ms;
  --motion-normal: 210ms;
  --motion-page: 240ms;
  --motion-modal: 250ms;
  --motion-drawer: 240ms;
  --motion-theme: 360ms;
  --ease-standard: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-out-smooth: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in-out-soft: cubic-bezier(0.4, 0, 0.2, 1);
  --ease-theme: cubic-bezier(0.22, 1, 0.36, 1);
}

/* Central Motion Reduction via App Toggle OR OS System Preference */
[data-reduce-motion="true"] {
  --motion-micro: 0ms !important;
  --motion-normal: 0ms !important;
  --motion-page: 0ms !important;
  --motion-modal: 0ms !important;
  --motion-drawer: 0ms !important;
  --motion-theme: 0ms !important;
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --motion-micro: 0ms !important;
    --motion-normal: 0ms !important;
    --motion-page: 0ms !important;
    --motion-modal: 0ms !important;
    --motion-drawer: 0ms !important;
    --motion-theme: 0ms !important;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  margin: 0;
  padding: 0;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--ui-font);
  direction: rtl;
  text-align: right;
  min-height: 100vh;
  scroll-behavior: smooth;
  transition: background-color var(--motion-theme) var(--ease-theme),
              color var(--motion-theme) var(--ease-theme);
}

body {
  margin: 0;
  padding: 0;
  padding-top: 4rem; /* Compensate for fixed 4rem (64px) top header */
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--ui-font);
  direction: rtl;
  text-align: right;
  overflow-x: clip;
  min-height: 100vh;
  transition: background-color var(--motion-theme) var(--ease-theme),
              color var(--motion-theme) var(--ease-theme);
}

/* Hardware-Accelerated View Transitions API (60/120fps GPU Texture Cross-Fade):
   Blends old and new reading modes atomically on the compositor thread with zero main-thread lag. */
::view-transition-group(root) {
  animation-duration: var(--motion-theme);
  animation-timing-function: var(--ease-theme);
}

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: var(--motion-theme);
  animation-timing-function: var(--ease-theme);
  mix-blend-mode: normal;
}

/* Disable per-element CSS transitions while ViewTransition captures snapshots
   so snapshots are instantaneous and never double-transition */
html[data-vt-active="true"],
html[data-vt-active="true"] *,
html[data-vt-active="true"] *::before,
html[data-vt-active="true"] *::after {
  transition: none !important;
}

/* Never run CSS transitions on children of hidden views or closed dropdown menus */
.view-section.hidden *,
.custom-dropdown-menu.hidden * {
  transition: none !important;
  animation: none !important;
}

/* Coordinated reading-theme transition.
   The class is applied and style-flushed BEFORE data-theme changes, so the browser
   interpolates the existing surfaces instead of snapping them to the new palette. */
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) body,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) #top-header,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) #reader-subtoolbar,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) #mobile-bottom-nav,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) #desktop-sidebar,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .surface-card,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .surface-secondary,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .surface-elevated,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .scripture-text,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .verse-item,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .verse-num-badge,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .btn-subtle,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .custom-dropdown-trigger,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) .custom-dropdown-menu,
html[data-reduce-motion="false"].is-theme-switching:not([data-vt-active="true"]) #quick-typo-popover {
  transition: background-color var(--motion-theme) var(--ease-theme),
              color var(--motion-theme) var(--ease-theme),
              border-color var(--motion-theme) var(--ease-theme),
              box-shadow var(--motion-theme) var(--ease-theme),
              fill var(--motion-theme) var(--ease-theme),
              stroke var(--motion-theme) var(--ease-theme);
}

[data-reduce-motion="true"] *,
[data-reduce-motion="true"] *::before,
[data-reduce-motion="true"] *::after {
  transition-duration: 0.001ms !important;
  animation-duration: 0.001ms !important;
  animation-iteration-count: 1 !important;
  scroll-behavior: auto !important;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.001ms !important;
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   Task 2: Crisp SVG Icon Rendering System across Desktop & Mobile Sizes
   ========================================================================== */
svg {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
  overflow: visible;
  shape-rendering: geometricPrecision;
  text-rendering: geometricPrecision;
}

svg path,
svg circle,
svg rect,
svg line,
svg polyline,
svg polygon {
  vector-effect: non-scaling-stroke;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Standardized Reference Icon Size System */
.icon-xs { width: 0.875rem; height: 0.875rem; } /* 14px */
.icon-sm { width: 1rem; height: 1rem; }         /* 16px */
.icon-md { width: 1.125rem; height: 1.125rem; } /* 18px */
.icon-lg { width: 1.25rem; height: 1.25rem; }   /* 20px */
.icon-xl { width: 1.5rem; height: 1.5rem; }     /* 24px */

/* Custom Scrollbar */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: var(--bg-primary);
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 3px;
}

/* Accessible Focus Ring */
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Offline fallback for essential utility classes when Tailwind CDN is unreachable */
.hidden {
  display: none !important;
}
@media (min-width: 768px) {
  .md\:flex { display: flex !important; }
  .md\:inline-flex { display: inline-flex !important; }
  .md\:hidden { display: none !important; }
}
@media (min-width: 1024px) {
  .lg\:hidden { display: none !important; }
}
svg.w-3\.5 { width: 0.875rem; height: 0.875rem; }
svg.w-4 { width: 1rem; height: 1rem; }
svg.w-5 { width: 1.25rem; height: 1.25rem; }
