:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #21262d;
  --border: #30363d;
  --accent: #58a6ff;
  --accent2: #1f6feb;
  --text: #c9d1d9;
  --muted: #8b949e;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --purple: #bc8cff;
  --tip: #1f3a5f;
  --warn: #3d2e00;
  --q: #2d1f5f;
  --sidebar: 280px;
}

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

body {
  font-family: 'Segoe UI', 'Noto Sans Arabic', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.75;
  display: flex;
  min-height: 100vh;
}

/* ───── DIRECTION FIX ─────
   Body direction is set by JS per language.
   All text elements use dir="auto" so each paragraph
   detects its own direction from the first character.
   Code blocks are ALWAYS ltr regardless.
*/
p, li, h1, h2, h3, h4, h5, td, th, .callout-text {
  unicode-bidi: plaintext;
}
pre, code, .file-path, .diagram {
  direction: ltr !important;
  text-align: left !important;
  unicode-bidi: normal !important;
}

/* ───── PROGRESS BAR ───── */
#progress-bar-wrap {
  position: fixed; top: 0; left: 0;
  width: 100%; height: 3px; z-index: 9999;
}
#progress-bar {
  height: 100%; width: 0%;
  background: var(--accent);
  transition: width 0.1s;
}

/* ───── LANG TOGGLE ───── */
#lang-btn {
  position: fixed; top: 14px; inset-inline-end: 16px; z-index: 1100;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 6px 16px; border-radius: 20px;
  cursor: pointer; font-weight: 600; font-size: .9rem;
  transition: all .2s;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
#lang-btn:hover { border-color: var(--accent); transform: scale(1.05); }

/* ───── SIDEBAR ───── */
#sidebar {
  width: var(--sidebar);
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  height: 100vh; position: sticky; top: 0;
  overflow-y: auto; padding: 20px 0;
  flex-shrink: 0;
}
#sidebar .course-title {
  font-size: .8rem; color: var(--muted);
  padding: 0 16px 12px; text-transform: uppercase;
  letter-spacing: .08em; border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
#sidebar a {
  display: block; padding: 8px 16px;
  color: var(--muted); text-decoration: none;
  font-size: .88rem; border-inline-start: 3px solid transparent;
  transition: all .15s;
}
#sidebar a:hover { color: var(--text); background: var(--surface2); }
#sidebar a.active {
  color: var(--accent);
  border-inline-start-color: var(--accent);
  background: var(--surface2);
}
#sidebar a.chapter-link {
  font-size: .78rem; padding-inline-start: 28px;
  color: var(--muted);
}
#sidebar .chapter-nav-title {
  font-size: .75rem; color: var(--muted);
  padding: 12px 16px 4px; text-transform: uppercase;
  letter-spacing: .06em;
}

/* ───── BRANDING ───── */
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.brand-logo {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: contain;
  flex-shrink: 0;
}
.brand-name {
  font-weight: 700;
  font-size: .95rem;
  color: var(--text);
  white-space: nowrap;
}

.hero-logo {
  width: 96px;
  height: 96px;
  border-radius: 20px;
  object-fit: contain;
  display: block;
  margin: 0 auto 16px;
  box-shadow: 0 8px 32px rgba(88,166,255,0.2);
}

/* ───── MAIN ───── */
main {
  flex: 1; 
  padding: 56px 48px 80px;
  max-width: 1000px;
  width: 100%;
  overflow-x: hidden;
}

/* ───── CHAPTER HEADER ───── */
.chapter-hero {
  margin-bottom: 48px; padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}
.chapter-num {
  font-size: .8rem; color: var(--accent);
  text-transform: uppercase; letter-spacing: .1em;
}
.chapter-hero h1 { font-size: 2rem; margin: 8px 0 12px; }
.chapter-hero .desc { color: var(--muted); font-size: 1.05rem; }

/* ───── SECTIONS ───── */
.lesson {
  margin-bottom: 56px;
  padding-bottom: 40px;
  border-bottom: 1px solid var(--border);
}
.lesson:last-child { border-bottom: none; }
.lesson-title {
  font-size: 1.3rem; font-weight: 700;
  margin-bottom: 16px; color: var(--text);
  display: flex; align-items: center; gap: 10px;
}

/* ───── CODE BLOCKS ───── */
.code-wrap { margin: 16px 0; position: relative; }
.file-path {
  font-size: .78rem; color: var(--muted);
  background: var(--surface2);
  padding: 4px 12px; border-radius: 6px 6px 0 0;
  border: 1px solid var(--border);
  border-bottom: none; font-family: monospace;
}
.code-wrap pre {
  margin: 0; border-radius: 0 0 6px 6px;
  border: 1px solid var(--border);
}
.copy-btn {
  position: absolute; top: 30px; right: 8px;
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--muted); padding: 3px 10px; border-radius: 4px;
  font-size: .75rem; cursor: pointer; transition: .15s;
}
.copy-btn:hover { color: var(--text); }

/* ───── LINE WALKTHROUGH ───── */
.walkthrough { margin: 20px 0; }
.wt-block {
  border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
  margin-bottom: 12px;
}
.wt-code {
  background: var(--surface2); padding: 10px 16px;
  font-family: monospace; font-size: .9rem; color: var(--purple);
  direction: ltr; text-align: left;
  border-bottom: 1px solid var(--border);
}
.wt-body { padding: 14px 18px; }
.wt-row { margin-bottom: 12px; }
.wt-label {
  font-size: .78rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em;
  margin-bottom: 4px;
}
.wt-label.what  { color: var(--accent); }
.wt-label.why   { color: var(--green); }
.wt-label.break { color: var(--red); }
.wt-label.conn  { color: var(--yellow); }

/* ───── DATA FLOW ───── */
.flow-diagram {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 20px 24px;
  font-family: monospace; font-size: .88rem;
  line-height: 2; white-space: pre;
  overflow-x: auto; margin: 16px 0;
  direction: ltr; text-align: left;
}

/* ───── CALLOUTS ───── */
.callout {
  border-radius: 8px; padding: 14px 18px;
  margin: 16px 0; display: flex; gap: 12px;
  align-items: flex-start;
}
.callout-icon { font-size: 1.2rem; flex-shrink: 0; margin-top: 2px; }
.callout.tip  { background: var(--tip);  border-inline-start: 3px solid var(--accent); }
.callout.warn { background: var(--warn); border-inline-start: 3px solid var(--yellow); }
.callout.why  { background: var(--q);   border-inline-start: 3px solid var(--purple); }
.callout.conn { background: var(--surface2); border-inline-start: 3px solid var(--green); }

/* ───── COMPARISON TABLE ───── */
.comp-table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: .9rem; }
.comp-table th {
  background: var(--surface2); padding: 10px 14px;
  border: 1px solid var(--border); color: var(--accent); text-align: center;
}
.comp-table td { padding: 9px 14px; border: 1px solid var(--border); }
.comp-table tr:nth-child(even) td { background: #ffffff05; }
.chosen-col { color: var(--green) !important; font-weight: 600; }

/* ───── DB SCHEMA ───── */
.schema-table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: .88rem; }
.schema-table th {
  background: var(--surface2); padding: 8px 14px;
  border: 1px solid var(--border); color: var(--yellow);
}
.schema-table td { padding: 8px 14px; border: 1px solid var(--border); font-family: monospace; }
.schema-table .pk  { color: var(--yellow); }
.schema-table .fk  { color: var(--purple); }
.schema-table .req { color: var(--red); }

/* ───── BOTTOM NAV ───── */
.chapter-nav {
  display: flex; justify-content: space-between;
  margin-top: 60px; padding-top: 24px;
  border-top: 1px solid var(--border);
}
.chapter-nav a {
  display: flex; align-items: center; gap: 8px;
  color: var(--accent); text-decoration: none;
  font-size: .95rem; padding: 10px 18px;
  border: 1px solid var(--border); border-radius: 8px;
  transition: border-color .2s;
}
.chapter-nav a:hover { border-color: var(--accent); }

/* ───── BACK TO TOP ───── */
#btt {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--muted); width: 40px; height: 40px;
  border-radius: 50%; cursor: pointer; display: none;
  align-items: center; justify-content: center; font-size: 1.1rem;
  transition: .2s;
}
#btt:hover { color: var(--text); border-color: var(--accent); }

/* ── RESPONSIVE ─────────────────────────────── */

/* Tablet (≤ 900px): sidebar becomes top bar */
@media (max-width: 900px) {
  body { flex-direction: column; }

  #sidebar {
    width: 100%;
    height: auto;
    position: sticky;
    top: 0;
    border-inline-end: none;
    border-bottom: 1px solid var(--border);
    padding: 0;
    z-index: 1000;
    background: var(--surface);
    display: flex;
    flex-direction: column;
  }

  .sidebar-brand {
    padding: 10px 16px 8px;
  }

  /* Nav links become horizontal scrollable row */
  .sidebar-links {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    padding: 4px 8px 8px;
    gap: 4px;
    scrollbar-width: none;
  }
  .sidebar-links::-webkit-scrollbar { display: none; }

  #sidebar a {
    white-space: nowrap;
    padding: 10px 16px;
    border-inline-start: none;
    border-bottom: 3px solid transparent;
    border-radius: 0;
    font-size: .85rem;
    flex-shrink: 0;
    color: var(--muted);
  }
  #sidebar a.active {
    border-bottom-color: var(--accent);
    color: var(--accent);
    background: var(--surface2);
  }

  /* Hide sub-section links on mobile sidebar (too many) */
  #sidebar a.chapter-link { display: none; }
  .chapter-nav-title { display: none; }

  main {
    padding: 24px 20px 60px;
    max-width: 100%;
  }

  .chapter-hero h1 { font-size: 1.5rem; }

  /* Chapter nav bottom buttons: stack on small */
  .chapter-nav {
    flex-direction: column;
    gap: 10px;
  }
  .chapter-nav a { justify-content: center; }
}

/* Mobile (≤ 600px) */
@media (max-width: 600px) {
  .brand-name { display: none; } /* logo only, no text */

  main { padding: 20px 14px 60px; }

  .chapter-hero h1 { font-size: 1.25rem; }
  .chapter-hero .desc { font-size: .9rem; }

  .lesson-title { font-size: 1.1rem; }

  /* Code blocks: horizontal scroll */
  .code-wrap pre {
    font-size: .78rem;
    overflow-x: auto;
  }

  /* Walkthrough blocks: tighter */
  .wt-code { font-size: .8rem; }
  .wt-body { padding: 10px 12px; }

  /* Flow diagrams: scroll */
  .flow-diagram {
    font-size: .75rem;
    overflow-x: auto;
  }

  /* Comparison tables: scroll */
  .comp-table, .schema-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Callouts: tighter */
  .callout { padding: 10px 12px; }

  /* Lang toggle: smarter position */
  #lang-btn {
    top: 10px; inset-inline-end: 10px;
    padding: 5px 12px; font-size: .8rem;
    z-index: 1101;
  }

  /* Progress bar: slightly thicker for visibility */
  #progress-bar-wrap { height: 4px; }

  #sidebar .course-title { display: none; }

  /* Back to top: lower position */
  #btt { bottom: 16px; right: 16px; }
}

/* Large screens (≥ 1400px): wider content */
@media (min-width: 1400px) {
  main { max-width: 1000px; }
  #sidebar { width: 300px; }
}

/* ── STEPS ─────────────────────────────────────── */
.steps-container { margin: 24px 0; }
.step-item {
  display: flex;
  gap: 16px;
  margin-bottom: 32px;
  position: relative;
}
.step-item::before {
  content: '';
  position: absolute;
  inset-inline-start: 19px;
  top: 40px;
  bottom: -18px;
  width: 2px;
  background: var(--border);
}
.step-item:last-child::before { display: none; }
.step-num {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent2);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: .95rem;
  flex-shrink: 0;
  z-index: 1;
  box-shadow: 0 0 10px rgba(88,166,255,0.3);
}
.step-body { flex: 1; padding-top: 8px; }
.step-title {
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 8px;
  color: var(--text);
}
.step-desc { margin-bottom: 12px; font-size: 0.95rem; color: var(--muted); }

/* ── CROSS-REFERENCE ─────────────────────────── */
.xref-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px dashed var(--border);
  color: var(--accent);
  padding: 10px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: .88rem;
  margin: 12px 0;
  transition: all .2s;
  width: 100%;
  text-align: inherit;
  font-family: inherit;
  justify-content: space-between;
}
.xref-btn:hover {
  border-color: var(--accent);
  background: var(--surface2);
  color: var(--text);
}
.xref-btn.open {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-style: solid;
  border-color: var(--accent);
  background: var(--surface2);
}
.xref-btn.open .xref-arrow { transform: rotate(180deg); }
.xref-arrow { transition: transform .2s; font-size: .7rem; opacity: 0.7; }
.xref-content {
  border: 1px solid var(--accent);
  border-top: none;
  border-radius: 0 0 8px 8px;
  padding: 20px;
  background: var(--surface2);
  margin-top: -1px;
  margin-bottom: 16px;
  animation: slideDown .2s ease-out;
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.xref-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  color: var(--accent);
  text-decoration: none;
  padding: 8px 16px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  font-size: .9rem;
  font-weight: 600;
  transition: all .2s;
}
.xref-link:hover {
  background: var(--accent2);
  color: #fff;
  border-color: var(--accent2);
}

/* ── TERMINAL ─────────────────────────────────── */
.terminal-wrap {
  margin: 20px 0;
  position: relative;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.terminal-label {
  background: #1a1a2e;
  border: 1px solid var(--border);
  border-bottom: none;
  padding: 6px 16px;
  font-size: .75rem;
  color: var(--green);
  border-radius: 8px 8px 0 0;
  font-family: 'Fira Code', 'Courier New', monospace;
  display: flex;
  align-items: center;
  gap: 8px;
}
.terminal-label::before {
  content: ''; width: 10px; height: 10px; border-radius: 50%;
  background: #ff5f56; box-shadow: 15px 0 0 #ffbd2e, 30px 0 0 #27c93f;
  margin-right: 35px;
}
.terminal-block {
  background: #0d0d1a !important;
  border: 1px solid var(--border) !important;
  border-radius: 0 0 8px 8px !important;
  margin: 0 !important;
  direction: ltr !important;
  padding: 16px !important;
}
.terminal-block code {
  color: var(--green) !important;
  font-family: 'Fira Code', 'Courier New', monospace !important;
  font-size: 0.9rem !important;
}

/* Ensure direction for steps */
[dir="rtl"] .step-item::before { left: auto; right: 19px; }
[dir="rtl"] .step-num { margin-left: 0; }
[dir="rtl"] .xref-arrow { margin-left: 0; margin-right: auto; }

/* ── ADDITIONAL UX FIXES ── */
.lesson { position: relative; clear: both; }
.wt-block, .code-wrap, .callout { width: 100%; }
