/**
 * Spretta mobile UX — progressive enhancement (iOS Safari, iOS Chrome, Android
 * Chrome, Samsung Internet, mobile Edge/Firefox). vh fallbacks first; dvh where
 * supported. Bottom inset uses measured --sp-console-offset (set in index.html).
 */

@media (max-width: 47.99rem) {
  :root {
    --sp-console-bar-h: 2.75rem;
  }

  /* Pad main content instead of body — avoids iOS Chrome/Safari double-scroll gaps. */
  body.sp-has-console:not(.sp-console-hidden) {
    padding-bottom: 0 !important;
  }

  body.sp-has-console:not(.sp-console-hidden) .sp-app-shell > .grow {
    padding-bottom: calc(
      var(--sp-console-offset, var(--sp-console-bar-h)) + env(safe-area-inset-bottom, 0px)
    );
  }

  body.sp-ai-drawer-open.sp-has-console:not(.sp-console-hidden) .sp-app-shell > .grow {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  body.sp-has-console:not(.sp-console-collapsed):not(.sp-console-hidden) .toast {
    bottom: calc(
      var(--sp-console-offset, var(--sp-console-h, 220px)) + env(safe-area-inset-bottom, 0px) + 1rem
    );
  }

  body.sp-has-console.sp-console-collapsed:not(.sp-console-hidden) .toast {
    bottom: calc(
      var(--sp-console-offset, var(--sp-console-bar-h)) + env(safe-area-inset-bottom, 0px) + 1rem
    );
  }

  .sp-console {
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }

  /* Expanded: fixed height with scrollable log; input + quick chips always pinned at bottom */
  .sp-console:not(.is-collapsed) {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: min(var(--sp-console-h, 280px), 58vh, 100%);
    max-height: min(58vh, 420px);
    min-height: 17rem;
    padding-bottom: 0;
  }

  @supports (height: 1dvh) {
    .sp-console:not(.is-collapsed) {
      height: min(var(--sp-console-h, 280px), 58dvh, 100%);
      max-height: min(58dvh, 420px);
    }
  }

  .sp-console:not(.is-collapsed) .sp-console__metrics {
    flex: 0 0 auto;
    max-height: 5rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sp-console:not(.is-collapsed) .sp-console__body {
    flex: 1 1 0;
    min-height: 2.5rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .sp-console:not(.is-collapsed) .sp-console__quick-actions {
    flex: 0 0 auto;
  }

  .sp-console:not(.is-collapsed) .sp-console__input-row {
    flex: 0 0 auto;
    position: relative;
    z-index: 4;
    padding-bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
    background: #0a0e12;
  }

  .sp-console__input {
    font-size: 16px;
    min-height: 2.75rem;
    line-height: 1.35;
    touch-action: manipulation;
  }

  .sp-console.is-collapsed {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .sp-console__body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* ---- Bottom CLI bar (grid — stable on iOS/Android/Edge) ---- */
  .sp-console__bar {
    display: grid;
    align-items: center;
    gap: 0.35rem 0.5rem;
    padding: 0.4rem 0.5rem;
    min-height: 0;
    box-sizing: border-box;
  }

  .sp-console__brand-btn {
    grid-area: brand;
    justify-self: start;
    min-width: 0;
    min-height: 0;
    height: 2rem;
    padding: 0.2rem 0.35rem;
    touch-action: manipulation;
  }

  .sp-console__summary {
    grid-area: summary;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sp-console__filters {
    grid-area: filters;
    flex: none;
    display: flex;
    flex-wrap: nowrap;
    gap: 0.25rem;
    min-width: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 0.05rem 0;
  }

  .sp-console__filters::-webkit-scrollbar {
    display: none;
  }

  .sp-console__actions {
    grid-area: actions;
    flex-shrink: 0;
    display: flex;
    flex-wrap: nowrap;
    gap: 0.3rem;
    align-items: center;
    justify-self: end;
    margin-left: 0;
  }

  .sp-console__bar .sp-console__icon-btn {
    width: 2rem;
    height: 2rem;
    min-width: 2rem;
    min-height: 2rem;
    touch-action: manipulation;
  }

  .sp-console__bar .sp-console__action {
    min-height: 2rem;
    padding: 0.25rem 0.5rem;
    touch-action: manipulation;
    white-space: nowrap;
  }

  .sp-console__filter {
    flex-shrink: 0;
    min-height: 1.85rem;
    padding: 0.3rem 0.55rem;
    touch-action: manipulation;
  }

  /* Expanded: row1 = CLI + actions, row2 = filter chips */
  .sp-console:not(.is-collapsed) .sp-console__bar {
    flex: 0 0 auto;
    height: auto;
    max-height: none;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto auto;
    grid-template-areas:
      "brand . actions"
      "filters filters filters";
  }

  .sp-console:not(.is-collapsed) .sp-console__summary {
    display: none !important;
  }

  /* Collapsed: single row; hide filters and Clear */
  .sp-console.is-collapsed .sp-console__bar {
    flex: 0 0 var(--sp-console-bar-h);
    height: var(--sp-console-bar-h);
    max-height: var(--sp-console-bar-h);
    padding: 0 0.5rem;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: 1fr;
    grid-template-areas: "brand summary actions";
    overflow: hidden;
  }

  .sp-console.is-collapsed .sp-console__filters,
  .sp-console.is-collapsed .sp-console__action {
    display: none !important;
  }

  .sp-console.is-collapsed .sp-console__summary {
    display: block !important;
  }

  /* ---- Top app header: row1 logo + CLI, row2 account links (no email on phone) ---- */
  .sp-app-shell > header {
    top: max(0.5rem, env(safe-area-inset-top, 0px));
    box-sizing: border-box;
  }

  .sp-app-shell > header .max-w-6xl {
    padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
  }

  .sp-app-topbar {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-items: center;
    column-gap: 0.5rem;
    row-gap: 0.35rem;
    flex-wrap: nowrap !important;
    justify-content: stretch !important;
    min-height: 3.25rem;
    height: auto !important;
    padding: 0.45rem 0.65rem !important;
  }

  .sp-app-topbar__brand {
    grid-column: 1;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
  }

  .sp-app-topbar__nav {
    display: contents !important;
  }

  /* Email only on tablet/desktop — keeps one crowded row off phones */
  .sp-app-topbar__user {
    display: none !important;
  }

  .sp-app-topbar__cli {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    align-items: center;
    justify-self: end;
  }

  .sp-app-topbar__links {
    grid-column: 1 / -1;
    grid-row: 2;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: flex-end;
    align-items: center;
    gap: 0.35rem;
    min-width: 0;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .sp-app-topbar__links.hidden {
    display: none !important;
  }

  .sp-header-logo {
    max-width: 100%;
    overflow: hidden;
  }

  .sp-header-logo__name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sp-console-header-toggle {
    flex-shrink: 0;
    gap: 0.3rem;
    padding: 0.2rem 0.35rem 0.2rem 0.3rem;
  }

  .sp-console-header-toggle .sp-console-header-toggle__label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .sp-console-header-toggle .sp-cli-brand--header {
    gap: 0;
  }

  .sp-app-topbar__links .btn-sm {
    padding: 0.35rem 0.55rem;
    font-size: 0.6875rem;
    line-height: 1.25;
    white-space: nowrap;
  }

  /* Main content: clear fixed Agents tab on the right edge */
  .sp-app-shell > main {
    padding-right: 2.35rem;
    box-sizing: border-box;
  }

  #featureHome .max-w-6xl,
  #featureHome .max-w-4xl,
  #featureHome .max-w-3xl {
    padding-left: 1rem;
    padding-right: 1rem;
    box-sizing: border-box;
  }

  .sp-console__quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: 0.35rem 0.5rem 0;
    border-top: 1px solid #232f3e;
    flex-shrink: 0;
  }

  .sp-console__quick-btn {
    min-height: 2.75rem;
    min-width: 2.75rem;
    padding: 0.35rem 0.65rem;
    font-size: 0.65rem;
    font-family: inherit;
    color: #93c5fd;
    background: #1e293b;
    border: 1px solid #3d4f5f;
    border-radius: 999px;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .sp-console__quick-btn:active {
    background: #334155;
  }

  .sp-console__quick-btn:focus-visible {
    outline: 2px solid #ff9900;
    outline-offset: 2px;
  }

  .sp-console__preview-list {
    margin: 0.35rem 0 0;
    padding-left: 1.1rem;
    font-size: 0.7rem;
    line-height: 1.35;
    color: #cbd5e1;
    list-style: disc;
  }

  .sp-console__preview-list li {
    margin: 0.15rem 0;
  }

  .sp-poker-card {
    min-width: 2.75rem;
    min-height: 2.75rem;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .btn-sm,
  .form-input,
  .form-select {
    touch-action: manipulation;
  }

  body.sp-ai-drawer-open .sp-console {
    visibility: hidden;
    pointer-events: none;
  }

  body.sp-ai-drawer-open .sp-console-fab {
    visibility: hidden;
    pointer-events: none;
  }

  /* ---- Daily status (team view) ---- */
  #teamView {
    overflow-x: hidden;
    max-width: 100%;
  }

  #teamView .sp-app-body {
    padding-top: 5.5rem;
    padding-left: max(1rem, env(safe-area-inset-left, 0px));
    padding-right: max(1rem, calc(env(safe-area-inset-right, 0px) + 2.25rem));
    max-width: 100%;
    box-sizing: border-box;
  }

  #teamView .sp-page-header {
    flex-direction: column;
    align-items: stretch;
  }

  #teamView .sp-page-header > .flex:last-child {
    width: 100%;
    justify-content: stretch;
  }

  #teamView .sp-page-header .btn-sm {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
    text-align: center;
  }

  #teamView .sp-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  #teamView .sp-toolbar-grow {
    min-width: 0;
    width: 100%;
  }

  #teamView .sp-toolbar > .btn-sm {
    width: 100%;
    justify-content: center;
  }

  #teamView .sp-update-meta {
    flex-wrap: wrap;
    row-gap: 0.2rem;
  }

  #teamView .sp-update-meta span {
    word-break: break-word;
  }

  #teamView .sp-update-body {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  #teamView .form-textarea,
  #teamView .form-select {
    max-width: 100%;
    box-sizing: border-box;
  }

  /* ---- Sprint planning & review ---- */
  .sp-sprints-section {
    overflow-x: hidden;
    max-width: 100%;
  }

  .sp-sprints-section .sp-app-body {
    padding-left: max(1rem, env(safe-area-inset-left, 0px));
    padding-right: max(1rem, calc(env(safe-area-inset-right, 0px) + 2.25rem));
    max-width: 100%;
    box-sizing: border-box;
  }

  .sp-sprints-section .sp-page-header {
    flex-direction: column;
    align-items: stretch;
  }

  .sp-sprints-section .sp-page-header > .sp-page-actions {
    width: 100%;
    justify-content: stretch;
  }

  .sp-sprints-section .sp-page-actions .btn-sm,
  .sp-sprints-section .sp-page-actions .sp-menu-wrap {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
  }

  .sp-sprints-section .sp-page-actions .btn-sm {
    width: 100%;
    justify-content: center;
    min-height: 2.75rem;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .sp-sprints-section .sp-page-actions .sp-menu-wrap {
    display: flex;
  }

  .sp-sprints-section .sp-page-actions .sp-menu-wrap > .btn-sm {
    width: 100%;
  }

  .sp-sprints-section .sp-sprint-board-scroll-hint {
    display: block;
  }

  .sp-sprints-section .sp-sprint-board,
  .sp-sprints-section .sp-sprint-review-board {
    margin-left: -0.25rem;
    margin-right: -0.25rem;
    padding-left: 0.25rem;
    padding-right: 0.25rem;
    scroll-snap-type: x proximity;
  }

  .sp-sprints-section .sp-sprint-col {
    flex: 0 0 min(82vw, 16rem);
    min-width: min(82vw, 16rem);
    scroll-snap-align: start;
    max-height: min(55vh, 55dvh, 22rem);
  }

  .sp-sprints-section .sp-sprint-list-item__actions {
    gap: 0.45rem;
  }

  .sp-sprints-section .sp-sprint-list-item__actions .btn-sm {
    flex: 1 1 calc(50% - 0.25rem);
    min-height: 2.75rem;
    justify-content: center;
  }

  .sp-sprints-section .sp-sprint-card__foot {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .sp-sprints-section .sp-sprint-card__move {
    width: 100%;
    min-height: 2.75rem;
  }

  .sp-sprints-section .sp-sprint-col__add {
    min-height: 2.75rem;
    touch-action: manipulation;
  }

  .sp-sprints-section .sp-sprint-review-stats {
    gap: 0.4rem;
  }

  .sp-sprints-section .sp-sprint-stat {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
    text-align: center;
  }

  .sp-sprints-section .sp-sprint-progress {
    max-width: 100%;
  }

  .sp-sprints-section .sp-sprint-review-members__row {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 0.25rem;
    -webkit-overflow-scrolling: touch;
  }

  .sp-sprints-section .sp-sprint-member-chip {
    flex-shrink: 0;
  }

  .sp-sprints-section .sp-sprint-trello-col {
    max-height: min(60vh, 60dvh, 24rem);
  }

  .sp-sprints-section .sp-sprint-review-tabs {
    display: flex;
    width: 100%;
  }

  .sp-sprints-section .sp-sprint-review-tab {
    flex: 1;
    text-align: center;
    min-height: 2.5rem;
  }

  .sp-sprints-section .sp-sprint-gantt__head,
  .sp-sprints-section .sp-sprint-gantt__lane {
    grid-template-columns: 6.5rem minmax(22rem, 1fr);
    min-width: 30rem;
  }

  .sp-sprints-section .sp-sprint-gantt__body {
    min-width: 30rem;
  }

  .sp-sprints-section .sp-sprint-gantt__bar-label {
    font-size: 0.62rem;
  }

  .sp-sprints-section .sp-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .sp-sprints-section .sp-toolbar > .btn-sm {
    width: 100%;
    min-height: 2.75rem;
    justify-content: center;
  }

  .sp-sprints-section .sp-lobby-form-fields .sp-form-action {
    width: 100%;
    min-height: 2.75rem;
  }

  .sp-sprints-section .sp-menu {
    left: 0;
    right: 0;
    min-width: 100%;
  }

  /* Modals: bottom sheet on phones */
  .sp-modal {
    align-items: flex-end;
    justify-content: stretch;
    padding: 0;
  }

  .sp-modal-panel {
    max-width: none;
    width: 100%;
    border-radius: 1rem 1rem 0 0;
    max-height: min(92vh, 92dvh);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-left: max(1.25rem, env(safe-area-inset-left, 0px));
    padding-right: max(1.25rem, env(safe-area-inset-right, 0px));
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom, 0px));
  }

  .sp-modal-panel--wide {
    max-width: none;
  }

  .sp-modal-actions {
    flex-direction: column-reverse;
    align-items: stretch;
  }

  .sp-modal-actions .btn-sm {
    width: 100%;
    min-height: 2.75rem;
    justify-content: center;
  }

  .sp-form-grid-2 {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 48rem) {
  .sp-console__quick-actions {
    display: none !important;
  }

  .sp-app-topbar {
    display: flex !important;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: space-between;
    grid-template-columns: unset;
    grid-template-rows: unset;
    row-gap: 0;
    min-height: 3.5rem;
    height: auto !important;
    padding: 0.75rem 1rem !important;
    column-gap: 0.75rem;
  }

  .sp-app-topbar__brand {
    grid-column: unset;
    grid-row: unset;
  }

  .sp-app-topbar__nav {
    display: flex !important;
    flex: 0 1 auto;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.75rem;
    justify-content: flex-end;
  }

  .sp-app-topbar__user {
    display: list-item !important;
    max-width: 16rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sp-app-topbar__cli,
  .sp-app-topbar__links {
    grid-column: unset;
    grid-row: unset;
    display: flex;
    align-items: center;
  }

  .sp-app-topbar__links {
    gap: 0.5rem;
  }

  .sp-console-header-toggle .sp-console-header-toggle__label {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip: auto;
    white-space: nowrap;
    border: 0;
  }

  .sp-app-shell > main {
    padding-right: 0;
  }
}
