:root {
    --navy: #000a32;
    --indigo: #232c77;
    --blue: #009fe3;
    --orange: #f19106;
    --bg: #f4f6fb;
    --card-bg: #ffffff;
    --text: #1c2340;
    --text-muted: #6b7280;
    --border: #e6e9f2;
    --danger: #e0453f;
    --success: #22a35a;
    --radius: 14px;
    --shadow: 0 2px 10px rgba(0, 10, 50, 0.06);
    --shadow-lg: 0 8px 30px rgba(0, 10, 50, 0.10);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
}

a { color: inherit; text-decoration: none; }

.layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
    width: 250px;
    background: linear-gradient(180deg, var(--navy) 0%, var(--indigo) 100%);
    color: #fff;
    display: flex;
    flex-direction: column;
    padding: 22px 14px;
    flex-shrink: 0;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 22px 8px; }
.sidebar .brand img { width: 34px; height: 34px; border-radius: 8px; }
.sidebar .brand-text { font-weight: 700; font-size: 15px; line-height: 1.15; }
.sidebar .brand-text small { display: block; font-weight: 400; opacity: .65; font-size: 11px; }

.nav-section { margin-top: 14px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .5; padding: 6px 12px; }
.nav-link {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px; border-radius: 10px; font-size: 14px;
    color: rgba(255,255,255,.85); margin-bottom: 2px;
}
.nav-link i { font-size: 16px; width: 18px; text-align: center; }
.nav-link:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-link.active { background: var(--blue); color: #fff; font-weight: 600; }

.sidebar-footer { margin-top: auto; padding: 12px 8px 0; font-size: 12px; opacity: .6; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
    height: 64px; background: #fff; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; padding: 0 26px;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
.topbar .user-chip { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-muted); }
.topbar .user-chip .avatar {
    width: 32px; height: 32px; border-radius: 50%; background: var(--orange);
    color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px;
}
.content { padding: 26px; flex: 1; }

.app-footer {
    padding: 20px 26px; display: flex; justify-content: center; align-items: center;
    border-top: 1px solid var(--border);
}
.app-footer img { height: 26px; width: auto; opacity: .85; }
@media (max-width: 900px) { .app-footer { padding: 16px; } .app-footer img { height: 22px; } }

/* Cards */
.grid { display: grid; gap: 18px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-split { grid-template-columns: 1fr 340px; }
.grid-split-left { grid-template-columns: 340px 1fr; }
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 900px) {
    .grid-4, .grid-2, .grid-split, .grid-split-left { grid-template-columns: 1fr !important; }
}

/* Mobile nav */
.menu-toggle {
    display: none; background: none; border: none; font-size: 22px; color: var(--text);
    cursor: pointer; padding: 4px 6px; border-radius: 8px;
}
.sidebar-close { display: none; }
.sidebar-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,10,50,.5); z-index: 40;
}
@media (max-width: 900px) {
    .menu-toggle { display: inline-flex; align-items: center; }
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; z-index: 50;
        transform: translateX(-100%); transition: transform .25s ease;
        width: 270px; overflow-y: auto;
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay.show { display: block; }
    .sidebar-close {
        display: inline-flex; align-items: center; justify-content: center;
        margin-left: auto; background: rgba(255,255,255,.08); border: none; color: #fff;
        width: 30px; height: 30px; border-radius: 8px; cursor: pointer; font-size: 14px;
    }
}

.card {
    background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 20px; border: 1px solid var(--border);
}
.card-lg { padding: 22px; }
.card h3 { margin: 0 0 14px 0; font-size: 14px; color: var(--text-muted); font-weight: 600; }

.stat-card { display: flex; align-items: center; gap: 14px; }
.stat-card .icon {
    width: 46px; height: 46px; border-radius: 12px; display: flex; align-items: center; justify-content: center;
    font-size: 20px; color: #fff; flex-shrink: 0;
}
.stat-card .value { font-size: 26px; font-weight: 700; line-height: 1.1; }
.stat-card .label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* Dashboard multi-sensor: grupos por ubicacion + tarjetas */
.location-groups-wrap { display: flex; flex-wrap: wrap; gap: 28px; }
.location-group { margin-bottom: 0; flex: 1 1 320px; min-width: 300px; }
.location-title {
    font-size: 15px; font-weight: 700; color: var(--text); margin: 0 0 12px;
    display: flex; align-items: center; gap: 8px;
}
.location-title i { color: var(--blue); }
.location-count {
    background: #eef0f6; color: var(--text-muted); font-size: 11px; font-weight: 700;
    padding: 2px 9px; border-radius: 20px; margin-left: 2px;
}

.sensor-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 18px;
}
.sensor-card {
    background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow);
    border: 1px solid var(--border); padding: 24px; display: block;
    transition: transform .15s, box-shadow .15s, border-color .15s;
    cursor: pointer;
}
.sensor-card:hover {
    transform: translateY(-4px); box-shadow: var(--shadow-lg); border-color: var(--blue);
}
.sensor-card:hover .sensor-card-cta { color: var(--blue); gap: 8px; }
.sensor-card-offline { opacity: .7; border-style: dashed; }
.sensor-card-alert { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger); }

.sensor-card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.sensor-card-name { font-weight: 700; font-size: 17px; color: var(--text); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.dot-success { background: var(--success); box-shadow: 0 0 0 4px rgba(34,163,90,.15); }
.dot-danger { background: var(--danger); box-shadow: 0 0 0 4px rgba(224,69,63,.15); }
.dot-muted { background: #c7cbdb; }

.sensor-card-body { display: flex; flex-direction: column; gap: 12px; }
.sensor-card-metrics { display: flex; gap: 20px; flex-wrap: wrap; }
.sensor-card-metric { display: flex; align-items: center; gap: 8px; font-size: 22px; font-weight: 700; color: var(--text); }
.sensor-card-metric i { color: var(--text-muted); font-size: 16px; width: 18px; }
.sensor-card-chart { height: 64px; margin-top: 4px; }
.sensor-card-foot {
    margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border);
    font-size: 12px; color: var(--text-muted); display: flex; justify-content: space-between; align-items: center;
}
.sensor-card-cta {
    display: inline-flex; align-items: center; gap: 4px; font-weight: 700; color: var(--text-muted);
    transition: gap .15s, color .15s;
}

@media (max-width: 700px) {
    .sensor-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
    .sensor-card { padding: 18px; }
    .sensor-card-chart { height: 48px; }
    .sensor-card-metric { font-size: 18px; }
}
.bg-blue { background: var(--blue); }
.bg-orange { background: var(--orange); }
.bg-indigo { background: var(--indigo); }
.bg-navy { background: var(--navy); }
.bg-success { background: var(--success); }
.bg-danger { background: var(--danger); }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge-success { background: #e5f7ec; color: var(--success); }
.badge-danger { background: #fdeceb; color: var(--danger); }
.badge-muted { background: #eef0f6; color: var(--text-muted); }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text); }
.form-control {
    width: 100%; padding: 10px 12px; border-radius: 9px; border: 1px solid var(--border);
    font-size: 14px; background: #fbfcfe;
}
.form-control:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(0,159,227,.15); }
.form-row { display: flex; gap: 14px; }
.form-row > * { flex: 1; }

.date-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.date-row .form-group { margin-bottom: 0; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

/* Filas de sensor (Administración -> Sensores) */
.sensor-row-form {
    display: grid; grid-template-columns: 1.3fr 1.1fr 2fr 0.8fr auto;
    gap: 20px; align-items: end; width: 100%;
}
.sensor-row-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.sensor-row-field label { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); }
.sensor-row-field .form-control { width: 100%; }
.sensor-row-id {
    font-family: monospace; font-size: 13px; color: var(--text); background: #f4f6fb;
    padding: 10px 12px; border-radius: 9px; margin: 0; word-break: break-all;
}
.sensor-row-actions { display: flex; gap: 10px; flex-wrap: wrap; }

@media (max-width: 1000px) {
    .sensor-row-form { grid-template-columns: 1fr 1fr; }
    .sensor-row-field-grow { grid-column: 1 / -1; }
}

@media (max-width: 700px) {
    .sensor-row-form { grid-template-columns: 1fr; }
    .sensor-row-field-grow { grid-column: auto; }
}
.btn-row .btn { flex: 1; justify-content: center; min-width: 140px; }

/* Filas de intervalo (Administración -> Frecuencia de Lectura) */
.interval-row-form {
    display: flex; align-items: center; gap: 20px; flex-wrap: wrap; width: 100%;
}
.interval-row-info { display: flex; flex-direction: column; gap: 4px; min-width: 160px; flex: 1 1 200px; }
.interval-row-name { font-size: 15px; font-weight: 700; color: var(--text); }
.interval-row-location { font-size: 12.5px; color: var(--text-muted); display: flex; align-items: center; gap: 5px; }
.interval-presets { display: flex; gap: 6px; flex-wrap: wrap; }
.interval-chip {
    border: 1px solid var(--border); background: #fff; color: var(--text);
    font-size: 12.5px; font-weight: 600; padding: 7px 12px; border-radius: 999px;
    cursor: pointer; transition: all .15s ease;
}
.interval-chip:hover { border-color: var(--blue); color: var(--blue); }
.interval-chip.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.interval-custom { display: flex; align-items: center; gap: 8px; }
.interval-custom .form-control { text-align: center; }

@media (max-width: 700px) {
    .interval-row-form { flex-direction: column; align-items: stretch; }
    .interval-row-actions, .interval-custom { justify-content: space-between; }
}

.btn {
    display: inline-flex; align-items: center; gap: 8px; padding: 10px 18px; border-radius: 9px;
    border: none; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: #0089c4; }
.btn-secondary { background: #eef0f6; color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-sm { padding: 6px 12px; font-size: 12.5px; }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th { text-align: left; padding: 10px 12px; color: var(--text-muted); font-weight: 600; border-bottom: 1px solid var(--border); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
td { padding: 11px 12px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

.out-of-range { color: var(--danger); font-weight: 700; }

.alert-banner { padding: 12px 16px; border-radius: 10px; margin-bottom: 18px; font-size: 14px; display: flex; gap: 10px; align-items: center; }
.alert-banner.error { background: #fdeceb; color: #b5312c; }
.alert-banner.ok { background: #e5f7ec; color: #1a7a44; }

/* Login page */
.login-wrap {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--navy) 0%, var(--indigo) 100%);
}
.login-card {
    background: #fff; border-radius: 18px; padding: 40px 36px; width: 380px;
    box-shadow: var(--shadow-lg);
}
.login-card img { width: 56px; display: block; margin: 0 auto 14px; }
.login-card h2 { text-align: center; margin: 0 0 4px; font-size: 19px; }
.login-card p.subtitle { text-align: center; color: var(--text-muted); font-size: 13px; margin: 0 0 26px; }
.login-card .btn { width: 100%; justify-content: center; margin-top: 6px; }
.login-footer { text-align: center; font-size: 12px; color: var(--text-muted); margin: 22px 0 0; }

.chart-box { position: relative; height: 300px; }
.chart-box.sm { height: 220px; }

.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.page-header h2 { margin: 0; font-size: 20px; }
.page-header p { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tab { padding: 10px 16px; font-size: 13.5px; font-weight: 600; color: var(--text-muted); border-bottom: 2px solid transparent; white-space: nowrap; flex-shrink: 0; }
.tab.active { color: var(--blue); border-color: var(--blue); }
.tab:hover { color: var(--text); }

/* Mobile fine-tuning */
@media (max-width: 900px) {
    .content { padding: 16px; }
    .topbar { height: auto; min-height: 58px; padding: 10px 14px; flex-wrap: nowrap; gap: 8px; }
    .topbar > div:first-child { min-width: 0; flex-shrink: 1; }
    .topbar h1 { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .topbar .user-chip { font-size: 12px; gap: 6px; flex-shrink: 0; }
    .topbar .user-chip .avatar { width: 30px; height: 30px; font-size: 13px; }
    .topbar .user-chip > div:last-child { display: none; }

    .grid { gap: 12px; }
    .card { padding: 16px; }

    .form-row { flex-direction: column; gap: 0; }

    .page-header { flex-direction: column; align-items: flex-start; gap: 10px; }
    .page-header .btn, .page-header a.btn { width: 100%; justify-content: center; }

    .card form[method="get"].form-row,
    form.form-row {
        flex-direction: column;
    }
    .card form .btn:not(.btn-sm), .card form a.btn:not(.btn-sm) { width: 100%; justify-content: center; margin-top: 8px; }

    .chart-box { height: 240px; }
    .chart-box.sm { height: 200px; }

    table { font-size: 12.5px; }
    th, td { padding: 8px 10px; }
    .table-wrap table { min-width: 480px; }
    th, td { white-space: nowrap; }

    .login-card { width: 92vw; padding: 32px 24px; }
}

/* Modal (native <dialog>) */
dialog.modal {
    border: none;
    border-radius: var(--radius);
    padding: 0;
    width: min(440px, calc(100vw - 32px));
    background: var(--card-bg);
    color: var(--text);
    box-shadow: var(--shadow-lg);
}
dialog.modal::backdrop { background: rgba(0, 10, 50, 0.45); }
dialog.modal[open] { animation: modal-in .14s ease-out; }
@keyframes modal-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.modal-head {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-close {
    background: none; border: none; font-size: 15px; line-height: 1;
    color: var(--text-muted); cursor: pointer; padding: 4px;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 20px; }
.modal-section { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }
.modal-section:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.modal-section .form-group:last-of-type { margin-bottom: 14px; }

/* Grupo de navegación plegable (sidebar) */
.nav-group-toggle {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    width: 100%; background: none; border: none; cursor: pointer; font: inherit;
    text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
    color: rgba(255,255,255,.85); opacity: .5; padding: 6px 12px;
}
.nav-group-toggle:hover { opacity: .85; }
.nav-group-toggle.open { opacity: .7; }
.nav-group-toggle i { font-size: 12px; transition: transform .15s ease; }
.nav-group-toggle.open i { transform: rotate(90deg); }
.nav-group-items[hidden] { display: none; }
