/**
 * CZIS Responsive Styles
 * Makes /czis and child pages mobile/device responsive
 * Load this after style.css
 */

/* Sidebar toggle icons beside burger - always visible */
.czis-sidebar-toggles {
    flex-shrink: 0;
}
.czis-sidebar-toggles .nav-link {
    cursor: pointer;
}
.czis-sidebar-toggles .czis-toggle-left:hover,
.czis-toggle-right-wrap .czis-toggle-right:hover {
    opacity: 0.8;
}
.czis-toggle-right-wrap {
    flex-shrink: 0;
}

/* Visible layer legends panel - bottom right */
#visible-legends-panel {
    z-index: 2;
    position: absolute;
    right: 15px;
    bottom: calc(var(--statusbar-height) + 55px);
    left: auto;
    border-top-right-radius: 1rem;
    border-bottom-right-radius: 1rem;
    width: fit-content;
    max-width: min(400px, 90vw);
    transition: right 0.4s ease;
}

/* Keep legend to the left of the open right sidebar */
#app.czis-right-open #visible-legends-panel {
    right: calc(var(--right-sidebar-width) + 15px);
}
#visible-legends-panel .visible-legends-toggler {
    border-top-right-radius: 1rem;
    border-bottom-right-radius: 1rem;
}
#visible-legends-panel .card-body {
    width: auto;
}
#visible-legends-panel .visible-legends-list {
    max-height: 35vh;
    overflow-y: auto;
    width: fit-content;
    min-width: 0;
}
#visible-legends-panel .legend-item {
    display: flex;
    align-items: flex-start;
    margin-bottom: 0.5rem;
    font-size: 0.8rem;
    width: fit-content;
}
#visible-legends-panel .legend-item img {
    flex-shrink: 0;
    margin-right: 0.5rem;
}
#visible-legends-panel .legend-item .legend-name {
    word-break: break-word;
    white-space: nowrap;
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ========== Left menu (layer panel) UI improvements ========== */
#layer-con.layer-panel {
    background: linear-gradient(180deg, #f8f9fa 0%, #fff 100%);
    border-right: 1px solid #dee2e6;
    box-shadow: 2px 0 8px rgba(0,0,0,0.06);
    overflow-x: auto;
    overflow-y: auto;
}

#layer-con .layer-panel-card {
    border: none;
    border-radius: 0;
    box-shadow: none;
}

#layer-con .layer-panel-header {
    padding: 0.5rem 0.6rem;
    font-weight: 600;
    font-size: 0.9rem;
    letter-spacing: 0.02em;
    border-bottom: 1px solid rgba(0,0,0,0.06);
}

#layer-con .layer-panel-header-location {
    background: linear-gradient(135deg, #5a6268 0%, #495057 100%);
    color: #fff;
}

#layer-con .layer-panel-header-layers {
    background: linear-gradient(135deg, #28a745 0%, #1e7e34 100%);
    color: #fff;
}

#layer-con .layer-panel-header-btn {
    color: inherit !important;
    text-decoration: none !important;
    padding: 0.25rem 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    transition: opacity 0.2s;
}

#layer-con .layer-panel-header-btn:hover {
    opacity: 0.9;
    color: inherit !important;
}

#layer-con .layer-panel-chevron {
    margin-left: auto;
    font-size: 0.75rem;
    font-family: "Font Awesome 5 Free";
    font-weight: 900;
}

#layer-con .layer-panel-chevron::before {
    content: "\f078";
}

#layer-con .layer-panel-header-btn[aria-expanded="false"] .layer-panel-chevron::before {
    content: "\f054";
}

#layer-con #collapse-dist .card-body {
    padding: 0.6rem 0.75rem;
}

#layer-con #collapse-dist td {
    padding: 0.25rem 0;
    font-size: 0.8rem;
    color: #495057;
}

#layer-con #collapse-dist td:first-child {
    width: 1%;
    white-space: nowrap;
    padding-right: 0.5rem;
    font-weight: 500;
}

#layer-con #collapse-dist .form-control {
    font-size: 0.8rem;
    padding: 0.3rem 0.5rem;
    border-radius: 4px;
    border-color: #ced4da;
}

#layer-con #collapse-dist .form-control:focus {
    border-color: #80bdff;
    box-shadow: 0 0 0 0.2rem rgba(0,123,255,0.15);
}

/* Layer tree improvements */
#layer-con .layertree {
    padding: 0.4rem 0;
}

#layer-con .layercontainer .layer {
    padding: 0.45rem 0.6rem;
    margin: 0 0.25rem 0.25rem;
    border-radius: 6px;
    min-height: 2.2em;
    line-height: 1.4;
    transition: background-color 0.25s ease,
                box-shadow 0.25s ease,
                transform 0.2s ease,
                border-color 0.2s ease;
}

#layer-con .layercontainer .layer:hover {
    background-color: rgba(40,167,69,0.18);
    box-shadow: 0 1px 4px rgba(40,167,69,0.2);
    transform: translateX(2px);
}

#layer-con .layercontainer .layer.active {
    background-color: rgba(255,193,7,0.18);
    border: 1px solid rgba(255,193,7,0.6);
    border-radius: 6px;
    box-shadow: 0 1px 4px rgba(255,193,7,0.25);
}

#layer-con .group-collapse-check {
    width: 22px;
    height: 22px;
    min-width: 22px;
    font-size: 0.75rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #e9ecef;
    color: #495057;
    border-radius: 4px;
    margin-right: 0.35rem;
    transition: background 0.15s;
    text-decoration: none !important;
}

#layer-con .group-collapse-check:hover {
    background: #dee2e6;
    color: #212529;
}

#layer-con .group-collapse-check[aria-expanded="true"] {
    background: #28a745;
    color: #fff;
}

/* Chevron icon for expand/collapse (hide + when FA available) */
#layer-con .group-collapse-check {
    font-size: 0;
    line-height: 0;
}

#layer-con .group-collapse-check::before {
    content: "\f054";
    font-family: "Font Awesome 5 Free";
    font-weight: 900;
    font-size: 0.65rem;
    display: inline-block;
    transition: transform 0.2s;
}

#layer-con .group-collapse-check[aria-expanded="true"]::before {
    content: "\f078";
}

#layer-con .layertree input[type="checkbox"] {
    margin-right: 0.4rem;
    width: 14px;
    height: 14px;
    cursor: pointer;
    accent-color: #28a745;
}

#layer-con .layer span:first-of-type {
    font-size: 0.82rem;
    color: #212529;
}

#layer-con .layer-group {
    margin-left: 1.25rem;
    padding-left: 0.25rem;
    border-left: 1px solid #e9ecef;
}

#layer-con .layercontainer .legend {
    padding: 0.25rem 0 0.25rem 1rem;
    /* Hide legend when layer is not selected – only active layer shows legend */
    display: none;
}

#layer-con .layercontainer .legend img {
    max-height: none;
    height: auto;
    width: auto;
    object-fit: contain;
}

/* Only the selected layer shows the legend; height auto so it fits content */
#layer-con .layercontainer .layer.active .legend {
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    width: 100%;
    min-width: 0;
    padding: 0.5rem 0;
    min-height: 0;
    height: auto;
}

#layer-con .layercontainer .layer.active .legend img {
    max-width: 100%;
    width: auto;
    height: auto;
    max-height: none;
    object-fit: contain;
}

#layer-con .layercontainer .opacity-handler {
    padding: 0.25rem 0 0.25rem 1rem;
}

#layer-con .layercontainer .opacity-handler input[type="range"] {
    height: 4px;
    cursor: pointer;
}

/* Admin Boundary: Mauza / Union disabled until a single Upazila is selected */
#layer-con .layercontainer .layer.layer-upazila-disabled {
    opacity: 0.55;
    cursor: not-allowed;
}
#layer-con .layercontainer .layer.layer-upazila-disabled input.visible {
    cursor: not-allowed;
    pointer-events: none;
}
#layer-con .layercontainer .layer.layer-upazila-disabled span {
    color: #6c757d;
    cursor: not-allowed;
}

/* Map north arrow and scale bar */
#map .ol-rotate {
    top: calc(0.65em + 1.5em);
    right: 0.65em;
    z-index: 4;
}
#map .ol-rotate.ol-hidden {
    opacity: 1;
    visibility: visible;
}
#map .ol-overlaycontainer-stopevent .ol-scale-line,
#map .ol-overlaycontainer .ol-scale-line,
#map .ol-scale-line {
    bottom: 10px;
    left: 10px;
    z-index: 4;
    display: block !important;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
    padding: 2px 4px;
}
#map .ol-scale-line-inner {
    color: #222;
    border: 1px solid #222;
    border-top: none;
    font-size: 11px;
    font-weight: 600;
}

/* ========== Right sidebar professional styling ========== */
#rightSideBar.right-sidebar-panel {
    background: linear-gradient(180deg, #f8f9fa 0%, #fff 100%);
    border-left: 1px solid #dee2e6;
    box-shadow: -2px 0 8px rgba(0,0,0,0.06);
}

/* Tab pills - icon bar */
#rightSideBar .right-sidebar-tabs {
    background: #2c3e50;
    border-radius: 0;
    padding: 0.4rem 0.25rem;
}

#rightSideBar .right-sidebar-tabs .nav-item {
    margin: 0 0.1rem;
}

#rightSideBar .right-sidebar-tabs .nav-link {
    padding: 0.5rem 0.65rem;
    border-radius: 6px;
    color: rgba(255,255,255,0.75);
    transition: all 0.2s ease;
}

#rightSideBar .right-sidebar-tabs .nav-link:hover {
    color: #fff;
    background: rgba(255,255,255,0.15);
}

#rightSideBar .right-sidebar-tabs .nav-link.active {
    background: #3498db;
    color: #fff;
    border: none;
}

/* Card headers in right sidebar */
#rightSideBar .right-sidebar-card-header {
    font-weight: 600;
    font-size: 0.9rem;
    padding: 0.55rem 0.75rem;
    border: none;
    transition: opacity 0.2s;
}

#rightSideBar .right-sidebar-card-header:hover {
    opacity: 0.92;
}

#rightSideBar .card-header.bg-danger {
    background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%) !important;
}

#rightSideBar .card-header.bg-success {
    background: linear-gradient(135deg, #27ae60 0%, #1e8449 100%) !important;
}

#rightSideBar .card-header.bg-primary {
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%) !important;
}

#rightSideBar .card-header.bg-info {
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%) !important;
}

/* Action buttons (Framework, Process, Codes, etc.) */
#rightSideBar .right-sidebar-btn {
    padding: 0.5rem 0.75rem;
    font-weight: 500;
    font-size: 0.88rem;
    border-radius: 6px;
    margin-bottom: 0.4rem;
    transition: all 0.2s ease;
}

#rightSideBar .right-sidebar-btn:hover {
    transform: translateX(2px);
}

#rightSideBar .right-sidebar-btn.btn-success {
    background: linear-gradient(135deg, #27ae60 0%, #1e8449 100%);
    border-color: #1e8449;
}

#rightSideBar .right-sidebar-btn.btn-info {
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
    border-color: #2980b9;
}

#rightSideBar .right-sidebar-btn.btn-warning {
    background: linear-gradient(135deg, #f39c12 0%, #d68910 100%);
    border-color: #d68910;
    color: #fff;
}

#rightSideBar .right-sidebar-btn.btn-danger {
    background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
    border-color: #c0392b;
}

/* Info item buttons */
#rightSideBar .info-item-button {
    padding: 0.45rem 0.65rem;
    font-size: 0.85rem;
    font-weight: 500;
    border-radius: 6px;
    margin-bottom: 0.35rem;
    transition: all 0.2s ease;
}

#rightSideBar .info-item-button:hover {
    transform: translateX(2px);
}

#rightSideBar .info-item-button.default-info-item {
    font-weight: 600;
}

/* Right sidebar cards */
#rightSideBar .card {
    border-radius: 6px;
    border-color: #e9ecef;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

#rightSideBar .card-body {
    border-radius: 0 0 6px 6px;
}

/* Right sidebar scrollbar */
#rightSideBar::-webkit-scrollbar {
    width: 6px;
}
#rightSideBar::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}
#rightSideBar::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
}
#rightSideBar::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Left panel scrollbar */
#layer-con::-webkit-scrollbar {
    width: 6px;
}
#layer-con::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}
#layer-con::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
}
#layer-con::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Popups from right menu - above right sidebar (z-index 1050) */
.ui-dialog,
.ui-dialog.ui-front,
body .ui-dialog,
.ui-widget-overlay,
.modal,
.modal-backdrop,
#modalContainer.ui-dialog-content {
    z-index: 1100 !important;
}
.ui-widget-overlay {
    z-index: 1099 !important;
}

/* Iframe/URL popups: ensure visible with proper dimensions */
.ui-dialog .ui-dialog-content {
    display: block;
}
#modalContainer.ui-dialog-content {
    min-height: 400px;
}
#modalContainer.ui-dialog-content #thedialog {
    min-height: 400px;
}

/* Narrow/small device: popups full width (full screen) */
@media (max-width: 767px) {
    .ui-dialog,
    body .ui-dialog {
        width: 100% !important;
        max-width: 100% !important;
        left: 0 !important;
        right: 0 !important;
        margin: 0 !important;
        top: 0 !important;
        height: 100vh !important;
        max-height: 100vh !important;
        position: fixed !important;
    }
    .ui-dialog .ui-dialog-content {
        height: calc(100vh - 80px) !important;
        max-height: calc(100vh - 80px) !important;
        overflow: auto;
        display: block !important;
    }
    /* Iframe popups (modalContainer): ensure visible and sized */
    #modalContainer.ui-dialog-content {
        height: calc(100vh - 80px) !important;
        min-height: 400px;
        overflow: hidden;
    }
    #modalContainer.ui-dialog-content #thedialog {
        height: 100% !important;
        min-height: 400px;
        display: block !important;
        visibility: visible !important;
    }
    .modal-dialog {
        max-width: 100% !important;
        margin: 0 !important;
        min-height: calc(100vh - 1rem);
    }
    .modal-content {
        width: 100%;
    }
}

/* Define CSS variables (required by style.css - may be undefined in czis layout) */
:root {
    --banner-height: 8vh;
    --topnav-height: 45px;
    --toolbar-height: 2.5em;
    --statusbar-height: 1.5em;
    --content-height: calc(100vh - var(--banner-height) - var(--topnav-height));
    --map-height: calc(var(--content-height) - var(--statusbar-height));
    --left-sidebar-width: 340px;
    --right-sidebar-width: 400px;
}

/* Base responsive adjustments */
html {
    -webkit-text-size-adjust: 100%;
}

/* Prevent horizontal scroll on small screens */
@media (max-width: 1024px) {
    body, #app, #content {
        overflow-x: hidden;
    }
}

/* Mobile-first: smaller screens */
@media (max-width: 767px) {
    :root {
        --banner-height: 6vh;
        --topnav-height: 44px;
        --left-sidebar-width: 0px;
    }

    /* Navbar - collapse and stack */
    #top-nav .navbar-nav {
        flex-direction: row;
        flex-wrap: wrap;
    }
    #top-nav .navbar-nav .nav-link {
        padding: 0.25rem 0.5rem;
        font-size: 0.9rem;
    }
    #top-nav .nav-link[style*="font-size:1.3rem"] {
        font-size: 0.85rem !important;
        max-width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    #upz-sol-count {
        font-size: 0.8rem !important;
        margin-left: 5px !important;
    }
    #navbarSupportedContent .container {
        max-width: 140px;
    }
    #navbarSupportedContent .col {
        padding-left: 2px;
        padding-right: 2px;
    }

    /* Left sidebar - overlay on mobile, hidden by default (off-screen left) */
    #layer-con {
        position: fixed !important;
        left: -300px;
        top: calc(var(--banner-height) + var(--topnav-height));
        height: calc(100vh - var(--banner-height) - var(--topnav-height)) !important;
        width: 85vw !important;
        max-width: 280px;
        z-index: 1050;
        box-shadow: 4px 0 15px rgba(0,0,0,0.2);
    }

    /* Right sidebar - overlay on mobile, hidden by default (off-screen right) */
    #rightSideBar {
        position: fixed !important;
        right: -350px;
        top: calc(var(--banner-height) + var(--topnav-height));
        height: calc(100vh - var(--banner-height) - var(--topnav-height)) !important;
        width: 90vw !important;
        max-width: 320px;
        z-index: 1050;
        overflow-y: auto;
        box-shadow: -4px 0 15px rgba(0,0,0,0.2);
    }

    /* Map container - full viewport fill when sidebars overlay */
    #content {
        width: 100% !important;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    #mapWrapper {
        margin-left: 0 !important;
        margin-right: 0 !important;
        width: 100% !important;
        flex: 1 1 auto !important;
        min-width: 0;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }
    #map-containter {
        width: 100% !important;
        flex: 1 1 auto !important;
        min-height: 0;
        display: flex;
        flex-direction: column;
        position: relative;
    }
    #map-containter #map-wrapper {
        flex: 1 1 auto !important;
        min-height: 0;
        width: 100%;
        position: relative;
    }
    #map-containter #map {
        width: 100% !important;
        height: 100% !important;
        min-height: 200px;
        position: absolute;
        top: var(--toolbar-height);
        left: 0;
        right: 0;
        bottom: 0;
    }
    #toolbarWrap {
        left: 0 !important;
        right: 0 !important;
        padding-right: 0.5rem;
        flex-shrink: 0;
    }

    /* Crop name badge */
    #cropSuiteName {
        right: 10px;
        left: 10px;
        max-width: calc(100% - 20px);
        font-size: 0.9rem;
    }

    /* Status bar */
    #status-bar {
        font-size: 0.75rem;
        flex-shrink: 0;
    }
    #status-bar .mystatus {
        padding-right: 8px !important;
    }

    /* Visible layer legends panel - keep right on mobile */
    #visible-legends-panel {
        left: auto !important;
        right: 8px !important;
        width: auto !important;
        max-width: min(400px, 90vw);
    }

    #app.czis-right-open #visible-legends-panel {
        right: calc(min(90vw, 320px) + 8px) !important;
    }

    /* Upazila status panel */
    #upazila-status {
        left: 0 !important;
        width: 95% !important;
        max-width: 100%;
    }
    #upazila-status .col-3 {
        flex: 0 0 100%;
        max-width: 100%;
    }
    #upazila-status .col-9 {
        flex: 0 0 100%;
        max-width: 100%;
    }
    #upazila-status .nav-pills {
        flex-direction: row !important;
        flex-wrap: wrap;
        border-bottom: 1px solid #dee2e6;
    }
    #upazila-status .nav-pills .nav-link {
        padding: 0.4rem 0.6rem;
        font-size: 0.85rem;
    }

    /* Accordion/card bodies - prevent overflow */
    .card-body {
        padding: 0.5rem !important;
    }
    .accordion .card-header {
        font-size: 0.9rem;
    }
    .nav-tabs .nav-link {
        padding: 0.35rem 0.5rem;
    }
    .tab-content {
        font-size: 0.9rem;
    }
    .form-control {
        font-size: 16px; /* Prevents iOS zoom on focus */
    }
}

/* Tablet */
@media (min-width: 768px) and (max-width: 1024px) {
    :root {
        --left-sidebar-width: 280px;
        --right-sidebar-width: 300px;
    }

    #rightSideBar {
        width: 300px !important;
    }
    #layer-con {
        width: 280px !important;
    }
}

/* Small mobile */
@media (max-width: 480px) {
    :root {
        --banner-height: 5vh;
    }
    #myBanner img {
        max-height: 50px;
    }
}
