/* tinymoon — data-display widgets: badges, cards, stats + data tables, and
   empty state. The layer that sits above the base four sheets and styles the
   widgets that render data. Link it fifth, after primitives.css — but only
   when your app renders these widgets. Content-first and pure-control apps
   can omit it. The DOM these style comes from the matching js/ modules.
   Requires tokens.css and base.css. */

/* ---------- badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-5);
  font-weight: var(--weight-medium);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: var(--space-2) var(--space-7);
  border: 1px solid var(--border-2);
  color: var(--text-dim);
  white-space: nowrap;
}

/* Variant chips extend the base .badge. The semantic status color drives the
   border and a soft fill (the 3:1 non-text UI floor); the label stays at the
   high-contrast --text token so the 10px uppercase text always clears 4.5:1 in
   BOTH themes -- saturated status hues (especially light-theme green) cannot
   hold 4.5:1 as small text, so they carry meaning through the border, not the
   glyphs. --gold, added this phase, is the warn hue. */
.badge-ok { color: var(--text); border-color: var(--green); background: color-mix(in srgb, var(--green) 12%, transparent); }
.badge-warn { color: var(--text); border-color: var(--gold); background: var(--gold-soft); }
.badge-err { color: var(--text); border-color: var(--red); background: color-mix(in srgb, var(--red) 12%, transparent); }
.badge-muted { color: var(--text-dim); border-color: var(--border); }
/* .badge-neutral is the bare .badge default (border-2 hairline, dimmed text). */

/* ---------- cards ---------- */

/* Uniform card grid: fixed column width and fixed row height — every card
   is exactly the same size. Cards with less content pad out; cards with
   more truncate gracefully (1-line title with the full text in its
   tooltip). */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(320px, 100%), 1fr));
  grid-auto-rows: 232px;
  gap: var(--space-12);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: var(--space-14) var(--space-14) var(--space-12);
  display: flex;
  flex-direction: column;
  gap: var(--space-10);
  transition: border-color var(--dur-base) ease, box-shadow var(--dur-base) ease;
  position: relative;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}
.card:hover { border-color: var(--border-2); box-shadow: var(--shadow-card); }
/* Active card: the generic highlighted state — accent border, left edge,
   and glow. */
.card.active { border-color: var(--accent); box-shadow: 0 0 20px var(--accent-a12); }
.card.active::before {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent-glow);
}

.card-title-row { display: flex; align-items: center; gap: var(--space-8); }
.card-title {
  font-weight: var(--weight-medium);
  font-size: var(--text-sm);
  font-family: var(--font-ui);
  line-height: var(--leading-tight);
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card .kebab { width: 26px; height: 26px; margin: -2px -4px 0 0; flex: none; }
.card-badges { display: flex; flex-wrap: wrap; gap: var(--space-6); min-height: 22px; overflow: hidden; }
.card .card-badges { flex-wrap: nowrap; }

/* ---------- stats + data tables ---------- */

.report-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: var(--space-1);
  background: var(--border);
  border: 1px solid var(--border);
  margin: var(--space-12) 0;
}
.stat { background: var(--surface-2); padding: var(--space-10) var(--space-12); }
.stat .k {
  font-weight: var(--weight-normal);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  /* --text-dim, not --text-faint: at 10px the muted label must still clear
     4.5:1 once the grain overlay composites onto surface-2 (faint drops to
     ~4.47:1 there; dim holds ~6.5:1). */
  color: var(--text-dim);
  display: block;
  margin-bottom: var(--space-3);
}
.stat .v { font-weight: var(--weight-medium); font-size: var(--text-base); font-family: var(--font-mono); color: var(--text); display: inline-flex; align-items: center; gap: var(--space-5); }
.stat .v small { font-size: var(--text-label); color: var(--text-dim); }
.stat-v-text { color: var(--text); }

/* Trend: an explicit, non-text delta indicator. The direction colors a small
   CSS-drawn triangle (up=good, down=bad) or a dash (neutral) and a left edge on
   the tile -- both non-text (3:1 UI floor), so saturated status hues stay
   legible where colored value text would fail contrast. The value text stays
   --text. Direction is never inferred; the caller states good/bad/neutral. */
.stat.trend-good, .stat.trend-bad, .stat.trend-neutral { border-inline-start: 2px solid var(--border); }
.stat.trend-good { border-inline-start-color: var(--green); }
.stat.trend-bad { border-inline-start-color: var(--red); }
.stat.trend-neutral { border-inline-start-color: var(--gold); }
.stat-delta { display: none; width: 0; height: 0; }
.trend-good .stat-delta { display: inline-block; border-left: 3px solid transparent; border-right: 3px solid transparent; border-bottom: 5px solid var(--green); }
.trend-bad .stat-delta { display: inline-block; border-left: 3px solid transparent; border-right: 3px solid transparent; border-top: 5px solid var(--red); }
.trend-neutral .stat-delta { display: inline-block; width: 6px; height: 2px; background: var(--gold); }

table.data {
  border-collapse: collapse;
  width: 100%;
  font-weight: var(--weight-normal);
  font-size: var(--text-2xs);
  font-family: var(--font-mono);
}
table.data th {
  text-align: start;
  font-weight: var(--weight-medium);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  /* --text-dim, not --text-faint: the 10px uppercase header must clear 4.5:1
     even after the grain overlay composites onto the surface (faint lands at
     ~4.47:1 there). */
  color: var(--text-dim);
  padding: var(--space-7) var(--space-10);
  border-bottom: 1px solid var(--border-2);
  white-space: nowrap;
}
table.data td {
  padding: var(--space-6) var(--space-10);
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
  vertical-align: top;
}
table.data tr:hover td { background: var(--surface-2); color: var(--text); }
.hash { font-weight: var(--weight-normal); font-size: var(--text-micro); font-family: var(--font-mono); color: var(--text-faint); overflow-wrap: anywhere; }

/* Interactive data grid (createTable). role="grid" with roving-tabindex cells:
   a sticky header, a caller-side sort indicator, focus rings on cells, and the
   "N more rows not shown" footer note. */
.tm-table { table-layout: auto; }
.tm-table caption { text-align: start; color: var(--text-dim); font-size: var(--text-label); font-family: var(--font-mono); letter-spacing: 0.12em; text-transform: uppercase; padding: var(--space-6) var(--space-10); }
.tm-table thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 1; }
.tm-table th.sortable { cursor: pointer; user-select: none; }
.tm-table th.sortable:hover { color: var(--text-dim); }
/* Sort caret: a pure-CSS triangle (non-text), so contrast is a 3:1 UI concern. */
.tm-table th[aria-sort]::after {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  margin-inline-start: var(--space-5);
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  vertical-align: middle;
}
.tm-table th[aria-sort="none"]::after { border-bottom: 4px solid var(--text-faint); opacity: 0.3; }
.tm-table th[aria-sort="ascending"]::after { border-bottom: 4px solid var(--accent); }
.tm-table th[aria-sort="descending"]::after { border-top: 4px solid var(--accent); }
.tm-table [role="gridcell"]:focus-visible,
.tm-table [role="columnheader"]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tm-table tfoot .tm-table-more td { text-align: center; font-style: italic; color: var(--text-dim); border-bottom: none; }

/* ---------- virtual list ---------- */

/* Windowed list: a scroll container (contain:strict isolates its layout/paint)
   holding a spacer sized to the full item count; only the rows in view exist in
   the DOM, absolutely positioned at index * rowHeight. Give .tm-vlist a height
   in your app -- contain:strict needs a size to window against. */
.tm-vlist {
  position: relative;
  overflow-y: auto;
  contain: strict;
  border: 1px solid var(--border);
  background: var(--surface);
}
.tm-vlist-spacer { position: relative; width: 100%; }
.tm-vlist-row {
  position: absolute;
  inset-inline: 0;
  top: 0;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: var(--space-8);
  padding: 0 var(--space-10);
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
  font-size: var(--text-2xs);
  font-family: var(--font-mono);
  overflow: hidden;
  white-space: nowrap;
}

/* ---------- empty state ---------- */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-10);
  padding: var(--space-48) var(--space-24);
  color: var(--text-faint);
  border: 1px dashed var(--border-2);
  text-align: center;
}
.empty svg { width: 28px; height: 28px; opacity: 0.5; }
.empty .empty-title { font-weight: var(--weight-medium); font-size: var(--text-xs); font-family: var(--font-ui); color: var(--text-dim); }
.empty .empty-sub { font-weight: var(--weight-normal); font-size: var(--text-3xs); font-family: var(--font-mono); }

/* ---------- tree view (createTree) ---------- */

/* APG TreeView: a role="tree" list of role="treeitem" rows with role="group"
   child lists. Depth indentation is driven by the --tm-tree-depth custom
   property set per row; the twist chevron rotates on the open state. */
.tm-tree, .tm-tree-group { list-style: none; margin: 0; padding: 0; }
.tm-tree { font-size: var(--text-2xs); font-family: var(--font-mono); color: var(--text-dim); }
.tm-tree-row {
  display: flex;
  align-items: center;
  gap: var(--space-6);
  padding: var(--space-4) var(--space-8);
  padding-inline-start: calc(var(--space-8) + var(--tm-tree-depth, 0) * var(--space-16));
  cursor: pointer;
}
.tm-tree-item:focus { outline: none; }
.tm-tree-item:focus-visible > .tm-tree-row { outline: 2px solid var(--accent); outline-offset: -2px; }
.tm-tree-row:hover { background: var(--surface-2); color: var(--text); }
.tm-tree-twist {
  display: inline-flex;
  width: 14px;
  height: 14px;
  flex: none;
  color: var(--text-faint);
  transition: transform var(--dur-base) ease;
}
.tm-tree-twist svg { width: 14px; height: 14px; }
/* Collapsed twist points at the row (chevron rotated -90deg); open points down. */
.tm-tree-item[aria-expanded="false"] > .tm-tree-row .tm-tree-twist { transform: rotate(-90deg); }
.tm-tree-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- filter bar (createFilterBar) ---------- */

/* A layout-only strip: the caller's controls flow left-to-right and wrap on
   narrow viewports. Owns no filter state. */
.tm-filterbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-8);
  padding: var(--space-8) 0;
}
.tm-filterbar-slot { display: flex; align-items: center; min-width: 0; }

/* ---------- chips (createChips) ---------- */

.tm-chips { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-6); }
.tm-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-4) var(--space-3) var(--space-8);
  border: 1px solid var(--border-2);
  background: var(--surface-2);
  color: var(--text);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  letter-spacing: 0.04em;
}
.tm-chip-label { white-space: nowrap; }
.tm-chip-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  padding: 0;
  border: none;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
}
.tm-chip-x svg { width: 12px; height: 12px; }
.tm-chip-x:hover { color: var(--text); }
.tm-chip-x:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tm-chips-clear {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
  padding: var(--space-3) var(--space-6);
}
.tm-chips-clear:hover { color: var(--text); }
.tm-chips-clear:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- load more (createLoadMore) ---------- */

.tm-loadmore { display: flex; flex-direction: column; align-items: center; gap: var(--space-8); padding: var(--space-12) 0; }
.tm-loadmore-btn { display: inline-flex; align-items: center; gap: var(--space-6); }
.tm-loadmore-spin { display: none; width: 14px; height: 14px; }
.tm-loadmore-spin svg { width: 14px; height: 14px; }
.tm-loadmore.loading .tm-loadmore-spin { display: inline-flex; }
.tm-loadmore-error {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  color: var(--red);
  font-size: var(--text-2xs);
  font-family: var(--font-mono);
}
.tm-loadmore-end { color: var(--text-faint); font-size: var(--text-label); font-family: var(--font-mono); text-transform: uppercase; letter-spacing: 0.1em; }

/* ---------- breadcrumbs (createBreadcrumbs) ---------- */

.tm-crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: var(--space-2);
  font-size: var(--text-2xs);
  font-family: var(--font-mono);
}
.tm-crumb { display: inline-flex; align-items: center; gap: var(--space-2); }
/* Chevron separator drawn before every crumb after the first. */
.tm-crumb + .tm-crumb::before {
  content: "";
  display: inline-block;
  width: 5px;
  height: 5px;
  margin-inline: var(--space-4);
  border-top: 1.5px solid var(--text-faint);
  border-right: 1.5px solid var(--text-faint);
  transform: rotate(45deg);
}
.tm-crumb-link { background: none; border: none; padding: 0; font: inherit; color: var(--text-dim); cursor: pointer; text-decoration: none; }
.tm-crumb-link:hover { color: var(--accent); }
.tm-crumb-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tm-crumb-current { color: var(--text); font-weight: var(--weight-medium); }
.tm-crumb-static { color: var(--text-dim); }
.tm-crumb-more { background: none; border: none; padding: 0 var(--space-4); font: inherit; color: var(--text-dim); cursor: pointer; }
.tm-crumb-more:hover { color: var(--text); }
.tm-crumb-more:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- sparkline (createSparkline) ---------- */

/* Colors live ENTIRELY here (the JS emits no color attributes): the line
   strokes the accent, the optional area fills a token-soft accent. */
.tm-spark { display: inline-block; width: 120px; height: 32px; overflow: visible; }
.tm-spark-line { fill: none; stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; stroke-linejoin: round; stroke-linecap: round; }
.tm-spark-area { fill: var(--accent-soft); stroke: none; }

/* ---------- chart container (createChartContainer) ---------- */

/* Renderer-agnostic lifecycle shell. The --chart-margin-* tokens are the
   margin convention render(ctx) reads; override them per chart. The container
   never draws — .tm-chart-draw is where the caller's renderer paints. */
.tm-chart {
  --chart-margin-top: 8px;
  --chart-margin-right: 8px;
  --chart-margin-bottom: 20px;
  --chart-margin-left: 32px;
  position: relative;
  width: 100%;
  min-height: 120px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.tm-chart-draw { position: absolute; inset: 0; }

/* ---------- feed / log viewer (createFeed) ---------- */

.tm-feed {
  position: relative;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: var(--text-2xs);
  font-family: var(--font-mono);
}
.tm-feed-list { display: flex; flex-direction: column; }
.tm-feed-item {
  padding: var(--space-4) var(--space-10);
  border-inline-start: 2px solid transparent;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
}
/* Severity hooks: the caller sets data-level on the item node; the far-left
   edge picks up the matching status token (non-text 3:1 signal). */
.tm-feed-item[data-level="info"] { border-inline-start-color: var(--accent); }
.tm-feed-item[data-level="success"] { border-inline-start-color: var(--green); }
.tm-feed-item[data-level="warn"] { border-inline-start-color: var(--gold); }
.tm-feed-item[data-level="error"] { border-inline-start-color: var(--red); color: var(--text); }
/* Jump-to-latest: floats bottom-center, shown only while autoscroll is paused. */
.tm-feed-jump {
  position: sticky;
  bottom: var(--space-8);
  align-self: center;
  margin: 0 auto;
  display: inline-flex;
  padding: var(--space-4) var(--space-10);
  border: 1px solid var(--accent);
  background: var(--surface-2);
  color: var(--accent);
  font-size: var(--text-label);
  font-family: var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
}
.tm-feed-jump:hover { background: var(--surface-3); }
.tm-feed-jump:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* The `hidden` attribute must win over the display rules above. A bare
   `[hidden]` (specificity 0,1,0) loses to a class with `display:` set, so
   re-assert it at class+attribute specificity for the toggled elements. */
.tm-loadmore-btn[hidden],
.tm-loadmore-error[hidden],
.tm-feed-jump[hidden] { display: none; }
