:root {
  --bg0: #eef6f3;
  --bg1: #f7fbf9;
  --ink: #14323a;
  --muted: #5b737a;
  --brand: #0f766e;
  --brand-deep: #0b4f4a;
  --accent: #f59e0b;
  --accent-soft: #fff7e6;
  --card: rgba(255, 255, 255, 0.92);
  --line: rgba(20, 50, 58, 0.1);
  --ok: #059669;
  --bad: #dc2626;
  --shadow: 0 12px 32px rgba(15, 70, 66, 0.12);
  --radius: 18px;
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  --display: "Aref Ruqaa", Georgia, serif;
  --topbar-h: 64px;
  --bottom-nav-h: 68px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --touch: 48px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 480px at 100% -10%, rgba(45, 212, 191, 0.2), transparent 55%),
    radial-gradient(700px 420px at -10% 15%, rgba(245, 158, 11, 0.14), transparent 50%),
    linear-gradient(180deg, var(--bg1), var(--bg0));
  min-height: 100dvh;
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(15, 118, 110, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(15, 118, 110, 0.03) 1px, transparent 1px);
  background-size: 24px 24px;
  mask-image: radial-gradient(circle at center, black, transparent 85%);
  z-index: 0;
}

a { color: inherit; text-decoration: none; }
button, input, textarea { font: inherit; }
img, svg, iframe { max-width: 100%; }

.app {
  position: relative;
  z-index: 1;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 8px);
}

/* ===== Top bar (compact on phone) ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: calc(0.55rem + var(--safe-top)) 0.85rem 0.55rem;
  backdrop-filter: blur(14px);
  background: rgba(255,255,255,0.88);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  cursor: pointer;
  min-height: var(--touch);
}
.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  color: white;
  font-family: var(--display);
  font-size: 1.25rem;
  background: linear-gradient(145deg, var(--brand), var(--brand-deep));
  box-shadow: 0 8px 18px rgba(15, 118, 110, 0.3);
  flex-shrink: 0;
}
.brand strong { display: block; font-size: 1.05rem; line-height: 1.2; }
.brand-sub { color: var(--muted); font-size: 0.78rem; display: block; }

.nav-desktop { display: none; }

/* ===== Bottom nav — phone/tablet primary ===== */
.bottom-nav {
  position: fixed;
  bottom: 0;
  inset-inline: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0;
  padding: 0.35rem 0.2rem calc(0.35rem + var(--safe-bottom));
  background: rgba(255,255,255,0.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(16px);
  box-shadow: 0 -8px 28px rgba(15, 70, 66, 0.08);
}
.bottom-nav button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  min-height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  font-size: 0.68rem;
  font-weight: 600;
  cursor: pointer;
  border-radius: 12px;
  padding: 0.25rem 0.1rem;
}
.bottom-nav button .bn-ico {
  font-size: 1.05rem;
  line-height: 1;
}
.bottom-nav button.active,
.bottom-nav button:active {
  color: var(--brand-deep);
  background: rgba(15, 118, 110, 0.1);
}

.main {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 0.85rem 0.85rem 1.5rem;
}
.view { display: none; animation: rise 0.35s ease; }
.view.active { display: block; }

/* ===== Hero mobile-first ===== */
.hero {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.85rem;
  margin-bottom: 0.9rem;
}
.hero-copy, .hero-panel, .card, .subject-card, .lesson-card, .unit-block, .focus-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.hero-copy {
  padding: 1.15rem 1.05rem 1.25rem;
  position: relative;
  overflow: hidden;
  background:
    linear-gradient(160deg, rgba(255,255,255,0.95), rgba(232, 250, 246, 0.9)),
    var(--card);
}
.eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: var(--brand-deep);
  font-weight: 600;
  font-size: 0.82rem;
  margin-bottom: 0.55rem;
}
.hero h1 {
  font-family: var(--display);
  font-size: clamp(2.6rem, 14vw, 3.6rem);
  line-height: 0.95;
  margin: 0 0 0.65rem;
  color: var(--brand-deep);
}
.hero p {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.75;
}
.cta-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.55rem;
}
.btn {
  border: none;
  border-radius: 14px;
  padding: 0.9rem 1rem;
  min-height: var(--touch);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  font-weight: 600;
  width: 100%;
  text-align: center;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: linear-gradient(135deg, var(--brand), var(--brand-deep));
  color: white;
  box-shadow: 0 10px 22px rgba(15, 118, 110, 0.25);
}
.btn-secondary {
  background: white;
  color: var(--brand-deep);
  border: 1px solid rgba(15,118,110,0.2);
}
.btn-accent {
  background: var(--accent);
  color: #1f1300;
  font-weight: 700;
}
.btn-ghost {
  background: transparent;
  border: 1px dashed rgba(15,118,110,0.35);
  color: var(--brand-deep);
  width: auto;
  padding-inline: 0.9rem;
}

.hero-panel {
  padding: 1rem;
  display: grid;
  gap: 0.7rem;
  background: linear-gradient(180deg, rgba(15,118,110,0.94), rgba(11,79,74,0.97));
  color: #eafffa;
  border: none;
}
.hero-panel h2 {
  font-family: var(--display);
  font-size: 1.55rem;
  margin: 0.1rem 0 0.25rem;
}
.hero-panel p { margin: 0; opacity: 0.92; line-height: 1.65; font-size: 0.92rem; }
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.55rem;
  margin-top: 0.2rem;
}
.stat {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 14px;
  padding: 0.75rem;
}
.stat b { display: block; font-size: 1.25rem; margin-bottom: 0.15rem; }
.stat span { font-size: 0.78rem; opacity: 0.88; }

.section-head {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.65rem;
  margin: 0.4rem 0 0.85rem;
}
.section-head h2 { margin: 0; font-size: 1.25rem; }
.section-head p { margin: 0.2rem 0 0; color: var(--muted); font-size: 0.92rem; }

.subjects-grid, .cards-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.7rem;
}
.subject-card, .lesson-card {
  padding: 0.95rem;
  cursor: pointer;
  text-align: right;
  width: 100%;
  appearance: none;
  min-height: var(--touch);
}
.subject-card:active, .lesson-card:active { transform: scale(0.99); }
.subject-card .icon {
  width: 44px;
  height: 44px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  font-size: 1.2rem;
  margin-bottom: 0.65rem;
  color: white;
}
.subject-card h3, .lesson-card h3 { margin: 0 0 0.3rem; font-size: 1.02rem; }
.subject-card p, .lesson-card p { margin: 0; color: var(--muted); font-size: 0.88rem; line-height: 1.55; }
.badge {
  display: inline-flex;
  margin-top: 0.55rem;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  background: rgba(15, 118, 110, 0.12);
  color: var(--brand-deep);
  font-size: 0.74rem;
  font-weight: 700;
}
.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: rgba(15,118,110,0.12);
  overflow: hidden;
  margin-top: 0.7rem;
}
.progress-bar > span {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--brand), #34d399);
  border-radius: inherit;
  width: 0;
  transition: width 0.4s ease;
}

.card { padding: 1rem; }
.muted { color: var(--muted); }
.unit-block { padding: 0.9rem; margin-bottom: 0.85rem; }
.unit-block h3 {
  margin: 0 0 0.7rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--line);
  font-size: 1.05rem;
}
.lessons-list { display: grid; gap: 0.55rem; }

/* ===== Lesson mobile stack ===== */
.lesson-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.85rem;
}
.lesson-main h1 {
  margin: 0 0 0.4rem;
  font-size: 1.3rem;
  line-height: 1.35;
}
.meta-row {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.4rem;
  margin-bottom: 0.85rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 0.15rem;
}
.meta-row::-webkit-scrollbar { display: none; }
.chip {
  background: rgba(15,118,110,0.1);
  color: var(--brand-deep);
  border-radius: 999px;
  padding: 0.4rem 0.7rem;
  font-size: 0.78rem;
  white-space: nowrap;
  flex-shrink: 0;
  border: 0;
  cursor: pointer;
}
.explain {
  white-space: pre-wrap;
  line-height: 1.85;
  font-size: 0.98rem;
}
.key-points {
  margin: 0.85rem 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.5rem;
}
.key-points li {
  padding: 0.75rem 0.8rem;
  border-radius: 14px;
  background: rgba(15,118,110,0.06);
  border: 1px solid rgba(15,118,110,0.08);
  font-size: 0.92rem;
  line-height: 1.6;
}
.tools {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  margin: 0.85rem 0;
  position: sticky;
  top: calc(var(--topbar-h) + var(--safe-top));
  z-index: 15;
  background: rgba(247, 251, 249, 0.92);
  backdrop-filter: blur(10px);
  padding: 0.45rem 0;
}
.tools .btn { width: 100%; }

.side-stack { display: grid; gap: 0.85rem; align-content: start; }
.video-frame {
  position: relative;
  width: 100%;
  padding-top: 56.25%;
  border-radius: 14px;
  overflow: hidden;
  background: #0b4f4a;
}
.video-frame iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

.tutor { display: grid; gap: 0.65rem; }
.tutor-log {
  height: 220px;
  overflow: auto;
  display: grid;
  gap: 0.5rem;
  padding: 0.4rem;
  background: rgba(255,255,255,0.7);
  border-radius: 14px;
  border: 1px solid var(--line);
  -webkit-overflow-scrolling: touch;
}
.bubble {
  padding: 0.7rem 0.8rem;
  border-radius: 14px;
  line-height: 1.65;
  font-size: 0.92rem;
}
.bubble.ai {
  background: rgba(15,118,110,0.1);
  border-top-right-radius: 4px;
}
.bubble.me {
  background: rgba(245,158,11,0.15);
  border-top-left-radius: 4px;
}
.tutor-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.45rem;
  align-items: stretch;
}
.tutor-form input {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.8rem 0.85rem;
  background: white;
  min-height: var(--touch);
  font-size: 16px; /* يمنع زوم iOS عند التركيز */
}
.tutor-form .btn {
  width: auto;
  min-width: 72px;
  padding-inline: 0.9rem;
}
.quick-asks {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.4rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.quick-asks::-webkit-scrollbar { display: none; }
.quick-asks button {
  border: 1px solid var(--line);
  background: white;
  border-radius: 999px;
  padding: 0.55rem 0.8rem;
  min-height: 40px;
  cursor: pointer;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.quiz-box .q {
  font-weight: 700;
  margin-bottom: 0.75rem;
  line-height: 1.65;
  font-size: 0.98rem;
}
.options { display: grid; gap: 0.5rem; }
.option {
  text-align: right;
  border: 1px solid var(--line);
  background: white;
  border-radius: 14px;
  padding: 0.9rem 0.85rem;
  min-height: var(--touch);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1.5;
}
.option.correct {
  border-color: var(--ok);
  background: rgba(5,150,105,0.1);
}
.option.wrong {
  border-color: var(--bad);
  background: rgba(220,38,38,0.08);
}
.quiz-feedback {
  margin-top: 0.75rem;
  padding: 0.8rem;
  border-radius: 14px;
  background: var(--accent-soft);
  line-height: 1.65;
  font-size: 0.92rem;
}
.score-pill {
  display: inline-flex;
  gap: 0.35rem;
  align-items: center;
  background: rgba(15,118,110,0.1);
  color: var(--brand-deep);
  padding: 0.4rem 0.7rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.82rem;
}

.home-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.85rem;
  margin-top: 0.85rem;
}
.focus-card { padding: 1rem; }
.focus-card h3 { margin-top: 0; font-size: 1.05rem; }
.footer {
  text-align: center;
  color: var(--muted);
  padding: 0.4rem 1rem 0.2rem;
  font-size: 0.8rem;
}
.empty {
  padding: 1.5rem;
  text-align: center;
  color: var(--muted);
}

.diagrams-block { margin-top: 1rem; }
.diagram-frame {
  margin-top: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: auto;
  background: #fff;
  -webkit-overflow-scrolling: touch;
}
.diagram-frame svg {
  display: block;
  width: 100%;
  min-width: 0;
  height: auto;
}
.video-pick.active {
  background: rgba(15, 118, 110, 0.18);
  font-weight: 700;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Large phones / small tablets ===== */
@media (min-width: 480px) {
  .cta-row { grid-template-columns: 1fr 1fr; }
  .tools { grid-template-columns: 1fr 1fr; }
  .tools #btn-mark-lesson { grid-column: 1 / -1; }
  .subjects-grid, .cards-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ===== iPad / tablet ===== */
@media (min-width: 768px) {
  .main { padding: 1.1rem 1.2rem 1.8rem; }
  .hero h1 { font-size: 4rem; }
  .hero p { font-size: 1.02rem; max-width: 42ch; }
  .subjects-grid { grid-template-columns: repeat(3, 1fr); }
  .cards-grid { grid-template-columns: repeat(2, 1fr); }
  .tools {
    grid-template-columns: repeat(3, 1fr);
    position: static;
    background: transparent;
    backdrop-filter: none;
    padding: 0;
  }
  .tools #btn-mark-lesson { grid-column: auto; }
  .btn { width: auto; }
  .cta-row { display: flex; flex-wrap: wrap; }
  .cta-row .btn { width: auto; min-width: 160px; }
  .lesson-main h1 { font-size: 1.55rem; }
  .tutor-log { height: 260px; }
  .bottom-nav button { font-size: 0.74rem; }
  .bottom-nav button .bn-ico { font-size: 1.15rem; }
}

/* ===== iPad landscape / small laptop ===== */
@media (min-width: 900px) {
  .hero {
    grid-template-columns: 1.15fr 0.85fr;
    min-height: min(58vh, 520px);
    align-items: stretch;
  }
  .hero-copy {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 1.8rem;
  }
  .home-actions { grid-template-columns: 1.2fr 0.8fr; }
  .lesson-layout { grid-template-columns: 1.35fr 0.9fr; }
  .cards-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ===== Desktop: top nav, hide bottom nav ===== */
@media (min-width: 1024px) {
  .app { padding-bottom: 0; }
  .bottom-nav { display: none; }
  .nav-desktop {
    display: flex;
    gap: 0.3rem;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .nav-desktop button {
    border: 1px solid transparent;
    background: transparent;
    color: var(--muted);
    padding: 0.55rem 0.85rem;
    min-height: 42px;
    border-radius: 999px;
    cursor: pointer;
    transition: 0.2s ease;
  }
  .nav-desktop button:hover,
  .nav-desktop button.active {
    color: var(--brand-deep);
    background: rgba(15, 118, 110, 0.1);
    border-color: rgba(15, 118, 110, 0.15);
  }
  .topbar { padding: 0.85rem 1.2rem; }
  .main { padding: 1.3rem 1.2rem 2.4rem; }
  .subjects-grid { grid-template-columns: repeat(4, 1fr); }
  .footer { padding: 1rem; font-size: 0.9rem; }
}

/* Very small phones */
@media (max-width: 360px) {
  .bottom-nav button span:not(.bn-ico) { font-size: 0.62rem; }
  .brand-sub { display: none; }
  .hero h1 { font-size: 2.35rem; }
}
