/**
 * Workbench — mobile / Pixel layout pass
 * Scoped to phone widths only. Desktop styles unchanged.
 * Target: ~412px CSS px (Pixel 9) and general phones ≤768px.
 */

/* ── Shared phone breakpoint ─────────────────────────────────────────────── */
@media (max-width: 768px) {
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    html,
    body {
        overflow-x: clip;
        max-width: 100%;
        width: 100%;
        overscroll-behavior-x: none;
        touch-action: pan-y pinch-zoom; /* one-finger vertical page scroll */
    }

    body {
        overscroll-behavior-y: auto;
    }

    .app-container,
    .main-content,
    .dashboard-shell,
    .dashboard-split,
    .dashboard-page,
    .card,
    .list-page-card,
    .dashboard-section,
    .dashboard-tile-card,
    .dashboard-ops-grid,
    .dashboard-ops-grid--compact,
    .stats-grid,
    .schedule-week-strip,
    .schedule-day {
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    .app-container,
    .main-content,
    .dashboard-shell,
    .dashboard-split,
    .dashboard-page {
        overflow-x: clip;
    }

    /* Keep fixed chrome above content */
    .mobile-topbar {
        position: sticky;
        top: 0;
        z-index: 80;
        display: flex;
        align-items: center;
        gap: 0.75rem;
        margin: -0.25rem -0.25rem 0.85rem;
        padding: 0.55rem 0.65rem;
        background: color-mix(in srgb, var(--main-bg, #f5f5f4) 92%, white);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        border-bottom: 1px solid var(--border-color);
    }

    .mobile-topbar__title {
        font-size: 1.05rem;
        line-height: 1.2;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .sidebar-open-btn {
        width: 2.75rem;
        height: 2.75rem;
        flex-shrink: 0;
        border-radius: 0.5rem;
    }

    .sidebar {
        width: min(20rem, 88vw);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .sidebar .nav-link {
        min-height: 2.75rem;
        padding: 0.7rem 0.85rem;
        font-size: 0.95rem;
    }

    .sidebar .nav-link-sub {
        min-height: 2.5rem;
        padding-top: 0.55rem;
        padding-bottom: 0.55rem;
    }

    .main-content {
        margin-left: 0 !important;
        padding: 0.75rem 0.7rem calc(1rem + env(safe-area-inset-bottom, 0));
        width: 100%;
        max-width: 100vw;
        box-sizing: border-box;
    }

    .card,
    .list-page-card,
    .dashboard-tile-card {
        padding: 0.85rem 0.8rem;
        margin-bottom: 0.7rem;
        border-radius: 0.65rem;
    }

    .card h1 {
        font-size: 1.25rem;
    }

    .card h2 {
        font-size: 1.05rem;
    }

    /* ── Touch-friendly controls ─────────────────────────────────────────── */
    .btn,
    button.btn,
    .btn-sm,
    button.btn-sm {
        min-height: 2.75rem;
        padding: 0.65rem 1rem;
        font-size: 0.9375rem;
        border-radius: 0.55rem;
    }

    .btn-sm,
    button.btn-sm {
        min-height: 2.5rem;
        padding: 0.5rem 0.85rem;
        font-size: 0.875rem;
    }

    .list-row-actions .btn,
    .list-row-actions button {
        min-height: 2.4rem;
        width: 100%;
    }

    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="password"],
    input[type="search"],
    input[type="time"],
    input[type="datetime-local"],
    select,
    textarea {
        font-size: 16px !important; /* prevents iOS/Android zoom-on-focus */
        min-height: 2.75rem;
        padding: 0.65rem 0.85rem;
        border-radius: 0.55rem;
    }

    textarea {
        min-height: 6.5rem;
    }

    select {
        min-height: 2.75rem;
    }

    /* ── Page / list chrome ──────────────────────────────────────────────── */
    .page-header,
    .list-page-card .list-toolbar,
    .list-toolbar.flex-between,
    .flex-between.list-toolbar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.75rem !important;
    }

    .list-toolbar h1,
    .page-header h1,
    .page-header h2 {
        font-size: 1.2rem;
        margin: 0;
    }

    .list-toolbar .btn-primary,
    .page-header .btn-primary,
    .list-toolbar > .btn {
        width: 100%;
        justify-content: center;
        order: 3;
    }

    .search-filter-bar,
    .list-toolbar .search-filter-bar {
        width: 100%;
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-start !important;
        gap: 0.55rem;
        margin-bottom: 0;
    }

    .search-filter-bar input,
    .search-filter-bar select,
    .search-filter-bar .search-wrapper {
        min-width: 0 !important;
        width: 100%;
        flex: 1 1 auto;
    }

    .list-pager,
    .list-pager.flex-between {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.65rem;
    }

    .list-pager-controls {
        width: 100%;
        justify-content: space-between;
    }

    .list-pager-controls .btn {
        flex: 1;
    }

    /* Queue / filter pills — horizontal scroll, no wrap crush */
    .dashboard-queue-tabs,
    .wo-queue-tabs {
        display: flex;
        flex-wrap: wrap;
        gap: 0.45rem;
        padding: 0.15rem 0 0.45rem;
        margin: 0 0 0.65rem;
        max-width: 100%;
        overflow: visible;
    }

    .dashboard-queue-tabs::-webkit-scrollbar,
    .wo-queue-tabs::-webkit-scrollbar {
        display: none;
    }

    button.dashboard-queue-tab,
    .dashboard-queue-tab {
        flex: 0 0 auto;
        min-height: 2.5rem;
        padding: 0.55rem 0.95rem;
        white-space: nowrap;
    }

    .dashboard-queue-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.65rem;
    }

    /* ── Tables: usable phone scroll, denser cells, hide low-value cols ─── */
    .table-container {
        margin: 0;
        padding-bottom: 0.25rem;
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
        touch-action: pan-x pan-y pinch-zoom; /* allow intentional table pan without hijacking page */
        border: 1px solid var(--border-color);
        border-radius: 0.55rem;
    }

    .table-container table {
        width: 100%;
        min-width: 0;
        margin: 0;
        font-size: 0.8125rem;
        table-layout: fixed;
    }

    .table-container table th,
    .table-container table td {
        padding: 0.65rem 0.45rem;
        vertical-align: middle;
        white-space: normal;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    .table-container table th,
    .table-container table td {
        max-width: 9rem;
    }

    .table-container table .list-row-actions,
    .table-container table td.list-row-actions {
        max-width: 5.5rem;
        white-space: nowrap;
    }

    .table-container table .col-flags,
    .table-container table th.col-flags,
    .table-container table td:first-child {
        width: 2.25rem;
        max-width: 2.25rem;
        padding-left: 0.35rem;
        padding-right: 0.25rem;
    }

    .table-container table th {
        position: sticky;
        top: 0;
        z-index: 1;
        background: var(--card-bg);
    }

    /* Work orders: keep flags, job, customer, status, actions */
    .table-container:has(tbody[data-live-tbody="workorders"]) th:nth-child(4),
    .table-container:has(tbody[data-live-tbody="workorders"]) td:nth-child(4),
    .table-container:has(tbody[data-live-tbody="workorders"]) th:nth-child(6),
    .table-container:has(tbody[data-live-tbody="workorders"]) td:nth-child(6),
    .table-container:has(tbody[data-live-tbody="workorders"]) th:nth-child(7),
    .table-container:has(tbody[data-live-tbody="workorders"]) td:nth-child(7),
    .table-container:has(tbody[data-live-tbody="workorders"]) th:nth-child(8),
    .table-container:has(tbody[data-live-tbody="workorders"]) td:nth-child(8),
    .table-container:has(tbody[data-live-tbody="workorders"]) th:nth-child(9),
    .table-container:has(tbody[data-live-tbody="workorders"]) td:nth-child(9) {
        display: none;
    }


    /* Customers: keep names, phone, actions */
    .table-container:has(tbody[data-live-tbody="customers"]) th:nth-child(4),
    .table-container:has(tbody[data-live-tbody="customers"]) td:nth-child(4),
    .table-container:has(tbody[data-live-tbody="customers"]) th:nth-child(6),
    .table-container:has(tbody[data-live-tbody="customers"]) td:nth-child(6),
    .table-container:has(tbody[data-live-tbody="customers"]) th:nth-child(7),
    .table-container:has(tbody[data-live-tbody="customers"]) td:nth-child(7) {
        display: none;
    }


    /* Devices: keep type, brand, model, customer, actions */
    .table-container:has(tbody[data-live-tbody="devices"]) th:nth-child(5),
    .table-container:has(tbody[data-live-tbody="devices"]) td:nth-child(5),
    .table-container:has(tbody[data-live-tbody="devices"]) th:nth-child(7),
    .table-container:has(tbody[data-live-tbody="devices"]) td:nth-child(7) {
        display: none;
    }


    /* Inventory: keep category, name, qty, sell, actions */
    .table-container:has(tbody[data-live-tbody="inventory"]) th:nth-child(4),
    .table-container:has(tbody[data-live-tbody="inventory"]) td:nth-child(4),
    .table-container:has(tbody[data-live-tbody="inventory"]) th:nth-child(5),
    .table-container:has(tbody[data-live-tbody="inventory"]) td:nth-child(5),
    .table-container:has(tbody[data-live-tbody="inventory"]) th:nth-child(6),
    .table-container:has(tbody[data-live-tbody="inventory"]) td:nth-child(6),
    .table-container:has(tbody[data-live-tbody="inventory"]) th:nth-child(9),
    .table-container:has(tbody[data-live-tbody="inventory"]) td:nth-child(9) {
        display: none;
    }


    table tbody tr.clickable-row {
        cursor: pointer;
    }

    /* ── Dashboard ───────────────────────────────────────────────────────── */
    .dashboard-shell > .dashboard-toolbar,
    .dashboard-toolbar {
        margin-bottom: 0.75rem;
    }

    .dashboard-header-actions {
        width: 100%;
        flex-direction: column;
        align-items: stretch;
        gap: 0.55rem;
    }

    .dashboard-search-wrap {
        width: 100%;
    }

    .dashboard-search-wrap input {
        width: 100%;
    }

    .dashboard-search-wrap {
        min-width: 0 !important;
        width: 100%;
        max-width: 100%;
    }

    .dashboard-finance-period,
    #finance-period {
        width: 100%;
        max-width: 100%;
        min-width: 0;
    }

    /* Any leftover nowrap labels inside dashboard cards */
    .dashboard-section,
    .dashboard-tile-card,
    .dashboard-ops-card,
    .schedule-week-strip {
        overflow-x: clip;
    }


    #dashboard-refresh-btn {
        width: 100%;
    }

    .dashboard-split {
        flex-direction: column;
        gap: 0.85rem;
    }

    .dashboard-tile-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 0.5rem !important;
    }

    .dashboard-tile-grid .dashboard-ops-card,
    .dashboard-tile-grid .stat-card {
        padding: 0.75rem 0.7rem;
    }

    .dashboard-tile-grid .dashboard-ops-card .stat-value {
        font-size: 1.45rem;
    }

    .dashboard-ops-grid,
    .dashboard-ops-grid--compact {
        grid-template-columns: 1fr !important;
        gap: 0.65rem;
    }

    .schedule-week-strip {
        grid-template-columns: 1fr !important;
        gap: 0.45rem;
    }

    .schedule-week-strip__head {
        flex-wrap: wrap;
        gap: 0.35rem 0.75rem;
    }

    .schedule-day {
        padding: 0.65rem 0.7rem;
        overflow: hidden;
        min-width: 0;
        max-width: 100%;
    }

    .schedule-chip,
    .schedule-day__empty,
    .schedule-block {
        min-height: 2.5rem;
        width: 100%;
        max-width: 100%;
        min-width: 0 !important; /* buttons default to min-content and blow the layout */
        text-align: left;
        overflow: hidden;
        box-sizing: border-box;
    }

    .schedule-chip__time,
    .schedule-chip__meta,
    .schedule-block__time,
    .schedule-block__meta {
        display: block;
        max-width: 100%;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .dashboard-bar-list,
    .dashboard-bar-list__meta,
    .dashboard-bar-list__name,
    .dashboard-dual-bars__label,
    .dashboard-dual-bars__row,
    .dashboard-search-item,
    .dashboard-search-item span {
        max-width: 100%;
        min-width: 0;
    }

    .dashboard-bar-list__name,
    .dashboard-dual-bars__label,
    .dashboard-search-item span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .dashboard-tile-grid .dashboard-ops-card,
    .dashboard-tile-grid .stat-card,
    .stat-card,
    .dashboard-ops-card {
        overflow: hidden;
        max-width: 100%;
    }

    .stat-value,
    .dashboard-ops-card .stat-value {
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    /* ── Modals / sheets — near full-screen on phone ─────────────────────── */
    .modal,
    .modal.active {
        padding: 0 !important;
        align-items: stretch !important;
        justify-content: flex-end !important;
    }

    .modal-content,
    .modal-large,
    .modal-content.modal-sheet {
        width: 100% !important;
        max-width: 100% !important;
        min-height: 100%;
        max-height: 100% !important;
        height: 100%;
        margin: 0;
        border-radius: 0 !important;
        border-left: none;
        border-right: none;
        box-shadow: none;
        padding: 0.85rem 0.85rem calc(0.85rem + env(safe-area-inset-bottom, 0));
    }

    .modal-content.modal-sheet {
        display: flex;
        flex-direction: column;
        padding: 0;
    }

    .modal-sheet > .modal-header,
    .modal-header--sheet {
        position: sticky;
        top: 0;
        z-index: 5;
        background: var(--card-bg);
        padding: 0.85rem 0.9rem;
        padding-top: max(0.85rem, env(safe-area-inset-top, 0));
    }

    .modal-header__main {
        flex-wrap: wrap;
    }

    .modal-header__main h2 {
        font-size: 1.05rem;
        line-height: 1.25;
    }

    .modal-close,
    button.modal-close {
        width: 2.75rem;
        height: 2.75rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        border-radius: 0.5rem;
        flex-shrink: 0;
    }

    .modal-sheet > .modal-body--view,
    .modal-sheet > .modal-body--form,
    .modal-body {
        padding: 0.75rem 0.9rem !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-sheet > .modal-actions,
    .modal-sheet > .modal-footer,
    .modal-actions {
        position: sticky;
        bottom: 0;
        z-index: 5;
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.55rem;
        padding: 0.75rem 0.9rem calc(0.75rem + env(safe-area-inset-bottom, 0)) !important;
        background: var(--card-bg);
    }

    .modal-sheet .action-buttons-left,
    .modal-sheet .action-buttons-right,
    .action-buttons-right {
        width: 100%;
        margin-left: 0 !important;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .modal-sheet .action-buttons-left .btn,
    .modal-sheet .action-buttons-right .btn,
    .modal-actions .btn {
        width: 100%;
        justify-content: center;
    }

    /* If only one button in a group, span full width */
    .modal-sheet .action-buttons-left .btn:only-child,
    .modal-sheet .action-buttons-right .btn:only-child {
        grid-column: 1 / -1;
    }

    .form-grid,
    .modal-sheet .form-grid,
    .sheet-kv-grid,
    .sheet-kv-grid--2,
    .sheet-kv-grid--3,
    .sheet-split,
    .sheet-panels,
    .sheet-panels--invoice,
    .sheet-panels--customer {
        grid-template-columns: 1fr !important;
    }

    .sheet-banner__row,
    .sheet-banner__meta {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.35rem;
    }

    .sheet-pay-form {
        grid-template-columns: 1fr !important;
    }

    .card-section,
    .modal-sheet .card-section,
    .modal-sheet .sheet-block,
    .modal-sheet .sheet-panel {
        padding: 0.75rem;
    }

    /* Note dialogs / secondary overlays */
    .wo-note-dialog,
    .wo-note-dialog__panel {
        width: min(100vw - 1rem, 28rem) !important;
        max-width: calc(100vw - 1rem) !important;
    }

    .wo-note-card__actions {
        flex-wrap: wrap;
    }

    .wo-note-card__actions .btn {
        flex: 1 1 auto;
    }

    /* ── Schedule diary ──────────────────────────────────────────────────── */
    body.page-schedule .main-content {
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0));
    }

    .schedule-diary.card {
        padding: 0.7rem;
    }

    .schedule-grid {
        display: grid;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow-x: auto;
        overflow-y: hidden;
        overscroll-behavior-x: contain;
        touch-action: pan-x pan-y pinch-zoom;
        -webkit-overflow-scrolling: touch;
    }

    .schedule-grid > * {
        min-width: 3.25rem;
    }

    .schedule-hours-options {
        flex-wrap: wrap;
        gap: 0.45rem;
    }

    .schedule-hours-options .btn {
        flex: 1 1 calc(50% - 0.45rem);
    }

    /* ── Settings ────────────────────────────────────────────────────────── */
    .settings-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.55rem;
    }

    .settings-tabs,
    .settings-tab-bar,
    [role="tablist"].settings-tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0.4rem;
        padding-bottom: 0.35rem;
        scrollbar-width: none;
    }

    .settings-tabs::-webkit-scrollbar {
        display: none;
    }

    .settings-tab {
        flex: 0 0 auto;
        min-height: 2.6rem;
        white-space: nowrap;
    }

    /* ── Login ───────────────────────────────────────────────────────────── */
    .login-container {
        padding: 1rem 0.75rem;
        align-items: flex-start;
        min-height: 100dvh;
    }

    .login-box {
        max-width: 100% !important;
        width: 100% !important;
        padding: 1.15rem !important;
        margin-top: 8vh;
    }

    .login-box img {
        max-width: min(100%, 16rem) !important;
        max-height: 5.5rem !important;
    }

    /* ── Image gallery / lightbox ────────────────────────────────────────── */
    .image-gallery__thumb {
        width: calc(50% - 0.25rem);
        max-width: none;
    }

    .image-lightbox__nav--prev { left: 0.35rem; }
    .image-lightbox__nav--next { right: 0.35rem; }

    /* ── Misc ────────────────────────────────────────────────────────────── */
    .flex-between {
        flex-wrap: wrap;
        gap: 0.65rem;
    }

    .table-filters {
        flex-direction: column;
        align-items: stretch;
    }

    .table-filters select.column-filter {
        width: 100%;
        min-width: 0;
    }

    .toast,
    .wb-toast,
    [class*="toast"] {
        max-width: calc(100vw - 1.5rem);
    }
}


    /* Invoices: keep number, customer, amount, status, actions */
    .table-container:has(tbody[data-live-tbody="invoices"]) th:nth-child(3),
    .table-container:has(tbody[data-live-tbody="invoices"]) td:nth-child(3),
    .table-container:has(tbody[data-live-tbody="invoices"]) th:nth-child(7),
    .table-container:has(tbody[data-live-tbody="invoices"]) td:nth-child(7) {
        display: none;
    }


    /* Finance cards / grids if present */
    .finance-grid,
    .finance-summary-grid,
    .dashboard-overview-grid {
        grid-template-columns: 1fr !important;
    }

    /* Prevent wide pre/code/tables from blowing the viewport */
    pre, code, .readonly-field {
        max-width: 100%;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    .sheet-clamp,
    .sheet-panel__list,
    .linked-item {
        max-width: 100%;
    }

    /* Make primary row tap target clearer */
    .table-container table tbody tr.clickable-row td {
        padding-top: 0.8rem;
        padding-bottom: 0.8rem;
    }

/* Pixel / compact phone denser pass */
@media (max-width: 430px) {
    .main-content {
        padding-left: 0.55rem;
        padding-right: 0.55rem;
    }

    .dashboard-tile-grid .dashboard-ops-card h3 {
        font-size: 0.7rem;
    }

    .dashboard-tile-grid .dashboard-ops-card .stat-value {
        font-size: 1.3rem;
    }

    .mobile-topbar__title {
        font-size: 0.98rem;
    }

    .modal-sheet .action-buttons-left,
    .modal-sheet .action-buttons-right {
        grid-template-columns: 1fr;
    }
}

/* Installed PWA on Android */
@media (display-mode: standalone) and (max-width: 768px) {
    .mobile-topbar {
        padding-top: max(0.55rem, env(safe-area-inset-top, 0));
    }

    .modal-sheet > .modal-header,
    .modal-header--sheet {
        padding-top: max(0.85rem, env(safe-area-inset-top, 0));
    }
}
