/* ========================================
   Mobile Styles — < 768px
   Map-centric, mobile-first redesign
   ======================================== */

@media (max-width: 767px) {
    :root {
        --mobile-pad: 8px;
    }

    /* ————— Global mobile resets ————— */
    html, body {
        font-size: 13px;
        overflow: hidden;
        -webkit-text-size-adjust: 100%;
        /* Account for notch / dynamic island / status bar */
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
        padding-left: env(safe-area-inset-left, 0);
        padding-right: env(safe-area-inset-right, 0);
        background: #000;
    }

    /* ————— HIDE DESKTOP HEADER & FOOTER ————— */
    .header {
        display: none !important;
    }
    .mobile-nav {
        display: none !important;
    }
    .mobile-content {
        display: none !important;
    }
    .save-indicator {
        display: none !important;
    }
    /* Hide old mobile dropdown */
    .mobile-dropdown-menu {
        display: none !important;
    }
    .mobile-dropdown-backdrop {
        display: none !important;
    }
    /* Hide desktop panels & resize handles */
    .panel-left,
    .panel-right,
    .resize-handle,
    .panel-expand-tab {
        display: none !important;
    }
    #btn-merge { display: none !important; }

    /* ————— MAP FILLS ENTIRE SCREEN ————— */
    .app-layout {
        display: flex;
        position: fixed;
        top: env(safe-area-inset-top, 0px);
        left: env(safe-area-inset-left, 0px);
        right: env(safe-area-inset-right, 0px);
        bottom: env(safe-area-inset-bottom, 0px);
        z-index: 1;
    }
    .panel-center {
        flex: 1;
        width: 100%;
        height: 100%;
    }
    #map-container {
        width: 100%;
        height: 100%;
    }

    /* ————— MOBILE TITLE PILL (upper left) ————— */
    .mobile-title-pill {
        display: flex !important;
        align-items: center;
        gap: 6px;
        position: fixed;
        top: calc(12px + env(safe-area-inset-top, 0));
        left: 12px;
        z-index: 1100;
        background: rgba(28, 28, 30, 0.88);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 24px;
        padding: 6px 14px 6px 10px;
        box-shadow: 0 2px 12px rgba(0,0,0,0.4);
        pointer-events: auto;
    }
    .mobile-title-pill .pill-logo svg {
        width: 22px;
        height: 22px;
        display: block;
    }
    .mobile-title-pill .pill-text {
        font-size: 13px;
        font-weight: 700;
        color: var(--gold-light);
        letter-spacing: -0.3px;
        white-space: nowrap;
    }

    /* ————— FLOATING ACTION BUTTONS ————— */
    .mobile-fab {
        display: flex !important;
        align-items: center;
        justify-content: center;
        position: fixed;
        z-index: 1100;
        width: 52px;
        height: 52px;
        border-radius: 50%;
        border: 1px solid rgba(255, 255, 255, 0.12);
        background: rgba(28, 28, 30, 0.92);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        box-shadow: 0 4px 16px rgba(0,0,0,0.5);
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        transition: transform 0.15s ease, background 0.15s ease;
        font-size: 22px;
        color: var(--gold-light);
        padding: 0;
        overflow: hidden;
    }
    .mobile-fab .fab-icon {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 50%;
        display: block;
    }
    .mobile-fab:active {
        transform: scale(0.92);
        background: rgba(44, 44, 46, 0.95);
    }
    .mobile-fab.open {
        background: rgba(219, 172, 63, 0.2);
        border-color: var(--gold);
    }

    /* Gear button — upper right */
    #mobile-fab-nav {
        top: calc(12px + env(safe-area-inset-top, 0));
        right: 12px;
    }
    /* Plus button — lower right */
    #mobile-fab-add {
        bottom: calc(20px + env(safe-area-inset-bottom, 0));
        right: 12px;
    }

    /* ————— FLYOUT MENUS ————— */
    .mobile-flyout {
        display: none;
        position: fixed;
        z-index: 1150;
        background: rgba(28, 28, 30, 0.96);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        border: 1px solid rgba(255, 255, 255, 0.12);
        border-radius: 16px;
        box-shadow: 0 8px 32px rgba(0,0,0,0.55);
        padding: 8px 0;
        min-width: 220px;
        max-width: 260px;
        animation: mobileFlyIn 0.15s ease;
    }
    .mobile-flyout.open {
        display: block;
    }

    /* Nav menu — anchored below gear button */
    #mobile-flyout-nav {
        top: calc(72px + env(safe-area-inset-top, 0));
        right: 12px;
    }
    /* Add menu — anchored above plus button */
    #mobile-flyout-add {
        bottom: calc(80px + env(safe-area-inset-bottom, 0));
        right: 12px;
    }

    @keyframes mobileFlyIn {
        from { opacity: 0; transform: scale(0.92); }
        to { opacity: 1; transform: scale(1); }
    }

    .mobile-flyout-item {
        display: flex;
        align-items: center;
        gap: 12px;
        width: 100%;
        padding: 13px 18px;
        background: none;
        border: none;
        color: var(--text);
        font-size: 15px;
        font-family: var(--font);
        font-weight: 500;
        cursor: pointer;
        transition: background 0.1s;
        text-align: left;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-flyout-item:active {
        background: rgba(255, 255, 255, 0.08);
    }
    .mobile-flyout-item .flyout-icon {
        font-size: 20px;
        width: 28px;
        text-align: center;
        flex-shrink: 0;
    }
    .mobile-flyout-item .flyout-label {
        flex: 1;
    }
    .mobile-flyout-sep {
        height: 1px;
        background: rgba(255, 255, 255, 0.08);
        margin: 4px 14px;
    }

    /* Backdrop behind flyout menus */
    .mobile-flyout-backdrop {
        position: fixed;
        inset: 0;
        z-index: 1140;
        background: rgba(0, 0, 0, 0.25);
    }

    /* ————— MODALS: 96% centered (not bottom sheet) ————— */
    .modal-overlay {
        align-items: center;
        justify-content: center;
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
        padding-left: env(safe-area-inset-left, 0);
        padding-right: env(safe-area-inset-right, 0);
    }
    .modal {
        min-width: unset;
        width: 96vw;
        max-width: 96vw;
        max-height: calc(92vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
        border-radius: var(--radius-lg);
        animation: fadeIn 0.15s ease;
    }
    .modal-header {
        padding: 12px 14px;
        font-size: 15px;
        gap: 8px;
    }
    /* Big prominent close button */
    .modal-header .btn-icon,
    .modal-header .close-modal {
        min-width: 36px;
        min-height: 36px;
        font-size: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.08);
        color: var(--text);
        border: 1px solid rgba(255, 255, 255, 0.12);
        transition: background 0.15s;
    }
    .modal-header .btn-icon:active,
    .modal-header .close-modal:active {
        background: rgba(255, 69, 58, 0.2);
    }
    .modal-body {
        padding: 12px 14px;
        font-size: 13px;
    }
    .modal-footer {
        padding: 10px 14px;
        gap: 8px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
    }
    .modal-footer .btn {
        flex: 1;
        justify-content: center;
        min-height: 42px;
        font-size: 14px;
    }

    /* Bottom sheet override — also 96% centered modal style */
    .bottom-sheet {
        position: relative;
        left: auto;
        right: auto;
        bottom: auto;
        width: 96vw;
        max-width: 96vw;
        max-height: 92vh;
        border-radius: var(--radius-lg);
        animation: fadeIn 0.15s ease;
        border: 1px solid var(--border);
    }
    .bottom-sheet-handle {
        display: none;
    }
    .bottom-sheet-header {
        padding: 12px 14px;
        font-size: 15px;
    }
    .bottom-sheet-header .btn-icon,
    .bottom-sheet-header .close-modal {
        min-width: 36px;
        min-height: 36px;
        font-size: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.08);
        color: var(--text);
        border: 1px solid rgba(255, 255, 255, 0.12);
    }
    .bottom-sheet-body {
        padding: 12px 14px;
    }

    /* ————— BUTTONS: touch-friendly sizing ————— */
    .btn {
        min-height: 40px;
        padding: 8px 14px;
        font-size: 13px;
        gap: 6px;
        border-radius: var(--radius-sm);
    }
    .btn-sm {
        min-height: 34px;
        padding: 6px 10px;
        font-size: 12px;
    }

    /* ————— FORMS: touch-friendly inputs ————— */
    input[type="text"],
    input[type="number"],
    input[type="url"],
    input[type="search"],
    select,
    textarea {
        padding: 8px 10px;
        font-size: 14px;
        min-height: 40px;
        border-radius: var(--radius-sm);
    }
    .form-group {
        margin-bottom: 10px;
    }
    .form-group label {
        font-size: 12px;
        margin-bottom: 3px;
    }
    .input-with-btn {
        gap: 4px;
    }

    /* Checkbox / radio — bigger touch targets */
    .checkbox-row {
        min-height: 40px;
        padding: 6px 0;
        font-size: 13px;
        gap: 8px;
    }
    .checkbox-row input[type="checkbox"],
    .checkbox-row input[type="radio"] {
        width: 20px;
        height: 20px;
    }

    /* Toggle */
    .toggle {
        font-size: 13px;
        gap: 8px;
    }
    .toggle-track {
        width: 36px;
        height: 20px;
    }
    .toggle-track::after {
        width: 16px;
        height: 16px;
    }
    .toggle input:checked + .toggle-track::after {
        transform: translateX(16px);
    }

    /* ————— DATA TABLE: scrollable compact ————— */
    .data-table-wrap {
        max-height: 200px;
        -webkit-overflow-scrolling: touch;
        border-radius: var(--radius-sm);
    }
    .data-table {
        font-size: 11px;
    }
    .data-table th {
        padding: 6px 8px;
        font-size: 10px;
        white-space: nowrap;
    }
    .data-table td {
        padding: 4px 6px;
        max-width: 120px;
    }

    /* ————— TABS: scrollable, compact ————— */
    .tabs {
        overflow-x: auto;
        scrollbar-width: none;
        flex-wrap: nowrap;
    }
    .tabs::-webkit-scrollbar { display: none; }
    .tab {
        padding: 10px 12px;
        font-size: 12px;
        flex-shrink: 0;
        white-space: nowrap;
    }

    /* ————— PHOTO GRID ————— */
    .photo-grid {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
        gap: 6px;
    }
    .photo-card .photo-info {
        padding: 3px 6px;
        font-size: 10px;
    }

    /* ————— WIZARD STEPS ————— */
    .wizard-steps {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
        gap: 2px;
        margin-bottom: 10px;
    }
    .wizard-steps::-webkit-scrollbar { display: none; }
    .wizard-step span.step-text { display: none; }
    .wizard-step-num {
        width: 22px;
        height: 22px;
        font-size: 10px;
    }
    .wizard-step-line {
        width: 12px;
    }

    /* ————— MAP INTERACTION BANNER: bottom-left above nav ————— */
    .map-interaction-banner {
        top: unset !important;
        bottom: calc(25px + env(safe-area-inset-bottom, 0px)) !important;
        left: 8px !important;
        right: auto !important;
        transform: none !important;
        max-width: 75vw;
        white-space: normal;
        font-size: 11px;
        padding: 6px 10px;
        gap: 6px;
        z-index: 999 !important;
    }
    .map-interaction-banner .interaction-cancel {
        min-height: 24px;
        min-width: 24px;
        border-radius: 12px;
        padding: 0 8px;
        font-size: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }
    .map-interaction-banner .interaction-cancel::before {
        content: '\2715';
        font-size: 13px;
        line-height: 1;
    }
    .map-interaction-banner span:last-child {
        display: none; /* hide '(Esc to cancel)' on mobile */
    }

    /* ————— TOAST: anchored bottom-left above nav ————— */
    .toast-container {
        top: unset;
        bottom: calc(58px + env(safe-area-inset-bottom, 0px));
        left: 8px;
        right: auto;
        max-width: 72vw;
        flex-direction: column-reverse;
    }
    .toast {
        max-width: unset;
        padding: 6px 10px;
        font-size: 11px;
        gap: 6px;
    }
    .toast-details { font-size: 10px; }
    .toast-details-body { font-size: 9px; padding: 4px; }

    /* ————— LOGS PANEL ————— */
    .logs-panel {
        height: 200px;
        bottom: 0;
        font-size: 10px;
    }
    .logs-header {
        padding: 4px 8px;
        flex-wrap: wrap;
    }
    .logs-header h3 { font-size: 11px; }
    .logs-toolbar {
        gap: 4px;
        padding: 2px 6px;
        flex-wrap: wrap;
    }
    .logs-toolbar input {
        width: 100px;
        font-size: 10px;
        padding: 2px 6px;
    }
    .logs-toolbar select {
        font-size: 10px;
        padding: 2px 4px;
    }
    .logs-toolbar .btn {
        min-height: 28px;
        padding: 4px 6px;
        font-size: 11px;
    }

    /* ————— EMPTY STATE ————— */
    .empty-state {
        padding: 24px 16px;
        gap: 10px;
    }
    .empty-state svg {
        width: 40px;
        height: 40px;
    }
    .empty-state p {
        font-size: 13px;
        max-width: 240px;
    }

    /* ————— BADGES ————— */
    .badge {
        padding: 2px 8px;
        font-size: 10px;
    }

    /* ————— PANEL SECTIONS (reused in modal content) ————— */
    .panel-section {
        margin-bottom: 8px;
        border-radius: var(--radius-sm);
    }
    .panel-section-header {
        padding: 8px 10px;
        font-size: 12px;
    }
    .panel-section-body {
        padding: 8px;
    }

    /* ————— DROP ZONE ————— */
    .drop-zone {
        padding: 24px 14px;
        font-size: 13px;
    }

    /* ————— ERROR / INFO / WARNING / SUCCESS BOXES ————— */
    .error-box,
    .success-box,
    .info-box,
    .warning-box {
        padding: 10px 12px;
        font-size: 13px;
        border-radius: var(--radius-sm);
    }

    /* ————— PROGRESS BAR ————— */
    .progress-bar-container {
        height: 18px;
        margin: 8px 0;
    }
    .progress-bar-text {
        font-size: 11px;
    }

    /* ————— LEAFLET POPUP ————— */
    #map-container .leaflet-popup-content {
        max-width: 260px;
        font-size: 12px;
        margin: 10px 12px;
    }
    #map-container .leaflet-popup-content td,
    #map-container .leaflet-popup-content th {
        padding: 2px 6px;
        font-size: 11px;
    }

    /* Map overlay */
    .map-overlay,
    .map-overlay-msg {
        padding: 16px 24px;
        font-size: 14px;
    }
    .map-overlay svg {
        width: 40px;
        height: 40px;
    }

    /* ————— SCROLLBAR ————— */
    ::-webkit-scrollbar { width: 3px; height: 3px; }

    /* ————— UTILITY overrides ————— */
    .p-8 { padding: 6px; }
    .p-12 { padding: 10px; }
    .gap-8 { gap: 6px; }
    .gap-12 { gap: 8px; }
    .mb-8 { margin-bottom: 6px; }
    .mt-8 { margin-top: 6px; }

    /* ————— MAP CONTROLS: hide zoom buttons on mobile ————— */
    .leaflet-control-zoom {
        display: none !important;
    }
    .leaflet-control-attribution {
        font-size: 8px !important;
        padding: 1px 4px !important;
    }

    /* ————— COORD SEARCH on mobile — move to bottom-left ————— */
    .coord-search-control {
        position: fixed !important;
        bottom: calc(30px + env(safe-area-inset-bottom, 0px)) !important;
        left: 10px !important;
        top: auto !important;
        z-index: 1000;
        flex-direction: column-reverse !important;
    }
    .coord-search-panel {
        min-width: 200px;
        margin-top: 0;
        margin-bottom: 4px;
    }
    .coord-search-input {
        font-size: 14px;
    }

    /* ————— SPLASH / TOOL GUIDE: 99% fullscreen ————— */
    .modal-overlay.splash-overlay {
        z-index: 10000 !important;
    }
    .splash-modal {
        width: 96vw !important;
        max-width: 96vw !important;
        height: calc(94vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) !important;
        max-height: calc(94vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) !important;
        border-radius: var(--radius) !important;
    }
    .splash-modal .modal-body {
        flex: 1;
        overflow-y: auto;
        max-height: none !important;
    }

    /* ————— DRAW TOOLBAR: mobile optimized ————— */
    .draw-toolbar {
        top: auto;
        bottom: calc(50px + env(safe-area-inset-bottom, 0));
        left: 12px;
        right: 72px;
        transform: none;
        min-width: 0;
    }
    .draw-tool-btn {
        padding: 12px 10px;
        font-size: 13px;
        min-height: 44px;
    }

    /* ————— GIS WIDGETS: mobile — bottom sheet with swipe-to-collapse ————— */
    .gis-widget {
        min-width: unset;
        max-width: unset;
        width: 100vw;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: 0 !important;
        max-height: 55vh;
        border-radius: 16px 16px 0 0;
        transition: transform 0.25s ease;
        box-shadow: 0 -4px 24px rgba(0,0,0,0.5);
    }
    /* Collapsed state — slid down off screen, only header peek visible */
    .gis-widget.widget-collapsed {
        transform: translateY(calc(100% - 52px));
    }
    /* Drag handle bar at top of widget */
    .widget-drag-handle {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 8px 0 4px;
        cursor: grab;
        -webkit-tap-highlight-color: transparent;
        touch-action: none;
    }
    .widget-drag-handle .handle-bar {
        width: 40px;
        height: 4px;
        border-radius: 2px;
        background: rgba(255, 255, 255, 0.3);
    }
    .gis-widget.widget-dragging {
        transition: none !important;
    }
    .gis-widget-header {
        padding: 8px 14px 12px;
        position: relative;
    }
    /* Hide old collapse button — replaced by drag handle */
    .widget-collapse-btn {
        display: none !important;
    }
    .gis-widget-header .widget-close {
        font-size: 22px;
        min-width: 36px;
        min-height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.08);
        border: 1px solid rgba(255, 255, 255, 0.12);
    }
    .gis-widget-body {
        padding: 14px;
        overflow-y: auto;
        max-height: calc(55vh - 100px);
    }

    /* ————— SPLASH MOBILE NOTICE: hide old banner ————— */
    .splash-mobile-notice {
        display: none !important;
    }

    /* ————— LAYER LIST items in modals ————— */
    .layer-item {
        padding: 10px 12px;
        min-height: 44px;
        gap: 6px 8px;
    }
    .layer-actions {
        opacity: 1 !important;
        pointer-events: auto !important;
    }
    .layer-actions .btn-icon {
        font-size: 16px;
        padding: 6px;
        min-width: 32px;
        min-height: 32px;
    }
    .layer-order-btns button {
        font-size: 12px;
        padding: 4px 8px;
        min-width: 28px;
        min-height: 28px;
    }

    /* ————— FIELD ITEMS in modals ————— */
    .field-item {
        padding: 6px 8px;
        min-height: 38px;
        gap: 6px;
        font-size: 12px;
    }
    .field-item .field-name {
        font-size: 12px;
    }
    .field-item .field-type {
        font-size: 10px;
        padding: 2px 6px;
    }
    .field-item input[type="checkbox"] {
        width: 18px;
        height: 18px;
    }
}

/* ————— Extra-small screens (< 380px) ————— */
@media (max-width: 379px) {
    .mobile-title-pill .pill-text {
        font-size: 11px;
    }
    .mobile-title-pill .pill-logo svg {
        width: 18px;
        height: 18px;
    }
    .mobile-fab {
        width: 46px;
        height: 46px;
        font-size: 20px;
    }
    .mobile-flyout-item {
        padding: 11px 14px;
        font-size: 14px;
    }
    .modal {
        width: 98vw;
        max-width: 98vw;
        max-height: calc(94vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    }
}

/* ————— Tablet tweaks (768–1024) ————— */
@media (min-width: 768px) and (max-width: 1024px) {
    .panel-left { width: 260px; min-width: 200px; }
    .panel-right { width: 240px; min-width: 180px; }

    /* Tighter style panel for narrower right panel */
    .style-row label { flex: 0 0 70px; font-size: 10px; }
    .style-row { gap: 4px; }
    .style-value { flex: 0 0 28px; font-size: 10px; }
    .header-center .btn span.btn-icon-text + span { display: none; }
    .btn { font-size: 12px; padding: 5px 10px; }
    .btn-sm { font-size: 11px; padding: 3px 8px; }
}

/* ————— Desktop: hide mobile-only elements ————— */
@media (min-width: 768px) {
    .mobile-nav { display: none !important; }
    .mobile-content { display: none !important; }
    .mobile-title-pill { display: none !important; }
    .mobile-fab { display: none !important; }
    .mobile-flyout { display: none !important; }
    .mobile-flyout-backdrop { display: none !important; }
    .widget-collapse-btn { display: none !important; }
    .widget-drag-handle { display: none !important; }
}
