/* ============================================================
   Mapa de Obras Públicas — Estilos globales
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --blue-900:  #1E3A8A;
    --blue-800:  #1D4ED8;
    --blue-600:  #2563EB;
    --blue-100:  #DBEAFE;
    --gray-900:  #111827;
    --gray-700:  #374151;
    --gray-500:  #6B7280;
    --gray-300:  #D1D5DB;
    --gray-100:  #F3F4F6;
    --white:     #FFFFFF;
    --red:       #EF4444;
    --green:     #10B981;
    --yellow:    #F59E0B;
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -2px rgba(0,0,0,.1);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.15), 0 4px 6px -4px rgba(0,0,0,.1);
    --radius:    10px;
    --transition: .2s ease;
    --topbar-h:  clamp(3rem, 8vh, 4rem);
    --touch-min: 44px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

html, body {
    height: 100%;
    min-height: 100dvh;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    color: var(--gray-900);
}

/* ============================================================
   Mapa a pantalla completa (base para index.php y dashboard.php)
   ============================================================ */
.map-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.map-wrapper {
    position: relative;
    flex: 1;
    min-height: 0;
    width: 100%;
}

#map {
    flex: 1;
    width: 100%;
    height: 100%;
    min-height: 200px;
}

/* Filtro por período (debajo de la barra superior) */
.period-filter {
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: .45rem max(1rem, var(--safe-l)) .45rem max(1rem, var(--safe-r));
    background: var(--white);
    border-bottom: 1px solid var(--gray-300);
    flex-shrink: 0;
    z-index: 90;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.period-filter::-webkit-scrollbar { display: none; }

.period-btn {
    flex-shrink: 0;
    padding: .4rem .85rem;
    min-height: 36px;
    border: 1.5px solid var(--gray-300);
    border-radius: 999px;
    background: var(--gray-100);
    color: var(--gray-700);
    font-size: .8rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.period-btn:hover {
    border-color: var(--blue-600);
    color: var(--blue-800);
}

.period-btn.is-active {
    background: var(--blue-800);
    border-color: var(--blue-800);
    color: var(--white);
}

.points-count-muted {
    font-size: .72em;
    color: var(--gray-500);
    font-weight: 400;
}

/* ============================================================
   Barra superior
   ============================================================ */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--blue-900);
    color: var(--white);
    padding: 0 max(1rem, var(--safe-l)) 0 max(1rem, var(--safe-r));
    min-height: var(--topbar-h);
    height: auto;
    flex-shrink: 0;
    gap: .5rem;
    flex-wrap: wrap;
    z-index: 100;
    box-shadow: var(--shadow-md);
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: .02em;
    text-decoration: none;
    color: var(--white);
}
.topbar-brand svg { width: 24px; height: 24px; }
.topbar-icon {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    object-fit: contain;
    flex-shrink: 0;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: .5rem;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.btn-label-short,
.brand-short { display: none; }

.topbar-user {
    font-size: .85rem;
    opacity: .85;
}

.badge-live {
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    background: rgba(16, 185, 129, .2);
    color: #6EE7B7;
    border: 1px solid rgba(16,185,129,.4);
    border-radius: 999px;
    font-size: .72rem;
    font-weight: 600;
    padding: .15rem .55rem;
    letter-spacing: .04em;
    text-transform: uppercase;
}
.badge-live::before {
    content: '';
    width: 6px; height: 6px;
    background: var(--green);
    border-radius: 50%;
    animation: pulse 1.8s infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50%       { opacity: .3; }
}

/* ============================================================
   Leyenda flotante (esquina inferior izquierda)
   ============================================================ */
.legend {
    position: absolute;
    bottom: 2.5rem;
    left: 1rem;
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: .75rem 1rem;
    z-index: 50;
    min-width: 170px;
}
.legend h3 {
    font-size: .75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--gray-500);
    margin-bottom: .5rem;
}
.legend-item {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-size: .82rem;
    color: var(--gray-700);
    margin-bottom: .3rem;
}
.legend-dot {
    width: 12px; height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* Leyendas móvil: dos botones + panel */
.legend-chips {
    display: none;
    position: fixed;
    bottom: calc(1rem + var(--safe-b));
    right: calc(1rem + var(--safe-r));
    z-index: 56;
    flex-direction: column;
    gap: .45rem;
    align-items: stretch;
}

.legend-chip {
    padding: .5rem .85rem;
    min-height: var(--touch-min);
    background: var(--white);
    color: var(--blue-800);
    border: 2px solid var(--blue-800);
    border-radius: 999px;
    font-size: .8rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    white-space: nowrap;
}

.legend-chip[aria-expanded="true"] {
    background: var(--blue-800);
    color: var(--white);
}

.legends-drawer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 58;
    background: var(--white);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 30px rgba(0,0,0,.18);
    transform: translateY(105%);
    transition: transform .28s ease;
    max-height: min(70dvh, 480px);
    display: flex;
    flex-direction: column;
}

.legends-drawer.is-open { transform: translateY(0); }

.legends-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .85rem 1rem;
    border-bottom: 1px solid var(--gray-100);
    font-weight: 700;
    color: var(--blue-900);
    flex-shrink: 0;
}

.legends-drawer-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    color: var(--gray-500);
    padding: .25rem .5rem;
}

.legends-drawer-body {
    overflow-y: auto;
    padding: 1rem max(1rem, var(--safe-r)) calc(1rem + var(--safe-b)) max(1rem, var(--safe-l));
    -webkit-overflow-scrolling: touch;
}

.legend-panel h3 {
    font-size: .75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--gray-500);
    margin-bottom: .35rem;
}

.legend-hint {
    font-size: .78rem;
    color: var(--gray-500);
    margin-bottom: .65rem;
}

.legends-desktop {
    display: none;
}

.legends-backdrop {
    position: fixed;
    inset: 0;
    z-index: 54;
    background: rgba(17, 24, 39, .45);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
}

.legends-backdrop.is-visible {
    opacity: 1;
    pointer-events: auto;
}

/* Modo ubicar punto (staff móvil) */
.map-pick-hint {
    position: fixed;
    left: 50%;
    bottom: calc(1rem + var(--safe-b));
    transform: translateX(-50%);
    z-index: 180;
    display: flex;
    align-items: center;
    gap: .65rem;
    flex-wrap: wrap;
    justify-content: center;
    max-width: calc(100% - 2rem);
    padding: .65rem 1rem;
    background: var(--blue-900);
    color: var(--white);
    border-radius: 999px;
    box-shadow: var(--shadow-lg);
    font-size: .85rem;
    font-weight: 600;
}

.map-pick-hint.hidden { display: none !important; }

.map-pick-mode .staff-panel.hidden { pointer-events: none; }

.staff-panel--compact .panel-body {
    max-height: min(42dvh, 360px);
}

.legend-estados {
    min-width: 150px;
}

.legend-tipos {
    max-width: 100%;
    max-height: none;
    overflow-y: visible;
}

.legend-tipos-grid {
    display: flex;
    flex-direction: column;
    gap: .15rem;
}

.legend-tipo-item {
    margin-bottom: 0;
}

.legend-tipo-icon {
    font-size: 1.05rem;
    width: 1.4rem;
    text-align: center;
    flex-shrink: 0;
}

/* ============================================================
   Contador de puntos (esquina superior derecha del mapa)
   ============================================================ */
.points-count {
    position: absolute;
    top: calc(var(--topbar-h) + 3.25rem);
    right: max(1rem, var(--safe-r));
    left: auto;
    max-width: min(320px, calc(100% - 2rem));
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: .5rem .9rem;
    font-size: .82rem;
    color: var(--gray-700);
    z-index: 50;
}
.points-count strong { color: var(--blue-800); }

/* ============================================================
   Panel staff — flotante lateral izquierdo
   ============================================================ */
.staff-panel {
    position: absolute;
    top: 68px;
    left: 1rem;
    width: 320px;
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    z-index: 200;
    overflow: hidden;
    transition: transform var(--transition), opacity var(--transition);
}
.staff-panel.hidden {
    transform: translateX(-340px);
    opacity: 0;
    pointer-events: none;
}

.panel-header {
    background: var(--blue-800);
    color: var(--white);
    padding: .75rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.panel-header h2 { font-size: .95rem; font-weight: 700; }

.panel-close {
    background: none;
    border: none;
    color: var(--white);
    cursor: pointer;
    font-size: 1.25rem;
    line-height: 1;
    opacity: .8;
    padding: 0 .2rem;
    transition: opacity var(--transition);
}
.panel-close:hover { opacity: 1; }

.panel-body {
    padding: 1rem;
    overflow-y: auto;
    max-height: calc(100dvh - 160px);
    -webkit-overflow-scrolling: touch;
}

/* ============================================================
   Formulario de punto
   ============================================================ */
.field { margin-bottom: .85rem; }
.field label {
    display: block;
    font-size: .8rem;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: .3rem;
}
.field input,
.field select,
.field textarea {
    width: 100%;
    padding: .5rem .75rem;
    border: 1.5px solid var(--gray-300);
    border-radius: 6px;
    font-size: .875rem;
    font-family: inherit;
    color: var(--gray-900);
    background: var(--white);
    transition: border-color var(--transition), box-shadow var(--transition);
}
.field input:focus,
.field select:focus,
.field textarea:focus {
    outline: none;
    border-color: var(--blue-600);
    box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
.field textarea { resize: vertical; min-height: 80px; }

.coords-display {
    font-size: .75rem;
    color: var(--gray-500);
    margin-top: .25rem;
    font-style: italic;
}

/* ============================================================
   Botones
   ============================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    padding: .5rem 1rem;
    border-radius: 6px;
    font-size: .875rem;
    font-weight: 600;
    border: none;
    cursor: pointer;
    transition: background var(--transition), opacity var(--transition), transform .1s;
    text-decoration: none;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary   { background: var(--blue-800); color: var(--white); }
.btn-primary:hover:not(:disabled) { background: var(--blue-900); }

.btn-danger    { background: var(--red); color: var(--white); }
.btn-danger:hover:not(:disabled) { background: #DC2626; }

.btn-secondary { background: var(--gray-100); color: var(--gray-700); border: 1.5px solid var(--gray-300); }
.btn-secondary:hover { background: var(--gray-300); }

.btn-block     { width: 100%; }
.btn-sm        { padding: .4rem .75rem; font-size: .8rem; min-height: 36px; }

@media (hover: none) and (pointer: coarse) {
    .btn,
    .btn-sm,
    .legend-chip,
    .period-btn,
    .panel-close {
        min-height: var(--touch-min);
    }
    .period-btn { min-width: auto; padding-left: 1rem; padding-right: 1rem; }
    .field input,
    .field select,
    .field textarea {
        min-height: var(--touch-min);
        font-size: 16px;
    }
}

.btn-group { display: flex; gap: .5rem; margin-top: .5rem; }

/* ============================================================
   Instrucción para agregar punto
   ============================================================ */
.add-instruction {
    background: var(--blue-100);
    border: 1.5px dashed var(--blue-600);
    border-radius: 6px;
    padding: .7rem .9rem;
    font-size: .82rem;
    color: var(--blue-800);
    text-align: center;
    margin-bottom: 1rem;
    display: none;
}
.add-instruction.visible { display: block; }

/* ============================================================
   Toast notifications
   ============================================================ */
#toast-container {
    position: fixed;
    bottom: calc(1.5rem + var(--safe-b));
    right: calc(1.5rem + var(--safe-r));
    left: calc(1.5rem + var(--safe-l));
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}
.toast {
    background: var(--gray-900);
    color: var(--white);
    padding: .65rem 1.1rem;
    border-radius: 8px;
    font-size: .85rem;
    box-shadow: var(--shadow-lg);
    animation: slideIn .25s ease;
    max-width: 100%;
    margin-left: auto;
}
.toast.success { background: #065F46; }
.toast.error   { background: #7F1D1D; }
@keyframes slideIn {
    from { transform: translateX(60px); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

/* ============================================================
   Login page
   ============================================================ */
.login-body {
    min-height: 100vh;
    background: linear-gradient(135deg, var(--blue-900) 0%, #1e40af 60%, #1d4ed8 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}

.login-card {
    background: var(--white);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    padding: 2.5rem 2rem;
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.login-logo svg { width: 60px; height: 60px; margin-bottom: 1rem; }
.login-logo-img {
    width: 72px;
    height: 72px;
    object-fit: contain;
    margin-bottom: 1rem;
    border-radius: 12px;
}

.login-title {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--gray-900);
    margin-bottom: .3rem;
}
.login-subtitle {
    font-size: .85rem;
    color: var(--gray-500);
    margin-bottom: 1.75rem;
}

.login-form { text-align: left; }
.login-form .field { margin-bottom: 1rem; }

.login-back {
    display: inline-block;
    margin-top: 1.25rem;
    font-size: .82rem;
    color: var(--blue-800);
    text-decoration: none;
}
.login-back:hover { text-decoration: underline; }

/* ============================================================
   Alerts
   ============================================================ */
.alert {
    padding: .6rem .9rem;
    border-radius: 6px;
    font-size: .85rem;
    margin-bottom: 1rem;
    text-align: left;
}
.alert-error { background: #FEF2F2; color: #B91C1C; border: 1px solid #FECACA; }

/* ============================================================
   Info Window personalizado de Google Maps
   ============================================================ */
.gm-info {
    font-family: 'Segoe UI', system-ui, sans-serif;
    min-width: 200px;
    max-width: 260px;
}
.gm-tipo-header {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-bottom: .5rem;
    padding-bottom: .45rem;
    border-bottom: 1px solid var(--gray-100);
}
.gm-tipo-icon {
    font-size: 1.5rem;
    line-height: 1;
}
.gm-tipo-name {
    font-size: .9rem;
    font-weight: 700;
    color: var(--blue-800);
}
.gm-info h4 {
    font-size: .95rem;
    font-weight: 700;
    color: var(--gray-900);
    margin-bottom: .35rem;
    line-height: 1.3;
}
.gm-info .gm-badge {
    display: inline-block;
    font-size: .7rem;
    font-weight: 700;
    padding: .15rem .5rem;
    border-radius: 999px;
    color: var(--white);
    margin-bottom: .4rem;
}
.gm-info p {
    font-size: .82rem;
    color: var(--gray-700);
    line-height: 1.45;
    margin-bottom: .3rem;
}
.gm-info .gm-meta {
    font-size: .72rem;
    color: var(--gray-500);
    margin-top: .4rem;
    border-top: 1px solid var(--gray-100);
    padding-top: .4rem;
}
.gm-info .gm-actions {
    display: flex;
    gap: .4rem;
    margin-top: .6rem;
}

/* ============================================================
   Spinner de carga
   ============================================================ */
.spinner {
    width: 18px; height: 18px;
    border: 2.5px solid rgba(255,255,255,.4);
    border-top-color: white;
    border-radius: 50%;
    animation: spin .7s linear infinite;
    display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   Responsive — móvil, tablet, escritorio, TV
   ============================================================ */

/* Móvil y tablet portrait (< 900px) */
@media (max-width: 899px) {
    .legend-chips { display: flex; }

    body.legends-open { overflow: hidden; }
    body.legends-open .legend-chips { opacity: .35; pointer-events: none; }

    .points-count {
        top: calc(var(--topbar-h) + 3rem);
        bottom: auto;
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        text-align: center;
        font-size: .78rem;
        padding: .4rem .75rem;
        max-width: calc(100% - 5rem);
    }

    body.legends-open .points-count,
    body.map-pick-mode .points-count,
    body.map-pick-mode .legend-chips {
        opacity: 0;
        pointer-events: none;
    }

    body.map-pick-mode .staff-panel:not(.hidden) {
        z-index: 200;
    }

    .topbar-brand .brand-long { display: none; }
    .topbar-brand .brand-short { display: inline; }

    .topbar-user {
        max-width: 6rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .btn-label-long { display: none; }
    .btn-label-short { display: inline; }

    .staff-panel {
        width: 100%;
        max-width: 100%;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        border-radius: 16px 16px 0 0;
        transform: none;
    }

    .staff-panel.hidden {
        transform: translateY(100%);
        opacity: 1;
        pointer-events: none;
    }

    .panel-body {
        max-height: min(50dvh, 400px);
    }

    .map-pick-mode .staff-panel:not(.hidden) {
        max-height: 45dvh;
    }

    .map-pick-hint {
        bottom: calc(5.5rem + var(--safe-b));
    }

    #toast-container {
        bottom: calc(5rem + var(--safe-b));
    }
}

/* Móvil pequeño */
@media (max-width: 480px) {
    .topbar { padding-top: .35rem; padding-bottom: .35rem; }
    .badge-live { font-size: .65rem; }
    .topbar-right { width: 100%; justify-content: space-between; }
    .login-card { padding: 1.75rem 1.25rem; }
    .gm-info { max-width: min(280px, 85vw); }
}

/* Tablet landscape y pantallas medianas */
@media (min-width: 600px) and (max-width: 899px) {
    .staff-panel {
        width: min(400px, 92vw);
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        border-radius: var(--radius);
        bottom: 1rem;
    }

    .staff-panel.hidden {
        transform: translate(-50%, 120%);
    }

    .points-count {
        bottom: calc(1rem + var(--safe-b));
        left: auto;
        right: 1rem;
        transform: none;
        text-align: right;
    }

}

/* Escritorio y tablet grande */
@media (min-width: 900px) {
    .legend-chips,
    .legends-drawer,
    .legends-backdrop { display: none !important; }

    .legends-desktop {
        display: block;
    }

    .legends-desktop .legend {
        position: absolute;
        pointer-events: auto;
    }

    .legends-desktop .legend-tipos {
        bottom: 9.5rem;
        left: 1rem;
        max-width: 240px;
        max-height: 260px;
        overflow-y: auto;
    }

    .legends-desktop .legend-estados {
        bottom: 1rem;
        left: 1rem;
    }

    .brand-short { display: none !important; }
    .topbar-brand .brand-long { display: inline; }

    .map-pick-hint {
        bottom: 2rem;
    }
}

/* Pantallas grandes y televisores */
@media (min-width: 1200px) {
    .topbar {
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }

    .topbar-brand { font-size: clamp(1rem, 1.2vw, 1.25rem); }
    .topbar-icon { width: clamp(28px, 2.5vw, 40px); height: clamp(28px, 2.5vw, 40px); }

    .legend {
        padding: 1rem 1.25rem;
        font-size: clamp(.85rem, 1vw, 1rem);
    }

    .legend h3 { font-size: clamp(.75rem, .9vw, .85rem); }

    .points-count {
        font-size: clamp(.85rem, 1vw, 1.05rem);
        padding: .65rem 1.1rem;
    }

    .staff-panel { width: min(380px, 28vw); }

    .btn { font-size: clamp(.875rem, 1vw, 1rem); padding: .6rem 1.15rem; }

    .login-card {
        max-width: min(440px, 35vw);
        padding: clamp(2rem, 3vw, 3rem);
    }

    .login-title { font-size: clamp(1.35rem, 2vw, 1.75rem); }
}

@media (min-width: 1920px) {
    :root {
        --topbar-h: 4.5rem;
    }

    .legend-tipos { bottom: 11rem; max-width: 280px; }
    .staff-panel { width: 420px; }
    .panel-header h2 { font-size: 1.1rem; }
    .field label { font-size: .9rem; }
    .field input, .field select, .field textarea { font-size: 1rem; }
}
