/* ==========================================================================
   Nestea AI OS — UI V1 design system.
   Единственный визуальный язык продукта. Контракт: NESTEA_UI_SYSTEM.md.
   Все дизайн-константы живут в секции токенов ниже; вне её — только
   технические значения (позиции, проценты, currentColor/transparent).
   ========================================================================== */

/* ==========================================================================
   Self-hosted typography (NESTEA_UI_SYSTEM.md §3)

   Manrope and Cormorant Garamond are NOT guaranteed system fonts on macOS or
   iOS, so without these files the approved typography silently degraded to a
   fallback on the owner's own devices. The woff2 assets are served from this
   origin only: no Google Fonts request, no external host, nothing for a CSP
   to whitelist beyond 'self'.

   Subsets are split latin / cyrillic exactly as upstream ships them, and the
   `unicode-range` below is what lets the browser fetch ONLY the file a given
   string needs. The Cyrillic files are mandatory — the interface is Russian.
   Licences and provenance: /fonts/README.txt.
   ========================================================================== */

/* Manrope is a VARIABLE font: one file per subset covers 400 / 500 / 600. */
@font-face {
  font-family: "Manrope";
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url("/fonts/manrope-var-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
                 U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
                 U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Manrope";
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url("/fonts/manrope-var-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}

/* Cormorant Garamond is variable too: .hero renders at 500 while .brand-name,
   .topbar-brand and .login-card h1 render at 600, so a single static cut would
   have been wrong for one of them. One file per subset covers 400..600. */
@font-face {
  font-family: "Cormorant Garamond";
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url("/fonts/cormorant-garamond-var-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
                 U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
                 U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Cormorant Garamond";
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url("/fonts/cormorant-garamond-var-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}

/* == DESIGN TOKENS START == */
:root {
  /* Palette: soft luxury, warm, powder accent (10–15% of the surface) */
  --color-bg: #FBF9F7;
  --color-surface: #FFFFFF;
  --color-surface-soft: #FCF6F4;

  --color-powder-50: #FBF3F1;
  --color-powder-100: #F7E8E5;
  --color-powder-200: #F0D4D0;
  --color-powder-300: #E8BAB4;
  --color-powder-500: #D99088;
  --color-powder-600: #C87870;
  --color-powder-700: #AD625C;

  --color-champagne: #E8DDCB;

  --color-text: #292522;
  --color-text-secondary: #756E69;
  --color-text-muted: #A49B95;

  --color-border: #EAE3DE;
  --color-border-strong: #DED3CD;

  /* Ink hover + overlay veils: the last colour literals that used to live
     inside rules. Every colour in this file now starts here. */
  --color-ink-hover: #3B3632;
  --color-scrim: rgba(41, 37, 34, 0.32);
  --color-overlay: rgba(41, 37, 34, 0.4);

  /* Status families: muted sage / warm champagne-amber / muted terracotta */
  --color-success: #7C9A82;
  --color-success-strong: #5F7F66;
  --color-success-soft: #EFF4EF;
  --color-warning: #B98A2F;
  --color-warning-soft: #F7EFDC;
  --color-error: #C0705F;
  --color-error-strong: #A85A4A;
  --color-error-soft: #F8ECE8;

  /* Typography: one UI stack + one editorial stack, swapped in ONE place */
  --font-ui: "Manrope", "Avenir Next", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-editorial: "Cormorant Garamond", "Iowan Old Style", "Baskerville", "Times New Roman", serif;

  --text-hero: clamp(1.75rem, 1.35rem + 1.5vw, 2.5rem);        /* 28 → 40 */
  --text-page: clamp(1.4375rem, 1.2rem + 0.9vw, 1.875rem);     /* 23 → 30 */
  --text-section: clamp(1.125rem, 1.05rem + 0.3vw, 1.25rem);   /* 18 → 20 */
  --text-card: 1rem;                                            /* 16 */
  --text-body: 1rem;                                            /* 16 */
  --text-meta: 0.8125rem;                                       /* 13 */
  --text-button: 0.9375rem;                                     /* 15 */

  /* Spacing scale (4 → 64) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-7: 32px;
  --space-8: 40px;
  --space-9: 48px;
  --space-10: 64px;
  --space-section: clamp(24px, 4vw, 48px);

  /* Shape */
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 24px;
  --radius-pill: 999px;

  /* Shadows: very soft, never a material stack */
  --shadow-soft: 0 1px 2px rgba(41, 37, 34, 0.04), 0 8px 24px rgba(41, 37, 34, 0.05);
  --shadow-lift: 0 2px 6px rgba(41, 37, 34, 0.06), 0 16px 40px rgba(41, 37, 34, 0.1);
  --shadow-overlay: 0 24px 80px rgba(41, 37, 34, 0.18);
  --focus-ring: 0 0 0 3px var(--color-powder-100);

  /* Shell geometry */
  --sidebar-w: clamp(220px, 18vw, 280px);
  --rail-w: clamp(300px, 26vw, 390px);
  --prose-w: 800px;
  --topbar-h: 52px;
  --bottomnav-h: 58px;

  /* Mobile keyboard: the ONLY value the tiny visualViewport handler writes.
     Layout stays pure CSS; JS never measures or positions anything. */
  --keyboard-inset: 0px;
}
/* == DESIGN TOKENS END == */

/* ==========================================================================
   Base
   ========================================================================== */

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

/* Every primitive below sets an explicit `display`, which would otherwise
   override the user-agent's `[hidden] { display: none }` and leave hidden
   elements on screen. This rule is what makes `hidden` reliable everywhere. */
[hidden] { display: none !important; }

html { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-ui);
  font-size: var(--text-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;
}

h1, h2, h3, p, ul { margin: 0; }
ul { padding: 0; list-style: none; }
a { color: var(--color-powder-700); }

button {
  font-family: inherit;
  font-size: var(--text-button);
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

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

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: var(--space-2);
  z-index: 200;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-4);
}
.skip-link:focus { left: var(--space-2); }

/* ==========================================================================
   Primitives: buttons, badges, inputs
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 10px var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-weight: 600;
  line-height: 1.2;
  min-height: 40px;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, opacity 0.15s ease;
  text-decoration: none;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; flex: none; }

.btn-primary { background: var(--color-text); color: var(--color-bg); }
.btn-primary:hover:not(:disabled) { background: var(--color-ink-hover); }

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

.btn-quiet { background: transparent; color: var(--color-text-secondary); }
.btn-quiet:hover:not(:disabled) { background: var(--color-surface-soft); color: var(--color-text); }

.btn-powder { background: var(--color-powder-600); color: var(--color-surface); }
.btn-powder:hover:not(:disabled) { background: var(--color-powder-700); }

.btn-danger {
  background: var(--color-surface);
  border-color: var(--color-error);
  color: var(--color-error-strong);
}
.btn-danger:hover:not(:disabled) { background: var(--color-error-soft); }

/* Approval language: sage approve, muted terracotta reject outline.
   Used today by video review; future approvals reuse the same pair. */
.btn-approve { background: var(--color-success); color: var(--color-surface); }
.btn-approve:hover:not(:disabled) { background: var(--color-success-strong); }
.btn-reject {
  background: transparent;
  border-color: var(--color-error);
  color: var(--color-error-strong);
}
.btn-reject:hover:not(:disabled) { background: var(--color-error-soft); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  color: var(--color-text-secondary);
  flex: none;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.icon-btn:hover:not(:disabled) { background: var(--color-surface-soft); color: var(--color-text); }
.icon-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.icon-btn svg { width: 20px; height: 20px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: var(--color-powder-50);
  border: 1px solid var(--color-powder-200);
  color: var(--color-powder-700);
  font-size: var(--text-meta);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}
.badge.badge-neutral {
  background: var(--color-surface-soft);
  border-color: var(--color-border);
  color: var(--color-text-secondary);
}
.badge.badge-success {
  background: var(--color-success-soft);
  border-color: var(--color-success);
  color: var(--color-success-strong);
}
.badge.badge-error {
  background: var(--color-error-soft);
  border-color: var(--color-error);
  color: var(--color-error-strong);
}

.input {
  width: 100%;
  padding: 10px var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  font-family: inherit;
  font-size: var(--text-body);
  color: var(--color-text);
}
.input:focus {
  outline: none;
  border-color: var(--color-powder-500);
  box-shadow: var(--focus-ring);
}

/* ==========================================================================
   App shell: sidebar | main | optional rail
   ========================================================================== */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: 100vh;
  grid-template-rows: calc(100dvh - var(--keyboard-inset, 0px));
  height: 100vh;
  height: calc(100dvh - var(--keyboard-inset, 0px));
}
.app-shell.rail-open {
  /* rail becomes a real third column only on wide desktop (see media query);
     below that it stays an overlay drawer */
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

.main {
  min-width: 0; /* content must never blow the grid up */
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--color-bg);
  overflow: hidden;
}

.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.view[hidden] { display: none; }

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-7) var(--space-7) var(--space-5);
}
.page-header-meta {
  color: var(--color-text-muted);
  font-size: var(--text-meta);
  text-align: right;
  white-space: nowrap;
}

.hero {
  font-family: var(--font-editorial);
  font-size: var(--text-hero);
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: 0.01em;
}
.hero-sub {
  margin-top: var(--space-2);
  color: var(--color-text-secondary);
  font-size: var(--text-card);
}

.section { padding: 0 var(--space-7) var(--space-7); }
.section-title {
  font-size: var(--text-section);
  font-weight: 600;
  margin-bottom: var(--space-4);
}

/* ---- Sidebar ---- */

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--space-5) var(--space-4);
  gap: var(--space-4);
  z-index: 60;
}

.brand { display: flex; flex-direction: column; padding: 0 var(--space-2); }
.brand-name {
  font-family: var(--font-editorial);
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.brand-sub {
  font-size: var(--text-meta);
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.sidebar-new-chat { width: 100%; }

.nav { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: var(--space-1); }
.nav-caption {
  font-size: 0.6875rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin: var(--space-4) var(--space-2) var(--space-2);
  font-weight: 600;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: 9px var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  font-size: var(--text-button);
  font-weight: 500;
  text-align: left;
  min-width: 0;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.nav-item svg { width: 18px; height: 18px; flex: none; }
.nav-item:hover { background: var(--color-surface-soft); color: var(--color-text); }
.nav-item.active { background: var(--color-powder-50); color: var(--color-powder-700); }
.nav-item .nav-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.nav-item-quiet { color: var(--color-text-muted); }

.chat-list { display: flex; flex-direction: column; gap: 2px; }
.chat-list .nav-item { font-size: var(--text-meta); color: var(--color-text-secondary); }

.sidebar-footer {
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.profile {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  min-width: 0;
}
.profile-orb {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-pill);
  background: linear-gradient(135deg, var(--color-powder-200), var(--color-champagne));
  color: var(--color-powder-700);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  flex: none;
}
.profile-login {
  font-size: var(--text-meta);
  color: var(--color-text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Scrim / drawer helpers ---- */

.scrim {
  position: fixed;
  inset: 0;
  background: var(--color-scrim);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.scrim.visible { opacity: 1; pointer-events: auto; }

/* ---- Topbar (hidden on desktop) ---- */

.topbar {
  display: none;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  padding-top: calc(var(--space-2) + env(safe-area-inset-top, 0px));
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  min-height: var(--topbar-h);
}
.topbar-brand {
  font-family: var(--font-editorial);
  font-size: 1.25rem;
  font-weight: 600;
}
.topbar-status {
  margin-left: auto;
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 45%;
}

/* ---- Bottom nav (mobile only) ---- */

.bottom-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 70;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.bottom-nav button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--bottomnav-h);
  font-size: 0.6875rem;
  color: var(--color-text-secondary);
  position: relative;
}
.bottom-nav button svg { width: 20px; height: 20px; }
.bottom-nav button.active { color: var(--color-powder-700); }
.bottom-nav .nav-count {
  position: absolute;
  top: 6px;
  right: calc(50% - 22px);
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--color-powder-600);
  color: var(--color-surface);
  font-size: 0.625rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ==========================================================================
   Cards (single family for every present and future module)
   ========================================================================== */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-soft);
  min-width: 0;
}
.card-eyebrow {
  font-size: 0.6875rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  font-weight: 700;
}
.card-title {
  font-size: var(--text-card);
  font-weight: 600;
  margin-top: var(--space-1);
  overflow-wrap: anywhere;
}
.card-meta {
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  margin-top: var(--space-1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-4);
}
.card-actions .btn { min-height: 36px; padding: 7px var(--space-3); font-size: 0.875rem; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(230px, 100%), 1fr));
  gap: var(--space-4);
  padding: 0 var(--space-7) var(--space-6);
}
.card-grid .card-actionable { cursor: pointer; transition: box-shadow 0.15s ease, transform 0.15s ease; }
.card-grid .card-actionable:hover { box-shadow: var(--shadow-lift); transform: translateY(-1px); }

.card-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  margin-left: var(--space-2);
  border-radius: var(--radius-pill);
  background: var(--color-powder-600);
  color: var(--color-surface);
  font-size: 0.75rem;
  font-weight: 700;
}

.resource-list { display: flex; flex-direction: column; gap: var(--space-3); }

.resource-card {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  min-width: 0;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}
.resource-card:hover { border-color: var(--color-powder-300); background: var(--color-surface-soft); }
.resource-card .resource-icon {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-soft);
  border: 1px solid var(--color-border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--color-powder-700);
  flex: none;
  overflow: hidden;
}
.resource-card .resource-icon svg { width: 18px; height: 18px; }
/* A real preview may take the icon slot (a generated keyframe in the
   decisions list); it fills the same 38px square and is never a second size. */
.resource-card .resource-thumb { width: 100%; height: 100%; object-fit: cover; display: block; }
.resource-card .resource-text { min-width: 0; flex: 1; }
.resource-card .resource-title {
  font-weight: 600;
  font-size: var(--text-button);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.resource-card .resource-meta { font-size: var(--text-meta); color: var(--color-text-muted); }
.resource-card .resource-actions { display: flex; gap: var(--space-1); flex: none; }

button.resource-card { width: 100%; text-align: left; cursor: pointer; }

/* In the chat stream a resource row shares the card measure, so a report,
   a plan and a video read as one family rather than three widths. */
.messages > .resource-card { max-width: min(560px, 100%); }

/* ==========================================================================
   Today
   ========================================================================== */

.today-scroll { flex: 1; min-height: 0; overflow-y: auto; padding-bottom: var(--space-8); }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-10) var(--space-6);
  text-align: center;
  color: var(--color-text-secondary);
}
.empty-state .empty-watermark {
  font-family: var(--font-editorial);
  font-size: clamp(2.5rem, 8vw, 4.5rem);
  line-height: 1;
  color: var(--color-powder-200);
  user-select: none;
  white-space: pre-line;
}
.empty-state .empty-question { font-size: var(--text-section); font-weight: 600; color: var(--color-text); }

/* ==========================================================================
   Chat
   ========================================================================== */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-6);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  min-width: 0;
}
.chat-header-titles { min-width: 0; flex: 1; }
.chat-header-agent {
  font-size: 0.6875rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  font-weight: 700;
}
.chat-title {
  font-size: var(--text-button);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-header-meta {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex: none;
  color: var(--color-text-muted);
  font-size: var(--text-meta);
}

.chat-body {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
}
.chat-body.drag-over::after {
  content: "";
  position: absolute;
  inset: var(--space-3);
  border: 2px dashed var(--color-powder-500);
  border-radius: var(--radius-lg);
  background: var(--color-powder-50);
  opacity: 0.85;
  pointer-events: none;
}
.drag-over-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  pointer-events: none;
  font-weight: 600;
  color: var(--color-powder-700);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-5) var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  scroll-behavior: smooth;
}

.date-separator {
  align-self: center;
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  background: var(--color-surface-soft);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  padding: 2px var(--space-3);
  margin: var(--space-2) 0;
}

.message { display: flex; gap: var(--space-3); min-width: 0; }

.message .msg-body { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.msg-meta { font-size: var(--text-meta); color: var(--color-text-muted); }
.msg-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  max-width: min(var(--prose-w), 100%);
}

/* Sources: the visible difference between an answer that went and looked and
   one that came out of the model's memory. Quiet by default (collapsed), so a
   sourced answer reads as calmer rather than noisier - but always present and
   always openable. */
.msg-sources {
  max-width: min(var(--prose-w), 100%);
  font-size: var(--text-meta);
  border-left: 2px solid var(--color-powder-200);
  padding-left: var(--space-3);
}
.msg-sources > summary {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  cursor: pointer;
  color: var(--color-text-muted);
  list-style: none;
}
.msg-sources > summary::-webkit-details-marker { display: none; }
.msg-sources > summary:hover { color: var(--color-text); }
.msg-sources > summary svg { width: 14px; height: 14px; flex: none; }
.msg-sources-list {
  margin: var(--space-1) 0 0;
  padding: 0 0 0 var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.msg-sources-list li { list-style: disc; }
.msg-sources-list a { color: var(--color-text-muted); overflow-wrap: anywhere; }
.msg-sources-list a:hover { color: var(--color-text); }

/* The query, once the search reports it. A trailing aside on the living
   phrase - never its own machine-state row. */
.activity-query {
  color: var(--color-text-muted);
  font-size: var(--text-meta);
  overflow-wrap: anywhere;
}

.message.user { justify-content: flex-end; }
.message.user .msg-body { align-items: flex-end; max-width: min(560px, 88%); }
.message.user .msg-content {
  background: var(--color-powder-100);
  border: 1px solid var(--color-powder-200);
  border-radius: var(--radius-lg);
  border-bottom-right-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
}

.message.assistant .orb {
  width: 30px;
  height: 30px;
  margin-top: 2px;
  border-radius: var(--radius-pill);
  background: linear-gradient(135deg, var(--color-powder-300), var(--color-champagne));
  color: var(--color-powder-700);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  flex: none;
}

.msg-actions { display: flex; gap: var(--space-1); align-items: center; min-height: 32px; }
.msg-action-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  padding: 5px var(--space-2);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}
.msg-action-btn svg { width: 14px; height: 14px; }
.msg-action-btn:hover { color: var(--color-text); background: var(--color-surface-soft); border-color: var(--color-border); }
.msg-action-btn.copied { color: var(--color-success-strong); }

.activity-line {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-6);
  color: var(--color-text-secondary);
  font-size: var(--text-button);
}
.activity-line .orb {
  width: 24px;
  height: 24px;
  border-radius: var(--radius-pill);
  background: linear-gradient(135deg, var(--color-powder-300), var(--color-champagne));
  color: var(--color-powder-700);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  flex: none;
  animation: orb-breathe 2.6s ease-in-out infinite;
}
@keyframes orb-breathe {
  0%, 100% { transform: scale(1); opacity: 0.85; }
  50% { transform: scale(1.12); opacity: 1; }
}

.skeleton {
  border-radius: var(--radius-md);
  background: linear-gradient(100deg, var(--color-surface-soft) 40%, var(--color-powder-50) 50%, var(--color-surface-soft) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.6s linear infinite;
  height: 14px;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---- Attachments tray ---- */

.attachments-tray {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6) 0;
}
.attachment-card {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  max-width: min(340px, 100%);
  min-width: 0;
}
.attachment-thumb {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--color-surface-soft);
  border: 1px solid var(--color-border);
  flex: none;
}
.attachment-icon {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-soft);
  border: 1px solid var(--color-border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-secondary);
  flex: none;
}
.attachment-text { min-width: 0; }
.attachment-name {
  font-size: var(--text-meta);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 200px;
}
.attachment-meta { font-size: 0.6875rem; color: var(--color-text-muted); display: flex; gap: var(--space-2); }
.attachment-ready { color: var(--color-success-strong); font-weight: 600; }
.attachment-upload-error { color: var(--color-error-strong); }

/* ---- Composer ---- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6);
  padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
  background: var(--color-bg);
  border-top: 1px solid var(--color-border);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 180px;
  padding: 10px var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  font-family: inherit;
  font-size: var(--text-body);
  line-height: 1.4;
  color: var(--color-text);
}
.composer textarea:focus {
  outline: none;
  border-color: var(--color-powder-500);
  box-shadow: var(--focus-ring);
}
.composer textarea:disabled { opacity: 0.6; }

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-pill);
  background: var(--color-powder-600);
  color: var(--color-surface);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  transition: background-color 0.15s ease, opacity 0.15s ease;
}
.send-btn svg { width: 20px; height: 20px; }
.send-btn:hover:not(:disabled) { background: var(--color-powder-700); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.run-status {
  padding: var(--space-2) var(--space-6);
  font-size: var(--text-meta);
  color: var(--color-text-secondary);
  background: var(--color-surface-soft);
  border-top: 1px solid var(--color-border);
}

/* ==========================================================================
   Artifacts: content plan + media (provider-neutral)
   ========================================================================== */

.plan-card { display: flex; flex-direction: column; gap: var(--space-3); max-width: min(560px, 100%); }
.plan-card .card-badges { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-top: var(--space-2); }

.media-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  box-shadow: var(--shadow-soft);
  max-width: min(460px, 100%);
  min-width: 0;
}
.media-card .media-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.media-card .media-title { font-weight: 600; font-size: var(--text-button); }
.media-card .media-status { font-size: var(--text-meta); color: var(--color-text-secondary); }
.media-card .media-status.status-working { color: var(--color-text-secondary); }
.media-card .media-status.status-ready { color: var(--color-success-strong); font-weight: 600; }
.media-card .media-status.status-error { color: var(--color-error-strong); font-weight: 600; }
.media-card video {
  width: 100%;
  max-width: 100%;
  border-radius: var(--radius-md);
  background: #000;
  display: block;
}
/* Keyframes (Task 1): the SAME card, a still frame instead of a clip.
   2/3 is the real geometry of the generated file (1024x1536), so the card
   reserves the right space before the image arrives and nothing jumps. */
.media-card .media-image {
  width: 100%;
  max-width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: var(--radius-md);
  background: var(--color-surface-soft);
  display: block;
}
/* The 40-90 second wait is SHOWN, not implied: the existing shimmer
   placeholder occupies exactly the space the frame will take. Without it the
   screen looks frozen and the owner presses the button a second time - which
   is a second paid generation. */
.media-card .media-wait {
  width: 100%;
  height: auto;
  aspect-ratio: 2 / 3;
}
.media-card .media-meta { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.media-card .media-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.media-card .media-note { font-size: var(--text-meta); color: var(--color-text-muted); }
/* The ambiguous submission window: an explanation plus, at most, the one
   human action that lifts the block. Set apart from the rest of the card so
   it reads as a warning rather than as another control. */
.media-card .media-unresolved {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px solid var(--color-error);
  border-radius: var(--radius-md);
  background: var(--color-error-soft);
}
.media-card .media-unresolved .media-note { color: var(--color-text-secondary); }

.provider-badge {
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-pill);
  padding: 2px 8px;
}

/* ==========================================================================
   Overlays: drawer, sheet, rail, modal, palette, toast
   ========================================================================== */

.drawer, .sheet, .rail {
  position: fixed;
  background: var(--color-surface);
  z-index: 80;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-overlay);
}

.drawer {
  top: 0;
  bottom: 0;
  left: 0;
  width: min(320px, calc(100vw - 48px));
  border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
  transform: translateX(-102%);
  transition: transform 0.22s ease;
  padding: var(--space-5) var(--space-4);
}
.drawer.open { transform: translateX(0); }

.sheet {
  left: 0;
  right: 0;
  bottom: 0;
  top: auto;
  max-height: calc(100dvh - 24px);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  transform: translateY(102%);
  transition: transform 0.22s ease;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.sheet.open { transform: translateY(0); }
.sheet.sheet-full { top: calc(var(--space-4) + env(safe-area-inset-top, 0px)); max-height: none; }

.rail {
  top: 0;
  bottom: 0;
  right: 0;
  width: min(var(--rail-w), calc(100vw - 48px));
  border-radius: var(--radius-lg) 0 0 var(--radius-lg);
  transform: translateX(102%);
  transition: transform 0.22s ease;
}
.rail.open { transform: translateX(0); }

.rail-header, .sheet-header, .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  min-width: 0;
}
.rail-title, .sheet-title, .modal-title {
  font-size: var(--text-card);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rail-body, .sheet-body, .modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-5);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: var(--text-button);
  line-height: 1.55;
}
.rail-actions, .sheet-actions {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--color-border);
}
.rail-actions .btn, .sheet-actions .btn { flex: 1; }

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: var(--color-overlay);
}
.modal {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-overlay);
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  display: flex;
  flex-direction: column;
}
.modal-header { border-bottom: none; padding-bottom: var(--space-2); }
.modal-body { padding: 0 var(--space-5); white-space: normal; }

/* The product photo a paid generation will be built from. Bounded in height
   so a tall portrait photo cannot push the confirm button off screen - the
   picture is there to be recognised, not examined. */
.modal-figure { margin: 0 0 var(--space-3); }
.modal-figure-image {
  display: block;
  max-width: 100%;
  max-height: 220px;
  width: auto;
  margin: 0 auto;
  border-radius: var(--radius-md);
  object-fit: contain;
}
.modal-figure-caption {
  margin-top: var(--space-2);
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  text-align: center;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5) var(--space-5);
}

.palette-root {
  position: fixed;
  inset: 0;
  z-index: 95;
  background: var(--color-overlay);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12vh var(--space-4) var(--space-4);
}
.palette {
  width: min(540px, calc(100vw - 32px));
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-overlay);
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 16vh);
}
.palette input {
  border: none;
  border-bottom: 1px solid var(--color-border);
  padding: var(--space-4) var(--space-5);
  font-family: inherit;
  font-size: var(--text-card);
  background: transparent;
  color: var(--color-text);
}
.palette input:focus { outline: none; }
.palette-list { overflow-y: auto; padding: var(--space-2); }
.palette-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  text-align: left;
  padding: 10px var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  min-width: 0;
}
.palette-item svg { width: 18px; height: 18px; flex: none; }
.palette-item .nav-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-item.selected, .palette-item:hover { background: var(--color-powder-50); color: var(--color-text); }

.toasts {
  position: fixed;
  top: calc(var(--space-3) + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 120;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  align-items: center;
  width: max-content;
  max-width: calc(100vw - 32px);
  pointer-events: none;
}
.toast {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--color-text);
  color: var(--color-bg);
  padding: 10px var(--space-4);
  border-radius: var(--radius-pill);
  font-size: var(--text-meta);
  font-weight: 600;
  box-shadow: var(--shadow-lift);
  animation: toast-in 0.2s ease;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.toast svg { width: 14px; height: 14px; flex: none; }
.toast.toast-error { background: var(--color-error-strong); color: var(--color-surface); }
.toast.toast-success { background: var(--color-success-strong); color: var(--color-surface); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* ==========================================================================
   Login
   ========================================================================== */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: var(--space-4);
  background:
    radial-gradient(60% 40% at 80% 0%, var(--color-powder-50), transparent),
    var(--color-bg);
}
.login-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lift);
  padding: var(--space-8);
  width: min(360px, 100%);
}
.login-card h1 {
  font-family: var(--font-editorial);
  font-size: 1.75rem;
  font-weight: 600;
  margin-bottom: var(--space-5);
}
.login-card label {
  display: block;
  margin-top: var(--space-4);
  font-size: var(--text-meta);
  color: var(--color-text-secondary);
  font-weight: 600;
}
.login-card input { margin-top: var(--space-1); }
.login-card button[type="submit"] { width: 100%; margin-top: var(--space-6); }
.error-text { color: var(--color-error-strong); font-size: var(--text-meta); margin-top: var(--space-3); }

/* ==========================================================================
   UI V1.1 — navigation & chat organization
   No new breakpoint, no new colour, no new overlay system: folders, the
   "•••" menu and the trash reuse the existing tokens and the modal shell.
   ========================================================================== */

/* ---- Sidebar: folders, chat rows, drop targets ---- */

.nav-caption-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.icon-btn-sm { width: 28px; height: 28px; }
.icon-btn-sm svg { width: 16px; height: 16px; }
.icon-btn-danger { color: var(--color-error); }
.icon-btn-danger:hover:not(:disabled) { background: var(--color-error-soft); color: var(--color-error-strong); }

.folder-list { display: flex; flex-direction: column; gap: 2px; }
.folder-row { border-radius: var(--radius-md); }
.folder-head { display: flex; align-items: center; gap: var(--space-1); min-width: 0; }
.folder-toggle { flex: 1; min-width: 0; }
.folder-toggle svg { width: 18px; height: 18px; flex: none; }
.folder-count {
  margin-left: auto;
  flex: none;
  font-size: var(--text-meta);
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}
.folder-chats {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-left: var(--space-5);
}

.chat-row {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  min-width: 0;
  border-radius: var(--radius-md);
}
.chat-row .nav-item { flex: 1; min-width: 0; font-size: var(--text-meta); }
.chat-row.dragging { opacity: 0.45; }

/* The "•••" is NEVER hover-only: it is the single path to rename / move /
   delete, so it stays visible (muted) and only gains contrast on hover. */
.row-menu-btn { flex: none; color: var(--color-text-muted); }
.row-menu-btn:hover:not(:disabled) { color: var(--color-text); }

/* Drop feedback: calm, and readable without relying on colour alone. */
.drop-target {
  background: var(--color-powder-50);
  outline: 1px dashed var(--color-powder-300);
  outline-offset: -1px;
}

.nav-empty-action { width: 100%; justify-content: flex-start; font-weight: 500; }

.nav-hint {
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-meta);
  color: var(--color-text-muted);
}
.nav-trash-item { margin-top: var(--space-4); }

/* ---- Chat header: the visual Back (compact / phone only) ---- */

.chat-back-btn { display: none; }

.trashed-note {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-6);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface-soft);
  color: var(--color-text-secondary);
  font-size: var(--text-meta);
}

/* ---- "•••" menu: a popover panel on desktop, a sheet on a phone ---- */

.menu-root {
  position: fixed;
  inset: 0;
  z-index: 92;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: var(--color-overlay);
}
.menu {
  width: min(360px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-overlay);
  display: flex;
  flex-direction: column;
}
.menu-list {
  display: flex;
  flex-direction: column;
  padding: var(--space-2);
  overflow-y: auto;
}
.menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 44px;
  padding: var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text);
  text-align: left;
  min-width: 0;
}
.menu-item svg { width: 18px; height: 18px; flex: none; }
.menu-item:hover { background: var(--color-surface-soft); }
/* Destructive language stays muted terracotta - never a full-bleed red. */
.menu-item-danger { color: var(--color-error-strong); }
.menu-item-danger:hover { background: var(--color-error-soft); }
.menu-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

/* ---- Prompt field (rename chat / folder, new folder) ---- */

.field { display: flex; flex-direction: column; gap: var(--space-2); }
.textarea { min-height: 104px; resize: vertical; line-height: 1.5; }
.field-label { font-size: var(--text-meta); color: var(--color-text-secondary); font-weight: 600; }
.field-error { font-size: var(--text-meta); color: var(--color-error-strong); }

/* ---- Folder picker (the mobile "Переместить" flow) ---- */

.picker-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2) var(--space-3);
  max-height: 50dvh;
  overflow-y: auto;
}
.picker-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 44px;
  padding: var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text);
  text-align: left;
  min-width: 0;
}
.picker-item svg { width: 18px; height: 18px; flex: none; }
.picker-item:hover { background: var(--color-surface-soft); }
.picker-item-current { background: var(--color-powder-50); color: var(--color-powder-700); }
.picker-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-mark { flex: none; color: var(--color-powder-600); }

/* ==========================================================================
   Responsive: compact (tablet / narrow desktop)
   ========================================================================== */

.topbar-menu-btn { display: none; }

@media (max-width: 1199px) {
  .app-shell, .app-shell.rail-open {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
  }

  .topbar { display: flex; }
  .topbar-menu-btn { display: inline-flex; }

  /* sidebar becomes an off-canvas drawer */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(320px, calc(100vw - 48px));
    border-right: 1px solid var(--color-border);
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    transform: translateX(-102%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-overlay);
  }
  .sidebar.open { transform: translateX(0); }

  .page-header { padding: var(--space-6) var(--space-5) var(--space-4); }
  .section { padding: 0 var(--space-5) var(--space-6); }
  .card-grid { padding: 0 var(--space-5) var(--space-5); }
  .messages { padding: var(--space-4); }
  .composer { padding: var(--space-3) var(--space-4); }
  .chat-header { padding: var(--space-3) var(--space-4); }
  .attachments-tray { padding: var(--space-3) var(--space-4) 0; }
  .activity-line { padding: var(--space-2) var(--space-4); }
  .trashed-note { padding: var(--space-3) var(--space-4); }

  /* The user can genuinely get lost here: the sidebar is off-canvas, so the
     chat header gets a real Back control. On wide desktop it stays hidden -
     the topbar must not become a second browser. */
  .chat-back-btn { display: inline-flex; }
}

/* ==========================================================================
   Responsive: mobile (< 768px) — touch-first composition
   ========================================================================== */

@media (max-width: 767px) {
  body { font-size: 16px; }

  .composer textarea,
  .input,
  .palette input,
  .login-card input { font-size: 16px; /* Safari anti-zoom */ }

  .bottom-nav { display: grid; }

  .main { padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px)); }

  .icon-btn { width: 44px; height: 44px; }
  .send-btn { width: 48px; height: 48px; }
  .msg-action-btn { min-height: 44px; }
  .btn { min-height: 44px; }

  /* The bottom bar is fixed above the drawer, so the drawer's own footer
     (profile / выйти) must clear it instead of hiding underneath. */
  .sidebar { padding-bottom: calc(var(--bottomnav-h) + var(--space-4) + env(safe-area-inset-bottom, 0px)); }

  .page-header { flex-direction: column; align-items: flex-start; gap: var(--space-2); }
  .page-header-meta { text-align: left; }

  .message.user .msg-body { max-width: 94%; }

  /* approval / media actions stack when narrow */
  .media-card .media-actions .btn { flex: 1 1 auto; }
  .media-card { max-width: 100%; }

  /* sheets take the full screen on phones */
  .sheet { top: calc(var(--space-3) + env(safe-area-inset-top, 0px)); max-height: none; border-radius: var(--radius-xl); }
  .drawer { width: min(320px, calc(100vw - 32px)); }

  .rail {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: calc(100dvh - 24px);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    transform: translateY(102%);
  }
  .rail.open { transform: translateY(0); }

  .chat-header-meta .clock { display: none; }

  /* The action menu and the folder picker become bottom sheets: reachable
     with a thumb, and never covered by the on-screen keyboard. */
  .menu-root { align-items: flex-end; padding: 0; }
  .menu {
    width: 100%;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    max-height: calc(100dvh - var(--keyboard-inset) - var(--space-8));
  }
  .modal-root { align-items: flex-end; padding: 0; }
  .modal {
    width: 100%;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    max-height: calc(100dvh - var(--keyboard-inset) - var(--space-8));
  }

  .row-menu-btn { width: 44px; height: 44px; }
  .folder-chats { padding-left: var(--space-4); }
}

/* Landscape phones: functional first */
@media (max-height: 520px) and (orientation: landscape) {
  .bottom-nav { display: none; }
  .main { padding-bottom: 0; }
  .messages { padding: var(--space-3); }
}

/* Wide desktop: context rail as persistent third column */
@media (min-width: 1440px) {
  .app-shell.rail-open {
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--rail-w);
  }
  .app-shell.rail-open .rail {
    position: static;
    width: auto;
    transform: none;
    box-shadow: none;
    border-left: 1px solid var(--color-border);
    border-radius: 0;
    z-index: auto;
  }
}

/* ==========================================================================
   Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
