/* =============================================================================
   Mail Aggregator — main.css
   Single stylesheet, served from /static/css/main.css.
   No inline styles anywhere; CSP forbids them (06-security.md sec 6).
   Mobile-first; system font stack; light + dark (prefers-color-scheme) themes.
   Modernised palette (zinc neutrals + indigo accent), generous spacing,
   softer radii and shadows, subtle motion. All existing class names from
   prior versions are preserved — only their visual presentation changed.
   ========================================================================== */

/* ---------- 1. Tokens (custom properties) -------------------------------- */

:root {
  /* Colors — zinc-neutral palette + indigo accent (08-frontend.md sec 5) */
  --color-bg:            #ffffff;
  --color-bg-subtle:     #fafafa;
  --color-bg-hover:      #f4f4f5;
  --color-bg-elevated:   #ffffff;
  --color-text:          #0a0a0a;
  --color-text-muted:    #52525b;
  --color-text-faint:    #a1a1aa;
  --color-border:        #e4e4e7;
  --color-border-strong: #d4d4d8;

  --color-primary:       #4f46e5;
  --color-primary-hover: #4338ca;
  --color-primary-soft:  #eef2ff;
  --color-primary-fg:    #ffffff;
  --color-primary-ring:  rgba(79, 70, 229, 0.18);

  /* Combobox listbox surface/foreground — defined as a *matched pair* so the
     dropdown can never end up with a background from one scheme and text from
     another (the dark-on-dark / light-on-light contrast bug). Light theme:
     white surface + near-black text. Overridden together in the dark-scheme
     and body.tg-app blocks below — always both, never one. */
  --combobox-surface:    #ffffff;
  --combobox-fg:         #0a0a0a;
  --combobox-hover-bg:   #eef2ff;
  --combobox-border:     #d4d4d8;

  --color-danger:        #dc2626;
  --color-danger-hover:  #b91c1c;
  --color-danger-soft:   #fef2f2;
  --color-success:       #15803d;
  --color-success-soft:  #ecfdf5;
  --color-warning:       #b45309;
  --color-warning-soft:  #fef3c7;
  --color-info-soft:     #eef2ff;
  --color-error-bg:      #fef2f2;
  --color-success-bg:    #ecfdf5;
  --color-warning-bg:    #fef3c7;
  --color-info-bg:       #eef2ff;

  --color-focus-ring:    #818cf8;
  --color-overlay:       rgba(9, 9, 11, 0.55);

  /* Spacing (4-pt) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;

  /* Typography */
  --font-family-system: -apple-system, BlinkMacSystemFont, "Segoe UI Variable",
                        "Segoe UI", Roboto, "Inter", Oxygen, Ubuntu, Cantarell,
                        "Helvetica Neue", Arial, sans-serif;
  --font-family-mono:   ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo,
                        Consolas, "Liberation Mono", monospace;
  --font-size-xs:    0.75rem;
  --font-size-sm:    0.875rem;
  --font-size-base:  0.9375rem;
  --font-size-md:    1.0625rem;
  --font-size-lg:    1.25rem;
  --font-size-xl:    1.5rem;
  --font-size-xxl:   2rem;
  --line-height-base: 1.55;
  --letter-spacing-tight: -0.011em;

  /* Layout */
  --content-max:      1040px;
  --topbar-height:    60px;
  --bottom-nav-height: 64px;

  /* Radii — softer, more modern */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  /* Shadows — soft, layered */
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 3px rgba(15, 23, 42, 0.02);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.06), 0 2px 4px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 12px 28px rgba(15, 23, 42, 0.10), 0 4px 8px rgba(15, 23, 42, 0.04);
  --shadow-ring-focus: 0 0 0 3px var(--color-primary-ring);

  /* Motion */
  --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 250ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ---------- 1a. Dark mode (prefers-color-scheme) ------------------------ */

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg:            #09090b;
    --color-bg-subtle:     #18181b;
    --color-bg-hover:      #27272a;
    --color-bg-elevated:   #18181b;
    --color-text:          #fafafa;
    --color-text-muted:    #a1a1aa;
    --color-text-faint:    #71717a;
    --color-border:        rgba(255, 255, 255, 0.10);
    --color-border-strong: rgba(255, 255, 255, 0.18);

    --color-primary:       #818cf8;
    --color-primary-hover: #a5b4fc;
    --color-primary-soft:  rgba(129, 140, 248, 0.12);
    --color-primary-fg:    #0b0a18;
    --color-primary-ring:  rgba(129, 140, 248, 0.28);

    /* Dark-scheme combobox pair (surface + fg flip together → readable). */
    --combobox-surface:    #18181b;
    --combobox-fg:         #fafafa;
    --combobox-hover-bg:   rgba(129, 140, 248, 0.18);
    --combobox-border:     rgba(255, 255, 255, 0.22);

    --color-danger:        #f87171;
    --color-danger-hover:  #fca5a5;
    --color-danger-soft:   rgba(248, 113, 113, 0.10);
    --color-success:       #4ade80;
    --color-success-soft:  rgba(74, 222, 128, 0.10);
    --color-warning:       #fbbf24;
    --color-warning-soft:  rgba(251, 191, 36, 0.12);
    --color-info-soft:     rgba(129, 140, 248, 0.10);
    --color-error-bg:      rgba(248, 113, 113, 0.10);
    --color-success-bg:    rgba(74, 222, 128, 0.10);
    --color-warning-bg:    rgba(251, 191, 36, 0.12);
    --color-info-bg:       rgba(129, 140, 248, 0.10);

    --color-focus-ring:    #a5b4fc;
    --color-overlay:       rgba(0, 0, 0, 0.70);

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.40);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.40), 0 1px 3px rgba(0, 0, 0, 0.30);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.45), 0 2px 4px rgba(0, 0, 0, 0.30);
    --shadow-lg: 0 12px 28px rgba(0, 0, 0, 0.55), 0 4px 8px rgba(0, 0, 0, 0.35);
  }
}

/* ---------- 2. Reset / base ---------------------------------------------- */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  font-family: var(--font-family-system);
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  letter-spacing: var(--letter-spacing-tight);
  color: var(--color-text);
  background: var(--color-bg-subtle);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

img, svg, video { max-width: 100%; height: auto; display: block; }

h1, h2, h3, h4 {
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

a {
  color: var(--color-primary);
  text-decoration: none;
  text-underline-offset: 2px;
  transition: color var(--transition);
}
a:hover { color: var(--color-primary-hover); text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* Skip link (a11y) */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--color-text);
  color: var(--color-bg);
  padding: var(--space-2) var(--space-3);
  text-decoration: none;
  z-index: 1000;
  border-radius: var(--radius-sm);
}
.skip-link:focus { top: var(--space-2); left: var(--space-2); }

/* Visually hidden but accessible to screen readers */
.visually-hidden {
  position: absolute !important;
  clip: rect(1px, 1px, 1px, 1px);
  clip-path: inset(50%);
  width: 1px;
  height: 1px;
  overflow: hidden;
  white-space: nowrap;
}

/* ---------- 3. Layout: topbar / main / footer ---------------------------- */

.topbar {
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 30;
}
@media (prefers-color-scheme: dark) {
  .topbar {
    background: rgba(9, 9, 11, 0.78);
  }
}
@supports not ((backdrop-filter: blur(0)) or (-webkit-backdrop-filter: blur(0))) {
  .topbar { background: var(--color-bg); }
}

.topbar__inner {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-3) var(--space-5);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  min-height: var(--topbar-height);
  flex-wrap: wrap;
}
.brand {
  font-weight: 600;
  font-size: var(--font-size-md);
  color: var(--color-text);
  text-decoration: none;
  letter-spacing: -0.01em;
}
.brand:hover { color: var(--color-text); text-decoration: none; }

.topnav {
  display: flex;
  gap: var(--space-1);
  margin-left: var(--space-4);
  flex: 1;
  flex-wrap: wrap;
}
.topnav__link {
  color: var(--color-text-muted);
  text-decoration: none;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-weight: 500;
  font-size: var(--font-size-sm);
  transition: background-color var(--transition), color var(--transition);
}
.topnav__link:hover {
  background: var(--color-bg-hover);
  color: var(--color-text);
  text-decoration: none;
}

.topbar__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-left: auto;
}
.topbar__username {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  font-weight: 500;
}

.main {
  flex: 1 0 auto;
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-6) var(--space-5);
}

.footer {
  border-top: 1px solid var(--color-border);
  padding: var(--space-5);
  text-align: center;
  color: var(--color-text-faint);
  font-size: var(--font-size-xs);
}

/* ---------- 4. Buttons --------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-family: inherit;
  font-size: var(--font-size-sm);
  font-weight: 500;
  line-height: 1;
  letter-spacing: -0.005em;
  padding: var(--space-2) var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  min-height: 40px;
  transition:
    background-color var(--transition),
    color var(--transition),
    border-color var(--transition),
    box-shadow var(--transition),
    transform var(--transition);
}
.btn:hover:not(:disabled):not([aria-disabled="true"]) {
  text-decoration: none;
}
.btn:active:not(:disabled):not([aria-disabled="true"]) {
  transform: scale(0.98);
}
.btn:disabled,
.btn[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.5;
}

.btn--primary {
  background: var(--color-primary);
  color: var(--color-primary-fg);
  border-color: var(--color-primary);
  box-shadow: var(--shadow-xs);
}
.btn--primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
  border-color: var(--color-primary-hover);
  box-shadow: var(--shadow-sm);
}

.btn--secondary {
  background: var(--color-bg);
  color: var(--color-text);
  border-color: var(--color-border-strong);
}
.btn--secondary:hover:not(:disabled) {
  background: var(--color-bg-hover);
  border-color: var(--color-border-strong);
}

.btn--danger {
  background: var(--color-bg);
  color: var(--color-danger);
  border-color: var(--color-border-strong);
}
.btn--danger:hover:not(:disabled) {
  background: var(--color-danger);
  color: #ffffff;
  border-color: var(--color-danger);
}

.btn--link {
  background: transparent;
  border-color: transparent;
  color: var(--color-primary);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: var(--space-1) var(--space-2);
  min-height: auto;
}
.btn--link:hover:not(:disabled) {
  background: transparent;
  color: var(--color-primary-hover);
  text-decoration: underline;
}

.btn--small {
  font-size: var(--font-size-xs);
  padding: var(--space-1) var(--space-3);
  min-height: 30px;
  border-radius: var(--radius-sm);
}

/* ---------- 5. Forms ----------------------------------------------------- */

.form { display: flex; flex-direction: column; gap: var(--space-4); }
.inline-form { display: inline-flex; align-items: center; gap: var(--space-2); margin: 0; }

.field { display: flex; flex-direction: column; gap: var(--space-2); }
.field--narrow { max-width: 8rem; }
.field--checkbox { display: flex; align-items: flex-end; padding-bottom: var(--space-2); }
.field__label {
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--color-text);
}
.field__optional {
  color: var(--color-text-muted);
  font-weight: 400;
  font-size: var(--font-size-xs);
}
.field__hint {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  margin: 0;
}
.field__input {
  font-family: inherit;
  font-size: var(--font-size-base);
  padding: var(--space-2) var(--space-3);
  /* Default state must be unmistakably visible as a rectangle even on
     subtle/Telegram-themed backgrounds (FE-FIX 1). Use the muted text colour
     for the idle border — it is darker than --color-border-strong and works
     for both light and dark schemes via tokens. */
  border: 1px solid var(--color-text-muted);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  width: 100%;
  min-height: 40px;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.field__input::placeholder {
  color: var(--color-text-faint);
}
.field__input:focus {
  border-color: var(--color-primary);
  outline: none;
  box-shadow: var(--shadow-ring-focus);
}
.field__input:focus-visible {
  border-color: var(--color-primary);
  outline: none;
  box-shadow: var(--shadow-ring-focus);
}
.field__input--compact { width: auto; min-width: 12rem; }
.field__input--textarea {
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  min-height: 14rem;
  resize: vertical;
  line-height: 1.6;
}
.field__input[readonly] {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
  align-items: end;
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  user-select: none;
  font-size: var(--font-size-sm);
  color: var(--color-text);
}
.checkbox input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--color-primary);
  cursor: pointer;
}

.form-section {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  background: var(--color-bg);
}
.form-section__legend {
  padding: 0 var(--space-2);
  font-weight: 600;
  font-size: var(--font-size-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
}
.form-section__details {
  border-top: 1px dashed var(--color-border);
  padding-top: var(--space-3);
}
.form-section__details summary {
  cursor: pointer;
  font-size: var(--font-size-sm);
  color: var(--color-primary);
  font-weight: 500;
}

.form-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.form-error {
  background: var(--color-error-bg);
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  margin: 0 0 var(--space-3);
}
.form-success {
  background: var(--color-success-bg);
  color: var(--color-success);
  border: 1px solid var(--color-success);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  margin: 0 0 var(--space-3);
}

/* ---------- 6. Flash messages ------------------------------------------- */

.flashes {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.flash {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  font-size: var(--font-size-sm);
}
.flash--info    { background: var(--color-info-bg);    border-color: var(--color-primary); color: var(--color-primary); }
.flash--success { background: var(--color-success-bg); border-color: var(--color-success); color: var(--color-success); }
.flash--warning { background: var(--color-warning-bg); border-color: var(--color-warning); color: var(--color-warning); }
.flash--error   { background: var(--color-error-bg);   border-color: var(--color-danger);  color: var(--color-danger); }

/* ---------- 7. Pagination ----------------------------------------------- */

.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-6);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border);
}
.pagination__link {
  text-decoration: none;
  color: var(--color-primary);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-weight: 500;
  font-size: var(--font-size-sm);
  transition: background-color var(--transition);
}
.pagination__link:hover:not(.pagination__link--disabled) {
  background: var(--color-primary-soft);
  text-decoration: none;
}
.pagination__link--disabled { color: var(--color-text-faint); cursor: default; }
.pagination__info { color: var(--color-text-muted); font-size: var(--font-size-sm); }

/* ---------- 8. Auth pages (login, set-password) -------------------------- */

.page--login .main,
.page--set-password .main {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - var(--topbar-height) - 80px);
}

.auth-card {
  width: 100%;
  max-width: 26rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  box-shadow: var(--shadow-md);
}
.auth-card__title {
  margin: 0 0 var(--space-4);
  font-size: var(--font-size-xl);
  letter-spacing: -0.02em;
}
.auth-card__intro { color: var(--color-text-muted); margin: 0 0 var(--space-5); }
.auth-card__hint  { color: var(--color-text-muted); margin: var(--space-5) 0 0; font-size: var(--font-size-sm); }

/* ---------- 9. Inbox ----------------------------------------------------- */

.inbox__toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.inbox__title {
  margin: 0;
  font-size: var(--font-size-xl);
  flex: 0 0 auto;
  letter-spacing: -0.02em;
}
.inbox__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-left: auto;
  align-items: center;
}
.inbox__filter {
  display: flex;
  gap: var(--space-3);
  align-items: center;
}

.inbox__list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--color-bg);
  box-shadow: var(--shadow-xs);
}

.msg-row {
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  transition: background-color var(--transition);
  position: relative;
}
.msg-row:last-child { border-bottom: none; }
.msg-row:hover { background: var(--color-bg-hover); }
.msg-row--unread { background: var(--color-primary-soft); }
.msg-row--unread:hover { background: var(--color-primary-soft); }
.msg-row--unread::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--color-primary);
}
.msg-row--unread .msg-row__from,
.msg-row--unread .msg-row__subject {
  font-weight: 600;
  color: var(--color-text);
}

.msg-row__link {
  display: grid;
  /* indicator | account | from | tags | subject | date */
  grid-template-columns: 12px auto minmax(140px, 1fr) auto minmax(200px, 2fr) auto;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  text-decoration: none;
  color: inherit;
}
.msg-row__link:hover { text-decoration: none; color: inherit; }
.msg-row__indicator { color: var(--color-primary); font-size: var(--font-size-xs); }
.msg-row--unread .msg-row__indicator { color: var(--color-primary); }
.msg-row__account-tag {
  font-size: var(--font-size-xs);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  padding: 2px var(--space-3);
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 14rem;
  font-weight: 500;
}
.msg-row__from {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: var(--font-size-sm);
}
.msg-row__subject {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--color-text);
  font-size: var(--font-size-sm);
}
.msg-row__attach { margin-left: var(--space-2); color: var(--color-text-muted); }
.msg-row__date {
  white-space: nowrap;
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.inbox__empty {
  padding: var(--space-7) var(--space-4);
  text-align: center;
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  color: var(--color-text-muted);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

/* ---------- 10. Message view -------------------------------------------- */

.message {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-xs);
}
.message__toolbar {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg-subtle);
}
.message__header {
  padding: var(--space-5);
  border-bottom: 1px solid var(--color-border);
}
.message__subject {
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-lg);
  letter-spacing: -0.015em;
  word-wrap: break-word;
}
.message__meta { display: grid; grid-template-columns: 1fr; gap: var(--space-1); margin: 0; }
.message__meta-row {
  display: grid;
  grid-template-columns: 100px 1fr;
  gap: var(--space-3);
  font-size: var(--font-size-sm);
  padding: var(--space-1) 0;
}
.message__meta-row dt {
  color: var(--color-text-muted);
  margin: 0;
  font-weight: 500;
}
.message__meta-row dd {
  margin: 0;
  word-break: break-word;
  color: var(--color-text);
}

.message__body { padding: var(--space-5); }
.message__body-text {
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}

/* Round-12 bug B: rendered (sanitised) HTML body. ``shared.html_sanitize``
   has already stripped scripts / event handlers, so the only job here is
   visual containment — keep tables, images and long URLs inside the
   message viewport instead of forcing horizontal page scroll. */
.msg-body--html {
  max-width: 100%;
  overflow-x: auto;
  line-height: 1.55;
  font-size: var(--font-size-sm);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}
.msg-body--html img { max-width: 100%; height: auto; }
.msg-body--html a { word-break: break-word; }
.msg-body--html table { border-collapse: collapse; max-width: 100%; }
.msg-body--html td,
.msg-body--html th { padding: 2px 6px; vertical-align: top; }
.msg-body--html pre {
  white-space: pre-wrap;
  word-break: break-word;
}
.message__notice {
  margin: var(--space-3) 0 0;
  padding: var(--space-2) var(--space-3);
  background: var(--color-info-bg);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}

.message__attachments {
  border-top: 1px solid var(--color-border);
  padding: var(--space-5);
}
.message__attachments-title { margin: 0 0 var(--space-3); font-size: var(--font-size-md); }
.attachment-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.attachment-list__item {
  display: grid;
  grid-template-columns: 2fr 1fr auto;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: background-color var(--transition);
}
.attachment-list__item:hover { background: var(--color-bg-hover); }
.attachment-list__item--skipped { opacity: 0.7; }
.attachment-list__name { font-weight: 500; word-break: break-word; }
.attachment-list__meta { color: var(--color-text-muted); font-size: var(--font-size-sm); }
.attachment-list__skipped { color: var(--color-text-muted); font-size: var(--font-size-sm); }

/* ---------- 11. Compose -------------------------------------------------- */

.compose__toolbar { margin-bottom: var(--space-4); }
.compose__title { margin: 0 0 var(--space-5); font-size: var(--font-size-xl); letter-spacing: -0.02em; }

/* ---------- 12. Accounts ------------------------------------------------- */

.accounts__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}
.accounts__title { margin: 0; font-size: var(--font-size-xl); letter-spacing: -0.02em; }
.accounts__nav { margin-bottom: var(--space-3); }
.accounts__empty {
  padding: var(--space-7) var(--space-4);
  text-align: center;
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  color: var(--color-text-muted);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.account-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.account-list__item {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-5);
  background: var(--color-bg);
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
  box-shadow: var(--shadow-xs);
}
.account-list__item:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
  border-color: var(--color-border-strong);
}
.account-list__item--disabled {
  background: var(--color-bg-subtle);
}
.account-list__item--disabled:hover {
  transform: none;
  box-shadow: var(--shadow-xs);
}
.account-list__row {
  display: grid;
  grid-template-columns: 16px 1fr auto auto auto;
  align-items: center;
  gap: var(--space-3);
}
.account-list__indicator {
  color: var(--color-success);
  font-size: 0.7em;
}
.account-list__item--disabled .account-list__indicator { color: var(--color-danger); }
.account-list__email { font-weight: 600; word-break: break-word; }
.account-list__provider {
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  padding: 2px var(--space-3);
  font-weight: 500;
}
.account-list__sync { color: var(--color-text-muted); font-size: var(--font-size-sm); }
.account-list__actions {
  display: flex;
  gap: var(--space-2);
  justify-self: end;
  flex-wrap: wrap;
}
.account-list__error {
  margin: var(--space-3) 0 0;
  padding: var(--space-2) var(--space-3);
  background: var(--color-error-bg);
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
  word-break: break-word;
}

.account-test-result {
  font-size: var(--font-size-sm);
  font-weight: 500;
}
.account-test-result--ok    { color: var(--color-success); }
.account-test-result--error { color: var(--color-danger); }
.account-test-result--pending { color: var(--color-text-muted); }

/* ---------- Outlook OAuth (ADR-0025) ------------------------------------- */
.outlook-oauth {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  background: var(--color-primary-soft);
  margin-bottom: var(--space-5);
}
.outlook-oauth__title {
  margin: 0 0 var(--space-2);
  font-size: var(--font-size-md);
}
.outlook-oauth__lead {
  margin: 0 0 var(--space-4);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  line-height: var(--line-height-base);
}
.outlook-oauth__error { margin: var(--space-3) 0 0; }
.outlook-oauth__unavailable {
  margin: var(--space-3) 0 0;
  padding: var(--space-2) var(--space-3);
  background: var(--color-warning-bg);
  color: var(--color-warning);
  border: 1px solid var(--color-warning);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
}
.outlook-oauth__result {
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  border-top: 1px dashed var(--color-border-strong);
}
.outlook-oauth__instruction {
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-sm);
  line-height: var(--line-height-base);
}
.outlook-oauth__url-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}
.outlook-oauth__url {
  flex: 1 1 320px;
  min-width: 0;
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
}
.outlook-oauth__divider {
  display: flex;
  align-items: center;
  text-align: center;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin: var(--space-5) 0;
}
.outlook-oauth__divider::before,
.outlook-oauth__divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--color-border);
}
.outlook-oauth__divider span { padding: 0 var(--space-3); }
.outlook-reconnect-panel {
  margin-top: var(--space-4);
  padding: var(--space-4);
  border: 1px solid var(--color-warning);
  border-radius: var(--radius-lg);
  background: var(--color-warning-bg);
}

/* ---------- 13. Admin ---------------------------------------------------- */

.admin__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}
.admin__title { margin: 0; font-size: var(--font-size-xl); letter-spacing: -0.02em; }
.admin__actions { display: flex; gap: var(--space-3); align-items: center; flex-wrap: wrap; }
.admin__search { margin-bottom: var(--space-4); }
.admin__filter { margin-bottom: var(--space-4); }
.admin__empty {
  padding: var(--space-6);
  text-align: center;
  color: var(--color-text-muted);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  background: var(--color-bg);
}

.admin-user-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.admin-user-list__item {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-bg);
  box-shadow: var(--shadow-xs);
  transition: border-color var(--transition);
}
.admin-user-list__item:hover { border-color: var(--color-border-strong); }
.admin-user__summary {
  cursor: pointer;
  padding: var(--space-3) var(--space-4);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
  list-style: none;
}
.admin-user__summary::-webkit-details-marker { display: none; }
.admin-user__summary::before {
  content: "›";
  display: inline-block;
  color: var(--color-text-muted);
  font-size: var(--font-size-md);
  transition: transform var(--transition);
  font-weight: 600;
}
.admin-user[open] > .admin-user__summary::before { transform: rotate(90deg); }
.admin-user__name { font-weight: 600; }
.admin-user__email { color: var(--color-text-muted); font-size: var(--font-size-sm); }
.admin-user__meta { color: var(--color-text-muted); font-size: var(--font-size-sm); }
.admin-user__badge {
  display: inline-block;
  background: var(--color-primary-soft);
  color: var(--color-primary);
  border: 1px solid var(--color-primary);
  /* FE-FIX 5: Russian role labels (Супер-админ, Лидер команды, Участник
     команды) need horizontal padding without fixed widths, and must stay on
     one line. */
  padding: 0.15em 0.7em;
  border-radius: var(--radius-full);
  font-size: var(--font-size-xs);
  font-weight: 500;
  line-height: 1.5;
  white-space: nowrap;
  vertical-align: middle;
}
.admin-user__warn {
  color: var(--color-warning);
  background: var(--color-warning-bg);
  border: 1px solid var(--color-warning);
  padding: 2px var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--font-size-xs);
  font-weight: 500;
}
.admin-user__body {
  padding: var(--space-4);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg-subtle);
}
.admin-user__sub {
  margin: 0 0 var(--space-2);
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.admin-user__accounts {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.admin-user__account { display: flex; gap: var(--space-3); align-items: baseline; flex-wrap: wrap; }
.admin-user__account--disabled { opacity: 0.7; }
.admin-user__account-email { font-weight: 500; }
.admin-user__account-meta { color: var(--color-text-muted); font-size: var(--font-size-sm); }
.admin-user__notice { margin: 0; color: var(--color-text-muted); font-size: var(--font-size-sm); }
.admin-user__actions { display: flex; gap: var(--space-2); margin-top: var(--space-3); flex-wrap: wrap; }

/* Admin users table (replacing the old summary/details list, ADR-0019) */
.admin-users-table,
.admin-groups-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.admin-users-table th,
.admin-users-table td,
.admin-groups-table th,
.admin-groups-table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: top;
  font-size: var(--font-size-sm);
}
.admin-users-table thead th,
.admin-groups-table thead th {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: var(--font-size-xs);
}
.admin-users-table tbody tr:last-child td,
.admin-groups-table tbody tr:last-child td { border-bottom: none; }
.admin-users-table tbody tr:hover,
.admin-groups-table tbody tr:hover { background: var(--color-bg-subtle); }
.admin-users-table__row--system { color: var(--color-text-muted); }
.admin-users-table__name { font-weight: 600; }
.admin-users-table__muted { color: var(--color-text-muted); font-size: var(--font-size-xs); }

/* Group banding (FE-FIX round-6 #1+#2): each group is its own <tbody> with
   a thick contoured border. Internal members of the same group are flush
   (no gap); the visible gap appears ONLY between groups via a transparent
   spacer <tbody> the template renders between adjacent buckets.

   Implementation notes:
     - ``border-collapse: separate`` + ``border-spacing: 0`` keeps cells
       flush so members read as one block while still allowing rounded
       corners / per-side borders on cells.
     - Selectors qualify ``.admin-users-table tbody.user-group ...`` to
       beat the global ``tbody tr:last-child td { border-bottom: none }``
       rule that would otherwise hide the group's bottom contour. */
.admin-users-table {
  border-collapse: separate;
  border-spacing: 0;
}
.admin-users-table tbody.user-group tr td {
  border-bottom: none; /* default: no inner row separator */
}

.user-group {
  --user-group-accent: var(--color-border-strong);
  --user-group-tint:   var(--color-bg-subtle);
}
.user-group--band-a {
  --user-group-accent: rgb(34, 197, 94);    /* green */
  --user-group-tint:   rgba(34, 197, 94, 0.08);
}
.user-group--band-b {
  --user-group-accent: rgb(59, 130, 246);   /* blue */
  --user-group-tint:   rgba(59, 130, 246, 0.08);
}
.user-group--no-group {
  --user-group-accent: var(--color-border-strong);
  --user-group-tint:   var(--color-bg-subtle);
}

.user-group tr {
  background: var(--user-group-tint);
}
.user-group tr td:first-child {
  border-left:  2px solid var(--user-group-accent);
}
.user-group tr td:last-child {
  border-right: 2px solid var(--user-group-accent);
}
/* Top of the first row + bottom of the last row carry the group contour.
   Selectors are qualified with ``.admin-users-table tbody.user-group`` so
   they outrank the global ``tbody tr:last-child td { border-bottom: none }``
   and the override above. */
.admin-users-table tbody.user-group tr:first-child td {
  border-top:    2px solid var(--user-group-accent);
}
.admin-users-table tbody.user-group tr:last-child td {
  border-bottom: 2px solid var(--user-group-accent);
}
.user-group tr:first-child td:first-child { border-top-left-radius:     8px; }
.user-group tr:first-child td:last-child  { border-top-right-radius:    8px; }
.user-group tr:last-child  td:first-child { border-bottom-left-radius:  8px; }
.user-group tr:last-child  td:last-child  { border-bottom-right-radius: 8px; }

.admin-users-table tbody.user-group tr:hover {
  background: var(--user-group-tint);
  filter: brightness(0.95);
}

/* Spacer tbody between adjacent groups — purely visual gap, no border
   so the next group's top contour stays clean. */
.admin-users-table tbody.user-group__spacer td {
  background: transparent;
  border: none;
  padding: 0;
  height: var(--space-4);
}
.admin-users-table tbody.user-group__spacer tr:hover {
  background: transparent;
  filter: none;
}

/* Inline "+" button next to the user's name — opens the move-to-group
   dialog (FE-FIX round-4 #3). Sized to align with the name baseline. */
.admin-users-table__name-row {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}
.admin-users-table__add-group {
  appearance: none;
  border: 1px solid var(--color-border-strong);
  background: var(--color-bg);
  color: var(--color-primary);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.admin-users-table__add-group:hover {
  background: var(--color-primary-soft);
  color: var(--color-primary-hover);
}
.admin-users-table__add-group:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Inline expand block inside the actions cell. Provides reset/delete + the
   nested mail-accounts list without taking a whole row when collapsed. */
.admin-user-details > .admin-user-details__summary {
  cursor: pointer;
  color: var(--color-primary);
  font-weight: 600;
  list-style: none;
  padding: 0 var(--space-2);
  user-select: none;
}
.admin-user-details > .admin-user-details__summary::-webkit-details-marker { display: none; }
.admin-user-details[open] > .admin-user-details__summary { color: var(--color-primary-hover); }
.admin-user-details__body {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px dashed var(--color-border);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Role badge variants (FE-FIX 5: per-role colours, per spec).
   - super_admin: blue
   - group_leader: green
   - group_member: gray */
.admin-user__badge--super_admin {
  background: var(--color-primary-soft);
  color: var(--color-primary);
  border-color: var(--color-primary);
}
.admin-user__badge--group_leader {
  background: var(--color-success-soft);
  color: var(--color-success);
  border-color: var(--color-success);
}
.admin-user__badge--group_member {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  border-color: var(--color-border-strong);
}

.admin-groups-table__name strong { font-weight: 600; }

/* /admin/groups: each group's tbody now spans 2 rows — header + accounts.
   We mark the second row visually so it reads as belonging to the group
   above and add a soft gap before the next group. */
.admin-groups-table tbody.admin-groups-table__group + tbody.admin-groups-table__group tr:first-child td {
  border-top: 2px solid var(--color-border-strong);
}
.admin-groups-table__accounts-row td {
  background: var(--color-bg-subtle);
  padding: var(--space-3) var(--space-4) var(--space-4);
  border-top: 1px dashed var(--color-border);
}
.group-accounts__title {
  font-size: var(--font-size-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}
.group-accounts__count { font-weight: 400; }
.group-accounts__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.group-accounts__item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-2);
  padding: var(--space-1) 0;
  font-size: var(--font-size-sm);
}
.group-accounts__item--disabled .group-accounts__email {
  text-decoration: line-through;
  color: var(--color-text-muted);
}
.group-accounts__email { font-weight: 500; }
.group-accounts__empty { margin: 0; font-style: italic; }
.admin__hint {
  margin: 0 0 var(--space-4);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}
.admin__nav { margin-bottom: var(--space-4); }

/* Group meta panel on /admin/groups/{id}/edit. Reuses message__meta-row
   layout styles (see "message" section). */
.admin-group__meta {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-1);
  margin: 0 0 var(--space-5);
  padding: var(--space-3) var(--space-4);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

/* Account row: secondary email line below the main label when nickname is set
   (ADR-0020). Lets users always see the underlying email. */
.account-list__email-secondary {
  margin: var(--space-1) 0 0;
  padding-left: calc(var(--space-3) + 12px);
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
}
.account-list__email-secondary code {
  font-family: var(--font-family-mono);
  font-size: inherit;
}

/* Message-view secondary email shown after the nickname (ADR-0020). */
.message__meta-secondary {
  margin-left: var(--space-2);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  font-family: var(--font-family-mono);
}

/* Radio button group used in admin "create user" dialog. */
.radio-group { display: flex; flex-direction: column; gap: var(--space-2); margin: var(--space-2) 0; }
.radio { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; }
.radio input[type="radio"] {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--color-primary);
}

.table-wrapper { overflow-x: auto; }
.audit-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.audit-table th,
.audit-table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: top;
  font-size: var(--font-size-sm);
}
.audit-table thead th {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: var(--font-size-xs);
}
.audit-table tbody tr:last-child td { border-bottom: none; }
.audit-table tbody tr:hover { background: var(--color-bg-subtle); }
.audit-details summary {
  cursor: pointer;
  color: var(--color-primary);
  font-size: var(--font-size-sm);
  font-weight: 500;
}
.audit-details__pre {
  margin: var(--space-2) 0 0;
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-xs);
  white-space: pre-wrap;
  word-break: break-word;
  max-width: 40ch;
  overflow-x: auto;
}

/* ---------- 14. Dialog (modal) ------------------------------------------ */

.dialog {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  background: var(--color-bg);
  color: var(--color-text);
  width: min(28rem, calc(100vw - 2rem));
  box-shadow: var(--shadow-lg);
}
.dialog::backdrop {
  background: var(--color-overlay);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.dialog__title {
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-lg);
  letter-spacing: -0.015em;
}
.dialog__form-cancel {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  margin: 0;
}

/* ---------- 15. Error pages --------------------------------------------- */

.error-page {
  text-align: center;
  padding: var(--space-7) var(--space-4);
  max-width: 32rem;
  margin: 0 auto;
}
.error-page__code {
  font-size: var(--font-size-xxl);
  font-weight: 700;
  color: var(--color-text-muted);
  margin: 0;
  font-family: var(--font-family-mono);
  letter-spacing: -0.04em;
}
.error-page__title {
  margin: var(--space-3) 0;
  font-size: var(--font-size-xl);
  letter-spacing: -0.02em;
}
.error-page__message { color: var(--color-text-muted); margin: 0 0 var(--space-5); }
.error-page__actions { display: flex; justify-content: center; gap: var(--space-3); }
.error-page__rid { margin-top: var(--space-5); color: var(--color-text-faint); font-size: var(--font-size-xs); }
.error-page__rid code { font-family: var(--font-family-mono); }

/* ---------- 16. Bottom navigation (mobile + Telegram WebApp) ------------- */
/* Hidden by default on desktop; shown on mobile (<=640px) and inside
   Telegram WebApp regardless of viewport. CSS-only, CSP-friendly. */

.bottom-nav {
  display: none;
}

/* Bottom-nav items are text-only navigation links (FE-FIX 3); no icons,
   no logout — Logout lives only in the topbar (FE-FIX 2). */
.bottom-nav__item {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-2) var(--space-2);
  text-decoration: none;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  font-weight: 600;
  letter-spacing: 0;
  border-radius: var(--radius-md);
  transition: color var(--transition), background-color var(--transition);
  min-height: 48px;
}
.bottom-nav__item:hover {
  color: var(--color-text);
  text-decoration: none;
  background: var(--color-bg-hover);
}
.bottom-nav__item--active {
  color: var(--color-primary);
}
.bottom-nav__item--active:hover {
  color: var(--color-primary-hover);
}
.bottom-nav__label {
  font-weight: inherit;
  letter-spacing: 0;
  font-size: inherit;
  line-height: 1.2;
}

/* Show bottom nav on mobile (<=640px) and override main padding so the
   fixed nav does not overlap content. */
@media (max-width: 640px) {
  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--color-bg);
    border-top: 1px solid var(--color-border);
    padding: var(--space-1) var(--space-2);
    padding-bottom: max(var(--space-1), env(safe-area-inset-bottom));
    z-index: 40;
    box-shadow: 0 -1px 2px rgba(15, 23, 42, 0.04);
    gap: var(--space-1);
  }
  .main {
    padding-bottom: calc(var(--bottom-nav-height) + var(--space-5));
  }
}

/* Show bottom nav inside Telegram WebApp on any viewport width. */
body.tg-app .bottom-nav {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-bg);
  border-top: 1px solid var(--color-border);
  padding: var(--space-1) var(--space-2);
  padding-bottom: max(var(--space-1), env(safe-area-inset-bottom));
  z-index: 40;
  box-shadow: 0 -1px 2px rgba(15, 23, 42, 0.04);
  gap: var(--space-1);
}
body.tg-app .main {
  padding-bottom: calc(var(--bottom-nav-height) + var(--space-5));
}

/* ---------- 17. Mobile (≤640px) ----------------------------------------- */

@media (max-width: 640px) {
  .topbar__inner { padding: var(--space-2) var(--space-4); gap: var(--space-2); }
  .topnav { margin-left: 0; order: 3; flex-basis: 100%; display: none; }
  .topbar__user { margin-left: auto; }

  .main { padding: var(--space-4) var(--space-3); }

  .inbox__toolbar { flex-direction: column; align-items: stretch; }
  .inbox__actions { margin-left: 0; }
  .inbox__filter { flex-direction: column; align-items: stretch; }

  .msg-row__link {
    grid-template-columns: 12px auto 1fr;
    grid-template-areas:
      "ind tag date"
      "from from from"
      "tags tags tags"
      "subj subj subj";
    row-gap: var(--space-1);
    padding: var(--space-3) var(--space-4);
  }
  .msg-row__indicator { grid-area: ind; }
  .msg-row__account-tag { grid-area: tag; max-width: none; }
  .msg-row__from { grid-area: from; }
  .msg-row__tags { grid-area: tags; max-width: none; flex-wrap: wrap; }
  .msg-row__subject { grid-area: subj; white-space: normal; }
  .msg-row__date { grid-area: date; justify-self: end; }

  .message__meta-row { grid-template-columns: 80px 1fr; }

  .account-list__row {
    grid-template-columns: 16px 1fr;
    grid-template-areas:
      "ind email"
      ". provider"
      ". sync"
      ". actions";
    row-gap: var(--space-1);
  }
  .account-list__indicator { grid-area: ind; }
  .account-list__email     { grid-area: email; }
  .account-list__provider  { grid-area: provider; }
  .account-list__sync      { grid-area: sync; }
  .account-list__actions   { grid-area: actions; justify-self: start; }

  .field-grid { grid-template-columns: 1fr; }

  .pagination { flex-direction: column; gap: var(--space-2); }
}

/* ---------- 18. Reduced motion ------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.001ms !important;
    animation-duration: 0.001ms !important;
  }
  .btn:active:not(:disabled) { transform: none; }
  .account-list__item:hover { transform: none; }
}

/* ---- Help / how-to box on /accounts/new -------------------------------- */
.help-box {
  margin: var(--space-3) 0 var(--space-5);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-bg-subtle);
}
.help-box__summary {
  cursor: pointer;
  padding: var(--space-3) var(--space-4);
  font-weight: 600;
  color: var(--color-primary);
  list-style: none;
}
.help-box__summary::-webkit-details-marker {
  display: none;
}
.help-box__summary::before {
  content: "▸ ";
  display: inline-block;
  margin-right: 0.4em;
  transition: transform var(--transition);
}
.help-box[open] > .help-box__summary::before {
  content: "▾ ";
}
.help-box__body {
  padding: 0 var(--space-4) var(--space-4);
  font-size: var(--font-size-sm);
  line-height: 1.6;
}
.help-box__body p {
  margin: 0 0 var(--space-3);
}
.help-provider {
  margin: var(--space-2) 0;
  border-left: 3px solid var(--color-border-strong);
  padding: var(--space-2) var(--space-3);
}
.help-provider > summary {
  cursor: pointer;
  font-weight: 500;
  padding: var(--space-1) 0;
}
.help-provider ol,
.help-provider ul {
  margin: var(--space-2) 0 var(--space-2) var(--space-4);
  padding: 0;
}
.help-provider li {
  margin-bottom: var(--space-2);
}
.help-provider code {
  background: var(--color-bg-hover);
  padding: 0.05em 0.4em;
  border-radius: var(--radius-sm);
  font-size: 0.9em;
  font-family: var(--font-family-mono);
}
.help-provider a {
  color: var(--color-primary);
  word-break: break-word;
}
.help-warning {
  margin-top: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: var(--color-warning-bg);
  border-left: 3px solid var(--color-warning);
  border-radius: var(--radius-md);
  font-size: 0.95em;
}

/* ---------- 19. Tags (ADR-0017, 08-frontend.md §5.1) -------------------- */
/* All colour values are duplicated in backend/app/tags/schemas.py (PALETTE_COLORS)
   and in _macros.html (TAG_PALETTE_MAP). Keep them in sync. */

.tag-chip {
  display: inline-block;
  padding: 0.18em 0.7em;
  border-radius: var(--radius-full);
  font-size: 0.8em;
  font-weight: 500;
  color: #ffffff;
  margin-right: 0.3em;
  white-space: nowrap;
  vertical-align: middle;
  line-height: 1.4;
  letter-spacing: 0;
}

.tag-color-c1 { background-color: #4f46e5; }                    /* indigo */
.tag-color-c2 { background-color: #dc2626; }                    /* red    */
.tag-color-c3 { background-color: #f59e0b; color: #1a1a1a; }    /* amber  */
.tag-color-c4 { background-color: #16a34a; }                    /* green  */
.tag-color-c5 { background-color: #7c3aed; }                    /* purple */
.tag-color-c6 { background-color: #0891b2; }                    /* cyan   */
.tag-color-c7 { background-color: #db2777; }                    /* pink   */
.tag-color-c8 { background-color: #475569; }                    /* slate  */

/* Tag-kind labels (builtin / custom) on the tags list page */
.tag-kind {
  display: inline-block;
  padding: 0.1em 0.6em;
  border-radius: var(--radius-full);
  font-size: var(--font-size-xs);
  font-weight: 500;
  text-transform: lowercase;
  letter-spacing: 0;
}
.tag-kind--builtin {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  border: 1px solid var(--color-border-strong);
}
.tag-kind--custom {
  background: var(--color-primary-soft);
  color: var(--color-primary);
  border: 1px solid var(--color-primary);
}

/* Tags page layout */
.tags__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}
.tags__title { margin: 0; font-size: var(--font-size-xl); letter-spacing: -0.02em; }
.tags__nav { margin-bottom: var(--space-3); }
.tags__empty {
  padding: var(--space-7) var(--space-4);
  text-align: center;
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  color: var(--color-text-muted);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

/* Tags list table */
.tags-list {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.tags-list th,
.tags-list td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  font-size: var(--font-size-sm);
}
.tags-list thead th {
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: var(--font-size-xs);
}
.tags-list tbody tr:last-child td { border-bottom: none; }
.tags-list tbody tr:hover { background: var(--color-bg-subtle); }
.tags-list__count { color: var(--color-text-muted); font-variant-numeric: tabular-nums; }
.tags-list__actions {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: var(--space-2);
  justify-content: flex-end;
  align-items: center;
}
.tags-list__actions-col { text-align: right; width: 1%; white-space: nowrap; }
.tags-list__row--builtin .tags-list__actions {
  /* builtin rows still align edit button to the right */
  justify-content: flex-end;
}

/* Tag form: colour swatch radio group */
.tag-color-fieldset .color-swatches {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}
.color-swatch-label {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 50%;
}
.color-swatch {
  display: inline-block;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
  transition: transform var(--transition);
}
.color-swatch-label:hover .color-swatch {
  transform: scale(1.08);
}
.color-swatch-input:checked + .color-swatch {
  border-color: var(--color-text);
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.15),
    0 0 0 2px var(--color-bg);
}
.color-swatch-input:focus-visible + .color-swatch {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 2px;
}

/* Tag form: rule rows */
.tag-rules {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.rule-row {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}
.rule-row select.field__input,
.rule-row input.field__input {
  width: auto;
  min-width: 12rem;
  flex: 1 1 12rem;
  /* Round-19: HARD cap. Some WebViews (Telegram on iOS in particular)
     stretch <select>/<input> inside flex containers to absurd heights.
     Pin the size unconditionally so a rule row is never taller than one
     line of text. Applies to every page using .rule-row so the create-
     and edit-tag forms behave the same. */
  min-height: 36px !important;
  max-height: 36px !important;
  height: 36px !important;
  padding: var(--space-1) var(--space-2) !important;
  font-size: var(--font-size-sm) !important;
  line-height: 1.2 !important;
  box-sizing: border-box !important;
}
.rule-row__remove {
  background: transparent;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  width: 36px;
  height: 36px;
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: var(--font-size-md);
  line-height: 1;
  flex: 0 0 auto;
  transition: background-color var(--transition), color var(--transition), border-color var(--transition);
}
.rule-row__remove:hover {
  background: var(--color-danger-soft);
  color: var(--color-danger);
  border-color: var(--color-danger);
}
.tag-rules__actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-2);
  flex-wrap: wrap;
}

/* Tag edit page: rules management section */
.tag-rules-manage {
  margin-top: var(--space-6);
  padding-top: var(--space-5);
  border-top: 1px solid var(--color-border);
}
.tag-rules-manage__title {
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-lg);
  letter-spacing: -0.015em;
}
.tag-rules-manage__sub {
  margin: 0 0 var(--space-2);
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.tag-rules-manage__list {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.tag-rules-manage__empty {
  color: var(--color-text-muted);
  margin: 0 0 var(--space-4);
}
.tag-rule-item {
  display: grid;
  grid-template-columns: minmax(140px, auto) 1fr auto;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
}
.tag-rule-item__type {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  font-weight: 500;
}
.tag-rule-item__pattern code {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  padding: 0.1em 0.45em;
  border-radius: var(--radius-sm);
  font-size: 0.9em;
  word-break: break-all;
  font-family: var(--font-family-mono);
}
.tag-rules-manage__add {
  margin-bottom: var(--space-4);
}
.tag-rules-manage__apply {
  margin: 0;
}

/* Tag chips column inside the inbox row + message header.
   Sits between the "from" column and the subject so a long subject
   never pushes tags out of view. Truncation happens in the subject
   column (white-space:nowrap + ellipsis), not here. */
.msg-row__tags {
  display: flex;
  gap: 0.3em;
  flex-wrap: nowrap;
  max-width: 14rem;
  overflow: hidden;
}
.msg-row__tags:empty { display: none; }
.message__tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3em;
  margin: var(--space-3) 0 0;
}

/* Mobile tweaks for tags list */
@media (max-width: 640px) {
  .tags-list,
  .tags-list thead,
  .tags-list tbody,
  .tags-list tr,
  .tags-list th,
  .tags-list td {
    display: block;
    width: 100%;
  }
  .tags-list thead { display: none; }
  .tags-list tbody tr {
    border-bottom: 1px solid var(--color-border);
    padding: var(--space-2) 0;
  }
  .tags-list td {
    border-bottom: none;
    padding: var(--space-1) var(--space-3);
  }
  .tags-list__actions {
    /* keep buttons side-by-side even on narrow screens (FE-FIX round-7 #2). */
    justify-content: flex-start;
    flex-wrap: nowrap;
  }
  .tag-rule-item {
    /* keep "type | pattern | delete" on one row instead of stacking,
       which made each rule block taller than the actual content. */
    grid-template-columns: minmax(110px, auto) 1fr auto;
    gap: var(--space-2);
  }
  .rule-row { flex-direction: column; align-items: stretch; }
  .rule-row select.field__input,
  .rule-row input.field__input,
  .rule-row select.field__input--compact,
  .rule-row input.field__input--compact {
    width: 100%;
    min-width: 0;
    /* Compact controls had min-width:12rem which still forced row width on
       mid-narrow Telegram WebView; force the compact override off here. */
  }
  .rule-row__remove { width: 100%; }
}

/* Mobile / Telegram WebView tweaks for the tag create/edit form.
   Without these the create form is ~5×3 stacked inputs + a generous
   form-section padding, which ate roughly half of a 640×844 viewport.
   Applies to .tags-form pages on narrow viewports AND to all Telegram
   WebView viewports (mobile/desktop), keyed off body.page--tags-form. Desktop
   layout is intentionally untouched. */
@media (max-width: 640px) {
  body.page--tags-form .form-section {
    padding: var(--space-3);
    gap: var(--space-3);
    border-radius: var(--radius-md);
  }
  body.page--tags-form .tags__form { gap: var(--space-3); }
  body.page--tags-form .tags__title {
    font-size: var(--font-size-lg);
    margin-bottom: var(--space-3);
  }

  /* Color picker: smaller swatches + tighter gap so the row no longer
     wraps to 2 lines on a 360px wide viewport. */
  body.page--tags-form .tag-color-fieldset .color-swatches { gap: var(--space-2); }
  body.page--tags-form .color-swatch { width: 26px; height: 26px; }

  /* Initial rules list (create form) — was the biggest space hog:
     5 rows × (select + input + remove) stacked column-wise. */
  body.page--tags-form .tag-rules { gap: var(--space-1); }
  body.page--tags-form .rule-row {
    gap: var(--space-1);
    padding: var(--space-2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-bg-subtle);
  }
  body.page--tags-form .rule-row select.field__input,
  body.page--tags-form .rule-row input.field__input {
    min-height: 36px;
    padding: var(--space-1) var(--space-2);
    font-size: var(--font-size-sm);
  }
  body.page--tags-form .rule-row__remove {
    min-height: 32px;
    height: 32px;
    font-size: var(--font-size-sm);
  }
  body.page--tags-form .tag-rules__actions { margin-top: var(--space-1); gap: var(--space-2); }
  body.page--tags-form .field__hint { font-size: var(--font-size-xs); margin: 0; }

  /* Form actions — full-width primary action keeps thumb reach easy. */
  body.page--tags-form .form-actions { flex-direction: column; align-items: stretch; gap: var(--space-2); }
  body.page--tags-form .form-actions .btn { width: 100%; }

  /* Edit page: management section (existing rules list, add-rule form,
     apply-to-existing form). Keep them compact and cap the list height
     so a tag with 30 rules doesn't push the action buttons off-screen. */
  body.page--tags-form .tag-rules-manage { margin-top: var(--space-4); padding-top: var(--space-3); }
  body.page--tags-form .tag-rules-manage__title { font-size: var(--font-size-md); margin-bottom: var(--space-2); }
  body.page--tags-form .tag-rules-manage__list {
    max-height: 14rem;
    overflow-y: auto;
    margin-bottom: var(--space-3);
    gap: var(--space-1);
    -webkit-overflow-scrolling: touch;
  }
  body.page--tags-form .tag-rule-item {
    padding: var(--space-1) var(--space-2);
    font-size: var(--font-size-xs);
    gap: var(--space-1);
  }
  body.page--tags-form .tag-rule-item__type { font-size: var(--font-size-xs); }
  body.page--tags-form .tag-rules-manage__add { margin-bottom: var(--space-3); }
  body.page--tags-form .tag-rules-manage__add .rule-row { flex-direction: column; }
  body.page--tags-form .tag-rules-manage__add .btn { width: 100%; }
  body.page--tags-form .tag-rules-manage__apply .btn { width: 100%; }
}

/* Same compaction inside Telegram WebView regardless of viewport width
   — Desktop Telegram client can be wider than 640px but the WebApp
   panel is still narrow. */
body.tg-app.page--tags-form .form-section {
  padding: var(--space-3);
  gap: var(--space-3);
  border-radius: var(--radius-md);
}
body.tg-app.page--tags-form .tags__form { gap: var(--space-3); }
body.tg-app.page--tags-form .tags__title {
  font-size: var(--font-size-lg);
  margin-bottom: var(--space-3);
}
body.tg-app.page--tags-form .tag-color-fieldset .color-swatches { gap: var(--space-2); }
body.tg-app.page--tags-form .color-swatch { width: 26px; height: 26px; }

body.tg-app.page--tags-form .tag-rules { gap: var(--space-1); }
body.tg-app.page--tags-form .rule-row {
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-1);
  padding: var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg-subtle);
}
body.tg-app.page--tags-form .rule-row select.field__input,
body.tg-app.page--tags-form .rule-row input.field__input,
body.tg-app.page--tags-form .rule-row select.field__input--compact,
body.tg-app.page--tags-form .rule-row input.field__input--compact {
  width: 100%;
  min-width: 0;
  min-height: 36px;
  padding: var(--space-1) var(--space-2);
  font-size: var(--font-size-sm);
}
body.tg-app.page--tags-form .rule-row__remove {
  width: 100%;
  min-height: 32px;
  height: 32px;
  font-size: var(--font-size-sm);
}
body.tg-app.page--tags-form .tag-rules__actions { margin-top: var(--space-1); gap: var(--space-2); }
body.tg-app.page--tags-form .field__hint { font-size: var(--font-size-xs); margin: 0; }

body.tg-app.page--tags-form .form-actions { flex-direction: column; align-items: stretch; gap: var(--space-2); }
body.tg-app.page--tags-form .form-actions .btn { width: 100%; }

body.tg-app.page--tags-form .tag-rules-manage { margin-top: var(--space-4); padding-top: var(--space-3); }
body.tg-app.page--tags-form .tag-rules-manage__title { font-size: var(--font-size-md); margin-bottom: var(--space-2); }
body.tg-app.page--tags-form .tag-rules-manage__list {
  max-height: 14rem;
  overflow-y: auto;
  margin-bottom: var(--space-3);
  gap: var(--space-1);
  -webkit-overflow-scrolling: touch;
}
body.tg-app.page--tags-form .tag-rule-item {
  /* keep one-line layout (type | pattern | delete) like the existing
     mobile rule above; TG WebView is narrow, allow a tighter min col. */
  grid-template-columns: minmax(100px, auto) 1fr auto;
  padding: var(--space-1) var(--space-2);
  font-size: var(--font-size-xs);
  gap: var(--space-1);
}
body.tg-app.page--tags-form .tag-rule-item__type { font-size: var(--font-size-xs); }
body.tg-app.page--tags-form .tag-rules-manage__add { margin-bottom: var(--space-3); }
body.tg-app.page--tags-form .tag-rules-manage__add .btn { width: 100%; }
body.tg-app.page--tags-form .tag-rules-manage__apply .btn { width: 100%; }

/* ---------- 20. Telegram WebApp adaptation (ADR-0018, 08-frontend.md §10) ----
   Activated only when tg.js detects window.Telegram.WebApp and adds the
   `tg-app` class to <body>. In a regular browser these rules never match.
   The --tg-* custom properties are populated by tg.js from Telegram's
   themeParams; each one falls back to the equivalent default-light token
   so the layout stays sensible if a particular param is absent.
   ---------------------------------------------------------------------- */

body.tg-app {
  /* Re-bind the design tokens used across the stylesheet to Telegram's
     theme. Components keep referencing --color-bg / --color-text / etc. —
     no per-component overrides required. */
  --color-bg:         var(--tg-bg, #ffffff);
  --color-bg-subtle:  var(--tg-secondary-bg, #fafafa);
  --color-bg-hover:   var(--tg-secondary-bg, #f4f4f5);
  /* Elevated surface was previously NOT rebound here, so it kept the
     prefers-color-scheme value while --color-text followed --tg-text. When the
     OS scheme and the Telegram theme disagreed (e.g. OS dark + TG light) the
     combobox listbox got a dark surface with dark text — the reported
     "dark-on-dark, unreadable" bug. Bind the elevated surface to Telegram's
     own background so surface + text always come from the SAME theme. */
  --color-bg-elevated: var(--tg-secondary-bg, var(--tg-bg, #ffffff));
  --color-text:       var(--tg-text, #0a0a0a);
  --color-text-muted: var(--tg-hint, #52525b);
  --color-primary:    var(--tg-button, #4f46e5);
  --color-primary-fg: var(--tg-button-text, #ffffff);

  /* Combobox pair, sourced entirely from the Telegram theme so the dropdown
     surface and option text are guaranteed mutually consistent inside the
     WebApp regardless of the device OS colour scheme. */
  --combobox-surface:  var(--tg-secondary-bg, var(--tg-bg, #ffffff));
  --combobox-fg:       var(--tg-text, #0a0a0a);
  --combobox-hover-bg: var(--tg-bg, rgba(129, 140, 248, 0.18));
  --combobox-border:   var(--tg-hint, rgba(0, 0, 0, 0.18));
}

/* Telegram WebView already shows its own back-button + close-button at the
   top of the screen; duplicating our top nav would only clutter the view.
   The brand title and the user/logout block stay visible (logout was
   removed from bottom-nav and must remain reachable from the topbar).
   The bottom-nav (built into base.html, gated by request.state.session)
   takes over primary navigation inside the WebView. */
body.tg-app .topnav {
  display: none;
}
body.tg-app .topbar__inner {
  padding: var(--space-2) var(--space-4);
  min-height: auto;
}

/* Tighter padding around the main content on the WebView (typically a
   smaller viewport). On wide screens the .main max-width still applies. */
body.tg-app .main {
  padding: var(--space-3) var(--space-4) calc(var(--bottom-nav-height) + var(--space-5));
}

/* The footer with brand name is also redundant inside Telegram. */
body.tg-app .footer {
  display: none;
}

/* ---------- 21. Integrations (ADR-0023, 04-api-contracts.md §4b) ---------
   Outbound-webhook configuration page (/my/integrations). One config card per
   team — empty-state card for "not configured", populated card with status
   badges + edit / toggle / test / rotate / delete actions for "configured".
   Mobile-first; @media (max-width: 640px) below stacks action buttons full-
   width. Telegram WebApp tweaks live in the body.tg-app.page--integrations
   block at the bottom of this section. */

.integrations__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
  flex-wrap: wrap;
}
.integrations__title {
  margin: 0;
  font-size: var(--font-size-xl);
  letter-spacing: -0.02em;
}
.integrations__lede {
  color: var(--color-text-muted);
  margin: 0 0 var(--space-5);
  max-width: 60ch;
  line-height: var(--line-height-base);
}
.integrations__lede code {
  font-family: var(--font-family-mono);
  font-size: 0.92em;
  background: var(--color-bg-hover);
  padding: 1px 6px;
  border-radius: var(--radius-sm);
}
.integrations__error {
  padding: var(--space-3) var(--space-4);
  background: var(--color-error-bg);
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-5);
  word-break: break-word;
}

.integrations-empty {
  padding: var(--space-7) var(--space-5);
  text-align: center;
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  color: var(--color-text-muted);
  background: var(--color-bg);
}
.integrations-empty__title {
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-lg);
  color: var(--color-text);
  letter-spacing: -0.015em;
}
.integrations-empty code {
  font-family: var(--font-family-mono);
  font-size: 0.92em;
  background: var(--color-bg-hover);
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  color: var(--color-text);
}

.integrations-card {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5) var(--space-5);
  background: var(--color-bg);
  box-shadow: var(--shadow-xs);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}
.integrations-card--empty {
  border-style: dashed;
  background: var(--color-bg-subtle);
}
.integrations-card--dead {
  border-color: var(--color-danger);
  background: var(--color-danger-soft);
}
.integrations-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin: 0;
}
.integrations-card__title {
  margin: 0;
  font-size: var(--font-size-lg);
  letter-spacing: -0.015em;
}
.integrations-card__hint {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}
.integrations-card__hint code {
  font-family: var(--font-family-mono);
  background: var(--color-bg-hover);
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  font-size: 0.92em;
}
.integrations-card__status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}
.integrations-card__dead {
  background: var(--color-bg);
  border: 1px solid var(--color-danger);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  color: var(--color-text);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.integrations-card__dead p {
  margin: 0;
}
.integrations-card__dead code {
  font-family: var(--font-family-mono);
  background: var(--color-bg-hover);
  padding: 1px 5px;
  border-radius: var(--radius-sm);
}

/* Status badges — match the visual weight of the tag-chip / role badge. */
.integrations-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--font-size-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  line-height: 1.4;
  border: 1px solid transparent;
  white-space: nowrap;
}
.integrations-badge--ok {
  color: var(--color-success);
  background: var(--color-success-soft);
  border-color: rgba(21, 128, 61, 0.25);
}
.integrations-badge--off {
  color: var(--color-text-muted);
  background: var(--color-bg-hover);
  border-color: var(--color-border);
}
.integrations-badge--warn {
  color: var(--color-warning);
  background: var(--color-warning-soft);
  border-color: rgba(180, 83, 9, 0.25);
}
.integrations-badge--dead {
  color: var(--color-danger);
  background: var(--color-danger-soft);
  border-color: rgba(220, 38, 38, 0.35);
}

/* Meta-info dl — URL / created / last_fired / failures / last_error. */
.integrations-card__meta {
  display: grid;
  grid-template-columns: minmax(180px, max-content) 1fr;
  gap: var(--space-2) var(--space-5);
  margin: 0;
}
.integrations-meta__row {
  display: contents;
}
.integrations-meta__label {
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin: 0;
}
.integrations-meta__value {
  margin: 0;
  font-size: var(--font-size-sm);
  color: var(--color-text);
  word-break: break-word;
  min-width: 0;
}
.integrations-meta__muted { color: var(--color-text-faint); }
.integrations-meta__bad   { color: var(--color-danger); font-weight: 600; }
.integrations-meta__url {
  font-family: var(--font-family-mono);
  font-size: 0.92em;
  background: var(--color-bg-hover);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  word-break: break-all;
  display: inline-block;
  max-width: 100%;
}
.integrations-meta__error {
  display: inline-block;
  max-width: 60ch;
  color: var(--color-danger);
  font-family: var(--font-family-mono);
  font-size: 0.92em;
  background: var(--color-bg);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-danger);
  word-break: break-word;
  white-space: pre-wrap;
}

/* Inline edit-URL form — keeps the input + button on one row above 640px. */
.integrations-form {
  margin: 0;
}
.integrations-form--inline {
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-4);
  margin-top: 0;
}

/* The is_active toggle row. */
.integrations-toggle {
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-4);
  margin: 0;
}
.integrations-toggle__label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
  user-select: none;
  font-size: var(--font-size-sm);
  color: var(--color-text);
}
.integrations-toggle__checkbox {
  width: 20px;
  height: 20px;
  accent-color: var(--color-primary);
  cursor: pointer;
}
.integrations-toggle__checkbox:disabled {
  cursor: progress;
  opacity: 0.6;
}
.integrations-toggle__text {
  font-weight: 500;
}

/* Action button row (test / rotate / delete). */
.integrations-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-4);
  margin: 0;
}

/* Secret-reveal modal. Uses the shared .dialog backdrop styles (section 14). */
.integrations-secret-reveal {
  width: min(34rem, calc(100vw - 2rem));
}
.integrations-secret-reveal__warning {
  background: var(--color-warning-soft);
  color: var(--color-warning);
  border: 1px solid rgba(180, 83, 9, 0.35);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  margin: 0 0 var(--space-4);
  font-size: var(--font-size-sm);
  line-height: var(--line-height-base);
}
.integrations-secret-reveal__warning strong { font-weight: 700; }
.integrations-secret-reveal__row {
  display: flex;
  gap: var(--space-2);
  align-items: stretch;
  margin: var(--space-2) 0 var(--space-2);
  flex-wrap: wrap;
}
.integrations-secret-reveal__input {
  flex: 1 1 240px;
  min-width: 0;
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-bg-subtle);
  color: var(--color-text);
}
.integrations-secret-reveal__input:focus {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 1px;
}
.integrations-secret-reveal__pre {
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  margin: var(--space-3) 0;
  white-space: pre-wrap;
  word-break: break-all;
}
.integrations-secret-reveal--fallback {
  margin-top: var(--space-5);
  border: 1px solid var(--color-warning);
  background: var(--color-warning-soft);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}

/* ---------- 21b. Telegram-привязки (ADR-0024 §4) ---------- */
.tg-links {
  margin-top: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  background: var(--color-bg);
}
.tg-links__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-2);
}
.tg-links__body {
  margin: var(--space-4) 0;
}
.tg-links__loading {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin: 0;
}
.tg-links__error {
  color: var(--color-danger);
  background: var(--color-danger-soft);
  border: 1px solid rgba(220, 38, 38, 0.35);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-sm);
}
.tg-links__retry {
  margin-top: var(--space-2);
}
.tg-links__empty {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}
.tg-links__empty p { margin: 0; }
.tg-links__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.tg-links__item {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-wrap: wrap;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  background: var(--color-bg-subtle);
}
.tg-links__item--dead {
  border-color: rgba(220, 38, 38, 0.35);
}
.tg-links__item-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 220px;
  min-width: 0;
}
.tg-links__id {
  font-family: var(--font-family-mono);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  word-break: break-all;
}
.tg-links__date {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
}
.tg-links__status {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.tg-links__dead-hint {
  margin: 0;
  font-size: var(--font-size-xs);
  color: var(--color-danger);
  max-width: 36ch;
}
.tg-links__item-actions {
  margin-left: auto;
}
.tg-links__actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  align-items: flex-start;
}
.tg-links__add-hint,
.tg-links__limit-note {
  margin: var(--space-2) 0 0;
}
.tg-links__noscript {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin-top: var(--space-3);
}

/* ---------- 21a. Mobile (<=640px) tweaks ---------- */
@media (max-width: 640px) {
  .integrations-card {
    padding: var(--space-4);
    gap: var(--space-4);
  }
  .integrations-card__header {
    align-items: flex-start;
    flex-direction: column;
  }
  .integrations-card__meta {
    grid-template-columns: 1fr;
    gap: var(--space-1) 0;
  }
  .integrations-meta__row {
    display: block;
    padding-bottom: var(--space-3);
    border-bottom: 1px dashed var(--color-border);
  }
  .integrations-meta__row:last-child {
    border-bottom: 0;
    padding-bottom: 0;
  }
  .integrations-meta__label {
    font-size: var(--font-size-xs);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: block;
    margin-bottom: 2px;
  }
  .integrations-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .integrations-actions .inline-form,
  .integrations-actions .inline-form .btn {
    width: 100%;
  }
  .integrations-form .form-actions .btn,
  .integrations-card__dead .inline-form .btn {
    width: 100%;
  }
  .integrations-secret-reveal__row {
    flex-direction: column;
  }
  .integrations-secret-reveal__row .btn { width: 100%; }
}

/* ---------- 21b. Telegram WebApp variant ---------- */
body.tg-app.page--integrations .integrations-card {
  padding: var(--space-4);
  gap: var(--space-4);
}
body.tg-app.page--integrations .integrations__lede {
  font-size: var(--font-size-sm);
}
body.tg-app.page--integrations .integrations-card__meta {
  grid-template-columns: 1fr;
  gap: var(--space-1) 0;
}
body.tg-app.page--integrations .integrations-meta__row {
  display: block;
  padding-bottom: var(--space-3);
  border-bottom: 1px dashed var(--color-border);
}
body.tg-app.page--integrations .integrations-meta__row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}
body.tg-app.page--integrations .integrations-meta__label {
  font-size: var(--font-size-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  display: block;
  margin-bottom: 2px;
}
body.tg-app.page--integrations .integrations-actions {
  flex-direction: column;
  align-items: stretch;
}
body.tg-app.page--integrations .integrations-actions .inline-form,
body.tg-app.page--integrations .integrations-actions .inline-form .btn,
body.tg-app.page--integrations .integrations-form .form-actions .btn,
body.tg-app.page--integrations .integrations-card__dead .inline-form .btn {
  width: 100%;
}
body.tg-app.page--integrations .integrations-secret-reveal {
  width: calc(100vw - 2rem);
}
body.tg-app.page--integrations .integrations-secret-reveal__row {
  flex-direction: column;
}
body.tg-app.page--integrations .integrations-secret-reveal__row .btn {
  width: 100%;
}

/* =============================================================================
   Searchable account combobox (inbox filter — ARIA 1.2 combobox pattern).
   ========================================================================== */
.combobox {
  position: relative;
  display: inline-flex;
  align-items: center;
}
.combobox__input {
  /* Leave room for the clear (×) button on the right. */
  padding-right: 2rem;
}
.combobox__clear {
  position: absolute;
  right: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  padding: 0;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}
.combobox__clear:hover { color: var(--color-text); }
.combobox__clear:focus-visible {
  outline: none;
  box-shadow: var(--shadow-ring-focus);
}
.combobox__listbox {
  position: absolute;
  top: calc(100% + var(--space-1));
  left: 0;
  z-index: 30;
  margin: 0;
  padding: var(--space-1);
  list-style: none;
  width: 100%;
  min-width: 16rem;
  max-height: 18rem;
  overflow-y: auto;
  /* Pin the surface + text as a MATCHED PAIR (--combobox-surface / -fg). These
     two tokens are always redefined together — in :root (light), the
     prefers-color-scheme:dark block, and body.tg-app — so the dropdown can
     never get a background from one scheme and text from another. This is the
     real fix for the "dark-on-dark / unreadable" report: the earlier version
     mixed --color-bg-elevated (scheme-driven) with --color-text (tg-driven),
     which desynced when the OS scheme and Telegram theme disagreed. */
  background-color: var(--combobox-surface);
  color: var(--combobox-fg);
  border: 1px solid var(--combobox-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}
/* Scope option colours under .combobox__listbox for extra specificity so no
   ancestor rule (e.g. a tinted toolbar text colour) can override them. */
.combobox__listbox .combobox__option,
.combobox__option {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
  /* Inherit the matched listbox foreground; explicit so it can never pick up a
     tinted/dark colour from an ancestor and clash with the surface. */
  color: var(--combobox-fg);
  background-color: transparent;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.combobox__listbox .combobox__option:hover,
.combobox__listbox .combobox__option.is-active,
.combobox__option:hover,
.combobox__option.is-active {
  background-color: var(--combobox-hover-bg);
  color: var(--combobox-fg);
}

@media (max-width: 768px) {
  .combobox { width: 100%; }
  .combobox__input { width: 100%; }
}
