/* ===== CSS Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #6C63FF;
  --primary-light: #8B83FF;
  --primary-dark: #4A42D4;
  --accent: #FF6584;
  --success: #4CAF50;
  --error: #F44336;
  --warning: #FF9800;
  --bg: #FAFBFF;
  --bg-card: #FFFFFF;
  --bg-code: #1E1E3F;
  --text: #2D2D2D;
  --text-light: #6B7280;
  --text-code: #E4E4E7;
  --border: #E5E7EB;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-mono: 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 80px;
}

body {
  font-family: var(--font);
  font-size: 1.05rem;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ===== Navigation ===== */
#top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
}

.nav-inner {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: 56px;
  overflow-x: auto;
}

.nav-logo {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--primary);
  white-space: nowrap;
  margin-right: 0.5rem;
  flex-shrink: 0;
}

.nav-link {
  text-decoration: none;
  color: var(--text-light);
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  white-space: nowrap;
  transition: background 0.2s, color 0.2s;
}

.nav-link:hover,
.nav-link.active {
  background: var(--primary);
  color: white;
}

#progress-bar {
  position: fixed;
  top: 56px;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  width: 0%;
  z-index: 101;
  transition: width 0.1s;
}

/* ===== Score Counter ===== */
#score-counter {
  position: fixed;
  bottom: 1.5rem;
  left: 1.5rem;
  background: var(--primary);
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  z-index: 50;
}

/* ===== Loading Banner ===== */
#pyodide-loading {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.8rem 1.2rem;
  font-size: 0.9rem;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#pyodide-loading .spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#pyodide-loading.loaded {
  background: var(--success);
  color: white;
  border-color: var(--success);
}

#pyodide-loading.error {
  background: var(--error);
  color: white;
  border-color: var(--error);
}

/* ===== Main Content ===== */
main {
  max-width: 860px;
  margin: 0 auto;
  padding: 80px 1rem 4rem;
}

.hero {
  text-align: center;
  padding: 2rem 0 3rem;
}

.hero h1 {
  font-size: 2.2rem;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 0.5rem;
}

.hero p {
  font-size: 1.1rem;
  color: var(--text-light);
}

/* ===== Chapter Sections ===== */
.chapter {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  margin-bottom: 2rem;
}

.chapter-header {
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid var(--primary);
}

.chapter-number {
  display: inline-block;
  background: var(--primary);
  color: white;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  margin-bottom: 0.4rem;
}

.chapter-header h2 {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text);
}

/* ===== Content Elements ===== */
.concept-box {
  margin-bottom: 1.5rem;
}

.concept-box h3 {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--primary-dark);
  margin-bottom: 0.5rem;
}

.concept-box p {
  margin-bottom: 0.8rem;
}

.callout {
  background: #F0EFFF;
  border-left: 4px solid var(--primary);
  padding: 1rem 1.2rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin: 1rem 0;
  font-size: 0.95rem;
}

.callout.tip {
  background: #E8F5E9;
  border-left-color: var(--success);
}

.callout.warning {
  background: #FFF3E0;
  border-left-color: var(--warning);
}

/* ===== Code Examples (static) ===== */
.code-example {
  margin: 1rem 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.code-example-label {
  background: #2D2B55;
  color: var(--text-light);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.4rem 1rem;
}

.code-example pre {
  background: var(--bg-code);
  color: var(--text-code);
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.6;
  padding: 1rem;
  overflow-x: auto;
  margin: 0;
}

.code-example .output-preview {
  background: #0D0D2B;
  color: #A5D6A7;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid #333366;
}

.code-example .output-label {
  color: var(--text-light);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.2rem;
}

/* Syntax highlighting for static code */
.kw { color: #C792EA; }   /* keywords */
.fn { color: #82AAFF; }   /* functions */
.st { color: #C3E88D; }   /* strings */
.cm { color: #676E95; }   /* comments */
.nu { color: #F78C6C; }   /* numbers */
.op { color: #89DDFF; }   /* operators */

/* ===== Diagrams ===== */
.diagram {
  margin: 1.5rem 0;
  text-align: center;
}

.diagram svg {
  max-width: 100%;
  height: auto;
}

/* ===== Tables ===== */
.styled-table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.95rem;
}

.styled-table th {
  background: var(--primary);
  color: white;
  padding: 0.6rem 1rem;
  text-align: left;
  font-weight: 600;
}

.styled-table td {
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
}

.styled-table tr:nth-child(even) {
  background: #F8F9FF;
}

.styled-table code {
  background: #EEF;
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 0.85em;
}

/* ===== Step-by-Step Walkthrough ===== */
.walkthrough {
  margin: 1.5rem 0;
  background: linear-gradient(135deg, #F8F7FF 0%, #F0EFFF 100%);
  border: 1px solid #E0DEFF;
  border-radius: var(--radius);
  padding: 1.5rem;
}

.walkthrough h3 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary-dark);
  margin-bottom: 1rem;
}

.walkthrough-steps {
  counter-reset: step;
}

.walkthrough-step {
  display: none;
  animation: fadeIn 0.3s ease;
}

.walkthrough-step.active {
  display: block;
}

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

.step-label {
  display: inline-block;
  background: var(--primary);
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  margin-bottom: 0.5rem;
}

.walkthrough-step p {
  margin-bottom: 0.5rem;
}

.walkthrough-step .code-example {
  margin: 0.5rem 0;
}

.walkthrough-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 1rem;
  gap: 0.5rem;
}

.walkthrough-nav .btn {
  min-width: 80px;
}

.step-dots {
  display: flex;
  gap: 6px;
}

.step-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #D0CEF0;
  transition: background 0.2s;
  cursor: pointer;
  border: none;
  padding: 0;
}

.step-dot.visited {
  background: var(--primary-light);
}

.step-dot.active {
  background: var(--primary);
  transform: scale(1.3);
}

/* ===== Exercises Section ===== */
.exercises {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 2px dashed var(--border);
}

.exercises > h3 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 1rem;
}

.exercise {
  background: #FAFBFF;
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary-light);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: 1.2rem;
  margin-bottom: 1rem;
  transition: border-left-color 0.3s;
}

.exercise.correct {
  border-left-color: var(--success);
  background: #F1F8F1;
}

.exercise.incorrect {
  border-left-color: var(--error);
  background: #FFF5F5;
}

#score-result {
  padding: 1.5rem;
  background: #F8F7FF;
  border-radius: var(--radius);
  border: 2px solid var(--primary-light);
}

#check-score-btn:not(:disabled) {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0.4); }
  50% { box-shadow: 0 0 0 10px rgba(108, 99, 255, 0); }
}

.exercise p {
  margin-bottom: 0.6rem;
}

.exercise-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-light);
  margin-bottom: 0.4rem;
}

/* MCQ */
.options {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.option-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.6rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: white;
  font-family: var(--font);
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.2s;
}

.option-btn:hover {
  border-color: var(--primary);
  background: #F0EFFF;
}

.option-btn.selected-correct {
  border-color: var(--success);
  background: #E8F5E9;
  color: var(--success);
  font-weight: 600;
}

.option-btn.selected-wrong {
  border-color: var(--error);
  background: #FFEBEE;
  color: var(--error);
}

.option-btn:disabled {
  cursor: default;
  opacity: 0.7;
}

/* Fill-in-the-blank */
.fill-blank-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.fill-input {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  padding: 0.5rem 0.8rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  min-width: 150px;
  transition: border-color 0.2s;
}

.fill-input:focus {
  outline: none;
  border-color: var(--primary);
}

.fill-input.correct {
  border-color: var(--success);
  background: #E8F5E9;
}

.fill-input.wrong {
  border-color: var(--error);
  background: #FFEBEE;
}

/* Buttons */
.btn {
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.5rem 1.2rem;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

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

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

.btn-primary:disabled {
  background: #B0B0B0;
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  color: var(--text-light);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: #F0F0F0;
}

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

.btn-run {
  background: var(--success);
  color: white;
}

.btn-run:hover {
  background: #388E3C;
}

.btn-run:disabled {
  background: #B0B0B0;
  cursor: not-allowed;
}

/* Code Editor */
.code-editor-area {
  width: 100%;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.5;
  background: var(--bg-code);
  color: var(--text-code);
  border: none;
  border-radius: var(--radius-sm);
  padding: 1rem;
  resize: vertical;
  min-height: 100px;
  tab-size: 4;
}

.code-editor-area:focus {
  outline: 2px solid var(--primary);
}

.editor-controls {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.code-output {
  background: #0D0D2B;
  color: #A5D6A7;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 0.8rem 1rem;
  border-radius: var(--radius-sm);
  margin-top: 0.5rem;
  min-height: 2rem;
  white-space: pre-wrap;
  display: none;
}

.code-output.visible {
  display: block;
}

.code-output.error {
  color: #EF9A9A;
}

/* Feedback */
.feedback {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  display: none;
}

.feedback.visible {
  display: block;
}

.feedback.correct {
  color: var(--success);
}

.feedback.wrong {
  color: var(--error);
}

/* ===== Binary Addition ===== */
.binary-table {
  border-collapse: collapse;
  margin: 1rem auto;
  font-family: var(--font-mono);
  font-size: 1.2rem;
}

.binary-table td {
  width: 40px;
  height: 40px;
  text-align: center;
  vertical-align: middle;
  border: 1px solid var(--border);
}

.binary-table .carry-row td {
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  height: 24px;
}

.binary-table .separator td {
  border-top: 2px solid var(--text);
  height: 4px;
  padding: 0;
}

.binary-table .op-cell {
  border: none;
  font-weight: 700;
}

.binary-input {
  width: 36px;
  height: 36px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 1.1rem;
  border: 2px solid var(--border);
  border-radius: 4px;
  background: white;
}

.binary-input:focus {
  outline: none;
  border-color: var(--primary);
}

.binary-input.correct {
  border-color: var(--success);
  background: #E8F5E9;
}

.binary-input.wrong {
  border-color: var(--error);
  background: #FFEBEE;
}

/* ===== Back to Top ===== */
#back-to-top {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  z-index: 49;
  display: none;
}

#back-to-top.visible {
  opacity: 1;
  pointer-events: auto;
  display: block;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .chapter {
    padding: 1.2rem;
    border-radius: var(--radius-sm);
  }

  .hero h1 {
    font-size: 1.6rem;
  }

  .chapter-header h2 {
    font-size: 1.3rem;
  }

  .nav-inner {
    gap: 0.3rem;
  }

  .nav-logo {
    font-size: 0.95rem;
  }

  .nav-link {
    font-size: 0.75rem;
    padding: 0.2rem 0.4rem;
  }

  #score-counter {
    bottom: 0.8rem;
    left: 0.8rem;
    font-size: 0.75rem;
    padding: 0.4rem 0.7rem;
  }

  .binary-table {
    font-size: 1rem;
  }

  .binary-table td {
    width: 32px;
    height: 32px;
  }

  .binary-input {
    width: 28px;
    height: 28px;
    font-size: 0.9rem;
  }
}

@media (max-width: 480px) {
  main {
    padding: 70px 0.5rem 3rem;
  }

  .chapter {
    padding: 1rem;
  }

  .options .option-btn {
    font-size: 0.85rem;
    padding: 0.5rem 0.8rem;
  }
}
