/*
 * Answer block: ```sheet (a spreadsheet-quality data table), and the plain
 * markdown tables the "tables" pass wraps (.ab-mdtable). Tokens in blocks.css.
 *
 * A sheet sits in the reply's reading column like a figure: a quiet title,
 * an "example numbers" tag, the table in one rounded hairline frame, the
 * colour key and two small actions under it, then the formula rows. The
 * table scrolls inside its frame (never the page) with a fade on the edge
 * that has more; past ~16 rows it scrolls down too, under a sticky header.
 *
 * Every table rule is prefixed body:not(#_) (one ID of weight): styles.css
 * styles every `.msg-content table/th/td` at (0,3,2) - display:block,
 * accent-tinted headers, zebra rows, 22px corners from rounded.css - and a
 * sheet must not inherit any of it.
 */

.ab-sheet,
.ab-mdtable {
  --abs-line: var(--ab-hair);
  --abs-head: color-mix(in srgb, var(--ab-ink) 4.5%, var(--ab-card));
  --abs-chrome: color-mix(in srgb, var(--ab-ink) 3%, var(--ab-card));
  --abs-hover: color-mix(in srgb, var(--ab-ink) 3.5%, var(--ab-card));
  --abs-shadow: color-mix(in srgb, var(--ab-ink) 16%, transparent);
  --abs-group: color-mix(in srgb, var(--ab-ink) 30%, transparent);
  --abs-letter-h: 24px;
  font-family: var(--ab-font-ui);
  color: var(--ab-ink);
}
.ab-sheet { container-type: inline-size; }
.ab-mdtable { margin: 0.9em 0 1em; max-width: 100%; min-width: 0; }

/* Visually hidden, still read aloud (captions, role words, live status). */
.ab-sheet-vh {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%);
  white-space: nowrap;
}

/* ── Title and the example-numbers tag ─────────────────────────────── */
.ab-sheet-top {
  display: flex; flex-direction: column; align-items: flex-start; gap: 7px;
  margin: 0 0 10px;
}
.ab-sheet-title {
  font: 600 15px/1.35 var(--ab-font-ui);
  letter-spacing: -0.005em;
  color: var(--ab-ink);
  text-wrap: balance;
}
.ab-sheet-note {
  display: inline-block;
  max-width: 100%;
  padding: 3px 10px;
  border-radius: 12px;
  background: var(--ab-warn-soft);
  color: var(--ab-warn);
  font: 600 12px/1.45 var(--ab-font-ui);
  overflow-wrap: anywhere;
}

/* ── The frame: rounded hairline, scrolls inside, fades on the far edge ─ */
.ab-sheet-frame {
  position: relative;
  border: 1px solid var(--abs-line);
  border-radius: var(--ab-radius);
  background: var(--ab-card);
  overflow: hidden;
}
.ab-sheet-scroll {
  overflow: auto;
  max-height: min(70vh, 560px);
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--ab-rule) transparent;
  -webkit-overflow-scrolling: touch;
}
.ab-mdtable .ab-sheet-scroll { max-height: none; }
.ab-sheet-scroll:focus-visible {
  outline: 2px solid var(--ab-accent);
  outline-offset: -2px;
  border-radius: var(--ab-radius);
}
.ab-sheet-fade {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.18s ease;
  z-index: 4;
}
.ab-sheet-fade-l, .ab-sheet-fade-r { top: 0; bottom: 0; width: 28px; }
.ab-sheet-fade-l { left: 0; background: linear-gradient(to right, var(--ab-card), transparent); }
.ab-sheet-fade-r { right: 0; background: linear-gradient(to left, var(--ab-card), transparent); }
.ab-sheet-fade-b { left: 0; right: 0; bottom: 0; height: 26px; background: linear-gradient(to top, var(--ab-card), transparent); }
.ab-sheet-frame.is-l .ab-sheet-fade-l,
.ab-sheet-frame.is-r .ab-sheet-fade-r,
.ab-sheet-frame.is-b .ab-sheet-fade-b { opacity: 1; }
/* With row numbers, the frozen number column carries the left edge instead. */
.ab-sheet-frame:has(.is-grid) .ab-sheet-fade-l { display: none; }

/* ── The sheet table ───────────────────────────────────────────────── */
body:not(#_) .ab-sheet table.ab-sheet-table {
  display: table;
  width: 100%;
  margin: 0;
  border: 0;
  border-radius: 0;
  border-collapse: separate;
  border-spacing: 0;
  overflow: visible;
  background: transparent;
  font: 400 13.5px/1.4 var(--ab-font-ui);
  color: var(--ab-ink);
  text-align: left;
}
/* ── The sheet table ───────────────────────────────────────────────── */
body:not(#_) .ab-sheet table.ab-sheet-table { font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1; }
body:not(#_) .ab-sheet .ab-sheet-table :is(th, td) {
  padding: 6px 12px;
  border: 0;
  border-bottom: 1px solid var(--abs-line);
  background: var(--ab-card);
  color: inherit;
  font: inherit;
  text-align: left;
  vertical-align: baseline;
  letter-spacing: 0;
  white-space: nowrap;
}
body:not(#_) .ab-sheet .ab-sheet-table :is(th, td).is-num { text-align: right; }
/* Short words stay on one line; a column of long text wraps as a paragraph
   with a floor width (the span: a cell's own min-width is not reliable). */
body:not(#_) .ab-sheet .ab-sheet-table td.is-long { white-space: normal; }
.ab-sheet-para {
  display: inline-block;
  min-width: 11em;
  max-width: 30em;
  white-space: normal;
  overflow-wrap: break-word;
  vertical-align: top;
}
body:not(#_) .ab-sheet .ab-sheet-table tbody tr { background: transparent; }
body:not(#_) .ab-sheet .ab-sheet-table tbody tr:last-child > * { border-bottom: 0; }

/* Header row: distinct, sticky while the body scrolls under it. */
body:not(#_) .ab-sheet .ab-sheet-table thead :is(th, td) {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--abs-head);
}
body:not(#_) .ab-sheet .ab-sheet-table thead tr.ab-sheet-names > * {
  border-bottom: 1px solid var(--ab-rule);
  font-weight: 600;
  font-size: 13px;
  line-height: 1.35;
  vertical-align: bottom;
}
body:not(#_) .ab-sheet .ab-sheet-table thead tr.ab-sheet-letters + tr.ab-sheet-names > * { top: var(--abs-letter-h); }
.ab-sheet-frame.is-t .ab-sheet-table thead tr:last-child > * { box-shadow: 0 6px 8px -7px var(--abs-shadow); }

/* Role colours: the same quantity, the same colour, across the whole reply. */
body:not(#_) .ab-sheet .ab-sheet-table th.is-measure { color: var(--ab-measure); }
body:not(#_) .ab-sheet .ab-sheet-table th.is-calc { color: var(--ab-calc); }
body:not(#_) .ab-sheet .ab-sheet-table th.is-given { color: var(--ab-given); }
body:not(#_) .ab-sheet .ab-sheet-table th.is-ignore { color: var(--ab-ignore); font-weight: 500; }
body:not(#_) .ab-sheet .ab-sheet-table td.is-calc { color: var(--ab-calc); font-weight: 600; }
body:not(#_) .ab-sheet .ab-sheet-table td.is-ignore { color: var(--ab-ignore); }
body:not(#_) .ab-sheet .ab-sheet-table td.is-blank { color: var(--ab-muted); }
body:not(#_) .ab-sheet .ab-sheet-table td.is-strong { font-weight: 650; }

/* Groups and highlights. */
body:not(#_) .ab-sheet .ab-sheet-table tr.is-group > * { box-shadow: inset 0 1.5px 0 var(--abs-group); }
body:not(#_) .ab-sheet .ab-sheet-table tr.is-hl > td { background: var(--ab-calc-soft); }
body:not(#_) .ab-sheet .ab-sheet-table tr.is-hl > th.ab-sheet-rn {
  background: linear-gradient(var(--ab-calc-soft), var(--ab-calc-soft)), var(--abs-chrome);
  color: var(--ab-calc);
}
@media (hover: hover) {
  body:not(#_) .ab-sheet .ab-sheet-table tbody tr:not(.is-hl):hover > td { background: var(--abs-hover); }
}

/* Spreadsheet chrome (grid: on): letters, row numbers, cell lines. */
body:not(#_) .ab-sheet .ab-sheet-table.is-grid :is(th, td) { border-right: 1px solid var(--abs-line); }
body:not(#_) .ab-sheet .ab-sheet-table.is-grid tr > :last-child { border-right: 0; }
body:not(#_) .ab-sheet .ab-sheet-table tr.ab-sheet-letters > * {
  height: var(--abs-letter-h);
  box-sizing: border-box;
  padding: 0 8px;
  background: var(--abs-chrome);
  color: var(--ab-muted);
  font: 500 11px/1 var(--ab-font-mono);
  text-align: center;
  vertical-align: middle;
  letter-spacing: 0.02em;
}
body:not(#_) .ab-sheet .ab-sheet-table .ab-sheet-rn {
  position: sticky;
  left: 0;
  z-index: 1;
  width: 1%;
  min-width: 34px;
  padding: 0 8px;
  background: var(--abs-chrome);
  color: var(--ab-muted);
  font: 500 11px/1 var(--ab-font-mono);
  text-align: center;
  vertical-align: middle;
}
body:not(#_) .ab-sheet .ab-sheet-table thead .ab-sheet-rn,
body:not(#_) .ab-sheet .ab-sheet-table .ab-sheet-corner {
  position: sticky;
  left: 0;
  z-index: 3;
  background: var(--abs-chrome);
}
body:not(#_) .ab-sheet .ab-sheet-table thead tr.ab-sheet-names > .ab-sheet-rn {
  font: 500 11px/1 var(--ab-font-mono);
  vertical-align: middle;
}
.ab-sheet-frame.is-l .ab-sheet-table :is(.ab-sheet-rn, .ab-sheet-corner) {
  box-shadow: 6px 0 8px -7px var(--abs-shadow);
}

/* A formula row under the pointer shows where it goes. */
body:not(#_) .ab-sheet .ab-sheet-table .is-col-hl { background-image: linear-gradient(var(--ab-calc-soft), var(--ab-calc-soft)); }
body:not(#_) .ab-sheet .ab-sheet-table th.ab-sheet-col.is-col-hl { color: var(--ab-calc); font-weight: 700; }
body:not(#_) .ab-sheet .ab-sheet-table td.is-target {
  box-shadow: inset 0 0 0 2px var(--ab-calc);
  border-radius: 4px;
}

/* ── Under the table: colour key, tab, actions ─────────────────────── */
.ab-sheet-foot {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 8px 16px;
  margin-top: 8px;
}
.ab-sheet-meta {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px;
  min-width: 0;
}
.ab-sheet-key {
  display: flex; flex-wrap: wrap; gap: 4px 14px;
  font: 500 12.5px/1.4 var(--ab-font-ui);
  color: var(--ab-muted);
}
.ab-sheet-key-item { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.ab-sheet-dot { width: 9px; height: 9px; border-radius: 50%; background: currentColor; flex: none; }
.ab-sheet-key-item.is-measure .ab-sheet-dot { background: var(--ab-measure); }
.ab-sheet-key-item.is-calc .ab-sheet-dot { background: var(--ab-calc); }
.ab-sheet-key-item.is-given .ab-sheet-dot { background: var(--ab-given); }
.ab-sheet-key-item.is-ignore .ab-sheet-dot { background: var(--ab-ignore); opacity: 0.6; }
.ab-sheet-tab {
  display: inline-flex; align-items: center;
  height: 24px; padding: 0 10px;
  border: 1px solid var(--abs-line);
  border-radius: 8px;
  background: var(--ab-card);
  color: var(--ab-ink);
  font: 500 12px/1 var(--ab-font-mono);
  box-shadow: inset 0 -2px 0 var(--ab-calc);
}
.ab-sheet-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }

/* Small quiet buttons. Weighted past the app's button catch-alls (pill
   corners at (0,3,1) !important, a faded label when disabled). */
body:not(#_) .ab-sheet-foot button.ab-sheet-btn.ab-sheet-btn,
body:not(#_) .ab-sheet-frow button.ab-sheet-btn.ab-sheet-btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 30px;
  min-width: 0;
  margin: 0;
  padding: 0 12px 0 10px;
  border: 1px solid var(--ab-hair) !important;
  border-radius: 999px !important;
  background: transparent !important;
  box-shadow: none !important;
  color: var(--ab-muted) !important;
  font: 500 12.5px/1 var(--ab-font-ui) !important;
  letter-spacing: 0 !important;
  text-transform: none !important;
  white-space: nowrap;
  opacity: 1 !important;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}
body:not(#_) :is(.ab-sheet-foot, .ab-sheet-frow) button.ab-sheet-btn.ab-sheet-btn:hover {
  color: var(--ab-ink) !important;
  border-color: var(--ab-rule) !important;
  background: var(--abs-hover) !important;
}
body:not(#_) :is(.ab-sheet-foot, .ab-sheet-frow) button.ab-sheet-btn.ab-sheet-btn:focus-visible {
  outline: 2px solid var(--ab-accent) !important;
  outline-offset: 2px;
}
body:not(#_) :is(.ab-sheet-foot, .ab-sheet-frow) button.ab-sheet-btn.ab-sheet-btn.is-done {
  color: var(--ab-good) !important;
  border-color: color-mix(in srgb, var(--ab-good) 45%, transparent) !important;
  background: var(--ab-good-soft) !important;
}
body:not(#_) :is(.ab-sheet-foot, .ab-sheet-frow) button.ab-sheet-btn.ab-sheet-btn.is-failed {
  color: var(--ab-warn) !important;
  border-color: color-mix(in srgb, var(--ab-warn) 45%, transparent) !important;
}
.ab-sheet-ico { display: inline-flex; width: 14px; height: 14px; flex: none; }
.ab-sheet-ico svg { width: 14px; height: 14px; border-radius: 0; }

/* ── Formula rows ──────────────────────────────────────────────────── */
.ab-sheet-formulas {
  display: flex; flex-direction: column; gap: 6px;
  margin-top: 12px;
}
.ab-sheet-frow {
  display: grid;
  grid-template-columns: minmax(7.5rem, 13rem) minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px 12px;
  padding: 6px 6px 6px 12px;
  border: 1px solid var(--abs-line);
  border-radius: var(--ab-radius-sm);
  background: var(--ab-card);
  min-width: 0;
}
.ab-sheet-frow.is-bare { grid-template-columns: minmax(0, 1fr) auto; }
.ab-sheet-flabel {
  min-width: 0;
  font: 400 13px/1.35 var(--ab-font-ui);
  color: var(--ab-muted);
  overflow-wrap: anywhere;
}
.ab-sheet-fcell {
  font: 600 12.5px/1 var(--ab-font-mono);
  color: var(--ab-ink);
  margin-right: 2px;
}
.ab-sheet-code {
  display: block;
  min-width: 0;
  padding: 3px 0;
  overflow-x: auto;
  white-space: nowrap;
  font: 500 12.5px/1.5 var(--ab-font-mono);
  color: var(--ab-ink);
  scrollbar-width: thin;
  -webkit-user-select: all;
  user-select: all;
}
body:not(#_) .ab-sheet-frow button.ab-sheet-btn.ab-sheet-btn { height: 28px; padding: 0 10px 0 9px; }
@container (max-width: 520px) {
  .ab-sheet .ab-sheet-actions { margin-left: 0; }
  .ab-sheet-frow { grid-template-columns: minmax(0, 1fr) auto; }
  .ab-sheet-frow .ab-sheet-code { grid-column: 1 / -1; grid-row: 2; }
  .ab-sheet-frow.is-bare .ab-sheet-code { grid-column: 1; grid-row: 1; }
}

/* Cut notice: what was shortened, and the whole source one tap away. */
.ab-sheet-cut {
  margin-top: 10px;
  font: 400 12.5px/1.45 var(--ab-font-ui);
  color: var(--ab-muted);
}
.ab-sheet-cut p { margin: 0 0 4px; }
.ab-sheet-cut summary { cursor: pointer; border-radius: 6px; }
.ab-sheet-cut summary:focus-visible { outline: 2px solid var(--ab-accent); outline-offset: 2px; }
body:not(#_) .ab-sheet-cut pre {
  margin: 6px 0 0; padding: 10px 12px;
  max-height: 260px; overflow: auto;
  border: 0; border-radius: var(--ab-radius-sm);
  background: var(--ab-paper) !important;
  font: 12px/1.45 var(--ab-font-mono);
  white-space: pre-wrap;
}

/* ── Plain markdown tables (the "tables" pass) ─────────────────────── */
/* Same rounded frame and fade as a sheet; the table inside keeps the
   reply's own look (styles.css), minus the display:block and the 22px
   corners it needed when it was its own scroller. Wrapping takes it out
   of chatclean's `.msg-content > table`, so the reply's serif is restated. */
body:not(#_) .ab-mdtable table {
  display: table;
  width: 100%;
  margin: 0;
  border: 0;
  border-radius: 0;
  overflow: visible;
}
body:not(#_) .view-chat .msg.assistant .msg-content .ab-mdtable table {
  font-family: var(--cc-serif, 'Source Serif 4', Georgia, 'Times New Roman', serif);
  font-size: 15px;
  line-height: 1.5;
}
body:not(#_) .ab-mdtable table th { font-weight: 600; }
body:not(#_) .ab-mdtable table thead { position: static; }
body:not(#_) .ab-mdtable table :is(th, td).ab-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1;
}
.ab-mdtable .ab-sheet-foot { margin-top: 6px; }
body:not(#_) .ab-mdtable-foot button.ab-sheet-btn.ab-sheet-btn { height: 28px; }

@media (prefers-reduced-motion: reduce) {
  .ab-sheet-fade,
  body:not(#_) :is(.ab-sheet-foot, .ab-sheet-frow) button.ab-sheet-btn.ab-sheet-btn { transition: none; }
}
@media print {
  .ab-sheet-scroll { max-height: none; overflow: visible; }
  .ab-sheet-actions, .ab-sheet-fade, .ab-sheet-frow button { display: none; }
}
