:root{
  --bg:#0f1720;
  --card:#0b1220;
  --muted:#94a3b8;
  --accent:#58a6ff;
  --accent-soft:rgba(88,166,255,0.16);
  --success:#3fb950;
  --danger:#e63946;
  --text:#e6eef8;
  --ghost:#24303a;
  --radius:10px;
  --border-subtle:rgba(148,163,184,0.35);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, Arial;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }

body{
  margin:0;
  background:linear-gradient(180deg,#071023 0%, #0f1720 100%);
  color:var(--text);
  -webkit-font-smoothing:antialiased;
  -moz-osx-font-smoothing:grayscale;
  padding:20px;
}

/* Layout base */
.wrap{
  max-width:1200px;
  margin:0 auto;
  display:flex;
  flex-direction:column;
  gap:16px;
}

header{
  display:flex;
  align-items:flex-start;
}

.title-block h1{
  margin:0;
  font-size:20px;
}
.subtitle{
  margin:4px 0 0;
  color:var(--muted);
  font-size:13px;
}

.tag-row{
  display:flex;
  flex-wrap:wrap;
  gap:8px;
  margin-top:8px;
}
.tag{
  font-size:11px;
  padding:4px 9px;
  border-radius:999px;
  border:1px solid rgba(148,163,184,0.45);
  color:var(--muted);
}

/* Cards */
.card{
  padding:12px 14px;
  border-radius:var(--radius);
  background:linear-gradient(180deg,rgba(15,23,42,0.96),rgba(15,23,42,0.88));
  box-shadow:0 10px 30px rgba(15,23,42,0.7);
  border:1px solid rgba(15,23,42,0.9);
}

.controls-card{
  padding-bottom:10px;
}

.card-header{
  display:flex;
  justify-content:space-between;
  align-items:flex-start;
  gap:8px;
  margin-bottom:8px;
}

.card-header h2{
  margin:0;
  font-size:15px;
}

/* Textos pequeños */
.small{ font-size:12px; }
.muted{ color:var(--muted); }

/* Barra de controles */
.controls-bar{
  display:flex;
  flex-wrap:wrap;
  gap:10px;
  align-items:flex-end;
}

.field{
  display:flex;
  flex-direction:column;
  gap:4px;
  font-size:12px;
  color:var(--muted);
  min-width:150px;
}
.field-grow{
  flex:1;
}

.controls-spacer{
  flex:1;
}

input, select{
  background:rgba(15,23,42,0.9);
  border:1px solid var(--border-subtle);
  color:var(--text);
  border-radius:8px;
  padding:6px 9px;
  font-size:13px;
}

input::placeholder{
  color:rgba(148,163,184,0.75);
}

input:focus, select:focus{
  outline:none;
  border-color:var(--accent);
  box-shadow:0 0 0 1px rgba(88,166,255,0.6);
}

/* Botones */
.btn{
  background:var(--ghost);
  color:var(--text);
  border:none;
  padding:7px 11px;
  border-radius:8px;
  cursor:pointer;
  font-size:13px;
  display:inline-flex;
  align-items:center;
  gap:6px;
}
.btn.ghost{
  background:transparent;
  border:1px solid rgba(148,163,184,0.45);
}
.btn.primary{
  background:var(--accent);
  color:#04101f;
  font-weight:600;
}
.btn:active{
  transform:translateY(1px);
}
.small-btn{
  font-size:12px;
  padding:5px 8px;
}

/* Botón DHONT más destacado */
.dhont-btn{
  text-transform:uppercase;
  letter-spacing:.08em;
  font-weight:700;
  padding:9px 18px;
  border-radius:999px;
  background:linear-gradient(90deg,#f97316,#fb923c);
  color:#04101f;
  box-shadow:
    0 0 0 1px rgba(248,171,77,0.8),
    0 10px 26px rgba(15,23,42,0.9);
}
.dhont-btn:hover{
  transform:translateY(-1px);
  box-shadow:
    0 0 0 1px rgba(248,171,77,1),
    0 14px 30px rgba(15,23,42,1);
}

/* Layout principal */
.layout{
  display:grid;
  grid-template-columns:minmax(0,2fr) minmax(260px,1fr);
  gap:16px;
  align-items:flex-start;
}
.main-card{ min-width:0; }
.side-card{ min-width:0; }

@media (max-width:960px){
  .layout{
    grid-template-columns:1fr;
  }
}

/* Tabla principal: wrapper */
.table-wrapper{
  margin-top:6px;
  border-radius:var(--radius);
  overflow:hidden;
  border:1px solid rgba(37,99,235,0.45);
  background:radial-gradient(circle at top left,rgba(56,189,248,0.15),rgba(15,23,42,0.96));
}

.table-scroll{
  overflow:auto;
  max-height:65vh;
}

/* Reglas globales de tabla (sin min-width) */
table{
  width:100%;
  border-collapse:collapse;
  font-size:13px;
}

/* La tabla principal sí tiene min-width */
#resultsTable{
  min-width:680px;
}

thead{
  background:rgba(15,23,42,0.98);
}
th, td{
  padding:7px 9px;
  border-bottom:1px solid rgba(15,23,42,0.9);
  text-align:left;
}
th{
  font-size:11px;
  text-transform:uppercase;
  letter-spacing:.04em;
  color:var(--muted);
  position:sticky;
  top:0;
  z-index:2;
  background:rgba(15,23,42,0.98);
}

/* Encabezados ordenables */
th.sortable{
  cursor:pointer;
}
th.sortable::after{
  content:"";
  display:inline-block;
  margin-left:4px;
  font-size:9px;
  opacity:0.4;
}
th.sorted-asc::after{
  content:"▲";
}
th.sorted-desc::after{
  content:"▼";
}

tbody tr:nth-child(odd){
  background:rgba(15,23,42,0.45);
}
tbody tr:nth-child(even){
  background:rgba(15,23,42,0.35);
}
tbody tr:hover{
  background:rgba(56,189,248,0.18);
}

/* Fila ganadora / electos */
tr.winner{
  background:linear-gradient(90deg,rgba(63,185,80,0.25),rgba(15,23,42,0.6));
  box-shadow:inset 2px 0 0 var(--success);
}

/* Celdas específicas */
.num{
  text-align:right;
  font-variant-numeric:tabular-nums;
  white-space:nowrap;
}

.cand-name{
  font-weight:500;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  max-width:260px;
  cursor:pointer; /* indica que se puede clickear */
}
.cand-sub{
  font-size:11px;
  color:var(--muted);
}

/* Lista / pacto pill */
.list-pill{
  display:inline-flex;
  align-items:center;
  gap:4px;
  font-size:11px;
  padding:2px 7px;
  border-radius:999px;
  background:rgba(15,23,42,0.95);
  border:1px solid rgba(148,163,184,0.45);
}
.list-pill .code{
  font-weight:700;
  color:var(--accent);
}

/* Badge electo */
.elected-badge{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  width:22px;
  height:22px;
  border-radius:999px;
  background:rgba(63,185,80,0.12);
  border:1px solid rgba(63,185,80,0.9);
  font-size:14px;
}

/* Resumen lateral */
.summary-grid{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:10px;
  margin-top:6px;
}
.summary-item{
  border-radius:10px;
  padding:8px 10px;
  background:radial-gradient(circle at top left,rgba(148,163,184,0.14),rgba(15,23,42,0.9));
  border:1px solid rgba(148,163,184,0.4);
}
.summary-label{
  font-size:11px;
  text-transform:uppercase;
  letter-spacing:.03em;
  color:var(--muted);
}
.summary-value{
  font-size:15px;
  font-variant-numeric:tabular-nums;
}
.summary-extra{
  font-size:11px;
  color:var(--muted);
}

.side-note{
  margin-top:10px;
}

/* Footer tabla */
.footer{
  margin-top:8px;
  display:flex;
  justify-content:space-between;
  gap:8px;
  flex-wrap:wrap;
}

/* Overlay seguimiento candidato / head to head */
.overlay{
  position:fixed;
  inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  z-index:50;
}
.overlay.hidden{
  display:none;
}
.overlay-backdrop{
  position:absolute;
  inset:0;
  background:rgba(15,23,42,0.85);
  backdrop-filter:blur(2px);
}
.overlay-dialog{
  position:relative;
  width:min(560px, 94vw);
  max-height:80vh;
  overflow:hidden;
}

.overlay-header{
  display:flex;
  justify-content:space-between;
  align-items:center;
  margin-bottom:6px;
}
.overlay-header h3{
  margin:0;
  font-size:16px;
}
.overlay-body{
  font-size:13px;
}

/* Contenido tablar de overlays */
.small-table{
  margin-top:6px;
}
.small-table .table-scroll{
  max-height:50vh;
}

/* Controles Head to head */
.h2h-pickers{
  display:grid;
  grid-template-columns:1fr 1fr auto;
  gap:8px;
  align-items:flex-end;
  margin-bottom:8px;
}
.h2h-actions{
  display:flex;
  align-items:flex-end;
}
@media (max-width:520px){
  .h2h-pickers{
    grid-template-columns:1fr;
  }
}

/* Fila del Distrito 19 completo destacada */
.district-row{
  background:linear-gradient(90deg, rgba(250,204,21,0.16), rgba(250,204,21,0.03));
  font-weight:600;
  border-bottom:1px solid rgba(250,204,21,0.45);
}
.district-row td:first-child{
  position:relative;
}
.district-row td:first-child::before{
  content:"★";
  font-size:11px;
  margin-right:4px;
  opacity:0.8;
}

/* === Tablas compactas en overlays (seguimiento + head to head) === */

/* que la tabla no se estire a todo el ancho ni tenga min-width grande */
.overlay .small-table table{
  width:auto;
  min-width:0;
  table-layout:auto;
}

/* celdas más compactas */
.overlay .small-table table th,
.overlay .small-table table td{
  padding:4px 6px;
  font-size:13px;
}

/* columna 'Comuna' más angosta */
.overlay .small-table table th:first-child,
.overlay .small-table table td:first-child{
  max-width:110px;    /* antes 140 */
  white-space:nowrap;
}

/* columnas numéricas angostas */
.overlay .small-table table th.num,
.overlay .small-table table td.num{
  width:70px;
  max-width:70px;
  white-space:nowrap;
}

/* el contenedor se ajusta al ancho real de la tabla */
.overlay .small-table{
  display:inline-block;
}

/* En pantallas pequeñas, forzamos aún más la compacidad */
@media (max-width:720px){
  .overlay-dialog{
    width:96vw;             /* usa casi todo el ancho del móvil */
  }
  .overlay .small-table table{
    min-width:0 !important; /* por si algún min-width se cuela */
    width:auto !important;  /* evitamos la tabla gigante */
  }
}

@media (max-width:720px){
  body{
    padding:14px;
  }
  .table-scroll{
    max-height:60vh;
  }
  .summary-grid{
    grid-template-columns:1fr;
  }
}
/* Ajuste especial para SEGUIMIENTO / HEAD TO HEAD en pantallas pequeñas */
@media (max-width: 720px){

  /* El contenedor del overlay ocupa todo el ancho del móvil */
  .overlay-dialog{
    width: 96vw;
  }

  /* La tabla del overlay usa el ancho completo y reparte las columnas */
  .overlay .small-table{
    display:block;
    width:100%;
  }

  .overlay .small-table table{
    width:100% !important;
    min-width:0 !important;
    table-layout:fixed;       /* columnas con ancho fijo, sin scroll lateral grande */
  }

  /* Columna COMUNA: mitad de la tabla, puede hacer salto de línea */
  .overlay .small-table table th:first-child,
  .overlay .small-table table td:first-child{
    width:50%;
    max-width:none;
    white-space:normal;       /* permite partir el nombre de la comuna */
  }

  /* Columnas numéricas (VOTOS y %) reparten el resto */
  .overlay .small-table table th.num,
  .overlay .small-table table td.num{
    width:25%;
    max-width:none;
    white-space:nowrap;
  }
}
