@media (max-width: 980px) {
  .app-shell { grid-template-columns: 66px 278px minmax(0, 1fr); }
  .rail { padding-inline: 7px; }
  .rail-button.active::before { left: -7px; }
  .messages { padding-inline: 18px; }
  .content-body { padding: 22px; }
  .profile-fields { grid-template-columns: 1fr 1fr; }
  .profile-fields .primary-button { grid-column: 1 / -1; }
}

@media (max-width: 720px) {
  .auth-screen {
    width: 100%;
    height: 100dvh;
    min-height: 0;
    place-items: start center;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    padding: max(16px, env(safe-area-inset-top)) 12px max(32px, env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }

  .auth-panel {
    margin: auto 0;
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) 58px;
  }

  .rail {
    grid-row: 2;
    flex-direction: row;
    justify-content: space-around;
    gap: 0;
    padding: 3px max(6px, env(safe-area-inset-right)) max(3px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    border-top: 1px solid var(--line);
    border-right: 0;
  }

  .brand-button,
  .rail-avatar { display: none; }
  .rail-main {
    flex: initial;
    width: auto;
    display: contents;
    padding: 0;
  }

  .rail-button {
    width: 62px;
    height: 46px;
  }

  .rail-button.active::before {
    top: -5px;
    left: 50%;
    width: 24px;
    height: 3px;
    transform: translateX(-50%);
    border-radius: 0 0 2px 2px;
  }

  .sidebar,
  .workspace {
    grid-row: 1;
    grid-column: 1;
  }

  .sidebar {
    position: relative;
    z-index: 2;
    border-right: 0;
  }

  .workspace {
    z-index: 1;
  }

  .app-shell.mobile-workspace .sidebar { display: none; }
  .app-shell.mobile-workspace .workspace { z-index: 3; }

  .mobile-only { display: grid; }
  .chat-view { grid-template-rows: 58px minmax(0, 1fr) auto auto auto; }
  .chat-header { padding: 0 8px; }
  .chat-header > .avatar { display: none; }
  .messages { padding: 14px 12px 10px; }
  .message-date-divider,
  .unread-divider { margin: 8px 0 14px; }
  .message-row { grid-template-columns: 32px minmax(0, 1fr); }
  .message-row.own { grid-template-columns: minmax(0, 1fr) 32px; }
  .message-row > .avatar { width: 32px; height: 32px; }
  .message-row { margin-bottom: 12px; }
  .message-row:has(+ .message-row.grouped),
  .message-row.grouped { margin-bottom: 4px; }
  .message-author { margin-bottom: 3px; }
  .message-bubble {
    padding: 8px 10px;
    font-size: calc(13px + var(--font-size-delta));
    line-height: 1.5;
    -webkit-touch-callout: none;
  }
  .message-image { max-height: 280px; }
  .message-gallery .message-image { height: 130px; }
  .sticker-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .message-actions {
    position: fixed;
    top: auto;
    right: 12px;
    bottom: calc(68px + env(safe-area-inset-bottom));
    left: 12px;
    z-index: 40;
    display: none;
    justify-content: space-around;
    gap: 2px;
    margin: 0;
    padding: 8px;
    overflow-x: auto;
    border: 1px solid #3a434f;
    border-radius: 8px;
    background: #252c34;
    box-shadow: 0 14px 38px rgba(0, 0, 0, .48);
    opacity: 1;
    pointer-events: auto;
  }
  .message-row.actions-visible .message-actions { display: flex; }
  .message-row.own .message-actions { right: 12px; left: 12px; }
  .message-actions button {
    min-width: 52px;
    min-height: 48px;
    flex: 1 0 52px;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    padding: 4px 6px;
    font-size: calc(10px + var(--font-size-delta));
  }
  .message-actions .lucide { width: 18px; height: 18px; }
  .composer { min-height: 58px; padding: 8px 9px; }
  .composer textarea { min-height: 40px; }
  .composer .icon-button,
  .send-button { width: 38px; flex-basis: 38px; }
  .content-header { padding-inline: 16px; }
  .content-body { padding: 20px 16px 86px; }
  .profile-editor { grid-template-columns: 1fr; }
  .profile-fields,
  .inline-form { grid-template-columns: 1fr; }
  .inline-form.bot-create-form { grid-template-columns: 1fr; }
  .inline-form.password-grid { grid-template-columns: 1fr; }
  .app-shell.deletion-mode {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) 58px;
  }
  .file-toolbar { grid-template-columns: auto 1fr; }
  .file-toolbar input { grid-column: 1 / -1; }
  .profile-editor .avatar { margin: 0 auto; }
  .section-heading { align-items: flex-start; }
  .group-tools { justify-content: flex-start; }
  .data-table { display: block; overflow-x: auto; }
  .modal-backdrop { padding: 0; place-items: end center; }
  .modal {
    width: 100%;
    max-height: 92vh;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 8px 8px 0 0;
  }
}

.bot-command-panel {
  position: absolute;
  right: 14px;
  bottom: calc(100% + 6px);
  left: 14px;
  z-index: 23;
  width: min(520px, calc(100% - 28px));
  max-height: min(390px, 48vh);
  overflow-y: auto;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-2);
  box-shadow: 0 14px 38px rgba(0, 0, 0, .42);
}

.bot-command-option {
  width: 100%;
  min-height: 52px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.bot-command-option:hover,
.bot-command-option.active {
  background: var(--accent-soft);
}

.bot-command-option:disabled {
  opacity: .52;
  cursor: not-allowed;
}

.bot-command-option > .avatar {
  width: 34px;
  height: 34px;
  flex-basis: 34px;
}

.bot-command-panel-icon {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 5px;
  color: var(--muted);
  background: var(--surface-3);
}

.bot-command-panel-icon .lucide { width: 17px; height: 17px; }

.bot-command-option-copy {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.bot-command-option-copy strong,
.bot-command-option-copy small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bot-command-option-copy strong { font-size: calc(13px + var(--font-size-delta)); }
.bot-command-option-copy small { color: var(--muted); font-size: calc(11px + var(--font-size-delta)); }

.bot-command-scope {
  color: var(--faint);
  font-size: calc(10px + var(--font-size-delta));
}

.message-bubble:has(> .bot-message-card),
.message-row.own .message-bubble:has(> .bot-message-card) {
  padding: 0;
  border-color: transparent;
  background: transparent;
}

.bot-message-card {
  width: min(460px, 68vw);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--text);
  white-space: normal;
}

.bot-message-card-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 15px 10px;
}

.bot-message-card-heading strong {
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: calc(15px + var(--font-size-delta));
}

.bot-message-card-image {
  display: block;
  width: 100%;
  max-height: 260px;
  object-fit: cover;
  cursor: zoom-in;
}

.bot-message-card-content {
  margin: 0;
  padding: 0 15px 13px;
  color: var(--muted);
  font-size: calc(12px + var(--font-size-delta));
  line-height: 1.55;
}

.bot-message-card-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  padding: 10px;
  border-top: 1px solid var(--line-soft);
}

.bot-message-card-action {
  min-width: 0;
  min-height: 46px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 16px;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--surface-3);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.bot-message-card-action:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.bot-message-card-action.primary {
  border-color: rgba(47, 179, 188, .5);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.bot-message-card-action.danger { color: var(--red); }
.bot-message-card-action.loading svg { animation: spin .8s linear infinite; }
.bot-message-card-action:disabled {
  opacity: .5;
  cursor: not-allowed;
  border-color: var(--line);
  background: var(--surface-2);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.bot-message-card-action > span {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.bot-message-card-action strong,
.bot-message-card-action small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bot-message-card-action strong { font-size: calc(12px + var(--font-size-delta)); }
.bot-message-card-action small { color: var(--muted); font-size: calc(10px + var(--font-size-delta)); }

.bot-command-form { width: 100%; min-width: 0; }

.bot-command-builtin {
  min-height: 46px;
  display: grid;
  grid-template-columns: 30px auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: var(--surface-2);
}

.bot-command-builtin span { color: var(--muted); font-size: calc(11px + var(--font-size-delta)); }
.bot-command-editor-list { display: grid; gap: 8px; }

.bot-command-editor-row {
  display: grid;
  grid-template-columns: 16px minmax(100px, .65fr) minmax(110px, .75fr) minmax(150px, 1.2fr) 104px 98px 76px 38px;
  align-items: center;
  gap: 7px;
}

.bot-command-editor-row input,
.bot-command-editor-row select { min-width: 0; }
.bot-command-slash { color: var(--accent-strong); font-weight: 700; text-align: center; }
.bot-command-remove { width: 38px; height: 38px; color: var(--red); }
.bot-command-add { justify-self: start; display: inline-flex; align-items: center; gap: 7px; }

.bot-detail { display: grid; gap: 18px; }
.bot-detail-hero { display: flex; align-items: center; gap: 14px; }
.bot-detail-hero > .avatar { width: 68px; height: 68px; flex: 0 0 68px; }
.bot-detail-copy { min-width: 0; display: grid; gap: 5px; }
.bot-detail-name { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.bot-detail-name h3 { margin: 0; font-size: calc(18px + var(--font-size-delta)); }
.bot-detail-description { margin: 4px 0 0; color: var(--muted); line-height: 1.55; }
.bot-detail-commands { display: grid; gap: 7px; }
.bot-detail-commands h4 { margin: 0 0 3px; }
.bot-detail-command {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 50px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
}
.bot-detail-command > span:first-child { min-width: 0; display: grid; gap: 3px; }
.bot-detail-command small { color: var(--muted); }

.group-bot-list { display: grid; gap: 10px; }
.group-bot-item {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-2);
}
.group-bot-item.disabled > .group-bot-header { opacity: .65; }
.group-bot-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.group-bot-profile {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0;
  border: 0;
  color: inherit;
  background: transparent;
  text-align: left;
}
.group-bot-profile > .avatar { width: 42px; height: 42px; flex: 0 0 42px; }
.group-bot-profile > span { min-width: 0; display: grid; gap: 3px; }
.group-bot-profile strong { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
.group-bot-profile small { overflow: hidden; color: var(--muted); text-overflow: ellipsis; white-space: nowrap; }
.group-bot-settings { display: grid; gap: 12px; margin-top: 13px; padding-top: 12px; border-top: 1px solid var(--line-soft); }
.group-bot-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.group-bot-enabled { padding: 0; }
.group-bot-enabled input[type="checkbox"],
.group-bot-command-row input[type="checkbox"] {
  width: 18px;
  min-width: 18px;
  height: 18px;
  min-height: 18px;
  margin: 0;
  padding: 0;
  accent-color: var(--accent);
}
.group-bot-header .status-label { flex: 0 0 auto; white-space: nowrap; }
.group-bot-command-settings { display: grid; gap: 5px; margin: 0; padding: 0; border: 0; }
.group-bot-command-settings legend { margin-bottom: 6px; color: var(--muted); font-size: calc(11px + var(--font-size-delta)); }
.group-bot-command-row { display: flex; align-items: center; gap: 9px; min-height: 38px; }
.group-bot-command-row > span { min-width: 0; display: grid; gap: 2px; }
.group-bot-command-row small { color: var(--muted); }
.group-bot-command-summary { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.group-bot-command-summary span { padding: 4px 7px; border: 1px solid var(--line); border-radius: 5px; color: var(--muted); }
.bot-parameter-form { min-width: min(420px, 78vw); }
