/*
 * CafePOS+ cross-device operational tuning (admin + cashier + public staff views).
 * Preserves existing aesthetic — layout, touch, overflow, and modal fixes only.
 */

/* ----- Global: prevent horizontal bleed on phones/tablets ----- */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

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

body.cafepos-staff,
body:has(.sidebar) {
    min-height: 100vh;
    min-height: 100dvh;
}

/* Resolve legacy style.css fixed sidebar on small screens (async.css wins with !important) */
@media (max-width: 991.98px) {
    body:not(.cafepos-admin):not(.cafepos-cashier) .sidebar {
        position: sticky !important;
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
    }

    .cafepos-table-region .mobile-table-scroll-hint {
        margin-bottom: 0.5rem;
        font-size: 0.8125rem;
        color: var(--bs-secondary-color, #6c757d);
    }

    .cafepos-surface-mobile .card:not(.pos-cart-panel):not(.pos-product-card):not(.addon-card),
    .cafepos-surface-mobile .table-responsive,
    .cafepos-surface-mobile .operational-table-wrap {
        content-visibility: auto;
        contain-intrinsic-size: auto 280px;
    }

    /* POS cart/menu must repaint on every HTMX OOB — never defer visibility */
    body.cafepos-cashier .pos-cart-panel,
    body.cafepos-cashier #pos-workspace,
    body.cafepos-cashier #cart-items,
    body.cafepos-cashier .cart-line,
    body.cafepos-cashier .pos-product-card,
    body.cafepos-cashier .addon-card {
        content-visibility: visible !important;
        contain-intrinsic-size: unset;
    }

    body.cafepos-admin .main {
        overflow-x: clip;
        -webkit-overflow-scrolling: touch;
    }

    .main,
    .main-content {
        margin-left: 0 !important;
        width: 100%;
        max-width: 100%;
    }

    .main {
        padding: 1rem 1rem 5.5rem;
        padding-bottom: calc(5.5rem + env(safe-area-inset-bottom, 0px));
    }

    body:has(.admin-notification-bell) .main {
        padding-top: 3.25rem;
    }

    .admin-notification-bell {
        top: max(0.5rem, env(safe-area-inset-top, 0px));
        right: max(0.65rem, env(safe-area-inset-right, 0px));
    }

    .page-hero {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.75rem;
    }

    .page-hero .d-flex.gap-2,
    .page-hero .btn-group,
    .responsive-toolbar {
        width: 100%;
        flex-wrap: wrap;
    }

    .page-hero .btn,
    .responsive-toolbar .btn,
    .responsive-toolbar .form-control {
        min-height: 44px;
    }

    .admin-dashboard-kpis .card-body,
    .metric-card .card-body {
        padding: 0.85rem 1rem;
    }

    .admin-kpi-hero-value {
        font-size: clamp(1.35rem, 6vw, 1.85rem);
        word-break: break-word;
    }

    .admin-kpi-micro {
        font-size: 0.78rem;
        line-height: 1.45;
    }

    .cashier-pos-grid {
        --bs-gutter-y: 1rem;
    }

    .cashier-pos-grid > .col-lg-4 {
        order: -1;
    }

    .pos-cart-panel {
        position: static !important;
        top: auto !important;
        z-index: 5;
    }

    body.cafepos-cashier-mobile .pos-cart-panel .card-body {
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
    }

    body.cafepos-cashier-mobile .pos-parked-banner {
        max-height: 12rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    body.cafepos-cashier-mobile #pos-order-class-region {
        margin-bottom: 0.5rem;
    }

    body.cafepos-cashier-mobile .cart-qty-controls {
        gap: 0.35rem;
    }

    /* Operational tables: scroll inside container, not the page */
    .table-responsive,
    .operational-table-wrap {
        display: block;
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
        margin-bottom: 0.5rem;
    }

    .table-responsive > .table,
    .operational-table-wrap > .table {
        margin-bottom: 0;
        min-width: 640px;
    }

    .table.align-middle th,
    .table.align-middle td {
        white-space: nowrap;
        vertical-align: middle;
    }

    /* Pricing: hide non-critical columns on phones */
    .pricing-table th:nth-child(4),
    .pricing-table td:nth-child(4),
    .pricing-table th:nth-child(6),
    .pricing-table td:nth-child(6) {
        display: none;
    }

    .pricing-table .text-end {
        white-space: nowrap;
    }

    .pricing-inline-form,
    .pricing-table .btn {
        min-height: 40px;
    }

    .receipt-modal-dialog {
        max-width: min(100%, 420px);
        margin: 0.75rem auto;
    }

    .receipt-modal-footer {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .receipt-modal-footer .btn {
        flex: 1 1 auto;
        min-height: 44px;
    }

    .action-button,
    .btn-success,
    .btn-outline-secondary,
    .btn-outline-primary {
        min-height: 44px;
    }

    .sidebar .nav-link {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
    }

    /* Modals: fit small screens, scroll content */
    .modal-dialog {
        margin: 0.65rem;
        max-width: calc(100% - 1.25rem);
    }

    #appModal .modal-dialog,
    .modal-dialog.modal-fullscreen-sm-down {
        max-height: calc(100dvh - 1.25rem);
    }

    #appModal .modal-content,
    .modal-content {
        max-height: calc(100dvh - 1.25rem);
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    #appModal .modal-body,
    #app-modal-content {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .form-control,
    .form-select {
        font-size: 16px; /* prevents iOS zoom-on-focus */
        min-height: 44px;
    }

    .form-control-sm {
        min-height: 40px;
        font-size: 16px;
    }
}

@media (max-width: 767.98px) {
    .dash-insight-card .row > [class*="col-"],
    .admin-financial-card .row > [class*="col-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }

    .table-action-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }

    .table-action-group .btn,
    .table-action-group form {
        width: 100%;
    }
}

@media (max-width: 575.98px) {
    .receipt-modal-dialog {
        max-width: calc(100% - 0.5rem);
        margin: 0.5rem;
    }

    .receipt-modal-dialog .modal-content {
        border-radius: 16px;
    }

    .receipt-shell.thermal-screen-shell {
        margin-inline: auto;
    }

    #pos-checkout-form .btn {
        min-height: 48px;
        font-size: 1rem;
    }

    .card.metric-card h3,
    .card.metric-card h4 {
        font-size: 1.15rem;
    }

    .admin-notification-dropdown {
        width: calc(100vw - 1rem);
        max-width: calc(100vw - 1rem);
        right: 0.5rem !important;
        left: 0.5rem !important;
    }
}

@media (min-width: 768px) and (max-width: 1199.98px) {
    .cashier-pos-grid > .col-lg-8,
    .cashier-pos-grid > .col-lg-4 {
        flex: 0 0 100%;
        max-width: 100%;
    }

    .adaptive-menu-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

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

@media (hover: none) and (pointer: coarse) {
    .icon-button,
    .cart-qty-controls .icon-button,
    .customer-card-action,
    .pos-product-card .action-button {
        min-height: 44px;
        min-width: 44px;
        touch-action: manipulation;
    }

    .btn,
    .nav-link,
    .dropdown-item {
        touch-action: manipulation;
    }

    .receipt-htmx-trigger {
        min-height: 40px;
        padding-inline: 0.75rem;
    }

    .pricing-price-input {
        min-width: 6.5rem;
        max-width: 8rem;
    }

    .pricing-inline-form {
        flex-wrap: wrap;
    }

    /* Disable hover lift on touch devices */
    .card:hover,
    .interactive-card:hover,
    .glass-card:hover {
        transform: none;
    }

    /* POS cart: avoid fade/repaint spikes during rapid HTMX OOB updates */
    body.cafepos-cashier-mobile .pos-cart-panel,
    body.cafepos-cashier-mobile #cart-items,
    body.cafepos-cashier-mobile #pos-cart-footer {
        transition: none !important;
    }

    body.cafepos-cashier-mobile #cart-items .async-fade.htmx-swapping,
    body.cafepos-cashier-mobile #cart-items .async-fade.htmx-settling {
        opacity: 1 !important;
        transform: none !important;
        animation: none !important;
    }

    body.cafepos-cashier-mobile #pos-menu-grid .menu-item {
        content-visibility: auto;
        contain-intrinsic-size: 280px 320px;
    }

    body.cafepos-cashier-mobile #cart-items {
        max-height: min(42vh, 360px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        contain: none;
    }

    body.cafepos-cashier-mobile .cart-line {
        contain: none;
    }

    body.cafepos-cashier-mobile #pos-cart-totals-region {
        contain: layout;
    }

    body.cafepos-cashier-mobile .pos-cart-panel {
        scroll-margin-top: calc(3.5rem + env(safe-area-inset-top, 0px));
    }

    body.cafepos-cashier-mobile .pos-cart-panel.is-cart-updated {
        outline: 2px solid rgba(45, 106, 79, 0.45);
        outline-offset: 2px;
        transition: outline-color 0.35s ease;
    }

    body.cafepos-cashier-mobile .pos-cart-panel.is-cart-updated:not(.is-cart-updated-active) {
        outline-color: transparent;
    }

    body.cafepos-cashier-mobile [data-pos-cart-op] button[type="submit"],
    body.cafepos-cashier-mobile .pos-product-card form button {
        touch-action: manipulation;
    }
}

/* POS menu availability — on-card only (no page-level toast for stock) */
body.cafepos-cashier .pos-availability-note {
    font-weight: 500;
    line-height: 1.35;
}

body.cafepos-cashier .pos-product-card.availability-caution,
body.cafepos-cashier .addon-card.availability-caution {
    border-color: rgba(255, 193, 7, 0.55);
    box-shadow: 0 0 0 1px rgba(255, 193, 7, 0.2);
}

body.cafepos-cashier .menu-item.is-availability-blocked .pos-availability-badge {
    animation: pos-availability-pulse 0.6s ease 1;
}

@keyframes pos-availability-pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.06);
    }
    100% {
        transform: scale(1);
    }
}

body.cafepos-cashier .pos-cart-flash-region .alert {
    margin-bottom: 0.5rem;
}

body.cafepos-cashier #pos-workspace .async-alerts:not(#pos-cart-flash-region .async-alerts) {
    display: none;
}

/* HTMX busy state — visible feedback on slow mobile networks */
body.htmx-page-busy {
    cursor: progress;
}

body.htmx-page-busy .pos-cart-panel,
body.htmx-page-busy #pricing-workspace {
    pointer-events: auto;
}

.is-async-busy {
    opacity: 0.88;
}

/* Customer / public — touch-friendly controls */
@media (max-width: 991.98px) {
    body.cafepos-customer .customer-main {
        padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
        padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
        padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0px));
    }

    body.cafepos-customer .btn,
    body.cafepos-customer .form-control,
    body.cafepos-customer .form-select {
        min-height: 44px;
    }

    body.cafepos-customer #reservation-order-tray {
        max-height: min(50vh, 420px);
        -webkit-overflow-scrolling: touch;
    }
}

/* POS menu scroll — contain momentum scroll to avoid WebKit repaint crashes */
body.cafepos-cashier #pos-workspace-left-root,
body.cafepos-cashier #pos-menu-grid,
body.cafepos-cashier .adaptive-menu-grid {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

body.cafepos-cashier.cafepos-pos-entry-grace #pos-workspace-left-root {
    pointer-events: auto;
}

body.cafepos-cashier.cafepos-pos-entry-grace .pos-product-card form button[type="submit"] {
    touch-action: manipulation;
}

/* Tablet POS — stacked columns, 3-up menu grid */
@media (min-width: 576px) and (max-width: 991.98px) {
    body.cafepos-cashier-tablet .adaptive-menu-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    body.cafepos-cashier-tablet .cashier-pos-grid > .col-lg-4 {
        order: -1;
    }
}

/* Desktop POS — side-by-side workspace, faster hit targets optional */
@media (min-width: 992px) {
    body.cafepos-cashier-desktop .cashier-pos-grid > .col-lg-8 {
        min-width: 0;
    }

    body.cafepos-cashier-desktop .pos-cart-panel {
        position: sticky;
        top: max(0.75rem, env(safe-area-inset-top, 0px));
    }

    body.cafepos-cashier-desktop .adaptive-menu-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}

/* Save-Data / slow network — fewer animations and shadows */
body.cafepos-save-data.cafepos-cashier .pos-cart-panel.is-cart-updated {
    box-shadow: none;
}

body.cafepos-save-data .async-fade,
body.cafepos-save-data .htmx-indicator-bar {
    animation: none !important;
}

@media (prefers-reduced-motion: reduce) {
    .card,
    .btn,
    .async-fade,
    .sidebar .nav-link {
        transition-duration: 0.01ms !important;
    }
}
