/* ============================================
   PRO1 Study Web — Design System
   ============================================ */

/* --- CSS Custom Properties --- */
:root {
  /* Colors - Light */
  --bg-primary: #fafafa;
  --bg-content: #ffffff;
  --bg-card: #ffffff;
  --bg-card-hover: #f0f4ff;
  --text-primary: #1a1a2e;
  --text-secondary: #4b5563;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;
  --border-light: #f3f4f6;
  --accent: #1a73e8;
  --accent-light: #e8f0fe;
  --accent-dark: #1557b0;

  /* Component colors */
  --def-border: #3b82f6;
  --def-bg: #eff6ff;
  --def-text: #1e40af;
  --thm-border: #10b981;
  --thm-bg: #ecfdf5;
  --thm-text: #065f46;
  --example-border: #f59e0b;
  --example-bg: #fffbeb;
  --example-text: #92400e;
  --remark-border: #8b5cf6;
  --remark-bg: #f5f3ff;
  --remark-text: #5b21b6;
  --warning-border: #ef4444;
  --warning-bg: #fef2f2;
  --warning-text: #991b1b;
  --step-bg: #f8fafc;
  --step-border: #cbd5e1;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.04);

  /* Typography */
  --font-body: "Segoe UI", "Noto Sans", system-ui, -apple-system, sans-serif;
  --font-mono: "SF Mono", "Fira Code", "Consolas", monospace;
  --font-size-base: 1.05rem;
  --line-height: 1.75;

  /* Spacing */
  --content-max-width: 820px;
  --toc-width: 240px;
  --header-height: 60px;

  /* Transitions */
  --transition: 0.2s ease;
}

/* Dark theme */
[data-theme="dark"] {
  --bg-primary: #0f172a;
  --bg-content: #1e293b;
  --bg-card: #1e293b;
  --bg-card-hover: #273449;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border-color: #334155;
  --border-light: #1e293b;
  --accent: #60a5fa;
  --accent-light: #1e3a5f;
  --accent-dark: #93bbfc;

  --def-border: #3b82f6;
  --def-bg: #172554;
  --def-text: #93c5fd;
  --thm-border: #10b981;
  --thm-bg: #022c22;
  --thm-text: #6ee7b7;
  --example-border: #f59e0b;
  --example-bg: #451a03;
  --example-text: #fcd34d;
  --remark-border: #8b5cf6;
  --remark-bg: #2e1065;
  --remark-text: #c4b5fd;
  --warning-border: #ef4444;
  --warning-bg: #450a0a;
  --warning-text: #fca5a5;
  --step-bg: #1e293b;
  --step-border: #475569;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.5);
}

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

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--header-height) + 20px);
}

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

/* --- Typography --- */
h1, h2, h3, h4 {
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.3;
  color: var(--text-primary);
}

h1 { font-size: 2rem; margin-bottom: 1rem; }
h2 { font-size: 1.5rem; margin: 2.5rem 0 1rem; padding-bottom: 0.5rem; border-bottom: 2px solid var(--border-color); }
h3 { font-size: 1.25rem; margin: 2rem 0 0.75rem; }
h4 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }

p { margin-bottom: 1rem; }

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

strong { font-weight: 600; }

ul, ol {
  margin: 0.5rem 0 1rem 1.5rem;
}

li { margin-bottom: 0.3rem; }

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--accent-light);
  padding: 0.15em 0.4em;
  border-radius: 4px;
}

/* --- Header --- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-content);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
  height: var(--header-height);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.site-logo {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.site-logo:hover { color: var(--accent); text-decoration: none; }

.site-logo .logo-icon {
  font-size: 1.5rem;
}

.header-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.breadcrumb a { color: var(--text-muted); }
.breadcrumb a:hover { color: var(--accent); }
.breadcrumb .separator { margin: 0 0.2rem; }

/* Theme toggle */
.theme-toggle {
  background: none;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--text-primary);
  transition: all var(--transition);
  display: flex;
  align-items: center;
}
.theme-toggle:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

/* --- Layout --- */
.page-layout {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  gap: 2rem;
}

.content-main {
  flex: 1;
  max-width: var(--content-max-width);
  min-width: 0;
}

/* --- TOC Sidebar --- */
.toc-sidebar {
  width: var(--toc-width);
  flex-shrink: 0;
  position: sticky;
  top: calc(var(--header-height) + 2rem);
  max-height: calc(100vh - var(--header-height) - 4rem);
  overflow-y: auto;
  font-size: 0.85rem;
}

.toc-sidebar h4 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.toc-sidebar ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-sidebar li {
  margin-bottom: 0.25rem;
}

.toc-sidebar a {
  display: block;
  padding: 0.3rem 0.75rem;
  color: var(--text-secondary);
  border-left: 2px solid transparent;
  border-radius: 0 4px 4px 0;
  transition: all var(--transition);
  text-decoration: none;
  line-height: 1.4;
}

.toc-sidebar a:hover {
  color: var(--accent);
  background: var(--accent-light);
  text-decoration: none;
}

.toc-sidebar a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
  background: var(--accent-light);
}

.toc-sidebar .toc-h3 {
  padding-left: 1.5rem;
  font-size: 0.8rem;
}

/* --- Index Page: Hero --- */
.hero {
  text-align: center;
  padding: 3rem 1rem;
  max-width: 700px;
  margin: 0 auto;
}

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

.hero .subtitle {
  font-size: 1.2rem;
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
}

.hero .description {
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* --- Topics Grid --- */
.topics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.25rem;
  max-width: 1000px;
  margin: 2rem auto;
  padding: 0 1rem;
}

.topic-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 1.5rem;
  text-decoration: none;
  color: var(--text-primary);
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.topic-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  border-color: var(--accent);
  background: var(--bg-card-hover);
  text-decoration: none;
  color: var(--text-primary);
}

.topic-card .topic-number {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.topic-card .topic-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  line-height: 1.3;
}

.topic-card .topic-desc {
  font-size: 0.9rem;
  color: var(--text-secondary);
  flex: 1;
  margin-bottom: 1rem;
}

.topic-card .topic-progress {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

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

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* --- Content Components --- */

/* Definition box */
.definition {
  background: var(--def-bg);
  border-left: 4px solid var(--def-border);
  border-radius: 0 8px 8px 0;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.definition .label {
  font-weight: 700;
  color: var(--def-text);
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
}

/* Theorem box */
.theorem {
  background: var(--thm-bg);
  border-left: 4px solid var(--thm-border);
  border-radius: 0 8px 8px 0;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.theorem .label {
  font-weight: 700;
  color: var(--thm-text);
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
}

/* Remark box */
.remark {
  background: var(--remark-bg);
  border-left: 4px solid var(--remark-border);
  border-radius: 0 8px 8px 0;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.remark .label {
  font-weight: 700;
  color: var(--remark-text);
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
}

/* Warning box */
.warning {
  background: var(--warning-bg);
  border-left: 4px solid var(--warning-border);
  border-radius: 0 8px 8px 0;
  padding: 1.25rem 1.5rem;
  margin: 1.5rem 0;
}

.warning .label {
  font-weight: 700;
  color: var(--warning-text);
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
}

/* --- Example Card --- */
.example {
  background: var(--bg-content);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  margin: 1.5rem 0;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.example-header {
  background: var(--example-bg);
  border-bottom: 1px solid var(--border-color);
  padding: 1rem 1.5rem;
}

.example-header .label {
  font-weight: 700;
  color: var(--example-text);
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
}

.example-problem {
  padding: 1.25rem 1.5rem;
}

/* Solution */
.solution {
  border-top: 1px solid var(--border-color);
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.4s ease;
}

.solution.open {
  max-height: none;
}

.solution-inner {
  padding: 1.25rem 1.5rem;
}

/* Solution steps */
.step {
  background: var(--step-bg);
  border-left: 3px solid var(--step-border);
  border-radius: 0 6px 6px 0;
  padding: 1rem 1.25rem;
  margin: 1rem 0;
}

.step .step-label {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.step.hidden-step {
  display: none;
}

.step.revealed {
  display: block;
  animation: fadeInStep 0.3s ease;
}

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

/* Toggle button */
.btn-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font-body);
  margin: 0.75rem 1.5rem;
}

.btn-toggle:hover {
  background: var(--accent-dark);
  transform: translateY(-1px);
}

.btn-toggle.active {
  background: var(--text-muted);
}

.btn-next-step {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.85rem;
  background: transparent;
  color: var(--accent);
  border: 2px solid var(--accent);
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font-body);
  margin: 0.5rem 0;
}

.btn-next-step:hover {
  background: var(--accent);
  color: #fff;
}

.btn-next-step:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-show-all {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font-body);
  margin-bottom: 1rem;
}

.btn-show-all:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* --- Exercise --- */
.exercise {
  background: var(--bg-content);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  margin: 1rem 0;
  transition: border-color var(--transition);
}

.exercise:hover {
  border-color: var(--accent);
}

.exercise-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
}

.exercise-checkbox {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  margin-top: 0.2rem;
  accent-color: var(--accent);
  cursor: pointer;
}

.exercise-content { flex: 1; }

.exercise-answer {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  margin-top: 0;
}

.exercise-answer.open {
  max-height: none;
  margin-top: 0.75rem;
}

.exercise-answer-inner {
  padding: 1rem;
  background: var(--step-bg);
  border-radius: 6px;
  border-left: 3px solid var(--accent);
}

.btn-answer {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.25rem 0;
  font-weight: 600;
  font-family: var(--font-body);
  margin-top: 0.5rem;
}
.btn-answer:hover { text-decoration: underline; }

.exercise.completed {
  border-color: var(--thm-border);
  background: var(--thm-bg);
}

/* --- Summary section --- */
.summary-box {
  background: var(--bg-content);
  border: 2px solid var(--accent);
  border-radius: 10px;
  padding: 1.5rem 2rem;
  margin: 2rem 0;
}

.summary-box h3 {
  color: var(--accent);
  margin-top: 0;
}

.formula-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.formula-list li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border-light);
}

.formula-list li:last-child { border-bottom: none; }

/* Common mistakes */
.mistakes-list {
  list-style: none;
  padding: 0;
}

.mistakes-list li {
  padding: 0.5rem 0 0.5rem 1.5rem;
  position: relative;
}

.mistakes-list li::before {
  content: "\26A0";
  position: absolute;
  left: 0;
}

/* --- Topic Navigation (bottom) --- */
.topic-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 2px solid var(--border-color);
  gap: 1rem;
}

.topic-nav a {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  text-decoration: none;
  transition: all var(--transition);
  font-weight: 600;
  max-width: 45%;
}

.topic-nav a:hover {
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: var(--shadow-md);
  text-decoration: none;
}

.topic-nav .nav-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 400;
}

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

/* --- KaTeX overrides --- */
.katex-display {
  margin: 1.5rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.5rem 0;
}

.katex { font-size: 1.1em; }

.math-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 0.25rem;
}

/* GEM step display */
.gem-chain {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0;
  overflow-x: auto;
  padding-bottom: 0.25rem;
}

.gem-arrow {
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
  white-space: nowrap;
}

/* --- Footer --- */
.site-footer {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border-color);
  margin-top: 3rem;
}

/* --- Mobile TOC toggle --- */
.toc-mobile-toggle {
  display: none;
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-primary);
  font-family: var(--font-body);
  margin-bottom: 1rem;
  text-align: left;
}

/* --- Responsive --- */
@media (max-width: 1024px) {
  .toc-sidebar {
    display: none;
  }

  .toc-mobile-toggle {
    display: block;
  }

  .toc-sidebar.mobile-open {
    display: block;
    position: static;
    width: 100%;
    max-height: none;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
    margin-bottom: 1.5rem;
  }

  .page-layout {
    flex-direction: column;
  }
}

@media (max-width: 768px) {
  :root {
    --font-size-base: 1rem;
  }

  h1 { font-size: 1.6rem; }
  h2 { font-size: 1.3rem; }
  h3 { font-size: 1.1rem; }

  .hero h1 { font-size: 1.8rem; }
  .hero .subtitle { font-size: 1rem; }

  .topics-grid {
    grid-template-columns: 1fr;
  }

  .page-layout {
    padding: 1rem;
  }

  .definition, .theorem, .remark, .warning {
    padding: 1rem;
  }

  .example-problem, .solution-inner, .btn-toggle {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .topic-nav {
    flex-direction: column;
  }

  .topic-nav a {
    max-width: 100%;
    width: 100%;
    justify-content: center;
  }

  .header-inner {
    padding: 0 1rem;
  }

  .breadcrumb { font-size: 0.8rem; }

  .katex { font-size: 1em; }
}

/* --- Intro section --- */
.intro {
  background: var(--accent-light);
  border-radius: 10px;
  padding: 1.5rem 2rem;
  margin-bottom: 2rem;
  border: 1px solid var(--border-color);
}

.intro h2 {
  border-bottom: none;
  margin-top: 0;
  color: var(--accent);
  font-size: 1.2rem;
}

.prereqs {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-top: 0.75rem;
}

.prereqs strong { color: var(--text-primary); }

/* --- Algorithm steps --- */
.algorithm {
  background: var(--bg-content);
  border: 2px solid var(--accent);
  border-radius: 10px;
  padding: 1.5rem;
  margin: 1.5rem 0;
}

.algorithm h4 {
  color: var(--accent);
  margin-top: 0;
}

.algorithm ol {
  margin-left: 1.25rem;
}

.algorithm li {
  margin-bottom: 0.5rem;
  padding-left: 0.25rem;
}

/* --- Code blocks (Prism.js) --- */
.content-main pre {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.25rem;
  margin: 1rem 0;
  overflow-x: auto;
  font-size: 0.88rem;
  line-height: 1.55;
}

.content-main pre code {
  font-family: var(--font-mono);
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

/* --- Content tables --- */
.content-main table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.95rem;
}
.content-main th,
.content-main td {
  padding: 0.6rem 1rem;
  border: 1px solid var(--border-color);
  text-align: left;
}
.content-main th {
  background: var(--accent-light);
  font-weight: 700;
  color: var(--accent);
}
.content-main tr:nth-child(even) td {
  background: var(--step-bg);
}

/* --- Print --- */
@media print {
  .site-header, .toc-sidebar, .toc-mobile-toggle,
  .theme-toggle, .btn-toggle, .btn-next-step,
  .btn-answer, .btn-show-all, .topic-nav { display: none; }

  .solution, .exercise-answer { max-height: none !important; }
  .hidden-step { display: block !important; }

  body { font-size: 11pt; color: #000; background: #fff; }
}
