/* Responsive rules, loaded last so they override component and page styles. */

/* =====================================================================
   Responsive layout
   Breakpoints: ≤1024 px tablet (off-canvas sidebar), ≤640 px phone.
   ===================================================================== */

.topbar__menu,
.topbar__brand,
.sidebar__close,
.action-bar__compact,
.activity__filter-toggle,
.menu__item--compact,
.menu__separator.menu__item--compact {
  display: none;
}

.action-bar,
.action-bar__wide {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.topbar__prefs {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.action-bar__compact {
  gap: var(--space-2);
  align-items: center;
}

.shell__backdrop {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: var(--overlay);
  animation: fade-in var(--duration) var(--ease);
}

/* Tabs: horizontal scroll with fade edges */
.tabs {
  scroll-behavior: smooth;
}

.tabs.has-more-end {
  mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);
}

.tabs.has-more-start {
  mask-image: linear-gradient(to right, transparent, #000 40px);
}

.tabs.has-more-start.has-more-end {
  mask-image: linear-gradient(to right, transparent, #000 40px, #000 calc(100% - 40px), transparent);
}

/* Long ids and URLs never overflow */
code,
.meta-code,
.domains__host,
.cell-sub,
.kv dd {
  overflow-wrap: anywhere;
}

/* ---------- Tablet and below (≤1024 px) ---------- */
@media (max-width: 1024px) {
  .shell {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 80;
    width: min(300px, 86vw);
    height: 100dvh;
    padding-top: calc(var(--space-4) + env(safe-area-inset-top));
    padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom));
    padding-left: calc(var(--space-3) + env(safe-area-inset-left));
    box-shadow: var(--shadow-lg);
    transform: translateX(-105%);
    transition: transform var(--duration) var(--ease);
    visibility: hidden;
  }

  .shell.is-nav-open .sidebar {
    transform: none;
    visibility: visible;
  }

  .sidebar__close {
    display: inline-flex;
    position: absolute;
    top: calc(var(--space-3) + env(safe-area-inset-top));
    right: var(--space-2);
  }

  .topbar__menu,
  .topbar__brand {
    display: inline-flex;
  }

  .topbar__brand img {
    width: 28px;
    height: 28px;
    object-fit: contain;
  }

  .topbar {
    padding-inline: calc(var(--space-4) + env(safe-area-inset-left)) calc(var(--space-4) + env(safe-area-inset-right));
  }

  .topbar__search {
    width: min(280px, 34vw);
  }

  .topbar__user-name {
    display: none;
  }

  .content,
  .shell__banner {
    padding-inline: var(--space-5);
  }

  /* Five tiles on a six-column grid: two wide, then three. */
  .summary--5 {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .summary--5 > * {
    grid-column: span 2;
  }

  .summary--5 > :nth-child(-n + 2) {
    grid-column: span 3;
  }

  .home__row,
  .overview {
    grid-template-columns: minmax(0, 1fr);
  }

  .compare__controls {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .compare__controls > :first-child {
    grid-column: 1 / -1;
  }

  .project__header {
    flex-wrap: wrap;
  }

  .env__head {
    display: none;
  }

  .env__row {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .env__cell--value {
    grid-column: 1 / -1;
    grid-row: 2;
  }
}

/* ---------- Phone (≤640 px) ---------- */
@media (max-width: 640px) {
  .topbar {
    gap: var(--space-2);
    padding-inline: calc(var(--space-3) + env(safe-area-inset-left)) calc(var(--space-3) + env(safe-area-inset-right));
  }

  .topbar__start,
  .topbar__end {
    gap: var(--space-1);
    min-width: 0;
  }

  .topbar__prefs,
  .topbar__mock {
    display: none;
  }

  .topbar__search {
    width: 40px;
    padding: 0;
    justify-content: center;
  }

  .topbar__search-text,
  .topbar__search .kbd {
    display: none;
  }

  .menu__item--compact {
    display: flex;
  }

  .menu__separator.menu__item--compact {
    display: block;
  }

  .content {
    padding: var(--space-4) calc(var(--space-3) + env(safe-area-inset-right)) calc(var(--space-10) + env(safe-area-inset-bottom)) calc(var(--space-3) + env(safe-area-inset-left));
  }

  .shell__banner {
    padding-inline: var(--space-3);
  }

  .page {
    gap: var(--space-4);
  }

  .page-header__title {
    font-size: var(--text-xl);
  }

  .page-header__row {
    flex-direction: column;
    align-items: stretch;
  }

  .page-header__actions {
    width: 100%;
  }

  .page-header__actions .search-box {
    flex: 1;
    min-width: 0;
  }

  .action-bar__wide {
    display: none;
  }

  .action-bar__compact {
    display: flex;
    width: 100%;
  }

  .action-bar__compact > .btn {
    flex: 1;
  }

  .title-toggles .toggle-btn--watch span {
    display: none;
  }

  .summary,
  .summary--5 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  /* The total spans the row; the four statuses pair up below it. */
  .summary--5 > *,
  .summary--5 > :nth-child(-n + 2) {
    grid-column: auto;
  }

  .summary--5 > :first-child {
    grid-column: 1 / -1;
  }

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

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

  .card--flush {
    padding: 0;
  }

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

  .project__meta {
    white-space: normal;
  }

  .card-grid,
  .role-cards {
    grid-template-columns: minmax(0, 1fr);
  }

  .kv__row {
    grid-template-columns: minmax(0, 1fr);
    gap: 2px;
  }

  .compare__controls {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Tables become stacked cards: one "label: value" line per cell. */
  .table-wrap > .table thead,
  .compare__table thead {
    display: none;
  }

  .table-wrap > .table,
  .table-wrap > .table tbody,
  .table-wrap > .table tr,
  .table-wrap > .table td {
    display: block;
    width: 100%;
  }

  .table-wrap > .table tr {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border);
  }

  .table-wrap > .table tbody tr:last-child {
    border-bottom: 0;
  }

  .table-wrap > .table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    min-height: 32px;
    padding: 4px 0 !important;
    border: 0;
    text-align: right;
  }

  .table-wrap > .table td::before {
    content: attr(data-label);
    flex: none;
    max-width: 45%;
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: 600;
    text-align: left;
  }

  .table-wrap > .table td.cell-grow,
  .table-wrap > .table td:first-child {
    display: block;
    text-align: left;
  }

  .table-wrap > .table td.cell-grow::before,
  .table-wrap > .table td:first-child::before {
    display: none;
  }

  .table-wrap > .table td > .cell-stack,
  .table-wrap > .table td > .row-actions {
    justify-items: end;
    text-align: right;
  }

  .table-wrap > .table td:first-child > .cell-stack,
  .table-wrap > .table td.cell-grow > .cell-stack {
    justify-items: start;
    text-align: left;
  }

  .table-wrap > .table td:not([data-label])::before,
  .table-wrap > .table td[data-label=""]::before {
    display: none;
  }

  .table-wrap > .table td:not([data-label]) {
    justify-content: flex-end;
  }

  .table tr.is-error td:first-child,
  .compare__table tr.is-diff td:first-child {
    box-shadow: none;
  }

  .table tr.is-error,
  .compare__table tr.is-diff {
    box-shadow: inset 3px 0 0 var(--warn);
  }

  .table tr.is-error {
    box-shadow: inset 3px 0 0 var(--err);
  }

  .pagination {
    flex-direction: column;
    align-items: stretch;
    padding: var(--space-3);
  }

  .pagination__buttons {
    justify-content: space-between;
  }

  /* Filters collapse behind a "Filters" button. */
  .activity__filter-toggle {
    display: inline-flex;
    justify-self: start;
  }

  .activity__filter-count:not(:empty) {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--radius-full);
    background: var(--primary);
    color: #ffffff;
    font-size: 11px;
    line-height: 20px;
  }

  .activity__filters {
    display: none;
    grid-template-columns: minmax(0, 1fr);
  }

  .activity__filters.is-open {
    display: grid;
  }

  .activity__dates {
    grid-column: auto;
  }

  .activity__date-range {
    flex-wrap: wrap;
  }

  .activity__date-range .input {
    flex: 1;
    min-width: 130px;
  }

  /* Dialogs, drawers and the palette become full-screen sheets. */
  .dialog,
  .dialog--sm,
  .dialog--lg,
  .drawer,
  .drawer--md,
  .palette {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .dialog__header,
  .drawer__header {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--card);
    padding-top: calc(var(--space-4) + env(safe-area-inset-top));
  }

  .dialog__body {
    flex: 1;
  }

  .dialog__footer {
    position: sticky;
    bottom: 0;
    padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  }

  .dialog__footer .btn {
    flex: 1;
  }

  .drawer__header {
    padding-inline: var(--space-4);
  }

  .drawer__body {
    padding: var(--space-3) var(--space-3) calc(var(--space-3) + env(safe-area-inset-bottom));
  }

  .palette__field {
    padding-top: calc(var(--space-3) + env(safe-area-inset-top));
  }

  .palette__footer {
    display: none;
  }

  .bell__panel {
    position: fixed;
    top: calc(var(--topbar-height) + env(safe-area-inset-top));
    left: var(--space-2);
    right: var(--space-2);
    width: auto;
  }

  .menu {
    max-width: calc(100vw - 16px);
  }

  .toasts {
    left: var(--space-3);
    right: var(--space-3);
    bottom: calc(var(--space-3) + env(safe-area-inset-bottom));
    width: auto;
  }

  /* Logs */
  .log {
    min-height: 360px;
  }

  .log__toolbar,
  .log__toolbar-start,
  .log__toolbar-end {
    width: 100%;
  }

  .log__search {
    flex: 1;
  }

  .log__search-input {
    width: 100%;
    min-width: 0;
  }

  .log__scroll {
    font-size: 12px;
  }

  .log__num {
    width: 40px;
  }

  .runtime-logs .log {
    height: calc(100dvh - 260px);
  }

  .env__toolbar .search-box {
    width: 100%;
  }

  .env__toolbar-actions {
    margin-left: 0;
    width: 100%;
  }

  .env__toolbar-actions .btn {
    flex: 1;
  }

  .env__footer {
    flex-wrap: wrap;
  }

  .matrix-wrap {
    max-height: none;
  }

  .matrix__perm {
    min-width: 200px;
    padding-inline: var(--space-3);
  }

  .members__toolbar .btn {
    width: 100%;
  }

  .savebar {
    flex-wrap: wrap;
  }

  .locked__bar {
    padding-inline: var(--space-3);
  }

  .locked__card,
  .session__card,
  .onboarding {
    padding: var(--space-8) var(--space-5) var(--space-6);
  }
}

/* Touch: bigger targets and no hover-only affordances. */
@media (pointer: coarse), (max-width: 640px) {
  .btn,
  .btn--sm,
  .input,
  .select--sm,
  .segmented__item,
  .menu__item,
  .tabs__tab,
  .toggle-btn,
  .topbar__user,
  .topbar__search {
    min-height: 40px;
  }

  .btn--icon,
  .btn--icon.btn--sm {
    min-width: 40px;
  }

  .switch {
    margin-block: 10px;
  }

  .role-chips .btn,
  .domains__ext {
    opacity: 1;
  }

  .checkbox {
    width: 20px;
    height: 20px;
  }
}

/* Grid children of a page never grow past the viewport (tabs and tables
   scroll inside their own containers). */
.page > *,
.tabs__panel,
.app-tabs {
  min-width: 0;
}

@media (max-width: 1024px) {
  .topbar__prefs,
  .topbar__mock {
    display: none;
  }

  .menu__item--compact {
    display: flex;
  }

  .menu__separator.menu__item--compact {
    display: block;
  }
}

@media (max-width: 640px) {
  .log__toolbar-end {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .log__search {
    flex: 1 1 100%;
    order: -1;
  }

  .log__sep {
    display: none;
  }

  .log__toolbar-start .segmented {
    flex: 1;
  }

  .log__toolbar-start .segmented__item {
    flex: 1;
  }
}
