:root {
    --topbar-height: 64px;
    --theme-rail-width-md: 96px;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
}

body {
    margin: 0;
}

#theme-sidebar {
    transform: translateX(-100%);
    transition: transform 220ms ease;
}

body.mobile-sidebar-open #theme-sidebar {
    transform: translateX(0);
}

body.mobile-sidebar-open #sidebar-backdrop {
    display: block;
}

@media (min-width: 768px) {
    #theme-sidebar {
        transform: translateX(0);
    }
}

#map {
    left: 0;
}

@media (min-width: 768px) {
    #map {
        left: var(--theme-rail-width-md);
    }
}

#theme-panel[data-open='true'] {
    animation: panelIn 160ms ease-out;
}

@keyframes panelIn {
    from {
        opacity: 0;
        transform: translateY(-6px) scale(0.985);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.theme-icon-btn {
    width: 100%;
    border: 1px solid rgba(253, 246, 228, 0.2);
    border-radius: 0.85rem;
    background: rgba(253, 246, 228, 0.08);
    color: #fdf6e4;
    transition: background-color 140ms ease, border-color 140ms ease, transform 140ms ease;
}

.theme-icon-btn:hover {
    transform: translateY(-1px);
    background: rgba(253, 246, 228, 0.16);
    border-color: rgba(253, 246, 228, 0.42);
}

.theme-icon-btn[data-active='true'] {
    background: #fdf6e4;
    color: #2f4c29;
    border-color: #a5aa6b;
    box-shadow: 0 0 0 2px rgba(165, 170, 107, 0.35);
}

.theme-icon-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px #fdf6e4, 0 0 0 4px rgba(61, 97, 49, 0.8);
}

.theme-icon-btn[data-empty='true'] {
    opacity: 0.35;
    pointer-events: none;
    filter: grayscale(0.6);
}

.theme-icon-btn--project {
    background: linear-gradient(160deg, #6B4C2A 0%, #8B6914 40%, #C4A855 100%) !important;
    border-color: rgba(196, 168, 85, 0.5) !important;
}

.theme-icon-btn--project:hover {
    background: linear-gradient(160deg, #7D5A33 0%, #9E7818 40%, #D4B860 100%) !important;
    border-color: rgba(212, 184, 96, 0.65) !important;
}

.theme-icon-btn--project[data-active='true'] {
    background: linear-gradient(160deg, #F5ECD4 0%, #E8D9A8 50%, #D4C48C 100%) !important;
    color: #4A3518 !important;
    border-color: #B8A050 !important;
    box-shadow: 0 0 0 2px rgba(184, 160, 80, 0.4);
}

.layer-row {
    transition: border-color 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.catalog-layer-card {
    background-color: rgba(255, 255, 255, 0.84) !important;
    border-color: rgba(165, 170, 107, 0.45) !important;
    backdrop-filter: blur(1px);
    -webkit-backdrop-filter: blur(1px);
}

.catalog-layer-card:hover {
    background-color: rgba(255, 255, 255, 0.92) !important;
    border-color: rgba(122, 147, 86, 0.55) !important;
}

.layer-row-active {
    border-color: #7a9356 !important;
    background: rgba(243, 240, 219, 0.9) !important;
    box-shadow: inset 0 0 0 1px rgba(122, 147, 86, 0.28);
}

.layer-row-disabled {
    opacity: 0.45;
    pointer-events: none;
}

.layer-row-disabled .layer-disabled-badge {
    display: inline-block;
}

#layer-tooltip::after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: -5px;
    width: 10px;
    height: 10px;
    transform: translateX(-50%) rotate(45deg);
    background: #1e2f1b;
    border-right: 1px solid rgba(64, 94, 55, 0.35);
    border-bottom: 1px solid rgba(64, 94, 55, 0.35);
}

#layer-panel,
#legend-content {
    scrollbar-width: thin;
    scrollbar-color: rgba(64, 94, 55, 0.45) rgba(253, 246, 228, 0.1);
}

#layer-panel::-webkit-scrollbar,
#legend-content::-webkit-scrollbar {
    width: 8px;
}

#layer-panel::-webkit-scrollbar-thumb,
#legend-content::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: rgba(64, 94, 55, 0.45);
}

.maplibregl-ctrl-top-right {
    top: 12px;
    right: 12px;
}

.maplibregl-ctrl-bottom-right {
    bottom: 12px;
    right: 12px;
}

.maplibregl-ctrl-group {
    border: 1px solid rgba(64, 94, 55, 0.38);
    border-radius: 10px;
    overflow: hidden;
    background: rgba(253, 246, 228, 0.86);
    box-shadow: 0 10px 22px rgba(36, 57, 34, 0.2);
}

.maplibregl-ctrl button,
.mapbox-gl-draw_ctrl-draw-btn,
.mapbox-gl-draw_ctrl-trash {
    background-color: rgba(253, 246, 228, 0.88);
}

.maplibregl-ctrl button:hover,
.mapbox-gl-draw_ctrl-draw-btn:hover,
.mapbox-gl-draw_ctrl-trash:hover {
    background-color: rgba(165, 170, 107, 0.27);
}

.maplibregl-ctrl-scale {
    border: 1px solid rgba(64, 94, 55, 0.42);
    border-radius: 8px;
    background: rgba(253, 246, 228, 0.85);
    color: #2d4727;
    padding: 2px 8px;
}

@media (max-width: 767px) {
    /* Brand: show subtitle, keep on one line */
    .brand-block p {
        display: block;
        font-size: 0.42rem;
        line-height: 1.3;
        letter-spacing: 0.1em;
        white-space: normal;
    }

    .brand-block h1 {
        font-size: 1.05rem;
        white-space: nowrap;
    }

    /* Top bar right: compact buttons */
    .top-bar-right {
        gap: 0.2rem;
    }

    #env-badge,
    #lang-toggle,
    #auth-btn,
    #auth-user-display button {
        padding-left: 0.4rem;
        padding-right: 0.4rem;
        font-size: 0.52rem;
        min-height: 22px;
        letter-spacing: 0.07em;
        white-space: nowrap;
    }

    /* Theme sidebar: narrower on mobile */
    #theme-sidebar {
        width: 56px;
    }

    #theme-buttons .theme-icon-btn {
        padding-top: 0.35rem;
        padding-bottom: 0.35rem;
        font-size: 0.38rem;
        border-radius: 0.5rem;
        gap: 0.15rem;
    }

    #theme-buttons .theme-icon-btn svg {
        width: 1rem;
        height: 1rem;
    }

    /* Chat input: smaller text on mobile */
    #agent-input {
        font-size: 0.75rem;
    }

    #agent-chat[data-state="closed"] {
        left: 1rem;
        bottom: 0.75rem;
    }
}

/* Agent chat animations */
#agent-window {
    animation: agentSlideUp 200ms ease-out;
}

@keyframes agentSlideUp {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.97);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

#agent-messages {
    scrollbar-width: thin;
    scrollbar-color: rgba(64, 94, 55, 0.3) transparent;
}

#agent-messages .agent-msg-assistant,
#agent-messages .agent-msg-user {
    font-size: 0.72rem;
}

.agent-dots span {
    animation: agentBlink 1.4s infinite both;
    font-weight: bold;
    font-size: 1.2em;
}

.agent-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.agent-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes agentBlink {
    0%, 80%, 100% { opacity: 0.2; }
    40% { opacity: 1; }
}

/* --- Agent bottom panel --- */
#agent-chat[data-state="expanded"] {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 0;
    z-index: 50;
}

@media (min-width: 768px) {
    #agent-chat[data-state="expanded"] {
        left: 7rem;   /* align with layer catalog (md:left-28) */
        right: 16rem; /* narrower panel, more map visible on right */
        bottom: 1rem; /* gap so map credits are visible */
        width: auto !important;
        border-radius: 0.75rem;
    }
}

#agent-chat[data-state="expanded"] #agent-window {
    height: var(--agent-panel-height, 40vh) !important;
    min-height: 180px;
    max-height: 70vh;
    border-radius: 0.75rem 0.75rem 0 0;
    margin: 0;
    background-color: rgba(253, 250, 240, 0.55) !important;
}

@media (min-width: 768px) {
    #agent-chat[data-state="expanded"] #agent-window {
        border-radius: 0.75rem; /* all corners rounded when floating */
    }
}

/* Closed state — panel hidden, toggle button over the map viewport */
#agent-chat[data-state="closed"] {
    bottom: 1rem;
    left: 8rem;   /* past the sidebar (md:left-28 = 7rem) + gap */
    width: auto !important;
}

/* Map always fills full viewport — agent panel floats on top */

.agent-drag-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 8px;
    cursor: ns-resize;
    user-select: none;
    -webkit-user-select: none;
    background: transparent;
}

.agent-drag-bar {
    width: 48px;
    height: 4px;
    border-radius: 2px;
    background: rgba(64, 94, 55, 0.3);
    transition: background 150ms;
}

.agent-drag-handle:hover .agent-drag-bar {
    background: rgba(64, 94, 55, 0.55);
}

@media (max-width: 767px) {
    /* Respect drag-to-resize on mobile */
    #agent-chat[data-state="expanded"] #agent-window {
        height: var(--agent-panel-height, 40vh) !important;
    }

    #agent-viz-col {
        display: none !important;
    }

    /* Narrow agent panel when sidebar is open — anchor right edge, push left edge */
    body.mobile-sidebar-open #agent-chat[data-state="expanded"] {
        left: 56px;
        width: auto !important;
        max-width: calc(100% - 56px) !important;
    }
}

/* ========================================
   Auth Sidebar
   ======================================== */

/* Sidebar slide-in from right */
@keyframes authSidebarIn {
    from { transform: translateX(100%); }
    to   { transform: translateX(0); }
}

/* Sidebar slide-out to right */
@keyframes authSidebarOut {
    from { transform: translateX(0); }
    to   { transform: translateX(100%); }
}

/* Toast slide-down from top of sidebar */
@keyframes authToastIn {
    from { opacity: 0; transform: translateY(-100%); }
    to   { opacity: 1; transform: translateY(0);      }
}

.auth-sidebar {
    background: rgba(240, 237, 222, 0.78);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border-left: 1px solid rgba(255, 255, 255, 0.45);
    box-shadow: -4px 0 32px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    transform: translateX(100%);
}

.auth-sidebar.auth-sidebar-open {
    animation: authSidebarIn 280ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.auth-sidebar.auth-sidebar-closing {
    animation: authSidebarOut 220ms ease-in forwards;
}

.auth-view {
    animation: none;
}

.auth-view.hidden {
    animation: none;
}

/* Toast — floats over the form at the top of the sidebar */
.auth-toast {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 11px 20px;
    font-size: 0.8125rem;
    font-weight: 500;
    text-align: center;
    z-index: 20;
    animation: authToastIn 280ms cubic-bezier(0.34, 1.2, 0.64, 1);
}

.auth-toast.auth-toast-success {
    background: rgba(220, 252, 231, 0.92);
    color: #166534;
    border-bottom: 1px solid #bbf7d0;
}

.auth-toast.auth-toast-error {
    background: rgba(254, 226, 226, 0.92);
    color: #991b1b;
    border-bottom: 1px solid #fecaca;
}

.auth-input {
    width: 100%;
    border: 1px solid rgba(209, 213, 219, 0.8);
    border-radius: 8px;
    padding: 9px 12px;
    font-size: 0.875rem;
    outline: none;
    background: rgba(255, 255, 255, 0.55);
    transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
    box-sizing: border-box;
}

.auth-input:focus {
    border-color: #3D6131;
    background: rgba(255, 255, 255, 0.8);
    box-shadow: 0 0 0 3px rgba(61, 97, 49, 0.15);
}

.auth-submit-btn {
    width: 100%;
    background: #3D6131;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
    box-shadow: 0 2px 6px rgba(61, 97, 49, 0.3);
}

.auth-submit-btn:hover {
    background: #2F4C29;
    box-shadow: 0 4px 12px rgba(61, 97, 49, 0.35);
    transform: translateY(-1px);
}

.auth-submit-btn:active {
    transform: translateY(0) scale(0.98);
    box-shadow: 0 1px 4px rgba(61, 97, 49, 0.25);
}

/* ── Geographic Selector ── */
#geo-selector-dropdown {
    z-index: 50;
}

.geo-tree-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 6px;
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
}

.geo-tree-item:hover {
    background: #f0edd8;
}

.geo-tree-ccaa {
    font-weight: 600;
    color: #294222;
}

.geo-tree-province {
    padding-left: 16px;
    color: #3d5a34;
}

.geo-tree-municipality {
    padding-left: 32px;
    color: #5a6855;
}

.geo-tree-expand {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    transition: transform 0.15s ease;
}

.geo-tree-expand.open {
    transform: rotate(90deg);
}

.geo-chip {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 2px 8px;
    border-radius: 9999px;
    background: #e8e4cf;
    color: #294222;
    font-size: 0.65rem;
    font-weight: 500;
    white-space: nowrap;
}

.geo-chip button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    border: none;
    background: transparent;
    cursor: pointer;
    color: #7b846e;
    font-size: 12px;
    padding: 0;
    line-height: 1;
}

.geo-chip button:hover {
    color: #dc2626;
}

.geo-select-icon {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    color: #7b846e;
    cursor: pointer;
    margin-left: auto;
}

.geo-select-icon:hover {
    color: #294222;
}

/* ========================================
   Agent Report Cards
   ======================================== */

.report-container {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 0.72rem;
    color: #2d4727;
    padding: 12px;
    background: white;
    border-radius: 10px;
}

.report-header {
    font-size: 0.6rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #3D6131;
    margin-bottom: 2px;
}

.report-title {
    font-size: 0.88rem;
    font-weight: 700;
    color: #1e2f1b;
    margin-bottom: 12px;
}

.report-section {
    background: white;
    border: 1px solid rgba(61, 97, 49, 0.22);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
}

.report-section-title {
    font-size: 0.68rem;
    font-weight: 700;
    color: #3D6131;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* Categorical bar rows */
.report-bar-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 5px;
}

.report-bar-label {
    flex: 0 0 110px;
    font-size: 0.65rem;
    color: #3D6131;
    text-align: right;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.report-bar-track {
    flex: 1;
    height: 14px;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 4px;
    overflow: hidden;
}

.report-bar-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 400ms ease-out;
}

.report-bar-pct {
    flex: 0 0 40px;
    font-size: 0.65rem;
    font-weight: 600;
    color: #2d4727;
    text-align: right;
}

.report-pixel-count {
    font-size: 0.58rem;
    color: #3D6131;
    margin-top: 6px;
    text-align: right;
}

/* Risk badge */
.report-risk-badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 9999px;
    font-size: 0.6rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 8px;
}

.report-risk-low {
    background: rgba(22, 163, 74, 0.15);
    color: #15803d;
}

.report-risk-moderate {
    background: rgba(245, 158, 11, 0.15);
    color: #b45309;
}

.report-risk-high {
    background: rgba(220, 38, 38, 0.15);
    color: #dc2626;
}

.report-risk-very_high {
    background: rgba(127, 29, 29, 0.2);
    color: #7f1d1d;
}

/* Stat cards grid */
.report-stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin-bottom: 4px;
}

.report-stat-card {
    background: #f9faf8;
    border: 1px solid rgba(61, 97, 49, 0.2);
    border-radius: 8px;
    padding: 6px 8px;
    text-align: center;
}

.report-stat-value {
    font-size: 0.82rem;
    font-weight: 700;
    color: #1e2f1b;
}

.report-stat-label {
    font-size: 0.55rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #3D6131;
    margin-top: 1px;
}

/* Comparison table */
.report-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.65rem;
    margin-top: 8px;
}

.report-table th {
    text-align: left;
    font-weight: 600;
    color: #3D6131;
    padding: 4px 6px;
    border-bottom: 1px solid rgba(61, 97, 49, 0.3);
}

.report-table td {
    padding: 4px 6px;
    color: #2d4727;
    border-bottom: 1px solid rgba(61, 97, 49, 0.12);
}

.report-table .report-color-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 4px;
    vertical-align: middle;
}

/* Floating overlay for mobile / closed viz panel */
.report-floating-overlay {
    position: fixed;
    bottom: 80px;
    right: 16px;
    width: 320px;
    max-height: 60vh;
    overflow-y: auto;
    background: rgba(253, 250, 240, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(61, 97, 49, 0.3);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(36, 57, 34, 0.25);
    z-index: 60;
    animation: agentSlideUp 200ms ease-out;
}

.report-floating-overlay .report-close-btn {
    position: absolute;
    top: 6px;
    right: 8px;
    background: none;
    border: none;
    font-size: 1rem;
    color: #3D6131;
    cursor: pointer;
    line-height: 1;
    padding: 2px;
}

.report-floating-overlay .report-close-btn:hover {
    color: #1e2f1b;
}

/* Inline report card in message thread (mobile) */
.report-inline-card {
    background: rgba(61, 97, 49, 0.05);
    border: 1px solid rgba(61, 97, 49, 0.2);
    border-radius: 8px;
    padding: 8px 10px;
    margin-top: 6px;
    font-size: 0.68rem;
}

/* Scrollbar for viz content */
#agent-viz-content {
    scrollbar-width: thin;
    scrollbar-color: rgba(61, 97, 49, 0.35) transparent;
}

#agent-viz-content::-webkit-scrollbar {
    width: 6px;
}

#agent-viz-content::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: rgba(61, 97, 49, 0.35);
}

/* Responsive: 2 cols on narrow screens */
@media (max-width: 360px) {
    .report-stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .report-bar-label {
        flex: 0 0 80px;
        font-size: 0.6rem;
    }
}

@media (max-width: 767px) {
    .report-floating-overlay {
        right: 8px;
        left: 8px;
        width: auto;
        bottom: 70px;
        max-height: 50vh;
    }
}

@media (max-width: 767px) {
    /* Layer catalog panel: position + sizing */
    #theme-panel {
        /* Default: sidebar hidden — start just after its hidden edge */
        left: 4px;
        /* Right: leave room for zoom/draw controls (~48px wide at right: 12px) */
        right: 58px;
    }

    /* Sidebar open: push catalog past it */
    body.mobile-sidebar-open #theme-panel {
        left: 60px;
    }

    /* Tighter header */
    #theme-panel > div:first-child {
        padding: 0.625rem 0.75rem;
    }

    #theme-panel-title {
        font-size: 1.1rem;
    }

    #theme-panel-eyebrow {
        font-size: 0.52rem;
    }

    /* Search bar */
    #layer-filter {
        height: 2rem;
        font-size: 0.65rem;
        padding-left: 1.9rem;
        padding-right: 0.5rem;
    }

    /* Search icon: vertically center relative to reduced input height */
    #theme-panel .relative > svg {
        top: 50%;
        transform: translateY(-50%);
    }

    #layer-count-badge {
        padding: 0.15rem 0.4rem;
        font-size: 0.65rem;
        min-width: 1.75rem;
    }

    #layer-panel {
        max-height: 38vh;
    }

    /* Layer row: tighter padding */
    .catalog-layer-card {
        padding: 0.35rem 0.5rem;
    }

    /* Layer row: smaller text */
    #layer-list .catalog-layer-card p.truncate {
        font-size: 0.65rem;
    }

    #layer-list .catalog-layer-card p.text-\[0\.62rem\] {
        font-size: 0.55rem;
    }

    /* Layer info button: smaller */
    #layer-list [data-role="layer-info"] {
        height: 1.375rem;
        width: 1.375rem;
        font-size: 0.6rem;
    }
}

/* ── Viewer lock (during agent analysis) ───────────────────── */

body.viewer-locked [data-role="theme-button"],
body.viewer-locked #layer-list .catalog-layer-card,
body.viewer-locked #geo-selector-btn,
body.viewer-locked .mapbox-gl-draw_ctrl-draw-btn {
    opacity: 0.45;
    pointer-events: none;
    filter: grayscale(0.3);
    transition: opacity 0.2s, filter 0.2s;
}

.agent-lock-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    margin: 0.5rem 0.75rem;
    padding: 0.375rem 0.75rem;
    border-radius: 0.5rem;
    background: #2F4C29;
    border: 1px solid #3D6131;
    color: #ffffff;
    font-size: 0.75rem;
    animation: fadeIn 0.2s ease-out;
}

.agent-lock-banner .lock-icon {
    width: 0.75rem;
    height: 0.75rem;
    flex-shrink: 0;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}
