/* toc-bot panel. Plain CSS, no build step. Component rules use only the
   tokens below; the dark scheme redefines the colour tokens. Text pairs are
   at least 4.5:1 and control borders at least 3:1 in both schemes. */
@layer tokens, base, layout, components, utilities;

@layer tokens {
  :root {
    color-scheme: light dark;
    /* Colour: surfaces and text */
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --fg: #1a1d23;
    --fg-muted: #5b6270;
    --border: #dde1e7;
    --border-control: #7f8796;
    --accent: #2550c4;
    --accent-fg: #ffffff;
    --accent-soft: #e8eefc;
    --focus: #2550c4;
    /* Colour: who wrote a message, kept apart from status colours */
    --author-contact: var(--surface-2);
    --author-bot: #eef1f6;
    --author-bot-fg: var(--fg-muted);
    --author-human: #e3f4ee;
    --author-human-border: #0f6e55;
    --author-note: #fdf6e3;
    --author-note-border: #7a5c00;
    /* Colour: status */
    --status-waiting: #8a5200;
    --status-waiting-soft: #fff3dc;
    --status-danger: #b42318;
    --status-danger-soft: #fdecea;
    --status-ok: #12704f;
    --status-ok-soft: #e5f5ed;
    --status-info: #2550c4;
    --status-info-soft: #e8eefc;
    /* Type: base 15 px, ratio about 1.2 */
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
    --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
    --text-xs: .8rem;       /* 12 px */
    --text-sm: .8667rem;    /* 13 px */
    --text-md: 1rem;        /* 15 px */
    --text-lg: 1.2rem;      /* 18 px */
    --text-xl: 1.4667rem;   /* 22 px */
    --leading-xs: 1.0667rem;
    --leading-sm: 1.2rem;
    --leading-md: 1.4667rem;
    --leading-lg: 1.6rem;
    --leading-xl: 1.8667rem;
    --weight-regular: 400;
    --weight-semibold: 600;
    --weight-bold: 700;
    /* Spacing: a 4 px scale */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    /* Radius */
    --radius-s: 6px;
    --radius-m: 10px;
    --radius-pill: 999px;
    /* Elevation */
    --shadow-1: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
    --shadow-2: 0 4px 12px rgb(16 24 40 / 12%), 0 1px 3px rgb(16 24 40 / 8%);
    --shadow-3: 0 8px 24px rgb(16 24 40 / 18%), 0 2px 6px rgb(16 24 40 / 10%);
    /* Motion: opacity and transform only */
    --duration-fast: 120ms;
    --duration: 200ms;
    --ease: cubic-bezier(.2, 0, 0, 1);
    /* Layout */
    --target-min: 24px;
    --topbar-h: 3.5rem;
    --sidenav-w: 14rem;
    --content-max: 90rem;
    --form-max: 40rem;
  }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #0f1115;
      --surface: #171a21;
      --surface-2: #1f232c;
      --fg: #e7e9ee;
      --fg-muted: #a3aab8;
      --border: #2a2f3a;
      --border-control: #6b7383;
      --accent: #7ea2ff;
      --accent-fg: #0b1220;
      --accent-soft: #1f2a44;
      --focus: #9db8ff;
      --author-bot: #232836;
      --author-human: #15312a;
      --author-human-border: #5fd1ac;
      --author-note: #2b2615;
      --author-note-border: #e0c25c;
      --status-waiting: #f3bb5c;
      --status-waiting-soft: #3a2c12;
      --status-danger: #ff8a80;
      --status-danger-soft: #3d1c1a;
      --status-ok: #6fd6a6;
      --status-ok-soft: #15302a;
      --status-info: #7ea2ff;
      --status-info-soft: #1f2a44;
      --shadow-1: 0 1px 2px rgb(0 0 0 / 40%);
      --shadow-2: 0 4px 12px rgb(0 0 0 / 50%);
      --shadow-3: 0 8px 24px rgb(0 0 0 / 60%);
    }
  }
  :root[data-theme="dark"] {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --fg: #e7e9ee;
    --fg-muted: #a3aab8;
    --border: #2a2f3a;
    --border-control: #6b7383;
    --accent: #7ea2ff;
    --accent-fg: #0b1220;
    --accent-soft: #1f2a44;
    --focus: #9db8ff;
    --author-bot: #232836;
    --author-human: #15312a;
    --author-human-border: #5fd1ac;
    --author-note: #2b2615;
    --author-note-border: #e0c25c;
    --status-waiting: #f3bb5c;
    --status-waiting-soft: #3a2c12;
    --status-danger: #ff8a80;
    --status-danger-soft: #3d1c1a;
    --status-ok: #6fd6a6;
    --status-ok-soft: #15302a;
    --status-info: #7ea2ff;
    --status-info-soft: #1f2a44;
    --shadow-1: 0 1px 2px rgb(0 0 0 / 40%);
    --shadow-2: 0 4px 12px rgb(0 0 0 / 50%);
    --shadow-3: 0 8px 24px rgb(0 0 0 / 60%);
  }
  :root[data-theme="light"] { color-scheme: light; }
  :root[data-theme="dark"] { color-scheme: dark; }
}

@layer base {
  * { box-sizing: border-box; }
  html {
    font-family: var(--font); font-size: 15px; line-height: var(--leading-md); color: var(--fg); background: var(--bg);
    scroll-padding-top: calc(var(--topbar-h) + var(--space-4));
  }
  body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--fg); }
  a { color: var(--accent); text-underline-offset: 2px; }
  h1 { font-size: var(--text-xl); line-height: var(--leading-xl); font-weight: var(--weight-bold); margin: 0; }
  h2 { font-size: var(--text-lg); line-height: var(--leading-lg); font-weight: var(--weight-semibold); margin: var(--space-5) 0 var(--space-2); }
  p { margin: var(--space-2) 0; }
  pre { white-space: pre-wrap; word-break: break-word; font-family: var(--mono); font-size: var(--text-sm); }
  :focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
  main:focus { outline: none; }
  details > summary { cursor: pointer; }
  table { border-collapse: collapse; width: 100%; background: var(--surface); }
  th, td { text-align: left; padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); vertical-align: top; }
  th { font-weight: var(--weight-semibold); font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); background: var(--surface-2); }
  tbody tr:last-child td { border-bottom: 0; }
  td { font-variant-numeric: tabular-nums; }
  label { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-sm); color: var(--fg-muted); }
  input, select, textarea, button {
    font: inherit; color: var(--fg); padding: 6px 10px; min-height: 32px;
    border: 1px solid var(--border-control); border-radius: var(--radius-s); background: var(--surface);
  }
  input[type="checkbox"], input[type="radio"] { min-height: 0; accent-color: var(--accent); width: 16px; height: 16px; }
  input:hover, select:hover, textarea:hover { border-color: var(--fg-muted); }
  textarea { resize: vertical; width: 100%; }
  progress { accent-color: var(--accent); }
}

@layer layout {
  .skip { position: absolute; left: var(--space-2); top: -3rem; z-index: 30; padding: var(--space-2) var(--space-3); background: var(--surface); border-radius: var(--radius-s); }
  .skip:focus { top: var(--space-2); }

  /* Shell: one-row top bar, side navigation, content. */
  header.top {
    position: sticky; top: 0; z-index: 10;
    display: flex; flex-wrap: nowrap; align-items: center; gap: var(--space-3);
    height: var(--topbar-h); padding: 0 var(--space-4); background: var(--surface); border-bottom: 1px solid var(--border);
  }
  .top-spacer { flex: 1; min-width: 0; }
  .frame { display: flex; align-items: flex-start; }
  .side {
    position: sticky; top: var(--topbar-h); flex: none; width: var(--sidenav-w);
    height: calc(100vh - var(--topbar-h)); overflow-y: auto;
    padding: var(--space-2) var(--space-3) var(--space-5); background: var(--surface); border-right: 1px solid var(--border);
  }
  main { flex: 1; min-width: 0; padding: var(--space-5); max-width: var(--content-max); margin: 0 auto; }
  .frame:not(.with-nav) main { max-width: 76rem; }
  .page-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: var(--space-2); margin: var(--space-1) 0 var(--space-4); }
  .crumb { margin: 0 0 var(--space-2); font-size: var(--text-sm); }
}

@layer components {
  /* Header parts */
  .brand { display: inline-flex; align-items: center; gap: var(--space-2); flex: none; font-weight: var(--weight-bold); text-decoration: none; color: var(--fg); letter-spacing: .01em; }
  .brand-mark { width: 1.1rem; height: 1.1rem; border-radius: 4px; background: linear-gradient(135deg, var(--accent), var(--author-human-border)); }
  .tenant, .switcher > summary { display: block; max-width: 16rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tenant { color: var(--fg-muted); padding-left: var(--space-3); border-left: 1px solid var(--border); }
  .role { font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-semibold); padding: 2px var(--space-2); border-radius: var(--radius-pill); background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
  .menu { position: relative; flex: none; font-size: var(--text-sm); }
  .menu > summary { list-style: none; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; min-width: var(--target-min); min-height: var(--target-min); padding: 0 var(--space-2); border-radius: var(--radius-s); color: var(--fg-muted); }
  .menu > summary::-webkit-details-marker { display: none; }
  .menu > summary:hover, .menu[open] > summary { color: var(--fg); background: var(--surface-2); }
  .menu > ul { position: absolute; right: 0; top: calc(100% + var(--space-1)); z-index: 30; min-width: 10rem; margin: 0; padding: var(--space-1); list-style: none; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-2); }
  .menu > ul a { display: flex; align-items: center; min-height: var(--target-min); padding: 0 var(--space-3); border-radius: var(--radius-s); color: var(--fg); text-decoration: none; }
  .menu > ul a:hover { background: var(--surface-2); }
  .menu > ul a[aria-current="true"] { font-weight: var(--weight-semibold); }
  .menu > ul a[aria-current="true"]::after { content: "\2713"; margin-left: auto; padding-left: var(--space-3); }
  .theme-icon::before { content: "\25D0"; font-size: 1.1em; }
  .theme-icon[data-theme-icon="light"]::before { content: "\2600"; }
  .theme-icon[data-theme-icon="dark"]::before { content: "\263E"; }

  /* Menus in the top bar are details elements: they open without scripts;
     panel.js closes them on Escape and on a click outside. */
  [data-menu] { position: relative; }
  [data-menu] > summary { list-style: none; cursor: pointer; border-radius: var(--radius-s); }
  [data-menu] > summary::-webkit-details-marker { display: none; }
  [data-menu] > summary:hover { background: var(--surface-2); }
  .switcher { min-width: 0; padding-left: var(--space-3); border-left: 1px solid var(--border); }
  .switcher > summary { color: var(--fg); font-weight: var(--weight-semibold); padding: var(--space-1) var(--space-2); }
  .switcher > summary::after { content: " \25BE"; color: var(--fg-muted); }
  .switcher-menu, .usermenu .menu {
    position: absolute; z-index: 20; top: calc(100% + var(--space-2)); min-width: 16rem; padding: var(--space-1);
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-2);
  }
  .switcher-menu { left: var(--space-3); }
  .switcher-search { width: 100%; margin-bottom: var(--space-1); }
  .switcher-menu ul { max-height: 60vh; overflow-y: auto; margin: 0; padding: 0; list-style: none; }
  .switcher-menu a { display: block; padding: 6px 10px; border-radius: var(--radius-s); color: var(--fg); text-decoration: none; }
  .switcher-menu a:hover { background: var(--surface-2); }
  .switcher-menu a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: var(--weight-semibold); }
  .switcher-none { margin: var(--space-2); }

  .usermenu { flex: none; }
  .usermenu > summary { display: flex; align-items: center; gap: var(--space-2); min-height: 32px; padding: 2px var(--space-2) 2px 2px; }
  .usermenu .menu { right: 0; }
  .usermenu .menu-id { display: flex; flex-direction: column; margin: 0; padding: var(--space-2) 10px; border-bottom: 1px solid var(--border); font-size: var(--text-sm); }
  .usermenu .logout { margin: 0; padding: var(--space-1) 0 0; }
  .usermenu .logout button { width: 100%; justify-content: flex-start; }
  .avatar { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: var(--weight-bold); font-size: var(--text-sm); }
  .who { color: var(--fg); font-size: var(--text-sm); max-width: 12rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .drawer { display: none; flex: none; }
  .drawer > summary { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; }
  .drawer-icon, .drawer-icon::before, .drawer-icon::after { display: block; width: 18px; height: 2px; border-radius: 1px; background: var(--fg); }
  .drawer-icon { position: relative; }
  .drawer-icon::before, .drawer-icon::after { content: ""; position: absolute; left: 0; }
  .drawer-icon::before { top: -6px; }
  .drawer-icon::after { top: 6px; }
  .drawer[open] .drawer-icon { background: transparent; }
  .drawer[open] .drawer-icon::before { top: 0; transform: rotate(45deg); }
  .drawer[open] .drawer-icon::after { top: 0; transform: rotate(-45deg); }

  /* Side navigation */
  .side ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
  .nav-group { margin: var(--space-4) 0 var(--space-1); padding: 0 10px; font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-semibold); color: var(--fg-muted); }
  .side a { display: flex; align-items: center; min-height: 36px; padding: 6px 10px; border-radius: var(--radius-s); color: var(--fg); text-decoration: none; }
  .side a:hover { background: var(--surface-2); }
  .side a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: var(--weight-semibold); box-shadow: inset 3px 0 0 var(--accent); }

  /* Staff inside a tenant: neutral and always there; break-glass and the
     emergency account keep their alert style. */

  .live-pill { display: inline-flex; align-items: center; gap: 6px; font-size: var(--text-xs); line-height: var(--leading-xs); color: var(--fg-muted); padding: 2px var(--space-2); border: 1px solid var(--border); border-radius: var(--radius-pill); }
  .live-pill::before { content: ""; width: var(--space-2); height: var(--space-2); border-radius: 50%; background: var(--fg-muted); }
  .live-pill[data-state="live"]::before { background: var(--status-ok); box-shadow: 0 0 0 3px var(--status-ok-soft); }
  .live-pill[data-state="connecting"]::before { background: var(--status-waiting); }

  /* Cards, tiles, empty states */
  .card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-1); padding: var(--space-3) var(--space-4); margin: var(--space-3) 0; }
  .card.table-wrap { padding: 0; overflow-x: auto; }
  .empty { text-align: center; color: var(--fg-muted); padding: var(--space-5) var(--space-4); }
  .tiles { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); gap: var(--space-3); }
  .tile { display: block; padding: var(--space-4); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); text-decoration: none; color: var(--fg); font-weight: var(--weight-semibold); box-shadow: var(--shadow-1); }
  .tile:hover { border-color: var(--accent); }

  /* Notices and banners */
  .notice { padding: var(--space-2) var(--space-3); border-radius: var(--radius-s); border-left: 4px solid; margin: var(--space-2) 0; }
  .notice-error, .error { border-left: 4px solid var(--status-danger); background: var(--status-danger-soft); padding: var(--space-2) var(--space-3); border-radius: var(--radius-s); }
  .notice-ok, .flash { border-left: 4px solid var(--status-ok); background: var(--status-ok-soft); padding: var(--space-2) var(--space-3); border-radius: var(--radius-s); }
  .notice-warn { border-color: var(--status-waiting); background: var(--status-waiting-soft); }
  .notice-info { border-color: var(--status-info); background: var(--status-info-soft); }
  .banner { margin: 0; padding: var(--space-2) var(--space-5); background: var(--status-waiting-soft); border-bottom: 1px solid var(--border); }
  .banner-danger { background: var(--status-danger-soft); color: var(--status-danger); font-weight: var(--weight-semibold); }
  .banner-box { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2) var(--space-4); padding: var(--space-3) var(--space-4); border-radius: var(--radius-m); border: 1px solid var(--border); margin: var(--space-3) 0; }
  .banner-box p { margin: 0; }
  .banner-bot { background: var(--author-bot); border-color: var(--border); }
  .banner-human { background: var(--author-human); border-color: var(--author-human-border); }
  .banner-warn { background: var(--status-waiting-soft); border-color: var(--status-waiting); }
  .banner-other { background: var(--surface-2); }
  .error-text { color: var(--status-danger); font-family: var(--mono); font-size: var(--text-sm); overflow-wrap: anywhere; }
  .filter-active { display: flex; gap: var(--space-2); align-items: baseline; flex-wrap: wrap; color: var(--fg-muted); }

  /* Queue: one row per conversation, person first; the whole row is the
     link (its ::after covers the row), participant chips sit above it. */
  .queue { list-style: none; margin: var(--space-3) 0; padding: 0; }
  .q-row {
    position: relative; display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; gap: var(--space-1) var(--space-3);
    align-items: start; min-height: 60px; padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border);
  }
  .q-row:last-child { border-bottom: 0; }
  .q-row:hover { background: var(--surface-2); }
  .q-row.is-waiting { box-shadow: inset 3px 0 0 var(--status-waiting); }
  .q-row.is-mine { box-shadow: inset 3px 0 0 var(--author-human-border); }
  .q-row.is-current { background: var(--accent-soft); }
  .q-row:focus-within { background: var(--surface-2); }
  .q-avatar {
    display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 50%;
    background: var(--accent-soft); color: var(--accent); font-weight: var(--weight-bold);
  }
  .q-avatar-anon { background: var(--surface-2); }
  .q-avatar-anon::before { content: ""; width: 12px; height: 12px; border-radius: 50%; background: var(--fg-muted); box-shadow: 0 9px 0 2px var(--fg-muted); clip-path: inset(0 -6px -2px -6px); transform: translateY(-3px); }
  .q-main { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
  .q-link { color: var(--fg); font-weight: var(--weight-semibold); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .q-link::after { content: ""; position: absolute; inset: 0; }
  .q-link:hover { text-decoration: underline; }
  .q-link:focus-visible { outline: none; }
  .q-link:focus-visible::after { outline: 3px solid var(--focus); outline-offset: -3px; border-radius: var(--radius-s); }
  .q-row.is-waiting .q-link { font-weight: var(--weight-bold); }
  .q-meta { display: flex; flex-wrap: wrap; gap: 0 var(--space-2); margin: 0; font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); }
  .q-meta > span + span::before { content: "\00b7"; margin-right: var(--space-2); }
  .q-main .participants { position: relative; z-index: 1; margin-top: var(--space-1); }
  .q-side { display: flex; flex-direction: column; align-items: flex-end; gap: var(--space-1); text-align: right; }
  .q-time { font-size: var(--text-xs); line-height: var(--leading-xs); color: var(--fg-muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
  .badge-other { background: var(--surface-2); color: var(--fg); box-shadow: inset 0 0 0 1px var(--border); }
  .queue-count { margin-bottom: 0; }
  .seg { display: inline-flex; flex-wrap: wrap; gap: 0; margin: 0; padding: 0; border: 0; min-width: 0; }
  .seg legend { width: 100%; float: left; margin-bottom: var(--space-1); font-size: var(--text-sm); color: var(--fg-muted); }
  .seg-opt { flex-direction: row; position: relative; color: var(--fg); font-size: var(--text-md); }
  .seg-opt input { position: absolute; opacity: 0; width: 1px; height: 1px; }
  .seg-opt span { display: inline-flex; align-items: center; min-height: 32px; padding: 4px 14px; border: 1px solid var(--border-control); background: var(--surface); cursor: pointer; }
  .seg-opt + .seg-opt span { margin-left: -1px; }
  .seg-opt:first-of-type span { border-radius: var(--radius-s) 0 0 var(--radius-s); }
  .seg-opt:last-of-type span { border-radius: 0 var(--radius-s) var(--radius-s) 0; }
  .seg-opt input:checked + span { position: relative; z-index: 1; background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: var(--weight-semibold); }
  .seg-opt input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; z-index: 2; }
  .js [data-noscript] { display: none; }

  /* Tables */
  table.list tbody tr:hover { background: var(--surface-2); }
  .pager { display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--space-2); margin: var(--space-2) 0 var(--space-4); }
  .pager a[rel="next"] { margin-left: auto; }
  /* Error details under a channel row: one line until opened. */
  table.instances tbody + tbody > tr:first-child > td { border-top: 1px solid var(--border); }
  table.instances tbody > tr:has(+ tr.detail-row) > td { border-bottom: 0; }
  tr.detail-row > td { padding-top: 0; border-top: 0; }
  tr.detail-row summary { color: var(--status-danger); font-size: var(--text-sm); line-height: var(--leading-sm); }
  tr.detail-row .error-text { margin: var(--space-1) 0 0; }
  .facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: var(--space-3) var(--space-4); }
  .facts dt { color: var(--fg-muted); font-size: var(--text-sm); line-height: var(--leading-sm); }
  .facts dd { margin: 0; }
  .facts progress { display: block; width: 100%; margin: var(--space-1) 0; }
  th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
  .flag-name { font-weight: var(--weight-semibold); }
  .flag-desc { color: var(--fg-muted); font-size: var(--text-sm); line-height: var(--leading-sm); max-width: 38rem; }
  .flag-edit { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
  .flag-edit input[type="text"] { width: 11rem; }

  /* Badges: state only. Chips: participants and conversation facts, 24 px
     tall, the WCAG 2.5.8 minimum when a chip is a link. */
  .badge { display: inline-block; padding: 2px var(--space-2); border-radius: var(--radius-pill); font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-semibold); background: var(--surface-2); color: var(--fg-muted); white-space: nowrap; }
  .badge-bot { background: var(--author-bot); color: var(--author-bot-fg); box-shadow: inset 0 0 0 1px var(--border); }
  .badge-human, .badge-on, .state-open { background: var(--status-ok-soft); color: var(--status-ok); }
  .badge-warn, .state-idle { background: var(--status-waiting-soft); color: var(--status-waiting); }
  .badge-off, .state-closed { background: var(--surface-2); color: var(--fg-muted); }
  .badge-danger { background: var(--status-danger-soft); color: var(--status-danger); }
  .chip { display: inline-flex; align-items: center; gap: var(--space-1); min-height: var(--target-min); padding: 2px var(--space-2); border: 1px solid var(--border); border-radius: var(--radius-pill); background: var(--surface); color: var(--fg); font-size: var(--text-xs); line-height: var(--leading-xs); white-space: nowrap; text-decoration: none; }
  a.chip { color: var(--fg-muted); background: var(--surface-2); border-color: transparent; }
  a.chip:hover { text-decoration: underline; }
  .chip-named, a.chip-named { background: var(--accent-soft); color: var(--accent); }
  .chip-k { color: var(--fg-muted); }
  .chips { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; margin: 0; }
  .participants { display: flex; flex-wrap: wrap; gap: var(--space-1); }

  /* Forms and buttons */
  form.inline { display: inline; }
  .filters, .stack { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: end; }
  .filters.card { padding: var(--space-3) var(--space-4); }
  label.inline { display: inline-flex; flex-direction: row; align-items: center; gap: var(--space-1); margin-right: var(--space-2); color: var(--fg); }
  button, .button {
    cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; min-height: 32px;
    padding: 6px 14px; border-radius: var(--radius-s); border: 1px solid var(--border-control); background: var(--surface); color: var(--fg); font-weight: 500;
    transition: background-color var(--duration-fast) var(--ease), opacity var(--duration-fast) var(--ease);
  }
  button:hover, .button:hover { background: var(--surface-2); }
  button[disabled], button[aria-busy="true"] { opacity: .6; cursor: progress; }
  .primary, button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: var(--weight-semibold); }
  .primary:hover, button.primary:hover, .button.primary:hover { filter: brightness(1.08); background: var(--accent); }
  .danger, button.danger { color: var(--status-danger); border-color: var(--status-danger); }
  button.danger:hover { background: var(--status-danger-soft); }
  button.ghost { border-color: transparent; background: transparent; color: var(--fg-muted); }
  button.ghost:hover { background: var(--surface-2); color: var(--fg); }
  .actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .login { max-width: 24rem; margin: var(--space-7) auto; display: flex; flex-direction: column; gap: var(--space-4); }
  .login form { display: flex; flex-direction: column; gap: var(--space-3); }
  pre.once { padding: var(--space-2); background: var(--status-waiting-soft); border-radius: var(--radius-s); }
  .meta { font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); }

  /* Conversation: a column that fills the viewport under the top bar; the
     transcript scrolls inside it and the composer is docked at the bottom.
     At 1280 px and wider the queue and the context pane sit beside it. */
  main:has(.inbox) { max-width: none; padding: var(--space-3) var(--space-4); }
  .inbox { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); }
  .inbox-queue, .conv-context { min-height: 0; overflow-y: auto; }
  .inbox-queue { display: none; }
  .inbox-queue-head { margin: 0 0 var(--space-2); font-size: var(--text-md); line-height: var(--leading-md); }
  .inbox-queue .queue { margin-top: var(--space-2); }
  .inbox-queue .q-row { grid-template-columns: 32px minmax(0, 1fr); padding: var(--space-3); }
  .inbox-queue .q-avatar { width: 32px; height: 32px; }
  .inbox-queue .q-side { grid-column: 2; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: flex-start; text-align: left; }
  .inbox-queue .q-meta .mono, .inbox-queue .participants { display: none; }
  .inbox-queue .queue-count { font-size: var(--text-sm); line-height: var(--leading-sm); }
  .conv-pane { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; min-height: 0; }
  .conv-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-1) var(--space-3); }
  .conv-head .crumb { flex-basis: 100%; margin: 0; }
  .conv-head h1 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
  .conv-id { margin-left: var(--space-2); font-size: var(--text-sm); font-weight: var(--weight-regular); color: var(--fg-muted); }
  .status-line {
    display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); min-height: 52px;
    padding: var(--space-2) var(--space-3); border-radius: var(--radius-m); border: 1px solid var(--border);
  }
  .status-line p { margin: 0; }
  .status-text { flex: 1 1 16rem; display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-2); }
  .status-chips { flex: 0 1 auto; }
  .chip-policy { background: var(--status-info-soft); border-color: transparent; color: var(--fg); }
  .transcript-wrap { flex: 1 1 auto; min-height: 12rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-1); padding: var(--space-4); overflow-y: auto; scroll-behavior: smooth; }
  .transcript { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-3); }
  .transcript > .day { align-self: center; margin: var(--space-2) 0; padding: 2px var(--space-3); border-radius: var(--radius-pill); background: var(--surface-2); color: var(--fg-muted); font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-semibold); }
  .msg { display: flex; flex-direction: column; gap: var(--space-1); max-width: min(42rem, 85%); }
  .msg-grouped { margin-top: calc(-1 * var(--space-2)); }
  .msg-customer { align-self: flex-start; }
  .msg-bot, .msg-human { align-self: flex-end; align-items: flex-end; }
  .bubble { padding: var(--space-2) var(--space-3); border-radius: 14px; border: 1px solid var(--border); background: var(--author-contact); max-width: 100%; }
  .msg-customer .bubble { border-bottom-left-radius: 4px; }
  .msg-bot .bubble { background: var(--author-bot); border-bottom-right-radius: 4px; }
  .msg-human .bubble { background: var(--author-human); border: 2px solid var(--author-human-border); border-bottom-right-radius: 4px; }
  .msg-head { display: flex; flex-wrap: wrap; gap: 2px var(--space-2); align-items: baseline; font-size: var(--text-xs); line-height: var(--leading-xs); color: var(--fg-muted); margin-bottom: 2px; }
  .msg-role { font-weight: var(--weight-bold); }
  .msg-bot .msg-role { color: var(--author-bot-fg); }
  .msg-human .msg-role { color: var(--author-human-border); }
  .msg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
  .msg-details { font-size: var(--text-xs); line-height: var(--leading-xs); color: var(--fg-muted); max-width: 100%; }
  .msg-details summary { display: inline-flex; align-items: center; min-height: var(--target-min); padding: 0 var(--space-2); border-radius: var(--radius-s); }
  .msg-details summary:hover { background: var(--surface-2); color: var(--fg); }
  .msg-details[open] { padding: var(--space-2) var(--space-3); border: 1px dashed var(--border-control); border-radius: var(--radius-s); background: var(--surface); }
  .msg-details p { margin: var(--space-1) 0; }
  .msg-files { list-style: none; margin: var(--space-1) 0 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-1); }
  .msg-files .file { display: flex; flex-wrap: wrap; align-items: center; gap: 2px var(--space-2); padding: var(--space-1) var(--space-2); border: 1px solid var(--border); border-radius: var(--radius-s); background: var(--surface); font-size: var(--text-sm); line-height: var(--leading-sm); }
  .file-glyph { flex: none; width: 14px; height: 16px; border-radius: 2px; border: 2px solid var(--fg-muted); }
  .file-image .file-glyph { height: 12px; border-radius: 3px; }
  .file-voice .file-glyph, .file-audio .file-glyph { width: 12px; border-radius: 6px; }
  .file-video .file-glyph { width: 16px; height: 12px; }
  .file-kind { font-weight: var(--weight-semibold); }
  .file-size { color: var(--fg-muted); }
  .msg-files .file-refused { background: var(--status-danger-soft); border-color: var(--status-danger); color: var(--status-danger); }
  .file-refused .file-glyph { border-color: var(--status-danger); }
  .file-refused .file-size { color: inherit; }
  .file-note { flex-basis: 100%; }
  .msg-new { animation: arrive var(--duration) var(--ease); }
  @keyframes arrive { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
  .composer-slot { flex: none; }
  .composer { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-1); }
  .composer textarea { resize: none; max-height: calc(8 * var(--leading-md) + 14px); overflow-y: auto; }
  .composer-foot { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: var(--space-2); }
  .composer-foot #reply-hint { flex: 1; }
  .reply-count { color: var(--fg-muted); font-variant-numeric: tabular-nums; }
  .reply-count:empty { display: none; }
  .composer-note { margin: 0; padding: var(--space-3); border: 1px dashed var(--border-control); border-radius: var(--radius-m); color: var(--fg-muted); background: var(--surface); }
  .conv-context { padding: var(--space-4); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-1); }
  .conv-context h2 { margin: 0 0 var(--space-3); font-size: var(--text-md); line-height: var(--leading-md); }
  .facts { margin: 0; display: grid; grid-template-columns: minmax(0, 1fr); }
  .facts dt { font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-semibold); color: var(--fg-muted); }
  .facts dd { margin: 0 0 var(--space-3); overflow-wrap: anywhere; }
  .breakglass-offer { margin-top: var(--space-3); border-top: 1px solid var(--border); padding-top: var(--space-3); }
  .breakglass-offer .form { margin-top: var(--space-3); }

  /* Live regions: loading and stale states */
  .live-region { position: relative; }
  .live-region[aria-busy="true"]::before {
    content: ""; position: absolute; left: 0; right: 0; top: 0; height: 2px; z-index: 1;
    background: linear-gradient(90deg, transparent, var(--accent), transparent); background-size: 50% 100%;
    animation: loading 1s linear infinite;
  }
  @keyframes loading { from { background-position: -50% 0; } to { background-position: 150% 0; } }
  .stale-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2) var(--space-4); margin: var(--space-2) 0; padding: var(--space-2) var(--space-3); border-left: 4px solid var(--status-waiting); border-radius: var(--radius-s); background: var(--status-waiting-soft); color: var(--fg); }
  .stale-bar button { padding: var(--space-1) var(--space-3); }

  /* Toasts: bottom left, away from Send; errors stay until dismissed. */
  .toasts { position: fixed; left: var(--space-4); bottom: var(--space-4); z-index: 20; display: flex; flex-direction: column; gap: var(--space-2); max-width: min(26rem, calc(100vw - 2rem)); pointer-events: none; }
  .toasts > div { display: flex; flex-direction: column; gap: var(--space-2); }
  .toast { pointer-events: auto; display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4); border-radius: var(--radius-m); background: var(--fg); color: var(--bg); box-shadow: var(--shadow-3); }
  .toast > span { flex: 1; padding-top: 2px; }
  .toast[data-kind="error"] { background: var(--status-danger-soft); color: var(--status-danger); border: 1px solid var(--status-danger); }
  .toast-close { flex: none; min-width: var(--target-min); min-height: var(--target-min); padding: 0 6px; border: 0; background: transparent; color: inherit; font-size: var(--text-lg); line-height: 1; }
  .toast-close:hover { background: rgb(127 127 127 / 20%); }

  .htmx-indicator { opacity: 0; visibility: hidden; }
  .htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; visibility: visible; transition: opacity var(--duration) var(--ease); }
}

@layer utilities {
  .mono { font-family: var(--mono); font-size: .92em; }
  .muted { color: var(--fg-muted); }
  .small { font-size: var(--text-sm); line-height: var(--leading-sm); }
  .nowrap { white-space: nowrap; }
  .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  [hidden] { display: none !important; }
}

@media (max-width: 1023px) {
  @layer tokens { :root { --sidenav-w: 12rem; } }
}
/* Phones: the side navigation becomes a drawer opened from the top bar. */
@media (max-width: 767px) {
  @layer layout {
    header.top { gap: var(--space-2); padding: 0 var(--space-2); }
    main { padding: var(--space-3); }
    .side { display: none; position: fixed; z-index: 9; top: var(--topbar-h); left: 0; bottom: 0; height: auto; width: min(18rem, 85vw); box-shadow: var(--shadow-3); }
    body:has(.drawer[open]) .side { display: block; }
  }
  @layer components {
    .drawer { display: block; }
    .brand-name { display: none; }
    .tenant, .switcher { border-left: 0; padding-left: 0; }
    .tenant, .switcher > summary { max-width: 10rem; }
    .switcher-menu { left: 0; position: fixed; top: var(--topbar-h); right: var(--space-2); left: var(--space-2); min-width: 0; }
    .usermenu > summary .who, .usermenu > summary .role { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
    .usermenu > summary { padding: 2px; }
    .live-pill { font-size: 0; gap: 0; padding: var(--space-2); }
    .q-row { grid-template-columns: 32px minmax(0, 1fr); padding: var(--space-3); }
    .q-avatar { width: 32px; height: 32px; }
    .q-side { grid-column: 2; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: flex-start; text-align: left; }
  }
  @layer base {
    th, td { padding: var(--space-2); }
  }
  /* Tables marked .stacked become one card per row: each cell carries its
     column name, so no main column scrolls off screen. */
  @layer components {
    .card.table-wrap:has(> table.stacked) { overflow-x: visible; }
    table.stacked thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
    table.stacked, table.stacked tbody, table.stacked tr, table.stacked td { display: block; width: 100%; }
    table.stacked tbody tr, table.stacked tbody.instance { border-bottom: 1px solid var(--border); }
    table.stacked tbody.instance tr { border-bottom: 0; }
    table.stacked td { border: 0; position: relative; padding: var(--space-1) var(--space-3) var(--space-1) calc(35% + var(--space-3)); min-height: 1.5rem; }
    table.stacked td:first-child { padding-top: var(--space-3); }
    table.stacked tr > td:last-child { padding-bottom: var(--space-3); }
    table.stacked td::before { content: attr(data-label); position: absolute; left: var(--space-3); width: calc(35% - var(--space-3)); color: var(--fg-muted); font-size: var(--text-sm); line-height: var(--leading-sm); padding-top: 2px; }
    table.stacked td:not([data-label])::before { content: none; }
    table.stacked td:not([data-label]) { padding-left: var(--space-3); }
    table.stacked td.nowrap { white-space: normal; }
    table.stacked tbody + tbody > tr:first-child > td { border-top: 0; }
  }
}

/* Keyboard help */
@layer components {
  .menu-item { width: 100%; justify-content: flex-start; }
  .shortcuts-dialog {
    position: fixed; inset: 0; margin: auto; height: fit-content; max-height: calc(100vh - 2 * var(--space-4)); overflow-y: auto;
    width: min(30rem, calc(100vw - 2 * var(--space-4))); padding: var(--space-5);
    border: 1px solid var(--border); border-radius: var(--radius-m); background: var(--surface); color: var(--fg); box-shadow: var(--shadow-3);
  }
  .shortcuts-dialog::backdrop { background: color-mix(in srgb, var(--fg) 45%, transparent); }
  .shortcuts-dialog h2 { margin: 0 0 var(--space-3); }
  .keys { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--space-2) var(--space-4); margin: 0 0 var(--space-4); align-items: baseline; }
  .keys dt { white-space: nowrap; }
  .keys dd { margin: 0; }
  kbd { display: inline-block; min-width: 1.6em; padding: 1px 6px; border: 1px solid var(--border-control); border-bottom-width: 2px; border-radius: var(--radius-s); background: var(--surface-2); font-family: var(--mono); font-size: var(--text-sm); text-align: center; }
  .shortcuts-dialog .form-actions { justify-content: flex-end; margin-top: var(--space-3); }
}

/* Inbox layout. From 768 px the conversation pane and the queue fill the
   viewport; below 1280 px the context pane is a toggle with scripts, and
   below the conversation (a page scroll away) without them. */
:root:has(body > .banner) { --strip-h: 2.5rem; }
@media (min-width: 768px) {
  @layer components {
    .inbox { --inbox-h: calc(100dvh - var(--topbar-h) - var(--strip-h, 0px) - 2 * var(--space-3)); position: relative; grid-template-rows: var(--inbox-h) auto; grid-template-areas: "pane" "ctx"; }
    .conv-pane, .inbox-queue { height: var(--inbox-h); }
    .inbox.has-queue { grid-template-columns: 17rem minmax(0, 1fr); grid-template-areas: "queue pane" ". ctx"; }
    .inbox-queue { display: block; grid-area: queue; }
    .conv-pane { grid-area: pane; }
    .conv-context { grid-area: ctx; }
    .inbox.has-queue .conv-head .crumb { display: none; }
  }
}
@media (min-width: 1280px) {
  @layer components {
    .inbox { grid-template-columns: minmax(0, 1fr) 18rem; grid-template-rows: var(--inbox-h); grid-template-areas: "pane ctx"; }
    .inbox.has-queue { grid-template-columns: 21rem minmax(0, 1fr) 18rem; grid-template-areas: "queue pane ctx"; }
    .conv-context { height: var(--inbox-h); }
    .context-toggle { display: none; }
  }
}
@media (max-width: 1279px) {
  @layer components {
    .js .conv-context:not(.is-open) { display: none; }
    .context-toggle[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent); }
  }
}
@media (min-width: 768px) and (max-width: 1279px) {
  @layer components {
    .js .inbox { grid-template-rows: var(--inbox-h); }
    .js .conv-context.is-open { grid-area: auto; position: absolute; z-index: 5; top: 0; right: 0; width: min(20rem, 90%); max-height: var(--inbox-h); box-shadow: var(--shadow-3); }
  }
}
@media (max-width: 767px) {
  @layer components {
    .transcript-wrap { min-height: 0; overflow: visible; }
    .composer-slot { position: sticky; bottom: 0; z-index: 2; padding: var(--space-2) 0; background: var(--bg); }
    .msg { max-width: 95%; }
  }
}

/* Reduced motion keeps every state change but drops movement; the loading
   bar becomes a static bar with an "Updating" label instead of vanishing. */
@media (prefers-reduced-motion: reduce) {
  @layer utilities {
    *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  }
  @layer components {
    .live-region[aria-busy="true"]::before { background: var(--accent); }
    .live-region[aria-busy="true"]::after {
      content: attr(data-busy-label); position: absolute; top: var(--space-1); right: var(--space-1); z-index: 1;
      padding: 0 var(--space-2); border-radius: var(--radius-pill); font-size: var(--text-xs); line-height: var(--leading-xs);
      background: var(--accent-soft); color: var(--accent);
    }
  }
}

/* Forms: fields, hints, errors, confirmation */
@layer components {
  .form { display: flex; flex-direction: column; gap: var(--space-4); max-width: var(--form-max); }
  h1 + .form, h1 + .error-summary { margin-top: var(--space-4); }
  h2 + .form { margin-top: var(--space-3); }
  .field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
  .field > label {
    flex-direction: row; flex-wrap: wrap; align-items: baseline; gap: var(--space-2);
    font-size: var(--text-sm); line-height: var(--leading-sm); font-weight: var(--weight-semibold); color: var(--fg);
  }
  .field-required { font-size: var(--text-xs); line-height: var(--leading-xs); font-weight: var(--weight-regular); color: var(--fg-muted); }
  .field-hint { margin: 0; font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); }
  .field > input, .field > select, .field > textarea { width: 100%; }
  .field > input[type="number"] { max-width: 10rem; }
  .field-error { margin: 0; font-size: var(--text-sm); line-height: var(--leading-sm); font-weight: var(--weight-semibold); color: var(--status-danger); overflow-wrap: anywhere; }
  .field-invalid { padding-left: var(--space-3); border-left: 4px solid var(--status-danger); }
  .field [aria-invalid="true"] { border-color: var(--status-danger); box-shadow: inset 0 0 0 1px var(--status-danger); }
  .form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
  .form-preview { padding: var(--space-2) var(--space-3); border-left: 4px solid var(--status-info); border-radius: var(--radius-s); background: var(--status-info-soft); }
  .form-preview p { margin: 0; }
  .form-preview p + p { margin-top: var(--space-1); }
  .error-summary {
    max-width: var(--form-max); margin: var(--space-3) 0; padding: var(--space-3) var(--space-4);
    border: 2px solid var(--status-danger); border-radius: var(--radius-m); background: var(--surface);
  }
  .error-summary h2 { margin: 0 0 var(--space-2); font-size: var(--text-md); line-height: var(--leading-md); color: var(--status-danger); }
  .error-summary ul { margin: 0; padding-left: var(--space-4); }
  .error-summary a { color: var(--status-danger); font-weight: var(--weight-semibold); overflow-wrap: anywhere; }
  .confirm { max-width: var(--form-max); padding: var(--space-5); }
  .confirm h1 { margin-bottom: var(--space-3); }
  .confirm-facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--space-2) var(--space-4); margin: var(--space-3) 0 var(--space-4); }
  .confirm-facts dt { font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); }
  .confirm-facts dd { margin: 0; overflow-wrap: anywhere; white-space: pre-line; }
  .confirm-consequence { font-weight: var(--weight-semibold); }
  .confirm .form-actions { margin-top: var(--space-4); }
  .confirm-dialog {
    position: fixed; inset: 0; margin: auto; height: fit-content;
    width: min(30rem, calc(100vw - 2 * var(--space-4))); padding: var(--space-5);
    border: 1px solid var(--border); border-radius: var(--radius-m); background: var(--surface); color: var(--fg); box-shadow: var(--shadow-3);
  }
  .confirm-dialog p { margin: 0 0 var(--space-4); font-size: var(--text-md); line-height: var(--leading-md); }
  .confirm-dialog .form-actions { justify-content: flex-end; }
  .confirm-dialog::backdrop { background: color-mix(in srgb, var(--fg) 45%, transparent); }
  @media (max-width: 640px) {
    .confirm { padding: var(--space-4); }
    .confirm-facts { grid-template-columns: minmax(0, 1fr); gap: 0; }
    .confirm-facts dd { margin-bottom: var(--space-2); }
  }
}

/* Broadcast composer and template editor: radio choices with a plain
   description, the message preview, the advanced audience filter. */
@layer components {
  .bc-composer, .editor { max-width: 48rem; }
  .compose-step { display: flex; flex-direction: column; gap: var(--space-4); }
  .compose-step > h2, .editor > h2 { margin: var(--space-2) 0 0; }
  .compose-fields, .compose-part { display: flex; flex-direction: column; gap: var(--space-4); }
  .compose-part > h3 { margin: 0; font-size: var(--text-md); }
  .js .bc-composer[data-mode="template"] [data-mode-part="message"],
  .js .bc-composer[data-mode="message"] [data-mode-part="template"] { display: none; }
  .js .bc-composer .compose-part > h3 { display: none; }
  .choices, .vars, .buttons-rows { display: flex; flex-direction: column; gap: var(--space-2); margin: 0; padding: 0; border: 0; min-width: 0; }
  .choices > legend, .vars > legend, .buttons-rows > legend { padding: 0; margin-bottom: var(--space-1); font-size: var(--text-sm); line-height: var(--leading-sm); font-weight: var(--weight-semibold); }
  .choice { display: flex; flex-direction: row; color: var(--fg); font-size: var(--text-md); gap: var(--space-3); align-items: flex-start; padding: var(--space-3); border: 1px solid var(--border-control); border-radius: var(--radius-s); background: var(--surface); cursor: pointer; }
  .choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
  .choice input { margin-top: 3px; flex: none; }
  .choice-text { display: flex; flex-direction: column; gap: 2px; }
  .choice-title { font-weight: var(--weight-semibold); }
  .choice-desc { font-size: var(--text-sm); line-height: var(--leading-sm); color: var(--fg-muted); }
  .button-row, .preview-choose { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: var(--space-3); }
  .advanced { border: 1px dashed var(--border-control); border-radius: var(--radius-s); padding: var(--space-2) var(--space-3); }
  .advanced > summary { font-weight: var(--weight-semibold); min-height: var(--target-min); display: flex; align-items: center; }
  .advanced[open] { display: flex; flex-direction: column; gap: var(--space-3); padding-bottom: var(--space-3); }
  .compose-preview { display: flex; flex-direction: column; gap: var(--space-3); margin: 0; }
  .compose-preview > h2 { margin: 0; }
  .msg-preview { margin: 0; max-width: 32rem; padding: var(--space-3) var(--space-4); border-radius: var(--radius-m); background: var(--author-bot); border: 1px solid var(--border); }
  .msg-preview figcaption { margin-bottom: var(--space-2); }
  .msg-preview-subject { margin: 0 0 var(--space-2); }
  .msg-preview-text { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
  .msg-preview-buttons { list-style: none; display: flex; flex-wrap: wrap; gap: var(--space-2); margin: var(--space-2) 0 0; padding: 0; }
  .msg-preview-buttons li { padding: var(--space-1) var(--space-3); border: 1px solid var(--border-control); border-radius: var(--radius-s); background: var(--surface); font-size: var(--text-sm); }
  .tpl-text pre, .tpl-source pre { margin: var(--space-1) 0; padding: var(--space-2) var(--space-3); white-space: pre-wrap; overflow-wrap: anywhere; font-family: var(--mono); font-size: var(--text-sm); background: var(--surface-2); border-radius: var(--radius-s); }
  .tpl-source > summary { font-size: var(--text-sm); color: var(--fg-muted); }
  .variant-edit { display: flex; flex-direction: column; gap: var(--space-3); }
  .variant-edit > legend { font-weight: var(--weight-semibold); padding: 0 var(--space-1); }
  .variant-blank { border-style: dashed; box-shadow: none; }
  .variant-card h3 { margin: 0 0 var(--space-2); font-size: var(--text-md); }
  label.check { display: flex; flex-direction: row; align-self: flex-start; color: var(--fg); font-size: var(--text-md); gap: var(--space-2); align-items: center; font-weight: var(--weight-semibold); }
  .confirm-facts dd { white-space: pre-wrap; overflow-wrap: anywhere; }
  /* Internal notes: centred and dashed, never mistaken for a message the customer saw */
  .msg-note { align-self: center; max-width: min(40rem, 92%); }
  .msg-note .bubble { background: var(--author-note); border: 1px dashed var(--author-note-border); border-radius: var(--radius-m); }
  .msg-note .msg-role { color: var(--author-note-border); }
  .msg-note .msg-role::before { content: ""; display: inline-block; width: 10px; height: 10px; margin-right: var(--space-1); border: 2px solid currentColor; border-radius: 2px; vertical-align: -1px; }
  .note-actions { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; margin-top: var(--space-1); font-size: var(--text-sm); }
  .note-actions a, button.link { display: inline-flex; align-items: center; min-height: var(--target-min); }
  button.link { padding: 0; border: 0; background: none; color: var(--accent); text-decoration: underline; font: inherit; cursor: pointer; }
  .danger-text, button.danger-text, button.link.danger-text { color: var(--status-danger); }
  button.ghost.danger-text:hover { background: var(--status-danger-soft); color: var(--status-danger); }
  .note-slot { flex: none; margin-top: var(--space-2); }
  .note-composer { padding: var(--space-2) var(--space-3); background: var(--surface); border: 1px dashed var(--author-note-border); border-radius: var(--radius-m); }
  .note-composer > summary { min-height: var(--target-min); display: flex; align-items: center; font-weight: var(--weight-semibold); cursor: pointer; }
  .note-form { display: flex; flex-direction: column; gap: var(--space-2); padding-bottom: var(--space-2); }
  .note-form textarea { resize: vertical; }
  .archived-note { margin: var(--space-2) 0 0; }
  /* The summary sits above the transcript; its text scrolls inside the
     block so a long summary never pushes the transcript off a fixed pane. */
  .summary-slot { flex: none; }
  .summary-box { padding: var(--space-2) var(--space-3); background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--status-info); border-radius: var(--radius-m); }
  .summary-box.is-stale { border-left-color: var(--status-waiting); }
  .summary-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-3); min-height: var(--target-min); cursor: pointer; }
  .summary-head { list-style: none; }
  .summary-head::-webkit-details-marker { display: none; }
  .summary-head::before { content: ""; flex: none; width: 7px; height: 7px; margin: 0 var(--space-1); border-right: 2px solid var(--fg-muted); border-bottom: 2px solid var(--fg-muted); transform: rotate(-45deg); transition: transform var(--duration-fast) var(--ease); }
  .summary-box[open] > .summary-head::before { transform: rotate(45deg); }
  .summary-title { font-weight: var(--weight-semibold); }
  .summary-meta { display: inline-flex; flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-2); min-width: 0; color: var(--fg-muted); font-size: var(--text-sm); line-height: var(--leading-sm); }
  .summary-source .mono { overflow-wrap: anywhere; }
  .summary-body { display: flex; flex-direction: column; gap: var(--space-2); padding-top: var(--space-2); }
  .summary-body p { margin: 0; }
  .summary-text { white-space: pre-line; overflow-wrap: anywhere; max-height: 9rem; overflow-y: auto; }
  .summary-stale { color: var(--status-waiting); }
  .summary-pending { color: var(--fg); }
  .summary-pending::before { content: ""; display: inline-block; width: .6em; height: .6em; margin-right: var(--space-2); border-radius: 50%; background: var(--status-info); vertical-align: .05em; }
  .summary-form { align-self: flex-start; }
  @media (max-width: 767px) { .summary-text { max-height: none; overflow: visible; } }
  .bulk-bar { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-3); align-items: center; margin: var(--space-3) 0 0; }
  .q-row.has-select { grid-template-columns: 28px 36px minmax(0, 1fr) auto; }
  .q-select { display: flex; align-items: center; justify-content: center; min-height: var(--target-min); }
  .q-select input { position: relative; z-index: 1; width: 18px; height: 18px; }
  @media (max-width: 767px) { .q-row.has-select { grid-template-columns: 28px 32px minmax(0, 1fr); } }
}
