:root {
  --bg-main: #0b0f19;
  --bg-surface: #111827;
  --bg-card: #1f2937;
  --bg-card-hover: #283548;
  --border: #2d3748;
  --border-subtle: #1e293b;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-faint: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-glow: rgba(59, 130, 246, 0.25);
  --success: #10b981;
  --success-glow: rgba(16, 185, 129, 0.2);
  --warning: #f59e0b;
  --sidebar-w: 380px;
}

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

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

body.drawer-locked {
  overflow: hidden;
}

/* App Layout */
#app {
  display: flex;
  width: 100vw;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

/* Mobile Sidebar Backdrop */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}

.sidebar-backdrop.active {
  display: block;
  opacity: 1;
  pointer-events: auto;
}

/* Sidebar */
aside.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100%;
  height: 100dvh;
  background-color: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 50;
}

.sidebar-header {
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: bold;
  font-size: 1.2rem;
  box-shadow: 0 4px 12px var(--primary-glow);
  flex-shrink: 0;
}

.brand-title h1 {
  font-size: 1.05rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.01em;
}

.brand-title .badge {
  display: inline-block;
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 9999px;
  font-weight: 600;
  margin-top: 2px;
}

.close-sidebar-btn {
  display: none;
  background: #1e293b;
  border: 1px solid var(--border);
  color: var(--text-muted);
  width: 34px;
  height: 34px;
  border-radius: 8px;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  cursor: pointer;
  touch-action: manipulation;
}

.close-sidebar-btn:hover {
  background: #334155;
  color: #fff;
}

/* Progress bar in header */
.global-progress {
  margin-top: 1rem;
  padding: 0.75rem;
  background: rgba(15, 23, 42, 0.6);
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.progress-bar-bg {
  width: 100%;
  height: 6px;
  background: #1e293b;
  border-radius: 9999px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #10b981);
  border-radius: 9999px;
  transition: width 0.3s ease;
}

/* Search input */
.search-box {
  margin-top: 0.75rem;
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 0.6rem 0.75rem 0.6rem 2.25rem;
  background: #0f172a;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: #fff;
  font-size: 16px; /* Prevents auto-zoom in iOS Safari */
  outline: none;
  transition: border-color 0.2s;
}

.search-box input:focus {
  border-color: var(--primary);
}

.search-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-faint);
  font-size: 0.9rem;
}

/* Modules Accordion List */
.modules-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.5rem 0;
}

.module-card {
  border-bottom: 1px solid var(--border-subtle);
}

.module-header {
  padding: 0.9rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  background-color: var(--bg-surface);
  transition: background 0.2s;
  touch-action: manipulation;
}

.module-header:hover {
  background-color: rgba(255, 255, 255, 0.03);
}

.module-header-left {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  max-width: 80%;
}

.module-arrow {
  font-size: 0.75rem;
  color: var(--text-faint);
  transition: transform 0.2s ease;
}

.module-card.open .module-arrow {
  transform: rotate(90deg);
}

.module-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: #e2e8f0;
  line-height: 1.3;
}

.module-meta {
  font-size: 0.72rem;
  color: var(--text-faint);
  white-space: nowrap;
}

.lesson-list {
  display: none;
  background-color: #0b0f19;
  border-top: 1px solid var(--border-subtle);
}

.module-card.open .lesson-list {
  display: block;
}

.lesson-item {
  padding: 0.75rem 1rem 0.75rem 2.2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: all 0.15s ease;
  touch-action: manipulation;
}

.lesson-item:hover {
  background-color: rgba(59, 130, 246, 0.05);
}

.lesson-item.active {
  background-color: rgba(59, 130, 246, 0.12);
  border-left-color: var(--primary);
}

.lesson-left {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex: 1;
  min-width: 0;
}

.check-btn {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--text-faint);
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
  touch-action: manipulation;
  font-size: 0.8rem;
}

.check-btn.checked {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.lesson-name {
  font-size: 0.82rem;
  color: #cbd5e1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

.lesson-item.active .lesson-name {
  color: #fff;
  font-weight: 600;
}

/* Main Content Area */
main.content-area {
  flex: 1;
  height: 100%;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-main);
  position: relative;
}

/* Top Navbar */
.top-nav {
  min-height: 60px;
  padding: 0.5rem 1.5rem;
  padding-top: max(0.5rem, env(safe-area-inset-top));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background-color: var(--bg-surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 40;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  flex: 1;
}

.menu-toggle-btn {
  background: #1e293b;
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 0.45rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  font-weight: 500;
  flex-shrink: 0;
  touch-action: manipulation;
}

.menu-toggle-btn:hover {
  background: #334155;
}

.current-breadcrumb {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.current-breadcrumb span {
  color: #fff;
  font-weight: 600;
}

.breadcrumb-separator {
  margin: 0 0.35rem;
  color: var(--text-faint);
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
}

.btn {
  padding: 0.45rem 0.9rem;
  border-radius: 7px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: all 0.2s;
  touch-action: manipulation;
}

.btn-secondary {
  background-color: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background-color: var(--bg-card-hover);
}

.btn-primary {
  background-color: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background-color: var(--primary-hover);
}

.btn-success {
  background-color: var(--success);
  color: #fff;
}

/* Main Viewport Container */
.player-container {
  padding: 1.5rem;
  max-width: 1280px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
}

/* Video Wrapper */
.video-box {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  border: 1px solid var(--border);
  position: relative;
}

.video-box video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

/* Video Controls / Speed Pills */
.video-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  padding: 0.5rem 0;
  gap: 1rem;
}

.speed-pills-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.speed-pills-wrapper::-webkit-scrollbar {
  display: none;
}

.speed-pills {
  display: inline-flex;
  gap: 0.4rem;
  align-items: center;
  white-space: nowrap;
}

.speed-pills .speed-label {
  font-size: 0.75rem;
  color: var(--text-faint);
  margin-right: 0.2rem;
}

.speed-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 0.3rem 0.65rem;
  border-radius: 6px;
  cursor: pointer;
  touch-action: manipulation;
}

.speed-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  font-weight: 600;
}

.playback-hint {
  font-size: 0.75rem;
  color: var(--text-faint);
  white-space: nowrap;
}

/* Text lesson view (when no video) */
.text-lesson-hero {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 1.5rem;
  text-align: center;
  margin-bottom: 1.5rem;
}

.hero-icon {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
}

.hero-module {
  font-size: 0.8rem;
  color: var(--primary);
  text-transform: uppercase;
  font-weight: 700;
  margin-bottom: 0.4rem;
}

.hero-title {
  font-size: 1.4rem;
  margin-bottom: 0.75rem;
  color: #fff;
}

.hero-desc {
  font-size: 0.9rem;
  color: var(--text-muted);
  max-width: 600px;
  margin: 0 auto;
}

/* Mobile Lesson Header Card */
.mobile-lesson-card {
  display: none;
  padding: 0.9rem 1rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-top: 0.75rem;
}

.mobile-mod-name {
  font-size: 0.72rem;
  color: #60a5fa;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.05em;
  margin-bottom: 2px;
}

.mobile-lesson-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: #fff;
  line-height: 1.35;
}

/* Tabs Section */
.content-tabs {
  margin-top: 1.5rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.tabs-nav-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  background: rgba(15, 23, 42, 0.5);
  border-bottom: 1px solid var(--border);
}

.tabs-nav-wrapper::-webkit-scrollbar {
  display: none;
}

.tabs-nav {
  display: inline-flex;
  padding: 0 0.5rem;
  white-space: nowrap;
}

.tab-btn {
  padding: 0.9rem 1.25rem;
  min-height: 48px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.2s;
  touch-action: manipulation;
}

.tab-btn:hover {
  color: #fff;
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.tab-pane {
  display: none;
  padding: 1.75rem;
  animation: fadeIn 0.2s ease-in-out;
}

.tab-pane.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Markdown Rendering in Summary Tab */
.markdown-body {
  color: #cbd5e1;
  line-height: 1.75;
  font-size: 0.95rem;
  word-break: break-word;
}

.markdown-body h1, .markdown-body h2, .markdown-body h3 {
  color: #fff;
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
  letter-spacing: -0.01em;
}

.markdown-body h1 { font-size: 1.4rem; border-bottom: 1px solid var(--border-subtle); padding-bottom: 0.5rem; }
.markdown-body h2 { font-size: 1.2rem; }
.markdown-body h3 { font-size: 1.05rem; }

.markdown-body p {
  margin-bottom: 1rem;
}

.markdown-body ul, .markdown-body ol {
  padding-left: 1.5rem;
  margin-bottom: 1rem;
}

.markdown-body li {
  margin-bottom: 0.4rem;
}

.markdown-body blockquote {
  border-left: 4px solid var(--primary);
  background: rgba(59, 130, 246, 0.08);
  padding: 0.75rem 1.25rem;
  border-radius: 0 8px 8px 0;
  margin: 1.25rem 0;
  color: #93c5fd;
  font-style: italic;
}

.markdown-body strong {
  color: #f8fafc;
  font-weight: 600;
}

.markdown-body code {
  background: #1e293b;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  font-size: 0.85em;
  color: #38bdf8;
}

/* PDF Viewer Tab */
.pdf-tab-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.pdf-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* Mobile PDF Action Box */
.pdf-mobile-actions {
  display: none;
  background: #1e293b;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.pdf-file-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.pdf-file-icon {
  font-size: 2rem;
  line-height: 1;
}

.pdf-file-text {
  min-width: 0;
  flex: 1;
}

.pdf-file-name {
  font-weight: 600;
  color: #fff;
  font-size: 0.95rem;
  line-height: 1.3;
  word-break: break-all;
}

.pdf-file-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.pdf-action-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.btn-touch {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
  border-radius: 10px;
  text-align: center;
}

.pdf-frame-wrapper {
  width: 100%;
  height: 750px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #1e293b;
}

.pdf-frame-wrapper iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
}

/* Notes Tab */
.notes-tab textarea {
  width: 100%;
  height: 320px;
  background: #0f172a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  color: #fff;
  font-size: 16px; /* Prevents auto-zoom in iOS Safari */
  font-family: inherit;
  line-height: 1.6;
  resize: vertical;
  outline: none;
}

.notes-tab textarea:focus {
  border-color: var(--primary);
}

.notes-status {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-faint);
  display: flex;
  justify-content: flex-end;
}

/* Mobile Bottom Navigation Bar */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(56px + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(15, 23, 42, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  z-index: 80;
  align-items: center;
  justify-content: space-around;
  padding-left: 0.25rem;
  padding-right: 0.25rem;
}

.bottom-nav-btn {
  flex: 1;
  height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 500;
  gap: 2px;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.15s ease, color 0.15s ease;
  touch-action: manipulation;
}

.bottom-nav-btn:active {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.bottom-nav-btn .nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.bottom-nav-btn.primary-complete-btn {
  color: #60a5fa;
  font-weight: 600;
}

.bottom-nav-btn.primary-complete-btn.completed {
  color: #10b981;
}

.bottom-nav-btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

/* ==========================================================================
   RESPONSIVE & MOBILE / IPHONE OPTIMIZATIONS (Max-Width 900px)
   ========================================================================== */
@media (max-width: 900px) {
  /* Sidebar as Drawer */
  aside.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 360px);
    min-width: unset;
    height: 100%;
    height: 100dvh;
    z-index: 100;
    transform: translateX(-100%);
    box-shadow: 10px 0 35px rgba(0, 0, 0, 0.7);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  aside.sidebar.mobile-open {
    transform: translateX(0);
  }

  .close-sidebar-btn {
    display: flex;
  }

  /* Desktop elements to hide on mobile */
  .desktop-nav-actions {
    display: none !important;
  }

  .playback-hint {
    display: none;
  }

  /* Show mobile elements */
  .mobile-bottom-nav {
    display: flex;
  }

  .mobile-lesson-card {
    display: block;
  }

  .pdf-mobile-actions {
    display: block;
  }

  /* Adjust player container padding for bottom bar and safe area */
  .player-container {
    padding: 0.75rem 0.75rem calc(70px + env(safe-area-inset-bottom)) 0.75rem;
  }

  .video-box {
    border-radius: 10px;
  }

  .video-bar {
    margin-top: 0.5rem;
    padding: 0.25rem 0;
  }

  .content-tabs {
    margin-top: 1rem;
    border-radius: 10px;
  }

  .tab-pane {
    padding: 1.15rem;
  }

  .pdf-frame-wrapper {
    height: 380px;
  }

  .current-breadcrumb .breadcrumb-separator,
  .current-breadcrumb #nav-module-title {
    display: none;
  }

  .current-breadcrumb #nav-lesson-title {
    font-size: 0.9rem;
  }

  .menu-btn-label {
    display: none;
  }
}
