/* ---------- colors ----------
   Set at runtime by the `color XY` command (cmd.exe style): --bg and --fg are the only inputs,
   everything else is derived from them. */
:root {
  --bg: #0c0c0c;
  --fg: #cccccc;
  --frame: color-mix(in srgb, var(--bg) 80%, #000);
  --bright: color-mix(in srgb, var(--fg) 55%, #fff);
  --dim: color-mix(in srgb, var(--fg) 55%, var(--bg));
  --accent: var(--bright);
  --warn: color-mix(in srgb, var(--fg) 35%, #ff4040);
  --sel: color-mix(in srgb, var(--fg) 22%, var(--bg));
  --glow: color-mix(in srgb, var(--fg) 30%, transparent);

  /* Font: swap these three for 'VT323' / 22px / 1.15 to get the old look back. */
  --font: 'JetBrains Mono', 'Courier New', monospace;
  --font-size: 15px;
  --lh: 1.45;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { height: 100dvh; margin: 0; }
body {
  background: var(--frame);
  color: var(--fg);
  font-family: var(--font);
  font-size: calc(var(--font-size) * var(--zoom, 1)); /* --zoom: the [-]/[+] buttons */
  line-height: var(--lh);
  overflow: hidden;
}
button, input, textarea {
  font: inherit;
  color: inherit;
}
button {
  background: none;
  border: 0;
  padding: 0 0.25em;
  cursor: pointer;
}
button:hover, button:focus-visible { background: var(--fg); color: var(--bg); outline: none; }
a { color: var(--accent); }
::selection { background: var(--fg); color: var(--bg); }
[hidden] { display: none !important; }
#screen, #editor { scrollbar-color: var(--dim) var(--bg); }
#screen ::-webkit-scrollbar, #editor ::-webkit-scrollbar { width: 12px; height: 12px; }
#screen ::-webkit-scrollbar-track, #editor ::-webkit-scrollbar-track { background: var(--bg); }
#screen ::-webkit-scrollbar-thumb, #editor ::-webkit-scrollbar-thumb { background: var(--dim); border: 3px solid var(--bg); }

/* --app-height / --app-top follow the visual viewport (see app.js) so the on-screen keyboard
   never covers the prompt or the extra-keys row; 100dvh is the fallback. */
#frame {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--app-top, 0px);
  height: var(--app-height, 100dvh);
}
#screen {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
  text-shadow: 0 0 4px var(--glow);
}

/* CRT scanlines + vignette */
#screen::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  background:
    repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.18) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 60%, rgba(0, 0, 0, 0.45) 100%);
  animation: flicker 6s infinite steps(1);
}
html.no-crt #screen::after { display: none; }
html.no-crt #screen { text-shadow: none; }
@keyframes flicker {
  0%, 100% { opacity: 1; }
  47% { opacity: 0.92; }
  48% { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  #screen::after { animation: none; }
  .cursor { animation: none !important; }
  #ex-sidebar { transition: none !important; }
}

/* ---------- top bar / status ---------- */
#bar, #status {
  display: flex;
  align-items: center;
  gap: 1em;
  padding: 0.15em 0.75em;
  background: var(--fg);
  color: var(--bg);
  text-shadow: none;
  flex: none;
}
#bar button:hover, #bar button:focus-visible { background: var(--bg); color: var(--fg); }
#bar button:disabled { opacity: 0.4; cursor: default; background: none; color: inherit; }
.bar-title { font-weight: bold; letter-spacing: 0.1em; }
.bar-user { flex: 1; opacity: 0.8; }
.bar-actions { display: flex; gap: 0.25em; }
#status { justify-content: space-between; font-size: 0.85em; }
#status-left { white-space: pre; }
#status-left.status-error { background: var(--warn); color: var(--bg); padding: 0 0.4em; }
.status-cursor { background: var(--bg); }
.key-table { border-collapse: collapse; }
.key-table td { padding: 0.15em 1.5em 0.15em 0; vertical-align: top; }

/* ---------- terminal ---------- */
#terminal {
  flex: 1;
  overflow-y: auto;
  padding: 0.6em 0.9em 1.5em;
  cursor: text;
  position: relative;
}
#term-output > * { margin: 0; }
.out {
  white-space: pre-wrap;
  word-break: break-word;
}
.out:empty { min-height: calc(var(--lh) * 1em); }
/* Streamed output (terminal.js): not-yet-typed elements and their trailing CSS content stay hidden. */
.stream-hidden { visibility: hidden; }
.stream-hold-after::after { visibility: hidden; }
.stream-cursor {
  display: inline-block;
  width: 0.6em;
  height: 1.1em;
  vertical-align: text-bottom;
  background: var(--fg);
}
.out.err { color: var(--warn); }
.match { background: var(--fg); color: var(--bg); }
.dim-text { color: var(--dim); }
.out.dim { color: var(--dim); }
.out.bright { color: var(--bright); }
.out.echo .prompt { color: var(--accent); }
.term-line { white-space: pre-wrap; word-break: break-all; min-height: calc(var(--lh) * 1em); }
.prompt { color: var(--accent); }
.prompt.root { color: var(--warn); }
.cursor {
  background: var(--fg);
  color: var(--bg);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { background: transparent; color: inherit; } }
#term-input {
  font-size: 16px; /* avoids iOS focus zoom; it's invisible anyway */
  position: absolute;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  padding: 0;
}

.ls-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(12ch, max-content));
  column-gap: 2ch;
}
.ls-long { white-space: pre; overflow-x: auto; }
.help-grid {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 3ch;
  padding-left: 2ch;
}
@media (max-width: 500px) {
  .help-grid { grid-template-columns: 1fr; padding-left: 1ch; }
  .help-grid span:nth-child(even) { color: var(--dim); padding-left: 2ch; margin-bottom: 0.2em; }
}
.dir { color: var(--bright); }
.hidden-file { color: var(--dim); }
.clickable { cursor: pointer; }
.clickable:hover { text-decoration: underline; }

/* neofetch */
.fetch { display: flex; gap: 3ch; align-items: flex-start; margin: 0.4em 0; white-space: normal; }
.fetch-logo { margin: 0; flex: none; line-height: 1.1; text-shadow: none; }
.fetch-logo span { text-shadow: 0 0 6px currentColor; }
.fire-1 { color: #7a1020; }
.fire-2 { color: #c50f1f; }
.fire-3 { color: #ff4d1a; }
.fire-4 { color: #ff8c1a; }
.fire-5 { color: #ffc93c; }
.fire-6 { color: #fff4c2; }
.fire-spark { color: #ffd23f; }
.fire-smoke { color: #8a8a8a; text-shadow: none !important; opacity: 0.7; }
.fire-ash { color: #5c5c5c; text-shadow: none !important; }
.fire-base { color: var(--dim); text-shadow: none !important; }
.fetch-key { color: var(--bright); }
.fetch-dim { color: var(--dim); }
.fetch-info { display: grid; grid-template-columns: max-content 1fr; column-gap: 1.5ch; row-gap: 0.1em; margin-top: 0.2em; }
.fetch-info .fetch-key::after { content: ':'; color: var(--dim); }
.fetch-row { display: contents; }
.fetch-swatches { margin-top: 0.6em; line-height: 0; }
.fetch-swatch { display: inline-block; width: 3ch; height: 1.1em; }
.sw-0 { background: #0c0c0c; }
.sw-1 { background: #0037da; }
.sw-2 { background: #13a10e; }
.sw-3 { background: #3a96dd; }
.sw-4 { background: #c50f1f; }
.sw-5 { background: #881798; }
.sw-6 { background: #c19c00; }
.sw-7 { background: #cccccc; }
.sw-8 { background: #767676; }
.sw-9 { background: #3b78ff; }
.sw-a { background: #16c60c; }
.sw-b { background: #61d6d6; }
.sw-c { background: #e74856; }
.sw-d { background: #b4009e; }
.sw-e { background: #f9f1a5; }
.sw-f { background: #f2f2f2; }
/* Phones: a smaller fire floats top-right and the rows wrap around it, using the full width below it. */
@media (max-width: 600px) {
  .fetch { display: flow-root; }
  .fetch-logo { float: right; font-size: 0.55em; margin: 0 0 0.6em 1ch; }
  .fetch-info { display: block; }
  /* Each row is its own flex box, so only the rows beside the fire get narrower. */
  .fetch-row { display: flex; column-gap: 1.5ch; margin-bottom: 0.1em; }
  .fetch-row .fetch-key { flex: none; width: var(--key-w, 10ch); }
  .fetch-row > :last-child { min-width: 0; overflow-wrap: anywhere; }
}

/* page frontmatter line */
.md .page-meta { color: var(--dim); margin-top: -0.2em; }

/* ---------- markdown ---------- */
.md h1, .md h2, .md h3, .md h4 {
  color: var(--bright);
  font-weight: normal;
  margin: 0.6em 0 0.3em;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.md h1 { font-size: 1.6em; }
.md h1::after {
  content: '';
  display: block;
  border-bottom: 2px dashed var(--dim);
  margin-top: 0.15em;
}
.md h2 { font-size: 1.25em; }
.md h2::before { content: '## '; color: var(--dim); }
.md h3 { font-size: 1.1em; }
.md h3::before { content: '### '; color: var(--dim); }
.md p { margin: 0.5em 0; }
.md ul, .md ol { margin: 0.4em 0; padding-left: 2.5ch; }
.md ul { list-style: none; }
.md ul > li::before { content: '* '; color: var(--accent); margin-left: -2ch; }
.md code {
  background: var(--sel);
  padding: 0 0.3ch;
}
.md pre {
  background: var(--sel);
  padding: 0.5em 0.8em;
  overflow-x: auto;
  margin: 0.5em 0;
}
.md pre code { background: none; padding: 0; }
.md .wikilink.missing { color: var(--dim); text-decoration: underline dashed; cursor: help; }
.md code.runnable { cursor: pointer; }
.md code.runnable::before { content: '> '; color: var(--dim); }
.md code.runnable:hover, .md code.runnable:focus-visible { background: var(--fg); color: var(--bg); outline: none; }
.md code.runnable:hover::before, .md code.runnable:focus-visible::before { color: inherit; }
/* `code` and code blocks: click to copy */
.md code.copyable { cursor: copy; }
.md code.copyable:hover, .md code.copyable:focus-visible { outline: 1px dashed var(--dim); outline-offset: 1px; }
.md .copyable.copied, .md code.copyable.copied { background: var(--fg); color: var(--bg); }
.md pre.copyable { position: relative; cursor: copy; }
.md pre.copyable.copied code { color: var(--bg); }
.copy-btn {
  position: absolute;
  top: 0.2em;
  right: 0.4em;
  color: var(--dim);
  font-size: 0.85em;
  opacity: 0;
}
.md pre.copyable:hover .copy-btn, .copy-btn:focus-visible, .md pre.copied .copy-btn { opacity: 1; }
.md pre.copied .copy-btn { color: var(--bg); }
@media (hover: none) { .copy-btn { opacity: 1; } }
.md blockquote {
  margin: 0.5em 0;
  padding-left: 1ch;
  border-left: 2px solid var(--accent);
  color: var(--dim);
}
.md hr { border: 0; border-top: 2px dashed var(--dim); margin: 1em 0; }
.md img { max-width: 100%; image-rendering: pixelated; }
.md strong { color: var(--bright); font-weight: normal; }
.md em { font-style: normal; text-decoration: underline; }
.md > :first-child { margin-top: 0; }

/* ---------- directory index (cat on a folder) ---------- */
.dir-index .crumbs { color: var(--dim); margin-bottom: 0.3em; }
.dir-index .crumbs a { color: var(--dim); }
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 28ch), 1fr));
  gap: 0.8em;
  margin: 0.8em 0;
}
.card {
  display: block;
  border: 2px solid var(--dim);
  padding: 0.5em 0.8em;
  color: var(--fg);
  text-decoration: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.card:hover, .card:focus-visible {
  border-color: var(--fg);
  background: var(--sel);
  color: var(--fg);
  outline: none;
}
.card-title { color: var(--bright); display: block; }
.card-title::before { content: attr(data-icon) ' '; color: var(--accent); }
.card-desc {
  display: block;
  color: var(--dim);
  margin-top: 0.2em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.card-img {
  display: block;
  width: calc(100% + 1.6em);
  margin: -0.5em -0.8em 0.5em;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-bottom: 2px solid var(--dim);
}
.card-date { display: block; color: var(--dim); font-size: 0.85em; }

.fm-table { color: var(--dim); margin-bottom: 1em; }

/* premiere teaser: ASCII construction site (construction.js) */
.md pre.construction { background: none; padding: 0; margin: 1.2em 0; line-height: 1.15; overflow: hidden; }
.cx-tape { color: #ffc93c; }
.cx-sign { color: var(--bg); background: #ffc93c; }
.cx-spark { color: #ffd23f; text-shadow: 0 0 6px currentColor; }

/* frontmatter status: draft (root only) / premiere ("coming soon" for guests) */
.badge {
  display: inline-block;
  margin-left: 1ch;
  padding: 0 0.6ch;
  border: 1px solid currentColor;
  font-size: 0.75em;
  text-transform: uppercase;
  vertical-align: middle;
}
.badge.status-draft, .tree-tag.status-draft, .badge.status-hidden, .tree-tag.status-hidden { color: var(--warn); }
.badge.status-premiere, .tree-tag.status-premiere { color: var(--accent); }
.card.status-draft { border-style: dashed; }
.card.status-premiere .card-desc { font-style: italic; }
.tree-tag { flex: none; font-size: 0.8em; padding-left: 1ch; }
.status-note { border: 2px dashed currentColor; padding: 0.3em 0.8em; }
.status-note.status-draft { color: var(--warn); }
.status-note.status-premiere { color: var(--accent); }

/* prev / next page links at the bottom of a file (pages.js) */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.8em;
  margin-top: 2.5em;
  padding-top: 1em;
  border-top: 2px dashed var(--dim);
}
.md .pager-link {
  display: block;
  border: 2px solid var(--dim);
  padding: 0.5em 0.8em;
  color: var(--fg);
  text-decoration: none;
  min-width: 0;
}
.md .pager-link:hover, .md .pager-link:focus-visible {
  border-color: var(--fg);
  background: var(--sel);
  outline: none;
}
.pager-link.next { text-align: right; grid-column: 2; }
.pager-label { display: block; color: var(--dim); font-size: 0.85em; }
.pager-title { display: block; color: var(--bright); overflow-wrap: anywhere; }
.page-image { max-width: 100%; border: 2px solid var(--dim); }
.empty-note { color: var(--dim); }

/* `class: links` pages: lists of links become Linktree-style buttons */
.md .page.links ul { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 0.6em; max-width: 44ch; }
.md .page.links ul > li::before { content: none; }
.md .page.links li > a {
  display: block;
  padding: 0.6em 1em;
  border: 2px solid var(--dim);
  color: var(--fg);
  text-decoration: none;
}
.md .page.links li > a:hover, .md .page.links li > a:focus-visible {
  border-color: var(--fg);
  background: var(--fg);
  color: var(--bg);
  outline: none;
}

/* Server-rendered blog post (seo.js), shown until the app has loaded; if it can't load, it stays. */
#ssr { flex: 1; overflow: auto; padding: 0.5em 1.2em 2em; }
#ssr > * { max-width: 90ch; }
#ssr li::before { content: none; }
#screen:has(#ssr) > .view:not(#ssr), #screen:has(#ssr) #term-keys { display: none; }

/* ---------- explorer ---------- */
#explorer {
  flex: 1;
  display: flex;
  min-height: 0;
}
#ex-sidebar {
  width: clamp(200px, 24%, 320px);
  border-right: 2px solid var(--dim);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.ex-head {
  display: flex;
  justify-content: space-between;
  padding: 0.2em 0.6em;
  color: var(--dim);
  border-bottom: 2px solid var(--dim);
}
.ex-admin { display: flex; gap: 0.1em; }
#ex-tree { overflow: auto; flex: 1; padding: 0.3em 0; }
.node {
  display: flex;
  white-space: pre;
  padding: 0 0.6em;
}
.node button { padding: 0; }
.node .twisty { color: var(--dim); flex: none; }
.node .node-label { flex: 1; text-align: left; min-width: 0; }
.node.selected { background: var(--sel); }
.node.visual { background: var(--dim); color: var(--bg); }
.node.visual.selected { background: var(--fg); }
.node.active { color: var(--bright); }

#ex-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
#ex-tabbar {
  display: flex;
  border-bottom: 2px solid var(--dim);
  flex: none;
}
#ex-toggle {
  flex: none;
  padding: 0.1em 0.5em;
  border-right: 2px solid var(--dim);
}
#ex-tabs {
  display: flex;
  overflow-x: auto;
  min-width: 0;
}
#explorer.collapsed #ex-sidebar { display: none; }
.tab {
  display: flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.1em 0.7em;
  border-right: 2px solid var(--dim);
  white-space: nowrap;
  color: var(--dim);
  cursor: pointer;
}
.tab.active { color: var(--bright); background: var(--sel); }
.tab.preview { font-style: italic; } /* replaced by the next file you open; double-click to keep */
.tab .close { padding: 0 0.2em; }
#ex-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2em 1em;
  color: var(--dim);
  flex: none;
}
#ex-content {
  flex: 1;
  overflow: auto;
  padding: 0.5em 1.2em 2em;
}
#ex-content > * { max-width: 90ch; }
.ex-placeholder { color: var(--dim); margin-top: 2em; text-align: center; white-space: pre; }

/* ---------- editor overlay ---------- */
#editor {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
}
.ed-head, #ed-foot {
  display: flex;
  gap: 1.5em;
  padding: 0.15em 0.75em;
  background: var(--fg);
  color: var(--bg);
  text-shadow: none;
  flex: none;
}
.ed-head #ed-path { flex: 1; text-align: center; }
#ed-foot { flex-wrap: wrap; column-gap: 2em; }
#ed-foot b { font-weight: normal; background: var(--bg); color: var(--fg); padding: 0 0.2em; }
#ed-text, #ed-preview {
  flex: 1;
  margin: 0;
  padding: 0.6em 1em;
  overflow: auto;
}
#ed-text {
  background: transparent;
  border: 0;
  resize: none;
  outline: none;
  line-height: var(--lh);
  caret-color: var(--bright);
  text-shadow: inherit;
}

/* cmatrix */
.cmatrix {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  z-index: 120;
  background: #000;
  cursor: none;
}

/* ---------- fuzzy finder ---------- */
#finder {
  position: fixed;
  inset: 0;
  z-index: 105;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12dvh 16px 16px;
  background: rgba(0, 0, 0, 0.45);
  color: var(--fg);
  font-family: var(--font);
}
.finder-box {
  width: min(100%, 70ch);
  background: var(--bg);
  border: 2px solid var(--fg);
  box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.5);
}
.finder-head {
  display: flex;
  justify-content: space-between;
  padding: 0.1em 0.6em;
  background: var(--fg);
  color: var(--bg);
}
#finder-input {
  display: block;
  width: 100%;
  border: 0;
  border-bottom: 2px solid var(--dim);
  background: var(--sel);
  padding: 0.3em 0.6em;
  outline: none;
}
#finder-list { max-height: 50dvh; overflow-y: auto; padding: 0.2em 0; }
.finder-item { padding: 0 0.6em; white-space: pre; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.finder-item b { font-weight: normal; color: var(--bright); text-decoration: underline; }
.finder-item.active { background: var(--fg); color: var(--bg); }
.finder-item.active b { color: var(--bg); }
.finder-empty { padding: 0 0.6em; color: var(--dim); }

/* ---------- dialog ---------- */
#dialog {
  position: fixed;
  inset: 0;
  z-index: 110;
  display: grid;
  color: var(--fg);
  font-family: var(--font);
  place-items: center;
  background: rgba(0, 0, 0, 0.45);
  padding: 16px;
}
#dialog-box {
  background: var(--bg);
  border: 2px solid var(--fg);
  box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.5);
  padding: 0.8em 1em;
  width: min(100%, 44ch);
}
#dialog-input {
  display: block;
  width: 100%;
  margin: 0.5em 0;
  background: var(--sel);
  border: 0;
  padding: 0.1em 0.4em;
  outline: none;
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5em; }

/* extra keys row, touch devices only */
#term-keys {
  display: none;
  gap: 0.3em;
  padding: 0.3em 0.5em;
  overflow-x: auto;
  border-top: 2px solid var(--dim);
  flex: none;
}
#term-keys button {
  flex: none;
  border: 2px solid var(--dim);
  padding: 0.1em 0.6em;
  min-width: 2.6em;
  touch-action: manipulation;
}
@media (pointer: coarse) {
  #term-keys { display: flex; }
  #term-keys.off { display: none; }
}

/* ---------- small screens ---------- */
@media (max-width: 700px) {
  :root { --font-size: 13.5px; }
  /* iOS zooms into inputs under 16px */
  #ed-text, #dialog-input, #finder-input { font-size: 16px; }

  /* The tree is a drawer over the page, below the tab bar; the page dims behind it (tap to close). */
  #explorer { position: relative; overflow: hidden; }
  #ex-sidebar {
    position: absolute;
    top: var(--ex-top, 2em);
    bottom: 0;
    left: 0;
    z-index: 5;
    width: min(85%, 320px);
    background: var(--bg);
    box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.55);
    /* steps(): moves in a few discrete jumps, like an old terminal redrawing (use ease-out for a smooth slide) */
    transition: transform 0.25s steps(5), box-shadow 0.25s steps(5), visibility 0s;
  }
  /* Closed: slide out to the left and fade the dimming, then hide (so it can't be tabbed into). */
  #explorer.collapsed #ex-sidebar {
    display: flex;
    visibility: hidden;
    transform: translateX(-100%);
    box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0);
    transition: transform 0.2s steps(4), box-shadow 0.2s steps(4), visibility 0s 0.2s;
  }
  #ex-tabbar { position: relative; z-index: 6; background: var(--bg); } /* stays above the dimming */
  #ex-toggle { white-space: nowrap; }
  #ex-content { padding: 0.5em 16px 2em; }
  .bar-user { display: none; }
  .bar-title { flex: 1; }
}
