/* Cell grid: the terminal language on a measured character cell.
   One monospace family, one size, a palette of terminal roles, and frames
   that sit where box-drawing characters would: a light line through the
   middle of the edge cells. Frames are CSS borders, not glyph text, so
   nothing decorative is read aloud or measured as text.
   CellGrid (cell-grid.js) writes the measured cell as --mw-cw / --mw-ch and
   the column budget as --mw-cols; without JavaScript the em fallbacks keep
   the same geometry within a fraction of a pixel. */

/* Célula palette, already corrected (every text role >= 4.6:1 and the frame
   line >= 3.1:1 on bg, bg2 and bg3). Zero specificity: CellPalette, a
   design system or the page wins. */
:where(:root) {
  --mw-cell-bg: #0b0d12;
  --mw-cell-bg2: #12151d;
  --mw-cell-bg3: #1b2029;
  --mw-cell-line: #626d7f;
  --mw-cell-ink: #e8ecf4;
  --mw-cell-dim: #a7b1c2;
  --mw-cell-accent: #57d9c4;
  --mw-cell-accent-bg: #57d9c4;
  --mw-cell-on-accent: #000;
  --mw-cell-red: #ff6b7a;
  --mw-cell-green: #62d980;
  --mw-cell-yellow: #ffc857;
  --mw-cell-blue: #6aa9ff;
  --mw-cell-magenta: #d69bff;
  --mw-cell-cyan: #57d9c4;
  --mw-cell-font: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;
}

/* ---- the grid ------------------------------------------------------------------ */
.mw-cell {
  --mw-cw: .6em;
  --mw-ch: calc(var(--mw-cell-leading, 1.35) * 1em);
  box-sizing: border-box;
  position: relative;
  width: calc(var(--mw-cols, 80) * var(--mw-cw));
  max-width: 100%;
  margin-inline: auto;
  color: var(--mw-cell-ink);
  font-family: var(--mw-cell-font);
  font-size: var(--mw-cell-size, 15px);
  font-weight: 400;
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
  font-kerning: none;
  letter-spacing: 0;
  line-height: var(--mw-ch);
  -webkit-text-size-adjust: 100%;
}
/* One size: hierarchy comes from weight, colour, inverse video and space. */
.mw-cell :is(h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dd, figure, figcaption, pre, blockquote, table, caption, th, td, button, input, select, textarea, summary, small, code, kbd, samp) {
  margin: 0;
  font: inherit;
  letter-spacing: 0;
}
.mw-cell :is(ul, ol) { padding: 0; list-style: none; }
.mw-cell :is(h1, h2, h3, h4, h5, h6, strong, b, th) { font-weight: 700; }
.mw-cell *, .mw-cell *::before, .mw-cell *::after { box-sizing: border-box; }
.mw-cell ::selection { background: var(--mw-cell-accent-bg); color: var(--mw-cell-on-accent); }

/* ---- panes: a frame through the middle of the edge cells, title on the rule --- */
.mw-cell-pane {
  position: relative;
  padding: var(--mw-ch) calc(2 * var(--mw-cw));
  background-color: var(--mw-cell-pane-bg, var(--mw-cell-bg));
}
.mw-cell-pane::before {
  content: "";
  position: absolute;
  inset: calc(var(--mw-ch) / 2) calc(var(--mw-cw) / 2);
  border: 1px solid var(--mw-cell-line);
  pointer-events: none;
}
.mw-cell-pane[data-mw-frame="double"]::before { border: 3px double var(--mw-cell-line); }
.mw-cell-pane[data-mw-frame="heavy"]::before { border-width: 2px; }
.mw-cell-pane[data-mw-frame="round"]::before { border-radius: calc(var(--mw-cw) * .9); }
.mw-cell-pane[data-mw-frame="none"]::before { display: none; }
.mw-cell-pane__title {
  position: absolute;
  top: 0;
  left: calc(2 * var(--mw-cw));
  z-index: 1;
  max-width: calc(100% - 4 * var(--mw-cw));
  padding-inline: var(--mw-cw);
  background-color: var(--mw-cell-pane-bg, var(--mw-cell-bg));
  color: var(--mw-cell-accent);
  font-weight: 700;
  white-space: nowrap;
}

/* ---- rows: side-by-side panes in whole cells (CellGrid sets --mw-span) -------- */
.mw-cell-row {
  display: flex;
  flex-wrap: wrap;
  column-gap: calc(var(--mw-gap, 2) * var(--mw-cw));
  row-gap: var(--mw-ch);
}
.mw-cell-row > * { flex: 1 1 calc(30 * var(--mw-cw)); min-width: 0; }
[data-mw-cell="on"] .mw-cell-row > * { flex: none; width: calc(var(--mw-span, 30) * var(--mw-cw)); }

/* ---- controls: three rows tall (a framed button), at least six cells wide ----- */
.mw-cell-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--mw-cw);
  min-width: calc(6 * var(--mw-cw));
  min-height: calc(3 * var(--mw-ch));
  margin: 0;
  padding: 0 calc(1.5 * var(--mw-cw));
  border: solid transparent;
  border-width: calc(var(--mw-ch) / 2) calc(var(--mw-cw) / 2);
  background: var(--mw-cell-pane-bg, var(--mw-cell-bg)) padding-box;
  box-shadow: inset 0 0 0 1px var(--mw-cell-line);
  color: var(--mw-cell-ink);
  font: inherit;
  font-weight: 700;
  line-height: var(--mw-ch);
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}
/* Pressed and selected read as inverse video, the terminal's own highlight. */
.mw-cell-btn[aria-pressed="true"],
.mw-cell-btn:active {
  background-color: var(--mw-cell-accent-bg);
  box-shadow: none;
  color: var(--mw-cell-on-accent);
}
@media (hover: hover) and (pointer: fine) {
  .mw-cell-btn:hover { background-color: var(--mw-cell-bg3); }
  .mw-cell-btn[aria-pressed="true"]:hover { background-color: var(--mw-cell-accent-bg); }
}
.mw-cell-btn:focus-visible { outline: 2px solid var(--mw-cell-accent); outline-offset: 0; }

.mw-cell-inverse { background-color: var(--mw-cell-accent-bg); color: var(--mw-cell-on-accent); }
.mw-cell-fg-dim { color: var(--mw-cell-dim); }
.mw-cell-fg-accent { color: var(--mw-cell-accent); }
.mw-cell-fg-red { color: var(--mw-cell-red); }
.mw-cell-fg-green { color: var(--mw-cell-green); }
.mw-cell-fg-yellow { color: var(--mw-cell-yellow); }
.mw-cell-fg-blue { color: var(--mw-cell-blue); }
.mw-cell-fg-magenta { color: var(--mw-cell-magenta); }
.mw-cell-fg-cyan { color: var(--mw-cell-cyan); }

/* A status line: segments never shrink into each other. */
.mw-cell-status {
  display: flex;
  flex-wrap: wrap;
  column-gap: calc(2 * var(--mw-cw));
  padding-inline: var(--mw-cw);
  background-color: var(--mw-cell-accent-bg);
  color: var(--mw-cell-on-accent);
}
.mw-cell-status > * { flex: 0 0 auto; white-space: nowrap; }

/* ---- cursor: one cell, blinks under 1 Hz while the visitor is active ----------- */
.mw-cell-cursor {
  display: inline-block;
  width: var(--mw-cw);
  height: var(--mw-ch);
  vertical-align: top;
  background-color: var(--mw-cell-accent-bg);
}
[data-mw-awake] .mw-cell-cursor { animation: mw-cell-blink 1.06s steps(1, end) infinite; }
@keyframes mw-cell-blink { 50% { opacity: 0; } }

/* Palette preview inside a switcher button (CellPalette fills the colours). */
.mw-cell-swatch {
  display: inline-block;
  flex: none;
  width: calc(3 * var(--mw-cw));
  height: calc(var(--mw-ch) * .6);
  vertical-align: middle;
  background: linear-gradient(90deg, var(--mw-swatch-accent, var(--mw-cell-accent)) 0 33.4%, var(--mw-swatch-ink, var(--mw-cell-ink)) 33.4% 66.7%, var(--mw-swatch-bg, var(--mw-cell-bg)) 66.7%);
  box-shadow: 0 0 0 1px var(--mw-cell-line);
}

/* ---- block bar chart (CellBars) ------------------------------------------------- */
.mw-cell-bars { container-type: inline-size; }
.mw-cell-bars__list { margin: 0; padding: 0; list-style: none; }
.mw-cell-bars__item {
  display: grid;
  grid-template-columns: calc(var(--mw-cell-bars-label, 14) * var(--mw-cw)) minmax(0, 1fr) calc(var(--mw-cell-bars-value, 6) * var(--mw-cw));
  column-gap: var(--mw-cw);
  align-items: center;
}
.mw-cell-bars__label { min-width: 0; }
/* The value keeps the last column, with or without the bar beside it. */
.mw-cell-bars__value { grid-column: -2 / -1; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.mw-cell-bars__track {
  --mw-cell-bars-track: var(--mw-cell-line);
  display: block;
  min-width: 0;
  height: var(--mw-ch);
  color: var(--mw-cell-bars-color, var(--mw-cell-accent));
}
.mw-cell-bars__track canvas { display: block; }
/* Narrow: the label takes its own line above the bar. */
@container (max-width: 340px) {
  .mw-cell-bars__item { grid-template-columns: minmax(0, 1fr) calc(var(--mw-cell-bars-value, 6) * var(--mw-cw)); }
  .mw-cell-bars__label { grid-column: 1 / -1; }
}

/* ---- material preset -------------------------------------------------------------
   [data-mw-material="cell"] maps the terminal roles to the design system and
   turns its cards and panels (.ds-card, .mw-surface) into flat framed cell
   panels: no radius, no shadow, a 1 px border in line-strong (3:1), the
   system's monospace. A system only promises 4.5:1 for its text roles, so the
   coloured text roles (logs, diffs) fall back to them; colour stays in marks
   (bars, cursor, swatches). --mw-surface-font (an addition) is the monospace.
   Recomputed below an element that switches data-mw-mode or data-mw-ds. */
[data-mw-material="cell"],
:where([data-mw-material="cell"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-cell-bg: var(--ds-background, #0b0d12);
  --mw-cell-bg2: var(--ds-surface, #12151d);
  --mw-cell-bg3: var(--ds-surface-2, #1b2029);
  --mw-cell-line: var(--ds-line-strong, #626d7f);
  --mw-cell-ink: var(--ds-text, #e8ecf4);
  --mw-cell-dim: var(--ds-text-muted, #a7b1c2);
  --mw-cell-accent: var(--ds-accent-text, #57d9c4);
  --mw-cell-accent-bg: var(--ds-accent, #57d9c4);
  --mw-cell-on-accent: var(--ds-on-accent, #000);
  --mw-cell-red: var(--ds-accent-text, #ff6b7a);
  --mw-cell-green: var(--ds-text, #62d980);
  --mw-cell-yellow: var(--ds-text, #ffc857);
  --mw-cell-blue: var(--ds-accent-text, #6aa9ff);
  --mw-cell-magenta: var(--ds-accent-text, #d69bff);
  --mw-cell-cyan: var(--ds-accent-text, #57d9c4);
  --mw-cell-font: var(--ds-mono-family, ui-monospace, "SF Mono", SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace);
  --mw-cell-bars-color: var(--ds-data-1, var(--mw-cell-accent));
  --mw-surface-tint: var(--mw-cell-bg2);
  --mw-surface-bg: var(--mw-cell-bg2);
  --mw-surface-bg-solid: var(--mw-cell-bg2);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  --mw-surface-edge: 1px solid var(--mw-cell-line);
  --mw-surface-edge-color: var(--mw-cell-line);
  --mw-surface-sheen: none;
  --mw-surface-texture: none;
  --mw-surface-shadow: 0 0 #0000;
  --mw-surface-shadow-in: 0 0 #0000;
  --mw-surface-radius: 0px;
  --mw-surface-ink: var(--mw-cell-ink);
  --mw-surface-ink-muted: var(--mw-cell-dim);
  --mw-surface-font: var(--mw-cell-font);
}
/* A system picks the frame with --ds-material-frame (single, double, heavy,
   round): panes and surfaces follow where style queries are supported, and
   keep the single line elsewhere. */
@container style(--ds-material-frame: double) {
  :where([data-mw-material="cell"]) .mw-cell-pane::before { border: 3px double var(--mw-cell-line); }
  :where([data-mw-material="cell"]) * { --mw-surface-edge: 3px double var(--mw-cell-line); }
}
@container style(--ds-material-frame: heavy) {
  :where([data-mw-material="cell"]) .mw-cell-pane::before { border-width: 2px; }
  :where([data-mw-material="cell"]) * { --mw-surface-edge: 2px solid var(--mw-cell-line); }
}
@container style(--ds-material-frame: round) {
  :where([data-mw-material="cell"]) .mw-cell-pane::before { border-radius: calc(var(--mw-cw) * .9); }
  :where([data-mw-material="cell"]) * { --mw-surface-radius: 6px; }
}
/* The same rule as surface.css, plus the monospace. */
[data-mw-material="cell"] .mw-surface {
  border: var(--mw-surface-edge);
  border-radius: var(--mw-surface-radius);
  background: var(--mw-surface-sheen), var(--mw-surface-texture), var(--mw-surface-bg);
  color: var(--mw-surface-ink);
  box-shadow: var(--mw-surface-shadow-in), var(--mw-surface-shadow);
  font-family: var(--mw-surface-font);
  font-variant-ligatures: none;
}

/* ---- reduced motion and forced colours ------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .mw-cell-cursor { animation: none !important; }
}
@media (forced-colors: active) {
  .mw-cell-btn { outline: 1px solid ButtonText; outline-offset: calc(var(--mw-ch) / -2); }
  .mw-cell-cursor { background-color: CanvasText; }
}
