*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

/* ══ Theme Tokens ══ */
:root {
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --font-sans: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'Geist Mono', 'SF Mono', monospace;
  --font-ar: 'IBM Plex Sans Arabic', 'Outfit', sans-serif;
  --transition-fast: 120ms ease;
  --transition-base: 200ms ease;
  --accent: #3b82f6;
  --accent-dim: rgba(59, 130, 246, 0.12);
  --accent-hover: #60a5fa;
  --accent-border: rgba(59, 130, 246, 0.25);
}

[data-theme="dark"] {
  --bg-primary: #0a0a0c;
  --bg-secondary: #111114;
  --bg-tertiary: #18181c;
  --bg-hover: #1e1e24;
  --bg-active: #252530;
  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-medium: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text-primary: #ededef;
  --text-secondary: #9898a0;
  --text-tertiary: #5e5e68;
  --shadow-dropdown: 0 12px 40px rgba(0, 0, 0, 0.5);
  --header-bg: rgba(10, 10, 12, 0.82);
  --paper: #f5f3ec;
  --paper-ink: #1c1b17;
}

[data-theme="light"] {
  --bg-primary: #f8f8fa;
  --bg-secondary: #ffffff;
  --bg-tertiary: #f0f0f4;
  --bg-hover: #e8e8ee;
  --bg-active: #dcdce4;
  --border-subtle: rgba(0, 0, 0, 0.06);
  --border-medium: rgba(0, 0, 0, 0.1);
  --border-strong: rgba(0, 0, 0, 0.16);
  --text-primary: #111118;
  --text-secondary: #555560;
  --text-tertiary: #8888a0;
  --shadow-dropdown: 0 12px 40px rgba(0, 0, 0, 0.12);
  --header-bg: rgba(248, 248, 250, 0.82);
  --paper: #ffffff;
  --paper-ink: #1c1b17;
}

/* ══ RTL base ══ */
[dir="rtl"] { font-family: var(--font-ar); }
[dir="rtl"] .font-mono { font-family: var(--font-mono); direction: ltr; }

html { scroll-behavior: smooth; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100dvh;
  transition: background var(--transition-base), color var(--transition-base);
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-medium); border-radius: 99px; }
html {
  scrollbar-width: thin;
  scrollbar-color: var(--border-medium) transparent;
}

.page-container { max-width: 1120px; margin: 0 auto; padding: 0 24px; }

/* ══ Header ══ */
.site-header {
  position: sticky; top: 0; z-index: 100;
  background: var(--header-bg);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border-bottom: 1px solid var(--border-subtle);
  height: 64px; display: flex; align-items: center;
  transition: background var(--transition-base);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 24px;
}
.site-title { font-weight: 600; font-size: 15px; letter-spacing: -0.3px; color: var(--text-primary); }
.site-title span { color: var(--text-tertiary); font-weight: 400; }

.header-controls { display: flex; align-items: center; gap: 6px; }

.header-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; background: var(--bg-secondary); border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm); color: var(--text-secondary); font-family: var(--font-sans);
  font-size: 13px; cursor: pointer; transition: all var(--transition-fast); white-space: nowrap;
  text-decoration: none;
}
.header-btn:hover { border-color: var(--border-medium); color: var(--text-primary); background: var(--bg-tertiary); }
.header-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }
.header-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.header-btn.icon-only { padding: 0; width: 34px; }

.lang-selector { position: relative; }
.lang-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; min-width: 160px;
  background: var(--bg-tertiary); border: 1px solid var(--border-medium); border-radius: var(--radius-md);
  box-shadow: var(--shadow-dropdown); padding: 4px; z-index: 60; display: none;
}
[dir="rtl"] .lang-dropdown { right: auto; left: 0; }
.lang-dropdown.open { display: block; }
.lang-option {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: var(--radius-sm);
  font-size: 13px; color: var(--text-secondary); cursor: pointer; border: none;
  background: none; width: 100%; text-align: left; font-family: var(--font-sans);
  transition: all var(--transition-fast);
}
[dir="rtl"] .lang-option { text-align: right; font-family: var(--font-ar); }
.lang-option:hover { background: var(--bg-hover); color: var(--text-primary); }
.lang-option.active { background: var(--accent-dim); color: var(--accent); }

/* ══ Footer ══ */
.site-footer { padding: 28px 0; border-top: 1px solid var(--border-subtle); margin-top: 40px; }
.footer-inner { display: flex; align-items: center; justify-content: space-between; font-size: 13px; color: var(--text-tertiary); }

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

@media (max-width: 768px) {
  .header-btn .btn-label { display: none; }
  .footer-inner { flex-direction: column; gap: 8px; text-align: center; }
}

/* ══ Shared Content Viewer Modal ══ */
.pdf-viewer-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  display: flex; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none; transition: opacity 0.25s ease;
}
.pdf-viewer-overlay.open { opacity: 1; pointer-events: auto; }
.pdf-viewer-modal {
  width: 94vw; height: 92vh; max-width: 1400px;
  background: var(--bg-secondary); border: 1px solid var(--border-medium); border-radius: var(--radius-lg);
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
  transform: scale(0.96) translateY(10px);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.pdf-viewer-overlay.open .pdf-viewer-modal { transform: scale(1) translateY(0); }
.pdf-viewer-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; gap: 12px; min-height: 52px;
  border-bottom: 1px solid var(--border-subtle); background: var(--bg-tertiary); flex-shrink: 0;
}
.pdf-viewer-title {
  font-size: 13px; font-weight: 500; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  min-width: 0; flex: 1; font-family: var(--font-mono);
}
.pdf-viewer-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.pdf-viewer-actions .header-btn { text-decoration: none; }
.pdf-viewer-content { flex: 1; position: relative; overflow-y: auto; padding: 16px; background: var(--bg-primary); }
.pdf-viewer-content .pdf-page {
  margin: 0 auto 16px; max-width: 900px; background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.45); border-radius: 4px; overflow: hidden;
}
.pdf-viewer-content .pdf-page canvas { display: block; width: 100%; height: auto; }
.pdf-loading { color: var(--text-secondary); font-size: 14px; padding: 24px; text-align: center; }
.pdf-error { color: #f87171; font-size: 14px; padding: 24px; text-align: center; }

/* Lang tabs inside viewer toolbar */
.lang-tabs { display: flex; background: rgba(255,255,255,0.06); border-radius: var(--radius-md); padding: 3px; }
.lang-tabs button {
  padding: 5px 14px; border: none; border-radius: var(--radius-sm); background: transparent;
  color: var(--text-secondary); font-family: inherit; font-size: 13px; font-weight: 500; cursor: pointer;
  transition: all var(--transition-fast);
}
.lang-tabs button.active { background: var(--accent); color: #fff; }
.lang-tabs button:disabled { opacity: 0.35; cursor: not-allowed; }
[data-theme="light"] .lang-tabs { background: rgba(0,0,0,0.06); }

/* Lecture body (markdown reader inside modal) */
.lecture-body { flex: 1; overflow-y: auto; padding: 28px 24px 64px; width: 100%; max-width: 820px; margin: 0 auto; }
.lecture-body h1, .lecture-body h2, .lecture-body h3 { margin: 1.4em 0 0.6em; line-height: 1.25; color: var(--text-primary); }
.lecture-body h1:first-child, .lecture-body h2:first-child, .lecture-body h3:first-child { margin-top: 0; }
.lecture-body p { margin: 0.8em 0; color: var(--text-secondary); max-width: 72ch; }
.lecture-body ul, .lecture-body ol { margin: 0.8em 0; padding-inline-start: 1.4em; color: var(--text-secondary); }
.lecture-body li { margin: 0.3em 0; }
.lecture-body strong { color: var(--text-primary); }
.lecture-body code { font-family: var(--font-mono); font-size: 0.9em; background: var(--bg-tertiary); padding: 2px 6px; border-radius: 5px; }
.lecture-body pre { background: var(--bg-secondary); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); padding: 14px; overflow-x: auto; margin: 1em 0; }
.lecture-body pre code { background: transparent; padding: 0; }
.lecture-body .mermaid { display: flex; justify-content: center; margin: 1.2em 0; }
.lecture-error { color: #f87171; font-size: 14px; padding: 20px; }

/* ══ Entry Animation ══ */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ══ Back to top ══ */
.back-to-top {
  position: fixed; bottom: 24px; right: 24px; width: 40px; height: 40px;
  background: var(--bg-tertiary); border: 1px solid var(--border-medium); border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-secondary);
  opacity: 0; pointer-events: none; transition: opacity var(--transition-base); z-index: 50;
}
.back-to-top.visible { opacity: 1; pointer-events: auto; }
.back-to-top:hover { background: var(--bg-hover); color: var(--text-primary); }
.back-to-top svg { width: 18px; height: 18px; }
[dir="rtl"] .back-to-top { right: auto; left: 24px; }

@media (max-width: 768px) {
  .pdf-viewer-modal { width: 100vw; height: 100dvh; max-width: none; border-radius: 0; }
  .pdf-viewer-actions .btn-label { display: none; }
  .lecture-body { padding: 20px 16px 48px; }
}
