/* ============================================================
   AI-Fun — clean-academic design system
   Type:  Newsreader (prose/display) · IBM Plex Sans (UI) · IBM Plex Mono (code)
   Color: warm paper, ink text, single indigo accent + semantic status hues
   ============================================================ */

:root {
  --paper:      oklch(0.987 0.004 85);
  --paper-2:    oklch(0.972 0.005 85);
  --paper-3:    oklch(0.958 0.006 85);
  --code-bg:    oklch(0.964 0.006 85);

  --ink:        oklch(0.27 0.012 60);
  --ink-soft:   oklch(0.44 0.012 60);
  --ink-faint:  oklch(0.60 0.010 60);

  --rule:       oklch(0.90 0.006 75);
  --rule-soft:  oklch(0.93 0.005 80);

  --accent:     oklch(0.47 0.11 264);
  --accent-2:   oklch(0.55 0.10 264);
  --accent-bg:  oklch(0.95 0.028 264);

  --ok:         oklch(0.56 0.10 150);
  --ok-bg:      oklch(0.95 0.04 150);
  --warn:       oklch(0.62 0.12 65);
  --warn-bg:    oklch(0.95 0.05 75);

  --serif: "Newsreader", Georgia, "Times New Roman", serif;
  --sans:  "IBM Plex Sans", system-ui, -apple-system, sans-serif;
  --mono:  "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;

  --reading: 720px;
  --rail-w: 272px;
  --tutor-w: 372px;
  --header-h: 60px;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr) var(--tutor-w);
  min-height: 100vh;
}

/* ---------- Header ---------- */
.header {
  grid-column: 1 / -1;
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 24px;
  background: color-mix(in oklch, var(--paper) 86%, transparent);
  backdrop-filter: saturate(1.1) blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 21px;
  letter-spacing: -0.01em;
  color: var(--ink);
  text-decoration: none;
}
.brand .dot { color: var(--accent); }
.brand small {
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.header-spacer { flex: 1; }

/* kernel status pill + launch */
.kernel {
  display: flex;
  align-items: center;
  gap: 10px;
}
.kstatus {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  padding: 6px 11px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--paper-2);
  white-space: nowrap;
}
.kstatus .led {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--ink-faint);
  box-shadow: 0 0 0 0 transparent;
}
.kstatus[data-state="idle"] .led    { background: var(--ink-faint); }
.kstatus[data-state="busy"] .led     { background: var(--warn); animation: pulse 1.1s infinite; }
.kstatus[data-state="ready"] .led    { background: var(--ok); }
.kstatus[data-state="error"] .led    { background: oklch(0.55 0.18 28); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

.btn {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 550;
  letter-spacing: 0.01em;
  border: 1px solid var(--accent);
  color: #fff;
  background: var(--accent);
  padding: 8px 15px;
  border-radius: 7px;
  cursor: pointer;
  transition: background .15s, transform .05s, opacity .15s;
  white-space: nowrap;
}
.btn:hover { background: var(--accent-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost {
  background: transparent; color: var(--accent); border-color: var(--rule);
}
.btn.ghost:hover { background: var(--accent-bg); border-color: var(--accent); }

/* ---------- Contents rail ---------- */
.rail {
  border-right: 1px solid var(--rule);
  padding: 30px 18px 60px 26px;
  position: sticky;
  top: var(--header-h);
  align-self: start;
  height: calc(100vh - var(--header-h));
  overflow-y: auto;
}
.rail .label {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 14px 6px;
}
.toc { list-style: none; margin: 0; padding: 0; }
.toc li { margin: 0; }
.toc a {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 13px;
  line-height: 1.4;
  border-left: 2px solid transparent;
  margin-left: -2px;
  transition: color .12s, background .12s, border-color .12s;
}
.toc a:hover { background: var(--paper-2); color: var(--ink); }
.toc a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-bg);
  font-weight: 550;
}
.toc a .n {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-faint);
  min-width: 16px;
  padding-top: 1px;
}
.toc a.active .n { color: var(--accent); }

.rail-foot {
  margin-top: 26px;
  padding-top: 18px;
  border-top: 1px solid var(--rule-soft);
  font-size: 12px;
  color: var(--ink-faint);
  line-height: 1.5;
}

/* ---------- Reading column ---------- */
.reading {
  padding: 0 40px 140px;
  min-width: 0;
}
.col {
  max-width: var(--reading);
  margin: 0 auto;
}

/* hero */
.hero { padding: 56px 0 30px; border-bottom: 1px solid var(--rule); margin-bottom: 14px; }
.hero .kicker {
  font-family: var(--mono);
  font-size: 11.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  display: block;
  margin-bottom: 16px;
}
.hero h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: clamp(34px, 4.5vw, 50px);
  line-height: 1.08;
  letter-spacing: -0.02em;
  margin: 0 0 18px;
  color: var(--ink);
  text-wrap: balance;
}
.hero .goal {
  font-family: var(--serif);
  font-size: 21px;
  line-height: 1.5;
  color: var(--ink-soft);
  font-style: italic;
  margin: 0 0 28px;
  max-width: 60ch;
}
.howto {
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: 10px;
  padding: 18px 22px 18px 20px;
}
.howto .label {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 10px;
}
.howto ol { margin: 0; padding-left: 20px; }
.howto li { font-size: 14.5px; color: var(--ink-soft); margin: 5px 0; }
.howto code { font-family: var(--mono); font-size: 0.88em; }

/* prose (markdown blocks) */
.lesson-block { padding: 26px 0; }
.prose h2 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 32px;
  line-height: 1.15;
  letter-spacing: -0.015em;
  margin: 22px 0 16px;
  color: var(--ink);
  display: flex;
  align-items: baseline;
  gap: 16px;
}
.prose h2 .sec-num {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 500;
  color: var(--accent);
  letter-spacing: 0.02em;
  padding-top: 6px;
}
.prose h3 {
  font-family: var(--serif); font-weight: 600; font-size: 23px;
  margin: 24px 0 10px; letter-spacing: -0.01em;
}
.prose h4 {
  font-family: var(--sans); font-weight: 600; font-size: 14px;
  letter-spacing: 0.02em; text-transform: uppercase; color: var(--ink-soft);
  margin: 20px 0 8px;
}
.prose p {
  font-family: var(--serif);
  font-size: 18.5px;
  line-height: 1.62;
  color: var(--ink);
  margin: 14px 0;
  text-wrap: pretty;
}
.prose p.key {
  border-left: 3px solid var(--accent);
  padding: 2px 0 2px 18px;
  color: var(--ink);
}
.prose ul, .prose ol { font-family: var(--serif); font-size: 18px; line-height: 1.6; color: var(--ink); padding-left: 26px; }
.prose ol.tight li, .prose ul li { margin: 7px 0; }
.prose ol.steps { counter-reset: step; list-style: none; padding-left: 0; }
.prose ol.steps li {
  position: relative; padding-left: 44px; margin: 10px 0; font-size: 17.5px;
}
.prose ol.steps li::before {
  counter-increment: step; content: counter(step);
  position: absolute; left: 0; top: 2px;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--accent-bg); color: var(--accent);
  font-family: var(--mono); font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.prose em { font-style: italic; }
.prose strong { font-weight: 650; }
.prose code {
  font-family: var(--mono); font-size: 0.84em;
  background: var(--code-bg); border: 1px solid var(--rule-soft);
  padding: 1px 5px; border-radius: 4px; color: var(--accent);
}

.note-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; }
.note-pair h4 { margin-top: 4px; }
.note-pair ul { font-size: 16px; }
.note-pair p { font-size: 16px; }
@media (max-width: 720px) { .note-pair { grid-template-columns: 1fr; gap: 8px; } }

pre.ascii {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: 9px;
  padding: 18px 20px;
  overflow-x: auto;
  color: var(--ink-soft);
  white-space: pre;
}

.table-wrap { overflow-x: auto; margin: 16px 0; }
.prose table { border-collapse: collapse; width: 100%; font-family: var(--sans); }
.prose th, .prose td {
  text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--rule);
  font-size: 14px; vertical-align: top;
}
.prose th {
  font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink-faint); font-weight: 600; border-bottom: 1.5px solid var(--rule);
}
.prose td strong { color: var(--accent); }

.next-card {
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: 12px;
  padding: 26px 28px;
  margin-top: 12px;
}
.next-card .kicker {
  font-family: var(--mono); font-size: 11px; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--accent);
}
.next-card h3 { margin: 8px 0 6px; font-family: var(--serif); font-size: 26px; }
.next-card p { font-size: 16.5px; margin: 8px 0; }
.next-card .muted { color: var(--ink-faint); font-size: 14.5px; font-style: italic; }

/* ---------- Code cells ---------- */
.cell.code-cell {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  margin: 22px 0;
  border: 1px solid var(--rule);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}
.cell-gutter {
  border-right: 1px solid var(--rule);
  padding: 14px 0;
  text-align: center;
  background: var(--paper-2);
}
.cell-gutter .prompt {
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.08em;
  color: var(--ink-faint); text-transform: uppercase;
}
.cell-main { min-width: 0; }
pre.code-pre {
  margin: 0;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.62;
  padding: 14px 18px;
  color: var(--ink);
  white-space: pre;
  overflow-x: auto;
  tab-size: 4;
}

/* hint shown on code cells before kernel launch */
.cell-hint {
  grid-column: 2;
  font-family: var(--mono); font-size: 11px; color: var(--ink-faint);
  padding: 8px 18px; border-top: 1px dashed var(--rule);
  background: var(--paper-2); line-height: 1.5;
}
.cell-hint a { color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.cell.code-cell.live .cell-hint { display: none; }

/* ---------- Thebe overrides (post-launch) ---------- */
.thebe-cell { margin: 0 !important; }
.thebe-controls {
  padding: 8px 14px !important;
  border-top: 1px solid var(--rule) !important;
  background: var(--paper-2) !important;
  font-family: var(--mono) !important; font-size: 11px !important;
  color: var(--ink-faint) !important;
}
.thebe-button, button.thebe-button {
  font-family: var(--sans) !important;
  font-size: 12px !important; font-weight: 550 !important;
  background: var(--accent) !important; color: #fff !important;
  border: 1px solid var(--accent) !important; border-radius: 6px !important;
  padding: 5px 12px !important; cursor: pointer !important; margin-right: 8px !important;
}
.thebe-button.thebe-run-button:hover { background: var(--accent-2) !important; }
.thebe-button:not(.thebe-run-button) {
  background: transparent !important; color: var(--ink-soft) !important;
  border-color: var(--rule) !important;
}
.CodeMirror {
  font-family: var(--mono) !important;
  font-size: 13px !important; line-height: 1.62 !important;
  background: var(--code-bg) !important; color: var(--ink) !important;
  height: auto !important; padding: 6px 0 !important;
}
.CodeMirror-gutters { background: var(--paper-2) !important; border-right: 1px solid var(--rule) !important; }
.cm-s-default .cm-keyword { color: oklch(0.5 0.16 300) !important; }
.cm-s-default .cm-def, .cm-s-default .cm-variable-2 { color: var(--accent) !important; }
.cm-s-default .cm-string { color: oklch(0.5 0.12 150) !important; }
.cm-s-default .cm-comment { color: var(--ink-faint) !important; font-style: italic; }
.cm-s-default .cm-number { color: oklch(0.55 0.15 40) !important; }
.thebe-output {
  padding: 12px 18px !important;
  border-top: 1px solid var(--rule) !important;
  background: var(--paper) !important;
  font-family: var(--mono) !important; font-size: 12.5px !important;
  line-height: 1.5 !important; overflow-x: auto !important;
}
.thebe-output pre { font-family: var(--mono) !important; white-space: pre-wrap; margin: 0; }
.thebe-output img { max-width: 100%; height: auto; border-radius: 6px; }

/* ---------- Reflection cells ---------- */
.reflection {
  margin: 26px 0;
  border: 1px solid var(--accent);
  border-radius: 11px;
  background: linear-gradient(0deg, var(--accent-bg) 0%, color-mix(in oklch, var(--accent-bg) 35%, var(--paper)) 100%);
  padding: 20px 24px 22px;
}
.reflection .r-label {
  font-family: var(--mono); font-size: 10.5px; letter-spacing: 0.16em;
  text-transform: uppercase; color: var(--accent); font-weight: 600;
  display: flex; align-items: center; gap: 8px; margin-bottom: 14px;
}
.reflection .r-label::before {
  content: "✎"; font-size: 13px; transform: translateY(1px);
}
.reflect-item { margin: 14px 0; }
.reflect-item:first-of-type { margin-top: 0; }
.reflect-item .q {
  font-family: var(--serif); font-size: 17.5px; font-style: italic;
  color: var(--ink); line-height: 1.5; margin-bottom: 8px;
}
.reflect-item textarea {
  width: 100%;
  font-family: var(--serif); font-size: 16px; line-height: 1.55;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 11px 13px;
  resize: vertical;
  min-height: 64px;
}
.reflect-item textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-bg);
}
.reflect-item textarea::placeholder { color: var(--ink-faint); font-style: italic; }
.reflect-actions { margin-top: 6px; display: flex; gap: 10px; align-items: center; }
.reflect-actions .btn { font-size: 12px; padding: 6px 12px; }
.reflect-feedback {
  margin-top: 12px; padding: 14px 16px;
  background: var(--paper); border: 1px solid var(--rule); border-radius: 8px;
  font-family: var(--serif); font-size: 16px; line-height: 1.55; color: var(--ink);
  display: none;
}
.reflect-feedback.show { display: block; }
.reflect-feedback .ff-label {
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--accent); margin-bottom: 7px; display: block;
}
.reflect-feedback.loading { color: var(--ink-faint); font-style: italic; }

/* ---------- Tutor panel ---------- */
.tutor {
  border-left: 1px solid var(--rule);
  background: var(--paper-2);
  position: sticky;
  top: var(--header-h);
  height: calc(100vh - var(--header-h));
  display: flex;
  flex-direction: column;
}
.tutor-head {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--rule);
}
.tutor-head h3 {
  font-family: var(--serif); font-size: 19px; font-weight: 600; margin: 0;
  display: flex; align-items: center; gap: 9px;
}
.tutor-head .badge {
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--accent); background: var(--accent-bg); padding: 3px 7px; border-radius: 5px;
}
.tutor-head p { margin: 6px 0 0; font-size: 12.5px; color: var(--ink-faint); line-height: 1.45; }

.tutor-log {
  flex: 1; overflow-y: auto; padding: 18px 20px;
  display: flex; flex-direction: column; gap: 14px;
}
.msg { max-width: 92%; }
.msg .who {
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-faint); margin-bottom: 4px;
}
.msg .bubble {
  font-family: var(--serif); font-size: 15px; line-height: 1.55; color: var(--ink);
}
.msg.user { align-self: flex-end; }
.msg.user .bubble {
  background: var(--accent); color: #fff; padding: 9px 13px; border-radius: 12px 12px 3px 12px;
  font-family: var(--sans); font-size: 14px;
}
.msg.user .who { text-align: right; }
.msg.bot .bubble {
  background: var(--paper); border: 1px solid var(--rule); padding: 11px 14px;
  border-radius: 12px 12px 12px 3px;
}
.msg.bot .bubble p { margin: 0 0 8px; }
.msg.bot .bubble p:last-child { margin-bottom: 0; }
.msg.bot .bubble code { font-family: var(--mono); font-size: 0.85em; background: var(--code-bg); padding: 1px 4px; border-radius: 3px; color: var(--accent); }
.msg.bot .bubble strong { font-weight: 650; }
.msg.bot.thinking .bubble { color: var(--ink-faint); font-style: italic; }

.tutor-suggest {
  padding: 0 20px 12px; display: flex; flex-wrap: wrap; gap: 7px;
}
.chip {
  font-family: var(--sans); font-size: 12px; color: var(--ink-soft);
  background: var(--paper); border: 1px solid var(--rule); border-radius: 999px;
  padding: 6px 12px; cursor: pointer; transition: all .12s; line-height: 1.2;
}
.chip:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-bg); }

.tutor-input {
  border-top: 1px solid var(--rule); padding: 14px 16px;
  display: flex; gap: 9px; align-items: flex-end;
}
.tutor-input textarea {
  flex: 1; resize: none; font-family: var(--sans); font-size: 14px; line-height: 1.45;
  border: 1px solid var(--rule); border-radius: 9px; padding: 10px 12px;
  background: var(--paper); color: var(--ink); max-height: 130px; min-height: 42px;
}
.tutor-input textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.tutor-send {
  border: none; background: var(--accent); color: #fff; width: 42px; height: 42px;
  border-radius: 9px; cursor: pointer; flex: none; display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.tutor-send:hover { background: var(--accent-2); }
.tutor-send:disabled { opacity: .45; cursor: not-allowed; }

/* mobile tutor toggle */
.tutor-fab { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .app { grid-template-columns: var(--rail-w) minmax(0,1fr); }
  .tutor {
    position: fixed; top: 0; right: 0; height: 100vh; width: min(400px, 90vw);
    z-index: 80; transform: translateX(100%); transition: transform .25s ease;
    box-shadow: -10px 0 40px -20px oklch(0.3 0.02 60 / 0.4);
  }
  .tutor.open { transform: translateX(0); }
  .tutor-close { display: inline-flex !important; }
  .tutor-fab {
    display: flex; align-items: center; gap: 8px;
    position: fixed; bottom: 22px; right: 22px; z-index: 70;
    background: var(--accent); color: #fff; border: none; cursor: pointer;
    font-family: var(--sans); font-size: 14px; font-weight: 550;
    padding: 13px 18px; border-radius: 999px;
    box-shadow: 0 8px 24px -8px oklch(0.47 0.11 264 / 0.6);
  }
}
@media (min-width: 1181px) { .tutor-close { display: none; } }
.tutor-close {
  margin-left: auto; background: transparent; border: 1px solid var(--rule);
  width: 30px; height: 30px; border-radius: 7px; cursor: pointer; color: var(--ink-soft);
  align-items: center; justify-content: center;
}

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .rail { display: none; }
  .reading { padding: 0 22px 120px; }
}
