/* Alpaca Support (ams_portal/) — Wave 364: Mobile & Tablet Executive Companion.
   Dedicated responsive stylesheet (zero edits to ams-portal.css).
   Spatial glass surfaces, zero decorative borders, 44px touch targets, 0px horizontal overflow. */

/* ── Global horizontal overflow guard (clip preserves position: sticky) ─────────────────────────────────────── */
html.sp,
body.sp-body {
  overflow-x: clip;
  max-width: 100%;
}

/* ── Desktop defaults: mobile-only chrome stays hidden ──────────────────────────────────────────────────────── */
.sp-mobile-menu-btn,
.sp-mobile-backdrop,
.sp-mobile-drawer,
.sp-mobile-action-bar {
  display: none;
}

/* ── Medium-Width Unified Header Wrap (<= 1100px) ───────────────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  header.sp-top {
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 12px;
    padding: 10px 14px;
    border-radius: 24px;
    max-width: 100%;
  }

  header.sp-top .sp-brand {
    min-width: 0;
    margin-right: auto;
    overflow: hidden;
  }

  header.sp-top > .sp-nav-tabs {
    order: 3;
    flex-basis: 100%;
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 0;
    background: transparent;
    flex-wrap: wrap;
    justify-content: center;
    gap: 4px;
  }

  header.sp-top .sp-top-right {
    flex-shrink: 0;
    margin-left: 0;
  }
}

/* ── Tablet & Mobile Navigation Drawer + Responsive Shell (<= 900px) ────────────────────────────────────────── */
@media (max-width: 900px) {
  html.sp.sp-drawer-open,
  html.sp.sp-drawer-open body.sp-body {
    overflow: hidden;
  }

  .sp-shell,
  .adm-shell,
  .sp-window,
  .adm-main,
  .sp-stack,
  .sp-card,
  .adm-section,
  .sp-tickets-sec {
    max-width: 100%;
    min-width: 0;
  }

  /* Compact top bar with mobile menu toggle; nav tabs wrap cleanly below without overflow */
  header.sp-top {
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    padding: 8px 12px;
    border-radius: 24px;
    max-width: 100%;
  }

  header.sp-top > .sp-nav-tabs {
    order: 3;
    flex-basis: 100%;
    width: 100%;
    max-width: 100%;
    margin-left: 0;
    flex-wrap: wrap;
    justify-content: center;
    gap: 4px;
  }

  header.sp-top .sp-brand {
    min-width: 0;
    margin-right: auto;
    overflow: hidden;
  }

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

  header.sp-top .sp-top-right {
    flex-shrink: 0;
    margin-left: 0;
  }

  .sp-mobile-menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--sp-t1);
    cursor: pointer;
    flex-shrink: 0;
    transition: background .16s ease, transform .14s ease;
  }

  .sp-mobile-menu-btn:hover {
    background: var(--sp-ctrl-fill);
  }

  .sp-mobile-menu-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }

  /* Dim backdrop behind slide-over drawer */
  .sp-mobile-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 90;
    background: var(--sp-modal-scrim);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    opacity: 0;
    transition: opacity .2s ease;
  }

  .sp-mobile-backdrop[hidden] {
    display: none;
  }

  .sp-mobile-backdrop.is-open {
    opacity: 1;
  }

  /* Spatial glass slide-over navigation drawer */
  .sp-mobile-drawer {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 10px;
    left: 10px;
    bottom: 10px;
    z-index: 95;
    width: min(328px, calc(100vw - 24px));
    max-width: calc(100vw - 20px);
    border: 0;
    border-radius: 26px;
    background: var(--sp-panel);
    backdrop-filter: blur(28px) saturate(160%);
    -webkit-backdrop-filter: blur(28px) saturate(160%);
    box-shadow: var(--sp-edge-big);
    color: var(--sp-t1);
    overflow: hidden;
    transform: translateX(calc(-100% - 20px));
    transition: transform .24s cubic-bezier(.2, .8, .2, 1);
  }

  .sp-mobile-drawer[hidden] {
    display: none;
  }

  .sp-mobile-drawer.is-open {
    transform: translateX(0);
  }

  .sp-mobile-drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 14px 14px 10px 18px;
    flex-shrink: 0;
  }

  .sp-mobile-drawer-title {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -.01em;
    color: var(--sp-t1);
  }

  .sp-mobile-drawer-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border: 0;
    border-radius: 14px;
    background: var(--sp-chip);
    color: var(--sp-t1);
    cursor: pointer;
  }

  .sp-mobile-drawer-close:hover {
    background: var(--sp-chip-hover);
  }

  .sp-mobile-drawer-close:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }

  .sp-mobile-drawer-body {
    flex: 1 1 auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 6px 14px 18px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  /* User / client / org context card */
  .sp-mobile-context {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: 18px;
    background: var(--sp-chip);
  }

  .sp-mobile-context-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 700;
    color: var(--sp-on-accent);
    background: var(--sp-avatar-user-bg);
    overflow: hidden;
  }

  .sp-mobile-context-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  .sp-mobile-context-meta {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .sp-mobile-context-name {
    font-size: 14px;
    font-weight: 700;
    color: var(--sp-t1);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sp-mobile-context-org,
  .sp-mobile-context-email {
    font-size: 12.5px;
    color: var(--sp-t2);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sp-mobile-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .sp-mobile-section-label {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--sp-t2);
    padding: 0 6px;
  }

  .sp-mobile-nav-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .sp-mobile-nav-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 44px;
    padding: 10px 14px;
    border: 0;
    border-radius: 14px;
    background: transparent;
    color: var(--sp-t1);
    font: inherit;
    font-size: 14.5px;
    font-weight: 600;
    text-decoration: none;
    text-align: left;
    cursor: pointer;
    width: 100%;
    box-sizing: border-box;
    transition: background .15s ease;
  }

  .sp-mobile-nav-link:hover {
    background: var(--sp-chip);
  }

  .sp-mobile-nav-link[aria-current="page"] {
    background: var(--sp-sel);
    color: var(--sp-accent-ink);
  }

  .sp-mobile-nav-link:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }

  /* Appearance segmented control inside drawer */
  .sp-mobile-theme-seg {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    padding: 4px;
    border-radius: 16px;
    background: var(--sp-chip);
  }

  .sp-mobile-theme-btn {
    min-height: 44px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--sp-t2);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s ease, color .15s ease;
  }

  .sp-mobile-theme-btn[aria-pressed="true"] {
    background: var(--sp-nav-on);
    color: var(--sp-t1);
    box-shadow: var(--sp-edge-sm);
  }

  .sp-mobile-theme-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }

  /* Account controls inside drawer */
  .sp-mobile-account-grid {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .sp-mobile-action-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding: 10px 14px;
    border: 0;
    border-radius: 14px;
    background: transparent;
    color: var(--sp-t1);
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
    width: 100%;
    box-sizing: border-box;
  }

  .sp-mobile-action-btn:hover {
    background: var(--sp-chip);
  }

  .sp-mobile-action-btn.sp-mobile-danger {
    color: var(--sp-bad-fg);
  }

  .sp-mobile-action-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }

  /* Responsive layout stacking & table containers */
  .sp-ticket-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .ad-charts,
  .sp-charts,
  .sp-status-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .ad-chrome-bar {
    flex-wrap: wrap;
    gap: 10px;
    max-width: 100%;
  }

  .adm-nav {
    max-width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  .ad-nav-btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .adm-search {
    min-width: 0;
    flex: 0 1 320px;
    height: var(--sp-ctrl-h, 44px);
    min-height: var(--sp-ctrl-h, 44px);
    max-height: var(--sp-ctrl-h, 44px);
    align-self: center;
    max-width: 100%;
  }

  .ad-sec-head,
  .ad-table-head-bar,
  .sp-hero,
  .sp-bar {
    flex-wrap: wrap;
    gap: 10px;
    max-width: 100%;
  }

  .ad-table-scroll,
  .ad-ov-tickets-scroll {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sp-ev-card,
  .sp-body-text,
  .sp-ticket-head-copy,
  .sp-ticket-head-copy h1,
  .sp-dl dd {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .sp-body-text pre,
  .sp-body-text code {
    max-width: 100%;
    overflow-x: auto;
  }

  /* Dashboard & KPI grids stay within viewport */
  .ad-kpis,
  .sp-val-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  }
}

/* ── Tablet Portrait & Mobile Touch Targets + Sticky Bottom Action Bar (<= 768px) ───────────────────────────── */
@media (max-width: 768px) {
  /* 44px minimum touch targets across interactive controls */
  .sp-primary-pill,
  .sp-primary-sm,
  .sp-chip-btn,
  .sp-icon-btn,
  .sp-tool-pill,
  .sp-user-btn,
  .sp-nav-tab,
  .sp-tabs button,
  .ad-nav-btn,
  .ad-sla-filter-chip,
  .segmented-btn,
  .sp-aud-btn,
  .sp-select-pill,
  .sp-search-pill,
  .sp-pw-toggle,
  .sp-menu-item,
  .sp-menu-seg-btn {
    min-height: 44px;
  }

  .sp-icon-btn,
  .sp-pw-toggle,
  .sp-send-orb {
    min-width: 44px;
    min-height: 44px;
  }

  .sp-form input[type="email"],
  .sp-form input[type="password"],
  .sp-form input[type="text"],
  .sp-form select,
  .sp-side select,
  .sp-approve-banner input[type="text"] {
    min-height: 44px;
    max-width: 100%;
    box-sizing: border-box;
  }

  /* Toolbars & filter controls wrap cleanly without clipping */
  .sp-tabs,
  .ad-queue-sla-chips,
  .sp-composer-toolbar,
  .sp-reply-actions,
  .sp-draft-actions,
  .sp-filter-chips,
  .sp-kb-areas,
  .sp-kb-controls,
  .ad-qbr-actions {
    flex-wrap: wrap;
    max-width: 100%;
  }

  .ad-qbr-actions.sp-push-right {
    margin-left: 0;
  }

  .sp-org-wrap,
  .sp-select-pill,
  .sp-search-pill,
  .adm-search {
    max-width: 100%;
    min-width: 0;
  }

  .sp-search-pill input,
  .adm-search input {
    min-width: 0;
    width: 100%;
  }

  /* Login & Auth stage */
  .sp-auth-stage {
    padding: 8px 0;
    max-width: 100%;
  }

  .sp-login {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  /* Ticket detail sticky bottom action / approval bar */
  body[data-page="ticket"] .sp-shell {
    padding-bottom: 96px;
  }

  .sp-mobile-action-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    z-index: 45;
    padding: 8px;
    border: 0;
    border-radius: 22px;
    background: var(--sp-panel);
    backdrop-filter: blur(28px) saturate(160%);
    -webkit-backdrop-filter: blur(28px) saturate(160%);
    box-shadow: var(--sp-edge-big);
    max-width: calc(100vw - 24px);
    box-sizing: border-box;
  }

  .sp-mobile-action-bar[hidden] {
    display: none;
  }

  .sp-mobile-bar-btn {
    flex: 1 1 0;
    min-width: 0;
    min-height: 44px;
    padding: 0 12px;
    border: 0;
    border-radius: 14px;
    background: var(--sp-chip);
    color: var(--sp-t1);
    font: inherit;
    font-size: 13.5px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background .15s ease, transform .14s ease;
  }

  .sp-mobile-bar-btn:hover {
    background: var(--sp-chip-hover);
  }

  .sp-mobile-bar-btn.is-primary {
    background: var(--sp-accent-fill);
    color: var(--sp-on-accent);
    box-shadow: var(--sp-primary-shadow);
  }

  .sp-mobile-bar-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--sp-focus) 35%, transparent);
  }
}

/* ── Collapse Admin Primary Nav into Slide-Over Drawer (<= 760px) ───────────────────────────────────────────── */
@media (max-width: 760px) {
  body[data-page="admin"] header.sp-top > .sp-nav-tabs {
    display: none;
  }

  body[data-page="admin"] header.sp-top .sp-top-right {
    flex: 0 1 auto;
    min-width: 0;
  }

  header.sp-top .sp-global-search {
    flex: 0 1 220px;
    width: 220px;
    height: var(--sp-ctrl-h, 44px);
    min-height: var(--sp-ctrl-h, 44px);
    max-height: var(--sp-ctrl-h, 44px);
  }

  header.sp-top .adm-search {
    height: var(--sp-ctrl-h, 44px);
    min-height: var(--sp-ctrl-h, 44px);
    max-height: var(--sp-ctrl-h, 44px);
  }
}

@media (max-width: 600px) {
  body[data-page="admin"] header.sp-top .sp-top-right {
    flex: 1 1 100%;
    justify-content: space-between;
  }

  body[data-page="admin"] header.sp-top .sp-global-search {
    flex: 1 1 auto;
    max-width: none;
  }

  body[data-page="admin"] header.sp-top .adm-search {
    flex: 1 1 auto;
    max-width: none;
    height: var(--sp-ctrl-h, 44px);
    min-height: var(--sp-ctrl-h, 44px);
    max-height: var(--sp-ctrl-h, 44px);
  }
}

/* ── Narrow Phone Polish (<= 480px, e.g. 390x844 iPhone) ────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .sp-shell,
  .adm-shell {
    padding: 10px 10px 44px;
    gap: 10px;
  }

  .sp-window,
  .adm-main {
    padding: 14px;
    border-radius: 22px;
  }

  .sp-ticket-head {
    gap: 10px;
    align-items: flex-start;
  }

  .sp-ticket-head-copy h1 {
    font-size: 20px;
    line-height: 1.25;
  }

  /* Keep 5-step stage track cleanly inside 390px */
  .sp-track {
    gap: 4px;
  }

  .sp-track-seg {
    min-width: 0;
  }

  .sp-track-label {
    font-size: 10.5px;
    line-height: 1.2;
    overflow-wrap: break-word;
    text-align: center;
  }

  .sp-bar {
    gap: 8px;
  }

  .sp-tabs {
    width: 100%;
  }

  .sp-tabs button {
    flex: 1 1 auto;
    justify-content: center;
  }

  .sp-org-wrap,
  #sp-export-xlsx {
    width: 100%;
    text-align: center;
    justify-content: center;
  }

  .sp-select-pill {
    width: 100%;
  }

  .ad-kpis,
  .sp-val-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .sp-row {
    flex-wrap: wrap;
  }

  .sp-composer-head .sp-audience-seg {
    width: 100%;
    display: flex;
  }

  .sp-composer-head .sp-aud-btn {
    flex: 1 1 0;
  }

  .sp-macro-popover {
    left: 0;
    right: 0;
    width: auto;
    max-width: calc(100vw - 48px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .sp-mobile-backdrop,
  .sp-mobile-drawer,
  .sp-mobile-menu-btn,
  .sp-mobile-bar-btn {
    transition: none !important;
  }
}
