:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  /* Surface hierarchy (dark) */
  --bg: #0b0e13;
  --surface: #12161d;
  --surface-raised: #171c25;
  --surface-soft: #0e1218;
  --surface-hover: #1a202c;
  --line: #5b6b82;
  --line-strong: #718198;

  /* Text hierarchy */
  --text: #eef3f8;
  --text-strong: #ffffff;
  --muted: #96a3b6;
  --faint: #66738a;

  /* Semantic colors — reserved for meaning, never decoration */
  --accent: #5b8cff;        /* brand / actions (blue) */
  --accent-strong: #8fb3ff; /* hover / emphasis for actions */
  --accent-ink: #0b1120;    /* text on accent */
  --focus: #8fb3ff;         /* visible focus ring */

  --success: #41d08e;       /* green = success */
  --success-ink: #06281b;
  --success-wash: rgb(65 208 142 / 14%);
  --warning: #e8b04b;       /* orange = warning */
  --warning-wash: rgb(232 176 75 / 14%);
  --danger: #ff7a7a;        /* red = errors / destructive */
  --danger-wash: rgb(255 122 122 / 14%);

  --info: #5b8cff;
  --info-wash: rgb(91 140 255 / 14%);

  /* Chat taxonomy colors are paired with visible labels, never used alone. */
  --chat-group: #b49aff;
  --chat-channel: #5bc2d9;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 30%);
  --shadow-md: 0 6px 24px -8px rgb(0 0 0 / 45%);
  --shadow-lg: 0 18px 50px rgb(0 0 0 / 35%);

  /* Spacing scale (4px base, 8px rhythm) */
  --sp-1: .25rem;
  --sp-2: .5rem;
  --sp-3: .75rem;
  --sp-4: 1rem;
  --sp-5: 1.25rem;
  --sp-6: 1.5rem;
  --sp-8: 2rem;
  --sp-10: 2.5rem;
  --sp-12: 3rem;
  --sp-16: 4rem;

  /* Radii */
  --r-sm: .375rem;
  --r-md: .5rem;
  --r-lg: .75rem;
  --r-xl: 1rem;
  --r-pill: 999px;

  /* Timing */
  --t-fast: 160ms;
  --t-mid: 180ms;

  --ease: cubic-bezier(.4, 0, .2, 1);
}

/* Light mode keeps the same cool, restrained language while reversing the
   contrast roles and using darker semantic accents for readable text. */
html[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-raised: #f8fafc;
  --surface-soft: #eef2f6;
  --surface-hover: #eef2f6;
  --line: #7a899e;
  --line-strong: #5e6d82;
  --text: #111827;
  --text-strong: #111827;
  --muted: #526176;
  --faint: #64748b;
  --accent: #315fcc;
  --accent-strong: #244db5;
  --accent-ink: #ffffff;
  --focus: #244db5;
  --success: #137a4a;
  --success-ink: #ffffff;
  --success-wash: rgb(19 122 74 / 14%);
  --warning: #9a5b00;
  --warning-wash: rgb(154 91 0 / 14%);
  --danger: #b42318;
  --danger-wash: rgb(180 35 24 / 14%);
  --info: #315fcc;
  --info-wash: rgb(49 95 204 / 14%);
  --chat-group: #6941c6;
  --chat-channel: #006b7a;
  --shadow-sm: 0 1px 2px rgb(15 23 42 / 12%);
  --shadow-md: 0 6px 24px -8px rgb(15 23 42 / 18%);
  --shadow-lg: 0 18px 50px rgb(15 23 42 / 16%);
}

* { box-sizing: border-box; }

html { min-width: 320px; -webkit-text-size-adjust: 100%; }

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-strong); }

button, input, select, textarea { font: inherit; }

button { cursor: pointer; }

button:disabled, .button:disabled, [disabled] { cursor: not-allowed; opacity: .55; }

/* Consistent, always-visible focus states */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ----------------------------- Typography ----------------------------- */

h1, h2, h3, h4, p { margin-top: 0; }

h1 {
  margin-bottom: .5rem;
  font-size: clamp(1.75rem, 3vw, 2.4rem);
  font-weight: 750;
  letter-spacing: -.03em;
  line-height: 1.15;
  color: var(--text-strong);
}

h2 {
  margin-bottom: .3rem;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--text-strong);
}

h3 { margin-bottom: .35rem; font-size: .95rem; font-weight: 650; }

.muted { color: var(--muted); }
.faint { color: var(--faint); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.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;
}

.is-hidden { display: none !important; }

/* ----------------------------- Layout ----------------------------- */

main {
  width: min(1440px, calc(100% - 4rem));
  margin: 0 auto;
  padding: var(--sp-8) 0 var(--sp-16);
}

.page-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: var(--sp-6);
  margin-bottom: var(--sp-6);
}

.page-description {
  max-width: 46rem;
  margin-bottom: 0;
  color: var(--muted);
}

.eyebrow {
  margin-bottom: var(--sp-2);
  color: var(--accent-strong);
  font-size: .72rem;
  font-weight: 750;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.page-actions, .page-actions form { display: flex; align-items: center; gap: var(--sp-3); }

/* ----------------------------- Buttons ----------------------------- */

.button, button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 2.5rem;
  padding: .5rem .9rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--surface-raised);
  color: var(--text);
  font-size: .875rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease), transform var(--t-fast) var(--ease);
}

.button:hover:not(:disabled), button:hover:not(:disabled) {
  border-color: var(--line-strong);
  background: var(--surface-hover);
  color: var(--text-strong);
}

.button:active:not(:disabled), button:active:not(:disabled) { transform: translateY(1px); }

/* Primary: Fetch — the single dominant action on a card */
.button-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  box-shadow: var(--shadow-sm);
}
.button-primary:hover:not(:disabled) { border-color: transparent; background: var(--accent-strong); color: var(--accent-ink); }

/* Success: Download — file is already staged locally, the action completes
   the user goal. Green ties the affordance to the success state. */
.button-success {
  border-color: transparent;
  background: var(--success);
  color: var(--success-ink);
  font-weight: 700;
  box-shadow: var(--shadow-sm);
}
.button-success:hover:not(:disabled) {
  border-color: transparent;
  background: color-mix(in srgb, var(--success) 88%, white 12%);
  color: var(--success-ink);
}

/* Secondary: Apply Filters */
.button-secondary {
  border-color: var(--line-strong);
  background: var(--surface);
  color: var(--accent-strong);
}
.button-secondary:hover:not(:disabled) { border-color: var(--accent); background: var(--info-wash); color: var(--accent-strong); }

/* Tertiary: neutral / quiet */
.button-quiet { background: transparent; color: var(--muted); }
.button-quiet:hover:not(:disabled) { background: var(--surface-raised); color: var(--text); }

/* Danger: destructive */
.button-danger {
  border-color: var(--danger-wash);
  background: var(--danger-wash);
  color: var(--danger);
}
.button-danger:hover:not(:disabled) {
  border-color: var(--danger);
  background: rgb(255 122 122 / 22%);
  color: #ffa3a3;
}

html[data-theme="light"] .button-danger:hover:not(:disabled) {
  background: var(--danger-wash);
  color: var(--danger);
}

.button-sm { min-height: 2.05rem; padding: .3rem .65rem; font-size: .8rem; border-radius: var(--r-sm); }
.button-disabled { pointer-events: none; opacity: .55; }

.text-link { font-size: .875rem; font-weight: 600; }

/* ----------------------------- Forms ----------------------------- */

form { display: block; }

/* Form labels: column layout wrapping the input. The `.field` class is the
   explicit variant used by new markup; bare `label` keeps the same default so
   legacy forms (sources, services, routing rules, telegram auth) stay aligned. */
label, label.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  color: var(--muted);
  font-size: .78rem;
  font-weight: 650;
}

/* Horizontal checkbox fields override the column default */
label.checkbox-field { flex-direction: row; align-items: center; gap: .5rem; }

/* Inline helper text shown beneath a form field */
.field-hint { color: var(--muted); font-weight: 400; font-size: .75rem; line-height: 1.35; }

input[type="text"], input[type="search"], input[type="password"], input[type="email"], select, textarea {
  min-width: 0;
  width: 100%;
  min-height: 2.5rem;
  padding: .55rem .8rem;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-soft);
  color: var(--text);
  font-size: .9rem;
  transition: border-color var(--t-fast) var(--ease), background var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--info-wash);
}

input::placeholder { color: var(--faint); }

/* Checkbox — larger hit area for accessibility */
input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ----------------------------- App shell / Sidebar ----------------------------- */

.app-shell {
  --sidebar-w: 15.5rem;
  min-height: 100vh;
  display: flex;
}

/* Fixed vertical left sidebar */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-w);
  border-right: 1px solid var(--line);
  background: var(--surface);
  transition: width var(--t-mid) var(--ease);
}

.sidebar-brand {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 4rem;
  padding: 0 var(--sp-4);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: .6rem;
  color: var(--text-strong);
  font-size: .95rem;
  font-weight: 700;
  white-space: nowrap;
}
.brand:hover { color: var(--text-strong); }

.brand-mark {
  display: grid;
  place-items: center;
  width: 1.9rem;
  height: 1.9rem;
  border-radius: .55rem;
  background: linear-gradient(135deg, var(--accent), #7a5bff);
  color: #fff;
  font-size: .85rem;
  font-weight: 800;
  box-shadow: var(--shadow-sm);
}

/* Collapse toggle: small floating circle on the sidebar's right border */
.sidebar-collapse {
  position: absolute;
  top: 1.2rem;
  right: -.85rem;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  padding: 0;
  box-sizing: border-box;
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: var(--surface-raised);
  color: var(--muted);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  line-height: 0;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.sidebar-collapse:hover {
  background: var(--surface-hover);
  color: var(--text);
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}
.sidebar-collapse-ico {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  line-height: 1;
  color: inherit;
  transition: transform var(--t-mid) var(--ease);
}

/* Collapsed sidebar: narrow icon rail */
.app-shell.is-collapsed { --sidebar-w: 4rem; }
.app-shell.is-collapsed .brand strong,
.app-shell.is-collapsed .nav-group-label,
.app-shell.is-collapsed .nav-label,
.app-shell.is-collapsed .user-identity,
.app-shell.is-collapsed .sidebar-footer .signout { display: none; }
.app-shell.is-collapsed .sidebar-brand { padding: 0 .5rem; justify-content: center; }
.app-shell.is-collapsed .sidebar-brand .brand { justify-content: center; }
.app-shell.is-collapsed .sidebar-collapse-ico { transform: rotate(180deg); }
.app-shell.is-collapsed .nav-main { padding: var(--sp-4) .5rem; align-items: center; }
.app-shell.is-collapsed .nav-main a { justify-content: center; padding: .45rem .5rem; width: 100%; }
.app-shell.is-collapsed .sidebar-footer { justify-content: center; padding: var(--sp-3) .5rem; }
.app-shell.is-collapsed .theme-toggle { justify-content: center; padding-inline: .5rem; }
.app-shell.is-collapsed .theme-toggle-label { display: none; }
.app-shell.is-collapsed .sidebar-account { justify-content: center; }
.app-shell.is-collapsed .sidebar-footer .user-chip { padding: .25rem; border: 0; background: transparent; }
.app-shell.is-collapsed .sidebar-footer .inline-form { display: none; }

/* Vertical primary navigation */
.nav-main {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  flex: 1;
  padding: var(--sp-4) var(--sp-3);
  overflow-y: auto;
}

.nav-group-label {
  margin: var(--sp-2) var(--sp-2) .4rem;
  color: var(--faint);
  font-size: .68rem;
  font-weight: 750;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.nav-main a {
  display: flex;
  align-items: center;
  gap: .65rem;
  min-height: 2.4rem;
  padding: .45rem .75rem;
  border-radius: var(--r-md);
  border: 1px solid transparent;
  color: var(--muted);
  font-size: .875rem;
  font-weight: 600;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}

.nav-main a:hover { background: var(--surface-raised); color: var(--text); }

.nav-main a.active {
  background: var(--surface-hover);
  border-color: var(--line);
  color: var(--text-strong);
}

.nav-main a.active .nav-ico { color: var(--accent-strong); }

.nav-ico {
  display: grid;
  place-items: center;
  width: 1.4rem;
  height: 1.4rem;
  flex: 0 0 auto;
  color: var(--faint);
  font-size: .95rem;
  line-height: 1;
}

.nav-ico svg { width: 1.15rem; height: 1.15rem; }

/* Sidebar footer: user identity pinned at the bottom */
.sidebar-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-3);
  border-top: 1px solid var(--line);
}

.theme-toggle {
  justify-content: flex-start;
  width: 100%;
  min-height: 2.75rem;
  padding: .5rem .75rem;
  border-color: transparent;
}
.theme-toggle:hover:not(:disabled) { border-color: var(--line); }
.theme-toggle-icon { display: inline-flex; width: 1.25rem; height: 1.25rem; flex: 0 0 auto; }
.theme-toggle-icon svg { width: 100%; height: 100%; }
.theme-icon-moon { display: none; }
html[data-theme="light"] .theme-icon-sun { display: none; }
html[data-theme="light"] .theme-icon-moon { display: inline-flex; }

.sidebar-account { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); width: 100%; }
.sidebar-footer .inline-form { display: inline-flex; align-items: center; }

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: .65rem;
  min-height: 2.35rem;
  min-width: 0;
  padding: .25rem .6rem .25rem .3rem;
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  background: var(--surface-raised);
}

.avatar {
  display: grid;
  place-items: center;
  width: 1.85rem;
  height: 1.85rem;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #7a5bff);
  color: #fff;
  font-size: .82rem;
  font-weight: 800;
}

.user-identity { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.user-name { max-width: 8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .83rem; font-weight: 650; color: var(--text); }
.user-meta { display: flex; align-items: center; gap: .3rem; }
.role-pill { padding: .1rem .45rem; border: 1px solid var(--line-strong); border-radius: var(--r-pill); font-size: .6rem; font-weight: 750; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.role-pill.role-admin { border-color: var(--info-wash); background: var(--info-wash); color: var(--accent-strong); }
.signout { min-height: 0; padding: .4rem .55rem; font-size: .8rem; flex: 0 0 auto; }

/* Main content sits beside the fixed sidebar */
.app-main {
  flex: 1;
  min-width: 0;
  margin-left: var(--sidebar-w);
  transition: margin-left var(--t-mid) var(--ease);
}

/* Mobile nav toggle (hidden on desktop) */
.nav-toggle {
  display: none;
  align-items: center;
  gap: .5rem;
  min-height: 2.5rem;
  margin: var(--sp-3) var(--sp-4) 0 auto;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--r-md);
  padding: .45rem .8rem;
  font-size: .85rem;
  font-weight: 600;
}

.nav-toggle-bar {
  width: 1rem;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
  box-shadow: 0 -4px 0 currentColor, 0 4px 0 currentColor;
}

/* Backdrop behind the sidebar when open as a mobile drawer */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 45;
  border: 0;
  background: rgb(0 0 0 / 55%);
  backdrop-filter: blur(2px);
}

/* ----------------------------- Notices / Toasts ----------------------------- */

.notice {
  margin-bottom: var(--sp-4);
  padding: .75rem 1rem;
  border: 1px solid var(--success-wash);
  border-radius: var(--r-lg);
  background: var(--success-wash);
  color: #bdf4d8;
  font-size: .875rem;
}

.notice-error {
  border-color: var(--danger-wash);
  background: var(--danger-wash);
  color: #ffb3b3;
}

/* Toast system */
.toast-region {
  position: fixed;
  right: var(--sp-4);
  bottom: var(--sp-4);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-width: 18rem;
  max-width: 26rem;
  padding: .75rem 1rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  background: var(--surface-raised);
  color: var(--text);
  font-size: .875rem;
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toast-in .22s var(--ease), toast-out .2s var(--ease) forwards;
}

.toast.is-leaving { animation: toast-out .2s var(--ease) forwards; }

.toast-dot { flex: 0 0 auto; width: .5rem; height: .5rem; border-radius: 50%; }
.toast-success .toast-dot { background: var(--success); }
.toast-warning .toast-dot { background: var(--warning); }
.toast-error .toast-dot { background: var(--danger); }
.toast-info .toast-dot { background: var(--accent); }

@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(.97); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px) scale(.97); } }

/* ----------------------------- Metrics / Dashboard ----------------------------- */

.metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--sp-3);
  margin: var(--sp-10) 0 var(--sp-6);
}

.metric {
  position: relative;
  overflow: hidden;
  padding: var(--sp-4);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--t-fast) var(--ease), transform var(--t-fast) var(--ease);
}

.metric:hover { border-color: var(--line-strong); transform: translateY(-1px); }

.metric-label {
  display: flex;
  align-items: center;
  gap: .4rem;
  margin-bottom: var(--sp-3);
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
}

.metric-value {
  display: block;
  margin-bottom: .2rem;
  font-size: 1.9rem;
  font-weight: 800;
  letter-spacing: -.04em;
  line-height: 1;
}

.metric-hint { display: block; color: var(--faint); font-size: .75rem; }

.metric:not(.metric-bare)::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  border-radius: var(--r-lg) var(--r-lg) 0 0;
}

.metric-total::before { background: var(--line-strong); }
.metric-total .metric-value { color: var(--text-strong); }

.metric-warning::before { background: var(--warning); }
.metric-warning .metric-value { color: var(--warning); }

.metric-active::before { background: var(--accent); }
.metric-active .metric-value { color: var(--accent-strong); }

.metric-danger::before { background: var(--danger); }
.metric-danger .metric-value { color: var(--danger); }

.metric-success::before { background: var(--success); }
.metric-success .metric-value { color: var(--success); }

/* Make failures actionable: subtle attention */
.metric-danger.metric--needs-attention {
  border-color: var(--danger-wash);
  background: linear-gradient(180deg, rgb(255 122 122 / 8%), var(--surface));
}
.metric-danger.metric--needs-attention .metric-label::after {
  content: "!";
  display: grid;
  place-items: center;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: .65rem;
  font-weight: 800;
}

/* ----------------------------- Panels / Filters ----------------------------- */

.panel {
  margin-bottom: var(--sp-6);
  padding: var(--sp-5);
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.section-heading, .list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-4);
}

.section-heading { margin-bottom: var(--sp-4); }
.section-heading h2 { margin-bottom: 0; }
.section-heading .muted { margin-bottom: 0; font-size: .84rem; }

.filter-panel { padding: var(--sp-5); }

.filters { display: block; }
.filters label.field { min-width: 0; }

/* Full-width search row: search input grows, Search button on the right */
.search-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}

.search-row .field-search { flex: 1; min-width: 0; }

.search-row .field-search input {
  min-height: 2.75rem;
  font-size: .95rem;
  padding-left: 1rem;
}

.search-row .button-primary {
  flex: 0 0 auto;
  min-height: 2.75rem;
  min-width: 7rem;
  font-size: .9rem;
}

/* Filter + sort row below the search */
.filter-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr)) auto;
  gap: var(--sp-3);
  align-items: end;
}

.filter-row .reset-filters {
  align-self: end;
  min-height: 2.5rem;
  white-space: nowrap;
}

.filter-row .field select {
  min-height: 2.5rem;
}

/* ----------------------------- Results header + bulk toolbar ----------------------------- */

.library-section { margin-top: var(--sp-8); }

.list-header { align-items: center; margin-bottom: var(--sp-4); }
.list-header h2 { margin-bottom: .15rem; }
.list-header .sub { margin-bottom: 0; font-size: .82rem; color: var(--faint); }
.results-count { color: var(--muted); font-weight: 650; }

.select-page {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  min-height: 2.5rem;
  padding: .4rem .8rem;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--muted);
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  transition: border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
.select-page:hover { border-color: var(--line-strong); color: var(--text); background: var(--surface-raised); }

/* Custom-checkbox indicator */
.select-page input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 1.1rem;
  height: 1.1rem;
  margin: 0;
  flex: 0 0 auto;
  border: 1px solid var(--line-strong);
  border-radius: calc(var(--r-sm) - .1rem);
  background: var(--surface-soft);
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}
.select-page input[type="checkbox"]::before {
  content: "";
  width: .62rem;
  height: .62rem;
  border-radius: calc(var(--r-sm) - .15rem);
  background: var(--accent-ink);
  transform: scale(0);
  transition: transform var(--t-fast) var(--ease);
}
.select-page input[type="checkbox"]:checked { background: var(--accent); border-color: var(--accent); }
.select-page input[type="checkbox"]:checked::before { transform: scale(1); }
.select-page input[type="checkbox"]:indeterminate { background: var(--info-wash); border-color: var(--accent); }

/* When anything on the page is selected, surface the header control */
.select-page.has-selection { border-color: var(--accent); background: var(--info-wash); color: var(--text-strong); }

/* Sticky selection toolbar — appears only when items are selected */
.selection-toolbar {
  position: sticky;
  bottom: var(--sp-4);
  z-index: 30;
  display: none;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-4) 0 0;
  padding: .6rem .9rem;
  border: 1px solid var(--accent);
  border-radius: var(--r-xl);
  background: rgb(17 22 29 / 94%);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow-lg);
}

.selection-toolbar.is-visible { display: flex; }

.selection-toolbar .selection-count {
  margin-right: auto;
  font-size: .875rem;
  font-weight: 650;
  color: var(--text-strong);
}

.selection-toolbar .button { flex: 0 0 auto; }

/* ----------------------------- Media grid + cards ----------------------------- */

.library-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--sp-4);
}

.library-card {
  --format-color: var(--line-strong);
  --format-wash: rgb(150 163 182 / 6%);
  position: relative;
  display: flex;
  min-width: 0;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-mid) var(--ease), transform var(--t-mid) var(--ease), background var(--t-fast) var(--ease);
}

.library-card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.library-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-md);
}

.library-card-text   { --format-color: #e8b04b; --format-wash: rgb(232 176 75 / 10%); }
.library-card-audio  { --format-color: #a996ff; --format-wash: rgb(169 150 255 / 10%); }
.library-card-video  { --format-color: #5bc2d9; --format-wash: rgb(91 194 217 / 10%); }
.library-card-image  { --format-color: #41d08e; --format-wash: rgb(65 208 142 / 10%); }
.library-card-other  { --format-color: var(--line-strong); --format-wash: rgb(150 163 182 / 6%); }

.card-media {
  position: relative;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  background: radial-gradient(120% 130% at 50% 0%, var(--format-wash), var(--surface-soft) 60%);
}

.card-thumbnail, .card-thumbnail img { display: block; width: 100%; height: 100%; }
.card-thumbnail img { object-fit: cover; }

/* Checkbox anchored in the card header area */
.card-select {
  position: absolute;
  top: var(--sp-2);
  left: var(--sp-2);
  z-index: 2;
  display: grid;
  place-items: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--r-md);
  background: rgb(11 14 19 / 70%);
  backdrop-filter: blur(6px);
  opacity: 0;
  transform: scale(.95);
  transition: opacity var(--t-fast) var(--ease), transform var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}

.library-card:hover .card-select,
.library-card:focus-within .card-select,
.library-card.is-selected .card-select { opacity: 1; transform: none; }

.library-card.is-selected .card-select { background: var(--accent); }
.library-card.is-selected .card-select input { accent-color: #fff; }

/* Fallback media placeholder — file-type driven */
.file-placeholder {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  color: var(--format-color);
}

.file-placeholder .file-glyph {
  display: grid;
  place-items: center;
  width: 3.5rem;
  height: 3.5rem;
  border: 1px solid var(--format-wash);
  border-radius: var(--r-lg);
  background: var(--surface-raised);
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: .02em;
  box-shadow: var(--shadow-sm);
}

.card-content {
  display: flex;
  min-width: 0;
  flex: 1;
  flex-direction: column;
  padding: var(--sp-3) var(--sp-4) var(--sp-2);
}

.card-heading {
  display: flex;
  align-items: start;
  gap: var(--sp-2);
  min-width: 0;
}

.card-title-wrap { min-width: 0; flex: 1; }
.card-title-wrap h3 { margin-bottom: .15rem; }

.card-title-wrap h3 a {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-strong);
  font-size: .95rem;
  font-weight: 650;
}
.card-title-wrap h3 a:hover { color: var(--accent-strong); }

.original-title {
  margin: 0;
  overflow: hidden;
  color: var(--faint);
  font-size: .76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.format-badge {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  padding: .2rem .5rem;
  border: 1px solid var(--format-color);
  border-radius: var(--r-pill);
  background: var(--format-wash);
  color: var(--format-color);
  font-size: .66rem;
  font-weight: 750;
  letter-spacing: .05em;
  text-transform: uppercase;
}

.duplicate-badge {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  margin-top: var(--sp-2);
  padding: .2rem .5rem;
  border-radius: var(--r-pill);
  background: var(--warning-wash);
  color: var(--warning);
  font-size: .68rem;
  font-weight: 650;
}
.duplicate-badge::before { content: "⚠"; font-size: .72rem; }

.item-meta {
  display: flex;
  align-items: center;
  gap: .35rem;
  flex-wrap: wrap;
  margin-top: var(--sp-2);
  color: var(--muted);
  font-size: .76rem;
}

.item-meta .sep { color: var(--line-strong); }
.item-meta a.telegram-link { font-size: .76rem; font-weight: 600; }

.item-state {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
  margin-top: var(--sp-2);
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  min-height: 1.5rem;
  padding: .15rem .55rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-pill);
  background: var(--surface-raised);
  font-size: .72rem;
  font-weight: 650;
  white-space: nowrap;
}

.status-badge::before { content: ""; width: .45rem; height: .45rem; border-radius: 50%; background: currentColor; }

.status-discovered { border-color: var(--warning-wash); background: var(--warning-wash); color: var(--warning); }
.status-discovered::before { box-shadow: 0 0 0 0 var(--warning); animation: pulse-dot 2s infinite; }
.status-queued, .status-downloading, .status-uploading, .status-verifying, .status-running { border-color: var(--info-wash); background: var(--info-wash); color: var(--accent-strong); }
.status-queued::before, .status-downloading::before, .status-uploading::before { animation: pulse-dot 1.6s infinite; }
.status-downloaded, .status-imported, .status-succeeded { border-color: var(--success-wash); background: var(--success-wash); color: var(--success); }
.status-failed { border-color: var(--danger-wash); background: var(--danger-wash); color: var(--danger); }

@keyframes pulse-dot {
  0% { box-shadow: 0 0 0 0 currentColor; opacity: 1; }
  70% { box-shadow: 0 0 0 .35rem transparent; }
  100% { box-shadow: 0 0 0 0 transparent; opacity: .6; }
}

.job-state { color: var(--muted); font-size: .78rem; }

.item-progress { flex: 1; min-width: 5rem; }
.item-progress .progress { height: .4rem; min-width: 0; }

.error-details { margin-top: var(--sp-2); color: var(--danger); font-size: .8rem; }
.error-details summary { cursor: pointer; color: var(--danger); font-weight: 600; }
.error-details p { max-width: 70ch; margin: var(--sp-2) 0 0; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); }

.remote-links { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; margin-top: var(--sp-2); }
.remote-links a { font-size: .78rem; font-weight: 600; }

/* Card footer action area */
.card-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-top: auto;
  padding: var(--sp-3) var(--sp-4) var(--sp-4);
}

.card-actions form.item-fetch-form { display: contents; }

.card-actions .button, .card-actions button { flex: 1; }

/* De-emphasize Fetch until hover/selection */
.card-actions .button-primary.fetch {
  border-color: transparent;
  background: var(--surface-raised);
  color: var(--text);
  box-shadow: none;
}
.card-actions .button-primary.fetch:hover,
.library-card:hover .card-actions .button-primary.fetch,
.library-card:focus-within .card-actions .button-primary.fetch,
.card-actions .button-primary.fetch:focus-visible {
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: var(--shadow-sm);
}

.fetch-feedback { margin: var(--sp-2) 0 0; color: var(--accent-strong); font-size: .8rem; }
.fetch-feedback-error { color: var(--danger); }

/* ----------------------------- Skeleton loading ----------------------------- */

.skeleton {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  background: var(--surface);
}
.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, rgb(255 255 255 / 4%), transparent);
  animation: shimmer 1.6s infinite;
}
.skeleton-media { aspect-ratio: 1 / 1; background: var(--surface-soft); }
.skeleton-line { height: .85rem; margin: var(--sp-3) var(--sp-4) 0; border-radius: var(--r-sm); background: var(--surface-soft); }
.skeleton-line.short { width: 55%; }
.skeleton-line.mid { width: 78%; }

@keyframes shimmer { 100% { transform: translateX(100%); } }

/* ----------------------------- Empty state ----------------------------- */

.empty-state {
  display: grid;
  justify-items: center;
  padding: var(--sp-16) var(--sp-4);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-xl);
  background: var(--surface);
  text-align: center;
}

.empty-state h3 { font-size: 1.1rem; font-weight: 700; color: var(--text-strong); }
.empty-state p { max-width: 34rem; color: var(--muted); font-size: .9rem; }
.empty-state .button { min-width: 10rem; }

.empty-icon {
  display: grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  margin-bottom: var(--sp-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-xl);
  background: var(--surface-raised);
  color: var(--accent);
  font-size: 1.4rem;
  font-weight: 700;
}

/* ----------------------------- Pagination ----------------------------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  margin: var(--sp-8) 0 var(--sp-10);
  color: var(--muted);
  font-size: .875rem;
}
.pagination .button { min-width: 6.5rem; }

/* ----------------------------- Detail / misc pages ----------------------------- */

.toolbar { margin-bottom: var(--sp-4); padding: var(--sp-4); border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--surface); }

.detail-header { align-items: start; }
.detail-header h1 { max-width: 50rem; overflow-wrap: anywhere; }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-4); }
.detail-wide { grid-column: 1 / -1; }
.metadata-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-4); margin: 0; }
.metadata-list div { padding-bottom: var(--sp-3); border-bottom: 1px solid var(--line); }
.metadata-list dt { margin-bottom: .25rem; color: var(--muted); font-size: .78rem; }
.metadata-list dd { margin: 0; }

.file-list, .remote-list, .job-list { display: grid; gap: .5rem; margin: 0; padding: 0; list-style: none; }
.file-list li, .remote-list li, .job-list li { display: flex; align-items: center; gap: .7rem; min-width: 0; padding: .7rem 0; border-bottom: 1px solid var(--line); }
.file-list li:last-child, .remote-list li:last-child, .job-list li:last-child { border-bottom: 0; }

.routing-rule-list { display: grid; gap: .5rem; margin: 0; padding: 0; list-style: none; }
.routing-rule-row { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-4); padding: .8rem 0; border-bottom: 1px solid var(--line); }
.routing-rule-row:last-child { border-bottom: 0; }
.routing-rule-row p { margin: .25rem 0 0; font-size: .8rem; }

.file-list li span:nth-child(2) { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.file-list li span:last-child, .remote-list li span:last-child { margin-left: auto; white-space: nowrap; }
.job-list li { align-items: start; justify-content: space-between; flex-wrap: wrap; }
.job-list li > div { display: flex; align-items: center; gap: .6rem; }

.queue-row { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-6); }
.queue-retry-form { flex: 0 0 auto; }
.queue-content { min-width: 0; }
.queue-content h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queue-content p { display: flex; align-items: center; gap: .6rem; margin-bottom: 0; }

.progress { position: relative; min-width: 13rem; height: 1rem; overflow: hidden; border: 1px solid var(--line-strong); border-radius: var(--r-pill); background: var(--surface-soft); }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: var(--r-pill); transition: width .25s var(--ease); }
.progress small { position: absolute; inset: .14rem 0 0; color: var(--text); text-align: center; font-size: .68rem; }
.progress-indeterminate span { width: 35%; animation: progress-sweep 1.4s ease-in-out infinite; }
.progress.is-small { min-width: 0; height: .4rem; }
.progress.is-small small { display: none; }

@keyframes progress-sweep { from { transform: translateX(-100%); } to { transform: translateX(285%); } }

pre { max-width: 70rem; color: var(--danger); white-space: pre-wrap; overflow-wrap: anywhere; }
.error { color: var(--danger); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--sp-3); }
.form-grid button { width: max-content; }
.form-actions { display: flex; align-items: center; gap: var(--sp-3); grid-column: 1 / -1; }

.format-fieldset { position: relative; display: block; grid-column: 1 / -1; min-width: 0; margin: 0; padding: .75rem 1rem; border: 1px solid var(--line); border-radius: var(--r-lg); }
.format-fieldset > legend { padding: 0 .35rem; color: var(--text); font-weight: 750; }

.format-menu { position: relative; }
.format-menu-summary { display: flex; align-items: center; justify-content: space-between; gap: .75rem; min-height: 2.5rem; padding: .6rem .75rem; border: 1px solid var(--line-strong); border-radius: var(--r-md); background: var(--surface-soft); color: var(--text); cursor: pointer; list-style: none; }
.format-menu-summary::-webkit-details-marker { display: none; }
.format-menu-summary::after { content: ""; width: .5rem; height: .5rem; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg) translateY(-2px); }
.format-menu[open] .format-menu-summary::after { transform: rotate(225deg) translate(-2px, -2px); }
.format-menu-summary:hover { border-color: var(--accent); }
.format-menu-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.format-menu-panel { position: absolute; top: calc(100% + .4rem); left: 0; z-index: 20; display: grid; gap: .6rem; width: min(100%, 48rem); max-height: min(30rem, calc(100vh - 10rem)); overflow-y: auto; padding: .75rem; border: 1px solid var(--line-strong); border-radius: var(--r-lg); background: var(--surface-raised); box-shadow: var(--shadow-lg); }
.format-menu-panel > .muted { margin: 0; font-size: .82rem; }
.format-group { min-width: 0; margin: 0; padding: .55rem 0 0; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface-soft); }
.format-group legend { max-width: 100%; padding: 0 .5rem; }
.format-group-toggle { display: flex; flex-direction: row; align-items: center; gap: .55rem; color: var(--text); font-size: .85rem; font-weight: 750; }
.format-group-toggle input, .format-extension input { width: 1.1rem; height: 1.1rem; min-height: 0; padding: 0; accent-color: var(--accent); }
.format-extension-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr)); gap: .4rem; padding: .75rem; border-top: 1px solid var(--line); }
.format-extension { display: flex; flex-direction: row; align-items: center; gap: .45rem; min-width: 0; padding: .4rem .5rem; border: 1px solid transparent; border-radius: var(--r-sm); color: var(--muted); font-size: .8rem; }
.format-extension:hover { border-color: var(--line); background: var(--surface); color: var(--text); }
.format-group.is-disabled .format-extension-list { opacity: .5; }

.checkbox-field { flex-direction: row; align-items: center; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: var(--sp-3); }
.card { padding: var(--sp-4); border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--surface); }
.cards .card { display: flex; flex-direction: column; justify-content: space-between; gap: var(--sp-4); }
.card h3 { margin-bottom: .35rem; }
.card p { margin: .25rem 0; color: var(--muted); }

.breadcrumb { margin-bottom: var(--sp-6); color: var(--muted); font-size: .9rem; }
.breadcrumb span { margin: 0 .5rem; color: var(--line-strong); }

/* ----------------------------- Supporting pages ----------------------------- */

.page-header-stat {
  display: grid;
  justify-items: end;
  gap: .15rem;
  min-width: 6rem;
  color: var(--faint);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.page-header-stat strong {
  color: var(--text-strong);
  font-size: 1.8rem;
  letter-spacing: -.04em;
  line-height: 1;
}

.form-panel { padding: var(--sp-6); }
.form-panel > .section-heading { margin-bottom: var(--sp-5); }
.form-panel .form-grid { padding: 0; }

/* Creation happens in one calm, reusable modal instead of taking over the
   page. Results remain visible underneath so the user keeps their context. */
body.modal-open { overflow: hidden; }

dialog.modal {
  width: min(calc(100% - 2rem), 56rem);
  max-width: 56rem;
  max-height: calc(100vh - var(--sp-8));
  margin: auto;
  padding: 0;
  border: 0;
  border-radius: var(--r-xl);
  background: transparent;
  color: var(--text);
  overflow: visible;
}

dialog.modal::backdrop { background: rgb(3 5 9 / 74%); backdrop-filter: blur(4px); }
.modal-card { max-height: calc(100vh - var(--sp-8)); overflow-y: auto; border: 1px solid var(--line-strong); border-radius: var(--r-xl); background: var(--surface); box-shadow: var(--shadow-lg); }
.modal-header { display: flex; align-items: start; justify-content: space-between; gap: var(--sp-6); padding: var(--sp-6) var(--sp-6) var(--sp-4); }
.modal-header h2 { margin-bottom: .25rem; color: var(--text-strong); font-size: 1.25rem; }
.modal-header p:last-child { margin-bottom: 0; font-size: .82rem; }
.modal-close { width: 2.5rem; min-width: 2.5rem; padding: 0; font-size: 1rem; text-transform: uppercase; }
.modal-form { padding: 0 var(--sp-6) var(--sp-6); }
.modal-form > label { min-width: 0; }
.modal-form .format-fieldset,
.modal-form .advanced-settings,
.modal-form .form-actions { grid-column: 1 / -1; }
.modal-form .form-actions { justify-content: flex-end; margin-top: 0; }
.modal-form .form-actions .button { min-width: 7rem; }
.button-add { flex: 0 0 auto; }

.advanced-settings {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface-soft);
}

.advanced-settings > summary { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); color: var(--text); font-size: .85rem; font-weight: 700; cursor: pointer; list-style: none; }
.advanced-settings > summary::-webkit-details-marker { display: none; }
.advanced-settings > summary::after { content: ""; width: .5rem; height: .5rem; flex: 0 0 auto; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg) translateY(-2px); }
.advanced-settings[open] > summary::after { transform: rotate(225deg) translate(-2px, -2px); }
.advanced-settings-body { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-4); padding: var(--sp-4); border-top: 1px solid var(--line); }
.advanced-settings-body .checkbox-field { align-self: end; min-height: 2.5rem; }

.routing-format-fieldset { min-height: 0; }
.routing-format-panel { gap: var(--sp-4); }
.routing-format-options { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: var(--sp-2); }
.routing-format-option { min-height: 2.5rem; border-color: transparent; background: var(--surface); }
.routing-format-option:has(input:checked) { border-color: var(--accent); background: var(--info-wash); color: var(--text); }
.routing-format-option input[type="radio"] { width: 1.1rem; height: 1.1rem; min-height: 0; margin: 0; accent-color: var(--accent); }
.routing-format-panel .field { color: var(--muted); }

.form-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-5);
  align-items: start;
}

.form-grid > .form-actions,
.form-grid > button { grid-column: 1 / -1; }
.form-grid > button { justify-self: start; }
.form-grid .checkbox-field { align-self: end; min-height: 2.5rem; }
.form-grid .field-hint code { color: var(--text); }

.form-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin-top: var(--sp-2);
}

.resource-section,
.users-section { margin-top: var(--sp-8); }

.resource-section > .section-heading,
.users-section > .section-heading { margin-bottom: var(--sp-4); }

.resource-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr));
  gap: var(--sp-4);
}

.card.resource-card {
  display: flex;
  min-width: 0;
  flex-direction: column;
  justify-content: space-between;
  gap: var(--sp-5);
  min-height: 13rem;
  padding: var(--sp-5);
  border-color: transparent;
  box-shadow: var(--shadow-sm);
  transition: background var(--t-fast) var(--ease), box-shadow var(--t-mid) var(--ease), transform var(--t-mid) var(--ease);
}

.resource-card:hover {
  background: var(--surface-raised);
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.resource-card-disabled { opacity: .72; }
.resource-card > div:first-child { min-width: 0; }

.resource-card-heading {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: var(--sp-3);
  min-width: 0;
}

.resource-card-heading h3 {
  min-width: 0;
  margin-bottom: 0;
  overflow: hidden;
  color: var(--text-strong);
  font-size: 1.05rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.resource-card p { margin: var(--sp-2) 0 0; color: var(--muted); font-size: .82rem; }
.resource-card .resource-primary { color: var(--text); font-size: .9rem; font-weight: 600; overflow-wrap: anywhere; }
.resource-card .format-summary { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

.format-pill {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: .25rem .55rem;
  border-radius: var(--r-pill);
  background: var(--surface-raised);
  color: var(--muted);
  font-size: .7rem;
  font-weight: 650;
}

.index-history { margin-top: var(--sp-4); }
.index-history-heading { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); color: var(--muted); font-size: .72rem; font-weight: 650; }
.index-history-legend { display: inline-flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; font-size: .66rem; font-weight: 600; }
.index-history-legend > span { display: inline-flex; align-items: center; gap: var(--sp-1); }
.index-history-legend-swatch { display: inline-block; width: .45rem; height: .45rem; border-radius: var(--r-sm); }
.index-history-legend-ok { background: var(--success); }
.index-history-legend-error { background: var(--danger); }
.index-history-legend-active { border: 1px solid var(--line-strong); background: var(--surface-soft); }
.index-history-bars { display: grid; grid-template-columns: repeat(20, minmax(0, 1fr)); align-items: stretch; gap: var(--sp-1); margin-top: var(--sp-2); min-height: 1.5rem; }
.index-history-bar { min-width: 0; border: 1px solid var(--line-strong); border-radius: var(--r-sm); background: var(--surface-soft); cursor: help; outline: none; transition: transform var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease); }
.index-history-bar:hover, .index-history-bar:focus-visible { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.index-history-succeeded { border-color: var(--success); background: var(--success); }
.index-history-failed { border-color: var(--danger); background: var(--danger); }
.index-history-active { border-color: var(--line-strong); background: var(--surface-soft); }
.index-history-empty { margin-top: var(--sp-2) !important; font-size: .76rem !important; }

.resource-card .status { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.resource-card .status-badge { flex: 0 0 auto; }

.resource-card .card-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--sp-2);
  margin-top: auto;
  padding: var(--sp-3) 0 0;
}

.resource-card .card-actions form { display: contents; }
.resource-card .card-actions .button,
.resource-card .card-actions button { flex: 0 0 auto; }

.more-actions { position: relative; }
.more-actions > summary { list-style: none; cursor: pointer; }
.more-actions > summary::-webkit-details-marker { display: none; }

.more-actions-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + var(--sp-2));
  z-index: 10;
  display: grid;
  gap: var(--sp-1);
  min-width: 12rem;
  padding: var(--sp-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  background: var(--surface-raised);
  box-shadow: var(--shadow-lg);
}

.more-actions-menu form { display: block !important; }
.more-actions-menu .button { width: 100%; justify-content: flex-start; }

.chat-filter { margin-top: var(--sp-4); padding: var(--sp-4); }
.chat-filter .search-row { margin-bottom: 0; }
.chat-filter .field-search { flex: 1 1 20rem; }
.chat-type-filter { flex: 0 0 13rem; }
.chat-card { min-height: 12rem; }

.chat-kind-text { font-weight: 700; }
.chat-kind-group { color: var(--chat-group); }
.chat-kind-channel { color: var(--chat-channel); }
.chat-kind-separator { color: var(--line-strong); }

.telegram-session-panel,
.auth-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-6);
}

.auth-panel { display: block; max-width: 38rem; }
.auth-panel form { display: grid; gap: var(--sp-4); margin-top: var(--sp-5); }
.auth-panel form button { justify-self: start; }
.telegram-chat-section { margin-top: var(--sp-8); }

.queue-list { display: grid; gap: var(--sp-3); }

.card.queue-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(24rem, 36rem) auto;
  align-items: center;
  gap: var(--sp-6);
  padding: var(--sp-5) var(--sp-6);
  border-color: transparent;
  box-shadow: var(--shadow-sm);
  transition: background var(--t-fast) var(--ease), box-shadow var(--t-mid) var(--ease), transform var(--t-mid) var(--ease);
}

.queue-card:hover { background: var(--surface-raised); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.queue-card .queue-content { min-width: 0; }
.queue-card .queue-content h2 { margin-bottom: var(--sp-2); }
.queue-card .queue-content > p { margin-bottom: var(--sp-2); }
.queue-card .progress { width: 100%; min-width: 0; }
.queue-card .queue-retry-form { flex: 0 0 auto; }
.job-result { margin-top: var(--sp-2); color: var(--muted); font-size: .8rem; }
.job-result summary { cursor: pointer; color: var(--accent-strong); font-weight: 600; }
.job-result p { margin: var(--sp-2) 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }

/* Identity-first account rows. The edit panel is intentionally collapsed so
   status and identity are readable before any administrative action. */
.user-list { display: grid; gap: var(--sp-3); }

.user-row {
  display: grid;
  grid-template-columns: minmax(18rem, 1.4fr) minmax(13rem, .8fr) auto;
  align-items: center;
  gap: var(--sp-6);
  padding: var(--sp-5) var(--sp-6);
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition: background var(--t-fast) var(--ease), box-shadow var(--t-mid) var(--ease), transform var(--t-mid) var(--ease);
}

.user-row:hover { background: var(--surface-raised); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.user-row-disabled { opacity: .72; }
.user-persona { display: flex; align-items: center; gap: var(--sp-4); min-width: 0; }

.user-avatar {
  display: grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  flex: 0 0 auto;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #7a5bff);
  color: #fff;
  font-size: 1.05rem;
  font-weight: 800;
  box-shadow: var(--shadow-sm);
}

.user-identity-block { min-width: 0; }
.user-identity-block h3 { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: .15rem; overflow: hidden; color: var(--text-strong); font-size: 1rem; text-overflow: ellipsis; white-space: nowrap; }
.user-identity-block > p { margin-bottom: var(--sp-2); overflow: hidden; color: var(--muted); font-size: .86rem; text-overflow: ellipsis; white-space: nowrap; }
.you-label { padding: .15rem .45rem; border-radius: var(--r-pill); background: var(--info-wash); color: var(--accent-strong); font-size: .62rem; letter-spacing: .04em; text-transform: uppercase; }

.user-facts { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; color: var(--faint); font-size: .72rem; }
.user-facts span + span::before { content: "·"; margin-right: var(--sp-2); color: var(--line-strong); }
.user-access-summary { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }

.role-pill.role-lg,
.status-badge.status-lg,
.access-badge { min-height: 1.9rem; padding: .25rem .65rem; font-size: .74rem; }

.access-badge {
  display: inline-flex;
  align-items: center;
  border-radius: var(--r-pill);
  background: var(--surface-raised);
  color: var(--muted);
  font-weight: 650;
}

.access-badge-locked { background: var(--warning-wash); color: var(--warning); }
.user-edit { justify-self: end; min-width: 8.5rem; }
.user-edit > summary { display: inline-flex; width: 100%; list-style: none; cursor: pointer; }
.user-edit > summary::-webkit-details-marker { display: none; }
.edit-icon { display: inline-flex; width: 1rem; height: 1rem; }
.edit-icon svg { width: 100%; height: 100%; }

.user-edit[open] {
  grid-column: 1 / -1;
  width: 100%;
  padding-top: var(--sp-4);
  border-top: 1px solid var(--line);
}

.user-edit[open] > summary { width: max-content; }
.user-edit-panel { display: grid; grid-template-columns: minmax(14rem, 20rem) minmax(14rem, 1fr); align-items: end; gap: var(--sp-5); margin-top: var(--sp-4); }
.user-edit-form { display: grid; grid-template-columns: minmax(10rem, 14rem) minmax(14rem, 1fr) auto; align-items: end; gap: var(--sp-4); grid-column: 1 / -1; }
.user-edit-form .pin-field { min-height: 2.5rem; }
.pin-field > span { display: grid; gap: .1rem; }
.pin-field small { color: var(--muted); font-size: .72rem; font-weight: 400; line-height: 1.35; }
.user-edit-actions { display: flex; justify-content: flex-end; }
.user-status-form { justify-self: end; }
.edit-note { margin: 0; font-size: .8rem; }
.provisioning-panel { margin-top: var(--sp-8); }
.provisioning-panel pre { margin-bottom: 0; padding: var(--sp-4); border-radius: var(--r-lg); background: var(--surface-soft); color: var(--muted); font-size: .8rem; overflow-x: auto; }

.routing-rule-row .card-actions { display: flex; align-items: center; gap: var(--sp-2); margin: 0; padding: 0; }
.routing-rule-row .card-actions .button,
.routing-rule-row .card-actions button { flex: 0 0 auto; }
.routing-rule-list { gap: var(--sp-3); }
.routing-rule-row { min-height: 4.5rem; padding: var(--sp-4) var(--sp-5); border-bottom: 0; border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow-sm); }
.routing-rule-row strong { color: var(--text-strong); font-size: .92rem; }
.routing-rule-row p { margin-bottom: 0; line-height: 1.5; }

/* ----------------------------- Login ----------------------------- */

.login-page { background: radial-gradient(circle at 50% 0%, rgb(91 140 255 / 10%), transparent 34rem), var(--bg); }
.site-header-minimal { display: flex; align-items: center; justify-content: space-between; min-height: 4.5rem; padding: 0 var(--sp-6); }
.site-header-minimal .theme-toggle { width: auto; }
.login-main { display: grid; place-items: center; width: min(100% - 2rem, 30rem); min-height: calc(100vh - 4.5rem); padding: var(--sp-8) 0 var(--sp-12); }
.login-card { width: 100%; padding: var(--sp-8); border-radius: var(--r-xl); background: var(--surface); box-shadow: var(--shadow-lg); }
.login-card h1 { margin-bottom: var(--sp-6); }
.login-card form { display: grid; gap: var(--sp-4); }
.login-card .button { width: 100%; min-height: 2.75rem; }
.button-large { min-height: 3rem !important; }
.login-divider { display: flex; align-items: center; gap: var(--sp-3); margin: var(--sp-5) 0; color: var(--faint); font-size: .78rem; text-transform: uppercase; }
.login-divider::before, .login-divider::after { content: ""; height: 1px; flex: 1; background: var(--line); }

/* ----------------------------- Responsive ----------------------------- */

@media (max-width: 1080px) {
  .filter-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .library-list { grid-template-columns: repeat(2, 1fr); }
}

/* Mid-range tablets: two cards then single column */
@media (max-width: 620px) {
  .library-list { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  /* Sidebar becomes a slide-in drawer behind a backdrop */
  .sidebar {
    transform: translateX(-100%);
    transition: transform var(--t-mid) var(--ease);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-backdrop { display: block; opacity: 0; pointer-events: none; transition: opacity var(--t-mid) var(--ease); }
  .sidebar-backdrop.is-visible { opacity: 1; pointer-events: auto; }
  .app-main { margin-left: 0; }
  .nav-toggle { display: inline-flex; }

  /* On mobile the drawer always opens expanded: undo any collapsed compaction. */
  .app-shell.is-collapsed { --sidebar-w: 15.5rem; }
  .app-shell.is-collapsed .brand strong,
  .app-shell.is-collapsed .nav-group-label,
  .app-shell.is-collapsed .nav-label,
  .app-shell.is-collapsed .user-identity,
  .app-shell.is-collapsed .sidebar-footer .signout { display: inline; }
  .app-shell.is-collapsed .user-identity { display: flex; }
  .app-shell.is-collapsed .sidebar-brand { padding: 0 var(--sp-4); justify-content: flex-start; }
  .app-shell.is-collapsed .sidebar-brand .brand { justify-content: flex-start; }
  .app-shell.is-collapsed .sidebar-collapse-ico { transform: none; }
  .app-shell.is-collapsed .nav-main { padding: var(--sp-4) var(--sp-3); align-items: stretch; }
  .app-shell.is-collapsed .nav-main a { justify-content: flex-start; padding: .45rem .75rem; width: auto; }
  .app-shell.is-collapsed .sidebar-footer { justify-content: flex-end; gap: var(--sp-3); padding: var(--sp-3) var(--sp-3); }
  .app-shell.is-collapsed .theme-toggle { justify-content: flex-start; padding-inline: .75rem; }
  .app-shell.is-collapsed .theme-toggle-label { display: inline; }
  .app-shell.is-collapsed .sidebar-account { justify-content: space-between; }
  .app-shell.is-collapsed .sidebar-footer .user-chip { padding: .25rem .6rem .25rem .3rem; border: 1px solid var(--line); background: var(--surface-raised); }
  .app-shell.is-collapsed .sidebar-footer .inline-form { display: inline-flex; }
  .sidebar-collapse { display: none; }

  main { width: calc(100% - 1.5rem); padding-top: var(--sp-4); }
  .page-header, .list-header { align-items: start; flex-direction: column; }
  .page-actions, .page-actions form { width: 100%; flex-wrap: wrap; }
  .page-actions .button, .page-actions button, .page-actions select { flex: 1; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .search-row { flex-direction: column; align-items: stretch; }
  .search-row .button-primary { width: 100%; }
  .filter-row { grid-template-columns: 1fr; }
  .filter-row .reset-filters { justify-self: stretch; }
  .selection-toolbar { flex-wrap: wrap; justify-content: flex-end; }
  .selection-toolbar .selection-count { flex-basis: 100%; margin-right: 0; }
  .library-list { grid-template-columns: 1fr; }
  .card-actions { flex-wrap: wrap; }
  .detail-grid { grid-template-columns: 1fr; }
  .detail-wide { grid-column: auto; }
  .metadata-list { grid-template-columns: 1fr; }
  .queue-row { align-items: stretch; flex-direction: column; }
  .queue-retry-form button { width: 100%; }
  .progress { width: 100%; }
}

@media (max-width: 430px) {
  .brand strong { font-size: .92rem; }
  .metrics { gap: var(--sp-2); }
  .metric { padding: var(--sp-3); }
  .metric-value { font-size: 1.5rem; }
  .format-badge { margin-top: .1rem; }
}

@media (max-width: 1080px) {
  main { width: min(100% - 3rem, 900px); }
  .user-row { grid-template-columns: minmax(0, 1fr) auto; }
  .user-edit { grid-column: 1 / -1; justify-self: start; }
  .queue-card { grid-template-columns: minmax(0, 1fr) minmax(20rem, 32rem); }
  .queue-card .queue-retry-form { grid-column: 2; justify-self: end; }
}

@media (max-width: 760px) {
  main { width: calc(100% - 1.5rem); }
  dialog.modal { width: calc(100% - 1rem); max-height: calc(100vh - var(--sp-4)); }
  .modal-card { max-height: calc(100vh - var(--sp-4)); }
  .modal-header { padding: var(--sp-5) var(--sp-5) var(--sp-4); }
  .modal-form { padding: 0 var(--sp-5) var(--sp-5); }
  .advanced-settings-body { grid-template-columns: 1fr; }
  .section-heading { align-items: start; flex-direction: column; }
  .section-heading .button-add { width: 100%; }
  .page-header-stat { align-self: flex-start; justify-items: start; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid > .form-actions,
  .form-grid > button { grid-column: auto; }
  .form-actions { flex-wrap: wrap; }
  .form-actions .button { flex: 1; }
  .resource-list { grid-template-columns: 1fr; }
  .chat-type-filter { flex: 1 1 auto; }
  .resource-card .card-actions { justify-content: stretch; flex-wrap: wrap; }
  .resource-card .card-actions > .button,
  .resource-card .card-actions > form,
  .resource-card .card-actions > details { flex: 1 1 8rem; }
  .resource-card .card-actions > form .button,
  .resource-card .card-actions > details > summary { width: 100%; }
  .more-actions-menu { right: auto; left: 0; }
  .telegram-session-panel { align-items: stretch; flex-direction: column; }
  .telegram-session-panel form button { width: 100%; }
  .queue-card { grid-template-columns: 1fr; gap: var(--sp-4); }
  .queue-card .queue-retry-form { grid-column: auto; justify-self: stretch; }
  .queue-card .queue-retry-form button { width: 100%; }
  .user-row { grid-template-columns: 1fr; align-items: start; padding: var(--sp-5); }
  .user-edit { grid-column: auto; justify-self: stretch; width: 100%; }
  .user-edit[open] { grid-column: auto; }
  .user-edit > summary,
  .user-edit[open] > summary { width: 100%; }
  .user-edit-panel { grid-template-columns: 1fr; }
  .user-edit-form { grid-template-columns: 1fr; }
  .user-edit-actions { justify-content: stretch; }
  .user-edit-actions .button,
  .user-status-form,
  .user-status-form .button { width: 100%; }
  .user-status-form { justify-self: stretch; }
  .login-card { padding: var(--sp-6); }
}

@media (max-width: 430px) {
  .page-header-stat { display: none; }
  .form-panel { padding: var(--sp-4); }
  .resource-card { padding: var(--sp-4) !important; }
  .user-persona { align-items: start; }
  .user-avatar { width: 2.5rem; height: 2.5rem; }
  .user-access-summary { gap: var(--sp-1); }
}


/* Infinite scroll sentinel + states */
.infinite-scroll-sentinel {
  display: block;
  min-height: 0;
  height: 0;
  padding: 0;
  margin: 0;
}
.infinite-scroll-end[hidden] { display: none; }
.infinite-scroll-end {
  text-align: center;
  color: var(--muted);
  font-size: .9rem;
  padding: var(--sp-6) var(--sp-4);
  margin: 0;
}
.infinite-scroll-error {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  padding: var(--sp-4);
  color: var(--danger);
}
.infinite-scroll-error .button { color: var(--danger); }
