:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #1c2733;
    --muted: #6b7a89;
    --line: #dde3e9;
    --accent: #0a6ebd;
    --accent-dark: #075a99;
    --ok: #1f9d55;
    --warn: #b9770c;
    --danger: #c0392b;
    --radius: 8px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--ink);
    background: var(--bg);
    font-size: 15px;
    line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Topbar */
.topbar {
    display: flex; align-items: center; gap: 24px;
    background: #0e2233; color: #fff;
    padding: 0 20px; height: 54px;
}
.brand { font-weight: 700; }
.mainnav { display: flex; gap: 4px; flex-wrap: wrap; }
.mainnav a {
    color: #cdd8e2; padding: 8px 12px; border-radius: 6px; font-size: 14px;
}
.mainnav a:hover { background: rgba(255,255,255,.1); text-decoration: none; }
.mainnav a.active { background: var(--accent); color: #fff; }

/* Caja de usuario (derecha de la barra) */
.userbox { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.userbox .uname { color: #cdd8e2; font-size: 13px; white-space: nowrap; padding: 6px 8px; border-radius: 6px; }
.userbox a.uname:hover { background: rgba(255,255,255,.1); text-decoration: none; }

/* Login / arranque */
.login-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 20px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 26px; width: 100%; max-width: 380px; box-shadow: 0 8px 30px rgba(0,0,0,.08); }
.login-brand { font-weight: 700; color: var(--ink); margin-bottom: 14px; }
.login-card h1 { font-size: 20px; margin: 0 0 6px; }
.login-card .field { margin-bottom: 14px; }
.login-card .field input { width: 100%; }
.login-foot { font-size: 12px; }

.container { max-width: 1200px; margin: 0 auto; padding: 22px 20px 60px; }
.page-title { margin: 4px 0 18px; font-size: 22px; }
.intro { margin: -10px 0 20px; color: var(--muted); font-size: 15px; max-width: 70ch; }

.footer { text-align: center; color: var(--muted); padding: 20px; font-size: 13px; }

/* Cards / panels */
.panel {
    background: var(--panel); border: 1px solid var(--line);
    border-radius: var(--radius); padding: 16px; margin-bottom: 18px;
}

/* Dashboard tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.tile {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 18px; text-align: center;
}
.tile .num { font-size: 30px; font-weight: 700; color: var(--accent); }
.tile .lbl { color: var(--muted); font-size: 13px; margin-top: 4px; }
/* Los KPI usan grid y no arrastran margen inferior: hay que separarlos de lo que sigue. */
.panel-tras-tiles { margin-top: 22px; }

/* Tablas resumen del panel de inicio */
.resumen-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; align-items: start; }
.resumen-grid .panel { margin-bottom: 0; }
.resumen-grid h2 { font-size: 16px; margin: 0 0 2px; }
.sub-nota { color: var(--muted); font-size: 12px; margin-bottom: 10px; }
table.mini { width: 100%; border-collapse: collapse; font-size: 13px; }
table.mini td { padding: 6px 4px; border-bottom: 1px solid #eef2f6; vertical-align: top; }
table.mini tr:last-child td { border-bottom: 0; }
.mini-k { white-space: nowrap; font-family: ui-monospace, "Consolas", monospace; width: 1%; }
.mini-v { width: 100%; }
.mini-n { text-align: right; white-space: nowrap; color: var(--accent-dark); }

/* Toolbar / filters */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 14px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--muted); }
input, select, textarea {
    font: inherit; padding: 7px 9px; border: 1px solid var(--line);
    border-radius: 6px; background: #fff; color: var(--ink); min-width: 140px;
}
textarea { min-height: 60px; resize: vertical; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bcd8f0; border-color: var(--accent); }

/* Buttons */
.btn {
    display: inline-block; cursor: pointer; border: 1px solid var(--accent);
    background: var(--accent); color: #fff; padding: 8px 14px; border-radius: 6px;
    font-size: 14px; text-decoration: none;
}
.btn:hover { background: var(--accent-dark); text-decoration: none; }
.btn.ghost { background: #fff; color: var(--accent); }
.btn.ghost:hover { background: #eef6fc; }
.btn.danger { background: var(--danger); border-color: var(--danger); }
.btn.small { padding: 4px 9px; font-size: 13px; }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
table.data { border-collapse: collapse; width: 100%; font-size: 14px; }
table.data th, table.data td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.data th { background: #f0f4f8; position: sticky; top: 0; }
table.data tr:hover td { background: #f7fafd; }
table.data td.wrap { white-space: normal; max-width: 320px; }
/* Filas seleccionables (tabla de registros de la ficha de especie) */
table.data tbody.selectable tr { cursor: pointer; }
table.data tbody.selectable tr.sel td,
table.data tbody.selectable tr.sel:hover td { background: #fbe1de; }

/* Cabecera de panel con acción a la derecha */
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; }
.ph-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* Fichas de lectura (especie_view / referencia_view) */
.view-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; flex-wrap: wrap; margin-bottom: 8px; }
.view-head .sci { margin: 0 0 4px; font-size: 22px; }
.view-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.subhead { margin: 18px 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--accent-dark); }
.info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px 20px; }
.info-grid .kv { display: flex; flex-direction: column; gap: 2px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.info-grid .kv .k { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.info-grid .kv .v { font-size: 15px; word-break: break-word; }
.cite { margin: 0; padding: 10px 12px; background: #f7fafd; border-left: 3px solid var(--accent); border-radius: 4px; }

/* Pagination */
.pager { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.pager .info { color: var(--muted); font-size: 13px; }

/* Badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 20px; font-size: 12px; }
.badge.si { background: #e2f5ea; color: var(--ok); }
.badge.no { background: #fbe7e5; color: var(--danger); }
.badge.na { background: #eef1f4; color: var(--muted); }

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 0 0 16px; }
legend { font-weight: 600; color: var(--accent-dark); padding: 0 6px; }

/* Dos fieldsets lado a lado (escritorio) */
.fieldset-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.fieldset-row fieldset { margin-bottom: 16px; }
.fieldset-row .field { margin-bottom: 12px; }
.fieldset-row .field input { width: 100%; }
@media (max-width: 760px) { .fieldset-row { grid-template-columns: 1fr; } }
.form-actions { display: flex; gap: 10px; margin-top: 8px; }

/* Autocomplete */
.ac-wrap { position: relative; }
.ac-list {
    position: absolute; z-index: 20; left: 0; right: 0; top: 100%;
    background: #fff; border: 1px solid var(--line); border-radius: 6px;
    max-height: 240px; overflow-y: auto; box-shadow: 0 6px 20px rgba(0,0,0,.12);
}
.ac-list div { padding: 7px 10px; cursor: pointer; font-size: 14px; }
.ac-list div:hover, .ac-list div.sel { background: #eef6fc; }

/* Map */
#map { height: 560px; border-radius: var(--radius); border: 1px solid var(--line); }

/* Estadísticas / gráficos */
.charts-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 18px; margin-top: 18px; }
.chart-card {
    grid-column: span 2;                 /* por defecto: 1/3 del ancho */
    background: var(--panel); border: 1px solid var(--line);
    border-radius: var(--radius); padding: 16px;
}
.chart-card.chart-wide { grid-column: 1 / -1; }          /* ancho completo */
.chart-card.chart-phylum { grid-column: span 3; }         /* mitad del ancho (~1,5×) */
.chart-card h2 { margin: 0 0 2px; font-size: 16px; }
.chart-card .sub { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.chart-box { position: relative; height: 300px; }
.chart-card.chart-wide .chart-box { height: 340px; }
.chart-card.chart-phylum .chart-box { height: 380px; }     /* también más alto (~2× área) */
@media (max-width: 900px) {
    .charts-grid { grid-template-columns: 1fr; }
    .chart-card, .chart-card.chart-wide, .chart-card.chart-phylum { grid-column: 1 / -1; }
}

/* Esquema de la base de datos (esquema.php) */
.esq-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.esq-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.esq-card:target { border-color: var(--accent); box-shadow: 0 0 0 3px #bcd8f0; }
.esq-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
            background: #eaf1f8; border-bottom: 1px solid var(--line); padding: 9px 12px; }
.esq-nombre { font-weight: 700; font-family: ui-monospace, "Consolas", monospace; }
.esq-filas { color: var(--muted); font-size: 12px; white-space: nowrap; }
.esq-cols { width: 100%; border-collapse: collapse; font-size: 13px; }
.esq-cols td { padding: 6px 10px; border-bottom: 1px solid #eef2f6; vertical-align: top; }
.esq-cols tr:last-child td { border-bottom: 0; }
.esq-k { width: 46px; white-space: nowrap; }
.esq-n { font-family: ui-monospace, "Consolas", monospace; }
.esq-pk { font-weight: 700; }
.esq-fk { font-style: italic; }
.esq-ref { font-size: 11px; margin-left: 6px; font-family: system-ui, sans-serif; }
.esq-desc { font-family: system-ui, sans-serif; font-size: 11px; color: var(--muted);
            max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.esq-desc.sin { font-style: italic; color: #b9a15c; }
.esq-t { color: var(--muted); font-size: 11px; white-space: nowrap; text-align: right; }
.esq-p { text-align: right; width: 46px; }
.kbadge { display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 4px; border-radius: 3px; margin-right: 2px; }
.kbadge.pk { background: #dcecfa; color: var(--accent-dark); }
.kbadge.fk { background: #f3e8fb; color: #7a3fa0; }
.pct { font-size: 11px; color: var(--ok); }
.pct.medio { color: var(--warn); }
.pct.bajo { color: var(--danger); }
.mermaid { background: transparent; }

/* Estado del sistema (salud.php) */
.chk-lista { display: flex; flex-direction: column; gap: 2px; }
.chk { display: flex; gap: 12px; padding: 11px 12px; border-radius: 6px; align-items: flex-start; }
.chk + .chk { border-top: 1px solid var(--line); }
.chk-icono { flex: 0 0 22px; height: 22px; border-radius: 50%; display: flex; align-items: center;
             justify-content: center; font-weight: 700; font-size: 13px; color: #fff; }
.chk-ok    .chk-icono { background: var(--ok); }
.chk-aviso .chk-icono { background: var(--warn); }
.chk-error .chk-icono { background: var(--danger); }
.chk-error { background: #fdf1ef; }
.chk-aviso { background: #fdf9ef; }
.chk-titulo { font-weight: 600; }
.chk-detalle { color: var(--muted); font-size: 13px; margin-top: 2px; }
.chk-accion { font-size: 13px; margin-top: 6px; }
.chk-accion code { background: #eef2f6; padding: 1px 5px; border-radius: 3px; }

/* Misc */
.muted { color: var(--muted); }
.right { text-align: right; }
.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; }
.flash.ok { background: #e2f5ea; color: var(--ok); }
.flash.err { background: #fbe7e5; color: var(--danger); }
.spinner { color: var(--muted); font-size: 14px; padding: 20px; text-align: center; }
