:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #273449;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --teal: #14b8a6;
  --teal-dark: #0f766e;
  --blue: #3b82f6;
  --indigo: #6366f1;
  --amber: #f59e0b;
  --red: #ef4444;
  --green: #22c55e;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
}

.app-shell {
  display: grid;
  grid-template-columns: 245px 1fr;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 6px 8px;
}
.brand img { width: 38px; height: 38px; border-radius: 10px; }
.brand h1 { font-size: 1.15rem; font-weight: 800; letter-spacing: -0.02em; }
.brand span { display: block; font-size: 0.72rem; color: var(--teal); font-weight: 600; }

.nav-list { display: flex; flex-direction: column; gap: 6px; }
.nav-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 11px 13px;
  text-align: left;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: all 0.15s ease;
}
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active {
  background: rgba(20, 184, 166, 0.14);
  color: var(--teal);
  border-color: rgba(20, 184, 166, 0.35);
}

.sidebar-footer {
  margin-top: auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  font-size: 0.8rem;
}
.ai-lock-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 999px;
  margin-bottom: 6px;
}
.ai-lock-badge.locked { background: rgba(34,197,94,0.16); color: var(--green); border: 1px solid rgba(34,197,94,0.35); }
.ai-lock-badge.unlocked { background: rgba(245,158,11,0.16); color: var(--amber); border: 1px solid rgba(245,158,11,0.35); }

/* Main area */
.main-wrap { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: rgba(11, 17, 32, 0.9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.timer-box {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 12px;
}
.timer-digits { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 1.08rem; font-weight: 800; color: var(--teal); min-width: 66px; }
.timer-digits.warn { color: var(--amber); }
.timer-digits.over { color: var(--red); }

.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.content {
  padding: 24px;
  max-width: 1140px;
  width: 100%;
  margin: 0 auto;
  padding-bottom: 90px;
}

/* Cards & Grids */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.card-title { font-size: 1.08rem; font-weight: 700; }
.muted { color: var(--muted); font-size: 0.88rem; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.grid-4 { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }

.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 15px;
}
.stat-label { font-size: 0.78rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.stat-value { font-size: 1.65rem; font-weight: 800; margin-top: 4px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 9px 15px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn:hover { filter: brightness(1.12); transform: translateY(-1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--teal-dark); border-color: var(--teal); color: #fff; }
.btn-blue { background: #1d4ed8; border-color: var(--blue); color: #fff; }
.btn-amber { background: rgba(245,158,11,0.18); border-color: var(--amber); color: #fde68a; }
.btn-danger { background: rgba(239,68,68,0.16); border-color: var(--red); color: #fecaca; }
.btn-sm { padding: 6px 11px; font-size: 0.8rem; border-radius: 8px; }

/* Gatekeeper Banners */
.banner {
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 18px;
  border: 1px solid;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.banner-lock { background: rgba(245, 158, 11, 0.11); border-color: rgba(245, 158, 11, 0.45); }
.banner-open { background: rgba(20, 184, 166, 0.11); border-color: rgba(20, 184, 166, 0.45); }

/* Badges */
.pill {
  display: inline-block;
  font-size: 0.74rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.pill-teal { background: rgba(20,184,166,0.16); color: #5eead4; border-color: rgba(20,184,166,0.4); }
.pill-amber { background: rgba(245,158,11,0.16); color: #fcd34d; border-color: rgba(245,158,11,0.4); }
.pill-red { background: rgba(239,68,68,0.16); color: #fca5a5; border-color: rgba(239,68,68,0.4); }
.pill-green { background: rgba(34,197,94,0.16); color: #86efac; border-color: rgba(34,197,94,0.4); }
.pill-blue { background: rgba(59,130,246,0.16); color: #93c5fd; border-color: rgba(59,130,246,0.4); }

/* Timeline items */
.timeline-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--teal);
  border-radius: 12px;
  padding: 15px 16px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.timeline-item.mandatory { border-left-color: var(--amber); }

/* Forms */
input, select, textarea {
  width: 100%;
  background: #0f172a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--teal); }
label { display: block; font-size: 0.82rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.form-group { margin-bottom: 14px; }

/* Lesson Sections */
.lesson-section {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 14px;
}
.lesson-section h3 {
  font-size: 0.96rem;
  color: var(--teal);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.lesson-section ul { padding-left: 18px; }
.lesson-section li { margin-bottom: 7px; font-size: 0.94rem; }

.comp-table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.87rem; }
.comp-table th, .comp-table td { border: 1px solid var(--border); padding: 9px 11px; text-align: left; }
.comp-table th { background: rgba(20,184,166,0.16); color: #5eead4; }

/* MCQ & Rapid Fire */
.mcq-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  margin-bottom: 14px;
}
.mcq-option {
  display: block;
  width: 100%;
  text-align: left;
  background: #0f172a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 14px;
  margin-top: 8px;
  cursor: pointer;
  font-size: 0.91rem;
  transition: all 0.15s;
}
.mcq-option:hover:not(:disabled) { border-color: var(--blue); background: rgba(59,130,246,0.12); }
.mcq-option.correct { background: rgba(34,197,94,0.18); border-color: var(--green); color: #dcfce7; }
.mcq-option.wrong { background: rgba(239,68,68,0.18); border-color: var(--red); color: #fee2e2; }

.explanation-box {
  margin-top: 12px;
  padding: 12px 14px;
  background: rgba(15, 23, 42, 0.9);
  border-left: 3px solid var(--teal);
  border-radius: 8px;
  font-size: 0.89rem;
}

/* Flashcard */
.flashcard-box {
  background: linear-gradient(145deg, #172554 0%, #0f172a 100%);
  border: 1px solid #334155;
  border-radius: 18px;
  padding: 28px 24px;
  min-height: 230px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.flashcard-front { font-size: 1.12rem; font-weight: 700; }
.flashcard-back {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px dashed #475569;
  font-size: 1rem;
  color: #a7f3d0;
}
.fsrs-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-top: 16px;
}

/* Upload Zone */
.upload-zone {
  border: 2px dashed #334155;
  border-radius: 14px;
  padding: 26px;
  text-align: center;
  background: rgba(15, 23, 42, 0.55);
  transition: border-color 0.2s;
}
.upload-zone:hover { border-color: var(--teal); }

.progress-bar-bg {
  width: 100%;
  height: 10px;
  background: #0f172a;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--teal), var(--blue));
  transition: width 0.25s ease;
}

/* Simple Bar Chart */
.bar-chart {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  height: 150px;
  padding-top: 16px;
}
.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  height: 100%;
  justify-content: flex-end;
}
.bar-fill {
  width: 100%;
  max-width: 30px;
  background: linear-gradient(180deg, var(--teal), var(--blue));
  border-radius: 6px 6px 0 0;
  min-height: 4px;
}
.bar-lbl { font-size: 0.68rem; color: var(--muted); }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #0f172a;
  border: 1px solid var(--teal);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 99;
  display: none;
  max-width: 380px;
  font-size: 0.88rem;
}

.hidden { display: none !important; }
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2.5px solid rgba(255,255,255,0.2);
  border-top-color: var(--teal);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Mobile Responsive Bottom Bar */
@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    height: auto;
    flex-direction: row;
    padding: 6px 8px;
    border-right: none;
    border-top: 1px solid var(--border);
    z-index: 50;
    justify-content: space-around;
  }
  .brand, .sidebar-footer { display: none; }
  .nav-list { flex-direction: row; width: 100%; justify-content: space-around; }
  .nav-btn { flex-direction: column; gap: 3px; font-size: 0.7rem; padding: 7px 6px; text-align: center; }
  .content { padding: 14px 12px 92px 12px; }
  .topbar { padding: 10px 12px; }
  .fsrs-bar { grid-template-columns: repeat(2, 1fr); }
}


/* ========== Mobile & Tablet Ergonomics v3.0 ========== */
html {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
body {
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.btn, .nav-btn, .mcq-option {
  min-height: 44px;
}
.flashcard-box {
  user-select: none;
  -webkit-user-select: none;
  touch-action: pan-y;
}
.conn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.conn-pill.online { border-color: rgba(34,197,94,0.45); color: #86efac; background: rgba(34,197,94,0.14); }
.conn-pill.offline { border-color: rgba(239,68,68,0.5); color: #fca5a5; background: rgba(239,68,68,0.16); }

/* Tablet Landscape (768px - 1140px): Compact rail sidebar + wider reading canvas */
@media (min-width: 861px) and (max-width: 1140px) {
  .app-shell { grid-template-columns: 205px 1fr; }
  .content { padding: 18px 18px 80px 18px; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}

/* Mobile Portrait & Small Tablets (<= 860px): Thumb-zone bottom bar + safe area inset */
@media (max-width: 860px) {
  .sidebar {
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom)) 6px;
    background: rgba(17, 24, 39, 0.96);
    backdrop-filter: blur(14px);
  }
  .nav-btn {
    min-height: 48px;
    font-size: 0.68rem;
    padding: 6px 4px;
    border-radius: 10px;
    flex: 1;
  }
  .content { padding-bottom: calc(104px + env(safe-area-inset-bottom)); }
  .topbar {
    padding: 8px 12px;
    gap: 8px;
  }
  .timer-box {
    width: 100%;
    justify-content: space-between;
  }
  .topbar-actions {
    width: 100%;
    justify-content: space-between;
  }
  .card { padding: 15px; }
  .flashcard-box { min-height: 250px; padding: 22px 16px; }
  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(74px + env(safe-area-inset-bottom));
    max-width: none;
  }
}
