:root {
    --green: #2e7d32;
    --green-dark: #1b5e20;
    --bg: #f4f5f7;
    --panel: #ffffff;
    --border: #dcdfe4;
    --text: #222;
    --muted: #6b7280;
    --danger: #c62828;
    --ok: #2e7d32;
}

* { box-sizing: border-box; }

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

/* --- Topbar --- */
.topbar {
    background: var(--green-dark);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 0 12px;
    flex-wrap: nowrap;
    white-space: nowrap;
    position: relative;
}
.brand { display: flex; align-items: center; gap: 8px; padding: 10px 0; text-decoration: none; color: #fff; flex: 0 0 auto; }
.brand .title {
    font-weight: 800; font-size: 19px; letter-spacing: .5px; color: #fff;
    transition: color .15s ease, text-shadow .15s ease;
}
.brand:hover .title { color: #eaf7ea; text-shadow: 0 1px 6px rgba(255,255,255,.3); }

/* Botón del menú */
.menu-toggle {
    background: rgba(255,255,255,.12);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 6px 9px;
    font-size: 12.5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
}
.menu-toggle:hover { background: rgba(255,255,255,.22); }
.menu-toggle .menu-icon { font-size: 16px; line-height: 1; }

/* Desplegable de navegación */
.dropnav {
    position: absolute;
    top: 100%;
    left: 16px;
    z-index: 60;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0,0,0,.18);
    min-width: 240px;
    max-height: 80vh;
    overflow-y: auto;
    padding: 8px 0;
    margin-top: 4px;
}
.dropnav a {
    display: block;
    color: var(--text);
    text-decoration: none;
    padding: 9px 18px;
    font-size: 14px;
}
.dropnav a:hover { background: #eef7ee; }
.dropnav a.active { color: var(--green-dark); font-weight: 600; background: #e8f5e9; box-shadow: inset 3px 0 0 var(--green); }
.dropnav-group {
    padding: 10px 18px 4px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    font-weight: 600;
    border-top: 1px solid var(--border);
    margin-top: 4px;
}
.dropnav-group:first-of-type { border-top: none; margin-top: 0; }

/* Link principal fijo en la barra (Producción) */
.toplink {
    color: #fff;
    text-decoration: none;
    font-weight: 600;
    font-size: 12.5px;
    padding: 6px 9px;
    border-radius: 6px;
    background: rgba(255,255,255,.14);
    margin-left: 2px;
    flex: 0 0 auto;
}
.toplink:hover { background: rgba(255,255,255,.26); }
.toplink.active { background: #fff; color: var(--green-dark); }
.topmenu-btn { cursor: pointer; border: none; font-family: inherit; }

/* Desplegable de nivel superior (al lado de Producción) */
.topmenu { position: relative; display: inline-block; }
.topmenu-panel {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 60;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0,0,0,.18);
    min-width: 200px;
    padding: 8px 0;
    margin-top: 6px;
}
.topmenu-panel a {
    display: block;
    color: var(--text);
    text-decoration: none;
    padding: 9px 18px;
    font-size: 14px;
}
.topmenu-panel a:hover { background: #eef7ee; }
.topmenu-panel a.active { color: var(--green-dark); font-weight: 600; background: #e8f5e9; box-shadow: inset 3px 0 0 var(--green); }

.userbox { margin-left: auto; display: flex; align-items: center; gap: 10px; color: #d7ecd8; font-size: 13px; flex: 0 0 auto; padding-left: 6px; }
.userbox a { color: #fff; text-decoration: none; background: rgba(255,255,255,.12); padding: 6px 12px; border-radius: 6px; }
.userbox a:hover { background: rgba(255,255,255,.22); }

/* --- Layout --- */
.container { max-width: 1100px; margin: 24px auto; padding: 0 16px; }

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    gap: 12px;
    flex-wrap: wrap;
}
.page-head h1 { font-size: 22px; margin: 0; }

/* --- Panels --- */
.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 18px;
    margin-bottom: 20px;
}
.panel h2 { margin-top: 0; font-size: 17px; }

/* --- Buttons --- */
.btn {
    display: inline-block;
    background: var(--green);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 9px 16px;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { background: var(--green-dark); }
.btn-secondary { background: #607d8b; }
.btn-secondary:hover { background: #455a64; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #8e0000; }
.btn-sm { padding: 5px 10px; font-size: 13px; }

/* --- Tables --- */
table.grid {
    width: 100%;
    border-collapse: collapse;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}
table.grid th, table.grid td {
    padding: 9px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
table.grid th {
    background: #eef1f0;
    font-weight: 600;
    color: #374151;
}
table.grid tr:last-child td { border-bottom: none; }
table.grid tbody tr:hover { background: #f7faf7; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.actions { white-space: nowrap; text-align: right; }
/* La columna de acciones (Editar / Borrar) queda fija a la derecha aunque la tabla sea más ancha que la pantalla. */
table.grid td.actions { position: sticky; right: 0; background: #fff; box-shadow: -6px 0 6px -6px rgba(0,0,0,.18); }
.empty { text-align: center; color: var(--muted); padding: 24px; }

/* --- Forms --- */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 13px; color: var(--muted); }
/* Estilo unificado para todos los controles de formulario */
input[type=text], input[type=number], input[type=email], input[type=password],
select, textarea {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    width: 100%;
    background: #fff;
    color: var(--text);
    box-sizing: border-box;
}
/* Flecha personalizada para los select (se ven consistentes en todos los navegadores) */
select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23607d8b' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 30px;
    cursor: pointer;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--green);
    box-shadow: 0 0 0 2px rgba(46,125,50,.15);
}
/* Controles dentro de las tablas ocupan todo el ancho de la celda */
table.grid td input, table.grid td select { margin: 0; }
.form-actions { margin-top: 16px; display: flex; gap: 10px; }

/* --- Flash --- */
.flash {
    padding: 12px 16px;
    border-radius: 6px;
    margin-bottom: 16px;
    border: 1px solid transparent;
}
.flash-ok { background: #e8f5e9; color: #1b5e20; border-color: #a5d6a7; }
.flash-error { background: #ffebee; color: #b71c1c; border-color: #ef9a9a; }

/* --- Searchable select (combobox con búsqueda) --- */
.ss { position: relative; }
.ss-input { width: 100%; }
.ss-list {
    position: absolute;
    z-index: 30;
    left: 0; right: 0; top: 100%;
    background: #fff;
    border: 1px solid var(--green);
    border-top: none;
    max-height: 240px;
    overflow-y: auto;
    box-shadow: 0 6px 14px rgba(0,0,0,.12);
    border-radius: 0 0 6px 6px;
}
.ss-item { padding: 8px 10px; cursor: pointer; }
.ss-item:hover { background: #eef7ee; }
.ss-empty { padding: 8px 10px; color: var(--muted); }
/* Las tablas con combobox no deben recortar el desplegable */
table.grid.ss-table { overflow: visible; }

/* --- Impresión de reportes --- */
@media print {
    .topbar, .no-print, .page-head > div, .footer { display: none !important; }
    .container { max-width: 100%; margin: 0; padding: 0; }
    .page-head h1 { font-size: 18px; }
    table.grid { font-size: 11px; }
    table.grid th, table.grid td { padding: 4px 6px; }
}

/* --- Misc --- */
.footer { text-align: center; color: var(--muted); padding: 24px; }
.muted { color: var(--muted); }
.tag { background:#eef1f0; border-radius:4px; padding:2px 8px; font-size:12px; }

/* --- Micro-interacciones: feedback al pasar el mouse por lo clickeable --- */
.btn, .toplink, .menu-toggle, .userbox a, .dropnav a, .topmenu-panel a,
.ss-item, .tag, table.grid tbody tr, tr.rowlink, .kpi {
    transition: background-color .15s ease, color .15s ease, box-shadow .15s ease,
                transform .1s ease, border-color .15s ease, filter .15s ease;
}

/* Botones: leve elevación al pasar el mouse y hundido al apretar */
.btn { box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.btn:hover { transform: translateY(-1px); box-shadow: 0 3px 8px rgba(0,0,0,.18); }
.btn:active { transform: translateY(0); box-shadow: 0 1px 2px rgba(0,0,0,.12); filter: brightness(.95); }

/* Filas de listados clickeables: resalte más marcado + barra lateral verde */
tr.rowlink:hover { background: #e8f3ea; box-shadow: inset 3px 0 0 var(--green); }

/* Links de texto (los que no son botones ni tags): subrayado y color al pasar */
.panel a:not(.btn):not(.tag):not(.toplink),
table.grid a:not(.btn):not(.tag) { color: var(--green-dark); text-decoration: none; }
.panel a:not(.btn):not(.tag):not(.toplink):hover,
table.grid a:not(.btn):not(.tag):hover { text-decoration: underline; }

/* Ítems del buscador: mismo resalte que las filas */
.ss-item:hover { background: #e8f3ea; box-shadow: inset 3px 0 0 var(--green); }

/* Tags que son links (ej. "Pendiente de factura") */
a.tag { text-decoration: none; cursor: pointer; }
a.tag:hover { filter: brightness(.96); box-shadow: 0 1px 5px rgba(0,0,0,.16); }

/* Tarjetas KPI del tablero: leve elevación */
.kpi { transition: box-shadow .15s ease, transform .1s ease; }
.kpi:hover { box-shadow: 0 5px 16px rgba(0,0,0,.09); transform: translateY(-1px); }

/* Foco accesible por teclado (no tapa el mouse, solo navegación con Tab) */
.btn:focus-visible, a:focus-visible, .toplink:focus-visible, .ss-input:focus-visible {
    outline: 2px solid var(--green); outline-offset: 2px;
}

/* Animación de aparición de los desplegables (menú ☰ y menús de arriba) */
@keyframes navDrop {
    from { opacity: 0; transform: translateY(-8px) scale(.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}
.dropnav:not([hidden]), .topmenu-panel:not([hidden]) {
    animation: navDrop .16s ease-out;
    transform-origin: top left;
}
/* Animación de aparición del combobox de búsqueda */
.ss-list { animation: navDrop .14s ease-out; transform-origin: top; }

/* Crédito de PiramideSoft en el pie */
.credito {
    display: flex; align-items: center; justify-content: center; gap: 10px;
    margin-bottom: 8px; color: var(--muted); font-size: 13px;
}
.credito-logo { height: 30px; width: auto; opacity: .9; transition: opacity .15s ease, transform .15s ease; }
.credito:hover .credito-logo { opacity: 1; transform: scale(1.04); }
.credito a { color: var(--green-dark); text-decoration: none; font-weight: 600; }
.credito a:hover { text-decoration: underline; }

/* Respeta a quien prefiere menos movimiento */
@media (prefers-reduced-motion: reduce) {
    .btn, .toplink, .menu-toggle, .userbox a, .dropnav a, .topmenu-panel a,
    .ss-item, .tag, table.grid tbody tr, tr.rowlink, .kpi { transition: none; }
    .btn:hover, .btn:active, .kpi:hover { transform: none; }
    .dropnav:not([hidden]), .topmenu-panel:not([hidden]), .ss-list { animation: none; }
}

/* Contenedor que permite deslizar las tablas anchas (sobre todo en celular) */
.table-wrap { overflow: auto; -webkit-overflow-scrolling: touch; margin-bottom: 20px;
    max-height: calc(100vh - 110px); min-height: 160px;      /* la tabla no pasa el alto de la pantalla: la barra horizontal queda siempre a la vista */
    scrollbar-width: auto; scrollbar-color: #7d8a86 #e3e7e6; }
.table-wrap::-webkit-scrollbar { height: 16px; width: 14px; }
.table-wrap::-webkit-scrollbar-track { background: #e3e7e6; border-radius: 8px; }
.table-wrap::-webkit-scrollbar-thumb { background: #7d8a86; border-radius: 8px; border: 3px solid #e3e7e6; }
.table-wrap::-webkit-scrollbar-thumb:hover { background: #4f5d59; }
.table-wrap > table.grid thead th { position: sticky; top: 0; z-index: 3; }   /* encabezado fijo al bajar */
.table-wrap > table.grid td.actions { z-index: 1; }
.table-wrap > table.grid { margin-bottom: 0; overflow: visible; overflow: clip; } /* sin "hidden": si no, la columna de acciones no puede quedar fija */
/* Links extra del menú ☰ que SOLO aparecen en celular (en PC ya están en la barra).
   Se usa `.dropnav .nav-mobile` (especificidad 0,2,0) para ganarle a `.dropnav a` (0,1,1),
   si no, los links no se ocultaban y se veían duplicados en PC. */
.dropnav .nav-mobile { display: none; }

/* ============================================================
   CELULAR / pantallas chicas
   ============================================================ */
@media (max-width: 760px) {
    body { font-size: 14px; }

    /* Barra: solo ☰ Menú + Exitia + Salir. El resto va al menú ☰. */
    .topbar { gap: 8px; padding: 0 10px; }
    .topbar > .toplink,
    .topbar > .topmenu { display: none !important; }
    .brand .title { font-size: 17px; }
    .userbox { gap: 8px; }
    .userbox .user-name { display: none; }           /* en celular solo "Salir" */
    .dropnav .nav-mobile { display: block; }          /* el menú ☰ queda completo en celular */

    /* El menú ☰ ocupa casi todo el ancho */
    .dropnav { left: 8px; right: 8px; min-width: 0; }

    /* Layout más compacto */
    .container { margin: 14px auto; padding: 0 12px; }
    .page-head { gap: 8px; }
    .page-head h1 { font-size: 19px; }
    .panel { padding: 14px; border-radius: 8px; }

    /* Formularios: una columna */
    .form-grid { grid-template-columns: 1fr; }
    .form-actions { flex-wrap: wrap; }
    .form-actions .btn { flex: 1 1 auto; text-align: center; }

    /* Botones del encabezado a lo ancho para tocarlos fácil */
    .page-head > div { width: 100%; display: flex; flex-wrap: wrap; gap: 8px; }
    .page-head > div .btn { flex: 1 1 auto; text-align: center; }

    /* Tablas: un poco más compactas (y deslizables por .table-wrap) */
    table.grid th, table.grid td { padding: 8px 9px; }

    /* Tableros */
    .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
    .kpi { padding: 12px; }
    .kpi .k-val { font-size: 22px; }
}

/* Teléfonos angostos */
@media (max-width: 420px) {
    .kpis { grid-template-columns: 1fr; }
    .brand .title { font-size: 16px; }
}

/* Notebooks / pantallas medianas: tablas más compactas para que entren sin tener que correrlas */
@media (max-width: 1440px) {
    .table-wrap > table.grid th, .table-wrap > table.grid td { padding: 7px 7px; font-size: 13px; }
    .table-wrap > table.grid td.actions .btn-sm { padding: 3px 7px; font-size: 12px; }
}
