/* --- Variables de Tema (Claro por defecto) --- */
:root {
    --primary-color: #830202;
    --text-color: #333;
    --light-text-color: #f0f0f0;
    --bg-gradient: linear-gradient(to top, #eacda3 0%, #d6ae7b 100%);
    --card-bg: rgba(255, 255, 255, 0.25);
    --card-border: rgba(255, 255, 255, 0.4);
    --shadow: 0 8px 32px 0 rgba(100, 31, 31, 0.2);
    --border-radius: 16px;
    --consumo-alto-color: #d90429;
    --table-row-bg-even: rgba(0,0,0,0.03);
}

/* --- Variables para Modo Oscuro --- */
body.dark-mode {
    --primary-color: #ff8a8a;
    --text-color: #e0e0e0;
    --bg-gradient: linear-gradient(to top, #232526 0%, #414345 100%);
    --card-bg: rgba(40, 40, 40, 0.4);
    --card-border: rgba(255, 255, 255, 0.1);
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
    --consumo-alto-color: #ffb4a2;
    --table-row-bg-even: rgba(255,255,255,0.05);
}

/* === PREVENCIÓN GLOBAL DE DESBORDAMIENTO === */
* {
    box-sizing: border-box;
}

html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 1rem;
    background: var(--bg-gradient);
    color: var(--text-color);
    transition: background 0.3s, color 0.3s;
    min-height: 100vh;
    overflow-x: hidden;
    max-width: 100vw;
}

h1, h2, h3 {
    color: var(--primary-color);
    font-weight: 600;
}

/* --- Cabecera y Logos --- */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logo { height: 50px; }
.logo-mobile { display: none; }

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card-bg);
    padding: 8px 16px;
    border-radius: 20px;
    backdrop-filter: blur(10px);
    border: 1px solid var(--card-border);
}

.user-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-color);
}

.logout-btn {
    background: none;
    border: none;
    font-size: 16px;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: background-color 0.3s;
}

.logout-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}
.header h1 {
    margin: 0;
    font-size: 1.8rem;
    text-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}

/* --- Interruptor Modo Oscuro --- */
.theme-switch-wrapper {
    display: flex;
    align-items: center;
    margin-left: auto;
}
.theme-switch {
    position: relative; display: inline-block; width: 50px; height: 24px;
}
.theme-switch input { opacity: 0; width: 0; height: 0; }
.slider {
    position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
    background-color: #ccc; transition: .4s; border-radius: 24px;
}
.slider:before {
    position: absolute; content: ""; height: 16px; width: 16px; left: 4px;
    bottom: 4px; background-color: white; transition: .4s; border-radius: 50%;
}
input:checked + .slider { background-color: var(--primary-color); }
input:checked + .slider:before { transform: translateX(26px); }

/* --- Contenedor y Tarjetas --- */
.container {
    max-width: 100%;
    width: 100%;
    margin: 0 auto;
    padding: 0 15px;
    box-sizing: border-box;
    overflow-x: hidden;
}
.card {
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--card-border);
    border-radius: var(--border-radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    color: var(--text-color);
    max-width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    box-sizing: border-box;
}
.card h3 {
    margin-top: 0;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 1.5rem;
    margin-top: 1.5rem;
    max-width: 100%;
    overflow-x: hidden;
    box-sizing: border-box;
}

/* --- Sección de Búsqueda --- */
.search-card { margin-bottom: 1.5rem; }
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}
.form-group label {
    font-weight: 600;
    margin-bottom: 0.3rem;
    display: block;
}
.form-group input {
    width: 100%;
    padding: 0.6rem;
    border-radius: 8px;
    border: 1px solid #ccc;
    box-sizing: border-box;
    background: var(--card-bg);
    color: var(--text-color);
}
.btn-buscar {
    padding: 0.8rem 1.5rem;
    border: none;
    border-radius: 8px;
    background-color: var(--primary-color);
    color: white;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.3s, transform 0.2s;
    float: right;
}
.btn-buscar:hover {
    background-color: #a50303;
    transform: translateY(-2px);
}
.search-results ul { list-style: none; padding: 0; margin-top: 1rem; }
.select-result {
  color: #e0e0e0;
  width: 100%; text-align: left; padding: 0.5rem; background: none;
  border: none; cursor: pointer; border-bottom: 1px solid #ddd;
}
.select-result:hover { background-color: rgba(0,0,0,0.05); }

/* --- Botones --- */
#export-buttons { margin-top: 1rem; }
.button, .btn-history {
    padding: 0.6rem 1rem;
    margin: 0.2rem;
    border: none;
    border-radius: 8px;
    background-color: #6d1600;
    color: white;
    cursor: pointer;
    transition: background-color 0.3s;
}
.button:hover, .btn-history:hover { background-color: #9c2000; }
.btn-history { background-color: #f0f0f0; color: #333; }

/* --- Tablas y Búsqueda en Tablas --- */
.table-search { margin-bottom: 1rem; }
.table-search input { width: 100%; padding: 0.6rem; border-radius: 8px; border: 1px solid #ccc; box-sizing: border-box; background-color: var(--card-bg); color: var(--text-color); }
.data-table { 
    width: 100%; 
    border-collapse: collapse; 
    font-size: 0.9rem; 
    max-width: 100%;
    table-layout: fixed;
    overflow: hidden;
}
.data-table th, .data-table td { 
    padding: 0.7rem; 
    text-align: left; 
    border-bottom: 1px solid var(--card-border); 
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.data-table thead th { font-weight: bold; color: var(--primary-color); }
.data-table tbody tr:nth-child(even) { background-color: var(--table-row-bg-even); }
.data-table tfoot { font-weight: bold; color: var(--primary-color); }
.totaladeudos { text-align: right; }
.sinadeudo { color: green; font-weight: bold; text-align: center; }

/*Estado de Corte*/
.sincorte {
  color: green;
  font-weight: bolder;
}

.edocorte {
  color: var(--consumo-alto-color);
  font-weight: bolder;
  animation: blinker 2s linear infinite;
}

@keyframes blinker {
  50% {
    opacity: 0;
  }
}

/* --- Clases de Estado y Alertas --- */
.consumo-alto { color: var(--consumo-alto-color) !important; font-weight: bold; }
.ot-completada { background-color: rgba(40, 167, 69, 0.15) !important; }
.ot-programada { background-color: rgba(255, 193, 7, 0.15) !important; }
.ot-anulada { background-color: rgba(108, 117, 125, 0.15) !important; }

/* --- Paginación --- */
.pagination { text-align: center; margin-top: 1rem; }
.pagination button { padding: 0.5rem 1rem; margin: 0 0.3rem; border: 1px solid #ccc; border-radius: 6px; background-color: #fff; cursor: pointer; }
.pagination button:disabled { cursor: not-allowed; opacity: 0.5; }
.pagination span { margin: 0 1rem; font-weight: bold; }

/* --- Elementos Específicos --- */
#map { 
    width: 100%; 
    height: 300px; 
    border-radius: var(--border-radius); 
    border: 1px solid #ccc; 
    max-width: 100%;
    overflow: hidden;
}
canvas { 
    max-height: unset !important;
    margin-bottom: 1rem; 
    max-width: 100%;
}

/* --- Diseño Responsivo --- */
@media (max-width: 1024px) { 
    .logo-desktop { display: none; } 
    .logo-mobile { display: block; } 
    .dashboard-grid { grid-template-columns: 1fr 1fr; } 
    #map { height: 250px; }
}

@media (max-width: 768px) { 
    body { padding: 0.5rem; } 
    .header { flex-direction: column; align-items: center; text-align: center; gap: 0.5rem; } 
    .theme-switch-wrapper { margin: 0.5rem 0 0 0; } 
    .dashboard-grid { grid-template-columns: 1fr; } 
    .form-grid { grid-template-columns: 1fr 1fr; } 
    #map { height: 200px; }
    .data-table { font-size: 0.8rem; }
    .data-table th, .data-table td { padding: 0.5rem; }
}

@media (max-width: 480px) {
    .form-grid { grid-template-columns: 1fr; }
    #map { height: 180px; }
    .data-table, .data-table thead, .data-table tbody, .data-table th, .data-table td, .data-table tr { display: block; }
    .data-table thead { display: none; }
    .data-table tr { border: 1px solid #ccc; border-radius: 8px; margin-bottom: 0.5rem; padding: 0.5rem; }
    .data-table td { display: flex; justify-content: space-between; padding: 0.5rem; border: none; text-align: right; }
    .data-table td::before { content: attr(data-label); font-weight: bold; text-align: left; color: var(--primary-color); }
}

@media (min-width: 1201px) {
    canvas {
        max-height: 240px !important;
    }
}