/* =====================================================================
   smartisp — estilos base con design tokens + modo claro/oscuro + responsive
   ===================================================================== */
:root {
    --brand: #2563eb;
    --brand-dark: #1d4ed8;
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --text-1: #0f172a;
    --text-2: #64748b;
    --text-3: #94a3b8;
    --green: #16a34a;
    --green-lt: #dcfce7;
    --red: #dc2626;
    --red-lt: #fee2e2;
    --amber: #d97706;
    --amber-lt: #fef3c7;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,.04), 0 4px 12px rgba(0,0,0,.06);
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    --sidebar-w: 230px;
}
:root[data-theme="dark"] {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #172033;
    --border: #334155;
    --text-1: #f1f5f9;
    --text-2: #94a3b8;
    --text-3: #64748b;
    --green-lt: #14532d;
    --red-lt: #7f1d1d;
    --amber-lt: #78350f;
    --shadow: 0 1px 3px rgba(0,0,0,.3), 0 4px 12px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text-1);
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

/* ------------------------------------------------------------- layout app */
.app { display: flex; min-height: 100vh; max-width: 100%; }
.sidebar {
    width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border);
    padding: 1rem; flex-shrink: 0;
}
.sidebar h1 { font-size: 1.05rem; margin: 0 0 1.25rem; color: var(--brand); }
.sidebar nav a {
    display: block; padding: .6rem .7rem; border-radius: 8px; color: var(--text-2);
    text-decoration: none; margin-bottom: .2rem; font-weight: 500;
}
.sidebar nav a:hover { background: var(--surface-2); color: var(--text-1); }
.sidebar nav a.active { background: var(--brand); color: #fff; }
.main { flex: 1; padding: 1.5rem 2rem; min-width: 0; }

.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; gap: .5rem; }
.topbar .who { color: var(--text-2); font-size: .85rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.menu-toggle { display: none; background: var(--surface); border: 1px solid var(--border);
    border-radius: 8px; width: 40px; height: 40px; font-size: 1.2rem; cursor: pointer; color: var(--text-1); }
.sidebar-overlay { display: none; }

/* ------------------------------------------------------------- cards */
.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 1.25rem 1.5rem; margin-bottom: 1.25rem;
}
.card-header { margin: -1.25rem -1.5rem 1.25rem; padding: .9rem 1.5rem; border-bottom: 1px solid var(--border); }
.card-title { margin: 0; font-size: 1rem; }

/* ------------------------------------------------------------- forms */
.form-group { margin-bottom: 1rem; }
.form-label { display: block; font-weight: 600; margin-bottom: .35rem; font-size: .85rem; }
.required { color: var(--red); }
.form-control, .form-select {
    width: 100%; padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface); color: var(--text-1); font-size: 16px; outline: none;
}
.form-control:focus, .form-select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.form-hint { color: var(--text-3); font-size: .78rem; margin-top: .3rem; }
.text-mono { font-family: var(--font-mono); }
.d-flex { display: flex; } .gap-2 { gap: 1rem; } .gap-1 { gap: .5rem; }
.mb-2 { margin-bottom: 1rem; } .mb-3 { margin-bottom: 1.5rem; }

.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .5rem;
    padding: .75rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.perm-grid label { display: flex; align-items: center; gap: .45rem; font-size: .85rem; cursor: pointer; }

/* ------------------------------------------------------------- buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .6rem 1rem;
    border: 0; border-radius: 8px; font-weight: 600; font-size: .88rem; cursor: pointer; text-decoration: none;
    min-height: 40px; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-secondary { background: var(--surface-2); color: var(--text-1); border: 1px solid var(--border); }

/* ------------------------------------------------------------- tabla */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { padding: .65rem .9rem; text-align: left; border-bottom: 1px solid var(--border); font-size: .88rem; }
.table th { background: var(--surface-2); color: var(--text-2); font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; white-space: nowrap; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: var(--surface-2); }

.pill { display: inline-flex; padding: 2px 10px; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.pill.on { background: var(--green-lt); color: var(--green); }
.pill.off { background: var(--red-lt); color: var(--red); }
.pill.role { background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }

.alert { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: .88rem; }
.alert-danger { background: var(--red-lt); color: var(--red); }

/* ------------------------------------------------------------- login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.login-card { width: 100%; max-width: 380px; }

.theme-toggle { background: none; border: 1px solid var(--border); border-radius: 8px;
    padding: 0 .65rem; height: 40px; cursor: pointer; color: var(--text-2); font-size: 1rem; }

/* ------------------------------------------------------------- overlay procesando */
.proc-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.6); z-index: 2000;
    display: none; align-items: center; justify-content: center; flex-direction: column; gap: 1rem; }
.proc-overlay.show { display: flex; }
.proc-overlay .spinner { width: 52px; height: 52px; border: 5px solid rgba(255,255,255,.25);
    border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
.proc-overlay .proc-text { color: #fff; font-weight: 600; font-size: 1rem; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------- badges ONU */
.badge-onu { display: inline-flex; align-items: center; gap: .4rem; padding: 3px 10px; border-radius: 999px;
    font-size: .75rem; font-weight: 700; border: 1px solid transparent; white-space: nowrap; }
.badge-onu .ic { font-size: .7rem; }
.badge-onu.on  { background: var(--green-lt); color: var(--green); }
.badge-onu.los { background: var(--red-lt);   color: var(--red); }
.badge-onu.pf  { background: var(--amber-lt); color: var(--amber); }
.badge-onu.off { background: var(--surface-2); color: var(--text-2); border-color: var(--border); }
.badge-onu.dis { background: var(--surface-2); color: var(--text-3); border-color: var(--border); }
.badge-onu.unk { background: var(--surface-2); color: var(--text-3); border-color: var(--border); }

.icon-btn { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
    width: 38px; height: 38px; cursor: pointer; font-size: 1.05rem; color: var(--text-2); margin-left: .25rem; }
.icon-btn:hover { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ------------------------------------------------------------- modal propio */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.55);
    display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 1rem; }
.modal-overlay[hidden] { display: none; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    width: 100%; max-width: 580px; max-height: 92vh; display: flex; flex-direction: column;
    box-shadow: 0 20px 60px rgba(0,0,0,.3); animation: modal-in .18s ease-out; }
@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-header { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); display: flex;
    align-items: center; justify-content: space-between; gap: 1rem; }
.modal-header h3 { margin: 0; font-size: 1rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-close { background: none; border: 0; font-size: 1.4rem; line-height: 1; color: var(--text-3); cursor: pointer; padding: 0 .25rem; }
.modal-body { padding: 1.25rem; overflow-y: auto; }
.modal-footer { padding: 1rem 1.25rem; border-top: 1px solid var(--border); display: flex;
    justify-content: flex-end; gap: .5rem; flex-wrap: wrap; }

.data-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .65rem 1rem; margin-bottom: 1.25rem; }
.data-item { min-width: 0; }
.data-item .k { font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-3); display: block; margin-bottom: .1rem; }
.data-item .v { font-size: .9rem; color: var(--text-1); word-break: break-word; }
.modal-section-title { font-size: .8rem; font-weight: 700; color: var(--text-2); text-transform: uppercase;
    letter-spacing: .04em; margin: .25rem 0 .75rem; padding-top: .75rem; border-top: 1px dashed var(--border); }

/* =====================================================================
   RESPONSIVE
   ===================================================================== */
@media (max-width: 1024px) { .main { padding: 1.25rem; } }

@media (max-width: 768px) {
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; z-index: 900;
        transform: translateX(-100%); transition: transform .25s ease;
        box-shadow: 4px 0 24px rgba(0,0,0,.15);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 800; }
    .sidebar-overlay.show { display: block; }
    .menu-toggle { display: inline-flex; align-items: center; justify-content: center; }
    .main { padding: 1rem; }
    .topbar { flex-wrap: wrap; }

    .card { padding: 1rem; }
    .card-header { margin: -1rem -1rem 1rem; padding: .8rem 1rem; }

    form.d-flex { flex-direction: column; align-items: stretch !important; }
    form.d-flex .form-group { width: 100%; flex: none !important; min-width: 0 !important; }
    .who-name { display: none; }  /* el nombre largo empuja el ancho en pantallas chicas */
    .d-flex.gap-2 { gap: .6rem; }

    table.responsive-cards, .responsive-cards thead, .responsive-cards tbody,
    .responsive-cards tr, .responsive-cards td { display: block; width: 100%; }
    .responsive-cards thead { display: none; }
    .responsive-cards tr { border: 1px solid var(--border); border-radius: var(--radius);
        margin-bottom: .75rem; background: var(--surface); overflow: hidden; }
    .responsive-cards td { display: flex; justify-content: space-between; align-items: center; gap: 1rem;
        text-align: right; border-bottom: 1px solid var(--border); padding: .6rem .85rem; }
    .responsive-cards td:last-child { border-bottom: none; }
    .responsive-cards td::before { content: attr(data-label); font-weight: 700; font-size: .72rem;
        text-transform: uppercase; letter-spacing: .04em; color: var(--text-3); text-align: left; }
    .responsive-cards td[data-label=""]::before { content: ""; }
    .responsive-cards tr:hover td { background: var(--surface); }

    .data-grid { grid-template-columns: 1fr; }
    .modal { max-height: 88vh; }
    .btn { width: 100%; }
    .modal-footer .btn { width: auto; }
    .perm-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }

    /* En móvil las tablas no se comprimen: mantienen ancho legible y scrollean
       dentro de su .table-wrap (no en la página). El botón de acción no se encoge. */
    .table { min-width: 520px; }
    .table .btn { width: auto; }
}

@media (max-width: 420px) { .who-name { display: none; } }