/* ── CSS Custom Properties ─────────────────────────────────── */
:root {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --border: #30363d;
  --border-bright: #484f58;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --accent-green: #3fb950;
  --accent-blue: #58a6ff;
  --accent-purple: #bc8cff;
  --accent-orange: #f0883e;
  --accent-red: #f85149;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'SF Mono', 'Consolas', 'Liberation Mono', monospace;
  --radius: 6px;
  --transition: 0.25s ease;

  /* Level colors mapped */
  --level-0: var(--accent-green);
  --level-1: var(--accent-blue);
  --level-2: var(--accent-purple);
  --level-3: var(--accent-orange);
  --level-4: var(--accent-red);
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
}

a { color: var(--accent-blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Scrollbar ────────────────────────────────────────────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-bright); }

/* ── Layout Grid ──────────────────────────────────────────── */
.app-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 48px 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  height: 100vh;
  width: 100vw;
}

/* ── Top Bar ──────────────────────────────────────────────── */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar-level {
  font-size: 13px;
  color: var(--text-secondary);
}

.topbar-level .level-name {
  color: var(--text-primary);
  font-weight: 600;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.topbar-score {
  font-size: 12px;
  color: var(--text-muted);
}

.topbar-score span {
  color: var(--accent-green);
  font-weight: 600;
}

/* ── Progress Bar (top) ───────────────────────────────────── */
.progress-bar-wrapper {
  flex: 1;
  max-width: 300px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-bar-track {
  flex: 1;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent-green);
  border-radius: 3px;
  transition: width var(--transition);
  box-shadow: 0 0 6px var(--accent-green);
}

.progress-bar-label {
  font-size: 11px;
  color: var(--text-muted);
  min-width: 32px;
  text-align: right;
}

/* ── Sidebar ──────────────────────────────────────────────── */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-logo {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.sidebar-logo .logo-accent {
  color: var(--accent-blue);
}

.sidebar-nav {
  flex: 1;
  padding: 8px 0;
  overflow-y: auto;
}

/* ── Level Groups ─────────────────────────────────────────── */
.level-group {
  margin-bottom: 4px;
}

.level-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  cursor: pointer;
  user-select: none;
  transition: background var(--transition);
  border-left: 3px solid transparent;
}

.level-header:hover {
  background: var(--bg-tertiary);
}

.level-header.active {
  background: var(--bg-tertiary);
}

.level-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid;
}

.level-title {
  flex: 1;
  font-size: 13px;
  color: var(--text-secondary);
  transition: color var(--transition);
}

.level-header.active .level-title,
.level-header:hover .level-title {
  color: var(--text-primary);
}

.level-header.locked {
  opacity: 0.4;
  cursor: not-allowed;
}

.level-header.locked:hover {
  background: transparent;
}

.level-header.locked:hover .level-title {
  color: var(--text-secondary);
}

.level-lock {
  font-size: 11px;
  color: var(--text-muted);
}

/* Level progress mini bar */
.level-progress-mini {
  width: 40px;
  height: 3px;
  background: var(--bg-primary);
  border-radius: 2px;
  overflow: hidden;
}

.level-progress-mini-fill {
  height: 100%;
  border-radius: 2px;
  transition: width var(--transition);
}

/* ── Section Items ────────────────────────────────────────── */
.section-list {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.3s ease;
}

.section-list.open {
  max-height: 500px;
}

.section-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px 6px 48px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  transition: all var(--transition);
  border-left: 3px solid transparent;
}

.section-item:hover {
  color: var(--text-secondary);
  background: rgba(255,255,255,0.02);
}

.section-item.active {
  color: var(--text-primary);
  background: rgba(255,255,255,0.04);
}

.section-item .section-status {
  font-size: 10px;
  width: 14px;
  text-align: center;
}

.section-status.completed { color: var(--accent-green); }
.section-status.available { color: var(--text-muted); }
.section-status.in-progress { color: var(--accent-blue); }

/* ── Sidebar Footer ───────────────────────────────────────── */
.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.reset-btn {
  display: block;
  width: 100%;
  padding: 6px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  border-radius: var(--radius);
  transition: all var(--transition);
}

.reset-btn:hover {
  border-color: var(--accent-red);
  color: var(--accent-red);
}

/* ── Main Content ─────────────────────────────────────────── */
.main-content {
  grid-area: main;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
}

/* ── Tabs ─────────────────────────────────────────────────── */
.tabs-bar {
  display: flex;
  padding: 0 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  gap: 0;
}

.tab-btn {
  padding: 10px 20px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
}

.tab-btn:hover {
  color: var(--text-secondary);
  background: rgba(255,255,255,0.02);
}

.tab-btn.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent-blue);
}

/* ── Tab Panels ───────────────────────────────────────────── */
.tab-panels {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-panel {
  position: absolute;
  inset: 0;
  padding: 24px;
  overflow-y: auto;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.tab-panel.active {
  opacity: 1;
  pointer-events: auto;
}

/* ── Read Panel Content ───────────────────────────────────── */
.read-content h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.read-content h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 20px 0 10px;
  color: var(--text-primary);
}

.read-content p {
  margin-bottom: 12px;
  color: var(--text-secondary);
  line-height: 1.7;
}

.read-content ul, .read-content ol {
  margin: 0 0 16px 24px;
  color: var(--text-secondary);
}

.read-content li {
  margin-bottom: 6px;
}

.read-content code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 13px;
  color: var(--accent-blue);
}

.read-content strong {
  color: var(--text-primary);
  font-weight: 600;
}

.read-content table,
.read-content .content-table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: 13px;
}

.read-content table th,
.read-content .content-table th {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  text-align: left;
  padding: 8px 12px;
  border: 1px solid var(--border);
  font-weight: 600;
}

.read-content table td,
.read-content .content-table td {
  padding: 8px 12px;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.read-content table tr:hover td,
.read-content .content-table tr:hover td {
  background: var(--bg-tertiary);
}

.read-content blockquote {
  border-left: 3px solid var(--accent-purple);
  padding: 12px 16px;
  margin: 16px 0;
  background: var(--bg-secondary);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-secondary);
  font-style: italic;
}

.read-content h3 + p,
.read-content h3 + ul,
.read-content h3 + ol {
  margin-top: 4px;
}

.code-block {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 16px 0;
  overflow-x: auto;
}

.code-block pre {
  padding: 16px;
  margin: 0;
}

.code-block code {
  background: none;
  padding: 0;
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
}

/* Section complete button */
.section-complete-btn {
  display: inline-block;
  margin-top: 24px;
  padding: 8px 20px;
  background: transparent;
  border: 1px solid var(--accent-green);
  color: var(--accent-green);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius);
  transition: all var(--transition);
}

.section-complete-btn:hover {
  background: rgba(63, 185, 80, 0.1);
  box-shadow: 0 0 12px rgba(63, 185, 80, 0.2);
}

.section-complete-btn.completed {
  background: rgba(63, 185, 80, 0.15);
  cursor: default;
  opacity: 0.7;
}

/* ── Explorer Panel ───────────────────────────────────────── */
.explorer-wrapper {
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: 100%;
}

.explorer-svg {
  width: 100%;
  max-width: 800px;
  height: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
}

.explorer-svg .node-label {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  text-anchor: middle;
  dominant-baseline: middle;
}

.explorer-svg .conn-label {
  font-family: var(--font-mono);
  font-size: 10px;
  fill: var(--text-muted);
  text-anchor: middle;
}

.explorer-svg .node-group {
  transition: opacity 0.3s ease;
}

.explorer-svg .conn-line {
  transition: opacity 0.3s ease, stroke 0.3s ease;
}

.explorer-detail {
  min-height: 100px;
}

.detail-hint {
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 20px;
}

.detail-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-left: 3px solid;
  border-radius: var(--radius);
  padding: 16px 20px;
  animation: fadeSlideIn 0.25s ease;
}

.detail-card h3 {
  font-size: 16px;
  margin-bottom: 8px;
}

.detail-card p {
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.6;
}

.detail-connections {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.detail-label {
  font-size: 11px;
  color: var(--text-muted);
}

.detail-tag {
  font-size: 11px;
  padding: 2px 8px;
  border: 1px solid;
  border-radius: 3px;
  color: var(--text-secondary);
}

/* ── Challenge Panel ──────────────────────────────────────── */
.challenges-wrapper {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.challenge-block {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.challenge-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.challenge-number {
  font-size: 11px;
  color: var(--text-muted);
}

.challenge-type-badge {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.challenge-type-multiple-choice { background: rgba(88, 166, 255, 0.15); color: var(--accent-blue); }
.challenge-type-fill-blank { background: rgba(188, 140, 255, 0.15); color: var(--accent-purple); }
.challenge-type-drag-match { background: rgba(240, 136, 62, 0.15); color: var(--accent-orange); }
.challenge-type-build-it { background: rgba(63, 185, 80, 0.15); color: var(--accent-green); }

.challenge-done-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 3px;
  background: rgba(63, 185, 80, 0.15);
  color: var(--accent-green);
}

.challenge-question {
  font-size: 14px;
  color: var(--text-primary);
  margin-bottom: 16px;
  line-height: 1.6;
}

.challenge-body {
  position: relative;
}

/* Multiple Choice */
.mc-options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mc-option {
  display: block;
  width: 100%;
  padding: 10px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: all var(--transition);
}

.mc-option:hover:not(:disabled) {
  border-color: var(--accent-blue);
  color: var(--text-primary);
  background: rgba(88, 166, 255, 0.05);
}

.mc-option:disabled {
  cursor: default;
}

.mc-option.mc-correct {
  border-color: var(--accent-green);
  background: rgba(63, 185, 80, 0.1);
  color: var(--accent-green);
}

.mc-option.mc-wrong {
  border-color: var(--accent-red);
  background: rgba(248, 81, 73, 0.1);
  color: var(--accent-red);
}

/* Fill in the Blank */
.fill-blank-code {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  margin-bottom: 12px;
}

.fill-blank-code pre {
  padding: 16px;
  margin: 0;
}

.fill-blank-code code {
  font-size: 14px;
  color: var(--text-primary);
  line-height: 1.5;
  white-space: pre;
}

.fill-blank-input {
  display: inline-block;
  width: 140px;
  padding: 2px 8px;
  background: var(--bg-primary);
  border: 1px solid var(--accent-blue);
  border-radius: 3px;
  color: var(--accent-blue);
  font-family: var(--font-mono);
  font-size: 14px;
  outline: none;
  transition: all var(--transition);
}

.fill-blank-input:focus {
  box-shadow: 0 0 8px rgba(88, 166, 255, 0.3);
}

.fill-blank-input.fill-correct {
  border-color: var(--accent-green);
  color: var(--accent-green);
  background: rgba(63, 185, 80, 0.1);
}

.fill-blank-input.fill-wrong {
  border-color: var(--accent-red);
  color: var(--accent-red);
  background: rgba(248, 81, 73, 0.1);
}

/* Drag & Match */
.drag-match-wrapper {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.drag-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.drag-arrows {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  justify-content: flex-start;
  padding-top: 2px;
}

.drag-arrow {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 38px;
  color: var(--text-muted);
  font-size: 12px;
}

.drag-item {
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
  user-select: none;
}

.drag-item:hover {
  border-color: var(--border-bright);
}

.drag-source.drag-selected {
  border-color: var(--accent-blue);
  color: var(--accent-blue);
  box-shadow: 0 0 8px rgba(88, 166, 255, 0.2);
}

.drag-item.drag-paired {
  opacity: 0.8;
}

.drag-item.drag-matched {
  border-color: var(--accent-green);
  opacity: 0.7;
}

.drag-item.drag-correct {
  border-color: var(--accent-green);
  color: var(--accent-green);
}

.drag-item.drag-wrong {
  border-color: var(--accent-red);
  color: var(--accent-red);
}

/* Build It */
.build-it-wrapper {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.build-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.build-field-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.build-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.build-input {
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 13px;
  outline: none;
  transition: all var(--transition);
}

.build-input:focus {
  border-color: var(--accent-green);
  box-shadow: 0 0 8px rgba(63, 185, 80, 0.2);
}

.build-input-error {
  border-color: var(--accent-red) !important;
  animation: shake 0.3s ease;
}

.build-input:disabled {
  opacity: 0.7;
}

.build-preview-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.build-preview {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
}

.build-preview code {
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
}

/* Challenge buttons */
.challenge-btn {
  display: inline-block;
  padding: 8px 20px;
  background: transparent;
  border: 1px solid var(--accent-blue);
  color: var(--accent-blue);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius);
  transition: all var(--transition);
  margin-top: 8px;
}

.challenge-btn:hover:not(:disabled) {
  background: rgba(88, 166, 255, 0.1);
  box-shadow: 0 0 10px rgba(88, 166, 255, 0.2);
}

.challenge-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Feedback */
.challenge-feedback {
  margin-top: 16px;
  padding: 12px 16px;
  border-radius: var(--radius);
  border-left: 3px solid;
  animation: fadeSlideIn 0.3s ease;
}

.feedback-correct {
  background: rgba(63, 185, 80, 0.08);
  border-color: var(--accent-green);
}

.feedback-wrong {
  background: rgba(248, 81, 73, 0.08);
  border-color: var(--accent-red);
}

.feedback-icon {
  font-weight: 700;
  margin-right: 8px;
}

.feedback-correct .feedback-icon { color: var(--accent-green); }
.feedback-wrong .feedback-icon { color: var(--accent-red); }

.feedback-text {
  font-size: 13px;
  font-weight: 600;
}

.feedback-correct .feedback-text { color: var(--accent-green); }
.feedback-wrong .feedback-text { color: var(--accent-red); }

.feedback-explanation {
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
}

/* Challenge empty state */
.challenge-empty {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

.challenge-empty-icon {
  display: block;
  font-size: 32px;
  margin-top: 12px;
  opacity: 0.3;
}

/* ── Animations ───────────────────────────────────────────── */
@keyframes fadeSlideIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ── Welcome screen ───────────────────────────────────────── */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  padding: 40px;
}

.welcome-ascii {
  font-size: 11px;
  color: var(--accent-green);
  line-height: 1.3;
  margin-bottom: 24px;
  opacity: 0.8;
}

.welcome-title {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.welcome-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

/* ── Language Switcher ────────────────────────────────────── */
.lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 11px;
  transition: all var(--transition);
}

.lang-switch:hover {
  border-color: var(--accent-blue);
}

.lang-option {
  padding: 1px 4px;
  border-radius: 3px;
  color: var(--text-muted);
  transition: all var(--transition);
}

.lang-option.active {
  color: var(--accent-blue);
  background: rgba(88, 166, 255, 0.12);
  font-weight: 700;
}

.lang-separator {
  color: var(--border-bright);
  font-size: 10px;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 900px) {
  .app-layout {
    grid-template-columns: 200px 1fr;
  }
  .build-fields {
    grid-template-columns: 1fr;
  }
}
