:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --text: #1d2420;
  --muted: #5e6862;
  --border: #dcd9d0;
  --accent: #1f7a5c;
  --accent-soft: #e0f0e9;
  --accent-text: #ffffff;
  --warn: #9a5b00;
  --warn-soft: #fbefd9;
  --danger: #a33a2f;
  --danger-soft: #f8e3e0;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 4%);
  /* Marcas de los gráficos: validado con el validador de paletas (claro, superficie #fff). */
  --chart: #11875d;
  --chart-hover: #0b6c4a;
  --grid: #ecebe6;
  color-scheme: light;
}

/* Oscuro: si el sistema lo pide y no has elegido «Claro», o si has elegido «Oscuro» (Ajustes o botón ☀️/🌙). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141816;
    --surface: #1c211e;
    --surface-2: #252b27;
    --text: #e6ebe7;
    --muted: #9aa49e;
    --border: #333b36;
    --accent: #4cbf93;
    --accent-soft: #1e3a2f;
    --accent-text: #0d1a14;
    --warn: #e8b25a;
    --warn-soft: #3a2f1c;
    --danger: #f08b7e;
    --danger-soft: #3d2420;
    --shadow: none;
    /* Validado contra la superficie oscura #1c211e. */
    --chart: #3aa77e;
    --chart-hover: #52bf95;
    --grid: #2a312d;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #141816;
  --surface: #1c211e;
  --surface-2: #252b27;
  --text: #e6ebe7;
  --muted: #9aa49e;
  --border: #333b36;
  --accent: #4cbf93;
  --accent-soft: #1e3a2f;
  --accent-text: #0d1a14;
  --warn: #e8b25a;
  --warn-soft: #3a2f1c;
  --danger: #f08b7e;
  --danger-soft: #3d2420;
  --shadow: none;
  /* Validado contra la superficie oscura #1c211e. */
  --chart: #3aa77e;
  --chart-hover: #52bf95;
  --grid: #2a312d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1rem; margin-top: 1.25rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.5em; }
.small { font-size: .85rem; }

button, .button, input, textarea, select { font: inherit; color: inherit; }
button, .button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: .45rem .85rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
}
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: progress; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--danger); }
.ghost { background: none; border: none; color: var(--muted); padding: .4rem .6rem; text-decoration: none; }
.ghost:hover { color: var(--text); }
.icon-button { border: none; background: none; font-size: 1.3rem; padding: .2rem .5rem; }
input[type="text"], input[type="password"], input[type="search"], textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: .55rem .7rem;
}
textarea { resize: vertical; min-height: 4.5rem; }
select {
  max-width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: .5rem .6rem;
}
.field { display: grid; gap: .35rem; justify-items: start; }
input:focus, textarea:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: flex; align-items: center; gap: .45rem; color: var(--muted); font-size: .9rem; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
  box-shadow: var(--shadow);
  display: grid;
  gap: .6rem;
}

/* Estructura */
.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: auto 1fr;
  height: 100%;
}
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
/* En pantallas estrechas, el nombre corto para que la barra no ocupe tres filas */
@media (max-width: 480px) { .brand-extra { display: none; } }
.top-actions { margin-left: auto; display: flex; gap: .25rem; }
.pill {
  font-size: .8rem;
  padding: .15rem .6rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.pill.ok { background: var(--accent-soft); color: var(--accent); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: .75rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}
.chat-list { display: flex; flex-direction: column; gap: .15rem; }
.group-heading {
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  margin: .8rem .4rem .2rem;
}
.chat-item {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .45rem .5rem;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item.unmonitored .name { color: var(--muted); }
.badge {
  font-size: .75rem;
  min-width: 1.6rem;
  text-align: center;
  padding: 0 .4rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}
.dot { width: .5rem; height: .5rem; border-radius: 50%; background: var(--border); flex: none; }
.dot.on { background: var(--accent); }

.main { overflow-y: auto; padding: 1.5rem; }
.main > * { max-width: 920px; }

/* Bloques */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.row.spread { justify-content: space-between; }
.stack { display: grid; gap: .75rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .75rem; margin: 1rem 0; }
.stat { background: var(--surface-2); border-radius: 8px; padding: .6rem .8rem; }
.stat b { display: block; font-size: 1.4rem; }
.stat span { color: var(--muted); font-size: .85rem; }

.tabs { display: flex; flex-wrap: wrap; gap: .25rem; border-bottom: 1px solid var(--border); margin: 1rem 0; }
.tabs button {
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  color: var(--muted);
  padding: .5rem .8rem;
  white-space: nowrap;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.list { margin: .25rem 0 0; padding-left: 1.2rem; }
.list li { margin: .3rem 0; }
.tag {
  display: inline-block;
  font-size: .75rem;
  padding: .05rem .5rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  margin-right: .35rem;
}
.tag.alta, .tag.conflicto, .tag.queja, .tag.spam, .tag.datos_sensibles { background: var(--danger-soft); color: var(--danger); }
.tag.media, .tag.normas, .tag.baja_actividad, .tag.privado { background: var(--warn-soft); color: var(--warn); }
.tag.respuesta, .tag.dinamizacion, .tag.anuncio { background: var(--accent-soft); color: var(--accent); }

.quote {
  border-left: 3px solid var(--border);
  padding: .1rem .7rem;
  margin: .4rem 0;
  color: var(--muted);
  font-size: .9rem;
}
.suggestion textarea { min-height: 6rem; margin: .5rem 0; }

.messages { display: flex; flex-direction: column; gap: .5rem; }
.msg { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .45rem .7rem; max-width: 85%; }
.msg.mine { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.msg .meta { font-size: .78rem; color: var(--muted); }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }

pre.digest {
  white-space: pre-wrap;
  font: inherit;
  margin: 0;
  overflow-wrap: anywhere;
}
.qr { display: grid; justify-items: center; gap: .75rem; text-align: center; }
.qr img { width: 280px; max-width: 100%; background: #fff; padding: 8px; border-radius: 8px; }
.empty { color: var(--muted); padding: 1rem 0; }
.spinner {
  width: 1rem; height: 1rem;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: .6rem 1rem;
  border-radius: 8px;
  max-width: calc(100% - 32px);
  z-index: 10;
}

.only-mobile { display: none; }

/* Comunidades en la barra lateral */
.community {
  display: flex;
  align-items: center;
  gap: .25rem;
  padding: .3rem .4rem .3rem .1rem;
  border-radius: 8px;
}
.community:hover, .community.active { background: var(--surface-2); }
.community .caret {
  border: none;
  background: none;
  padding: .1rem .35rem;
  color: var(--muted);
  font-size: .85rem;
}
.community-name {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.badge.on { background: var(--accent-soft); color: var(--accent); }
.community-groups { margin: 0 0 .35rem 1.1rem; padding-left: .35rem; border-left: 1px solid var(--border); }

/* Cabeceras y avisos */
.eyebrow { margin: 0 0 .2rem; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.description { white-space: pre-wrap; max-height: 9em; overflow-y: auto; }
.notice { background: var(--warn-soft); border-color: transparent; color: var(--text); }
.issues { margin: .35rem 0; font-size: .9rem; }
.stat small { display: block; color: var(--muted); font-size: .78rem; margin-top: .15rem; }

/* Portada: tarjetas de comunidades y grupos */
.ov-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.ov-card { display: flex; flex-direction: column; gap: .2rem; color: var(--text); text-decoration: none; margin: 0; }
.ov-card:hover { border-color: var(--accent); }
.ov-card .issues { margin: .2rem 0 0; }

/* Gráficos de columnas */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }
.chart-grid > .card { margin-bottom: 1rem; }
figure.chart { margin: 0 0 1rem; }
.chart figcaption { display: flex; flex-direction: column; margin-bottom: .75rem; }
.cc-plot { position: relative; height: 150px; margin-left: 2rem; border-bottom: 1px solid var(--border); }
.cc-grid { position: absolute; left: 0; right: 0; border-top: 1px solid var(--grid); }
.cc-tick {
  position: absolute;
  right: calc(100% + .4rem);
  top: -.6em;
  font-size: .72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.cc-cols { position: absolute; inset: 0; display: flex; align-items: flex-end; gap: 2px; }
.cc-col {
  position: relative;
  flex: 1;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  outline-offset: 0;
}
.cc-bar { width: 100%; max-width: 24px; min-height: 0; background: var(--chart); border-radius: 4px 4px 0 0; transition: background .1s; }
.cc-col:hover .cc-bar, .cc-col:focus .cc-bar { background: var(--chart-hover); }
.cc-peak { position: absolute; transform: translateY(-.2rem); font-size: .75rem; color: var(--text); font-weight: 600; }
.cc-x { display: flex; gap: 2px; margin: .3rem 0 .5rem 2rem; }
.cc-x span { flex: 1; text-align: center; font-size: .72rem; color: var(--muted); white-space: nowrap; overflow: visible; }
.chart details summary { cursor: pointer; }
.data-table { border-collapse: collapse; margin-top: .4rem; font-variant-numeric: tabular-nums; }
.data-table th, .data-table td { text-align: left; padding: .15rem .8rem .15rem 0; border-bottom: 1px solid var(--grid); }
.chart-tip {
  position: fixed;
  z-index: 20;
  pointer-events: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .35rem .6rem;
  box-shadow: 0 4px 16px rgb(0 0 0 / 15%);
  display: flex;
  flex-direction: column;
  font-size: .85rem;
}
.chart-tip span { color: var(--muted); font-size: .78rem; }

/* Personas y datos del grupo */
.people { margin: 0; padding-left: 1.3rem; }
.people li { margin: .25rem 0; }
.people li span:first-child { margin-right: .4rem; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: .5rem 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }

/* Tabla de grupos de una comunidad */
.group-table { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.group-row {
  display: grid;
  grid-template-columns: 2rem minmax(0, 2.2fr) minmax(140px, 1.4fr) 5.5rem 8.5rem;
  gap: .75rem;
  align-items: center;
  padding: .6rem .8rem;
  border-bottom: 1px solid var(--grid);
}
.group-row:last-child { border-bottom: none; }
.group-head { font-size: .75rem; color: var(--muted); padding-top: .45rem; padding-bottom: .45rem; }
.group-row.unmonitored .gr-name a { color: var(--muted); }
.gr-name { display: flex; flex-direction: column; min-width: 0; }
.gr-name a { font-weight: 600; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gr-label { display: none; }
.gr-metric { font-size: .9rem; font-variant-numeric: tabular-nums; }
.inline-bar { display: flex; align-items: center; gap: .5rem; }
.ib-track { flex: 1; height: 8px; background: var(--grid); border-radius: 4px; overflow: hidden; }
.ib-fill { display: block; height: 100%; background: var(--chart); border-radius: 0 4px 4px 0; }
.ib-value { min-width: 2.5rem; text-align: right; }

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-flex; }
  .sidebar {
    position: fixed;
    inset: 49px 0 0 0;
    z-index: 5;
    border-right: none;
    transform: translateX(-100%);
    transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .main { padding: 1rem 16px; }
  .top-actions .ghost { padding: .4rem .35rem; font-size: .9rem; }
  .topbar { gap: .4rem; padding: .5rem .5rem; flex-wrap: wrap; }
  .msg { max-width: 100%; }
  .group-row { grid-template-columns: 2rem repeat(3, minmax(0, 1fr)); row-gap: .35rem; }
  .group-row .gr-follow { grid-row: 1 / span 2; }
  .group-row .gr-name { grid-column: 2 / -1; }
  .gr-metric { font-size: .85rem; }
  .gr-label { display: block; font-size: .72rem; color: var(--muted); }
  .group-head { display: none; }
  .facts { grid-template-columns: 1fr; gap: 0; }
  .facts dd { margin-bottom: .4rem; }
}

/* Opciones de Ajustes (motor de IA, tema) */
.ai-options { display: grid; gap: .35rem; }
.ai-option {
  display: flex;
  align-items: flex-start;
  gap: .55rem;
  padding: .5rem .65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.ai-option:hover { border-color: var(--accent); }
.ai-option:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.ai-option input { margin-top: .3rem; }
.ai-option .tag { margin-left: .4rem; }
.ai-options + .stack input[type="text"] { flex: 1; min-width: 12rem; }
#theme-toggle { font-size: 1.05rem; }

/* Páginas web conectadas */
.web-url { display: block; overflow-wrap: anywhere; color: var(--muted); }
.web-text { max-height: 18rem; overflow-y: auto; background: var(--surface-2); padding: .6rem .75rem; border-radius: 8px; margin-top: .5rem; }
