/* Theme tokens. Colours are only written here, each as
   light-dark(<light>, <dark>). The palette follows the OS setting unless
   the user pinned one with the toggle: theme-init.js / theme-toggle.js
   set data-theme on <html>, which fixes color-scheme. Works without
   JavaScript too (then it simply follows the OS). */
:root {
  color-scheme: light dark;

  --bg:     light-dark(#ffffff, #16181d);
  --fg:     light-dark(#1a1a1a, #e8e8e8);
  --muted:  light-dark(#666666, #9aa0aa);
  --border: light-dark(#e2e2e2, #2c2f36);
  --row:    light-dark(#fafafa, #1d2026);
  --accent: light-dark(#2563eb, #6ea8fe);
  --card:   light-dark(#fbfbfb, #1b1e24);
  --danger: light-dark(#b91c1c, #f87171);
  --ok:     light-dark(#15803d, #4ade80);
  --on-accent: light-dark(#ffffff, #0f172a);
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

/* The toggle shows a moon in light mode and a sun in dark mode. */
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun  { display: inline; }
:root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun  { display: inline; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--fg);
  background: var(--bg);
}

a { color: var(--accent); }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.app-header h1 { font-size: 1.25rem; margin: 0; }
.app-header h1 a { color: inherit; text-decoration: none; }

.nav { display: flex; align-items: center; gap: 1rem; }
.nav a { color: var(--accent); text-decoration: none; }
.nav a:hover { text-decoration: underline; }
/* The username link is quieter than a regular nav link. */
.nav a.who { color: var(--muted); }
.nav a.who:hover { color: var(--accent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }

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

table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
table.data thead th { color: var(--muted); font-weight: 600; }
table.data tbody tr:nth-child(even) { background: var(--row); }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.5rem;
}
.narrow { max-width: 22rem; margin: 3rem auto; }

form label { display: block; margin-bottom: 0.85rem; font-weight: 600; }
form input {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.6rem;
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
}
form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button.primary {
  margin-top: 0.5rem;
  padding: 0.55rem 1.1rem;
  font: inherit;
  font-weight: 600;
  color: var(--on-accent);
  background: var(--accent);
  border: 0;
  border-radius: 0.4rem;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.05); }

/* Sign out, separated from the password form at the bottom of the card. */
.logout-form {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}
.logout-form button {
  padding: 0.5rem 1.1rem;
  font: inherit;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  cursor: pointer;
}
.logout-form button:hover { border-color: var(--accent); color: var(--accent); }

.notice { padding: 0.6rem 0.8rem; border-radius: 0.4rem; margin: 0 0 1rem; }
.notice.error { color: var(--danger); border: 1px solid var(--danger); }
.notice.success { color: var(--ok); border: 1px solid var(--ok); }

/* ---------------------------------------------------------------------
   Notebook and files
   --------------------------------------------------------------------- */
:root {
  --code-bg: light-dark(#f6f7f9, #1e2128);
  --busy:    light-dark(#b45309, #fbbf24);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
main.wide { max-width: 1120px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

.crumbs { font-size: 0.95rem; }
.crumbs a { text-decoration: none; }

table.files td { vertical-align: middle; }
table.files a.dir { font-weight: 600; }
table.files a.nb::before { content: "▤ "; color: var(--muted); }
table.files td.actions { text-align: right; width: 4rem; }
.file-forms { display: flex; flex-wrap: wrap; gap: 1rem; margin-top: 1.5rem; }
.file-forms form { display: flex; gap: 0.5rem; align-items: center; }
.file-forms input:not([type="file"]) {
  padding: 0.45rem 0.55rem; font: inherit; color: var(--fg);
  background: var(--bg); border: 1px solid var(--border); border-radius: 0.4rem;
}
.file-forms button.primary { margin-top: 0; }

button, .menu summary {
  font: inherit; font-size: 0.9rem; color: var(--fg); background: transparent;
  border: 1px solid var(--border); border-radius: 0.4rem; padding: 0.3rem 0.7rem;
  cursor: pointer;
}
button:hover, .menu summary:hover { border-color: var(--accent); color: var(--accent); }
button.danger { color: var(--danger); }

details.menu { position: relative; display: inline-block; }
details.menu summary { list-style: none; }
details.menu summary::-webkit-details-marker { display: none; }
details.menu > div, details.menu > form {
  position: absolute; right: 0; z-index: 5; min-width: 13rem;
  background: var(--card); border: 1px solid var(--border); border-radius: 0.5rem;
  padding: 0.4rem; box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
}
details.menu > div a { display: block; padding: 0.3rem 0.5rem; text-decoration: none; }
details.menu > div a:hover { background: var(--row); }
table.files details.menu form {
  display: flex; gap: 0.4rem; margin: 0.2rem 0; position: static; box-shadow: none; border: 0;
}
table.files details.menu[open] { z-index: 5; }
table.files details.menu[open] > summary ~ form:first-of-type { margin-top: 0.4rem; }
table.files details.menu input { font: inherit; padding: 0.25rem 0.4rem; border: 1px solid var(--border);
  border-radius: 0.3rem; background: var(--bg); color: var(--fg); min-width: 10rem; }

.kstate { font-size: 0.8rem; padding: 0.1rem 0.5rem; border-radius: 1rem; border: 1px solid var(--border);
  color: var(--muted); margin-left: 0.5rem; }
.kstate.idle { color: var(--ok); border-color: var(--ok); }
.kstate.busy, .kstate.starting { color: var(--busy); border-color: var(--busy); }
.kstate.dead { color: var(--danger); border-color: var(--danger); }

/* notebook */
.nb-bar {
  position: sticky; top: 0; z-index: 4; background: var(--bg);
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 0.5rem; padding: 0.5rem 0; border-bottom: 1px solid var(--border); margin-bottom: 1rem;
}
.nb-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; }
.nb-tools form { display: inline; }
.save-state { font-size: 0.8rem; color: var(--muted); min-width: 4rem; }

.cell { display: grid; grid-template-columns: 3.5rem minmax(0, 1fr); gap: 0.5rem;
  margin: 0.35rem 0; padding: 0.3rem 0; border-left: 3px solid transparent; }
.cell:focus-within { border-left-color: var(--accent); }
.cell-side { text-align: right; font: 0.8rem var(--mono); color: var(--muted); padding-top: 0.45rem; }
/* the cell's tools: one row over the top-right corner of the editor, so
   they take no room in the layout */
.cell { position: relative; }
.cell-tools {
  position: absolute; top: -0.55rem; right: 0.4rem; z-index: 3;
  display: none; flex-direction: row; gap: 0.15rem; padding: 0.1rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 0.4rem;
  box-shadow: 0 2px 6px rgb(0 0 0 / 0.08);
}
.cell:hover .cell-tools, .cell:focus-within .cell-tools { display: flex; }
.cell-tools button { padding: 0 0.35rem; font-size: 0.75rem; line-height: 1.3rem; min-width: 1.5rem; border: 0; }
.cell-tools button:hover { background: var(--row); }
.cell.running .count { color: var(--busy); }
.cell.failed .count { color: var(--danger); }
.cell.stale .out { opacity: 0.55; }

textarea.src {
  min-height: 2.3rem;
  width: 100%; resize: none; overflow: hidden; display: block;
  font: 0.9rem/1.45 var(--mono); tab-size: 2;
  color: var(--fg); background: var(--code-bg);
  border: 1px solid var(--border); border-radius: 0.4rem; padding: 0.5rem 0.6rem;
}
textarea.src:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.cell.md textarea.src { font-family: inherit; font-size: 0.95rem; }
.cell.hide-code textarea.src { opacity: 0.6; }
.md-view { padding: 0.1rem 0.2rem; cursor: text; min-height: 1.5rem; }
.md-view > :first-child { margin-top: 0; }
.md-view > :last-child { margin-bottom: 0; }

.out:empty { display: none; }
.out { margin-top: 0.4rem; overflow-x: auto; }
.out pre { margin: 0.25rem 0; font: 0.85rem/1.4 var(--mono); white-space: pre-wrap; word-break: break-word; }
.out pre.out-error { color: var(--danger); }
.out .out-img img { max-width: 100%; height: auto; display: block; border-radius: 0.4rem; }
.out .out-html .table-wrap { margin: 0.25rem 0; }
.out .nb-dims { color: var(--muted); font-size: 0.8rem; margin: 0.1rem 0 0; }
.completions { font: 0.8rem var(--mono); color: var(--muted); padding: 0.3rem 0.4rem; word-break: break-word; }

.add-cell { display: flex; gap: 0.5rem; margin: 1rem 0 0 4rem; }
.help { font-size: 0.8rem; margin-left: 4rem; }

/* tables in outputs (the report classes, in the app colours) */
.out .out-html table { background: transparent; }
.out-html .table-wrap { overflow-x: auto; }
.out-html table { border-collapse: collapse; font-size: 0.88rem; margin: 0.25rem 0; }
.out-html caption { text-align: left; color: var(--muted); font-size: 0.8rem; padding-bottom: 0.3rem; }
.out-html th, .out-html td { padding: 0.3rem 0.75rem; border-bottom: 1px solid var(--border); }
.out-html th { font-weight: 600; color: var(--muted); text-align: left; }
.out-html td.num, .out-html th.num { text-align: right; font-variant-numeric: tabular-nums; }
.out-html tr.total td { font-weight: 600; border-top: 1px solid var(--muted); }
.out-html p.more { color: var(--muted); font-size: 0.8rem; margin: 0.2rem 0; }
.out-html figure { margin: 0.5rem 0; }
.out-html figure svg { max-width: 100%; height: auto; }
.out-html aside.note { border-left: 3px solid var(--accent); padding: 0.4rem 0.8rem; background: var(--row); }
button, .menu summary { white-space: nowrap; }
.file-forms input:not([type="file"]) { width: 12rem; display: inline-block; margin-top: 0; }
.file-forms input[type="file"] { width: auto; display: inline-block; margin-top: 0; border: 0; padding: 0; }

/* output actions: explore a frame, download a chart */
.out-actions { display: flex; gap: 0.6rem; align-items: center; margin: 0.15rem 0 0.4rem; font-size: 0.8rem; }
.out-actions button { font-size: 0.8rem; padding: 0.1rem 0.5rem; }
.out-actions a { color: var(--muted); text-decoration: none; border-bottom: 1px dotted var(--muted); }
.out-actions a:hover { color: var(--accent); border-color: var(--accent); }
.out-item:not(:hover) .out-actions a { opacity: 0.6; }

/* table viewer */
.tview { margin: 0.3rem 0; }
.tview-top { display: flex; gap: 0.5rem; align-items: center; font-size: 0.8rem; margin-bottom: 0.3rem; }
.tview-top input { font: inherit; padding: 0.25rem 0.45rem; width: 14rem;
  border: 1px solid var(--border); border-radius: 0.3rem; background: var(--bg); color: var(--fg); }
.tview-top button { padding: 0 0.5rem; }
.tview table { border-collapse: collapse; font-size: 0.85rem; }
.tview th, .tview td { padding: 0.25rem 0.7rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tview th { cursor: pointer; color: var(--muted); font-weight: 600; text-align: left; user-select: none; }
.tview th.num, .tview td.num { text-align: right; font-variant-numeric: tabular-nums; }
.tview th[data-sort="asc"]::after { content: " ▲"; }
.tview th[data-sort="desc"]::after { content: " ▼"; }
.tview tbody tr:nth-child(even) { background: var(--row); }

/* variables panel */
.vars-panel {
  position: fixed; right: 1rem; top: 5rem; bottom: 1rem; width: 18rem; z-index: 6; overflow-y: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 0.6rem;
  padding: 0.7rem 0.9rem; box-shadow: 0 8px 24px rgb(0 0 0 / 0.18); font-size: 0.85rem;
}
.vars-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.vars-head button { padding: 0 0.45rem; }
.vars-list dt { font: 600 0.85rem var(--mono); margin-top: 0.45rem; }
.vars-list dd { margin: 0; color: var(--muted); word-break: break-word; }

/* export dialog */
.export-menu form { display: flex; flex-direction: column; gap: 0.35rem; min-width: 16rem; }
.export-menu fieldset { border: 0; padding: 0; margin: 0 0 0.3rem; }
.export-menu legend { font-weight: 600; font-size: 0.85rem; margin-bottom: 0.2rem; }
form label.check { display: flex; gap: 0.45rem; align-items: center; font-weight: 400; font-size: 0.9rem; margin: 0.1rem 0; }
form label.check input { width: auto; margin: 0; display: inline; }
.export-menu button.primary { align-self: flex-start; }

/* running kernels on the file browser */
.running { margin: 0.5rem 0 1rem; padding: 0.6rem 0.8rem; border: 1px solid var(--border);
  border-radius: 0.5rem; background: var(--card); font-size: 0.9rem; }
.running ul { list-style: none; margin: 0.4rem 0 0; padding: 0; }
.running li { display: flex; align-items: center; gap: 0.6rem; padding: 0.2rem 0; }
.running li form { margin-left: auto; }
.running .kstate { margin-left: 0; }
.md-view:focus { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 0.3rem; }
