/* =====================================================
   DeliveryPro - Mobile & PWA Enhancements
   ===================================================== */

/* ============ PREVENT ZOOM & TOUCH BEHAVIOR ============ */

/* ЖЁСТКАЯ блокировка zoom */
html {
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: pan-x pan-y;
    -ms-touch-action: pan-x pan-y;
}

body {
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: pan-x pan-y;
    -ms-touch-action: pan-x pan-y;
    overflow-x: hidden !important;
    width: 100%;
    max-width: 100vw;
}

/* Prevent double-tap zoom */
* {
    touch-action: manipulation;
    -ms-touch-action: manipulation;
}

/* Allow specific elements to have custom touch behavior */
.scrollable {
    touch-action: pan-y;
}

.map-container {
    touch-action: pan-x pan-y pinch-zoom;
}

/* Запрет горизонтальной прокрутки */
html, body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
    position: relative;
}

/* Все контейнеры не должны выходить за пределы экрана */
* {
    max-width: 100%;
}

/* ============ SAFE AREA SUPPORT ============ */

:root {
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* Apply Safe Area to body */
body {
    padding-top: var(--safe-area-top);
    padding-bottom: var(--safe-area-bottom);
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

/* ============ MODERN VIEWPORT UNITS ============ */

/* Support for dynamic viewport height */
.full-height {
    height: 100vh; /* Fallback */
    height: 100dvh; /* Dynamic viewport height */
}

.min-full-height {
    min-height: 100vh; /* Fallback */
    min-height: 100dvh;
}

/* Small viewport height (keyboard open) */
.full-height-small {
    height: 100svh;
}

/* Large viewport height (keyboard closed) */
.full-height-large {
    height: 100lvh;
}

/* ============ APP SHELL MOBILE ADJUSTMENTS ============ */

/* Ensure app shell works with Safe Area */
.app-shell {
    min-height: 100vh;
    min-height: 100dvh;
}

/* Topbar with Safe Area */
.topbar {
    padding-top: max(12px, var(--safe-area-top));
    padding-left: max(16px, var(--safe-area-left));
    padding-right: max(16px, var(--safe-area-right));
}

/* ============ MOBILE KEYBOARD BEHAVIOR ============ */

/* Container that adjusts when keyboard is open */
.keyboard-aware {
    transition: padding-bottom 0.2s ease;
}

body.keyboard-open .keyboard-aware {
    padding-bottom: 0;
}

/* Hide elements when keyboard is open */
body.keyboard-open .hide-on-keyboard {
    display: none;
}

/* ============ TOUCH TARGETS ============ */

/* Minimum touch target size */
button,
a,
input,
select,
textarea,
.clickable {
    min-height: 44px;
    min-width: 44px;
}

/* Ensure small buttons still have large touch area */
.btn-sm,
.icon-btn {
    padding: 12px;
}

/* Touch feedback */
button.is-touching,
.btn.is-touching,
.clickable.is-touching {
    opacity: 0.7;
    transform: scale(0.98);
    transition: transform 0.1s ease, opacity 0.1s ease;
}

/* Increase spacing between clickable elements */
@media (max-width: 768px) {
    .btn-group > .btn {
        margin: 4px;
    }

    .table tbody tr {
        cursor: pointer;
        -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
    }

    .table tbody tr:active {
        background: var(--primary-light);
    }

    /* Card touch feedback */
    .card.clickable {
        transition: transform 0.15s ease, box-shadow 0.15s ease;
    }

    .card.clickable:active {
        transform: scale(0.98);
        box-shadow: var(--shadow-sm);
    }

    /* List items */
    .list-item,
    .mini-list li {
        min-height: 48px;
        padding: 12px;
    }
}

/* ============ FORM INPUTS - PREVENT AUTO ZOOM ============ */

/* iOS zooms in on inputs smaller than 16px */
input,
textarea,
select {
    font-size: 16px !important;
}

/* Ensure consistent input styling */
input[type="text"],
input[type="email"],
input[type="tel"],
input[type="number"],
input[type="password"],
input[type="date"],
textarea,
select {
    font-size: 16px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* ============ MODALS ON MOBILE ============ */

@media (max-width: 768px) {
    .modal-backdrop {
        padding: var(--safe-area-top) var(--safe-area-left) var(--safe-area-bottom) var(--safe-area-right);
        align-items: flex-end;
    }

    .modal {
        max-width: 100%;
        max-height: calc(90vh - var(--safe-area-top) - var(--safe-area-bottom));
        max-height: calc(90dvh - var(--safe-area-top) - var(--safe-area-bottom));
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        margin-bottom: 0;
    }

    /* Modal with keyboard open */
    body.keyboard-open .modal {
        max-height: calc(100vh - var(--keyboard-height) - var(--safe-area-top) - 20px);
        max-height: calc(100svh - var(--safe-area-top) - 20px);
    }

    /* Full-screen modal option */
    .modal--fullscreen {
        max-height: calc(100vh - var(--safe-area-top) - var(--safe-area-bottom));
        max-height: calc(100dvh - var(--safe-area-top) - var(--safe-area-bottom));
        border-radius: 0;
    }

    /* Bottom sheet style modal */
    .modal--bottom-sheet {
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        animation: slideUp 0.3s ease;
    }

    @keyframes slideUp {
        from {
            transform: translateY(100%);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }

    /* Ensure modal body is scrollable */
    .modal__body {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-height: calc(70vh - 120px);
        max-height: calc(70dvh - 120px);
    }

    /* Make modal buttons larger on mobile */
    .modal__footer .btn {
        min-height: 48px;
        font-size: 16px;
        flex: 1;
    }

    /* Stack buttons vertically on small screens */
    .modal__footer {
        flex-direction: column-reverse;
        gap: 12px;
    }

    .modal__footer .btn {
        width: 100%;
    }
}

/* ============ FIXED ELEMENTS WITH SAFE AREA ============ */

.fixed-top {
    top: var(--safe-area-top);
}

.fixed-bottom {
    bottom: var(--safe-area-bottom);
}

/* Fix topbar position on mobile */
@media (max-width: 768px) {
    .topbar {
        position: sticky;
        top: 0;
        z-index: 90;
        background: var(--surface);
    }

    /* Hide topbar when keyboard is open to save space */
    body.keyboard-open .topbar {
        transform: translateY(-100%);
        transition: transform 0.3s ease;
    }

    body:not(.keyboard-open) .topbar {
        transform: translateY(0);
        transition: transform 0.3s ease;
    }

    /* App footer */
    .app-footer {
        padding-bottom: calc(80px + var(--safe-area-bottom));
    }

    /* Floating action buttons */
    .fab,
    .floating-button {
        bottom: calc(80px + var(--safe-area-bottom) + 16px);
        right: calc(16px + var(--safe-area-right));
    }

    /* Hide floating buttons when keyboard is open */
    body.keyboard-open .fab,
    body.keyboard-open .floating-button {
        display: none;
    }

    /* Sticky elements should not overlap with tab bar */
    .sticky-bottom {
        bottom: calc(64px + var(--safe-area-bottom));
    }
}

/* ============ MOBILE TAB BAR ============ */

.mobile-tab-bar {
    display: none;
}

@media (max-width: 768px) {
    .mobile-tab-bar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--surface);
        border-top: 1px solid var(--border);
        padding-bottom: var(--safe-area-bottom);
        z-index: 100;
        height: calc(64px + var(--safe-area-bottom));
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    }

    .mobile-tab-bar__item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        padding: 8px 4px;
        color: var(--text-muted);
        text-decoration: none;
        font-size: 11px;
        font-weight: 500;
        position: relative;
        transition: color 0.15s ease;
        min-height: 44px;
        touch-action: manipulation;
    }

    .mobile-tab-bar__item:active {
        background: var(--surface-alt);
    }

    .mobile-tab-bar__item.is-active {
        color: var(--primary);
    }

    .mobile-tab-bar__icon {
        width: 24px;
        height: 24px;
        stroke-width: 2;
    }

    .mobile-tab-bar__badge {
        position: absolute;
        top: 4px;
        right: 50%;
        transform: translateX(12px);
        background: var(--danger);
        color: white;
        font-size: 10px;
        font-weight: 600;
        min-width: 18px;
        height: 18px;
        border-radius: 9px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 4px;
    }

    /* Add padding to content when tab bar is visible */
    .content {
        padding-bottom: calc(80px + var(--safe-area-bottom));
    }

    /* Hide sidebar on mobile, show tab bar instead */
    .sidebar {
        transform: translateX(-100%);
    }

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

/* ============ SCROLL BEHAVIOR ============ */

/* Smooth scrolling for the whole page */
html {
    scroll-behavior: smooth;
}

/* Prevent overscroll bounce on body */
body {
    overscroll-behavior-y: none;
}

/* Allow natural scroll in containers */
.scrollable {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* ============ STANDALONE MODE DETECTION ============ */

/* Detect if app is running as installed PWA */
@media all and (display-mode: standalone) {
    /* Additional top padding for status bar in standalone */
    .topbar {
        padding-top: max(16px, var(--safe-area-top));
    }

    /* Hide browser-specific elements */
    .browser-only {
        display: none !important;
    }
}

/* iOS standalone detection */
@supports (-webkit-touch-callout: none) {
    .standalone-only {
        display: none;
    }
}

@media all and (display-mode: standalone) {
    @supports (-webkit-touch-callout: none) {
        .standalone-only {
            display: block;
        }
    }
}

/* ============ ORIENTATION SUPPORT ============ */

@media (orientation: landscape) and (max-height: 500px) {
    /* Compact layout for landscape on phones */
    .topbar {
        height: 48px;
        padding-top: 8px;
        padding-bottom: 8px;
    }

    .mobile-tab-bar {
        height: calc(48px + var(--safe-area-bottom));
    }

    .mobile-tab-bar__item {
        font-size: 10px;
        gap: 2px;
        padding: 4px;
    }

    .mobile-tab-bar__icon {
        width: 20px;
        height: 20px;
    }
}

/* ============ ANDROID NAVIGATION BAR ============ */

@media (max-width: 768px) {
    /* Ensure bottom elements don't hide behind Android nav bar */
    .fixed-bottom,
    .mobile-tab-bar {
        padding-bottom: max(8px, var(--safe-area-bottom));
    }
}

/* ============ ACCESSIBILITY ============ */

/* Ensure focus is visible on touch devices */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* Remove focus outline for mouse users */
button:focus:not(:focus-visible),
a:focus:not(:focus-visible) {
    outline: none;
}

/* ============ TABLET ADJUSTMENTS ============ */

@media (min-width: 769px) and (max-width: 1024px) {
    /* Show sidebar on tablet */
    .mobile-tab-bar {
        display: none;
    }

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

/* ============ MOBILE DASHBOARD & CHARTS ============ */

@media (max-width: 768px) {
    /* Dashboard grid - single column */
    .dashboard-grid {
        grid-template-columns: 1fr !important;
        gap: 16px;
    }

    /* Stats grid - 2 columns on mobile */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px;
    }

    /* Stat cards - more compact */
    .stat-card {
        padding: 16px 12px;
    }

    .stat-card__value {
        font-size: 24px !important;
    }

    .stat-card__label {
        font-size: 11px !important;
    }

    /* Chart box - адаптивная высота */
    .chart-box {
        height: 220px !important;
        width: 100% !important;
        overflow: hidden;
    }

    /* Canvas - адаптивный */
    .chart-box canvas {
        max-width: 100% !important;
        height: auto !important;
        width: 100% !important;
    }

    /* Cards */
    .card {
        overflow: hidden;
    }

    .card__body {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Tables - horizontal scroll для больших таблиц */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100vw;
        margin: 0 -16px;
        padding: 0 16px;
    }

    .table {
        min-width: 100%;
        font-size: 13px;
    }

    .table th,
    .table td {
        padding: 10px 8px;
        white-space: nowrap;
    }

    /* Mini list - более компактный */
    .mini-list li {
        padding: 10px 0;
        font-size: 13px;
    }

    /* Report summary - single column */
    .report-summary {
        grid-template-columns: 1fr !important;
        gap: 10px;
    }

    /* Route planner */
    .route-planner {
        grid-template-columns: 1fr !important;
    }

    /* Map canvas */
    .map-canvas {
        height: 300px !important;
    }

    /* Settings tabs - scroll */
    .settings-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        margin: 0 -16px 18px;
        padding: 0 16px;
    }

    .settings-tabs a {
        white-space: nowrap;
    }
}

/* ============ ОЧЕНЬ МАЛЕНЬКИЕ ЭКРАНЫ ============ */

@media (max-width: 360px) {
    /* Stats grid - single column на очень маленьких экранах */
    .stats-grid {
        grid-template-columns: 1fr !important;
    }

    .stat-card__value {
        font-size: 20px !important;
    }

    .chart-box {
        height: 180px !important;
    }
}

/* ============ PRINT MODE ============ */

@media print {
    .mobile-tab-bar,
    .topbar,
    .sidebar {
        display: none !important;
    }

    body {
        padding: 0 !important;
    }
}
