/* Sistema global de temas. Las páginas consumen tokens semánticos de variables.css. */
html { background: var(--color-bg); }
body { background-color: var(--color-bg); color: var(--color-text-primary); }

.form-control,
.form-select,
.form-check-input,
.input-group-text {
    color: var(--color-text-primary);
    border-color: var(--control-border);
    background-color: var(--control-bg);
}
.form-control:hover:not(:disabled):not([readonly]),
.form-select:hover:not(:disabled) { border-color: var(--control-hover-border); }
.form-control::placeholder { color: var(--color-placeholder); opacity: 1; }
.form-control:disabled,
.form-select:disabled,
.form-control[readonly] { color: var(--color-text-secondary); background-color: var(--color-input-disabled); }
.form-control:focus,
.form-select:focus,
.form-check-input:focus {
    border-color: var(--control-focus-border);
    background-color: var(--control-bg);
    color: var(--color-text-primary);
    box-shadow: 0 0 0 3px var(--control-focus-ring);
}

.form-check-input:checked,
.form-check-input:indeterminate,
.form-switch .form-check-input:checked {
    border-color: var(--control-primary-bg);
    background-color: var(--control-primary-bg);
}

.form-check-input:checked:focus,
.form-check-input:indeterminate:focus {
    border-color: var(--control-primary-bg);
    background-color: var(--control-primary-bg);
}

input[type="checkbox"],
input[type="radio"],
input[type="range"] { accent-color: var(--control-primary-bg); }

.form-range::-webkit-slider-thumb { background-color: var(--control-primary-bg); }
.form-range::-moz-range-thumb { background-color: var(--control-primary-bg); }

.card,
.modal-content,
.dropdown-menu,
.list-group-item,
.accordion-item,
.accordion-button,
.offcanvas,
.toast,
.popover {
    color: var(--color-text-primary);
    border-color: var(--color-border);
    background-color: var(--color-surface);
}
.modal-header,
.modal-footer,
.card-header,
.card-footer,
.accordion-button:not(.collapsed),
.dropdown-item:hover,
.dropdown-item:focus,
.list-group-item-action:hover,
.list-group-item-action:focus {
    color: var(--color-text-primary);
    border-color: var(--color-border);
    background-color: var(--color-surface-hover);
}
.dropdown-item { color: var(--color-text-primary); }
.dropdown-divider { border-color: var(--color-divider); }
.btn-close { filter: var(--theme-close-filter, none); }

.alert-success { background: var(--color-alert-success-bg); color: var(--color-alert-success-text); }
.alert-danger { background: var(--color-alert-danger-bg); color: var(--color-alert-danger-text); }
.alert-warning { background: var(--color-alert-warning-bg); color: var(--color-alert-warning-text); }
.alert-info { background: var(--color-alert-info-bg); color: var(--color-alert-info-text); }

.bg-white,
.bg-light { background-color: var(--color-surface-hover) !important; }
.text-dark,
.text-black { color: var(--color-text-primary) !important; }
.text-muted { color: var(--color-text-muted) !important; }
.border,
.border-top,
.border-bottom,
.border-start,
.border-end { border-color: var(--color-border) !important; }

/* Bootstrap define estas utilidades con !important; se sobreescriben en el puente global. */
.text-primary,
.link-primary { color: var(--accent-text) !important; }
.bg-primary { background-color: var(--control-primary-bg) !important; }
.text-bg-primary {
    color: var(--control-primary-text) !important;
    background-color: var(--control-primary-bg) !important;
}
.border-primary { border-color: var(--accent-primary) !important; }
.btn-link { color: var(--accent-text); }
.btn-link:hover { color: var(--accent-text-hover); }

::selection {
    color: var(--color-text-primary);
    background: var(--accent-soft-hover);
}

a:focus-visible {
    outline: 3px solid var(--control-focus-ring);
    outline-offset: 2px;
}

.nav-tabs { border-color: var(--color-border); }
.nav-tabs .nav-link { color: var(--color-text-secondary); }
.nav-tabs .nav-link.active { color: var(--accent-text); border-color: var(--color-border) var(--color-border) var(--color-surface); background: var(--color-surface); }
.nav-pills .nav-link.active,
.nav-pills .show > .nav-link {
    color: var(--control-primary-text);
    background-color: var(--control-primary-bg);
}

.dropdown-item.active,
.dropdown-item:active,
.list-group-item.active {
    color: var(--accent-text);
    border-color: var(--accent-border);
    background-color: var(--accent-soft-hover);
}

.accordion-button:focus,
.dropdown-item:focus-visible,
.list-group-item-action:focus-visible,
.nav-link:focus-visible,
.page-link:focus-visible {
    outline: 3px solid var(--control-focus-ring);
    outline-offset: 2px;
    box-shadow: none;
}

/* Perfil y selector único de apariencia. */
.theme-profile { position: relative; z-index: 30; }
.theme-profile-trigger {
    display: flex; align-items: center; gap: 0.5rem; min-height: 2.5rem; padding: 0.2rem 0.45rem;
    border: 1px solid transparent; border-radius: var(--radius-pill); color: var(--color-text-primary);
    background: transparent; cursor: pointer; transition: background var(--transition), border-color var(--transition);
}
.theme-profile-trigger:hover,
.theme-profile-trigger[aria-expanded="true"] { border-color: var(--color-border); background: var(--color-surface-hover); }
.theme-profile-trigger:focus-visible { outline: 3px solid var(--color-focus-ring); outline-offset: 2px; }
.theme-profile-chevron { color: var(--color-text-muted); font-size: 0.65rem; }
.theme-menu-backdrop { position: fixed; inset: 0; z-index: 1; border: 0; background: transparent; }
.theme-menu {
    position: absolute; top: calc(100% + 0.55rem); right: 0; z-index: 2; width: min(310px, calc(100vw - 1.5rem));
    max-height: min(82dvh, 760px); overflow-y: auto;
    padding: 0.55rem; border: 1px solid var(--color-border); border-radius: var(--radius-lg);
    color: var(--color-text-primary); background: var(--color-surface); box-shadow: var(--shadow-lg);
}
.theme-menu-header { display: flex; align-items: center; gap: 0.7rem; padding: 0.65rem 0.65rem 0.8rem; border-bottom: 1px solid var(--color-border); }
.theme-menu-header div { display: flex; min-width: 0; flex-direction: column; }
.theme-menu-header strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-menu-header small,
.theme-option small { color: var(--color-text-muted); font-size: 0.72rem; }
.theme-menu-label { padding: 0.75rem 0.65rem 0.35rem; color: var(--color-text-muted); font-size: 0.68rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.theme-option {
    width: 100%; display: grid; grid-template-columns: 2rem minmax(0, 1fr) 1.25rem; align-items: center;
    gap: 0.65rem; padding: 0.65rem; border: 0; border-radius: var(--radius-sm);
    color: var(--color-text-primary); background: transparent; text-align: left; cursor: pointer;
}
.theme-option:hover,
.theme-option:focus-visible { outline: none; background: var(--color-surface-hover); }
.theme-option.is-active { color: var(--accent-text); background: var(--accent-soft); }
.theme-option > span:nth-child(2) { display: flex; min-width: 0; flex-direction: column; }
.theme-option-icon { width: 2rem; height: 2rem; display: grid; place-items: center; border-radius: 50%; color: var(--accent-text); background: var(--color-surface-hover); }
.theme-option-check { justify-self: end; color: var(--accent-text); }
.theme-dark-variants {
    margin: 0.15rem 0.35rem 0.45rem 1rem;
    padding: 0.1rem 0 0.25rem 0.45rem;
    border-left: 2px solid var(--accent-border);
}
.theme-dark-variant-label { padding: 0.45rem 0.5rem 0.25rem; }
.theme-dark-option { padding: 0.48rem 0.5rem; grid-template-columns: 1.7rem minmax(0, 1fr) 1rem; gap: 0.5rem; }
.theme-dark-option .theme-option-icon { width: 1.7rem; height: 1.7rem; font-size: 0.8rem; }
.theme-dark-option strong { font-size: 0.8rem; }
.theme-dark-option small { font-size: 0.68rem; }
.theme-accent-label { margin-top: 0.35rem; border-top: 1px solid var(--color-border); }
.theme-accent-grid {
    display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0.45rem;
    padding: 0.35rem 0.65rem 0.65rem;
}
.theme-accent-option {
    position: relative; width: 100%; aspect-ratio: 1; min-width: 0; padding: 0.25rem;
    display: grid; place-items: center; border: 2px solid transparent; border-radius: 50%;
    color: #fff; background: transparent; cursor: pointer;
    transition: border-color var(--transition), background var(--transition), transform var(--transition);
}
.theme-accent-option:hover { transform: translateY(-1px); background: var(--color-surface-hover); }
.theme-accent-option:focus-visible { outline: 3px solid var(--color-focus-ring); outline-offset: 1px; }
.theme-accent-option.is-active { border-color: var(--color-text-primary); background: var(--color-surface-hover); }
.theme-accent-swatch {
    width: 100%; height: 100%; min-width: 1.65rem; min-height: 1.65rem;
    border: 1px solid color-mix(in srgb, var(--accent-swatch) 70%, var(--color-border));
    border-radius: 50%; background: var(--accent-swatch); box-shadow: var(--shadow-sm);
}
.theme-accent-check {
    position: absolute; inset: 50% auto auto 50%; transform: translate(-50%, -50%);
    color: #fff; font-size: 0.82rem; line-height: 1;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.75));
}

[data-theme="dark"] { --theme-close-filter: invert(1) grayscale(100%) brightness(180%); }
[data-theme="dark"] .topbar-username { color: var(--color-text-primary); }
[data-theme="dark"] img:not([src$=".svg"]) { opacity: 0.96; }

@media (max-width: 640.98px) {
    .theme-profile-trigger { padding-inline: 0.2rem; }
    .theme-profile-trigger .topbar-username,
    .theme-profile-chevron { display: none; }
    .theme-menu { position: fixed; top: var(--topbar-height); right: 0.75rem; }
}

@media print {
    :root { color-scheme: light !important; }
    body, .card, .table, .modal-content { color: #111 !important; background: #fff !important; }
    .theme-profile { display: none !important; }
}
