/* The web notebook (MATH-140). Colours are tokens, swapped for the dark theme (MATH-243). */

:root {
  color-scheme: light dark;
  --bg: #fbfaf8;
  --surface: #ffffff;
  --surface-2: #f2f0ec;
  --border: #dedad3;
  --border-strong: #c6c0b6;
  --fg: #1f1d1a;
  --muted: #6f6a62;
  --accent: #c43c1f;
  --accent-fg: #ffffff;
  --label: #4a63a8;
  --focus: #3d6fd8;
  --ok: #2f8a4c;
  --busy: #c98a12;
  --restarted: #3d6fd8;
  --error: #c0392b;
  --print: #3c3a36;
  --message: #b0440e;
  --message-bg: #fdf1e8;
  --stale: #9a948b;
  /* highlighting */
  --tok-comment: #8a857c;
  --tok-string: #2e7d32;
  --tok-number: #0e6f8a;
  --tok-builtin: #2a3fa0;
  --tok-symbol: #1f1d1a;
  --tok-pattern: #2e8b6e;
  --tok-slot: #9c2f8f;
  --tok-out: #9c2f8f;
  --tok-operator: #6b5a3e;
  --tok-bracket: #57524a;
  --tok-bad: #d0021b;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "DejaVu Sans Mono", "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --label-w: 6.5rem;
  --tools-w: 8.4rem; /* the cell tools beside the input */
}

/* The theme in use: <html data-theme>, set by theme.js (System follows the system). */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #17181b;
  --surface: #1f2125;
  --surface-2: #2a2c31;
  --border: #34363c;
  --border-strong: #4a4d55;
  --fg: #e6e4df;
  --muted: #9b978f;
  --accent: #e0603f;
  --accent-fg: #17181b;
  --label: #8ea6e8;
  --focus: #7aa2f7;
  --ok: #5cc27a;
  --busy: #e2b04a;
  --restarted: #7aa2f7;
  --error: #ef6f61;
  --print: #cfccc6;
  --message: #f0a070;
  --message-bg: #2e2219;
  --stale: #6f6b65;
  --tok-comment: #7d7a73;
  --tok-string: #8fcf86;
  --tok-number: #6cc7d9;
  --tok-builtin: #93a8f5;
  --tok-symbol: #e6e4df;
  --tok-pattern: #6fd3ae;
  --tok-slot: #e08ad3;
  --tok-out: #e08ad3;
  --tok-operator: #c9b48d;
  --tok-bracket: #a8a39a;
  --tok-bad: #ff5c6c;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 var(--sans);
  min-height: 100vh;
}

button {
  font: inherit;
  font-size: 0.875rem;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

kbd {
  font: 0.75rem var(--mono);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3em;
  background: var(--surface);
}

/* --- the bar ---------------------------------------------------------------------------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.55rem 1rem;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
}
.brand {
  margin: 0;
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: 0.01em;
}
.brand::before {
  content: "WL";
  display: inline-block;
  margin-right: 0.45rem;
  padding: 0.05rem 0.3rem;
  border-radius: 4px;
  background: var(--accent);
  color: var(--accent-fg);
  font: 700 0.75rem/1.3 var(--mono);
  vertical-align: 0.1em;
}
.actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }
#run-all { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
#run-all:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 85%, var(--fg)); }

.status {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 9rem;
  justify-content: flex-end;
}
.status .dot {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}
.status[data-state="ready"] .dot { background: var(--ok); }
.status[data-state="running"] .dot { background: var(--busy); animation: pulse 1s ease-in-out infinite; }
.status[data-state="restarted"] .dot { background: var(--restarted); }
.status[data-state="error"] .dot { background: var(--error); }
.status[data-state="loading"] .dot { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

.menu { position: relative; }
.menu-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 15rem;
  padding: 0.3rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
}
.menu-list[hidden] { display: none; }
.menu-list button { border: 0; text-align: left; padding: 0.4rem 0.6rem; background: transparent; }
.menu-list button:hover, .menu-list button:focus-visible { background: var(--surface-2); }
.menu-list button { display: flex; align-items: center; gap: 1rem; }
.menu-list button kbd { margin-left: auto; color: var(--muted); }
.menu-list hr { width: 100%; margin: 0.25rem 0; border: 0; border-top: 1px solid var(--border); }

.theme {
  font: inherit;
  font-size: 0.875rem;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 0.25rem 0.4rem;
  cursor: pointer;
}
.theme:hover { background: var(--surface-2); }
.theme:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

/* --- files for Get (MATH-143) ------------------------------------------------------------ */

.files {
  max-width: 64rem;
  margin: 0.75rem auto 0;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.files[hidden] { display: none; }
.files.dragging { border-color: var(--focus); border-style: dashed; }
.files-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.3rem 0.8rem; }
.files-head h2 { margin: 0; font-size: 0.95rem; }
.files-note { margin: 0; flex: 1 1 18rem; font-size: 0.8rem; color: var(--muted); }
.files-list { list-style: none; margin: 0.4rem 0 0; padding: 0; }
.files-list li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.2rem 0;
  border-top: 1px solid var(--border);
}
.files-list code { font: 0.85rem var(--mono); overflow-wrap: anywhere; }
.files-size { margin-left: auto; font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.files-list button { padding: 0.1rem 0.45rem; }
.files-empty { margin: 0.4rem 0 0; font-size: 0.85rem; color: var(--muted); }
.files-empty[hidden] { display: none; }

.drop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgb(0 0 0 / 0.35);
  pointer-events: none;
}
.drop[hidden] { display: none; }
.drop > div {
  padding: 1rem 1.4rem;
  border: 2px dashed var(--focus);
  border-radius: 10px;
  background: var(--surface);
  text-align: center;
}

/* --- loading ---------------------------------------------------------------------------- */

.loading {
  max-width: 28rem;
  margin: 1rem auto 0;
  padding: 0 1rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}
.loading[hidden] { display: none; }
.progress {
  height: 4px;
  border-radius: 2px;
  background: var(--surface-2);
  overflow: hidden;
  margin-bottom: 0.4rem;
}
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }
#progress-bar.indeterminate { animation: pulse 1s ease-in-out infinite; }
.loading.failed { color: var(--error); }
.loading.failed #progress-bar { background: var(--error); width: 100%; }

/* --- cells ------------------------------------------------------------------------------ */

.notebook {
  max-width: 64rem;
  margin: 0 auto;
  padding: 1rem 1rem 0.5rem;
}

.cell { margin: 0 0 0.9rem; }

.cell-in, .out-result {
  display: grid;
  grid-template-columns: var(--label-w) minmax(0, 1fr) auto;
  align-items: start;
  gap: 0 0.5rem;
}
.out-result { grid-template-columns: var(--label-w) minmax(0, 1fr); }

.label {
  font: 0.78rem/1.6 var(--mono);
  color: var(--label);
  text-align: right;
  padding-top: 0.45rem;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}
.out-label { padding-top: 0.1rem; }
.cell[data-state="running"] .in-label { color: var(--busy); }
.cell[data-state="queued"] .in-label { color: var(--muted); }

.editor {
  display: grid;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-width: 0;
}
.cell:focus-within .editor { border-color: var(--focus); }
.cell[data-state="running"] .editor { border-color: var(--busy); }
.cell[data-state="queued"] .editor { border-style: dashed; }
.editor > * {
  grid-area: 1 / 1;
  margin: 0;
  padding: 0.4rem 0.55rem;
  border: 0;
  font: 14px/1.5 var(--mono);
  letter-spacing: normal;
  tab-size: 4;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  min-width: 0;
}
.editor-hl { color: var(--fg); pointer-events: none; overflow: hidden; }
.editor-ta {
  resize: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  caret-color: var(--fg);
  outline: none;
  width: 100%;
  height: 100%;
  min-height: 2.2em;
}
.editor-ta::selection { background: color-mix(in srgb, var(--focus) 30%, transparent); color: transparent; }
.editor-ta:focus-visible { outline: none; }

.tok-comment { color: var(--tok-comment); font-style: italic; }
.tok-string { color: var(--tok-string); }
.tok-number { color: var(--tok-number); }
.tok-builtin { color: var(--tok-builtin); }
.tok-symbol { color: var(--tok-symbol); }
.tok-pattern { color: var(--tok-pattern); font-style: italic; }
.tok-slot, .tok-out { color: var(--tok-slot); }
.tok-operator { color: var(--tok-operator); }
.tok-bracket { color: var(--tok-bracket); }
.tok-bad { color: var(--tok-bad); text-decoration: underline wavy var(--tok-bad); text-underline-offset: 3px; }
/* Italics would shift the text against the caret in some fonts: keep the widths. */
.tok-comment, .tok-pattern { font-synthesis: none; }

.cell-tools {
  display: flex;
  gap: 2px;
  padding-top: 0.25rem;
  opacity: 0;
  transition: opacity 0.1s;
}
.cell:hover .cell-tools, .cell:focus-within .cell-tools { opacity: 1; }
.cell-tools button {
  padding: 0.05rem 0.4rem;
  font-size: 0.8rem;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}
.cell-tools button:hover { color: var(--fg); border-color: var(--border); }

.cell-out:empty { display: none; }
.cell-out { margin-top: 0.35rem; margin-right: var(--tools-w); }
.cell-out > pre, .out-text {
  margin: 0;
  font: 14px/1.25 var(--mono);
  white-space: pre;
  overflow-x: auto;
  padding: 0.1rem 0.55rem 0.15rem;
}
.cell-out > pre { margin-left: calc(var(--label-w) + 0.5rem); }
.out-print { color: var(--print); border-left: 3px solid var(--border-strong); }
.out-message {
  color: var(--message);
  background: var(--message-bg);
  border-left: 3px solid var(--message);
  border-radius: 0 4px 4px 0;
  margin-top: 2px !important;
  margin-bottom: 2px !important;
}
.out-result { margin: 0.15rem 0; }

/* Typeset outputs (MATH-242): KaTeX, left-aligned, scrolled sideways when wide. */
.out-body { position: relative; min-width: 0; }
.out-tex {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.05rem 0.55rem 0.1rem;
  color: var(--fg);
}
.out-tex .katex-display { margin: 0.1rem 0; }
.out-tex .katex-display.fleqn > .katex { padding-left: 0; }
.out-tex .katex { font-size: 1.08em; }
.cell-out > .out-tex { margin-left: calc(var(--label-w) + 0.5rem); }
.out-print.out-tex { color: var(--print); }
.out-copy {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.1s;
}
.out-body:hover .out-copy, .out-copy:focus-within { opacity: 1; }
.out-copy button {
  padding: 0 0.35rem;
  font-size: 0.72rem;
  color: var(--muted);
  background: var(--surface);
  border-color: var(--border);
}
.out-copy button:hover { color: var(--fg); }
@media (hover: none) {
  .out-copy { position: static; opacity: 1; justify-content: flex-end; }
}
@media print {
  .out-copy { display: none; }
  .out-tex { overflow: visible; }
}
.out-meta {
  margin-left: calc(var(--label-w) + 0.5rem);
  padding: 0 0.55rem;
  font-size: 0.72rem;
  color: var(--muted);
}
.cell-out.stale .out-label { color: var(--stale); text-decoration: line-through; }
.cell-out.stale .out-text, .cell-out.stale > pre { opacity: 0.6; }
.cell-out.stale .out-meta { color: var(--stale); font-style: italic; }

/* Get reading a file in the running cell (MATH-145). */
.get-progress {
  margin-left: calc(var(--label-w) + 0.5rem);
  padding: 0.2rem 0.55rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.78rem;
  color: var(--muted);
}
.get-progress-bar {
  flex: 0 0 8rem;
  height: 4px;
  border-radius: 2px;
  background: var(--surface-2);
  overflow: hidden;
}
.get-progress-bar > div { height: 100%; width: 0; background: var(--busy); transition: width 0.1s; }
.get-progress-bar.indeterminate > div { animation: pulse 1s ease-in-out infinite; }
.get-progress > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.add-row { max-width: 64rem; margin: 0 auto; padding: 0 1rem; }
.add-row button {
  margin-left: calc(var(--label-w) + 0.5rem);
  color: var(--muted);
  border-style: dashed;
  background: transparent;
}

.footer {
  max-width: 64rem;
  margin: 1.5rem auto 0;
  padding: 0.75rem 1rem 2rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.footer p { margin: 0.3rem 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: calc(100vw - 2rem);
  padding: 0.55rem 0.8rem;
  border-radius: 8px;
  background: var(--fg);
  color: var(--bg);
  font-size: 0.875rem;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
}
.toast[hidden] { display: none; }
.toast button { background: transparent; color: var(--bg); border-color: color-mix(in srgb, var(--bg) 50%, transparent); }
.toast button:hover:not(:disabled) { background: color-mix(in srgb, var(--bg) 15%, transparent); }

/* --- the search and the shortcuts (MATH-243) ------------------------------------------ */

.dialog {
  width: min(36rem, calc(100vw - 2rem));
  max-height: min(32rem, calc(100vh - 4rem));
  margin: 12vh auto auto;
  padding: 0;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 16px 48px rgb(0 0 0 / 0.3);
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.dialog-foot {
  margin: 0;
  padding: 0.4rem 0.8rem;
  font-size: 0.75rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.search input {
  flex: none;
  width: 100%;
  margin: 0;
  padding: 0.7rem 0.9rem;
  font: inherit;
  font-size: 1rem;
  color: var(--fg);
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border);
  outline: none;
}
.search input::placeholder { color: var(--muted); }
.search-results { list-style: none; margin: 0; padding: 0.3rem; overflow-y: auto; min-height: 0; }
.search-results li {
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
}
.search-results li[aria-selected="true"] { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--focus); }
.search-title { display: block; }
.search-snippet {
  display: block;
  font: 0.78rem/1.4 var(--mono);
  color: var(--muted);
  white-space: pre;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search mark { background: color-mix(in srgb, var(--busy) 35%, transparent); color: inherit; border-radius: 2px; }
.search-empty { margin: 0; padding: 0.8rem 0.9rem; color: var(--muted); }
.search-empty[hidden] { display: none; }

.help-head { display: flex; align-items: center; padding: 0.6rem 0.6rem 0.3rem 0.9rem; }
.help-head h2 { margin: 0; font-size: 1rem; }
.help-head button { margin-left: auto; border-color: transparent; background: transparent; color: var(--muted); }
.help-keys { border-collapse: collapse; margin: 0 0.9rem 0.9rem; font-size: 0.875rem; }
.help-keys td { padding: 0.3rem 0; border-top: 1px solid var(--border); vertical-align: top; }
.help-keys td:first-child { padding-right: 1.2rem; white-space: nowrap; }
.help-keys kbd { font-size: 0.8rem; }

/* --- narrow screens: labels above the cells -------------------------------------------- */

@media (max-width: 640px) {
  :root { --label-w: 0px; --tools-w: 0px; }
  .bar { padding: 0.5rem 0.75rem; }
  .status { margin-left: 0; min-width: 0; justify-content: flex-start; width: 100%; }
  .notebook { padding: 0.75rem 0.6rem 0.5rem; }
  .add-row { padding: 0 0.6rem; }
  .cell-in {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "label tools" "editor editor";
    align-items: end;
  }
  .cell-in .in-label { grid-area: label; text-align: left; padding-top: 0; }
  .cell-in .editor { grid-area: editor; }
  .cell-in .cell-tools { grid-area: tools; }
  .out-result { grid-template-columns: minmax(0, 1fr); }
  .out-label { text-align: left; }
  .cell-tools { opacity: 1; padding: 0 0 2px; }
  .cell-tools button { padding: 0.1rem 0.55rem; }
  .cell-out > pre, .cell-out > .out-tex, .out-meta, .add-row button { margin-left: 0; }
  .editor > * { font-size: 16px; } /* no zoom on focus in iOS Safari */
  .footer .keys { display: none; }
  .dialog { margin-top: 1rem; max-height: calc(100vh - 2rem); }
  .search input { font-size: 16px; } /* no zoom on focus in iOS Safari */
}
