/* Overview: below ~1100px the rigid 1fr/250px/450px grid starves the map
 * column - stack the three sections instead and let the page scroll. */
@media (max-width: 1100px) {
    .main-content {
        grid-template-columns: 1fr;
    }
    .distribution-section,
    .filters-section {
        max-height: 420px;
    }
    #map {
        height: 480px;
        min-height: 360px;
    }
}

/* Responsive Block Detail View */
@media (max-width: 1200px) {
    .block-detail-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    /* Below 1200px the 2fr/3fr split squeezes the right column; fall back
     * to an even split so cards still get readable width. */
    .block-detail-grid.detail-5050 {
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 768px) {
    .block-detail-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .block-detail-grid,
    .block-detail-grid.detail-5050 {
        grid-template-columns: 1fr;
    }

    .feasibility-badges {
        flex-wrap: wrap;
    }

    .block-detail-view {
        padding: 1rem;
    }

    /* Mobile fix (2026-06-09): the outer detail grid stacks above, but the
     * inner card grids stayed 2-column, so on a phone two cards (e.g. MMUA +
     * Biophysical) crammed side-by-side and overflowed the screen with their
     * text truncated. Stack every inner card grid to a single full-width
     * column. */
    .detail-5050 .detail-right,
    .cluster-cards {
        grid-template-columns: 1fr;
    }

    /* The map is sticky on desktop so it tracks the scrolling card column; on a
     * phone the columns are stacked, so an un-stuck map reads naturally and
     * never hovers over the cards. */
    .detail-5050 .detail-left {
        position: static;
        top: auto;
    }

    /* The desktop map height is calc(100vh - 250px) to fill a tall column; on a
     * phone that leaves the map dominating the screen. Cap it. */
    .detail-5050 .detail-card-map .detail-card-body {
        height: 320px;
        min-height: 260px;
    }

    /* Grid items default to min-width:auto, so a card whose metric list is
     * wider than the screen (min-width:max-content rows) would stretch the
     * track and push the page wider than the viewport. min-width:0 lets the
     * card stay viewport-width while its metric list scrolls inside its own
     * body. */
    .detail-5050 .detail-left,
    .detail-5050 .detail-right,
    .detail-card {
        min-width: 0;
    }

    /* The block ribbon is a single non-wrapping flex row (Overview link +
     * recommendation banner + feasibility badge + Cluster Planning button) with
     * a 2rem gap, so it spills past a phone screen. Let it wrap and give the
     * recommendation banner its own full-width line. */
    .header-top-row {
        flex-wrap: wrap;
        gap: 0.75rem;
    }
    .header-top-row .recommendation-line {
        flex-basis: 100%;
    }

    /* Belt-and-braces: nothing in the detail view should trigger a horizontal
     * page scroll on mobile. */
    .block-detail-view,
    .block-detail-grid {
        overflow-x: hidden;
    }
}

/* ============================================
   Mobile Responsive Styles
   ============================================ */

@media (max-width: 480px) {
    .header {
        padding: 0 1rem;
    }

    .title-section h1 {
        font-size: 1.2rem;
    }

    .title-section p {
        font-size: 0.75rem;
    }

    .logo {
        height: 35px;
    }

    .filter-bar {
        padding: 1rem;
        gap: 0.75rem;
    }

    .filter-group select {
        min-width: 100%;
        font-size: 0.85rem;
    }

    .btn-primary,
    .btn-export {
        width: 100%;
        padding: 0.6rem 1rem;
    }

    .main-content {
        padding: 1rem;
        gap: 1rem;
    }

    #map {
        height: 300px;
        min-height: 250px;
    }

    .chart-container {
        height: 120px;
    }

    .modal-content {
        width: 100%;
        max-height: 95vh;
        border-radius: 10px 10px 0 0;
        margin-top: auto;
    }

    .config-table {
        font-size: 0.75rem;
    }

    .config-table td {
        padding: 0.5rem;
    }

    .config-label-cell {
        min-width: 120px;
    }

    .config-min-cell,
    .config-max-cell {
        width: 100px;
    }
}

/* =========================================================================
   Cluster Planning Modal
   ========================================================================= */
.cluster-modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 30, 45, 0.55);
    z-index: 9000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.cluster-modal-inner {
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: min(1400px, 100%);
    height: min(900px, 100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.cluster-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 07-Jun feedback: taller ribbon so the header/filter bar breathes. */
    padding: 22px 22px;
    background: linear-gradient(90deg, #28537D, #5088C6);
    color: #fff;
}

.cluster-modal-title {
    display: flex;
    align-items: center;
    gap: 10px;
    /* Scales with viewport so the title doesn't look tiny on 1440p / 4K. */
    font-size: clamp(16px, 1.2vw, 19px);
    font-weight: 600;
}

.cluster-modal-block {
    padding: 2px 10px;
    background: rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    font-size: clamp(13px, 0.95vw, 15px);
    font-weight: 500;
}

/* 07-Jun feedback: selected cluster ID, shown in the planner header ribbon. */
.cluster-modal-id {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 2px 12px;
    background: rgba(255, 255, 255, 0.32);
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 999px;
    font-size: clamp(13px, 0.95vw, 15px);
    font-weight: 700;
    letter-spacing: 0.02em;
}

.cluster-modal-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.cluster-modal-controls select {
    /* 07-Jun feedback: wider, larger-font dropdowns so they read comfortably.
       The controls row already wraps (flex-wrap above), so on a 1366px laptop
       these wrap to a second line rather than overflowing the ribbon. */
    padding: 9px 14px;
    min-width: 160px;
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    background: #fff;
    color: #28537D;
    font-size: clamp(15px, 1vw, 16px);
    cursor: pointer;
}

.cluster-modal-close {
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s;
}
.cluster-modal-close:hover { background: rgba(255, 255, 255, 0.32); }

.cluster-modal-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 22px;
    background: #f5f7f9;
    border-bottom: 1px solid #e3e7eb;
    /* Scales modestly with viewport; clamps so phones don't shrink and 4K
       doesn't blow up. */
    font-size: clamp(13px, 0.92vw, 15px);
    color: #555;
}

.cluster-modal-actions {
    display: flex;
    gap: 10px;
    align-items: center;
}

.cluster-action-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    background: #fff;
    border: 1px solid #cfd6dc;
    color: #28537D;
    border-radius: 4px;
    font-size: clamp(12px, 0.85vw, 14px);
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}
.cluster-action-btn:hover { background: #eef3f8; }
.cluster-action-btn[disabled],
.cluster-action-btn.disabled {
    opacity: 0.5;
    pointer-events: none;
}

.cluster-modal-body {
    flex: 1;
    display: flex;
    min-height: 0;
    align-items: stretch;
}

.cluster-modal-map {
    flex: 1;
    min-width: 0;
    background: #eaf0f4;
}

.cluster-modal-side {
    /* Fluid width: grows on bigger screens so the rule-explanation list and
       village names don't wrap every 3 words on 1440p / 4K monitors. Caps at
       a generous 460px so it never starves the map. */
    width: clamp(340px, 24vw, 460px);
    min-width: 340px;
    background: #fff;
    border-left: 1px solid #e3e7eb;
    overflow-y: auto;
    padding: 14px 16px 18px 16px;
    font-size: 13px;
    color: #243240;
    box-sizing: border-box;
}

.cluster-side-empty {
    color: #888;
    font-style: italic;
    padding: 8px 4px;
}

.cluster-side-panel h3 {
    margin: 0 0 4px 0;
    font-size: 17px;
    color: #28537D;
    display: flex;
    align-items: center;
    gap: 6px;
}

.cluster-side-panel .cluster-side-sub {
    color: #607080;
    font-size: 13px;
    margin-bottom: 12px;
}

.cluster-side-panel .cluster-side-section {
    margin: 14px 0 4px 0;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #5a6b7a;
    font-weight: 600;
}

.cluster-side-panel .cluster-side-row {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
    font-size: 14px;
    border-bottom: 1px dashed #eef2f5;
}
.cluster-side-panel .cluster-side-row:last-child { border-bottom: none; }
.cluster-side-panel .cluster-side-row .label { color: #4a5868; }
.cluster-side-panel .cluster-side-row .value {
    font-weight: 600;
    color: #1d3557;
    font-variant-numeric: tabular-nums;
}
.cluster-side-panel .cluster-side-row.muted .value { color: #9aa5b1; font-weight: 500; }

.cluster-side-panel .cluster-side-back {
    background: none;
    border: none;
    color: #28537D;
    cursor: pointer;
    font-size: 12px;
    padding: 0 0 8px 0;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.cluster-side-panel .cluster-side-back:hover { text-decoration: underline; }

.cluster-side-panel details.cluster-side-other {
    margin-top: 10px;
}
.cluster-side-panel details.cluster-side-other summary {
    cursor: pointer;
    color: #28537D;
    font-size: 12px;
    padding: 4px 0;
    user-select: none;
}

.cluster-side-panel .gp-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 6px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    vertical-align: middle;
}

.cluster-side-panel .gp-chip-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 6px;
    margin: 4px 0 6px 0;
}

.cluster-side-panel .gp-chip {
    display: inline-flex;
    align-items: center;
    padding: 3px 9px 3px 5px;
    background: #f5f7f9;
    border: 1px solid #e3e7eb;
    border-radius: 999px;
    font-size: 12px;
    color: #243240;
    white-space: nowrap;
    line-height: 1.4;
}
.cluster-side-panel .gp-chip .gp-swatch {
    margin-right: 4px;
}

@media (max-width: 900px) {
    .cluster-modal-side { display: none; }
}

#open-cluster-planner {
    display: none;
    align-items: center;
    gap: 6px;
}
#open-cluster-planner:hover { background: #1f4467 !important; }

