/* ======================================================================
   Math for ML — Design system
   ====================================================================== */

:root {
  /* Light theme defaults */
  --bg:           #fafaf7;
  --bg-elev-1:    #ffffff;
  --bg-elev-2:    #f3f2ec;
  --bg-elev-3:    #ebe9df;
  --border:       #e3e1d6;
  --border-strong:#cfccbd;

  --ink:          #1c1d1f;
  --ink-soft:     #3a3b3f;
  --ink-mute:     #6b6e75;
  --ink-faint:    #9a9da4;

  --accent:       #2f6b4f;
  --accent-soft:  #d8ebd9;
  --accent-deep:  #1f4a36;

  --warn:         #b86e1e;
  --warn-soft:    #f4e3c9;

  --danger:       #b54b4b;
  --danger-soft:  #f4d8d8;

  --info:         #3960a8;
  --info-soft:    #d6e2f4;

  --code-bg:      #2b2e33;
  --code-ink:     #e7e6e0;

  --shadow-sm:    0 1px 2px rgba(20,20,20,.04), 0 1px 3px rgba(20,20,20,.06);
  --shadow-md:    0 6px 18px rgba(20,20,20,.06), 0 2px 6px rgba(20,20,20,.04);
  --shadow-lg:    0 22px 60px rgba(20,20,20,.08), 0 4px 12px rgba(20,20,20,.05);

  --serif:        "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Liberation Serif", serif;
  --sans:         "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono:         "JetBrains Mono", "SF Mono", "Cascadia Code", "Roboto Mono", Menlo, Consolas, monospace;

  --radius-sm:    6px;
  --radius-md:    10px;
  --radius-lg:    14px;
  --radius-xl:    20px;

  --topbar-h:     58px;
  --sidebar-w:    320px;
  --content-max:  860px;

  --duration:     180ms;
  --ease:         cubic-bezier(.2,.8,.2,1);
}

/* Dark theme */
[data-theme="dark"] {
  --bg:           #14161a;
  --bg-elev-1:    #1b1e23;
  --bg-elev-2:    #21252b;
  --bg-elev-3:    #2a2f36;
  --border:       #2d3138;
  --border-strong:#3e434b;

  --ink:          #ecedef;
  --ink-soft:     #d1d3d7;
  --ink-mute:     #a0a4ad;
  --ink-faint:    #6e7280;

  --accent:       #6cc299;
  --accent-soft:  #243a31;
  --accent-deep:  #b6dfc8;

  --warn:         #e0a45a;
  --warn-soft:    #3a2f1b;

  --danger:       #e07b7b;
  --danger-soft:  #3a2222;

  --info:         #7eaaff;
  --info-soft:    #1f2a44;

  --code-bg:      #11131a;
  --code-ink:     #d8dde5;

  --shadow-sm:    0 1px 2px rgba(0,0,0,.4), 0 1px 3px rgba(0,0,0,.3);
  --shadow-md:    0 6px 18px rgba(0,0,0,.4), 0 2px 6px rgba(0,0,0,.25);
  --shadow-lg:    0 22px 60px rgba(0,0,0,.5), 0 4px 12px rgba(0,0,0,.35);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

body {
  min-height: 100vh;
  overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-decoration-thickness: 1.5px; text-underline-offset: 3px; }

button { font-family: inherit; }

/* ----------------------------------------------------------------------
   App shell
   ---------------------------------------------------------------------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "topbar topbar"
    "sidebar content";
  min-height: 100vh;
  transition: grid-template-columns .25s var(--ease);
}
.app.sidebar-collapsed {
  grid-template-columns: 0 1fr;
}
.app.sidebar-collapsed .sidebar {
  transform: translateX(-100%);
  transition: transform .25s var(--ease);
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  background: var(--bg-elev-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
  backdrop-filter: blur(8px);
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  font-family: var(--serif);
  font-size: 28px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  width: 36px; height: 36px;
  border-radius: 8px;
  display: grid; place-items: center;
  line-height: 1;
}
.brand-text { line-height: 1.1; }
.brand-title { font-weight: 600; font-size: 14px; letter-spacing: -.005em; }
.brand-sub { font-size: 11px; color: var(--ink-mute); margin-top: 2px; }

.icon-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--ink-soft);
  width: 36px; height: 36px;
  border-radius: 8px;
  cursor: pointer;
  display: grid; place-items: center;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.icon-btn:hover { background: var(--bg-elev-2); color: var(--ink); }

.progress-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--ink-mute);
}
.progress-pill-value {
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

/* ----------------------------------------------------------------------
   Sidebar
   ---------------------------------------------------------------------- */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-elev-1);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  display: flex;
  flex-direction: column;
}

.root-home-link {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  align-self: flex-start;
  margin: 12px 12px 0;
  padding: 5px 9px;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--ink-faint);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.root-home-link:hover {
  background: var(--bg-elev-2);
  border-color: var(--border);
  color: var(--ink-mute);
  text-decoration: none;
}

.nav-tree {
  flex: 1;
  padding: 10px 12px 18px;
  font-size: 13.5px;
}

.nav-chapter { margin-bottom: 4px; }

.nav-chapter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  color: var(--ink-soft);
  font-weight: 600;
  letter-spacing: -.005em;
  transition: background var(--duration) var(--ease);
}
.nav-chapter-header:hover { background: var(--bg-elev-2); color: var(--ink); }
.nav-chapter-header .chapter-num {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px;
  font-size: 11px; font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 5px;
  margin-right: 10px;
  flex-shrink: 0;
}
.nav-chapter-header .chapter-name { flex: 1; }
.nav-chapter-header .caret {
  width: 14px; height: 14px;
  transition: transform var(--duration) var(--ease);
  color: var(--ink-faint);
  flex-shrink: 0;
}
.nav-chapter[data-collapsed="true"] .caret { transform: rotate(-90deg); }
.nav-chapter[data-collapsed="true"] .nav-sections { display: none; }

.nav-sections {
  margin-left: 32px;
  padding-left: 10px;
  border-left: 1px dashed var(--border);
  margin-top: 2px;
  margin-bottom: 8px;
}

.nav-section {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 5px;
  color: var(--ink-mute);
  cursor: pointer;
  font-size: 13px;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
  text-decoration: none;
}
.nav-section:hover { background: var(--bg-elev-2); color: var(--ink); text-decoration: none; }
.nav-section.active { background: var(--accent-soft); color: var(--accent-deep); font-weight: 600; }

.nav-status {
  width: 10px; height: 10px; border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  flex-shrink: 0;
}
.nav-status.complete {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.nav-status.in-progress {
  background: var(--warn);
  border-color: var(--warn);
}

.nav-subsections {
  margin-left: 22px;
  padding-left: 8px;
  border-left: 1px dotted var(--border);
}

.nav-subsection {
  font-size: 12.5px;
  padding: 5px 8px;
  border-radius: 4px;
  color: var(--ink-mute);
  cursor: pointer;
  display: block;
  text-decoration: none;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.nav-subsection:hover { background: var(--bg-elev-2); color: var(--ink); text-decoration: none; }
.nav-subsection.active { color: var(--accent-deep); font-weight: 600; }

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
}

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--ink-mute);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all var(--duration) var(--ease);
}
.ghost-btn:hover { color: var(--ink); border-color: var(--border-strong); background: var(--bg-elev-2); }

/* ----------------------------------------------------------------------
   Content
   ---------------------------------------------------------------------- */
.content {
  grid-area: content;
  padding: 32px 48px 96px;
  overflow-x: hidden;
  min-width: 0;
}

.content-inner {
  max-width: var(--content-max);
  margin: 0 auto;
}

/* Home / welcome */
.home {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 8px;
}

.home-hero {
  text-align: center;
  padding: 60px 20px 50px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 48px;
}
.home-hero-eyebrow {
  font-size: 12.5px;
  letter-spacing: .15em;
  text-transform: uppercase;
  color: var(--ink-mute);
  font-weight: 600;
  margin-bottom: 16px;
}
.home-hero-title {
  font-family: var(--serif);
  font-size: clamp(38px, 5vw, 64px);
  line-height: 1.05;
  margin: 0 0 22px;
  letter-spacing: -.02em;
  font-weight: 600;
}
.home-hero-sub {
  font-size: 18px;
  color: var(--ink-soft);
  max-width: 680px;
  margin: 0 auto 32px;
  line-height: 1.55;
}
.home-hero-cta {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px;
  margin-bottom: 64px;
}
.home-card {
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.home-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}
.home-card-icon {
  font-size: 28px;
  color: var(--accent);
  margin-bottom: 12px;
  line-height: 1;
}
.home-card h3 {
  font-size: 16px;
  margin: 0 0 8px;
  font-weight: 600;
  letter-spacing: -.005em;
}
.home-card p {
  font-size: 14px;
  color: var(--ink-mute);
  line-height: 1.55;
  margin: 0;
}

.home-toc h2 {
  font-family: var(--serif);
  font-size: 28px;
  letter-spacing: -.01em;
  margin-bottom: 24px;
  font-weight: 600;
}

.toc-chapter {
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  margin-bottom: 16px;
  transition: box-shadow var(--duration) var(--ease);
}
.toc-chapter:hover { box-shadow: var(--shadow-sm); }
.toc-chapter-header {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 12px;
}
.toc-chapter-num {
  font-family: var(--serif);
  font-size: 32px;
  color: var(--accent);
  font-weight: 600;
  line-height: 1;
}
.toc-chapter-title {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -.005em;
}
.toc-chapter-tagline {
  color: var(--ink-mute);
  margin-bottom: 14px;
  font-size: 14.5px;
}
.toc-section-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 6px;
}
.toc-section-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--ink-soft);
  text-decoration: none;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.toc-section-item:hover { background: var(--bg-elev-2); color: var(--ink); text-decoration: none; }

/* ----------------------------------------------------------------------
   Section view
   ---------------------------------------------------------------------- */
.section {
  max-width: var(--content-max);
  margin: 0 auto;
}

.section-header {
  margin-bottom: 28px;
  padding-bottom: 22px;
  border-bottom: 1px solid var(--border);
}

.section-breadcrumbs {
  font-size: 12.5px;
  color: var(--ink-mute);
  margin-bottom: 12px;
  letter-spacing: .02em;
}
.section-breadcrumbs span { color: var(--ink-faint); }
.section-breadcrumbs a { color: var(--ink-mute); }

.section-title {
  font-family: var(--serif);
  font-size: clamp(30px, 3.6vw, 44px);
  line-height: 1.1;
  letter-spacing: -.015em;
  font-weight: 600;
  margin: 0 0 12px;
}

.section-tagline {
  font-size: 17px;
  color: var(--ink-soft);
  margin: 0 0 16px;
  line-height: 1.5;
  max-width: 720px;
}

.section-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  background: var(--bg-elev-2);
  color: var(--ink-mute);
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.section-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 28px;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 10px 16px;
  font-size: 14px;
  color: var(--ink-mute);
  font-weight: 500;
  cursor: pointer;
  transition: color var(--duration) var(--ease), border-color var(--duration) var(--ease);
  margin-bottom: -1px;
}
.tab-btn:hover { color: var(--ink); }
.tab-btn.active {
  color: var(--accent-deep);
  border-bottom-color: var(--accent);
  font-weight: 600;
}
[data-theme="dark"] .tab-btn.active { color: var(--accent-deep); }

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadein .25s var(--ease); }

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

.section-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 60px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

/* ----------------------------------------------------------------------
   Buttons
   ---------------------------------------------------------------------- */
.primary-btn, .secondary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all var(--duration) var(--ease);
  font-family: inherit;
}
.primary-btn {
  background: var(--accent);
  color: white;
}
.primary-btn:hover { background: var(--accent-deep); transform: translateY(-1px); box-shadow: var(--shadow-sm); }

.secondary-btn {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--border-strong);
}
.secondary-btn:hover { background: var(--bg-elev-2); color: var(--ink); border-color: var(--ink-mute); }

.primary-btn:disabled, .secondary-btn:disabled {
  opacity: .4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* ----------------------------------------------------------------------
   Responsive
   ---------------------------------------------------------------------- */
@media (max-width: 900px) {
  :root { --sidebar-w: 280px; }
  .content { padding: 24px 28px 80px; }
}

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas: "topbar" "content";
  }
  .app.sidebar-collapsed {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    width: 280px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform .25s var(--ease);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }
  .app.sidebar-collapsed .sidebar.open { transform: translateX(0); }
  .content { padding: 18px 16px 80px; }
  .progress-pill .progress-pill-label { display: none; }
  .home-hero { padding: 30px 0 30px; }
}
