/* =====================================================================
   CRM Alto Grande — estilos base (Fase 1)
   Minimalista, fondo claro, sin dependencias externas (CSP: style-src 'self').
   ===================================================================== */

:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-muted: #f9fafb;
    --border: #e2e5ea;
    --border-strong: #cdd2da;
    --text: #18212f;
    --text-muted: #5b6676;
    --text-subtle: #7b8594;

    --primary: #1d4f7c;
    --primary-hover: #163d61;
    --primary-soft: #e8f0f7;
    --focus: #3b82c4;

    --success: #17703f;
    --success-soft: #e6f4ec;
    --warning: #8a5a00;
    --warning-soft: #fdf3dc;
    --danger: #b42318;
    --danger-soft: #fcebea;
    --info: #1d4f7c;
    --info-soft: #e8f0f7;

    --radius: 10px;
    --radius-sm: 7px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .05);
    --shadow-md: 0 4px 16px rgba(16, 24, 40, .08);

    --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    --gutter: 24px;
    --max-width: 1120px;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.55;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { line-height: 1.25; margin: 0; }
p { margin: 0; }
code { font-family: var(--mono); font-size: .92em; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0);
    white-space: nowrap; border: 0;
}

.skip-link {
    position: absolute; left: 12px; top: -48px;
    background: var(--primary); color: #fff;
    padding: 8px 14px; border-radius: var(--radius-sm); z-index: 100;
}
.skip-link:focus { top: 12px; }

.muted { color: var(--text-subtle); }

/* ---------- Topbar / navegación ---------- */

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.topbar__inner {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 0 var(--gutter);
    min-height: 60px;
    display: flex; align-items: center; gap: 28px;
}

.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); font-weight: 650; }
.brand:hover { text-decoration: none; }
.brand__mark {
    display: inline-grid; place-items: center;
    width: 32px; height: 32px; border-radius: 8px;
    background: var(--primary); color: #fff;
    font-size: 13px; font-weight: 700; letter-spacing: .5px;
}
.brand__mark--lg { width: 44px; height: 44px; border-radius: 11px; font-size: 16px; }
.brand__name { white-space: nowrap; }

.nav { display: flex; align-items: center; flex: 1; gap: 16px; }
.nav__list { display: flex; gap: 4px; list-style: none; margin: 0; padding: 0; flex: 1; }
.nav__link {
    display: block; padding: 8px 12px; border-radius: var(--radius-sm);
    color: var(--text-muted); font-weight: 500;
}
.nav__link:hover { background: var(--surface-muted); color: var(--text); text-decoration: none; }
.nav__link[aria-current="page"] { color: var(--primary); background: var(--primary-soft); }

.nav__user { display: flex; align-items: center; gap: 10px; }
.nav__account { display: flex; align-items: center; gap: 10px; padding: 4px 8px; border-radius: var(--radius-sm); color: var(--text); }
.nav__account:hover { background: var(--surface-muted); text-decoration: none; }
.nav__account-text { display: flex; flex-direction: column; line-height: 1.2; }
.nav__account-name { font-weight: 600; font-size: 14px; }
.nav__account-role { font-size: 12px; color: var(--text-subtle); }
.nav__logout { margin: 0; }

.nav-toggle {
    display: none; margin-left: auto;
    width: 40px; height: 40px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); cursor: pointer; position: relative;
}
.nav-toggle__bar, .nav-toggle__bar::before, .nav-toggle__bar::after {
    position: absolute; left: 50%; width: 18px; height: 2px; margin-left: -9px;
    background: var(--text); border-radius: 2px; content: "";
}
.nav-toggle__bar { top: 50%; margin-top: -1px; }
.nav-toggle__bar::before { top: -6px; }
.nav-toggle__bar::after { top: 6px; }

.avatar {
    display: inline-grid; place-items: center; flex: none;
    width: 34px; height: 34px; border-radius: 50%;
    background: var(--primary-soft); color: var(--primary);
    font-size: 13px; font-weight: 700;
}
.avatar--sm { width: 30px; height: 30px; font-size: 12px; }

/* ---------- Layout ---------- */

.main {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 28px var(--gutter) 48px;
    outline: none;
}
.footer {
    max-width: var(--max-width); margin: 0 auto;
    padding: 0 var(--gutter) 32px; color: var(--text-subtle); font-size: 13px;
}

.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; }
.page-title { font-size: 26px; font-weight: 700; letter-spacing: -.01em; }
.page-subtitle { margin-top: 4px; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.back-link { display: inline-block; margin-bottom: 12px; color: var(--text-muted); font-size: 14px; }

.grid { display: grid; gap: 20px; }
.grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 20px; }
.layout-split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.narrow { max-width: 640px; }

/* ---------- Cards ---------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px;
}
.card--flush { padding: 0; overflow: hidden; }
.card--danger { border-color: #f1c8c4; }
.card__header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card__title { font-size: 16px; font-weight: 650; margin-bottom: 14px; padding: 0; }
.card__header .card__title { margin-bottom: 0; }
.card__title--lg { font-size: 20px; }
.card__text { color: var(--text-muted); margin-bottom: 14px; }
.card__link { font-size: 14px; }

/* ---------- Stats ---------- */

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat {
    display: flex; flex-direction: column; gap: 2px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 18px 20px; box-shadow: var(--shadow); color: var(--text);
}
a.stat:hover { border-color: var(--border-strong); text-decoration: none; }
.stat__value { font-size: 28px; font-weight: 700; letter-spacing: -.02em; }
.stat__label { color: var(--text-muted); font-size: 14px; }
.stat--warning { border-color: #ecd59c; background: var(--warning-soft); }

/* ---------- Botones ---------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 40px; padding: 8px 16px;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    font: inherit; font-weight: 600; font-size: 14px; line-height: 1.2;
    cursor: pointer; text-decoration: none; white-space: nowrap;
    transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.btn:hover { text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-hover); }
.btn--secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn--secondary:hover { background: var(--surface-muted); }
.btn--ghost { background: transparent; color: var(--text-muted); }
.btn--ghost:hover { background: var(--surface-muted); color: var(--text); }
.btn--danger { background: var(--danger); color: #fff; }
.btn--danger:hover { background: #8f1c13; }
.btn--sm { min-height: 32px; padding: 5px 12px; font-size: 13px; }
.btn--block { width: 100%; }

/* ---------- Formularios ---------- */

.form { display: flex; flex-direction: column; gap: 16px; }
.form--inline { flex-direction: row; align-items: center; gap: 10px; margin-top: 10px; }
.fieldset { border: 0; margin: 0; padding: 0; min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.fieldset:disabled .input { background: var(--surface-muted); }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.field__label { font-weight: 600; font-size: 14px; }
.field__optional { font-weight: 400; color: var(--text-subtle); }
.field__hint { font-size: 13px; color: var(--text-subtle); margin-top: 6px; }
.field__hint--inline { display: block; margin-top: 2px; }
.field__error { font-size: 13px; color: var(--danger); font-weight: 500; }

.input {
    width: 100%; min-height: 42px; padding: 9px 12px;
    font: inherit; color: var(--text);
    background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    transition: border-color .15s ease, box-shadow .15s ease;
}
.input:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px rgba(59, 130, 196, .2); }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input--compact { width: auto; min-width: 150px; min-height: 36px; padding: 6px 10px; }
.input--mono { font-family: var(--mono); font-size: 13px; }
.input--code { font-family: var(--mono); font-size: 20px; letter-spacing: .25em; max-width: 220px; }
select.input { cursor: pointer; }

.checkbox { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.checkbox input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--primary); }

.segmented { display: inline-flex; flex-wrap: wrap; gap: 8px; }
.segmented__option {
    display: inline-flex; align-items: center; gap: 8px;
    padding: 8px 14px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    cursor: pointer; background: var(--surface);
}
.segmented__option input { accent-color: var(--primary); }
.segmented__option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.form__actions { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; padding-top: 4px; }
.form__actions--sticky {
    position: sticky; bottom: 0; background: var(--bg);
    padding: 14px 0; border-top: 1px solid var(--border);
}

.copy-field { display: flex; gap: 8px; margin: 6px 0; }

/* ---------- Alertas ---------- */

.flash-stack { display: flex; flex-direction: column; gap: 10px; margin-bottom: 18px; }
.alert {
    padding: 12px 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
    font-size: 14px; margin-bottom: 16px;
}
.flash-stack .alert { margin-bottom: 0; }
.alert--success { background: var(--success-soft); color: var(--success); border-color: #bfe3cc; }
.alert--error { background: var(--danger-soft); color: var(--danger); border-color: #f3c3be; }
.alert--warning { background: var(--warning-soft); color: var(--warning); border-color: #ecd59c; }
.alert--info { background: var(--info-soft); color: var(--info); border-color: #c8dbec; }
.callout { display: flex; align-items: center; justify-content: space-between; gap: 16px; }

/* ---------- Badges / switch ---------- */

.badge {
    display: inline-flex; align-items: center;
    padding: 2px 9px; border-radius: 999px;
    font-size: 12px; font-weight: 600; line-height: 1.6;
    background: var(--surface-muted); color: var(--text-muted); border: 1px solid var(--border);
}
.badge--active { background: var(--success-soft); color: var(--success); border-color: #bfe3cc; }
.badge--away { background: var(--warning-soft); color: var(--warning); border-color: #ecd59c; }
.badge--disabled { background: var(--surface-muted); color: var(--text-subtle); }
.badge--muted { background: var(--surface-muted); color: var(--text-subtle); }

.inline-form { margin: 0; }
.switch {
    position: relative; display: inline-flex; align-items: center;
    width: 64px; height: 28px; padding: 0 10px 0 32px;
    border: 1px solid var(--border-strong); border-radius: 999px;
    background: var(--surface-muted); color: var(--text-muted);
    font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
}
.switch::before {
    content: ""; position: absolute; left: 3px; top: 3px;
    width: 20px; height: 20px; border-radius: 50%; background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .2); transition: left .15s ease;
}
.switch--on { background: var(--primary); border-color: var(--primary); color: #fff; padding: 0 32px 0 12px; }
.switch--on::before { left: 39px; }

/* ---------- Tablas ---------- */

.filters { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; align-items: center; }
.filters__field { min-width: 170px; }
.filters__field--grow { flex: 1; min-width: 220px; }

.table-wrap {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow-x: auto;
}
.table { width: 100%; border-collapse: collapse; }
.table th {
    text-align: left; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-subtle); padding: 12px 16px; background: var(--surface-muted); border-bottom: 1px solid var(--border);
}
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fbfcfd; }

.user-cell { display: flex; align-items: center; gap: 12px; color: var(--text); }
.user-cell:hover { text-decoration: none; }
.user-cell:hover .user-cell__name { color: var(--primary); }
.user-cell > span:last-child { display: flex; flex-direction: column; min-width: 0; }
.user-cell__name { font-weight: 600; }
.user-cell__email { font-size: 13px; color: var(--text-subtle); overflow-wrap: anywhere; }

.pagination { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 18px; }
.pagination__status { color: var(--text-muted); font-size: 14px; }

/* ---------- Listas de datos ---------- */

.kv { margin: 0; display: flex; flex-direction: column; }
.kv__row { display: flex; justify-content: space-between; gap: 16px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.kv__row:last-child { border-bottom: 0; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; font-weight: 500; text-align: right; }
.card .kv { margin-bottom: 14px; }

.activity { list-style: none; margin: 0; padding: 0; }
.activity__item { display: flex; flex-direction: column; padding: 10px 0; border-bottom: 1px solid var(--border); }
.activity__item:last-child { border-bottom: 0; }
.activity__what { font-weight: 500; }
.activity__meta { font-size: 13px; color: var(--text-subtle); }
.empty-inline { color: var(--text-subtle); }

/* ---------- Estados vacíos ---------- */

.empty-state {
    background: var(--surface); border: 1px dashed var(--border-strong); border-radius: var(--radius);
    padding: 40px 24px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.empty-state__title { font-size: 18px; font-weight: 650; }
.empty-state__text { color: var(--text-muted); max-width: 460px; margin-bottom: 8px; }

/* ---------- Roles / permisos ---------- */

.role-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.role-card {
    display: flex; flex-direction: column; gap: 6px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 18px 20px; box-shadow: var(--shadow); color: var(--text);
}
.role-card:hover { border-color: var(--primary); text-decoration: none; }
.role-card__name { font-weight: 650; font-size: 16px; }
.role-card__description { color: var(--text-muted); font-size: 14px; flex: 1; }
.role-card__meta { color: var(--text-subtle); font-size: 13px; }

.legend { margin-bottom: 18px; }
.legend summary { cursor: pointer; color: var(--primary); font-weight: 500; margin-bottom: 8px; }
.legend .kv { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px; max-width: 560px; }

.permission-list { list-style: none; margin: 0; padding: 0; }
.permission { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.permission:last-child { border-bottom: 0; }
.permission__text { display: flex; flex-direction: column; }
.permission__label { font-weight: 600; }
.permission__description { font-size: 13px; color: var(--text-subtle); }

/* ---------- Health ---------- */

.checks { list-style: none; margin: 0; padding: 0; }
.check {
    display: grid; grid-template-columns: 14px minmax(0, 1.2fr) minmax(0, 2fr) auto;
    gap: 6px 14px; align-items: center; padding: 14px 20px; border-bottom: 1px solid var(--border);
}
.check:last-child { border-bottom: 0; }
.check__dot { width: 10px; height: 10px; border-radius: 50%; background: var(--success); }
.check--warning .check__dot { background: #d99a00; }
.check--error .check__dot { background: var(--danger); }
.check__label { font-weight: 600; }
.check__value { color: var(--text-muted); overflow-wrap: anywhere; }
.check__status { font-size: 12px; font-weight: 600; color: var(--success); }
.check--warning .check__status { color: var(--warning); }
.check--error .check__status { color: var(--danger); }
.check__detail { grid-column: 2 / -1; font-size: 13px; color: var(--text-subtle); }

/* ---------- MFA ---------- */

.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 20px; }
.steps__item { display: flex; flex-direction: column; gap: 6px; padding-left: 4px; }
.steps__text { color: var(--text-muted); }
.steps__more summary { cursor: pointer; color: var(--primary); font-size: 14px; }
.secret {
    display: inline-flex; flex-wrap: wrap; gap: 10px;
    padding: 12px 16px; background: var(--surface-muted); border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-size: 17px; letter-spacing: .08em; width: fit-content;
}
.recovery-codes {
    list-style: none; margin: 0 0 16px; padding: 16px;
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px;
    background: var(--surface-muted); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.recovery-codes code { font-size: 16px; letter-spacing: .06em; }

/* ---------- Auth ---------- */

.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth__main { width: 100%; max-width: 420px; }
.auth__brand { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 22px; }
.auth__brand-name { font-weight: 700; font-size: 18px; }
.auth__card {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    box-shadow: var(--shadow-md); padding: 32px;
}
.auth__title { font-size: 22px; font-weight: 700; margin-bottom: 6px; }
.auth__subtitle { color: var(--text-muted); margin-bottom: 22px; }
.auth__footer { margin-top: 20px; text-align: center; font-size: 14px; color: var(--text-muted); }

.error__status { font-size: 44px; font-weight: 800; color: var(--border-strong); line-height: 1; margin-bottom: 10px; }
.error__code { font-size: 13px; color: var(--text-subtle); margin-bottom: 20px; }
.error__details {
    white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; max-height: 260px; overflow: auto;
    background: var(--surface-muted); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; margin-bottom: 20px;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid--2, .layout-split { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 760px) {
    :root { --gutter: 16px; }
    .topbar__inner { flex-wrap: wrap; gap: 0; min-height: 56px; }
    .nav-toggle { display: block; }
    .nav {
        display: none; flex-basis: 100%; flex-direction: column; align-items: stretch;
        padding: 8px 0 14px; gap: 8px; border-top: 1px solid var(--border); margin-top: 8px;
    }
    .nav.is-open { display: flex; }
    .nav__list { flex-direction: column; }
    .nav__user { justify-content: space-between; padding-top: 8px; border-top: 1px solid var(--border); }

    .main { padding-top: 20px; }
    .page-header { flex-direction: column; }
    .page-title { font-size: 22px; }
    .field-row { grid-template-columns: minmax(0, 1fr); }
    .callout { flex-direction: column; align-items: flex-start; }
    .filters__field, .filters__field--grow { min-width: 100%; }
    .filters .btn { flex: 1; }

    .table--responsive thead { display: none; }
    .table--responsive tr { display: block; padding: 12px 16px; border-bottom: 1px solid var(--border); }
    .table--responsive tbody tr:last-child { border-bottom: 0; }
    .table--responsive td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 0; border: 0; }
    .table--responsive td::before { content: attr(data-label); font-size: 12px; font-weight: 600; color: var(--text-subtle); text-transform: uppercase; letter-spacing: .04em; }
    .table--responsive td:first-child { padding-bottom: 10px; }
    .table--responsive td:first-child::before { display: none; }

    .permission { flex-direction: column; align-items: stretch; }
    .input--compact { width: 100%; }
    .check { grid-template-columns: 14px minmax(0, 1fr) auto; }
    .check__value { grid-column: 2 / -1; }
    .recovery-codes { grid-template-columns: minmax(0, 1fr); }
    .copy-field { flex-direction: column; }
    .auth__card { padding: 24px 20px; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}
