/* ══════════════════════════════════════════════════════════════════════
   COFFEE HOUSE POS — Complete Cross-Device Responsive System
   Supports: Mobile (320px–575px), Phablets (576px–767px),
             Tablets Portrait (768px–991px), Tablet Landscape / Laptops (992px–1399px),
             and Wide Desktop Screens (≥1400px).
   ══════════════════════════════════════════════════════════════════════ */

/* ── 1. GLOBAL RESETS & SAFE AREA NORMALIZATION ─────────────────────── */
:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --mobile-nav-height: 64px;
}

html, body {
    overflow-x: clip;
    max-width: 100vw;
    width: 100%;
}

*, *::before, *::after {
    box-sizing: border-box;
}

/* Prevent iOS input auto-zoom (requires >=16px font size on touch inputs) */
@supports (-webkit-touch-callout: none) {
    input:not([type="checkbox"]):not([type="radio"]),
    select, textarea {
        font-size: 16px !important;
    }
    body {
        -webkit-text-size-adjust: 100%;
    }
}

/* Smooth Touch Interactions */
button, .btn, .card-product, .nav-link, .mobile-nav-item, .dropdown-item,
.page-link, .form-check-input, .btn-quick-add, .badge-pill {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* ── 2. RESPONSIVE TABLES & HORIZONTAL SCROLL CONTAINERS ───────────── */
.table-responsive {
    width: 100%;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    position: relative;
    border-radius: var(--r-md, 12px);
}

.table-responsive::-webkit-scrollbar {
    height: 6px;
    width: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: rgba(212, 149, 106, 0.4);
    border-radius: 99px;
}

.table-responsive::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.04);
}

/* Table Text Optimization */
.table th, .table td {
    vertical-align: middle;
}

.table .action-col, .table td.text-end, .table th.text-end {
    white-space: nowrap !important;
    min-width: max-content;
}

/* DataTables Responsive Controls */
.dataTables_wrapper {
    width: 100%;
    overflow-x: hidden;
}

.dataTables_wrapper .dataTables_length,
.dataTables_wrapper .dataTables_filter {
    margin-bottom: 0.75rem;
}

.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_paginate {
    margin-top: 0.75rem;
}

/* ── 3. HORIZONTAL SCROLLABLE TAB BARS & PILLS ─────────────────────── */
.nav-pills.report-nav-pills,
.report-nav-wrapper .report-nav-pills,
.settings-tab-nav,
.custom-inventory-tabs,
.table-zone-tabs,
.nav-pills-scrollable,
#posCategoryPills {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    gap: 8px;
    padding-bottom: 6px;
}

.nav-pills.report-nav-pills::-webkit-scrollbar,
.report-nav-wrapper .report-nav-pills::-webkit-scrollbar,
.settings-tab-nav::-webkit-scrollbar,
.custom-inventory-tabs::-webkit-scrollbar,
.table-zone-tabs::-webkit-scrollbar,
.nav-pills-scrollable::-webkit-scrollbar,
#posCategoryPills::-webkit-scrollbar {
    display: none !important;
    height: 0 !important;
}

.nav-pills.report-nav-pills .nav-link,
.settings-tab-nav a,
.custom-inventory-tabs .nav-link,
#posCategoryPills .btn {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
}

/* ── 4. RESPONSIVE CARDS & KPI GRIDS ───────────────────────────────── */
.kpi-card, .stat-card, .dev-card-stat {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.kpi-value, .stat-value {
    font-size: clamp(1.25rem, 2.2vw, 1.85rem) !important;
    word-break: break-word;
}

.card-header, .card-footer {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

/* ── 5. FORM CONTROLS & TOUCH TARGETS ──────────────────────────────── */
.form-label {
    font-weight: 600;
    margin-bottom: 4px;
}

.input-group {
    flex-wrap: nowrap;
}

.input-group > .form-control,
.input-group > .form-select {
    min-width: 0;
}

/* ── 6. BREAKPOINT: LARGE DESKTOP & WIDESCREEN (≥ 1400px) ─────────── */
@media (min-width: 1400px) {
    :root {
        --sidebar-w: 268px;
    }
    .main-content {
        padding: 28px 36px;
    }
    .pos-page .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)) !important;
    }
}

/* ── 7. BREAKPOINT: STANDARD LAPTOP (1200px – 1399.98px) ──────────── */
@media (min-width: 1200px) and (max-width: 1399.98px) {
    :root {
        --sidebar-w: 250px;
    }
    .main-content {
        padding: 22px 28px;
    }
    .pos-page .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(155px, 1fr)) !important;
    }
}

/* ── 8. BREAKPOINT: TABLET LANDSCAPE & SMALL LAPTOP (992px – 1199.98px) */
@media (min-width: 992px) and (max-width: 1199.98px) {
    :root {
        --sidebar-w: 220px;
    }
    .main-content {
        padding: 18px 22px;
    }
    .app-sidebar .sidebar-nav .nav-link {
        font-size: 0.82rem;
        padding: 9px 10px;
    }
    .pos-page .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)) !important;
        gap: 10px;
    }
    .page-title {
        font-size: 1.5rem !important;
    }
}

/* ── 8B. POS 2-COLUMN: KERANJANG DI KIRI + MENU DI KANAN (KIOSK / FULLSCREEN >= 992px) ────────── */
@media (min-width: 992px) {
    .pos-full-3col,
    .pos-page.row {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: stretch !important;
        gap: 1rem !important;
    }
    .pos-full-3col > .pos-col-cart,
    .pos-page > .pos-col-cart {
        flex: 0 0 38% !important;
        max-width: 38% !important;
        min-width: 320px !important;
    }
    .pos-full-3col > .pos-col-menu,
    .pos-page > .pos-col-menu {
        flex: 1 1 62% !important;
        max-width: 62% !important;
        min-width: 350px !important;
    }
    .pos-full-3col .cart-panel,
    .pos-page .cart-panel {
        height: 100% !important;
        min-height: calc(100vh - 85px) !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        overflow-y: auto !important;
    }
    .pos-full-3col .pos-menu-card,
    .pos-page .pos-menu-card {
        height: 100% !important;
        min-height: calc(100vh - 85px) !important;
        max-height: calc(100vh - 85px) !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }
    .pos-full-3col #productList,
    .pos-page #productList {
        flex: 1 1 auto !important;
        max-height: calc(100vh - 190px) !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    .pos-full-3col .cart-table-wrapper,
    .pos-page .cart-table-wrapper {
        max-height: 160px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }
    .pos-full-3col .product-grid,
    .pos-page .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)) !important;
        gap: 12px !important;
    }
}

@media (min-width: 1600px) {
    .pos-full-3col > .pos-col-cart,
    .pos-page > .pos-col-cart {
        flex: 0 0 38% !important;
        max-width: 38% !important;
    }
    .pos-full-3col > .pos-col-menu,
    .pos-page > .pos-col-menu {
        flex: 0 0 60.5% !important;
        max-width: 60.5% !important;
    }
    .pos-full-3col .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)) !important;
    }
}

/* ── 9. BREAKPOINT: TABLET PORTRAIT (768px – 991.98px) ─────────────── */
@media (min-width: 768px) and (max-width: 991.98px) {
    :root {
        --sidebar-w: 72px;
    }

    .main-content {
        padding: 18px 20px;
        min-width: 0;
    }

    /* POS 2-Column Split for Tablet Portrait */
    .pos-page.row {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        gap: 12px !important;
    }
    .pos-page > .pos-col-cart {
        width: 42% !important;
        max-width: 42% !important;
        flex: 0 0 42% !important;
    }
    .pos-page > .pos-col-menu {
        width: 58% !important;
        max-width: 58% !important;
        flex: 0 0 58% !important;
    }

    .pos-page .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(135px, 1fr)) !important;
        gap: 10px;
    }

    .pos-page .cart-panel {
        position: relative !important;
        top: auto !important;
    }

    /* KDS Kitchen Tablet 2-3 Column Flow */
    .kds-column-card {
        margin-bottom: 16px;
    }

    /* Settings Split Grid on Tablet */
    .settings-page .row.g-4 > .col-lg-8,
    .settings-page .row.g-4 > .col-lg-7,
    .settings-page .row.g-4 > .col-lg-5 {
        width: 100% !important;
    }

    .settings-section[style*="position:sticky"] {
        position: relative !important;
        top: auto !important;
    }
}

/* ── 10. BREAKPOINT: ALL MOBILE PHONES & PHABLETS (< 768px) ────────── */
@media (max-width: 767.98px) {
    /* Main Content Spacing for Floating Bottom Bar */
    .main-content {
        padding: 14px 14px calc(92px + var(--safe-bottom)) 14px !important;
        min-height: calc(100vh - var(--navbar-h, 60px));
    }

    /* Top Navbar Mobile Polish */
    .app-navbar {
        height: 58px;
        padding: 0 12px;
    }
    .app-navbar .brand span.d-none.d-md-inline {
        display: none !important;
    }
    .app-navbar .brand-icon {
        width: 30px; height: 30px; font-size: 0.95rem;
    }
    .btn-avatar {
        padding: 3px 8px 3px 3px !important;
        font-size: 0.78rem !important;
    }
    .btn-avatar .avatar-img {
        width: 26px; height: 26px; font-size: 0.75rem;
    }

    /* Off-canvas Sidebar Drawer */
    .app-sidebar {
        position: fixed !important;
        top: 0 !important;
        bottom: 0 !important;
        left: 0 !important;
        width: min(300px, 84vw) !important;
        height: 100vh !important;
        z-index: 1050 !important;
        transform: translateX(-100%) !important;
        transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.3s ease !important;
        box-shadow: none !important;
        padding-top: var(--safe-top);
        padding-bottom: var(--safe-bottom);
    }

    .app-sidebar.open {
        transform: translateX(0) !important;
        box-shadow: 10px 0 50px rgba(0, 0, 0, 0.5) !important;
    }

    /* Floating Bottom Navigation Bar */
    .mobile-bottom-nav {
        position: fixed !important;
        bottom: calc(10px + var(--safe-bottom)) !important;
        left: 10px !important;
        right: 10px !important;
        z-index: 9990 !important;
    }

    .mobile-nav-inner {
        padding: 6px 6px;
        border-radius: 24px;
    }

    .mobile-nav-item {
        min-height: 48px;
        padding: 4px 2px;
    }
    .mobile-nav-icon {
        font-size: 1.15rem;
        margin-bottom: 2px;
    }
    .mobile-nav-label {
        font-size: 0.65rem;
    }

    /* Page Header Cards & Banners */
    .page-header-card, .dev-hero-banner {
        padding: 16px 16px !important;
        border-radius: var(--r-lg, 16px) !important;
        margin-bottom: 16px !important;
        position: relative !important;
    }
    .page-header-card::before, .dev-hero-banner::before {
        pointer-events: none !important;
    }
    .page-header-card a, .page-header-card button {
        position: relative !important;
        z-index: 10 !important;
        pointer-events: auto !important;
    }
    .page-title {
        font-size: 1.3rem !important;
    }
    .page-subtitle {
        font-size: 0.8rem !important;
    }

    /* Flex Header Bars Wrap */
    .d-flex.justify-content-between.align-items-center,
    .d-flex.justify-content-between {
        flex-wrap: wrap !important;
        gap: 10px !important;
    }

    .d-flex.gap-2.align-items-center,
    .d-flex.gap-2 {
        flex-wrap: wrap !important;
    }

    /* Buttons inside Header Bars take auto width or stack */
    .btn {
        min-height: 42px;
        font-size: 0.88rem;
    }
    .btn-sm {
        min-height: 36px;
        font-size: 0.82rem;
    }

    /* Button Groups & Toolbars */
    .btn-group, .btn-toolbar {
        flex-wrap: wrap !important;
        width: 100%;
        gap: 6px;
    }
    .btn-group > .btn {
        flex: 1 1 auto;
        margin-left: 0 !important;
    }

    /* Cards and Spacing */
    .card-header, .card-footer {
        padding: 0.75rem 1rem !important;
    }
    .card-body {
        padding: 1rem !important;
    }

    /* Forms */
    .form-control, .form-select {
        min-height: 44px !important;
        font-size: 0.95rem;
    }
    .form-control-sm, .form-select-sm {
        min-height: 38px !important;
        font-size: 0.86rem;
    }

    /* Modal as Bottom Sheet */
    .modal-dialog {
        margin: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
    }

    .modal.fade .modal-dialog {
        transform: translateY(100%) !important;
        transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) !important;
    }

    .modal.show .modal-dialog {
        transform: translateY(0) !important;
    }

    .modal-content {
        border-radius: 24px 24px 0 0 !important;
        border-bottom: none !important;
        max-height: 88vh !important;
        overflow-y: auto !important;
        padding-bottom: calc(16px + var(--safe-bottom)) !important;
    }

    .modal-header {
        position: sticky;
        top: 0;
        z-index: 10;
        background: inherit;
        backdrop-filter: blur(10px);
        padding: 14px 18px !important;
    }

    .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 10;
        background: inherit;
        backdrop-filter: blur(10px);
        padding: 12px 18px !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
    }
    .modal-footer .btn {
        flex: 1 1 auto;
    }

    /* POS Mobile Layout */
    .pos-page.row {
        display: flex !important;
        flex-direction: column !important;
        margin: 0 !important;
    }
    .pos-page > .col-lg-7,
    .pos-page > .col-lg-5 {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 0 !important;
        padding-right: 0 !important;
    }

    .pos-page .product-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    .pos-page .card-product {
        min-height: 200px !important;
        padding: 10px !important;
        border-radius: 14px !important;
    }

    .pos-page .product-image {
        height: 95px !important;
    }

    .pos-page .product-name {
        font-size: 0.82rem !important;
        min-height: 32px;
    }

    .pos-page .product-price {
        font-size: 0.88rem !important;
    }

    .pos-page .cart-panel {
        position: relative !important;
        top: auto !important;
        margin-top: 14px !important;
        border-radius: 16px !important;
    }

    /* POS Search Bar */
    .style-search {
        min-width: 100% !important;
        width: 100% !important;
        margin-top: 6px;
    }

    /* Toasts & Update Banners */
    #pwa-toast-container {
        bottom: calc(76px + var(--safe-bottom)) !important;
    }
    .pwa-toast {
        max-width: 90vw;
        white-space: normal !important;
        text-align: center;
        font-size: 0.8rem;
    }

    #pwa-update-banner {
        top: auto !important;
        bottom: calc(82px + var(--safe-bottom)) !important;
        left: 12px !important;
        right: 12px !important;
        transform: translateY(120%) !important;
    }
    #pwa-update-banner.show {
        transform: translateY(0) !important;
    }
    .pwa-banner-content {
        white-space: normal !important;
    }
}

/* ── 11. BREAKPOINT: SMALL MOBILE PHONES (< 576px) ─────────────────── */
@media (max-width: 575.98px) {
    body {
        font-size: 13.5px;
    }

    .main-content {
        padding: 10px 10px calc(88px + var(--safe-bottom)) 10px !important;
    }

    /* KPI Cards in 2 columns or 1 column */
    .row.g-3 > .col-xl-3.col-md-6,
    .row.g-3 > .col-md-3.col-6 {
        width: 50% !important;
        flex: 0 0 50% !important;
    }

    .kpi-card .card-body, .stat-card .card-body {
        padding: 0.75rem !important;
    }

    .kpi-icon {
        width: 36px !important;
        height: 36px !important;
        font-size: 1rem !important;
    }

    .kpi-label, .stat-label {
        font-size: 0.65rem !important;
    }

    .kpi-value, .stat-value {
        font-size: 1.15rem !important;
    }

    /* Table Zone & Filter Summary Cards */
    #tableCardsGrid > [class*="col-"] {
        width: 100% !important;
    }

    /* Product Grid inside Products Management */
    .products-page .products-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    .products-page .product-thumb {
        height: 90px !important;
    }

    /* Chart Containers */
    #salesChart, .chart-container canvas {
        height: 200px !important;
        max-height: 220px !important;
    }

    /* KDS Kitchen Cards */
    .kds-col {
        width: 100% !important;
    }

    /* Auth & Login Page */
    .login-wrapper {
        padding: 0 !important;
    }
    .login-card {
        border-radius: 20px !important;
    }
    .login-left {
        padding: 24px 18px !important;
    }
    .login-right {
        padding: 24px 18px !important;
    }
    .brand-title {
        font-size: 1.35rem !important;
    }
}

/* ── 12. BREAKPOINT: EXTRA SMALL PHONES (< 380px) ──────────────────── */
@media (max-width: 379.98px) {
    .main-content {
        padding: 8px 8px calc(84px + var(--safe-bottom)) 8px !important;
    }

    .row.g-3 > .col-xl-3.col-md-6,
    .row.g-3 > .col-md-3.col-6 {
        width: 100% !important;
        flex: 0 0 100% !important;
    }

    .pos-page .product-grid {
        grid-template-columns: 1fr !important;
    }

    .pos-page .product-image {
        height: 120px !important;
    }

    .mobile-nav-label {
        display: none !important;
    }
    .mobile-nav-icon {
        font-size: 1.3rem !important;
        margin-bottom: 0 !important;
    }
}

/* ── 13. TOUCH DEVICES OPTIMIZATION (pointer: coarse) ──────────────── */
@media (pointer: coarse) {
    .nav-link, .dropdown-item, .page-link, .form-check-input {
        min-height: 42px;
    }
    .nav-link, .dropdown-item {
        display: inline-flex;
        align-items: center;
    }
    .btn {
        min-height: 44px;
    }
    .btn-sm {
        min-height: 38px;
    }
    .table td .btn, .table th .btn {
        min-height: 36px;
        min-width: 36px;
    }
    .form-control, .form-select {
        min-height: 44px;
    }
}

/* ── 14. PRINT STYLES ──────────────────────────────────────────────── */
@media print {
    .app-sidebar, .app-navbar, .mobile-bottom-nav,
    #sidebarCollapseBtn, #sidebarMobileBtn, #sidebarOverlay,
    #pwa-install-float, #pwa-toast-container, #pwa-offline-banner,
    .btn:not(.btn-print-keep), .modal, .dropdown {
        display: none !important;
    }
    .main-content {
        padding: 0 !important;
        margin: 0 !important;
        width: 100% !important;
        background: #ffffff !important;
        color: #000000 !important;
    }
    .card, .panel, .glass-card {
        box-shadow: none !important;
        border: 1px solid #ddd !important;
        background: #fff !important;
    }
}