:root {
  font-family: ui-monospace, 'SF Mono', Menlo, Monaco, 'Cascadia Mono',
    'Roboto Mono', Consolas, monospace;
  color-scheme: dark;
  --bg: #050507;
  --fg: #e5e5e5;
  --muted: #888;
  --dim: #555;
  --border: #1f1f26;
  --border-hover: #2a2a33;
  --accent: #4ade80;
  --danger: #f87171;
  --warn: #facc15;
  --blue: #60a5fa;
  --panel: #0a0a10;
  --panel-lighter: #0d0d14;
  --code: #11111a;
  --py: #fbbf24;
  --sql: #60a5fa;
  --md: #a78bfa;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; }

body {
  background: radial-gradient(ellipse at top, #0a0a14 0%, var(--bg) 70%);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(1.25rem, env(safe-area-inset-top)) max(1.25rem, env(safe-area-inset-right))
    max(1.25rem, env(safe-area-inset-bottom)) max(1.25rem, env(safe-area-inset-left));
  gap: 1rem;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  flex-wrap: wrap;
  min-width: 0;
  flex: 1;
}
.brand h1 {
  font-size: clamp(1.5rem, 4vw, 2.1rem);
  font-weight: 600;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, #fff, #888);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand h1::before {
  content: '> ';
  color: var(--accent);
  -webkit-text-fill-color: var(--accent);
}
.home-link { text-decoration: none; display: inline-block; transition: opacity .15s; }
.home-link:hover { opacity: .75; }

.nb-title {
  font-family: inherit;
  font-size: 1rem;
  background: transparent;
  color: var(--fg);
  border: none;
  border-bottom: 1px dashed transparent;
  padding: 0.2rem 0.1rem;
  outline: none;
  min-width: 16ch;
  flex: 1;
  max-width: 28rem;
}
.nb-title:hover, .nb-title:focus { border-bottom-color: var(--border-hover); }

.actions { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }

button {
  font-family: inherit;
  font-size: 0.8rem;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 5px;
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
}
button:hover:not(:disabled) { background: rgba(74, 222, 128, 0.08); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

button.ghost {
  color: var(--muted);
  border-color: var(--border);
}
button.ghost:hover:not(:disabled) {
  color: var(--fg);
  border-color: var(--border-hover);
  background: transparent;
}

.export-wrap { position: relative; }
.export-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.3rem);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  min-width: 160px;
  z-index: 10;
  overflow: hidden;
}
.export-menu button {
  border: none;
  border-radius: 0;
  text-align: left;
  padding: 0.6rem 0.9rem;
  color: var(--muted);
}
.export-menu button:hover { color: var(--fg); background: rgba(255,255,255,0.02); }
.export-menu button + button { border-top: 1px solid var(--border); }

/* ---------- files bar ---------- */
.files-bar {
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.7rem;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: rgba(13, 13, 18, 0.4);
  font-size: 0.75rem;
}
.files-bar.empty .files-list { display: none; }
.files-bar:not(.empty) .files-hint { display: none; }

.files-label {
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-size: 0.65rem;
}
.files-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  flex: 1;
  min-width: 0;
}
.files-hint {
  color: var(--dim);
  font-size: 0.7rem;
  flex: 1;
}
.files-hint code {
  background: var(--code);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.05em 0.35em;
  font-size: 0.85em;
  color: var(--muted);
}

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.55rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 0.73rem;
  max-width: 100%;
}
.file-chip .file-name {
  color: var(--fg);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 20ch;
}
.file-chip .file-size {
  color: var(--dim);
  font-size: 0.65rem;
}
.file-chip .file-del {
  background: transparent;
  border: none;
  color: var(--dim);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0 0.2rem;
  line-height: 1;
}
.file-chip .file-del:hover { color: var(--danger); }

button.files-add { font-size: 0.7rem; padding: 0.3rem 0.6rem; }

/* ---------- drop overlay ---------- */
.drop-overlay {
  position: fixed;
  inset: 1rem;
  border: 2px dashed var(--accent);
  border-radius: 12px;
  background: rgba(74, 222, 128, 0.08);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  z-index: 200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
}
body.dropping .drop-overlay { opacity: 1; }

/* ---------- figures ---------- */
.nb-figure {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0.5rem 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
}

/* ---------- cells ---------- */
main#cells {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
}

.cell {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  transition: border-color 0.15s;
}
.cell:hover { border-color: var(--border-hover); }
.cell.active { border-color: var(--accent); }

.cell-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.65rem;
  border-bottom: 1px solid var(--border);
  background: #08080c;
  font-size: 0.72rem;
}

.cell-type {
  font-family: inherit;
  font-size: 0.68rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.45rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
}
.cell-type.py { color: var(--py); border-color: rgba(251,191,36,0.35); }
.cell-type.sql { color: var(--sql); border-color: rgba(96,165,250,0.35); }
.cell-type.md { color: var(--md); border-color: rgba(167,139,250,0.35); }

.cell-spacer { flex: 1; }

.cell-head button {
  font-size: 0.68rem;
  padding: 0.3rem 0.55rem;
  color: var(--muted);
  border-color: var(--border);
  background: transparent;
}
.cell-head button:hover:not(:disabled) {
  color: var(--fg);
  border-color: var(--border-hover);
  background: transparent;
}
.cell-head button.run-btn {
  color: var(--accent);
  border-color: var(--accent);
}
.cell-head button.run-btn:hover:not(:disabled) {
  background: rgba(74,222,128,0.08);
}

.cell-editor {
  min-height: 3rem;
}
.cell-editor.rendered {
  display: none;
}
.cell.mode-preview .cell-editor { display: none; }
.cell.mode-preview .md-rendered { display: block; }
.md-rendered {
  display: none;
  padding: 1rem 1.25rem;
  font-family: -apple-system, system-ui, 'Segoe UI', sans-serif;
  font-size: 0.92rem;
  line-height: 1.65;
  color: var(--fg);
  cursor: text;
}
.md-rendered h1, .md-rendered h2, .md-rendered h3, .md-rendered h4 {
  margin: 1.2em 0 0.4em;
  line-height: 1.25;
  color: #fff;
}
.md-rendered h1:first-child, .md-rendered h2:first-child, .md-rendered h3:first-child { margin-top: 0; }
.md-rendered h1 { font-size: 1.55rem; }
.md-rendered h2 { font-size: 1.25rem; color: var(--accent); }
.md-rendered h3 { font-size: 1.05rem; }
.md-rendered p { margin: 0.55em 0; }
.md-rendered ul, .md-rendered ol { margin: 0.55em 0 0.55em 1.4em; }
.md-rendered li { margin: 0.18em 0; }
.md-rendered a { color: var(--accent); text-decoration: none; }
.md-rendered a:hover { text-decoration: underline; }
.md-rendered code {
  font-family: ui-monospace, Menlo, monospace;
  background: var(--code);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.06em 0.35em;
  font-size: 0.85em;
  color: var(--accent);
}
.md-rendered pre {
  background: var(--code);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.8em 1em;
  overflow-x: auto;
  margin: 0.7em 0;
}
.md-rendered pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 0.82rem;
  color: var(--fg);
}
.md-rendered blockquote {
  border-left: 3px solid var(--accent);
  padding: 0.2em 0 0.2em 1em;
  margin: 0.8em 0;
  color: var(--muted);
}
.md-rendered table { border-collapse: collapse; margin: 0.8em 0; width: 100%; }
.md-rendered th, .md-rendered td {
  border: 1px solid var(--border);
  padding: 0.4em 0.7em;
  text-align: left;
}
.md-rendered th { background: #0a0a10; }
.md-rendered hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }

.cell-output {
  border-top: 1px solid var(--border);
  padding: 0.85rem 1rem;
  font-size: 0.82rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--fg);
  background: #08080c;
}
.cell-output:empty { display: none; }
.cell-output .err { color: var(--danger); }
.cell-output .info { color: var(--muted); font-style: italic; }
.cell-output .result { color: var(--accent); }

/* sql result table */
.sql-result {
  margin: 0.3rem 0 0.4rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: auto;
  max-width: 100%;
}
.sql-result table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.78rem;
  font-family: ui-monospace, Menlo, monospace;
}
.sql-result th {
  background: #0a0a10;
  color: var(--accent);
  text-align: left;
  padding: 0.4rem 0.7rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  white-space: nowrap;
}
.sql-result td {
  padding: 0.35rem 0.7rem;
  border-bottom: 1px solid var(--border);
  color: var(--fg);
  white-space: nowrap;
}
.sql-result tr:last-child td { border-bottom: none; }
.sql-result tr:nth-child(even) td { background: rgba(255,255,255,0.015); }
.sql-result td.null { color: var(--dim); font-style: italic; }
.sql-meta {
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
  font-style: italic;
}

/* ace editor overrides */
.ace_editor {
  background: var(--panel) !important;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace !important;
  font-size: 13px !important;
}
.ace_gutter { background: #08080c !important; color: var(--dim) !important; }

/* add cell row */
.add-cell-row {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
  flex-wrap: wrap;
}

/* footer */
footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding-top: 0.2rem;
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
}
footer > a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
}
footer > a:hover { color: var(--accent); }

.status-bar {
  display: flex;
  gap: 1rem;
  font-size: 0.7rem;
  color: var(--dim);
  flex-wrap: wrap;
  justify-content: center;
}
.rt-status.loading { color: var(--warn); }
.rt-status.ready { color: var(--accent); }
.rt-status.error { color: var(--danger); }
.rt-status.dirty { color: var(--warn); }

.credits { font-size: 0.7rem; color: var(--dim); text-align: center; }
.credits a { color: var(--dim); text-decoration: none; font-size: 0.7rem; }
.credits a:hover { color: var(--accent); }

/* toast */
.toast {
  position: fixed;
  top: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 0.55rem 1rem;
  border-radius: 6px;
  font-size: 0.8rem;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: toast-in 0.2s ease-out;
}
.toast.error { border-color: var(--danger); color: var(--danger); }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, -8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

@media (max-width: 640px) {
  .actions { width: 100%; justify-content: flex-start; }
  button { padding: 0.55rem 1rem; font-size: 0.85rem; }
  .cell-head button { padding: 0.4rem 0.7rem; font-size: 0.75rem; }
  .nb-title { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
