:root {
  --bg: #faf9f7;
  --surface: #ffffff;
  --surface-2: #f3f2ee;
  --border: #e5e3dd;
  --text: #1f1d1a;
  --text-dim: #6b6864;
  --accent: #2d6cdf;
  --accent-soft: #e8f0fe;
  --done: #2e8b57;
  --done-soft: #e7f4ec;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.04);
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.55;
}

header {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  padding: 1.25rem 2rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

header h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: -0.01em;
  cursor: pointer;
  user-select: none;
}

#crumbs {
  color: var(--text-dim);
  font-size: 0.9rem;
}

#crumbs a {
  color: var(--text-dim);
  text-decoration: none;
  cursor: pointer;
}
#crumbs a:hover { color: var(--accent); }
#crumbs .sep { margin: 0 0.5rem; opacity: 0.5; }

.header-link {
  font-size: 0.85rem;
  color: var(--text-dim);
  text-decoration: none;
  cursor: pointer;
  margin-left: 0.85rem;
}
.header-link:first-of-type { margin-left: auto; }
.header-link:hover { color: var(--accent); }

.stats-widget {
  font-size: 0.78rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  margin-left: auto;
}
.stats-widget .dot { opacity: 0.5; }
.stats-widget + .header-link { margin-left: 0.85rem; }
.stats-widget + .header-link { margin-left: 0.85rem; }
header .stats-widget ~ .header-link:first-of-type { margin-left: 0.85rem; }

main {
  max-width: 1080px;
  margin: 2rem auto;
  padding: 0 2rem;
}

/* ── HOME GRID ─────────────────────────────────────────────────────────── */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease, border-color 0.1s ease;
  position: relative;
  min-height: 130px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.card:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
  box-shadow: var(--shadow);
}

.card-topic {
  font-weight: 600;
  font-size: 1.05rem;
  margin-bottom: 0.25rem;
  word-break: break-word;
}

.card-meta {
  font-size: 0.8rem;
  color: var(--text-dim);
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.card-meta .level {
  background: var(--surface-2);
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
}

.progress {
  margin-top: 0.6rem;
  background: var(--surface-2);
  border-radius: 999px;
  height: 6px;
  overflow: hidden;
}

.progress > span {
  display: block;
  height: 100%;
  background: var(--done);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 0.3rem;
}

.card .delete {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease, color 0.15s ease;
  padding: 0.15rem 0.4rem;
  line-height: 1;
}
.card:hover .delete { opacity: 1; }
.card .delete:hover { color: var(--danger); }

.card.generating {
  cursor: default;
  border-style: dashed;
}
.card.generating:hover {
  transform: none;
  border-color: var(--border);
  box-shadow: none;
}
.card.generating .card-meta .spinner {
  margin-right: 0.4rem;
  border-color: var(--accent);
  border-top-color: transparent;
}

.card.errored {
  border-color: #f5b8b1;
  background: #fff7f6;
}
.card.errored .error-tag {
  color: var(--danger);
  font-weight: 500;
}
.card.errored .error-msg {
  font-size: 0.75rem;
  color: var(--danger);
  margin-top: 0.4rem;
  line-height: 1.35;
  word-break: break-word;
}

.add-card {
  background: transparent;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 2.5rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 130px;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.add-card:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

/* ── OUTLINE ──────────────────────────────────────────────────────────── */

.outline-header {
  margin-bottom: 1.5rem;
}
.outline-header h2 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: -0.01em;
}
.outline-header .meta {
  color: var(--text-dim);
  font-size: 0.9rem;
  margin-top: 0.25rem;
}

.section {
  margin-bottom: 1.5rem;
}
.section-title {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin: 0 0 0.5rem 0;
  font-weight: 600;
}

.topic {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin-bottom: 0.4rem;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  transition: border-color 0.1s ease, background 0.1s ease;
}

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

.topic.done {
  background: var(--done-soft);
  border-color: #c8e6d3;
}

.topic-check {
  width: 18px;
  height: 18px;
  border: 2px solid var(--text-dim);
  border-radius: 4px;
  flex-shrink: 0;
  margin-top: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: white;
  background: white;
}

.topic.done .topic-check {
  background: var(--done);
  border-color: var(--done);
}

.topic-body {
  flex: 1;
}

.topic-title {
  font-weight: 500;
}

.topic-blurb {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 0.2rem;
}

/* ── TOPIC VIEW ───────────────────────────────────────────────────────── */

.topic-view-header {
  margin-bottom: 1rem;
}
.topic-view-header h2 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: -0.01em;
}
.topic-view-header .section-label {
  color: var(--text-dim);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.explanation {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
}

/* ── Paragraph rows + margin gutter ──────────────────────────────────── */

.para-row {
  display: grid;
  grid-template-columns: 1fr 200px;
  gap: 1.25rem;
  align-items: start;
  margin-bottom: 0.25rem;
}
.para-text > *:first-child { margin-top: 0; }
.para-text > *:last-child { margin-bottom: 0; }

.para-gutter {
  font-size: 0.8rem;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding-top: 0.1rem;
  border-left: 2px solid transparent;
  padding-left: 0.6rem;
  min-height: 24px;
}

.para-row:hover .para-gutter,
.para-gutter.starred,
.para-gutter.has-note {
  border-left-color: var(--border);
}
.para-gutter.starred {
  border-left-color: #e6b800;
}
.para-gutter.has-note {
  border-left-color: var(--accent);
}

.star-btn {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 1.05rem;
  line-height: 1;
  color: var(--text-dim);
  cursor: pointer;
  align-self: flex-start;
  width: auto;
  opacity: 0.4;
  transition: opacity 0.1s ease, color 0.1s ease, transform 0.05s ease;
}
.para-row:hover .star-btn { opacity: 1; }
.para-gutter.starred .star-btn {
  color: #e6b800;
  opacity: 1;
}
.star-btn:hover { transform: scale(1.15); color: #e6b800; }

.note-add {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  align-self: flex-start;
  opacity: 0;
  transition: opacity 0.1s ease, color 0.1s ease, border-color 0.1s ease;
  cursor: pointer;
}
.para-row:hover .note-add { opacity: 0.85; }
.note-add:hover {
  opacity: 1;
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.note-display {
  background: #fffbe8;
  border: 1px solid #f1e4a0;
  border-radius: 6px;
  padding: 0.4rem 0.55rem;
  font-size: 0.8rem;
  color: #6d5d1a;
  cursor: text;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.35;
}
.note-display:hover {
  border-color: #d8c463;
}

.note-editor {
  width: 100%;
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--accent);
  border-radius: 6px;
  resize: vertical;
  background: #fffbe8;
}
.note-editor:focus { outline: none; }

.note-actions {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}
.note-actions button {
  font-size: 0.75rem;
  padding: 0.25rem 0.55rem;
}
.note-save { background: var(--accent); }

@media (max-width: 720px) {
  .para-row {
    grid-template-columns: 1fr;
    gap: 0.4rem;
  }
  .para-gutter {
    border-left: none;
    border-top: 1px solid transparent;
    padding-left: 0;
    padding-top: 0.4rem;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .para-row:hover .para-gutter,
  .para-gutter.starred,
  .para-gutter.has-note { border-left: none; }
  .para-gutter.starred { border-top-color: #e6b800; }
  .para-gutter.has-note { border-top-color: var(--accent); }
}

.explanation h1, .explanation h2, .explanation h3 {
  margin-top: 1rem;
  margin-bottom: 0.5rem;
}
.explanation h1 { font-size: 1.3rem; }
.explanation h2 { font-size: 1.15rem; }
.explanation h3 { font-size: 1.0rem; }
.explanation p { margin: 0.5rem 0; }
.explanation ul, .explanation ol { margin: 0.5rem 0; padding-left: 1.5rem; }
.explanation code {
  background: var(--surface-2);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.9em;
}
.explanation pre {
  background: var(--surface-2);
  padding: 0.75rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.85em;
}
.explanation pre code { background: none; padding: 0; }

.actions-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.chat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.chat h3 {
  margin: 0 0 0.75rem 0;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.msg {
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  max-width: 85%;
  word-wrap: break-word;
}
.msg.user {
  background: var(--accent-soft);
  align-self: flex-end;
}
.msg.assistant {
  background: var(--surface-2);
  align-self: flex-start;
}
.msg p:first-child { margin-top: 0; }
.msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 0.4rem 0; padding-left: 1.3rem; }
.msg code { background: rgba(0,0,0,0.06); padding: 0.05rem 0.3rem; border-radius: 3px; font-size: 0.9em; }
.msg pre { background: rgba(0,0,0,0.06); padding: 0.5rem 0.75rem; border-radius: 6px; overflow-x: auto; font-size: 0.85em; }

.chat-input-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
}

.chat-input-row textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  padding: 0.6rem 0.8rem;
  font-family: inherit;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.chat-input-row textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* ── BUTTONS ──────────────────────────────────────────────────────────── */

button {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 1rem;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.1s ease, transform 0.05s ease;
}
button:hover:not(:disabled) { background: #1f57c2; }
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
button.ghost:hover:not(:disabled) { background: var(--surface-2); }

button.done-btn {
  background: var(--done);
}
button.done-btn:hover:not(:disabled) { background: #1f6c43; }

/* ── MODAL ────────────────────────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}
.modal.hidden { display: none; }

.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 480px;
  width: 100%;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

.modal-card h2 {
  margin: 0 0 1rem 0;
  font-size: 1.3rem;
}
.modal-card h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin: 1rem 0 0.5rem 0;
}

.modal-card input[type="text"] {
  width: 100%;
  padding: 0.65rem 0.85rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.modal-card input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

.level-options {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.level-options label {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 0 0.6rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.level-options label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.level-options input { grid-row: 1 / 3; align-self: center; }
.level-options span { font-weight: 500; }
.level-options small { color: var(--text-dim); font-size: 0.8rem; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.25rem;
}

.modal-card details {
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.85rem;
}
.modal-card details[open] { background: var(--surface-2); }
.modal-card summary {
  cursor: pointer;
  font-size: 0.9rem;
  user-select: none;
  font-weight: 500;
}
.modal-card summary .muted {
  font-weight: normal;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.sources-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.field > span {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  font-weight: 600;
}
.field > span small {
  font-weight: normal;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-dim);
  margin-left: 0.3rem;
}
.field textarea {
  padding: 0.55rem 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  resize: vertical;
}
.field textarea:focus { outline: none; border-color: var(--accent); }

.field input[type="file"] {
  font-size: 0.85rem;
}

.file-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.4rem;
}
.file-list .chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.sources-summary {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.85rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}
.sources-summary strong {
  color: var(--text);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.75rem;
}
.sources-summary .source-item {
  margin-top: 0.2rem;
}
.sources-summary .source-item.error { color: var(--danger); }

.error {
  margin-top: 0.75rem;
  padding: 0.6rem 0.8rem;
  background: #fdecea;
  border: 1px solid #f5b8b1;
  border-radius: 6px;
  color: var(--danger);
  font-size: 0.9rem;
}

.hidden { display: none !important; }

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

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 0.4rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading {
  text-align: center;
  color: var(--text-dim);
  padding: 2rem;
}

/* ── BUILD-ALL PROGRESS ──────────────────────────────────────────────── */

.build-bar {
  background: var(--accent-soft);
  border: 1px solid #c9d8f5;
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.build-bar.success {
  background: var(--done-soft);
  border-color: #c8e6d3;
}
.build-bar .label {
  flex: 1;
  font-size: 0.9rem;
  color: var(--text);
}
.build-bar .progress {
  flex: 1 1 100%;
  margin: 0;
  background: rgba(255,255,255,0.6);
}
.build-bar .progress > span {
  background: var(--accent);
}

.outline-actions {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
  align-items: center;
  flex-wrap: wrap;
}

.topic-built {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-left: 0.4rem;
}

/* ── LOGIN ───────────────────────────────────────────────────────────── */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  max-width: 360px;
  width: 100%;
  box-shadow: var(--shadow);
}
.login-card h1 {
  margin: 0 0 0.25rem 0;
  font-size: 1.4rem;
}
.login-hint {
  margin: 0 0 1.25rem 0;
  color: var(--text-dim);
  font-size: 0.9rem;
}
.login-card form { display: flex; flex-direction: column; gap: 0.75rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.3rem; }
.login-card label span {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}
.login-card input {
  padding: 0.6rem 0.8rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card button { margin-top: 0.5rem; }

/* ── ABOUT modal ─────────────────────────────────────────────────────── */

.about-list {
  margin: 0.5rem 0 1rem 0;
  padding-left: 1.2rem;
}
.about-list li {
  margin-bottom: 0.5rem;
  line-height: 1.5;
}
.modal-card p.dim {
  color: var(--text-dim);
  font-size: 0.9rem;
  margin: 0.4rem 0;
}
