/**
 * Mobile & tablet responsive — Oil Distribution App
 * Breakpoints: 1200 (large tablet), 992 (tablet), 768 (mobile landscape), 480 (mobile)
 */

/* Touch-friendly base */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

button,
a,
input,
select,
textarea,
.nav-item,
.btn,
.period-tab,
.dist-card-clickable,
.dist-purchase-row-clickable,
.staff-row {
    -webkit-tap-highlight-color: rgba(245, 158, 11, 0.2);
}

/* Prevent iOS zoom on input focus */
@media (max-width: 768px) {
    input,
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ========== Large tablet (≤1200px) ========== */
@media (max-width: 1200px) {
    :root {
        --sidebar-width: 260px;
    }

    .main-content {
        padding: 22px 20px;
    }

    .order-filters-row {
        grid-template-columns: 1fr 1fr;
    }

    .order-search-group {
        grid-column: 1 / -1;
    }

    .price-sheet-header {
        grid-template-columns: 120px 1fr 120px;
        gap: 12px;
    }
}

/* ========== Tablet (≤992px) — sidebar drawer ========== */
@media (max-width: 992px) {
    .sidebar {
        width: min(88vw, 300px);
        transform: translateX(-100%);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay.visible {
        display: block;
    }

    .main-wrapper {
        margin-left: 0;
        width: 100%;
    }

    .menu-toggle {
        display: flex;
        flex-shrink: 0;
    }

    .topbar {
        padding: 12px 16px;
        gap: 8px;
        position: sticky;
        top: 0;
        z-index: 100;
    }

    .topbar-left {
        gap: 10px;
    }

    .so-staff-sale-officer .topbar-left {
        max-width: 44%;
        padding-right: 8px;
    }

    .so-staff-sale-officer .topbar-center {
        left: calc(50% + 18px);
        max-width: calc(100% - 155px);
    }

    .so-staff-sale-officer .topbar-left .page-heading {
        max-width: 100%;
        font-size: 0.95rem;
    }

    .page-heading {
        font-size: 1.1rem;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .topbar-brand-logo-wrap {
        min-width: 76px;
        max-width: 112px;
        padding: 3px 8px;
    }

    .topbar-brand-logo {
        height: 26px;
        max-width: 100px;
        min-width: 72px;
    }

    .topbar-user {
        display: flex;
        align-items: center;
        gap: 6px;
        flex-shrink: 0;
        max-width: 42%;
    }

    .topbar-user .role-badge {
        font-size: 0.6rem;
        padding: 2px 6px;
    }

    .topbar-user {
        font-size: 0.75rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .main-content {
        padding: 16px;
    }

    .page-card {
        max-width: 100%;
        padding: 18px 16px;
    }

    .page-card-wide {
        padding: 18px 14px;
    }

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

    .page-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .page-toolbar > div:first-child {
        width: 100%;
    }

    .page-toolbar .btn,
    .page-toolbar-actions .btn,
    .page-toolbar > .btn {
        width: 100%;
        justify-content: center;
    }

    .page-toolbar-actions {
        width: 100%;
        flex-direction: column;
    }

    .price-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .price-actions {
        justify-content: stretch;
    }

    .price-actions .btn {
        flex: 1;
        min-width: 0;
    }

    .cards-grid {
        grid-template-columns: 1fr;
    }

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

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

    .order-filters-row {
        grid-template-columns: 1fr;
    }

    .order-filter-actions {
        width: 100%;
    }

    .order-filter-actions .btn {
        flex: 1;
    }

    .dash-hero {
        padding: 18px 16px;
    }

    .dash-hero-title {
        font-size: 1.2rem;
    }

    .dash-kpi-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .dash-metrics-strip {
        grid-template-columns: 1fr;
    }

    .dash-charts-grid {
        grid-template-columns: 1fr;
    }

    .dash-chart-card.dash-chart-wide {
        grid-column: span 1;
    }

    .dash-panels-grid {
        grid-template-columns: 1fr;
    }

    .dash-quick-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .price-sheet-header {
        grid-template-columns: 1fr;
        text-align: center;
    }

    .price-logo-wrap {
        justify-content: center;
    }

    .primary-totals-card {
        grid-template-columns: 1fr !important;
    }

    .officer-info-card .officer-detail-grid {
        grid-template-columns: 1fr;
    }

    .complaint-card-header {
        flex-direction: column;
    }

    .complaint-details-table,
    .complaint-details-table tr,
    .complaint-details-table td {
        display: block;
        width: 100%;
    }

    .complaint-detail-label {
        width: 100%;
    }

    .complaint-badges {
        text-align: left;
    }

    /* Tables: smooth horizontal scroll on touch devices */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    p.table-hint + .table-wrap::before,
    .table-hint + .table-wrap::before {
        content: "Swipe left/right to see all columns →";
        display: block;
        font-size: 0.72rem;
        color: #94a3b8;
        margin-bottom: 8px;
        font-style: italic;
    }

    .data-table {
        min-width: 560px;
        font-size: 0.8rem;
    }

    .customer-order-table,
    .order-mgmt-table {
        min-width: 720px;
    }

    .primary-sale-table {
        min-width: 680px;
    }

    .data-table th,
    .data-table td {
        padding: 10px 10px;
    }

    .order-party-cell,
    .order-datetime {
        min-width: 0;
        max-width: none;
    }

    .primary-totals-card {
        grid-template-columns: 1fr;
    }

    .line-items-container .form-grid {
        grid-template-columns: 1fr;
    }

    .purchase-form-panel .form-actions {
        flex-direction: column;
    }

    .purchase-form-panel .form-actions .btn {
        width: 100%;
    }

    .td-actions {
        white-space: nowrap;
    }

    .btn-sm {
        padding: 8px 12px;
        font-size: 0.72rem;
        min-height: 36px;
    }

    .btn-update-delivery {
        white-space: normal;
        text-align: center;
        line-height: 1.2;
    }

    .modal-overlay {
        padding: 12px;
        align-items: flex-start;
        overflow-y: auto;
    }

    .modal-panel {
        max-width: 100%;
        max-height: min(92dvh, calc(100vh - 1.5rem));
        border-radius: 16px;
        margin: auto;
    }

    .distribution-modal-panel,
    .order-delivery-modal {
        max-width: 100%;
    }

    .modal-header {
        padding: 16px;
    }

    .modal-body {
        padding: 16px;
    }

    .modal-footer {
        flex-wrap: wrap;
        padding: 12px 16px;
        gap: 8px;
    }

    .flash-container {
        padding: 0 16px;
        margin-top: 8px;
    }

    .flash {
        font-size: 0.85rem;
    }
}

/* ========== Mobile (≤768px) ========== */
@media (max-width: 768px) {
    .topbar-user {
        max-width: 50%;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .stat-card {
        padding: 16px;
    }

    .stat-value {
        font-size: 1.5rem;
    }

    .dash-kpi-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .dash-kpi-value {
        font-size: 1.4rem;
    }

    .dash-kpi-link {
        padding: 14px 12px;
    }

    .dash-quick-grid {
        grid-template-columns: 1fr;
    }

    .form-actions {
        flex-direction: column;
    }

    .form-actions .btn {
        width: 100%;
    }

    .btn {
        min-height: 44px;
        padding: 12px 16px;
    }

    .btn-icon {
        min-width: 44px;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .period-tab {
        flex: 1;
        text-align: center;
        min-width: calc(50% - 4px);
        padding: 10px 8px;
    }

    .order-period-tabs {
        gap: 6px;
    }

    .order-pagination {
        flex-wrap: wrap;
        gap: 10px;
    }

    .line-item-row,
    .purchase-line-card {
        padding: 12px;
    }

    .grand-totals-card,
    .primary-totals-card {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .assign-panel-inner {
        gap: 10px;
    }

    .detail-grid {
        grid-template-columns: 1fr;
    }

    .login-card {
        padding: 24px 20px;
    }

    .login-wrapper {
        padding: 16px;
        max-width: 100%;
    }

    .quick-actions {
        flex-direction: column;
    }

    .quick-actions .btn {
        width: 100%;
    }

    .dist-card-header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .page-description {
        font-size: 0.875rem;
    }

    .toolbar-title {
        font-size: 1rem;
    }

    .customer-order-sheet-page .page-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    #toggleOrderFormBtn,
    #togglePurchaseFormBtn {
        width: 100%;
    }
}

/* ========== Small mobile (≤480px) ========== */
@media (max-width: 480px) {
    .sidebar {
        width: 100%;
        max-width: 100%;
    }

    .brand-title {
        font-size: 0.9rem;
    }

    .nav-item {
        padding: 14px 16px;
        font-size: 0.9rem;
        min-height: 48px;
    }

    .page-heading {
        font-size: 1rem;
    }

    .topbar-user .role-badge {
        display: none;
    }

    .stats-grid,
    .order-stats-grid {
        grid-template-columns: 1fr;
    }

    .dash-kpi-grid {
        grid-template-columns: 1fr;
    }

    .dash-hero-meta {
        width: 100%;
        text-align: left;
        margin-top: 8px;
    }

    .dash-hero {
        flex-direction: column;
    }

    .order-stat-value {
        font-size: 1.4rem;
    }

    .data-table {
        min-width: 520px;
    }

    .customer-order-table,
    .order-mgmt-table {
        min-width: 640px;
    }

    .modal-overlay {
        padding: 8px;
        overflow-y: auto;
        align-items: flex-start;
    }

    .modal-panel {
        border-radius: 12px;
        max-height: min(94dvh, calc(100vh - 1rem));
        margin: auto;
    }

    .modal-footer {
        flex-direction: column-reverse;
    }

    .modal-footer .btn {
        width: 100%;
    }

    .modal-footer form {
        width: 100%;
    }

    .modal-footer form .btn {
        width: 100%;
    }

    .inline-form {
        display: block;
        width: 100%;
    }

    .inline-form .btn {
        width: 100%;
        margin-top: 4px;
    }

    .td-actions .inline-form {
        display: inline-block;
        width: auto;
    }

    .complaint-resolve-form .btn {
        width: 100%;
    }

    .price-sheet {
        padding: 12px;
    }

    .flash {
        padding: 10px 12px;
    }
}

/* ========== Landscape phone ========== */
@media (max-width: 992px) and (orientation: landscape) {
    .sidebar {
        width: min(70vw, 280px);
    }

    .modal-panel {
        max-height: 85vh;
    }

    .dash-chart-wrap.dash-chart-tall {
        height: 180px;
    }
}

/* Safe area (notched phones) */
@supports (padding: max(0px)) {
    .sidebar {
        padding-left: max(0px, env(safe-area-inset-left));
    }

    .main-content {
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }

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

    .login-wrapper {
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }
}

/* Print unchanged */
@media print {
    .sidebar,
    .menu-toggle,
    .sidebar-overlay,
    .topbar {
        display: none !important;
    }

    .main-wrapper {
        margin-left: 0 !important;
    }
}
