/* Mobile / tablet responsive — phone-first chrome (tables stay tables) */

:root {
  --accounts-table-min-w: 860px;
}

/* —— Compact ≤1050 —— */
@media (max-width: 1050px) {
  .fleet-header {
    column-gap: 8px;
    row-gap: 8px;
  }

  .fleet-header-actions {
    max-width: 100%;
    gap: 6px;
  }

  /* Icon-only for Refresh; keep text on destructive / batch actions */
  .fleet-header-tools .btn-label,
  .fleet-header-actions .btn-label {
    display: none;
  }

  .fleet-header-tools .btn-sm,
  .fleet-header-actions .btn-sm {
    min-width: 32px;
    min-height: 32px;
    padding-inline: 8px;
    justify-content: center;
  }

  .fleet-header-tools .btn-keep-label .btn-label,
  .fleet-header-tools .toolbar-menu-trigger .btn-label,
  .fleet-header-tools #btn-live-stop-all .btn-label,
  .fleet-header-tools #btn-tasks-purge-finished .btn-label {
    display: inline;
  }

  .fleet-header-tools .btn-keep-label,
  .fleet-header-tools #btn-live-stop-all,
  .fleet-header-tools #btn-tasks-purge-finished,
  .fleet-header-tools .toolbar-menu-trigger {
    min-width: 0;
    padding-inline: 10px;
    gap: 6px;
  }

  .accounts-panel .accounts-table-wrap,
  .accounts-table-wrap,
  .table-wrap,
  .live-session-table-wrap {
    -webkit-overflow-scrolling: touch;
  }

  .accounts-roster-table {
    width: max(100%, var(--accounts-table-min-w));
    min-width: var(--accounts-table-min-w);
    table-layout: auto;
  }

  .fleet-body {
    padding: 14px 16px;
  }
}

@media (max-width: 900px) {
  .fleet-hint,
  .agents-hint {
    display: none;
  }
}

/* —— Phone ≤720 —— */
@media (max-width: 720px) {
  :root {
    --titlebar-h: calc(44px + env(safe-area-inset-top, 0px));
  }

  /* Titlebar */
  .titlebar {
    height: auto;
    min-height: calc(44px + env(safe-area-inset-top, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: max(8px, env(safe-area-inset-left, 0px));
    padding-right: max(8px, env(safe-area-inset-right, 0px));
    position: sticky;
    top: 0;
    z-index: 60;
  }

  .sidebar-toggle,
  .titlebar .btn-icon {
    width: 44px;
    height: 44px;
  }

  .app-brand {
    font-size: var(--text-card);
  }

  .user-label {
    display: none;
  }

  #btn-logout {
    min-width: 44px;
    min-height: 44px;
    padding-inline: 12px;
    font-size: var(--text-base);
  }

  .sidebar-backdrop {
    top: calc(44px + env(safe-area-inset-top, 0px));
  }

  /* Mobile drawer only — nested scroll OK for overlay; page content uses document scroll */
  .sidebar {
    top: calc(44px + env(safe-area-inset-top, 0px));
    max-height: calc(100dvh - 44px - env(safe-area-inset-top, 0px));
    min-height: 0;
    height: auto;
    padding-left: env(safe-area-inset-left, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .nav-item {
    min-height: 48px;
    padding-inline: 12px;
  }

  /*
   * Page header — tighten the shared 2-row grid for phone.
   * Areas already set at ≤1050: title|tools / actions.
   */
  .fleet-header {
    padding: 12px 14px;
    min-height: 0;
    row-gap: 10px;
  }

  .fleet-title {
    font-size: var(--text-xl);
    line-height: 1.25;
  }

  .fleet-header-tools .btn-sm {
    min-width: 40px;
    min-height: 40px;
  }

  .fleet-header-tools .btn-keep-label,
  .fleet-header-tools .toolbar-menu-trigger,
  .fleet-header-tools #btn-live-stop-all,
  .fleet-header-tools #btn-tasks-purge-finished {
    min-height: 40px;
    padding-inline: 10px;
  }

  .fleet-header-actions {
    overflow: visible;
  }

  /* Status pills — compact but keep label (dot-only was uninformative on iPhone) */
  .fleet-sse-pill {
    max-width: 100%;
    min-height: 0;
    height: auto;
    padding: 3px 8px;
    gap: 5px;
    font-size: var(--text-2xs);
    line-height: 1.25;
  }

  button.fleet-sse-pill {
    min-height: 0;
    height: auto;
    padding: 3px 8px;
  }

  .fleet-sse-pill > span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: min(48vw, 12rem);
  }

  .fleet-sse-pill--sync {
    gap: 5px;
    padding: 3px 8px;
    min-width: 0;
  }

  .fleet-sse-pill--sync > span:not(.status-dot) {
    /* show label — was clipped to 1px (sr-only) which left only the dot */
    position: static;
    width: auto;
    height: auto;
    padding: 0;
    margin: 0;
    overflow: hidden;
    clip: auto;
    clip-path: none;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: min(40vw, 9rem);
    border: 0;
  }

  /* Search: icon on title row; expand overlay when open */
  .fleet-header-tools .accounts-search-toggle {
    display: inline-flex;
  }

  .fleet-header-tools .accounts-search-wrap[data-collapsible-search]:not(.is-open) .accounts-search {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
    opacity: 0;
    pointer-events: none;
  }

  .fleet-header-tools .accounts-search-wrap[data-collapsible-search].is-open {
    position: absolute;
    left: 12px;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 6;
    gap: 6px;
    padding: 4px 6px;
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    box-shadow: 0 8px 24px color-mix(in srgb, #000 35%, transparent);
  }

  .fleet-header-tools .accounts-search-wrap[data-collapsible-search].is-open .accounts-search {
    position: static;
    width: 100%;
    min-width: 0;
    height: 36px;
    flex: 1 1 auto;
    clip: auto;
    margin: 0;
    opacity: 1;
    pointer-events: auto;
    overflow: visible;
  }

  .fleet-header-tools .accounts-search-wrap[data-collapsible-search].has-query:not(.is-open) .accounts-search-toggle {
    color: var(--status-success-fg, #22c55e);
  }

  /* Filters stay on actions row; search no longer lives there */
  .fleet-header-actions .accounts-ownership-filter,
  .fleet-header-actions > select,
  .fleet-header-actions > .admin-filter,
  .fleet-header-actions > .admin-date {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    max-width: none;
    height: 40px;
  }

  .fleet-body,
  .agents-body {
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    overflow: visible;
  }

  .accounts-table-wrap,
  .live-session-table-wrap,
  .tasks-table-wrap,
  .table-wrap {
    overflow-x: auto;
    overflow-y: visible;
    max-height: none;
  }

  /* Stats — 2 columns, denser */
  .fleet-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
  }

  .fleet-stat-card {
    padding: 10px 12px;
    border-radius: 10px;
  }

  .fleet-stat-card strong {
    font-size: var(--text-xl);
  }

  .fleet-stat-label {
    font-size: var(--text-2xs);
  }

  /* Live toolbar */
  .live-session-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .live-session-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0;
    padding: 0;
  }

  .live-filter {
    flex: 1 1 auto;
    min-height: 36px;
    justify-content: center;
  }

  .live-session-toolbar-end {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    width: 100%;
  }

  .live-worker-filter,
  select.live-worker-filter {
    width: 100%;
    max-width: none;
    min-width: 0;
    /* Match Task `.tasks-select` — not the 40px/16px touch bump that dwarfed filter chips */
    height: 28px;
    min-height: 28px;
    padding: 0 28px 0 10px;
    font-size: var(--text-base);
    line-height: 1.2;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  .live-sessions-body > .live-session-table-wrap {
    margin: 0 -12px;
  }

  /* Lock Live section chips — Safari text autosize blows up colspan on “Semua”. */
  .live-sessions-table tbody tr.live-section-row > td.live-section-label {
    font-size: 10px !important;
    line-height: 1.35;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  /* Tasks */
  .tasks-status-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0;
    padding: 0;
  }

  .tasks-filter-btn {
    flex: 1 1 auto;
    min-height: 36px;
  }

  .tasks-create-form {
    flex-direction: column;
    align-items: stretch;
  }

  .tasks-field,
  .tasks-field--narrow {
    width: 100%;
    max-width: none;
  }

  #btn-tasks-create {
    width: 100%;
    min-height: 44px;
  }

  /* Admin forms */
  .admin-inline-form {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .admin-inline-form input,
  .admin-inline-form select {
    width: 100%;
    min-height: 40px;
  }

  .admin-inline-form .btn {
    width: 100%;
    min-height: 44px;
  }

  /* Fleet cards / agents */
  .fleet-worker-grid,
  .agents-grid {
    grid-template-columns: 1fr;
  }

  .fleet-tuning-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .fleet-tuning-row input,
  .fleet-tuning-row .btn {
    width: 100%;
    min-height: 40px;
  }

  .agents-section-head {
    flex-wrap: wrap;
    gap: 8px;
  }

  .agents-section-head .btn {
    min-height: 40px;
  }

  /* Quality dock */
  .accounts-quality-dock {
    flex-direction: column;
    align-items: stretch;
  }

  .quality-dock-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
  }

  .quality-dock-actions .btn {
    flex: 1 1 auto;
    min-height: 40px;
  }

  .quality-dock-actions .btn-label {
    display: inline;
  }

  /* Row icon touch */
  .row-action-slot,
  .btn-icon-action {
    width: 36px;
    height: 36px;
  }

  .toolbar-menu-item,
  .accounts-more-item {
    min-height: 44px;
    padding-block: 10px;
  }

  .toolbar-menu.is-open .toolbar-menu-panel {
    position: fixed;
    left: max(12px, env(safe-area-inset-left, 0px));
    right: max(12px, env(safe-area-inset-right, 0px));
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    top: auto;
    min-width: 0;
    max-width: none;
    z-index: 60;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
  }

  /* Tables remain tables — only ensure scroll */
  .admin-table {
    min-width: 560px;
  }

  .download-release .release-meta {
    grid-template-columns: 1fr;
  }

  .download-release .release-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .download-release .release-actions .btn {
    width: 100%;
    min-height: 44px;
  }

  .toast-host {
    left: max(12px, env(safe-area-inset-left, 0px));
    right: max(12px, env(safe-area-inset-right, 0px));
    bottom: max(16px, env(safe-area-inset-bottom, 0px));
    max-width: none;
  }
}

/* —— Narrow phone ≤520 —— */
@media (max-width: 520px) {
  .app-brand {
    max-width: 7.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

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

  .fleet-header-actions > select,
  .fleet-header-actions > .accounts-ownership-filter,
  .fleet-header-actions > .admin-filter,
  .fleet-header-actions > .admin-date {
    flex: 1 1 100%;
  }

  .boot-card {
    margin: 0;
    border-radius: 0;
    min-height: 100dvh;
    max-width: none;
  }

  .boot-stage {
    padding: max(12px, env(safe-area-inset-top, 0px)) 0 max(12px, env(safe-area-inset-bottom, 0px));
  }
}

/* —— Full-screen modals —— */
@media (max-width: 640px) {
  .modal-overlay {
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
  }

  .modal-overlay > .modal {
    width: 100%;
    max-width: none;
    height: 100%;
    max-height: none;
    min-height: 100%;
    border-radius: 0;
    margin: 0;
  }

  .modal-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer {
    flex-direction: column-reverse;
    align-items: stretch;
    gap: 8px;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  }

  .modal-footer .btn {
    width: 100%;
    min-height: 44px;
    justify-content: center;
  }

  .modal-header {
    padding-top: max(12px, env(safe-area-inset-top, 0px));
  }

  .proxy-fields-row {
    grid-template-columns: 1fr;
  }
}
