/* Estruturas que evitam estouro de largura entre módulos. */
.portal-shell {
    position: relative;
    display: flex;
    min-height: 100vh;
}

/* O posicionamento estrutural do portal não pode depender da disponibilidade
   do CDN de utilitários. Estas regras são o fallback mínimo do shell. */
#sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    display: flex;
    width: 17rem;
    flex-direction: column;
    transform: translateX(-100%);
    transition: width 180ms ease, transform 180ms ease;
}

#sidebar:not(.-translate-x-full) { transform: translateX(0); }

#sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(2, 6, 23, 0.7);
    backdrop-filter: blur(4px);
}

#sidebar-overlay.hidden { display: none !important; }

/* Fallback sem CDN: não pode prevalecer sobre lg:block, xl:block etc. */
:where(.hidden) { display: none; }

.portal-body.overflow-hidden { overflow: hidden; }

#app-main {
    width: 100%;
    min-width: 0;
    flex: 1 1 0%;
    padding: 5rem 1rem 2rem;
    transition: margin 180ms ease, padding 180ms ease;
}

#toggle-sidebar {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 60;
    display: inline-flex;
    width: 3rem;
    height: 3rem;
    align-items: center;
    justify-content: center;
}

#collapse-sidebar { display: none; }

@media (min-width: 1024px) {
    #collapse-sidebar { display: inline-flex; }
    #sidebar { transform: translateX(0) !important; }
    #app-main { margin-left: 17rem; padding: 2rem; }
    #toggle-sidebar,
    #sidebar-overlay { display: none !important; }
}

.portal-body {
    min-height: 100vh;
    overflow-x: hidden;
    background: var(--portal-bg);
}

.portal-background-glow-top {
    position: fixed;
    inset: 0 0 auto;
    height: 22rem;
    pointer-events: none;
    background: var(--portal-glow-top);
    opacity: 0.9;
    z-index: -1;
}

.portal-background-glow-left,
.portal-background-glow-right {
    position: fixed;
    width: 20rem;
    height: 20rem;
    border-radius: 50%;
    pointer-events: none;
    filter: blur(30px);
    opacity: 0.55;
    z-index: -1;
}

.portal-background-glow-left {
    left: -8rem;
    top: 36%;
    background: var(--portal-glow-left);
}

.portal-background-glow-right {
    right: -8rem;
    top: 48%;
    background: var(--portal-glow-right);
}

.portal-background-grid {
    position: fixed;
    inset: 0;
    pointer-events: none;
    background: var(--portal-grid);
    opacity: 0.7;
    z-index: -1;
}

html[data-theme="light"] .portal-background { opacity: 0.48; }

.portal-flash-card,
.portal-sidebar,
.portal-sidebar-brand,
.portal-menu-section,
.portal-user-card,
.portal-mobile-toggle,
.portal-icon-button,
.portal-sidebar {
    background:
        linear-gradient(180deg, rgba(15, 23, 42, 0.06), transparent 20%),
        var(--portal-bg-elevated);
    border-right: 1px solid var(--portal-border);
}

html[data-theme="light"] .portal-sidebar {
    background:
        linear-gradient(180deg, rgba(32, 47, 72, 0.96), rgba(19, 31, 50, 0.98)),
        var(--portal-bg-elevated);
    border-right-color: rgba(255, 255, 255, 0.06);
    box-shadow: 0 28px 60px rgba(9, 17, 31, 0.24);
}

html[data-theme="light"] .portal-sidebar-brand,
html[data-theme="light"] .portal-menu-section,
html[data-theme="light"] .portal-user-card {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(255, 255, 255, 0.08);
    box-shadow: none;
}

.portal-sidebar-brand { background: linear-gradient(135deg, var(--portal-card), var(--portal-card-muted)); }
.portal-menu-section,
.portal-user-card { background: var(--portal-card-muted); }

html[data-theme="light"] .portal-sidebar .text-white,
html[data-theme="light"] .portal-sidebar .text-slate-100,
html[data-theme="light"] .portal-sidebar .text-slate-200,
html[data-theme="light"] .portal-sidebar .text-slate-300 { color: #f8fbff !important; }

html[data-theme="light"] .portal-sidebar .text-slate-400,
html[data-theme="light"] .portal-sidebar .text-slate-500 { color: rgba(215, 227, 242, 0.76) !important; }

html[data-theme="light"] .portal-sidebar [class*="bg-white/5"]:not(.menu-group-panel > a),
html[data-theme="light"] .portal-sidebar [class*="bg-white/[0.04]"],
html[data-theme="light"] .portal-sidebar [class*="bg-white/[0.03]"] { background: rgba(255, 255, 255, 0.06) !important; }

html[data-theme="light"] .portal-sidebar [class*="border-white/10"] { border-color: rgba(255, 255, 255, 0.08) !important; }

html[data-theme="light"] .portal-sidebar .hover\:bg-white\/5:hover:not(.menu-group-panel > a),
html[data-theme="light"] .portal-sidebar .hover\:bg-white\/10:hover {
    background: rgba(255, 255, 255, 0.1) !important;
    color: #ffffff !important;
}

html[data-theme="light"] .portal-sidebar .sidebar-home-link,
html[data-theme="light"] .portal-sidebar .menu-service-link,
html[data-theme="light"] .portal-sidebar .sidebar-admin-link,
html[data-theme="light"] .portal-sidebar .sidebar-login-link,
html[data-theme="light"] .portal-sidebar .sidebar-logout-link,
html[data-theme="light"] .portal-sidebar .menu-group-toggle { border: 1px solid transparent; }

html[data-theme="light"] .portal-sidebar .sidebar-home-link:hover,
html[data-theme="light"] .portal-sidebar .menu-service-link:hover,
html[data-theme="light"] .portal-sidebar .sidebar-admin-link:hover,
html[data-theme="light"] .portal-sidebar .sidebar-login-link:hover,
html[data-theme="light"] .portal-sidebar .sidebar-logout-link:hover,
html[data-theme="light"] .portal-sidebar .menu-group-toggle:hover { border-color: rgba(255, 255, 255, 0.08); }

/* Densidade da navegação: preservar espaço para os serviços e o rodapé. */
.portal-sidebar {
    padding: var(--ui-space-3);
}

.portal-sidebar .sidebar-brand {
    flex-shrink: 0;
    margin-bottom: var(--ui-space-3);
    padding: var(--ui-space-2);
    gap: var(--ui-space-2);
}

.portal-sidebar > nav {
    min-height: 0;
}

.portal-sidebar > nav > :not([hidden]) ~ :not([hidden]) {
    margin-top: var(--ui-space-1);
    margin-bottom: 0;
}

.portal-sidebar .portal-menu-section {
    padding: var(--ui-space-1);
}

.portal-sidebar .sidebar-home-link,
.portal-sidebar > nav > .menu-service-link,
.portal-sidebar .sidebar-admin-link,
.portal-sidebar .menu-group-toggle {
    min-height: var(--ui-control-min-height);
    padding: var(--ui-space-1) var(--ui-space-2);
    gap: var(--ui-space-2);
    font-size: 0.8125rem;
    line-height: 1.3;
}

.portal-sidebar .sidebar-home-link > span:first-child,
.portal-sidebar > nav > .menu-service-link > span:first-child,
.portal-sidebar .sidebar-admin-link > span:first-child,
.portal-sidebar .menu-group-toggle > span:first-child {
    flex-shrink: 0;
    width: 2rem;
    height: 2rem;
}

.portal-sidebar .sidebar-item-copy {
    min-width: 0;
    overflow-wrap: anywhere;
}

/* Revelar o conteúdo apenas depois de a sidebar recuperar sua largura.
   Os submenus ficam fora da animação para abrir seus rótulos imediatamente. */
@keyframes sidebar-content-reveal {
    from { opacity: 0; visibility: hidden; }
    to { opacity: 1; visibility: visible; }
}

@media (min-width: 1024px) and (prefers-reduced-motion: no-preference) {
    html:not(.sidebar-collapsed) body:not(.sidebar-collapsed) #sidebar :is(
        .sidebar-brand-copy,
        .sidebar-item-copy:not(.menu-group-panel *),
        .sidebar-user-copy,
        .sidebar-login-label,
        .sidebar-theme-copy,
        .sidebar-theme-chevron,
        .menu-group-icon
    ) {
        animation: sidebar-content-reveal 80ms ease-out 180ms both;
    }
}

@media (prefers-reduced-motion: reduce) {
    #sidebar,
    #app-main { transition: none; }
}

.portal-sidebar .sidebar-footer {
    flex-shrink: 0;
}

/* Hierarquia compartilhada pelos grupos que possuem submenus. */
.portal-sidebar .menu-group-toggle {
    font-weight: var(--ui-weight-semibold);
}

.portal-sidebar .menu-group-panel {
    position: relative;
    margin-top: var(--ui-space-1);
    margin-left: 1.5rem;
    padding-left: var(--ui-space-3);
    border-left: 1px solid rgba(148, 163, 184, 0.22);
}

.portal-sidebar .menu-group-panel > .menu-service-link {
    position: relative;
    min-height: 2.375rem;
    padding: 0.375rem 0.5rem;
    gap: var(--ui-space-2);
    border: 1px solid transparent;
    border-radius: var(--ui-radius-sm);
    background: transparent;
    color: #cbd5e1;
    font-size: 0.78rem;
    line-height: 1.25;
    box-shadow: none;
}

.portal-sidebar .menu-group-panel > .menu-service-link::before,
.portal-sidebar .menu-group-panel > .menu-service-link::after {
    content: "";
    position: absolute;
    top: 50%;
    pointer-events: none;
    background: rgba(148, 163, 184, 0.35);
    transform: translateY(-50%);
}

.portal-sidebar .menu-group-panel > .menu-service-link::before {
    left: calc(-0.75rem - 2px);
    width: 0.625rem;
    height: 1px;
}

.portal-sidebar .menu-group-panel > .menu-service-link::after {
    left: calc(-0.75rem - 4px);
    width: 5px;
    height: 5px;
    border-radius: 50%;
}

.portal-sidebar .menu-group-panel > .menu-service-link > .bi {
    flex: 0 0 0.875rem;
    font-size: var(--ui-text-xs);
    opacity: 0.7;
}

.portal-sidebar .menu-group-panel > .menu-service-link:hover {
    background: rgba(255, 255, 255, 0.035);
}

/* A classe existente é atualizada tanto pelo Jinja quanto pela navegação SPA. */
.portal-sidebar .menu-group-panel > .menu-service-link.bg-white\/10 {
    background: rgba(14, 165, 233, 0.15);
    color: #fff;
    font-weight: var(--ui-weight-medium);
}

.portal-sidebar .menu-group-panel > .menu-service-link.bg-white\/10::after {
    background: #22d3ee;
}

.portal-sidebar .menu-group-panel > .menu-service-link:focus-visible {
    outline: 2px solid #22d3ee;
    outline-offset: 2px;
}

/* As regras globais de tema em base.css usam !important e também casam
   com a classe de hover. Neutralizar somente nos links de submenu. */
html[data-theme="light"] .portal-sidebar .menu-group-panel > .menu-service-link {
    background: transparent !important;
    color: #cbd5e1 !important;
}

html[data-theme="light"] .portal-sidebar .menu-group-panel > .menu-service-link:hover {
    background: rgba(255, 255, 255, 0.035) !important;
}

html[data-theme="light"] .portal-sidebar .menu-group-panel > .menu-service-link.bg-white\/10 {
    background: rgba(14, 165, 233, 0.15) !important;
    color: #fff !important;
}

.sidebar-footer {
    margin-top: 0.55rem;
    padding-top: 0.55rem;
}

.sidebar-footer .portal-user-card { min-height: 3.5rem; padding: 0.5rem 0.7rem; }

.sidebar-footer .portal-user-card .h-10.w-10,
.sidebar-footer .portal-theme-toggle .h-10.w-10 {
    height: 2.25rem;
    width: 2.25rem;
    border-radius: 0.75rem;
}

.sidebar-footer-actions { margin-top: 0.45rem; }
.sidebar-footer .portal-theme-toggle { margin-top: 0; min-height: 3rem; padding: 0.45rem 0.65rem; }
.sidebar-footer .sidebar-logout-link { margin-top: 0; min-height: 3rem; padding: 0; }

.sidebar-footer .portal-user-card .text-sm,
.sidebar-footer .portal-theme-toggle { line-height: 1.15; }

.sidebar-footer .portal-user-card .text-xs,
.sidebar-footer #theme-toggle-label { font-size: 0.72rem; }

.portal-main,
.portal-main > section,
.admin-shell,
.emissor-page,
.portal-responsive-main-card { min-width: 0; max-width: 100%; }

.portal-main,
.portal-main > section,
.admin-shell { overflow-wrap: anywhere; }

.portal-responsive-main-card,
.admin-shell-card,
.dashboard-panel { width: 100%; }

.portal-main { color: var(--portal-text-primary); overflow-x: clip; }

.portal-responsive-hero {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
}

.portal-responsive-hero-copy { min-width: 0; flex: 1; }

.portal-responsive-upload-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.portal-responsive-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.portal-responsive-actions > * { width: 100%; }

.portal-responsive-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
}

.portal-responsive-frame-shell { min-height: calc(100svh - 6rem); }

@media (min-width: 1024px) {
    .portal-main.portal-main-emissor { padding-top: 1rem !important; }
}

.history-toolbar-button {
    min-height: 2.25rem;
    padding-inline: 0.85rem;
    border-radius: 0.95rem;
}

.history-filter-actions { align-items: center; margin-top: 0.35rem; }
.history-filter-submit { min-width: 8.5rem; justify-content: center; }
.history-filter-tip { border-radius: 1rem; }

@media (max-width: 767px) {
    .history-toolbar { align-items: stretch; }
    .history-filter-submit,
    .history-filter-reset { width: 100%; justify-content: center; }
    .history-filter-tip { width: 100%; }
}

@media (min-width: 640px) {
    .portal-responsive-actions {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
    }

    .portal-responsive-actions > * { width: auto; }
}

.dashboard-metrics-grid,
.admin-metric-grid,
.dashboard-overview-grid,
.portal-responsive-stats { min-width: 0; }

.dashboard-metric-card,
.admin-metric-card,
.dashboard-overview-grid > *,
.portal-responsive-sidepanel { min-width: 0; }

.dashboard-metric-value,
.dashboard-metric-helper,
.admin-metric-card p,
.portal-responsive-main-card p { overflow-wrap: anywhere; }

/* Estado visual da sidebar colapsada. */
html.sidebar-collapsed .portal-theme-toggle,
body.sidebar-collapsed .portal-theme-toggle {
    width: 3.25rem;
    min-width: 3.25rem;
    margin-inline: auto;
    padding-left: 0;
    padding-right: 0;
}

html.sidebar-collapsed .portal-theme-toggle > span,
body.sidebar-collapsed .portal-theme-toggle > span {
    gap: 0;
}

html.sidebar-collapsed .portal-user-card,
body.sidebar-collapsed .portal-user-card {
    width: 3.25rem;
    min-width: 3.25rem;
    min-height: 3.25rem;
    margin-inline: auto;
    justify-content: center;
    padding-left: 0;
    padding-right: 0;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

html.sidebar-collapsed .sidebar-logout-link,
body.sidebar-collapsed .sidebar-logout-link {
    width: 3.25rem;
    min-width: 3.25rem;
    min-height: 3.25rem;
    margin-inline: auto;
    justify-content: center;
    padding-left: 0;
    padding-right: 0;
    padding-top: 0;
    padding-bottom: 0;
}

html.sidebar-collapsed .sidebar-logout-link i,
body.sidebar-collapsed .sidebar-logout-link i {
    margin-right: 0;
}

html.sidebar-collapsed .sidebar-footer,
body.sidebar-collapsed .sidebar-footer {
    margin-top: 0.55rem;
    padding-top: 0.55rem;
    display: flex;
    flex-direction: column;
    align-items: center;
}

html.sidebar-collapsed .sidebar-footer-actions,
body.sidebar-collapsed .sidebar-footer-actions {
    width: 100%;
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 0.45rem;
    margin-top: 0.45rem;
}

html.sidebar-collapsed .portal-user-card .h-10.w-10,
body.sidebar-collapsed .portal-user-card .h-10.w-10,
html.sidebar-collapsed .portal-theme-toggle .h-10.w-10,
body.sidebar-collapsed .portal-theme-toggle .h-10.w-10 {
    height: 2.5rem;
    width: 2.5rem;
    border-radius: 0.95rem;
}
