:root {
  color-scheme: light dark;
  /* Each colour is light-dark(light, dark). Status colours clear 4.5:1 on
     their own theme's surface; tints are derived from them with color-mix. */
  --bg:            light-dark(#f4f4f5, #0a0a0a);
  --surface:       light-dark(#ffffff, #1a1a1a);
  --surface-alt:   light-dark(#eaeaec, #111);
  --border:        light-dark(#e2e2e5, #222);
  --border-strong: light-dark(#d2d2d6, #333);
  --text:          light-dark(#1a1a1a, #e0e0e0);
  --muted:         light-dark(#6a6a70, #888);
  --muted-dim:     light-dark(#9a9aa0, #555);
  --accent:        light-dark(#2563eb, #5b9cf6);
  --on-accent:     #fff;
  --danger:        light-dark(#b91c1c, #e06c75);
  --info:          light-dark(#1d4ed8, #61afef);
  --warn:          light-dark(#b45309, #f59e0b);
  --ok:            light-dark(#047857, #10b981);
  --kind-shell:    var(--accent);
  --kind-file:     var(--ok);
  --kind-other:    light-dark(#7e22ce, #a855f7);
  --kind-skill:    var(--warn);
  --chart-0:       light-dark(#2563eb, #5b9cf6);
  --chart-1:       light-dark(#ea580c, #f6a75b);
  --chart-2:       light-dark(#16a34a, #6fce8f);
  --chart-3:       light-dark(#db2777, #ce6f9e);
  --chart-4:       light-dark(#7c3aed, #b58bf6);
  --chart-5:       light-dark(#a16207, #cec36f);
  --row-hover:     light-dark(rgba(0, 0, 0, 0.025), rgba(255, 255, 255, 0.035));
  --hover-tint:    light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.06));
  --shadow:        0 1px 3px light-dark(rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0.4));
  --scroll-shadow: light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6));
  --font-mono:     ui-monospace, "SF Mono", Menlo, monospace;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, sans-serif;
  font-size: 0.85rem;
  min-height: 100vh;
}

/* Mobile first: logo and version share the top row and the sections take a
   full row beneath it; from 640px all three sit on one row. A section's
   underline sits on the header's bottom border. */
header {
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 16px;

  h1 {
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: -0.5px;

    a { color: inherit; text-decoration: none; }
    span { color: var(--accent); }
  }

  /* Live-status mark beside the logo: a pill with a pulsing dot. */
  .indicator {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-left: 8px;
    padding: 2px 7px;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    vertical-align: middle;
    cursor: help;

    &::before {
      content: "";
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: var(--accent);
      animation: indicator-pulse 2s ease-in-out infinite;
    }
  }

  .version {
    margin-left: auto;
    padding: 2px 8px;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.7rem;
    text-decoration: none;
  }
  a.version:hover { color: var(--accent); border-color: var(--accent); }

  .sections {
    order: 1;
    flex-basis: 100%;
    display: flex;
    gap: 4px;

    a {
      color: var(--muted);
      text-decoration: none;
      font-size: 0.9rem;
      font-weight: 500;
      padding: 12px 10px;
      border-bottom: 2px solid transparent;
      transition: color 0.15s, border-color 0.15s;

      &:hover { color: var(--text); }
      &[aria-current] { color: var(--accent); border-bottom-color: var(--accent); }
    }
  }
}

@media (min-width: 640px) {
  header {
    padding: 0 20px;
    flex-wrap: nowrap;
    gap: 20px;

    .sections { order: 0; flex-basis: auto; }
  }
}

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
@media (min-width: 640px) { .container { padding: 16px; gap: 20px; } }

footer.container { color: var(--muted-dim); font-size: 0.75rem; text-align: center; padding-top: 4px; padding-bottom: 20px; }

.hero {
  padding: 2px 0;

  h2 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--accent);
    margin-bottom: 4px;
  }
  p { color: var(--muted); font-size: 0.85rem; }
  a {
    color: var(--accent);
    text-decoration: none;

    &:hover { text-decoration: underline; }
  }
}

#stats { display: flex; flex-direction: column; gap: 20px; }
#stats.stale::before {
  content: "Not updating — cairde did not answer. Showing the last data received.";
  padding: 10px 14px;
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--warn);
  border-radius: 8px;
  background: var(--surface);
  font-size: 0.8rem;
}


/* Home: the status banner and each section summary are one link, a whole-card
   tap target; alerts are rows in one card, each its own link. */
.card.attention { border-left: 3px solid var(--warn); }
.card.attention.attention-bad { border-left-color: var(--danger); }
.card.attention h3 { margin-bottom: 4px; }
.alerts { list-style: none; display: grid; }
.alert {
  padding: 10px 0;
  border-top: 1px solid var(--border);

  &:first-child { border-top: 0; }
}
.alert-text {
  color: var(--text);
  text-decoration: none;

  &::after { content: " →"; color: var(--muted); }
  &:hover { color: var(--accent); }
}

/* Match and machine lists: on a phone each row wraps its detail under its
   name; from 640px rows share one subgrid so columns line up down the list. */
.match-rows, .machine-list {
  list-style: none;
  display: grid;
  font-size: 0.8rem;

  li {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 2px 10px;
    align-items: baseline;
    padding: 7px 0;
    border-top: 1px solid var(--border);
  }
  li:first-child { border-top: 0; }
}
.match-rows {
  margin-top: 8px;
  color: var(--muted);

  .match-rule { color: var(--text); font-weight: 600; }
  .alert-detections & .match-rule { color: var(--danger); }
  .match-machine { color: var(--text); }
  .match-ago { text-align: right; }
  .match-where { grid-column: 1 / -1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
.matches-more { display: inline-block; margin-top: 4px; font-size: 0.8rem; color: var(--accent); text-decoration: none; }
.machine-list {
  li { align-items: center; padding: 8px 0; }
  .machine-name { font-size: 0.9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .machine-seen { color: var(--muted); text-align: right; }
  .machine-stat { grid-column: 2 / -1; color: var(--muted); }
  .machine-stat b { color: var(--text); font-variant-numeric: tabular-nums; }
}
.pulse {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted-dim);

  &.live { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
}
@media (min-width: 640px) {
  .match-rows, .machine-list {
    li { grid-column: 1 / -1; grid-template-columns: subgrid; column-gap: 24px; }
  }
  .match-rows {
    grid-template-columns: auto auto minmax(0, 1fr) auto;

    .match-where { grid-area: 1 / 3; }
    .match-ago { grid-area: 1 / 4; }
  }
  .machine-list {
    grid-template-columns: auto minmax(0, 1fr) auto auto auto;

    .machine-stat { grid-column: auto; text-align: right; }
    .machine-seen { grid-area: 1 / 5; }
  }
}

/* The protection verdict: one line, coloured by level, the first thing on Home. */
a.status {
  --level: var(--muted);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid var(--border-strong);
  border-left: 4px solid var(--level);
  border-radius: 10px;
  background: var(--surface) linear-gradient(90deg, color-mix(in srgb, var(--level) 10%, transparent), transparent 60%);
  color: var(--text);
  text-decoration: none;
  box-shadow: var(--shadow);

  &.status-ok { --level: var(--ok); }
  &.status-warn { --level: var(--warn); }
  &.status-bad { --level: var(--danger); }
  &:hover { border-color: var(--level); }

  .status-dot {
    flex: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--level);
    box-shadow: 0 0 0 0 color-mix(in srgb, var(--level) 50%, transparent);
    animation: status-pulse 2.4s ease-out infinite;
  }
  &.status-idle .status-dot { animation: none; }
  .status-text { display: flex; flex-direction: column; gap: 2px; }
  strong { font-size: 1.05rem; }
  .status-detail { color: var(--muted); font-size: 0.8rem; }
}
@keyframes status-pulse {
  70% { box-shadow: 0 0 0 9px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}
@media (prefers-reduced-motion: reduce) {
  a.status .status-dot { animation: none; }
}

/* minmax(0, …): a wide histogram scrolls inside its panel instead of widening
   the column. */
.panels { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.panel h3 a { color: inherit; text-decoration: none; &:hover { color: var(--accent); } }
a.bar-row { color: inherit; text-decoration: none; &:hover .bar-label { color: var(--accent); } }
/* A badged label truncates the name, never the badge: the badge is what marks a Detection. */
.bar-label:has(.badge) {
  display: flex;
  align-items: center;
  gap: 6px;

  .bar-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
  .badge { flex: none; }
}
.panel .bars { grid-template-columns: minmax(0, min(11em, 40%)) 1fr auto; }

.summaries { display: grid; gap: 12px; }
.card.summary {
  display: block;
  color: inherit;
  text-decoration: none;

  &:hover { border-color: var(--accent); }
  .pace { margin-top: 8px; }
}
a.card.stat { color: inherit; text-decoration: none; }
a.card.stat:hover { border-color: var(--accent); }

/* A summary is a third of the width on desktop, so it keeps the phone's stat
   sizing rather than the wider grid's. */
.card.summary .stat-grid { grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 8px; }
.card.summary .stat-value { font-size: 1.2rem; }

@media (min-width: 640px) {
  .panels { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel-wide { grid-column: 1 / -1; }
}
/* Three summaries fit side by side only once each has room for its stats on
   one line; below that they stack. */
@media (min-width: 960px) {
  .summaries { grid-template-columns: repeat(3, 1fr); }
}

/* Mobile first: tabs share the row equally so every one is visible; the row
   still scrolls if labels ever outgrow it. From 640px they size to content. */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;

  &::-webkit-scrollbar { display: none; }
}

.tab {
  flex: 1 0 auto;
  padding: 12px 8px;
  text-align: center;
  text-decoration: none;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;

  &:hover { color: var(--text); }
  &[aria-current] { color: var(--accent); border-bottom-color: var(--accent); }
}

@media (min-width: 640px) {
  .hero h2 { font-size: 1.6rem; margin-bottom: 8px; }
  .hero p { font-size: 0.9rem; }
  .tabs { gap: 4px; }
  .tab { flex: 0 0 auto; padding: 8px 16px; }
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  font-weight: 600;
  vertical-align: middle;
  margin-left: 6px;
  color: var(--muted);
}
.badge.kind-shell { background: color-mix(in srgb, var(--kind-shell) 15%, transparent); color: var(--kind-shell); }
.badge.kind-file  { background: color-mix(in srgb, var(--kind-file) 15%, transparent); color: var(--kind-file); }
.badge.kind-other { background: color-mix(in srgb, var(--kind-other) 15%, transparent); color: var(--kind-other); }
.badge.kind-skill { background: color-mix(in srgb, var(--kind-skill) 15%, transparent); color: var(--kind-skill); }

.facets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.badge.facet {
  margin-left: 0;
  text-decoration: none;
  border: 1px solid transparent;
  background: var(--surface-alt);
  color: var(--muted);

  &:hover { color: var(--text); }
  &.active { border-color: currentColor; }
}

.card-header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
@media (min-width: 640px) {
  .card-header { flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; }
}
.card-header h3,
.card .card-header h3 {
  margin-bottom: 0;
}

/* Every field shares one look. On a phone the text is 16px: iOS zooms the
   page into any field smaller than that when it takes focus. */
input:is([type="text"], [type="search"], [type="number"], [type="date"]), select {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  min-width: 0;

  &::placeholder { color: var(--muted-dim); }
  &:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
  &:user-invalid { border-color: var(--danger); }
}

.filter-input { width: 100%; }

@media (min-width: 640px) {
  input:is([type="text"], [type="search"], [type="number"], [type="date"]), select {
    padding: 8px 12px;
    font-size: 0.85rem;
  }
  .filter-input { max-width: 320px; }
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
  font-size: 0.85rem;

  a, span { padding: 6px 8px; }
  a { color: var(--accent); text-decoration: none; }
  a:hover { text-decoration: underline; }
  .disabled { color: var(--muted-dim); }
}
@media (min-width: 640px) {
  .pagination { justify-content: flex-end; font-size: 0.8rem; }
  .pagination a, .pagination span { padding: 0; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  box-shadow: var(--shadow);

  h3 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 12px;
  }
}

/* Three across on a phone; wider screens fit more per row. */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 8px;
}

.stat { display: flex; flex-direction: column; gap: 4px; }
.stat-label { color: var(--muted); font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--accent); }

@media (min-width: 640px) {
  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
  .stat-label { font-size: 0.75rem; }
  .stat-value { font-size: 1.5rem; }
}

.stat-value.muted { color: var(--muted); font-size: 1.15rem; font-style: italic; }

/* A table wider than its card scrolls; a shadow marks each edge with more
   content past it. The "local" covers scroll with the table and hide the
   "scroll" shadows once an edge is reached. */
.table-wrap {
  overflow-x: auto;
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) left / 24px 100% no-repeat local,
    linear-gradient(to left, var(--surface) 30%, transparent) right / 24px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, var(--scroll-shadow), transparent) left / 10px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, var(--scroll-shadow), transparent) right / 10px 100% no-repeat scroll;
}
table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}
@media (min-width: 640px) {
  .card { padding: 16px; border-radius: 10px; }
  th, td { padding: 8px 10px; }
}
th { color: var(--muted); font-weight: 600; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.5px; white-space: nowrap; }
tbody tr:hover { background: var(--row-hover); }
tr:last-child td { border-bottom: none; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.mono { font-family: var(--font-mono); font-size: 0.8rem; color: var(--muted); overflow-wrap: break-word; }
/* Free text with no natural break points, e.g. a regex: break anywhere rather
   than widen its table. Identifiers keep break-word, which splits at hyphens. */
.wrap-any { overflow-wrap: anywhere; }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); }
.empty {
  color: var(--muted-dim);
  font-style: italic;

  a {
    color: var(--accent);
    text-decoration: underline;
    font-style: normal;
    margin-left: 4px;

    &:hover {
      color: var(--text);
    }
  }
}

.log-table { table-layout: fixed; }
.log-table col.col-time { width: 7em; }
.log-table col.col-session { width: 11em; }
.log-table col.col-tool { width: 14em; }
.log-table col.col-cmd { width: auto; }

.log-table td.cell-session {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-table td.cell-tool {
  overflow: hidden;
  white-space: nowrap;
}

.cell-tool-inner {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  max-width: 100%;
}

.cell-tool-inner .tool-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cell-tool-inner .badge {
  flex-shrink: 0;
  margin-left: 0;
}

.cell-time {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.cell-time .date {
  display: block;
  font-size: 0.7rem;
  color: var(--muted-dim);
  line-height: 1.2;
}
.cell-time .clock {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.2;
}

.note { color: var(--muted); font-weight: 400; font-size: 0.75rem; }

.rule-form .rule-fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.rule-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.75rem; color: var(--muted); }
/* Detect never substitutes, so its form asks for no replacement. */
.rule-form:has(select[name="mode"] option[value="detect"]:checked) .replacement-field { display: none; }
/* The hint names what the chosen Mode does; touch has no hover for a title. */
.mode-hint.detect,
.mode-field:has(option[value="detect"]:checked) .mode-hint.redact { display: none; }
.mode-field:has(option[value="detect"]:checked) .mode-hint.detect { display: inline; }
.pattern-tester { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.pattern-tester textarea { width: 100%; font: inherit; font-family: var(--font-mono); }
.pattern-tester mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit; }

/* A .panel-table is one line per item at every width; the item's first cell
   holds a <details> whose summary is stretched over the line, and opening it
   shows the full-width panel row beneath. A shared name keeps one open. */
.panel-table {
  > thead { display: none; }
  > tbody > tr:first-child { position: relative; }
  > tbody > tr:first-child > td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-bottom: 1px solid var(--border); }
  > tbody > tr:first-child > td:first-child > details > summary {
    list-style: none;
    cursor: pointer;

    &::-webkit-details-marker { display: none; }
    &::before { content: "▸"; display: inline-block; width: 1em; color: var(--muted); transition: rotate 0.15s; }
    &::after { content: ""; position: absolute; inset: 0; }
  }
  > tbody > tr:first-child > td:first-child > details[open] > summary::before { rotate: 90deg; }
  > tbody > tr:first-child a, > tbody > tr:first-child .badge { position: relative; z-index: 1; }
  > tbody > tr + tr { display: none; }
  > tbody > tr + tr:hover { background: none; }
  > tbody:has(> tr:first-child details[open]) {
    background: var(--row-hover);
    > tr:first-child > td { border-bottom: none; }
    > tr + tr { display: table-row; }
    > tr + tr > td { border-bottom: 1px solid var(--border); padding-bottom: 14px; }
  }
  > tbody:last-child > tr:last-child > td { border-bottom: none; }
}
@media (min-width: 640px) {
  .panel-table > thead { display: table-header-group; }
}

.add-rule {
  > summary {
    list-style: none;
    cursor: pointer;
    text-align: center;
    background: var(--accent);
    color: var(--on-accent);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 0.8rem;
    font-weight: 600;

    &::-webkit-details-marker { display: none; }
    &:hover { filter: brightness(1.1); }
  }
  &[open] > summary { margin-bottom: 12px; background: transparent; color: var(--muted); border: 1px solid var(--border-strong); }
}
@media (min-width: 640px) {
  .add-rule > summary { width: fit-content; margin-left: auto; padding: 8px 14px; }
}

/* max-width 0 gives a cell the rest of the line and truncates it, not wraps:
   the name on a phone, the pattern once there is room for it. */
.rule-table {
  .rule-name { max-width: 0; width: 100%; }
  .rule-name summary { font-weight: 600; }
  .rule-mode, .rule-matches { width: 1%; }
  .rule-pattern { display: none; color: var(--muted); }
  .rule-name .badge { margin-left: 6px; }
}
@media (min-width: 640px) {
  .rule-table .rule-pattern { display: table-cell; max-width: 0; width: 100%; }
  .rule-table .rule-name { min-width: 16rem; max-width: 16rem; width: auto; }
}
.rule-panel {
  dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 12px; margin: 0 0 10px; }
  dt { color: var(--muted); font-size: 0.75rem; }
  dd { margin: 0; overflow-wrap: anywhere; }
  .rule-view > a { font-size: 0.8rem; }
  .banner { margin-bottom: 10px; }
}
.rule-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 12px;

  .mode-switch { display: flex; gap: 8px; }
  .mode-switch input { width: 10em; }
}
/* Editing replaces the panel's details and its other actions until Cancel. */
.rule-panel:has(.rule-edit[open]) .rule-view,
.rule-actions:has(.rule-edit[open]) > :not(.rule-edit) { display: none; }
.rule-edit[open] { flex-basis: 100%; }
:is(.rule-edit, .add-rule) {
  .open-label, &[open] .closed-label { display: none; }
  &[open] .open-label { display: inline; }
}
tbody.acted > tr:first-child { animation: acted 1.5s ease-out; }
@keyframes acted { from { background: color-mix(in srgb, var(--accent) 18%, transparent); } }

.change-table {
  .change-when, .change-kind, .change-source { width: 1%; }
  .change-rule { max-width: 0; width: 100%; }
  .change-values { display: none; color: var(--muted); }
  .change-panel p { margin: 0; }
  .no-panel { padding-left: 1em; }
}
@media (min-width: 640px) {
  .change-table .change-rule { max-width: none; width: auto; }
  .change-table .change-values { display: table-cell; max-width: 0; width: 100%; }
}

/* Columns, not a grid: a grid row is as tall as its tallest card, which leaves
   a hole beside every short one. */
.price-grid {
  columns: 340px;
  column-gap: 16px;

  > * { break-inside: avoid; margin-bottom: 16px; }
  > .empty, > .other { column-span: all; }
}
.price-card { display: flex; flex-direction: column; gap: 12px; scroll-margin-top: 16px; }
.price-card:target { border-color: var(--accent); }
.price-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;

  h3 { margin-bottom: 0; overflow-wrap: anywhere; }
  .badge { margin-left: 0; flex-shrink: 0; }
}
.badge.warn { background: color-mix(in srgb, var(--warn) 15%, transparent); border-color: transparent; color: var(--warn); }

.unit { color: var(--muted-dim); font-weight: 400; font-size: 0.7rem; }

table.rates {
  th, td { padding: 5px 0; }
  td { padding-left: 12px; }
  thead th { font-size: 0.7rem; vertical-align: bottom; }
  .col-source { display: block; font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--muted-dim); }
  tbody th {
    text-transform: none;
    letter-spacing: 0;
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--muted);
    white-space: normal;
  }
  tbody tr:hover { background: none; }
  tr:last-child th { border-bottom: none; }
}

.history {
  h4 { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
  ol { list-style: none; border-left: 2px solid var(--border-strong); margin-left: 4px; }
  li { padding-left: 12px; position: relative; }
  li::before {
    content: "";
    position: absolute;
    left: -6px;
    top: 0.75em;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--surface);
    border: 2px solid var(--border-strong);
  }
  li:has(.tag.current)::before { border-color: var(--accent); background: var(--accent); }
  li:has(.tag.scheduled)::before { border-color: var(--accent); }
}
details.period {
  > summary {
    list-style: none;
    cursor: pointer;
    padding: 6px 0;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 4px 8px;
    font-size: 0.8rem;

    &::-webkit-details-marker { display: none; }
    &:hover .range { color: var(--accent); }
  }
  .range { font-weight: 600; font-variant-numeric: tabular-nums; }
  .changes { flex-basis: 100%; color: var(--muted); }
  .period-body { display: flex; flex-direction: column; gap: 10px; padding: 4px 0 12px; }
}
.tag {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--muted);
}
.tag.current, .tag.scheduled { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }

.edits { display: flex; flex-wrap: wrap; gap: 8px; }
.edits > details[open] { flex-basis: 100%; }
details.edit > summary {
  list-style: none;
  cursor: pointer;
  width: fit-content;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;

  &::-webkit-details-marker { display: none; }
  &:hover { border-color: var(--accent); }
}
details.edit[open] > summary { margin-bottom: 12px; }
details.edit.other > summary { border: none; padding: 0; font-size: 0.95rem; color: var(--text); }
details.edit.other > summary::before { content: "+ "; color: var(--accent); }

.rate-form {
  display: flex;
  flex-direction: column;
  gap: 12px;

  .rate-fields {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
  }
  label { display: flex; flex-direction: column; gap: 4px; font-size: 0.75rem; color: var(--muted); }
  button { align-self: flex-start; }
}

.rename-cell { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.rename-cell form { display: flex; gap: 6px; flex: 1 1 100%; }
.rename-cell input[type="text"] { width: 100%; }

@media (min-width: 640px) {
  .rename-cell form { flex: 0 1 auto; }
  .rename-cell input[type="text"] { width: 12em; padding: 6px 10px; }
}

/* A button's look is its class, never where it sits: plain is the primary
   action, .secondary and .danger are outlined. Sized for a thumb, then the
   outlined ones go compact from 640px, where they mostly sit in table rows. */
button {
  background: var(--accent);
  color: var(--on-accent);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 14px;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;

  &:hover { filter: brightness(1.1); }
  &[disabled] { opacity: 0.4; cursor: not-allowed; filter: none; }
}
button.secondary, button.danger {
  background: transparent;
  border-color: var(--border-strong);

  &:hover { filter: none; }
}
button.secondary { color: var(--muted); &:hover { background: var(--hover-tint); } }
button.danger { color: var(--danger); &:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); } }
td > button + button { margin-left: 4px; }
.banner button { margin-left: 8px; }

@media (min-width: 640px) {
  button { padding: 8px 14px; }
  button.secondary, button.danger { padding: 4px 10px; }
}

.kv tr:last-child th { border-bottom: none; }
.upd-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }

.banner {
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.8rem;
}
.banner.error { background: color-mix(in srgb, var(--danger) 12%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent); color: var(--danger); }
.banner.info { background: color-mix(in srgb, var(--info) 12%, transparent); border: 1px solid color-mix(in srgb, var(--info) 40%, transparent); color: var(--info); }
.banner.note { background: var(--surface-alt); border: 1px solid var(--border-strong); color: var(--muted); }
.explain-gap { display: flex; gap: 6px; margin: 6px 0 10px; }
.explain-gap input[type="text"] { flex: 1; min-width: 0; }
.explain-gap button { margin-left: 0; white-space: nowrap; }
@media (min-width: 640px) {
  .explain-gap input[type="text"] { flex: 0 1 24em; padding: 6px 10px; }
}

/* Rows share the parent's columns through subgrid, so every bar starts at the
   same x while the label column sizes to its longest label, capped at 40%. */
.bars {
  display: grid;
  grid-template-columns: fit-content(40%) 1fr auto;
  gap: 6px 8px;

  .legend { grid-column: 1 / -1; }
}
.bar-row {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: subgrid;
  align-items: center;
}
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; word-break: normal; }
.bar-track { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 4px; }
.bar { display: flex; height: 16px; border-radius: 3px; overflow: hidden; }
.seg { height: 100%; }
.bar-total { text-align: right; font-variant-numeric: tabular-nums; font-size: 0.75rem; color: var(--muted); }

.c0 { background: var(--chart-0); }
.c1 { background: var(--chart-1); }
.c2 { background: var(--chart-2); }
.c3 { background: var(--chart-3); }
.c4 { background: var(--chart-4); }
.c5 { background: var(--chart-5); }

.legend { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; font-size: 0.75rem; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 4px; }

.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }

.histogram { display: flex; flex-direction: column; gap: 8px; }
.hist-chart { display: flex; gap: 10px; width: 100%; }
/* space-between aligns label *boxes* to the plot's edges, which leaves every
   label's text half a line-height off the gridline it names. Growing the box
   by one line-height and pulling half of it back at each end puts the first
   and last text centres on the plot's top and bottom instead. */
.hist-scale {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex: 0 0 auto;
  height: calc(180px + 1em);
  margin-block: -0.5em;
  font-size: 0.7rem;
  color: var(--muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
/* Columns run oldest-left to newest-right (histogramViewOf reverses them), so
   the interesting end is the far right — an RTL scroll container starts there.
   The children switch back to ltr so only the scroll origin is reversed, not
   their layout. */
.hist-scroll { flex: 1 1 auto; min-width: 0; overflow-x: auto; direction: rtl; }
.hist-plot, .hist-labels { direction: ltr; }
/* One gridline per tick interval: a single top-edge line tiled by
   background-size, whose step comes from histogramView.GridStep inline rather
   than a constant duplicated here. border-bottom draws the zero line. */
.hist-plot {
  display: flex;
  min-width: 100%;
  width: max-content;
  align-items: flex-end;
  gap: 4px;
  height: 180px;
  background-image: linear-gradient(to bottom, var(--border) 0 1px, transparent 1px);
  border-bottom: 1px solid var(--border-strong);
}
/* Columns stretch to fill a plot wider than they need and never shrink below
   14px, so the plot scrolls only once there are more columns than fit; the bar
   inside stays a capped width, centred in its column. */
.hist-col { flex: 1 0 14px; height: 100%; }
.hist-track { display: flex; align-items: flex-end; justify-content: center; width: 100%; height: 100%; }
.hist-bar { display: flex; flex-direction: column-reverse; width: 100%; max-width: 28px; border-radius: 2px 2px 0 0; overflow: hidden; }
.hseg { width: 100%; transition: filter 0.1s; }
.hist-col:hover .hseg { filter: brightness(1.2); }
.hist-labels { display: flex; min-width: 100%; width: max-content; gap: 4px; margin-top: 4px; }
.hist-label {
  flex: 1 0 14px;
  font-size: 0.7rem;
  color: var(--muted);
  text-align: center;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  white-space: nowrap;
}

/* The row's one button opens its detail; its ::after stretches over the row so
   the whole row is the tap target, while the table keeps its row semantics. */
.clickable-row {
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;

  &:hover { background: var(--row-hover); }
  &:has(.row-open:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
}
.row-open {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;

  &::after { content: ""; position: absolute; inset: 0; }
  &:focus-visible { outline: none; }
  &:hover { filter: none; }
}
/* The detail fragment's wrapper only scopes Alpine; the card lays out its children. */
.modal-detail { display: contents; }

.cell-link {
  display: block;
  padding: 8px 0;
  color: var(--accent);
  text-decoration: none;

  &:hover {
    text-decoration: underline;
  }
}

.cmd-preview {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* A path — or an mcp__server__tool name — is identified by its tail, so clip
   the head: the RTL direction moves the clip and its ellipsis to the start of
   the line. The &#8206; (LRM) markers the templates wrap the path in keep its
   leading/trailing separators in place — remove them and "/src/main.go"
   renders as "src/main.go/". */
.cmd-preview.path-preview,
.tool-name.path-preview {
  direction: rtl;
  text-align: left;
}

.rollup-table { table-layout: fixed; }
.rollup-table col.col-subject { width: auto; }
.rollup-table col.col-tool { width: 7em; }
.rollup-table col.col-count { width: 7.5em; }
@media (min-width: 640px) {
  .cell-link { padding: 0; }
  .rollup-table col.col-tool { width: 12em; }
  .rollup-table col.col-count { width: 8em; }
}
.rollup-table td.cell-clip { overflow: hidden; }

dialog.modal {
  margin: auto;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0;
  max-width: 95vw;
  width: 100%;
  max-height: 90vh;
  box-shadow: var(--shadow), 0 20px 50px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

/* Opened before its content arrives, the dialog itself takes focus; a ring
   around the whole card would only look like an error. */
dialog.modal:focus-visible { outline: none; }

dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(2px);
}

.modal-card {
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
  gap: 12px;
}

.modal-title {
  font-weight: 600;
  font-size: 1rem;
  min-width: 0;
}

.modal-tool {
  font-family: var(--font-mono);
  color: var(--accent);
}

.modal-meta {
  display: block;
  font-size: 0.75rem;
  font-weight: normal;
  color: var(--muted);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;

  &:hover {
    color: var(--text);
    background: var(--hover-tint);
    filter: none;
  }
}

.modal-body {
  padding: 12px 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.code-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-size: 0.75rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 60vh;
  overflow-y: auto;
  font-family: var(--font-mono);
}

@media (min-width: 640px) {
  dialog.modal { max-width: min(840px, 92vw); max-height: 85vh; }
  .modal-card { max-height: 85vh; }
  .modal-header { padding: 14px 18px; }
  .modal-body { padding: 16px 18px; }
  .code-block { font-size: 0.8rem; padding: 12px; max-height: 55vh; }
}

/* The activity log alone keeps a phone-only layout: each row becomes a
   two-line card. Inverting it would mean restoring table display on every
   element from 640px up. */
@media (width < 640px) {
  .log-table,
  .log-table tbody {
    display: block;
  }

  .log-table thead,
  .log-table colgroup {
    display: none;
  }

  .log-table tr.clickable-row {
    display: grid;
    grid-template-areas:
      "cmd time"
      "session tool";
    grid-template-columns: 1fr auto;
    align-items: baseline;
    gap: 4px 10px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
  }

  .log-table td {
    padding: 0;
    border-bottom: none;
    min-width: 0;
  }

  .log-table td.cell-cmd {
    grid-area: cmd;
    font-weight: 600;
    color: var(--text);
    min-width: 0;
    overflow: hidden;
  }

  .log-table td.cell-time {
    grid-area: time;
    text-align: right;
    font-size: 0.75rem;
    color: var(--muted);
    white-space: nowrap;
    flex-shrink: 0;
  }

  .log-table td.cell-time .date {
    display: none;
  }

  .log-table td.cell-time .clock {
    display: inline;
    font-size: 0.75rem;
  }

  .log-table td.cell-session {
    grid-area: session;
    font-size: 0.75rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted-dim);
    min-width: 0;
  }

  .log-table td.cell-tool {
    grid-area: tool;
    text-align: right;
    font-size: 0.7rem;
    color: var(--muted);
    white-space: nowrap;
    flex-shrink: 0;
    min-width: 0;
  }

  .log-table td.cell-tool .cell-tool-inner {
    justify-content: flex-end;
  }
}

/* A .stack-table becomes one card per row on a phone: each cell names itself
   from its data-label, since the header row is hidden; a .title cell heads
   the card and an .actions cell ends it, wherever they sit in the table.
   From 640px it is an ordinary table. */
@media (width < 640px) {
  .stack-table, .stack-table tbody, .stack-table td { display: block; }
  .stack-table thead { display: none; }
  .stack-table tr { display: flex; flex-direction: column; padding: 10px 0; border-bottom: 1px solid var(--border); }
  .stack-table tr:last-child { border-bottom: none; }
  .stack-table td { padding: 2px 0; border: 0; text-align: left; white-space: normal; }
  .stack-table td.title { order: -1; }
  .stack-table td.actions { order: 1; }
  .stack-table td[data-label] { display: grid; grid-template-columns: 7.5rem 1fr; gap: 8px; justify-items: start; }
  .stack-table td[data-label]::before {
    content: attr(data-label);
    color: var(--muted);
    font-family: system-ui, sans-serif;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  .stack-table td.title { font-weight: 600; padding-bottom: 6px; }
  .stack-table td.actions { padding-top: 8px; }
}

@keyframes indicator-pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) {
  header .indicator::before, tbody.acted > tr:first-child { animation: none; }
}
