/* TCP UI: TCP's design system. Plain CSS, no build step, no framework.
 * Load tcp-ui.css and tcp-ui.js from the same folder (fonts/, icons.svg next to them).
 *
 * 1. Fonts and tokens (light, dark)   5. Feedback: badges, callouts, toasts, empty states, skeletons
 * 2. Base                             6. Overlays: dialogs, menus, command palette, popovers
 * 3. App shell: sidebar, top bar      7. Content: cards, tables, avatars, code, kbd
 * 4. Controls: buttons, fields        8. Utilities
 *
 * Icons: <svg class="icon"><use href="icons.svg#i-NAME"/></svg> (UI.icon() builds them, with the right path).
 * The second tone of a duotone icon is var(--tone) at var(--tone-opacity); set those to colour it.
 */

/* --- 1. Fonts and tokens ------------------------------------------------------------------------- */

@font-face {
  font-family: "Nunito";
  src: url("fonts/nunito.woff2") format("woff2"); /* relative to this file */
  font-weight: 400 800;
  font-display: swap;
}

:root {
  /* TCP brand */
  --navy: #123462;
  --navy-2: #1b3f73;
  --navy-3: #0c2547;
  --cyan: #1eafd6;
  --cyan-strong: #0f8fb3;
  --cyan-deep: #0b6f8b;
  --orange: #f08700;
  --purple: #98459a;
  --teal: #2d8a9e;

  /* Surfaces and text */
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --surface-3: #e6ebf2;
  --border: #dfe4ec;
  --border-strong: #c7d0dc;
  --text: #172033;
  --text-2: #36435a;
  --muted: #5d6b82;
  --faint: #8a96a8;
  --link: #0f8fb3;
  --focus: rgba(30, 175, 214, 0.5);

  /* Meaning */
  --accent-soft: #e3f6fb;
  --accent-text: #0b6f8b;
  --success: #1f9d55;
  --success-soft: #e3f5ea;
  --success-text: #136c3a;
  --warning: #f08700;
  --warning-soft: #fdf0dc;
  --warning-text: #8a4d00;
  --warning-border: #f3cf97;
  --danger: #d64545;
  --danger-soft: #fdecec;
  --danger-text: #a12f2f;
  --danger-border: #f0c7c7;
  --info-soft: #e3f6fb;
  --info-text: #0b6f8b;
  --purple-soft: #f4e8f4;
  --purple-text: #7a347c;

  /* Section colours (icon second tones); rename or add per product */
  --tone-claude: #1eafd6;
  --tone-activity: #21c1d9;
  --tone-containers: #2d8a9e;
  --tone-logs: #8fa6c8;
  --tone-deploys: #f08700;
  --tone-approvals: #b45fb6;
  --tone-secrets: #e8b400;
  --tone-apps: #2fb36f;
  --tone-docs: #6f8fd6;
  --tone-settings: #8fa6c8;

  /* Sidebar (always navy) */
  --side-bg: #123462;
  --side-hover: #1b3f73;
  --side-active: #1f477f;
  --side-text: #dbe6f5;
  --side-muted: #9fb4d3;
  --side-border: rgba(255, 255, 255, 0.08);

  /* Shape and depth */
  --r-xs: 6px;
  --r-sm: 9px;
  --r: 11px;
  --r-lg: 14px;
  --r-xl: 18px;
  --shadow-1: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-2: 0 2px 6px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-pop: 0 16px 40px rgba(12, 37, 71, 0.18), 0 2px 8px rgba(12, 37, 71, 0.08);

  /* Type */
  --font: "Nunito", ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --sidebar-w: 256px;
  --topbar-h: 56px;
  color-scheme: light;
}

/* Dark: follows the system unless the user picked light; or forced with data-theme="dark" */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e141e; --surface: #161f2e; --surface-2: #1c2738; --surface-3: #243149;
    --border: #263349; --border-strong: #34445f;
    --text: #e8edf4; --text-2: #c3cddb; --muted: #97a3b6; --faint: #6f7d92; --link: #5ccbea;
    --accent-soft: #10364a; --accent-text: #7fd6ee;
    --success-soft: #11321f; --success-text: #7fdba3;
    --warning-soft: #36270f; --warning-text: #f7c27a; --warning-border: #5a4220;
    --danger-soft: #3a1717; --danger-text: #f2a0a0; --danger-border: #5a2a2a;
    --info-soft: #10364a; --info-text: #7fd6ee;
    --purple-soft: #33203a; --purple-text: #e1b2e3;
    --side-bg: #0b1a31; --side-hover: #132848; --side-active: #183056;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-2: 0 2px 8px rgba(0, 0, 0, 0.35);
    --shadow-pop: 0 18px 44px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e141e; --surface: #161f2e; --surface-2: #1c2738; --surface-3: #243149;
  --border: #263349; --border-strong: #34445f;
  --text: #e8edf4; --text-2: #c3cddb; --muted: #97a3b6; --faint: #6f7d92; --link: #5ccbea;
  --accent-soft: #10364a; --accent-text: #7fd6ee;
  --success-soft: #11321f; --success-text: #7fdba3;
  --warning-soft: #36270f; --warning-text: #f7c27a; --warning-border: #5a4220;
  --danger-soft: #3a1717; --danger-text: #f2a0a0; --danger-border: #5a2a2a;
  --info-soft: #10364a; --info-text: #7fd6ee;
  --purple-soft: #33203a; --purple-text: #e1b2e3;
  --side-bg: #0b1a31; --side-hover: #132848; --side-active: #183056;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-2: 0 2px 8px rgba(0, 0, 0, 0.35);
  --shadow-pop: 0 18px 44px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

/* --- 2. Base ---------------------------------------------------------------------------------- */

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4 { margin: 0; font-weight: 800; letter-spacing: -0.01em; line-height: 1.25; }
h1 { font-size: 25px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }
h4 { font-size: 14px; }
p { margin: 0; }
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
/* Whole cards and list rows that are links read as blocks, not as underlined text */
a.card, a.list-item, a.card:hover, a.list-item:hover { color: inherit; text-decoration: none; }
code, pre, kbd, samp { font-family: var(--mono); }
img, svg { display: block; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
::selection { background: color-mix(in srgb, var(--cyan) 30%, transparent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

.icon {
  width: 20px;
  height: 20px;
  flex: none;
  display: inline-block;
  vertical-align: middle;
  fill: currentColor;
}
.icon-xs { width: 14px; height: 14px; }
.icon-sm { width: 16px; height: 16px; }
.icon-lg { width: 24px; height: 24px; }
.icon-xl { width: 32px; height: 32px; }
.icon-2xl { width: 44px; height: 44px; }
/* A coloured second tone: set --tone (and it renders at full strength) */
.icon[style*="--tone"], .icon.toned { --tone-opacity: 1; }

/* --- 3. App shell ------------------------------------------------------------------------------- */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "side top" "side main";
  height: 100vh;
}
.app[data-sidebar="collapsed"] { --sidebar-w: 72px; }

.sidebar {
  grid-area: side;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--side-bg);
  color: var(--side-text);
  border-right: 1px solid var(--side-border);
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--topbar-h);
  padding: 0 16px;
  color: #fff;
  text-decoration: none;
  flex: none;
}
.sidebar-brand:hover { text-decoration: none; }
.sidebar-brand .logo { height: 24px; padding: 4px 6px; background: #fff; border-radius: 7px; }
.sidebar-brand .word { font-size: 21px; font-weight: 800; letter-spacing: -0.01em; }
.sidebar-scroll { flex: 1; overflow-y: auto; padding: 6px 10px 12px; }
.sidebar-foot { padding: 10px; border-top: 1px solid var(--side-border); }

.sidebar-context { padding: 6px 10px 12px; }
.sidebar-context .overline { color: var(--side-muted); }
.sidebar-context .title { margin-top: 2px; color: #fff; font-weight: 800; line-height: 1.3; }
.sidebar-back {
  display: inline-flex; align-items: center; gap: 4px; margin: 2px 0 8px;
  color: var(--side-muted); font-size: 13px; font-weight: 700;
}
.sidebar-back:hover { color: #fff; text-decoration: none; }

.nav-heading {
  margin: 14px 10px 4px;
  color: var(--side-muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--side-text);
  font: inherit;
  font-weight: 700;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.nav-item .icon { color: #fff; }
.nav-item:hover { background: var(--side-hover); text-decoration: none; }
.nav-item[aria-current="page"] { background: var(--side-active); color: #fff; box-shadow: inset 3px 0 0 var(--cyan); }
.nav-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count {
  min-width: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--orange); color: #fff; font-size: 11px; font-weight: 800; text-align: center; line-height: 18px;
}
.nav-item .count.neutral { background: rgba(255, 255, 255, 0.16); }
.nav-sub { padding-left: 30px; font-weight: 600; min-height: 30px; font-size: 14px; }
.app[data-sidebar="collapsed"] .nav-item { justify-content: center; padding: 6px; }
.app[data-sidebar="collapsed"] .nav-item .label,
.app[data-sidebar="collapsed"] .nav-heading,
.app[data-sidebar="collapsed"] .sidebar-context,
.app[data-sidebar="collapsed"] .sidebar-brand .word { display: none; }
.app[data-sidebar="collapsed"] .nav-item .count { position: absolute; margin: -18px 0 0 22px; }

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .crumbs { display: flex; align-items: center; gap: 6px; min-width: 0; color: var(--muted); font-weight: 700; }
.topbar .crumbs a { color: var(--muted); }
.topbar .crumbs .current { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }

.search-trigger {
  display: flex; align-items: center; gap: 8px;
  min-width: 300px; height: 36px; padding: 0 10px 0 12px;
  border: 1px solid var(--border); border-radius: var(--r);
  background: var(--surface-2); color: var(--muted);
  font: inherit; font-weight: 600; cursor: pointer;
}
.search-trigger:hover { border-color: var(--border-strong); background: var(--surface); }
.search-trigger kbd { margin-left: auto; }

.main { grid-area: main; min-width: 0; min-height: 0; overflow: auto; }
.main.flush { overflow: hidden; display: flex; flex-direction: column; }
.page { max-width: 1200px; margin: 0 auto; padding: 24px 28px 48px; }
.page-wide { max-width: none; }
.page-header { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 20px; }
.page-header .titles { flex: 1; min-width: 0; }
.page-header h1 { display: flex; align-items: center; gap: 10px; }
.page-header .subtitle { margin-top: 4px; color: var(--muted); }
.page-header .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.section { margin-top: 28px; }
.section-header { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.section-header h2 { flex: 1; display: flex; align-items: center; gap: 8px; font-size: 17px; }

/* --- 4. Controls ----------------------------------------------------------------------------------- */

.btn {
  --h: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: var(--h);
  padding: 0 16px;
  border: 0;
  border-radius: var(--r);
  font: inherit;
  font-weight: 800;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s, transform 0.08s, filter 0.15s, color 0.15s;
  user-select: none;
}
.btn:hover { text-decoration: none; }
.btn .icon { width: 18px; height: 18px; }
.btn-sm { --h: 30px; padding: 0 11px; border-radius: var(--r-sm); font-size: 13px; gap: 6px; }
.btn-sm .icon { width: 16px; height: 16px; }
.btn-lg { --h: 46px; padding: 0 22px; font-size: 16px; border-radius: var(--r-lg); }
.btn-icon { padding: 0; width: var(--h); }
.btn-block { width: 100%; }
.btn:disabled, .btn[aria-disabled="true"] { cursor: not-allowed; opacity: 0.5; filter: grayscale(0.4); transform: none !important; }

.btn-primary {
  color: #fff;
  background: linear-gradient(180deg, #2bbde3 0%, #0f93b8 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 1px 2px rgba(12, 37, 71, 0.25), 0 0 0 1px #0c86a8;
  --tone: #c4f1ff;
  --tone-opacity: 1;
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.06);
  transform: translateY(-1px);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 4px 12px rgba(15, 143, 179, 0.35), 0 0 0 1px #0c86a8;
}
.btn-primary:active:not(:disabled) {
  transform: translateY(0);
  filter: brightness(0.96);
  box-shadow: inset 0 2px 3px rgba(0, 0, 0, 0.18), 0 0 0 1px #0c86a8;
}

.btn-secondary {
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08), 0 0 0 1px var(--border);
  --tone: var(--cyan);
  --tone-opacity: 1;
}
.btn-secondary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(16, 24, 40, 0.1), 0 0 0 1px color-mix(in srgb, var(--cyan) 45%, var(--border));
}
.btn-secondary:active:not(:disabled) { transform: translateY(0); background: var(--surface-2); }

.btn-danger {
  color: var(--danger-text);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08), 0 0 0 1px var(--danger-border);
  --tone: #f3a5a5;
  --tone-opacity: 1;
}
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); transform: translateY(-1px); }
.btn-danger.solid { color: #fff; background: linear-gradient(180deg, #e25b5b 0%, #c43b3b 100%); box-shadow: inset 0 1px 0 rgba(255,255,255,.25), 0 0 0 1px #b03333; --tone: #ffd0d0; }

.btn-ghost { color: var(--link); background: transparent; --tone: var(--cyan); --tone-opacity: 1; }
.btn-ghost:hover:not(:disabled) { background: var(--accent-soft); }
.btn-ghost.neutral { color: var(--muted); }
.btn-ghost.neutral:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn.is-loading { pointer-events: none; }
.btn.is-loading::before {
  content: "";
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  animation: spin 1.4s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.btn-group { display: inline-flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Fields */
.field { display: grid; gap: 6px; align-content: start; }
.field > label, .form-label { font-size: 14px; font-weight: 800; color: var(--text); }
.field .hint { font-size: 13px; color: var(--muted); }
.field .error { font-size: 13px; color: var(--danger-text); font-weight: 700; }
.field.invalid .input, .field.invalid .select, .field.invalid .textarea { box-shadow: 0 0 0 1px var(--danger); }

.input, .select, .textarea {
  width: 100%;
  min-height: 38px;
  padding: 8px 12px;
  border: 0;
  border-radius: var(--r);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  box-shadow: inset 0 1px 2px rgba(16, 24, 40, 0.05), 0 0 0 1px var(--border);
  transition: box-shadow 0.15s;
}
.input::placeholder, .textarea::placeholder { color: var(--faint); }
.input:hover, .select:hover, .textarea:hover { box-shadow: inset 0 1px 2px rgba(16, 24, 40, 0.05), 0 0 0 1px var(--border-strong); }
.input:focus, .select:focus, .textarea:focus { outline: none; box-shadow: 0 0 0 1px var(--cyan), 0 0 0 4px var(--focus); }
.input:disabled, .select:disabled, .textarea:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
.input.mono, .textarea.mono { font-family: var(--mono); font-size: 14px; }
.input, .select { height: 40px; }
.textarea { min-height: 96px; resize: vertical; line-height: 1.5; }
.select {
  appearance: none;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='none' stroke='%235d6b82' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.8' d='m19 9l-7 6l-7-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 18px;
  cursor: pointer;
}
.input-with-icon { position: relative; }
.input-with-icon > .icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-with-icon > .input { padding-left: 38px; }

.check { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; cursor: pointer; }
.check input { width: 17px; height: 17px; margin: 0; accent-color: var(--cyan-strong); cursor: pointer; }

.switch { display: inline-flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; }
.switch input { appearance: none; margin: 0; width: 38px; height: 22px; border-radius: 999px; background: var(--surface-3); box-shadow: inset 0 0 0 1px var(--border-strong); position: relative; cursor: pointer; transition: background 0.15s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform 0.15s; }
.switch input:checked { background: var(--cyan-strong); box-shadow: none; }
.switch input:checked::after { transform: translateX(16px); }

.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: var(--r); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); }
.segmented button { border: 0; background: none; padding: 5px 12px; border-radius: var(--r-sm); font: inherit; font-size: 14px; font-weight: 700; color: var(--muted); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-2); }

.form-grid { display: grid; gap: 16px; max-width: 640px; }
.form-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* --- 5. Feedback ------------------------------------------------------------------------------------ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--text-2);
}
.badge .icon { width: 14px; height: 14px; }
.badge-success { background: var(--success-soft); color: var(--success-text); }
.badge-warning { background: var(--warning-soft); color: var(--warning-text); }
.badge-danger { background: var(--danger-soft); color: var(--danger-text); }
.badge-info { background: var(--info-soft); color: var(--info-text); }
.badge-purple { background: var(--purple-soft); color: var(--purple-text); }
.badge-outline { background: transparent; box-shadow: inset 0 0 0 1px var(--border-strong); color: var(--muted); }
.badge.live::before, .dot::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor;
}
.badge.live::before { animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
.dot { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 700; }
.dot-success { color: var(--success); } .dot-warning { color: var(--warning); } .dot-danger { color: var(--danger); } .dot-muted { color: var(--faint); }

.callout {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px 14px;
  border-radius: var(--r);
  background: var(--info-soft);
  color: var(--info-text);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 22%, transparent);
}
.callout > .icon { margin-top: 1px; }
.callout .body { flex: 1; min-width: 0; }
.callout .body strong { display: block; }
.callout .body p { color: inherit; opacity: 0.92; }
.callout-success { background: var(--success-soft); color: var(--success-text); }
.callout-warning { background: var(--warning-soft); color: var(--warning-text); }
.callout-danger { background: var(--danger-soft); color: var(--danger-text); }
.callout-neutral { background: var(--surface-2); color: var(--text-2); }

.empty {
  display: grid;
  justify-items: center;
  gap: 8px;
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
}
.empty > .icon { width: 48px; height: 48px; color: var(--border-strong); --tone: var(--cyan); --tone-opacity: 0.8; margin-bottom: 6px; }
.empty h3 { color: var(--text); }
.empty p { max-width: 420px; }
.empty .btn { margin-top: 10px; }

.skeleton {
  display: block;
  height: 14px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%);
  background-size: 200% 100%;
  animation: shimmer 2.8s ease-in-out infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border-strong); border-top-color: var(--cyan); animation: spin 1.4s linear infinite; }

/* Toasts */
.toasts {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 60;
  display: grid;
  gap: 10px;
  width: min(380px, calc(100vw - 36px));
}
.toast {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px 12px 12px 14px;
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--border);
  animation: toast-in 0.2s ease-out;
  --tone-opacity: 1;
}
.toast > .icon { margin-top: 1px; }
.toast .body { flex: 1; min-width: 0; }
.toast .body strong { display: block; }
.toast .body p { color: var(--muted); font-size: 14px; }
.toast-success > .icon { color: var(--success); --tone: var(--success); --tone-opacity: 0.35; }
.toast-error > .icon { color: var(--danger); --tone: var(--danger); --tone-opacity: 0.35; }
.toast-warning > .icon { color: var(--warning); --tone: var(--warning); --tone-opacity: 0.35; }
.toast-info > .icon { color: var(--cyan-strong); --tone: var(--cyan); --tone-opacity: 0.35; }
.toast.leaving { animation: toast-out 0.18s ease-in forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(6px) scale(0.98); } }

/* --- 6. Overlays -------------------------------------------------------------------------------------- */

dialog.dialog {
  width: min(520px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: var(--r-xl);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--border);
}
dialog.dialog::backdrop { background: rgba(9, 20, 38, 0.45); backdrop-filter: blur(2px); }
dialog.dialog[open] { animation: dialog-in 0.18s ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.dialog-header { display: flex; align-items: flex-start; gap: 12px; padding: 20px 22px 0; }
.dialog-header .icon { width: 28px; height: 28px; }
.dialog-header h2 { flex: 1; font-size: 18px; padding-top: 2px; }
.dialog-body { padding: 12px 22px 0; color: var(--text-2); }
.dialog-body pre { margin: 10px 0 0; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 20px 22px; }
.dialog.danger .dialog-header .icon { color: var(--danger); --tone: var(--danger); --tone-opacity: 0.3; }
.dialog.warning .dialog-header .icon { color: var(--warning); --tone: var(--warning); --tone-opacity: 0.3; }
.dialog.info .dialog-header .icon { color: var(--cyan-strong); --tone: var(--cyan); --tone-opacity: 0.35; }

.menu {
  position: fixed;
  z-index: 50;
  min-width: 220px;
  padding: 6px;
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--border);
  animation: dialog-in 0.12s ease-out;
}
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border: 0; border-radius: var(--r-sm);
  background: none; color: var(--text); font: inherit; font-weight: 700; text-align: left; cursor: pointer;
  --tone: var(--cyan); --tone-opacity: 1;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-2); outline: none; }
.menu-item .hint { margin-left: auto; color: var(--faint); font-size: 12px; font-weight: 700; }
.menu-item.danger { color: var(--danger-text); --tone: var(--danger); }
.menu-item[aria-checked="true"]::after { content: "✓"; margin-left: auto; color: var(--cyan-strong); font-weight: 800; }
.menu-sep { height: 1px; margin: 6px 4px; background: var(--border); }
.menu-label { padding: 6px 10px 4px; color: var(--faint); font-size: 11px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }

/* Command palette (⌘K) */
dialog.palette {
  width: min(640px, calc(100vw - 32px));
  margin-top: 12vh;
  padding: 0;
  border: 0;
  border-radius: var(--r-xl);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--border);
  overflow: hidden;
}
dialog.palette::backdrop { background: rgba(9, 20, 38, 0.4); backdrop-filter: blur(2px); }
.palette-input { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--border); --tone: var(--cyan); --tone-opacity: 1; }
.palette-input input { flex: 1; border: 0; outline: none; background: none; color: var(--text); font: inherit; font-size: 17px; font-weight: 600; }
.palette-results { max-height: min(420px, 55vh); overflow-y: auto; padding: 6px; }
.palette-group { padding: 8px 10px 4px; color: var(--faint); font-size: 11px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
.palette-item { display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: var(--r-sm); cursor: pointer; --tone-opacity: 1; }
.palette-item .text { flex: 1; min-width: 0; }
.palette-item .text strong { display: block; font-weight: 800; }
.palette-item .text span { color: var(--muted); font-size: 13px; }
.palette-item[aria-selected="true"] { background: var(--accent-soft); }
.palette-empty { padding: 28px 16px; text-align: center; color: var(--muted); }
.palette-foot { display: flex; gap: 14px; padding: 8px 14px; border-top: 1px solid var(--border); color: var(--faint); font-size: 12px; font-weight: 700; }

/* Popover panel (notifications) */
.popover {
  position: fixed;
  z-index: 50;
  width: min(400px, calc(100vw - 24px));
  max-height: min(560px, 75vh);
  display: flex;
  flex-direction: column;
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--border);
  animation: dialog-in 0.12s ease-out;
  overflow: hidden;
}
.popover-header { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.popover-header h3 { flex: 1; }
.popover-body { overflow-y: auto; }
.notice { display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; --tone-opacity: 1; }
.notice:hover { background: var(--surface-2); }
.notice.unread { background: color-mix(in srgb, var(--accent-soft) 60%, var(--surface)); }
.notice.unread::after { content: ""; width: 8px; height: 8px; margin-top: 7px; border-radius: 50%; background: var(--cyan); flex: none; }
.notice .text { flex: 1; min-width: 0; font-size: 14px; }
.notice .text strong { display: block; }
.notice .text span { color: var(--muted); font-size: 13px; }
.bell { position: relative; }
.bell .count { position: absolute; top: 2px; right: 2px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px; background: var(--orange); color: #fff; font-size: 10px; font-weight: 800; line-height: 16px; text-align: center; }

/* --- 7. Content ---------------------------------------------------------------------------------------- */

.card {
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), 0 0 0 1px var(--border);
}
.card-pad { padding: 18px 20px; }
.card-header { display: flex; align-items: center; gap: 10px; padding: 16px 20px 0; }
.card-header h3 { flex: 1; display: flex; align-items: center; gap: 8px; font-size: 16px; }
.card-body { padding: 14px 20px 18px; }
.card-footer { display: flex; gap: 8px; align-items: center; padding: 12px 20px; border-top: 1px solid var(--border); }
.card.interactive { transition: box-shadow 0.15s, transform 0.08s; cursor: pointer; }
.card.interactive:hover { transform: translateY(-1px); box-shadow: var(--shadow-2), 0 0 0 1px color-mix(in srgb, var(--cyan) 40%, var(--border)); }

.stat { display: grid; gap: 2px; }
.stat .value { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }
.stat .label { color: var(--muted); font-size: 13px; font-weight: 700; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  position: sticky; top: 0;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.03em;
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
}
.table td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 70%, transparent); }
.table .actions { text-align: right; white-space: nowrap; }
.table .primary { font-weight: 800; color: var(--text); }
.table .secondary { display: block; color: var(--muted); font-size: 13px; }

.list { display: grid; grid-template-columns: minmax(0, 1fr); }
.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-top: 1px solid var(--border); }
.list-item:first-child { border-top: 0; }
.list-item .main-text { flex: 1; min-width: 0; }
.list-item .main-text strong { display: block; font-weight: 800; }
.list-item .main-text span { color: var(--muted); font-size: 13px; }

.avatar {
  --size: 28px;
  width: var(--size); height: var(--size); flex: none;
  border-radius: 50%;
  display: inline-grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-text);
  font-size: calc(var(--size) * 0.4); font-weight: 800;
  object-fit: cover;
  box-shadow: 0 0 0 2px var(--surface);
}
.avatar-sm { --size: 22px; }
.avatar-lg { --size: 40px; }
.avatars { display: inline-flex; }
.avatars .avatar + .avatar { margin-left: -6px; }

kbd {
  display: inline-grid; place-items: center;
  min-width: 22px; height: 22px; padding: 0 6px;
  border-radius: 6px;
  background: var(--surface);
  box-shadow: inset 0 -1px 0 var(--border-strong), 0 0 0 1px var(--border);
  color: var(--muted);
  font-size: 12px; font-weight: 700;
}
code { padding: 1px 5px; border-radius: 5px; background: var(--surface-2); font-size: 0.9em; }
pre.code, pre.log {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--r);
  background: var(--surface-2);
  box-shadow: inset 0 0 0 1px var(--border);
  font: 13px/1.55 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow: auto;
}
pre.code code { padding: 0; background: none; }

.overline { font-size: 11px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: var(--faint); }

/* Living docs (Markdown) */
.prose { max-width: 820px; line-height: 1.65; }
.prose h1 { font-size: 26px; margin: 0 0 14px; }
.prose h2 { font-size: 20px; margin: 28px 0 10px; }
.prose h3 { font-size: 17px; margin: 22px 0 8px; }
.prose p, .prose ul, .prose ol, .prose table, .prose pre, .prose blockquote { margin: 0 0 14px; }
.prose ul, .prose ol { padding-left: 22px; }
.prose table { border-collapse: collapse; font-size: 14px; }
.prose th, .prose td { padding: 7px 11px; border: 1px solid var(--border); text-align: left; vertical-align: top; }
.prose th { background: var(--surface-2); }
.prose pre { padding: 12px 14px; border-radius: var(--r); background: var(--surface-2); overflow: auto; }
.prose pre code { padding: 0; background: none; }
.prose blockquote { padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--muted); }

/* --- 8. Utilities --------------------------------------------------------------------------------------- */

.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 13px; }
.strong { font-weight: 800; }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.stack { display: grid; gap: 12px; }
.stack.loose { gap: 20px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.spacer { flex: 1; }
.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; }

@media (max-width: 960px) {
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .search-trigger { min-width: 0; }
  .search-trigger .label { display: none; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 40; transform: translateX(-100%); transition: transform 0.2s; }
  .app[data-sidebar="open"] .sidebar { transform: none; box-shadow: var(--shadow-pop); }
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
  .page { padding: 18px 16px 40px; }
  .topbar .crumbs > :not(.current) { display: none; }
  .page-header { flex-direction: column; }
}
@media (prefers-reduced-motion: reduce) {
  /* Looping animations must also stop looping, or a 0.01ms loop flickers at full speed */
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
  /* Spinners are the only sign something is loading: keep them, slowly */
  .spinner, .btn.is-loading::before { animation-duration: 2s !important; animation-iteration-count: infinite !important; }
}
