:root {
  --bg-page: hsl(210, 25%, 97%);
  --bg-surface: hsl(0, 0%, 100%);
  --bg-subtle: hsl(210, 22%, 94%);
  --border-color: hsl(214, 20%, 88%);
  --border-strong: hsl(214, 18%, 78%);

  --text-primary: hsl(222, 32%, 15%);
  --text-secondary: hsl(218, 18%, 35%);
  --text-muted: hsl(215, 14%, 52%);

  --primary: hsl(213, 82%, 46%);
  --primary-hover: hsl(213, 84%, 39%);
  --primary-soft: hsl(213, 90%, 95%);

  --success: hsl(154, 68%, 36%);
  --success-soft: hsl(152, 65%, 94%);
  --success-border: hsl(152, 52%, 78%);

  --warning-soft: hsl(42, 95%, 95%);
  --warning-border: hsl(38, 85%, 65%);

  --copilot-soft: hsl(198, 88%, 95%);
  --copilot-border: hsl(198, 72%, 52%);

  --code-bg: #1e1e1e;
  --code-header: #252526;
  --code-border: #333333;
  --code-text: #d4d4d4;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 20px 45px rgba(15, 23, 42, 0.16);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

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

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background-color: var(--bg-page);
  color: var(--text-primary);
  line-height: 1.68;
  -webkit-font-smoothing: antialiased;
  font-variant-ligatures: none;
  font-feature-settings: "calt" 0, "liga" 0;
}

[hidden] {
  display: none !important;
}

/* Top Navigation */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
}

.topbar-inner {
  max-width: 1320px;
  margin: 0 auto;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.brand-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  background: none;
  border: none;
  padding: 0.2rem 0.4rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-primary);
  font-family: inherit;
  transition: background-color 0.15s ease;
}

.brand-btn:hover {
  background: var(--bg-subtle);
}

.brand-badge {
  background: var(--primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
}

.brand-title {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
}

.topbar-divider {
  color: var(--text-muted);
}

.current-chapter-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 340px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.progress-pill {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--bg-subtle);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border-color);
}

.progress-text {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.progress-track {
  width: 84px;
  height: 6px;
  background: var(--border-color);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--success);
  border-radius: 999px;
  transition: width 0.25s ease;
}

/* Buttons */
.btn-primary, .btn-secondary, .btn-ghost {
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.48rem 0.9rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
  border: 1px solid transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

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

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

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border-color);
}

.btn-secondary:hover {
  background: var(--bg-subtle);
  border-color: var(--border-strong);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

/* Main Container */
.main-container {
  max-width: 1320px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}

/* Portal Hero */
.hero-block {
  max-width: 780px;
  margin: 0 auto 2.5rem;
  text-align: center;
}

.hero-eyebrow {
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--primary);
  margin: 0 0 0.5rem;
}

.hero-block h1 {
  font-size: clamp(1.65rem, 3vw, 2.25rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.25;
  margin: 0 0 0.75rem;
}

.hero-subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: 1rem;
}

/* Chapter Grid */
.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(310px, 1fr));
  gap: 1.25rem;
}

.chapter-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 1.35rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 1rem;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}

.chapter-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary);
}

.chapter-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.mod-tag {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
}

.lock-badge {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
}

.lock-badge.locked {
  background: var(--bg-subtle);
  color: var(--text-secondary);
}

.lock-badge.unlocked {
  background: var(--success-soft);
  color: var(--success);
}

.chapter-card-title {
  margin: 0.25rem 0 0;
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.4;
}

.chapter-labs-preview {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.chapter-labs-preview li {
  margin-bottom: 0.18rem;
}

.chapter-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border-color);
  font-size: 0.82rem;
  color: var(--text-muted);
}

.chapter-card-action {
  color: var(--primary);
  font-weight: 600;
}

/* Reader Layout */
.reader-view {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 2rem;
  align-items: start;
}

.sidebar {
  position: sticky;
  top: 4.75rem;
}

.sidebar-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 1rem;
  box-shadow: var(--shadow-sm);
  max-height: calc(100vh - 6.5rem);
  overflow-y: auto;
}

.sidebar-header {
  padding-bottom: 0.75rem;
  margin-bottom: 0.65rem;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-kicker {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
}

.sidebar-title {
  margin: 0.2rem 0 0;
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.4;
}

.toc-nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.toc-link {
  display: block;
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 0.84rem;
  padding: 0.35rem 0.55rem;
  border-radius: 6px;
  border-left: 2px solid transparent;
  transition: all 0.15s ease;
  line-height: 1.35;
}

.toc-link.level-2 {
  font-weight: 700;
  color: var(--text-primary);
  margin-top: 0.35rem;
}

.toc-link.level-3 {
  padding-left: 1.1rem;
  font-size: 0.8rem;
}

.toc-link:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.toc-link.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-left-color: var(--primary);
  font-weight: 600;
}

/* Article Pane */
.article-pane {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 2.25rem 2.5rem;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}

.article-header {
  padding-bottom: 1.25rem;
  margin-bottom: 1.75rem;
  border-bottom: 1px solid var(--border-color);
}

.article-kicker {
  display: inline-block;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 0.22rem 0.65rem;
  border-radius: 6px;
  margin-bottom: 0.6rem;
}

.article-title {
  margin: 0;
  font-size: clamp(1.45rem, 2.4vw, 1.95rem);
  font-weight: 700;
  letter-spacing: -0.015em;
  line-height: 1.3;
}

.lab-heading {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 2.2rem 0 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--primary-soft);
  color: var(--text-primary);
  scroll-margin-top: 5.5rem;
}

.content-p {
  margin: 0.55rem 0;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}

.content-p a, .content-list a, .callout a {
  color: var(--primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.content-list {
  margin: 0.6rem 0 0.9rem;
  padding-left: 1.35rem;
  color: var(--text-secondary);
}

.content-list li {
  margin-bottom: 0.35rem;
}

/* Step Card */
.step-card {
  background: hsl(210, 30%, 98.5%);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 1.2rem 1.35rem;
  margin: 1.25rem 0;
  scroll-margin-top: 5.5rem;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.step-card.completed {
  background: var(--success-soft);
  border-color: var(--success-border);
}

.step-header {
  margin-bottom: 0.75rem;
}

.step-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.45;
}

.step-footer {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--border-strong);
  display: flex;
  justify-content: flex-end;
}

.step-check-label {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 0.42rem 0.85rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.step-check-label:hover {
  border-color: var(--success);
  color: var(--success);
}

.step-card.completed .step-check-label {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.step-checkbox {
  width: 1.05rem;
  height: 1.05rem;
  accent-color: var(--success);
  cursor: pointer;
  flex-shrink: 0;
}

.step-content > :first-child {
  margin-top: 0;
}

.step-content > :last-child {
  margin-bottom: 0;
}

/* Callouts */
.callout {
  border-radius: var(--radius-sm);
  padding: 0.9rem 1.1rem;
  margin: 1rem 0;
  font-size: 0.94rem;
  line-height: 1.6;
  border-left: 4px solid;
}

.note-callout {
  background: var(--warning-soft);
  border-left-color: var(--warning-border);
  color: hsl(34, 48%, 22%);
}

.copilot-callout {
  background: var(--copilot-soft);
  border-left-color: var(--copilot-border);
  color: hsl(204, 58%, 20%);
}

/* File Pill & VS Code Dark Code Card */
.file-pill {
  display: inline-block;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82rem;
  font-weight: 500;
  background: var(--bg-subtle);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  padding: 0.25rem 0.65rem;
  border-radius: 6px;
  margin: 0.65rem 0 0.25rem;
}

.code-card {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: var(--radius-sm);
  margin: 0.9rem 0;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.code-header {
  background: var(--code-header);
  border-bottom: 1px solid var(--code-border);
  padding: 0.45rem 0.85rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

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

.code-lang {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.74rem;
  font-weight: 600;
  color: #9cdcfe;
  text-transform: uppercase;
}

.code-filename {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.78rem;
  color: #cccccc;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.copy-btn {
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 600;
  color: #d4d4d4;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 5px;
  padding: 0.22rem 0.55rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color 0.15s ease;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

.code-card pre {
  margin: 0;
  padding: 0.95rem 1.05rem;
  overflow-x: auto;
}

.code-card code {
  font-family: 'JetBrains Mono', Consolas, monospace;
  font-size: 0.84rem;
  line-height: 1.55;
  color: var(--code-text);
  tab-size: 4;
}

/* Image Cards & Lightbox */
.image-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  margin: 1rem 0;
  text-align: center;
}

.image-card img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  cursor: zoom-in;
  display: block;
  margin: 0 auto;
}

.image-caption {
  margin-top: 0.45rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.lightbox-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(15, 23, 42, 0.88);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.lightbox-img {
  max-width: 92vw;
  max-height: 88vh;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
}

.lightbox-close {
  position: fixed;
  top: 1.25rem;
  right: 1.5rem;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 999px;
  font-size: 1.1rem;
  cursor: pointer;
}

/* Pagination */
.chapter-pagination {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 2.75rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-color);
}

.page-nav-btn {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.85rem 1.15rem;
  cursor: pointer;
  font-family: inherit;
  color: var(--text-primary);
  max-width: 48%;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.page-nav-btn:hover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.page-nav-btn.next {
  margin-left: auto;
  text-align: right;
}

.page-nav-arrow {
  font-size: 1.4rem;
  color: var(--primary);
  font-weight: 700;
}

.page-nav-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.page-nav-meta small {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.page-nav-meta strong {
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* PIN Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(15, 23, 42, 0.52);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  padding: 2rem 1.75rem 1.75rem;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.modal-card.shake {
  animation: shake 0.32s ease;
}

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

.modal-close {
  position: absolute;
  top: 0.9rem;
  right: 0.9rem;
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem 0.45rem;
  border-radius: 6px;
}

.modal-close:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.modal-icon {
  font-size: 2rem;
  margin-bottom: 0.4rem;
}

.modal-card h2 {
  margin: 0 0 0.35rem;
  font-size: 1.25rem;
}

.modal-desc {
  margin: 0 0 1.25rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.pin-input {
  width: 180px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.45em;
  text-align: center;
  padding: 0.55rem 0.75rem 0.55rem 1.1rem;
  border: 2px solid var(--border-strong);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.15s ease;
}

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

.pin-error {
  margin: 0.7rem 0 0;
  color: hsl(0, 72%, 48%);
  font-size: 0.85rem;
  font-weight: 600;
}

.modal-actions {
  margin-top: 1.35rem;
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}

.loading-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
}

/* Responsive */
@media (max-width: 960px) {
  .reader-view {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: static;
  }
  .sidebar-card {
    max-height: 260px;
  }
  .article-pane {
    padding: 1.5rem 1.25rem;
  }
}

@media (max-width: 600px) {
  .topbar-inner {
    padding: 0.65rem 1rem;
  }
  .brand-title {
    font-size: 0.9rem;
  }
  .current-chapter-label, .topbar-divider {
    display: none !important;
  }
  .chapter-pagination {
    flex-direction: column;
  }
  .page-nav-btn {
    max-width: 100%;
  }
}
