/* مختبر هجرة الأيونات – أنماط عربية RTL محسّنة للهواتف */
:root {
  --bg: #0B1220;
  --card: #131C2F;
  --text: #E8EEF9;
  --accent: #2DD4BF;
  --pos: #EF4444;
  --neg: #64748B;
  --purple: #A855F7;
  --blue: #3B82F6;
  --orange: #F97316;
  --gray-ion: #CBD5E1;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(0,0,0,0.5);
  --font: 'IBM Plex Sans Arabic', system-ui, sans-serif;
  --base: 17px;
  --line: 1.8;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="light"] {
  --bg: #F0F4FA;
  --card: #FFFFFF;
  --text: #0F172A;
  --accent: #0D9488;
  --shadow: 0 8px 32px rgba(0,0,0,0.12);
}

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

html {
  font-size: var(--base);
  line-height: var(--line);
  -webkit-text-size-adjust: 100%;
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100%;
  height: 100dvh;
  width: 100%;
  touch-action: manipulation;
  letter-spacing: 0;
}

/* ===== بوابة الدخول ===== */
.gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: opacity 0.6s ease;
}
.gate.hidden { opacity: 0; pointer-events: none; }

.gate-top {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #0B1220 0%, #131C2F 100%);
  transform: translateY(0);
  transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 2;
  padding: 1rem;
}
.gate-top.open { transform: translateY(-100%); }

.gate-greeting {
  font-size: clamp(1.5rem, 6vw, 2.3rem);
  font-weight: 700;
  text-align: center;
  margin-bottom: 0.35rem;
}
.gate-hint { font-size: 0.95rem; opacity: 0.75; text-align: center; }

.gate-bottom {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.4rem;
  background: linear-gradient(0deg, #0B1220 0%, #131C2F 100%);
  transform: translateY(0);
  transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 2;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
}
.gate-bottom.open { transform: translateY(100%); }

.gate-track {
  width: min(280px, 80vw);
  height: 56px;
  background: #1a2438;
  border-radius: 28px;
  border: 2px solid #2a3a55;
  position: relative;
  overflow: hidden;
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.5);
}
.gate-handle {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(145deg, #94a3b8, #475569);
  box-shadow: 0 0 14px rgba(45, 212, 191, 0.55), inset 0 1px 2px rgba(255,255,255,0.3);
  cursor: grab;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.gate-handle:active { cursor: grabbing; }
.gate-handle:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.handle-glow {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
  animation: pulse-glow 1.5s ease-in-out infinite;
}
@keyframes pulse-glow {
  0%, 100% { opacity: 0.7; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.15); }
}

.btn-enter {
  min-height: 48px; min-width: 120px;
  padding: 0.6rem 1.5rem;
  font-family: inherit; font-size: 1.05rem; font-weight: 600;
  background: var(--accent); color: #0B1220;
  border: none; border-radius: 999px; cursor: pointer;
}
.btn-enter:hover, .btn-enter:focus-visible {
  transform: scale(1.04);
  box-shadow: 0 0 16px rgba(45,212,191,0.5);
  outline: none;
}

/* ===== شكر تيك توك ===== */
.tiktok-thanks {
  position: fixed; inset: 0; z-index: 2000;
  background: radial-gradient(ellipse at center, #1a1040 0%, #0B1220 70%);
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.5s;
}
.tiktok-thanks.hidden { opacity: 0; pointer-events: none; }
.thanks-content { text-align: center; animation: fade-up 0.8s ease; }
.thanks-msg {
  font-size: clamp(1.7rem, 7vw, 2.7rem); font-weight: 700;
  margin-bottom: 2rem; color: #E9D5FF;
}
.btn-continue {
  min-height: 48px; padding: 0.7rem 2rem;
  font-family: inherit; font-size: 1.1rem; font-weight: 600;
  background: var(--purple); color: #fff;
  border: none; border-radius: 999px; cursor: pointer;
}
@keyframes fade-up {
  from { opacity: 0; transform: translateY(30px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== التطبيق ===== */
.app {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  background: var(--bg);
  height: 100dvh;
}
.app.hidden { display: none; }

.top-bar {
  height: 52px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 0.6rem;
  padding-top: env(safe-area-inset-top, 0);
  background: var(--card);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  z-index: 50; gap: 0.4rem;
}
.app-title {
  font-size: 0.95rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1; text-align: center;
}
.btn-icon {
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: transparent; border: none; color: var(--text);
  cursor: pointer; border-radius: 10px; flex-shrink: 0;
}
.btn-icon:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hamburger {
  width: 20px; height: 2px; background: var(--text); position: relative; display: block;
}
.hamburger::before, .hamburger::after {
  content: ''; position: absolute; width: 20px; height: 2px; background: var(--text); left: 0;
}
.hamburger::before { top: -6px; }
.hamburger::after { top: 6px; }

/* زر تيك توك زجاجي */
.tiktok-btn {
  display: flex; align-items: center; gap: 0.35rem;
  padding: 0.25rem 0.55rem 0.25rem 0.35rem;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.18);
  box-shadow: 0 0 12px rgba(45,212,191,0.25), inset 0 1px 0 rgba(255,255,255,0.1);
  color: var(--accent);
  text-decoration: none;
  flex-shrink: 0;
  animation: tiktok-pulse 2.5s ease-in-out infinite;
}
.tiktok-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tiktok-icon-wrap {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  background: rgba(45,212,191,0.15);
}
.tiktok-img { display: block; }
.tiktok-img.hidden { display: none; }
.tiktok-fallback { display: block; }
.tiktok-label { font-size: 0.75rem; font-weight: 600; color: var(--text); }
@keyframes tiktok-pulse {
  0%, 100% { box-shadow: 0 0 10px rgba(45,212,191,0.2); }
  50% { box-shadow: 0 0 18px rgba(45,212,191,0.45); }
}

/* ===== المشهد ===== */
.canvas-wrap {
  flex: 1; position: relative; min-height: 0; overflow: hidden;
}
#c { display: block; width: 100%; height: 100%; touch-action: none; }
.webgl-fallback {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1rem; padding: 1.5rem; text-align: center; background: var(--bg);
}
.webgl-fallback.hidden { display: none; }

/* أدوات جانبية يسار */
.side-tools {
  position: absolute;
  left: 0.5rem;
  bottom: 0.75rem;
  display: flex; flex-direction: column; gap: 0.4rem;
  z-index: 25;
}
.side-tool-btn {
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(19,28,47,0.85);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  color: var(--text);
  cursor: pointer;
}
.side-tool-btn[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(45,212,191,0.15);
}
.side-tool-btn:focus-visible { outline: 2px solid var(--accent); }

/* ===== bottom sheets ===== */
.info-sheet, .ctrl-sheet {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  padding: 0.6rem 1.1rem calc(1.2rem + var(--safe-bottom));
  box-shadow: var(--shadow);
  transform: translateY(110%);
  transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 40;
  max-height: 55%;
  overflow-y: auto;
}
.info-sheet.open, .ctrl-sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 40px; height: 4px; background: rgba(255,255,255,0.2);
  border-radius: 2px; margin: 0 auto 0.7rem;
}
.info-title, .sheet-title {
  font-size: 1.1rem; font-weight: 600; margin-bottom: 0.4rem; color: var(--accent);
}
.info-role { margin-bottom: 0.45rem; font-size: 0.95rem; }
.info-question {
  font-size: 0.9rem; opacity: 0.9;
  border-right: 3px solid var(--accent);
  padding-right: 0.7rem; margin-bottom: 0.7rem;
}
.btn-sheet-close {
  min-height: 46px; width: 100%;
  font-family: inherit; font-size: 1rem;
  background: rgba(45,212,191,0.12); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 10px; cursor: pointer;
}

/* لوحة التحكم */
.ctrl-sliders { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 1rem; }
.slider-block { display: flex; flex-direction: column; gap: 0.3rem; }
.slider-block label { font-size: 0.9rem; font-weight: 500; }
.slider-block input[type="range"] {
  width: 100%; height: 8px; accent-color: var(--accent); cursor: pointer;
}
.rm-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem;
  margin-bottom: 0.8rem;
}
.rm-btn {
  min-height: 44px; padding: 0.4rem 0.5rem;
  font-family: inherit; font-size: 0.85rem;
  background: #1e293b; color: var(--text);
  border: 1px solid #334155; border-radius: 8px; cursor: pointer;
}
.rm-btn[aria-pressed="true"] {
  background: rgba(239,68,68,0.18); border-color: var(--pos); color: #fca5a5;
}
.ctrl-actions { display: flex; gap: 0.4rem; margin-bottom: 0.7rem; }
.ctrl-actions .panel-btn { flex: 1; margin: 0; }

/* ===== شريط التحكم السفلي المبسط ===== */
.controls-bar {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  padding-bottom: calc(0.45rem + var(--safe-bottom));
  background: var(--card);
  border-top: 1px solid rgba(255,255,255,0.06);
  z-index: 30; min-height: 58px;
}
.ctrl-btn {
  min-width: 48px; min-height: 48px;
  font-size: 1.15rem;
  background: #1e293b; color: var(--text);
  border: none; border-radius: 12px; cursor: pointer; flex-shrink: 0;
}
.ctrl-btn:focus-visible { outline: 2px solid var(--accent); }
.ctrl-btn-wide {
  min-width: 72px; font-size: 0.95rem; font-weight: 600;
  font-family: inherit;
}
.timer-block { flex: 1; display: flex; flex-direction: column; gap: 0.2rem; min-width: 0; }
.timer-row {
  display: flex; align-items: baseline; justify-content: center; gap: 0.2rem;
  font-variant-numeric: tabular-nums; font-weight: 600; font-size: 0.95rem;
}
.timer-max { opacity: 0.5; font-size: 0.8rem; font-weight: 400; }
.timer-progress {
  height: 4px; background: #1e293b; border-radius: 2px; overflow: hidden;
}
.timer-fill {
  height: 100%; width: 0%; background: var(--accent);
  border-radius: 2px; transition: width 0.3s linear;
}

/* رسالة الحالة */
.status-msg {
  position: absolute;
  bottom: calc(70px + var(--safe-bottom));
  left: 50%; transform: translateX(-50%);
  background: rgba(19,28,47,0.95); color: var(--text);
  padding: 0.5rem 1rem; border-radius: 10px;
  font-size: 0.88rem; max-width: 90%; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 0.3s;
  z-index: 35; border: 1px solid rgba(45,212,191,0.3);
}
.status-msg.show { opacity: 1; }

/* ===== اللوحة الجانبية ===== */
.side-panel {
  position: fixed; top: 0; right: 0;
  width: min(320px, 90vw); height: 100%;
  background: var(--card);
  box-shadow: -8px 0 32px rgba(0,0,0,0.4);
  z-index: 100;
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  overflow-y: auto; display: flex; flex-direction: column;
}
.side-panel.open { transform: translateX(0); }
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  padding-top: calc(0.7rem + env(safe-area-inset-top, 0));
}
.panel-header h3 { font-size: 1.1rem; }
.panel-nav { padding: 0.7rem; flex: 1; }
.panel-section { margin-bottom: 1.1rem; }
.panel-section h4 {
  font-size: 0.82rem; opacity: 0.7; margin-bottom: 0.45rem;
  font-weight: 500; display: flex; align-items: center; gap: 0.3rem;
}
.sec-icon { font-size: 1rem; }
.exp-list { list-style: none; display: flex; flex-direction: column; gap: 0.3rem; }
.exp-list button {
  width: 100%; text-align: right; min-height: 46px;
  padding: 0.5rem 0.75rem; font-family: inherit; font-size: 0.88rem;
  background: #1e293b; color: var(--text);
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
}
.exp-list button.active { border-color: var(--accent); background: rgba(45,212,191,0.12); }
.panel-btn {
  width: 100%; min-height: 46px; margin-bottom: 0.35rem;
  padding: 0.55rem; font-family: inherit; font-size: 0.92rem;
  background: #1e293b; color: var(--text);
  border: none; border-radius: 8px; cursor: pointer;
}
.panel-btn:focus-visible { outline: 2px solid var(--accent); }
.env-btns { display: flex; gap: 0.35rem; margin-top: 0.35rem; }
.env-btn {
  flex: 1; min-height: 42px; font-family: inherit; font-size: 0.85rem;
  background: #1e293b; color: var(--text);
  border: 1px solid #334155; border-radius: 8px; cursor: pointer;
}
.env-btn.active { border-color: var(--accent); background: rgba(45,212,191,0.15); color: var(--accent); }

/* ===== النوافذ ===== */
.modal {
  position: fixed; inset: 0; z-index: 150;
  background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--card); border-radius: var(--radius);
  padding: 1.2rem; max-width: 400px; width: 100%;
  max-height: 85dvh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal-content h3 { margin-bottom: 0.9rem; font-size: 1.1rem; color: var(--accent); }
.safety-box {
  background: rgba(239,68,68,0.12); border: 1px solid rgba(239,68,68,0.35);
  border-radius: 10px; padding: 0.7rem; margin-bottom: 1rem; font-size: 0.9rem;
}
.prep-list { list-style: none; margin-bottom: 1rem; }
.prep-list li { margin-bottom: 0.55rem; }
.prep-list label {
  display: flex; align-items: flex-start; gap: 0.5rem;
  cursor: pointer; font-size: 0.92rem;
}
.prep-list input[type="checkbox"] {
  width: 20px; height: 20px; margin-top: 2px; accent-color: var(--accent); flex-shrink: 0;
}
.btn-modal-close {
  width: 100%; min-height: 48px; margin-top: 0.6rem;
  font-family: inherit; font-size: 1rem;
  background: rgba(45,212,191,0.12); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 10px; cursor: pointer;
}
.teacher-qa p { margin-bottom: 0.45rem; font-size: 0.92rem; }
.teacher-qa .answer {
  color: var(--accent); font-size: 0.88rem; margin-bottom: 0.9rem;
  padding-right: 0.7rem; border-right: 2px solid var(--accent);
}
.teacher-qa .answer.hidden { display: none; }
.predict-q { margin-bottom: 0.9rem; font-size: 1rem; }
.predict-options { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.9rem; }
.predict-options button {
  min-height: 48px; font-family: inherit; font-size: 0.92rem;
  background: #1e293b; color: var(--text);
  border: 1px solid #334155; border-radius: 8px; cursor: pointer;
  text-align: right; padding: 0.5rem 0.75rem;
}
.predict-options button.selected { border-color: var(--accent); background: rgba(45,212,191,0.15); }
.predict-feedback { font-size: 0.92rem; min-height: 1.4em; margin-bottom: 0.4rem; }

/* ===== شاشة الأسئلة ===== */
.quiz-screen {
  position: fixed; inset: 0; z-index: 300;
  background: var(--bg); display: flex; flex-direction: column;
}
.quiz-screen.hidden { display: none; }
.quiz-header {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.6rem 0.8rem;
  padding-top: calc(0.6rem + env(safe-area-inset-top, 0));
  background: var(--card); border-bottom: 1px solid rgba(255,255,255,0.06);
}
.quiz-progress-wrap { flex: 1; }
.quiz-progress-bar {
  height: 6px; background: #1e293b; border-radius: 3px; overflow: hidden; margin-bottom: 0.25rem;
}
.quiz-progress-fill {
  height: 100%; width: 0%; background: var(--accent); border-radius: 3px; transition: width 0.35s;
}
.quiz-counter { font-size: 0.82rem; opacity: 0.75; }
.quiz-body {
  flex: 1; overflow-y: auto; padding: 1rem;
  display: flex; align-items: flex-start; justify-content: center;
}
.quiz-card {
  width: 100%; max-width: 480px;
  background: var(--card); border-radius: var(--radius);
  padding: 1.2rem; box-shadow: var(--shadow);
}
.quiz-question {
  font-size: 1.05rem; font-weight: 600; margin-bottom: 1.1rem; line-height: 1.7;
}
.quiz-options { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1rem; }
.quiz-options button {
  min-height: 52px; font-family: inherit; font-size: 0.95rem;
  background: #1e293b; color: var(--text);
  border: 2px solid #334155; border-radius: 12px; cursor: pointer;
  text-align: right; padding: 0.7rem 1rem;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
}
.quiz-options button.correct {
  border-color: #22c55e; background: rgba(34,197,94,0.18); color: #86efac;
  animation: correct-pop 0.4s ease;
}
.quiz-options button.wrong {
  border-color: #ef4444; background: rgba(239,68,68,0.15); color: #fca5a5;
  animation: wrong-shake 0.4s ease;
}
@keyframes correct-pop {
  0% { transform: scale(1); } 50% { transform: scale(1.03); } 100% { transform: scale(1); }
}
@keyframes wrong-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}
.quiz-explain {
  background: rgba(45,212,191,0.1); border-right: 3px solid var(--accent);
  padding: 0.7rem 0.9rem; border-radius: 8px; font-size: 0.9rem;
  margin-bottom: 1rem; animation: slide-down 0.35s ease;
}
.quiz-explain.hidden { display: none; }
@keyframes slide-down {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}
.quiz-nav { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.btn-scene, .btn-next {
  min-height: 48px; flex: 1; font-family: inherit; font-size: 0.95rem; font-weight: 600;
  border: none; border-radius: 10px; cursor: pointer;
}
.btn-scene { background: #1e293b; color: var(--text); border: 1px solid #334155; }
.btn-next { background: var(--accent); color: #0B1220; }
.btn-scene.hidden, .btn-next.hidden { display: none; }

/* ===== نتيجة الاختبار ===== */
.quiz-result {
  position: fixed; inset: 0; z-index: 350;
  background: radial-gradient(ellipse at center, #131C2F 0%, #0B1220 80%);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.quiz-result.hidden { display: none; }
.result-content {
  text-align: center; position: relative; width: 100%; max-width: 400px;
}
.result-score-label { font-size: 1rem; opacity: 0.8; margin-bottom: 0.3rem; }
.result-score {
  font-size: clamp(2rem, 10vw, 3rem); font-weight: 700; color: var(--accent);
  margin-bottom: 0.2rem;
}
.result-percent { font-size: 1.3rem; opacity: 0.85; margin-bottom: 0.6rem; }
.result-stars { font-size: 1.8rem; margin-bottom: 0.6rem; letter-spacing: 0.15em; }
.result-title { font-size: 1.25rem; font-weight: 600; margin-bottom: 1.2rem; }
.confetti-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 0;
}
.result-actions {
  display: flex; flex-direction: column; gap: 0.4rem; position: relative; z-index: 1;
}
.review-list { max-height: 50vh; overflow-y: auto; margin-bottom: 0.8rem; }
.review-item {
  background: #1e293b; border-radius: 10px; padding: 0.7rem;
  margin-bottom: 0.5rem; font-size: 0.88rem;
}
.review-item strong { color: var(--accent); }

/* ===== تقليل الحركة ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== شاشات صغيرة ===== */
@media (max-width: 380px) {
  .app-title { font-size: 0.85rem; }
  .tiktok-label { display: none; }
  .side-tools { left: 0.3rem; bottom: 0.5rem; }
  .side-tool-btn { width: 40px; height: 40px; }
}

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