:root {
  --bg: #fbfbfd;
  --panel: #ffffff;
  --border: #e3e3ea;
  --text: #1d1d22;
  --muted: #6b7280;
  --accent: #4f46e5;
  --error: #b91c1c;
  --mono: "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.layout {
  display: flex;
  align-items: flex-start;
  min-height: 100vh;
}

.app-shell {
  flex: 1;
  min-width: 0;
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 20px 120px;
}

/* ── Sidebar file-tree ─────────────────────────────────────────────────── */
.sidebar {
  width: 250px;
  flex: 0 0 250px;
  align-self: stretch;
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  padding: 16px 10px 40px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  font-size: 13px;
}

.tree-section { margin-bottom: 18px; }
.tree-section-label {
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.03em;
  color: var(--muted);
  text-transform: uppercase;
  padding: 2px 6px 6px;
}
.tree-section-label { cursor: pointer; }
.tree-section-label:hover { color: var(--text); }
.tree-section-label .tw { font-size: 9px; }
.tree-empty {
  color: var(--muted);
  font-style: italic;
  padding: 2px 8px;
  font-size: 12px;
}
.tree-children { display: flex; flex-direction: column; }
.tree-folder-label {
  cursor: pointer;
  padding: 3px 6px;
  border-radius: 6px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-folder-label:hover { background: #f0f0f5; }
.tree-folder-label .tw { color: var(--muted); font-size: 10px; }
.tree-folder > .tree-children { margin-left: 12px; }

.tree-item {
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 6px;
  font-family: var(--mono);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-item:hover { background: #f0f0f5; }
.tree-item.active {
  background: var(--accent);
  color: #fff;
}

/* ── Built-in read-only banner ─────────────────────────────────────────── */
.builtin-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #9a3412;
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}
.builtin-banner .banner-text { flex: 1; }

.toolbar .folder-input {
  font-size: 13px;
  font-family: var(--mono);
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 5px 10px;
  color: var(--text);
  width: 180px;
}
.toolbar .title-static {
  font-size: 20px;
  font-weight: 600;
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
}
.toolbar .title-static .ro-tag {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #9a3412;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 6px;
  padding: 1px 7px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 18px;
}

.toolbar .title-input {
  font-size: 20px;
  font-weight: 600;
  border: none;
  background: transparent;
  outline: none;
  flex: 1;
  color: var(--text);
}

.toolbar .spacer { flex: 1; }

button.tb {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}
button.tb:hover { border-color: var(--accent); color: var(--accent); }
button.tb.primary { background: var(--accent); color: #fff; border-color: var(--accent); }

.cell {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.cell-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 4px 14px;
  color: var(--muted);
  font-size: 12px;
  font-family: var(--mono);
}
.cell-header .spacer { flex: 1; }
.cell-header button {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
  border-radius: 6px;
}
.cell-header button:hover { background: #f0f0f5; color: var(--text); }
.cell-header .run-hint { font-size: 11px; opacity: 0.7; }

.cell-editor { padding: 6px 10px; }

.cm-editor { font-family: var(--mono); font-size: 14px; }
.cm-editor.cm-focused { outline: none; }
.cm-editor .cm-content { padding: 6px 0; }

.cell-result {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  overflow-x: auto;
}
.cell-result.empty { display: none; }

/* brief pulse when a result is served from cache (re-run, nothing changed) —
   makes an otherwise-invisible cache hit perceptible without a full recompute. */
.cell-result.cache-hit { animation: thin-cache-hit .26s ease-out; }
@keyframes thin-cache-hit {
  0%   { background: color-mix(in srgb, var(--accent) 22%, transparent); }
  100% { background: transparent; }
}

.result-error {
  color: var(--error);
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  margin: 0;
}
.result-edn {
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  margin: 0;
}
.result-nil { color: var(--muted); font-family: var(--mono); }
.result-plot { width: 100%; min-height: 360px; }

.add-cell {
  display: flex;
  justify-content: center;
  margin: 8px 0 0;
}
.add-cell button {
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 18px;
  cursor: pointer;
  font-size: 13px;
}
.add-cell button:hover { border-color: var(--accent); color: var(--accent); }

.hidden-file-input { display: none; }

/* ── Keybindings modal ─────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(20, 20, 28, 0.38);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px;
}
.modal {
  width: 520px;
  max-width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.22);
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-size: 16px; font-weight: 600; flex: 1; }
.modal-hint {
  margin: 0;
  padding: 12px 18px 4px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}
.keys-list {
  overflow-y: auto;
  padding: 6px 18px 12px;
}
.keys-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.keys-row:last-child { border-bottom: none; }
.keys-row.conflict .keys-label { color: var(--error); }
.keys-row .spacer { flex: 1; }
.keys-label { font-size: 13px; }
.keys-warn { color: #b45309; }
.key-capture {
  font-family: var(--mono);
  font-size: 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #f7f7fb;
  color: var(--text);
  padding: 4px 10px;
  min-width: 130px;
  cursor: pointer;
}
.key-capture:hover { border-color: var(--accent); }
.key-capture.capturing {
  border-color: var(--accent);
  background: #eef2ff;
  color: var(--accent);
  outline: none;
}
.key-reset {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  border-radius: 6px;
  padding: 2px 7px;
}
.key-reset:hover { background: #f0f0f5; color: var(--text); }
.key-reset.ghost { visibility: hidden; }
.modal-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}
.modal-footer .spacer { flex: 1; }
.modal-note { font-size: 12px; color: var(--muted); }

/* Autocomplete dropdown — re-parented to <body> via the CM tooltips extension,
   so it floats above cells and the sticky toolbar instead of being clipped. */
.cm-tooltip.cm-tooltip-autocomplete {
  z-index: 1000;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
  background: var(--panel);
  overflow: hidden;
}
.cm-tooltip-autocomplete > ul {
  font-family: var(--mono);
  font-size: 13px;
  max-height: 16em;
}
.cm-tooltip-autocomplete > ul > li[aria-selected] {
  background: var(--accent);
  color: #fff;
}

/* Hover docstring tooltip (also re-parented to <body> via the tooltips ext). */
.cm-tooltip { z-index: 1000; }
/* Outer = scroll container. Capped to a narrow box (like mobile) so the 100ch
   body always overflows and scrolls horizontally, on desktop and mobile alike. */
.cm-doc-tooltip {
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.45;
  padding: 8px 11px;
  box-sizing: border-box;
  max-width: min(calc(100vw - 26px), 460px);
  max-height: 320px;
  overflow: auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
}
/* Inner = fixed 100-column wrap width. Wraps at exactly 100 chars; wider than
   the capped container, so the container scrolls horizontally. */
.cm-doc-tooltip-body {
  white-space: pre-wrap;
  width: 100ch;
}

/* ── Thin client additions ─────────────────────────────────────────────── */
.result-latex { overflow-x: auto; }
.result-text {
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  margin: 0;
  color: var(--muted);
}
.queue-banner {
  background: #eef2ff;
  border: 1px solid var(--accent);
  color: #3730a3;
  border-radius: 10px;
  padding: 9px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}
.spinner { display: inline-flex; gap: 5px; align-items: center; height: 18px; }
.spinner .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); display: inline-block;
  animation: thin-blink 1s infinite both;
}
.spinner .dot:nth-child(2) { animation-delay: .2s; }
.spinner .dot:nth-child(3) { animation-delay: .4s; }
@keyframes thin-blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* chart chunk loading placeholder */
.plot-loading { display: flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 13px; min-height: 40px; }
.plot-loading-text { color: var(--muted); }

/* "recomputing" badge: subtle spinning ↻ in the cell header (result stays put) */
.cell-recomputing { color: var(--accent); font-size: 12px; margin-left: 6px;
  display: inline-block; animation: thin-rot 1.1s linear infinite; }
@keyframes thin-rot { to { transform: rotate(360deg); } }

/* submission/connection error banner (keeps prior results; Retry re-runs) */
.error-banner { display:flex; align-items:center; gap:10px;
  background:#fef2f2; border:1px solid #fecaca; color:#b91c1c;
  border-radius:10px; padding:9px 14px; margin-bottom:16px; font-size:13px; }
.error-banner .error-text { flex:0 1 auto; }
.error-banner .spacer { flex:1; }

/* ── Responsive chrome ─────────────────────────────────────────────────────
   Mobile-only affordances — a hamburger that opens the sidebar as an off-canvas
   drawer, and a "⋯" overflow menu that holds the toolbar's secondary actions.
   On desktop the hamburger / ⋯ are hidden and the action buttons sit inline in
   the toolbar; the @media block below turns them into the drawer + dropdown. */
.backdrop { display: none; }
.menu-scrim { display: none; }
.menu-btn, .more-btn { display: none; }
.tb-more { display: flex; align-items: center; gap: 8px; }
.tb-actions { display: flex; align-items: center; gap: 8px; }
.tb-folder-row { display: none; }

.menu-btn, .more-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.menu-btn:hover, .more-btn:hover { border-color: var(--accent); color: var(--accent); }

@media (max-width: 820px) {
  .app-shell {
    max-width: none;
    padding: 14px 14px calc(96px + env(safe-area-inset-bottom));
  }

  /* Sidebar → off-canvas drawer */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    height: 100dvh;
    width: 82vw; max-width: 300px;
    flex-basis: auto;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 2px 0 18px rgba(0, 0, 0, 0.16);
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-left: calc(10px + env(safe-area-inset-left));
  }
  .layout.drawer-open .sidebar { transform: none; }
  .layout.drawer-open .backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(20, 20, 28, 0.42);
    z-index: 45;
  }

  /* Hamburger + overflow menu become visible */
  .menu-btn, .more-btn { display: inline-flex; align-items: center; justify-content: center; }

  .tb-more { display: inline-flex; position: relative; }
  .menu-scrim { display: block; position: fixed; inset: 0; z-index: 55; }
  .tb-actions {
    display: none;
    position: absolute;
    top: calc(100% + 8px); right: 0;
    flex-direction: column; align-items: stretch;
    min-width: 176px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
    padding: 6px;
    z-index: 60;
  }
  .tb-actions.open { display: flex; }
  .tb-actions .tb {
    border: none; background: transparent;
    text-align: left; width: 100%;
    padding: 10px 12px; border-radius: 8px;
  }
  .tb-actions .tb:hover { background: #f0f0f5; color: var(--text); }

  /* Folder editing inside the overflow menu (top-row input is hidden on mobile) */
  .tb-folder-row {
    display: flex; flex-direction: column; gap: 4px;
    padding: 8px 10px 10px;
    border-bottom: 1px solid var(--border); margin-bottom: 4px;
  }
  .tb-folder-label { font-size: 12px; color: var(--muted); }
  .tb-folder-input {
    font-family: var(--mono); font-size: 16px;
    border: 1px solid var(--border); border-radius: 8px;
    padding: 8px 10px; background: var(--panel); color: var(--text);
    width: 100%;
  }

  /* Keep the top row to a single compact line */
  .toolbar { gap: 6px; padding: 8px 0; }
  .toolbar .title-input, .toolbar .title-static {
    font-size: 17px; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  }
  .btn-label { display: none; }
  .folder-input { display: none; }

  /* Comfortable touch targets */
  .menu-btn, .more-btn, button.tb, .add-cell button { min-height: 40px; }
  .cell-header button { min-width: 34px; min-height: 34px; font-size: 16px; }
  .tree-item, .tree-folder-label { padding-top: 8px; padding-bottom: 8px; }

  /* CodeMirror is a contenteditable (not an <input>), so iOS doesn't auto-zoom
     it on focus — safe to use a smaller, more compact code font on mobile. */
  .cm-editor { font-size: 14px; }
  /* Match the editor's compact size for results too (scales KaTeX + text). */
  .cell-result { padding: 10px 12px; font-size: 14px; }

  /* Keybindings modal → bottom sheet */
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal {
    width: 100%; max-width: 100%;
    max-height: 90vh;
    border-radius: 16px 16px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ── First-run consent dialogs (GDPR + disclaimer) ─────────────────────────
   Reuse the .modal-overlay / .modal shell; these overlays intentionally have no
   outside-click handler, so the only way past them is the action button. */
.consent-modal { width: 460px; }
.consent-body {
  padding: 12px 18px 6px;
  font-size: 14px;
  line-height: 1.55;
}
.consent-body p { margin: 0 0 10px; }
.consent-body a { color: var(--accent); font-weight: 600; text-decoration: none; }
.consent-body a:hover { text-decoration: underline; }
.consent-check {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-top: 6px;
  cursor: pointer;
  font-size: 13px;
}
.consent-check input { margin-top: 2px; width: 17px; height: 17px; flex: 0 0 auto; }
button.tb:disabled, button.tb[disabled] { opacity: 0.5; cursor: not-allowed; }

/* Consent dialogs stay top-anchored on mobile (the shared .modal rule turns
   other modals into bottom sheets, which clipped the action button behind the
   browser chrome). */
@media (max-width: 820px) {
  .modal-overlay.consent-overlay { align-items: flex-start; padding: 4vh 12px; }
  .consent-overlay .modal { border-radius: 14px; max-height: 92vh; }
  .consent-overlay .consent-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
}
