:root {
    --app-bg: #f4f8f8;
    --surface: #ffffff;
    --surface-soft: #f8fbfb;
    --surface-raised: #ffffff;
    --sidebar-bg: #073b3c;
    --sidebar-bg-2: #0a4a4b;
    --sidebar-text: #d8eeee;
    --sidebar-muted: #83aaab;
    --text-primary: #213536;
    --text-secondary: #718183;
    --border: #dce8e8;
    --accent: #009b9d;
    --accent-dark: #007f81;
    --accent-soft: #e7f8f8;
    --danger: #d84f4f;
    --warning: #b98418;
    --success: #18885c;
    --shadow-sm: 0 7px 22px rgba(29, 67, 69, .07);
    --shadow-md: 0 16px 40px rgba(24, 62, 64, .12);
    --sidebar-width: 272px;
    --sidebar-collapsed-width: 86px;
    --topbar-height: 88px;
    --radius-sm: 11px;
    --radius-md: 17px;
    --radius-lg: 23px;
    color-scheme: light;
}

html[data-theme="dark"] {
    --app-bg: #0f1718;
    --surface: #172122;
    --surface-soft: #1c2829;
    --surface-raised: #202c2d;
    --sidebar-bg: #081f20;
    --sidebar-bg-2: #0b2b2c;
    --sidebar-text: #e4f3f3;
    --sidebar-muted: #84a2a4;
    --text-primary: #edf6f6;
    --text-secondary: #9aabad;
    --border: #2b3b3c;
    --accent: #19b6b8;
    --accent-dark: #3cc7c9;
    --accent-soft: #123536;
    --danger: #ff7777;
    --warning: #e3ad43;
    --success: #45bd87;
    --shadow-sm: 0 8px 24px rgba(0, 0, 0, .2);
    --shadow-md: 0 18px 48px rgba(0, 0, 0, .32);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html { min-height: 100%; scroll-behavior: smooth; }
body {
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0;
    color: var(--text-primary);
    background: var(--app-bg);
    font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    transition: background-color .22s ease, color .22s ease;
}
h1, h2, h3, h4, h5, h6 { font-family: "Manrope", "Inter", sans-serif; }
a { text-underline-offset: 3px; }

.app-shell { min-height: 100vh; }
.app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 1045;
    display: flex;
    width: var(--sidebar-width);
    flex-direction: column;
    overflow: hidden;
    color: var(--sidebar-text);
    background: linear-gradient(180deg, var(--sidebar-bg), var(--sidebar-bg-2));
    box-shadow: 12px 0 34px rgba(3, 40, 41, .14);
    transition: width .24s ease, transform .24s ease;
}
.sidebar-brand {
    display: flex;
    min-height: var(--topbar-height);
    align-items: center;
    gap: 9px;
    padding: 13px 14px;
    border-bottom: 1px solid rgba(255,255,255,.09);
}

.sidebar-brand-link {
    display: flex;
    min-width: 0;
    flex: 1;
    align-items: center;
    gap: 10px;
    overflow: hidden;
    color: #fff;
    text-decoration: none;
}

.sidebar-brand-link:hover,
.sidebar-brand-link:focus {
    color: #fff;
    text-decoration: none;
}

.sidebar-logo {
    display: block;
    width: 72px;
    height: 50px;
    flex: 0 0 72px;
    object-fit: contain;
    object-position: center;
}

.sidebar-brand-copy {
    display: flex;
    min-width: 0;
    flex: 1;
    flex-direction: column;
    justify-content: center;
    overflow: hidden;
}

.sidebar-brand-copy strong {
    display: block;
    overflow: hidden;
    color: #fff;
    font-family: "Manrope", "Inter", sans-serif;
    font-size: .92rem;
    font-weight: 800;
    line-height: 1.12;
    letter-spacing: -.018em;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-brand-copy small {
    display: block;
    overflow: hidden;
    margin-top: 5px;
    color: var(--sidebar-muted);
    font-size: .59rem;
    font-weight: 750;
    line-height: 1;
    letter-spacing: .09em;
    text-overflow: ellipsis;
    text-transform: uppercase;
    white-space: nowrap;
}

.sidebar-collapse-button,
.topbar-menu-button,
.topbar-icon-button {
    display: inline-flex;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 11px;
    transition: transform .16s ease, background .16s ease, color .16s ease;
}

.sidebar-collapse-button {
    flex: 0 0 38px;
    color: var(--sidebar-muted);
    background: rgba(255,255,255,.06);
}

.sidebar-collapse-button:hover {
    color: #fff;
    background: rgba(255,255,255,.13);
}

.sidebar-nav { flex: 1; overflow-x: hidden; overflow-y: auto; padding: 16px 12px; scrollbar-width: thin; scrollbar-color: rgba(255,255,255,.18) transparent; }
.sidebar-section + .sidebar-section { margin-top: 18px; }
.sidebar-section-label { display: block; padding: 0 12px 7px; color: var(--sidebar-muted); font-size: .61rem; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; white-space: nowrap; }
.sidebar-link {
    position: relative; display: flex; min-height: 44px; align-items: center; gap: 12px; margin: 3px 0; padding: 10px 12px;
    color: var(--sidebar-text); border-radius: 12px; font-size: .79rem; font-weight: 650; text-decoration: none;
    transition: color .16s ease, background .16s ease, transform .16s ease;
}
.sidebar-link i { width: 21px; flex: 0 0 21px; color: var(--sidebar-muted); font-size: .98rem; text-align: center; }
.sidebar-link:hover { transform: translateX(2px); color: #fff; background: rgba(255,255,255,.08); }
.sidebar-link:hover i { color: #fff; }
.sidebar-link.active { color: #fff; background: linear-gradient(135deg, rgba(25,182,184,.36), rgba(25,182,184,.16)); box-shadow: inset 3px 0 0 #31d0d2; }
.sidebar-link.active i { color: #65e1e2; }
.sidebar-profile { display: flex; align-items: center; gap: 10px; margin: 12px; padding: 12px; border: 1px solid rgba(255,255,255,.09); border-radius: 15px; background: rgba(255,255,255,.06); }
.profile-avatar, .topbar-user-avatar { display: inline-flex; align-items: center; justify-content: center; color: #fff; background: linear-gradient(135deg, var(--accent), #22c5c7); font-weight: 800; }
.profile-avatar { width: 38px; height: 38px; flex: 0 0 38px; border-radius: 11px; }
.profile-copy { display: flex; min-width: 0; flex: 1; flex-direction: column; }
.profile-copy strong { overflow: hidden; color: #fff; font-size: .75rem; text-overflow: ellipsis; white-space: nowrap; }
.profile-copy small { margin-top: 2px; color: var(--sidebar-muted); font-size: .62rem; }
.profile-logout { display: inline-flex; width: 32px; height: 32px; align-items: center; justify-content: center; color: var(--sidebar-muted); border-radius: 9px; text-decoration: none; }
.profile-logout:hover { color: #fff; background: rgba(216,79,79,.22); }

.app-main { min-height: 100vh; margin-left: var(--sidebar-width); transition: margin-left .24s ease; }
.app-topbar {
    position: sticky; top: 0; z-index: 1035; display: flex; min-height: calc(var(--topbar-height) + env(safe-area-inset-top)); align-items: center; justify-content: space-between; gap: 20px;
    padding: calc(14px + env(safe-area-inset-top)) calc(26px + env(safe-area-inset-right)) 14px calc(26px + env(safe-area-inset-left)); border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(16px);
}
.topbar-left, .topbar-actions { display: flex; align-items: center; }
.topbar-left { min-width: 0; gap: 12px; }
.topbar-actions { gap: 10px; }
.topbar-menu-button { display: none; color: var(--text-primary); background: var(--surface-soft); }
.topbar-heading { min-width: 0; }
.topbar-eyebrow { color: var(--accent); font-size: .6rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.topbar-heading h1 { margin: 2px 0 0; color: var(--text-primary); font-size: 1.25rem; font-weight: 800; letter-spacing: -.025em; }
.topbar-heading p { margin: 2px 0 0; color: var(--text-secondary); font-size: .68rem; }
.topbar-icon-button { color: var(--text-secondary); border: 1px solid var(--border); background: var(--surface); }
.topbar-icon-button:hover { transform: translateY(-1px); color: var(--accent); border-color: var(--accent); }
.theme-icon-light { display: none; }
html[data-theme="dark"] .theme-icon-dark { display: none; }
html[data-theme="dark"] .theme-icon-light { display: inline-block; }
.topbar-user-button { display: flex; align-items: center; gap: 9px; padding: 5px 9px 5px 5px; color: var(--text-primary); border: 1px solid var(--border); border-radius: 13px; background: var(--surface); }
.topbar-user-avatar { width: 35px; height: 35px; border-radius: 10px; font-size: .76rem; }
.topbar-user-copy { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.1; }
.topbar-user-copy strong { font-size: .7rem; }
.topbar-user-copy small { margin-top: 3px; color: var(--text-secondary); font-size: .58rem; }
.app-dropdown-menu { padding: 7px; border-color: var(--border); border-radius: 13px; background: var(--surface); box-shadow: var(--shadow-md); }
.app-dropdown-menu .dropdown-item { display: flex; align-items: center; gap: 8px; padding: 9px 10px; color: var(--text-primary); border-radius: 9px; font-size: .72rem; }
.app-dropdown-menu .dropdown-item:hover { background: var(--accent-soft); }

.app-content { width: 100%; max-width: 1640px; min-height: calc(100vh - var(--topbar-height) - 58px); margin: 0 auto; padding: 22px 26px 38px; }
.app-footer { display: flex; align-items: center; justify-content: center; gap: 9px; padding: 17px 24px; color: var(--text-secondary); border-top: 1px solid var(--border); font-size: .66rem; }
.app-footer > span:first-child { color: var(--accent); font-weight: 800; }
.app-footer-dot { width: 4px; height: 4px; border-radius: 50%; background: var(--border); }

.app-shell.sidebar-collapsed .app-sidebar { width: var(--sidebar-collapsed-width); }
.app-shell.sidebar-collapsed .app-main { margin-left: var(--sidebar-collapsed-width); }
.app-shell.sidebar-collapsed .sidebar-brand-copy,
.app-shell.sidebar-collapsed .sidebar-section-label,
.app-shell.sidebar-collapsed .sidebar-link span,
.app-shell.sidebar-collapsed .profile-copy,
.app-shell.sidebar-collapsed .profile-logout { display: none; }
.app-shell.sidebar-collapsed .sidebar-brand { justify-content: center; padding-inline: 10px; }
.app-shell.sidebar-collapsed .sidebar-brand-link { flex: 0 0 auto; }
.app-shell.sidebar-collapsed .sidebar-logo { width: 46px; height: 42px; flex: 0 0 46px; }
.app-shell.sidebar-collapsed .sidebar-collapse-button { position: absolute; top: 66px; right: 8px; width: 28px; height: 28px; border: 1px solid rgba(255,255,255,.1); }
.app-shell.sidebar-collapsed .sidebar-collapse-button i { transform: rotate(180deg); }
.app-shell.sidebar-collapsed .sidebar-link { justify-content: center; padding-inline: 0; }
.app-shell.sidebar-collapsed .sidebar-link i { width: auto; flex-basis: auto; }
.app-shell.sidebar-collapsed .sidebar-profile { justify-content: center; padding: 8px; }

.flash-stack { position: relative; z-index: 5; margin-bottom: 14px; }
.app-alert { display: flex; align-items: center; gap: 10px; border: 0; border-radius: 14px; box-shadow: var(--shadow-sm); font-size: .76rem; }
.app-alert-icon { font-size: 1rem; }
.app-alert-message { flex: 1; }

.public-shell { min-height: 100vh; padding: 18px; background: var(--app-bg); }
.public-content { width: 100%; }

.page-enter { animation: pageEnter .34s ease both; }
@keyframes pageEnter { from { opacity: 0; transform: translateY(7px); } to { opacity: 1; transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; } }

/* Compatibilidad global con Bootstrap y plantillas existentes */
.card, .modal-content, .dropdown-menu, .list-group-item { color: var(--text-primary); border-color: var(--border); background-color: var(--surface); }
.table { --bs-table-color: var(--text-primary); --bs-table-bg: transparent; --bs-table-border-color: var(--border); --bs-table-hover-bg: var(--accent-soft); }
.form-control, .form-select, .input-group-text { color: var(--text-primary); border-color: var(--border); background-color: var(--surface); }
.form-control:focus, .form-select:focus { color: var(--text-primary); border-color: var(--accent); background-color: var(--surface); box-shadow: 0 0 0 .2rem color-mix(in srgb, var(--accent) 15%, transparent); }
.form-control::placeholder { color: var(--text-secondary); opacity: .8; }
.text-muted { color: var(--text-secondary) !important; }
.text-primary { color: var(--accent) !important; }
.bg-primary { background-color: var(--accent) !important; }
.btn-primary { color: #fff; border-color: var(--accent); background: var(--accent); }
.btn-primary:hover, .btn-primary:focus { color: #fff; border-color: var(--accent-dark); background: var(--accent-dark); }
.btn-outline-primary { color: var(--accent); border-color: var(--accent); }
.btn-outline-primary:hover { color: #fff; border-color: var(--accent); background: var(--accent); }

.sidebar-backdrop { display: none; }

@media (max-width: 1199px) {
    :root { --sidebar-width: 248px; }
    .app-content { padding-inline: 20px; }
}

@media (max-width: 991px) {
    .app-sidebar { width: min(286px, 86vw); transform: translateX(-104%); }
    .app-main, .app-shell.sidebar-collapsed .app-main { margin-left: 0; }
    .app-shell.mobile-sidebar-open .app-sidebar { transform: translateX(0); }
    .app-shell.sidebar-collapsed .app-sidebar { width: min(286px, 86vw); }
    .app-shell.sidebar-collapsed .sidebar-brand-copy,
    .app-shell.sidebar-collapsed .sidebar-section-label,
    .app-shell.sidebar-collapsed .sidebar-link span,
    .app-shell.sidebar-collapsed .profile-copy,
    .app-shell.sidebar-collapsed .profile-logout { display: initial; }
    .app-shell.sidebar-collapsed .sidebar-link { justify-content: flex-start; padding-inline: 12px; }
    .app-shell.sidebar-collapsed .sidebar-profile { justify-content: flex-start; padding: 12px; }
    .sidebar-collapse-button { display: none; }
    .topbar-menu-button { display: inline-flex; }
    .sidebar-backdrop { position: fixed; inset: 0; z-index: 1040; background: rgba(1, 22, 23, .54); backdrop-filter: blur(2px); opacity: 0; pointer-events: none; transition: opacity .2s ease; }
    .app-shell.mobile-sidebar-open + .sidebar-backdrop { display: block; opacity: 1; pointer-events: auto; }
}

@media (max-width: 575px) {
    :root { --topbar-height: 74px; }
    .app-topbar {
        min-height: calc(var(--topbar-height) + env(safe-area-inset-top));
        padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 10px calc(12px + env(safe-area-inset-left));
    }
    .topbar-menu-button, .topbar-icon-button { width: 44px; height: 44px; flex: 0 0 44px; }
    .topbar-user-avatar { width: 40px; height: 40px; }
    .topbar-heading p, .topbar-eyebrow, .topbar-user-copy { display: none; }
    .topbar-heading h1 { font-size: 1rem; }
    .topbar-user-button { padding: 4px; }
    .topbar-user-button::after { display: none; }
    .app-content { padding: 14px 12px 28px; }
    .app-footer { flex-wrap: wrap; padding-inline: 12px; text-align: center; }
    .public-shell { padding: 0; }
}


@media (max-width: 575px) {
    .sidebar-logo {
        width: 68px;
        height: 47px;
        flex-basis: 68px;
    }

    .sidebar-brand-copy strong {
        font-size: .88rem;
    }

    .sidebar-brand-copy small {
        font-size: .56rem;
        letter-spacing: .075em;
    }
}

/* iPhone / PWA safe-area corrections */
@supports (padding: max(0px)) {
    .app-sidebar { padding-bottom: env(safe-area-inset-bottom); }
    @media (max-width: 991px) {
        .sidebar-brand {
            min-height: calc(var(--topbar-height) + env(safe-area-inset-top));
            padding-top: calc(14px + env(safe-area-inset-top));
        }
    }
}
