@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
    --ui-primary: 79 70 229;
    --ui-primary-hover: 67 56 202;
    --ui-success: 5 150 105;
    --ui-warning: 217 119 6;
    --ui-danger: 225 29 72;
    --ui-info: 2 132 199;
    --ui-radius: 0.75rem;
    --ui-shadow: 0 1px 2px rgb(15 23 42 / .06), 0 4px 14px rgb(15 23 42 / .05);
}

@layer base {
    html {
        scroll-behavior: smooth;
        font-size: 100%;
        text-size-adjust: 100%;
    }

    body { line-height: 1.5; }

    :focus-visible {
        outline: 3px solid rgb(79 70 229);
        outline-offset: 3px;
        border-radius: .25rem;
    }

    .dark :focus-visible { outline-color: rgb(165 180 252); }

    ::selection { @apply bg-indigo-200 text-slate-950; }

    button,
    a,
    input,
    select,
    textarea {
        touch-action: manipulation;
    }

    button:disabled,
    input:disabled,
    select:disabled,
    textarea:disabled {
        cursor: not-allowed;
        opacity: .6;
    }

    @media (max-width: 639px) {
        input:not([type='checkbox']):not([type='radio']),
        select,
        textarea {
            font-size: 16px;
        }
    }
}

.ui-tour-target {
    position: relative !important;
    z-index: 80 !important;
    border-radius: .75rem;
    outline: 4px solid rgb(129 140 248) !important;
    outline-offset: 5px;
    box-shadow: 0 0 0 9px rgb(255 255 255 / .92), 0 18px 50px rgb(15 23 42 / .3) !important;
}

.dark .ui-tour-target {
    box-shadow: 0 0 0 9px rgb(15 23 42 / .92), 0 18px 50px rgb(0 0 0 / .45) !important;
}

.app-shell-header:has(.ui-tour-target) { z-index: 80; }

@media (prefers-reduced-motion: no-preference) {
    .ui-tour-target { transition: outline-color .2s ease, box-shadow .2s ease; }
}

@layer components {
    .app-sidebar {
        color: rgb(51 65 85);
        background: linear-gradient(180deg, rgb(255 255 255) 0%, rgb(248 250 252) 100%);
        border-right: 1px solid rgb(226 232 240);
        box-shadow: 8px 0 30px rgb(15 23 42 / .06);
    }
    .dark .app-sidebar {
        color: rgb(226 232 240);
        background: linear-gradient(180deg, rgb(30 41 59) 0%, rgb(15 23 42) 100%);
        border-right-color: rgb(51 65 85);
        box-shadow: 8px 0 32px rgb(0 0 0 / .24);
    }
    .app-sidebar-brand {
        border-bottom: 1px solid rgb(226 232 240);
        background: rgb(255 255 255 / .88);
        backdrop-filter: blur(12px);
    }
    .dark .app-sidebar-brand { border-color: rgb(51 65 85); background: rgb(30 41 59 / .88); }
    .app-sidebar-title { color: rgb(15 23 42); }
    .app-sidebar-muted { color: rgb(100 116 139); }
    .dark .app-sidebar-title { color: white; }
    .dark .app-sidebar-muted { color: rgb(148 163 184); }
    .app-sidebar-profile { border-top: 1px solid rgb(226 232 240); background: rgb(248 250 252 / .92); }
    .dark .app-sidebar-profile { border-color: rgb(51 65 85); background: rgb(15 23 42 / .72); }
    .app-sidebar-logout { border: 1px solid rgb(254 205 211); background: rgb(255 241 242); color: rgb(190 18 60); }
    .app-sidebar-logout:hover { background: rgb(255 228 230); }
    .dark .app-sidebar-logout { border-color: rgb(159 18 57 / .65); background: rgb(136 19 55 / .22); color: rgb(253 164 175); }
    .dark .app-sidebar-logout:hover { background: rgb(159 18 57 / .32); }

    .app-sidebar nav input[type='search'] {
        border-color: rgb(203 213 225) !important;
        background: white !important;
        color: rgb(15 23 42) !important;
        box-shadow: 0 1px 2px rgb(15 23 42 / .04);
    }
    .app-sidebar nav input[type='search']::placeholder { color: rgb(100 116 139) !important; }
    .dark .app-sidebar nav input[type='search'] { border-color: rgb(71 85 105) !important; background: rgb(15 23 42 / .7) !important; color: white !important; }
    .dark .app-sidebar nav input[type='search']::placeholder { color: rgb(148 163 184) !important; }
    .app-sidebar nav > div[aria-label='Acciones rápidas'] a {
        border: 1px solid rgb(226 232 240);
        background: white !important;
        color: rgb(71 85 105) !important;
        box-shadow: 0 1px 2px rgb(15 23 42 / .04);
    }
    .app-sidebar nav > div[aria-label='Acciones rápidas'] a:hover { border-color: rgb(165 180 252); background: rgb(238 242 255) !important; color: rgb(67 56 202) !important; }
    .dark .app-sidebar nav > div[aria-label='Acciones rápidas'] a { border-color: rgb(51 65 85); background: rgb(51 65 85 / .55) !important; color: rgb(203 213 225) !important; }
    .dark .app-sidebar nav > div[aria-label='Acciones rápidas'] a:hover { border-color: rgb(99 102 241); background: rgb(49 46 129 / .55) !important; color: white !important; }
    .app-sidebar nav section > button { color: rgb(100 116 139) !important; }
    .app-sidebar nav section > button:hover { background: rgb(241 245 249) !important; color: rgb(51 65 85) !important; }
    .dark .app-sidebar nav section > button { color: rgb(148 163 184) !important; }
    .dark .app-sidebar nav section > button:hover { background: rgb(51 65 85 / .55) !important; color: white !important; }
    .app-sidebar nav section > button > span:first-of-type { border-color: rgb(203 213 225) !important; background: white; color: rgb(79 70 229) !important; }
    .dark .app-sidebar nav section > button > span:first-of-type { border-color: rgb(71 85 105) !important; background: rgb(30 41 59); color: rgb(165 180 252) !important; }
    .app-sidebar nav [class*='group/menu-item'] { color: rgb(71 85 105) !important; }
    .app-sidebar nav [class*='group/menu-item']:hover { background: rgb(238 242 255) !important; color: rgb(49 46 129) !important; }
    .dark .app-sidebar nav [class*='group/menu-item'] { color: rgb(203 213 225) !important; }
    .dark .app-sidebar nav [class*='group/menu-item']:hover { background: rgb(51 65 85 / .62) !important; color: white !important; }
    .app-sidebar nav [class*='group/menu-item']:has(a[aria-current='page']) {
        background: linear-gradient(90deg, rgb(79 70 229), rgb(99 102 241)) !important;
        color: white !important;
        box-shadow: 0 8px 20px rgb(79 70 229 / .2);
    }
    .app-sidebar nav [class*='group/menu-item'] a > span:first-child { background: rgb(238 242 255) !important; color: rgb(79 70 229); }
    .dark .app-sidebar nav [class*='group/menu-item'] a > span:first-child { background: rgb(51 65 85 / .8) !important; color: rgb(165 180 252); }
    .app-sidebar nav [class*='group/menu-item']:has(a[aria-current='page']) a > span:first-child { background: rgb(255 255 255 / .18) !important; color: white; }
    .app-sidebar nav h2 { color: rgb(100 116 139) !important; }
    .dark .app-sidebar nav h2 { color: rgb(148 163 184) !important; }
    .app-sidebar nav section[x-show*='recent'] a { background: rgb(238 242 255) !important; color: rgb(67 56 202) !important; }
    .dark .app-sidebar nav section[x-show*='recent'] a { background: rgb(49 46 129 / .55) !important; color: rgb(199 210 254) !important; }

    .ui-button {
        @apply inline-flex min-h-11 items-center justify-center gap-2 rounded-xl border px-4 py-2 text-sm font-bold shadow-sm transition disabled:pointer-events-none disabled:opacity-50;
    }
    .ui-button-primary { @apply border-indigo-600 bg-indigo-600 text-white hover:border-indigo-700 hover:bg-indigo-700 active:bg-indigo-800; }
    .ui-button-secondary { @apply border-slate-300 bg-white text-slate-800 hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100 dark:hover:bg-slate-700; }
    .ui-button-danger { @apply border-rose-600 bg-rose-600 text-white hover:bg-rose-700 active:bg-rose-800; }
    .ui-button-warning { @apply border-amber-600 bg-amber-600 text-white hover:bg-amber-700 active:bg-amber-800; }
    .ui-button-ghost { @apply border-transparent bg-transparent text-slate-700 shadow-none hover:bg-slate-100 dark:text-slate-200 dark:hover:bg-slate-800; }
    .ui-button-sm { @apply min-h-9 rounded-lg px-3 py-1.5 text-xs; }
    .ui-button-lg { @apply min-h-12 px-5 py-3 text-base; }

    .ui-field { @apply space-y-1.5; }
    .ui-label { @apply block text-sm font-bold text-slate-800 dark:text-slate-200; }
    .ui-control { @apply min-h-11 w-full rounded-xl border-slate-400 bg-white text-sm text-slate-950 placeholder:text-slate-500 shadow-sm focus:border-indigo-600 focus:ring-indigo-600 dark:border-slate-600 dark:bg-slate-900 dark:text-slate-100 dark:placeholder:text-slate-400 dark:focus:border-indigo-400 dark:focus:ring-indigo-400; }
    .ui-control-error { @apply border-rose-600 focus:border-rose-600 focus:ring-rose-600; }

    .ui-card { @apply rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-800; }
    .ui-card-header { @apply border-b border-slate-200 px-5 py-4 dark:border-slate-700; }
    .ui-card-body { @apply p-5; }

    .ui-badge { @apply inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-extrabold; }
    .ui-badge-neutral { @apply bg-slate-200 text-slate-800 dark:bg-slate-700 dark:text-slate-100; }
    .ui-badge-success { @apply bg-emerald-100 text-emerald-800 dark:bg-emerald-950 dark:text-emerald-300; }
    .ui-badge-warning { @apply bg-amber-100 text-amber-900 dark:bg-amber-950 dark:text-amber-300; }
    .ui-badge-danger { @apply bg-rose-100 text-rose-800 dark:bg-rose-950 dark:text-rose-300; }
    .ui-badge-info { @apply bg-sky-100 text-sky-800 dark:bg-sky-950 dark:text-sky-300; }

    .ui-alert { @apply flex gap-3 rounded-2xl border-l-4 p-4 text-sm shadow-sm; }
    .ui-alert-success { @apply border-emerald-600 bg-emerald-50 text-emerald-950 dark:bg-emerald-950/60 dark:text-emerald-100; }
    .ui-alert-error { @apply border-rose-600 bg-rose-50 text-rose-950 dark:bg-rose-950/60 dark:text-rose-100; }
    .ui-alert-warning { @apply border-amber-600 bg-amber-50 text-amber-950 dark:bg-amber-950/60 dark:text-amber-100; }
    .ui-alert-info { @apply border-sky-600 bg-sky-50 text-sky-950 dark:bg-sky-950/60 dark:text-sky-100; }

    .ui-tabs { @apply flex gap-1 overflow-x-auto border-b border-slate-200 dark:border-slate-700; }
    .ui-tab { @apply min-h-11 whitespace-nowrap border-b-2 border-transparent px-4 py-2 text-sm font-bold text-slate-600 hover:border-slate-300 hover:text-slate-950 dark:text-slate-300 dark:hover:text-white; }
    .ui-tab[aria-selected='true'] { @apply border-indigo-600 text-indigo-700 dark:border-indigo-400 dark:text-indigo-300; }

    [x-cloak], dialog:not([open]) { display: none !important; }
    .ui-dialog { @apply m-auto max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-lg overflow-y-auto rounded-2xl border-0 bg-white p-0 text-slate-950 shadow-2xl backdrop:bg-slate-950/75 dark:bg-slate-800 dark:text-white; }
    .ui-dialog::backdrop { backdrop-filter: blur(3px); }

    .ui-skeleton { @apply relative overflow-hidden rounded-lg bg-slate-200 dark:bg-slate-700; }
    .ui-skeleton::after { content: ''; @apply absolute inset-0 -translate-x-full bg-gradient-to-r from-transparent via-white/60 to-transparent motion-safe:animate-pulse dark:via-white/10; }

    .ui-loading-spinner { @apply h-4 w-4 animate-spin rounded-full border-2 border-current border-r-transparent motion-reduce:animate-none; }
    .ui-page-progress { @apply fixed inset-x-0 top-0 z-[110] h-1 origin-left scale-x-0 bg-indigo-600 opacity-0 transition; }
    .ui-page-progress[data-active='true'] { @apply scale-x-100 opacity-100; }
    .ui-is-loading { @apply cursor-wait; }

    .ui-table-scroll {
        @apply relative w-full overflow-x-auto overscroll-x-contain rounded-2xl border border-slate-300 bg-white dark:border-slate-700 dark:bg-slate-900;
        scrollbar-width: thin;
        scrollbar-color: rgb(100 116 139) transparent;
        -webkit-overflow-scrolling: touch;
    }

    .ui-table-scroll:focus-visible { @apply ring-4 ring-indigo-500/30; }
    .ui-table-scroll > table { @apply min-w-full; }
    .ui-table-scroll thead th {
        @apply sticky top-0 z-10 bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-100;
    }
    .ui-table-scroll[data-wide-table='true'] th:first-child,
    .ui-table-scroll[data-wide-table='true'] td:first-child {
        @apply sticky left-0 z-[5] bg-white shadow-[2px_0_0_rgba(148,163,184,.25)] dark:bg-slate-900;
    }
    .ui-table-scroll[data-wide-table='true'] thead th:first-child { @apply z-20 bg-slate-100 dark:bg-slate-800; }

    .ui-skip-link {
        @apply fixed left-4 top-3 z-[100] -translate-y-20 rounded-xl bg-white px-4 py-3 text-sm font-black text-indigo-800 shadow-xl transition-transform dark:bg-slate-900 dark:text-indigo-200;
    }
    .ui-skip-link:focus { @apply translate-y-0; }

    .ui-validation-summary {
        @apply mx-auto mt-4 max-w-7xl rounded-2xl border-l-4 border-rose-600 bg-rose-50 px-5 py-4 text-sm text-rose-900 shadow-sm dark:bg-rose-950/50 dark:text-rose-100;
    }

    .ui-required::after { content: ' *'; @apply text-rose-600 dark:text-rose-400; }

    .ui-empty-state {
        @apply rounded-2xl border-2 border-dashed border-slate-200 px-5 py-10 text-center text-sm text-slate-500 dark:border-slate-700 dark:text-slate-400;
    }

    .ui-form-help {
        @apply mt-1 text-xs leading-5 text-slate-500 dark:text-slate-400;
    }

    .ui-media-alternative {
        @apply mt-2 rounded-xl border-l-4 border-amber-600 bg-amber-50 p-3 text-sm font-semibold text-amber-950 dark:bg-amber-950/60 dark:text-amber-100;
    }
}

@media (max-width: 767px) {
    button:not(.ui-compact),
    a.ui-action,
    input[type='submit'],
    input[type='button'] { min-height: 44px; }

    .ui-table-scroll { max-width: calc(100vw - 2rem); }
    .ui-table-scroll th,
    .ui-table-scroll td { @apply whitespace-nowrap px-3 py-3 text-sm; }

    .app-shell-header {
        @apply flex-wrap items-start px-3 py-2;
    }

    .app-shell-header > :last-child {
        @apply w-full min-w-0 justify-between;
    }

    #main-content > * {
        max-width: 100%;
    }

    #main-content :where(.overflow-x-auto, .ui-table-scroll) {
        max-width: calc(100vw - 1.5rem);
        scrollbar-gutter: stable;
    }

    #main-content :where(.grid.grid-cols-2) {
        grid-template-columns: minmax(0, 1fr);
    }

    #main-content :where(form, section, article, div) {
        min-width: 0;
    }

    #main-content :where(input:not([type='checkbox']):not([type='radio']), select, textarea) {
        max-width: 100%;
    }

    /* El calendario conserva sus siete columnas, pero reduce el ancho de 980 px
       para evitar un desplazamiento excesivo en teléfonos y a 200% de zoom. */
    #main-content section.overflow-x-auto > .min-w-\[980px\] {
        min-width: 700px;
    }

    #main-content section.overflow-x-auto > .min-w-\[980px\] .min-h-36 {
        min-height: 7rem;
    }
}

@media (max-width: 479px) {
    .app-shell-header {
        min-height: auto;
    }

    .ui-modal-panel {
        height: 100dvh;
        border-radius: 0;
    }

    #main-content :where(.p-6, .p-7, .p-8) {
        padding: 1rem;
    }

    #main-content :where(.text-3xl, .text-4xl) {
        font-size: 1.5rem;
        line-height: 2rem;
    }
}

/* A 200% de zoom un escritorio se comporta como una tableta angosta. */
@media (max-width: 1024px) {
    .ui-dialog {
        width: calc(100% - 1rem);
        max-height: calc(100dvh - 1rem);
    }

    #main-content :where(.xl\:grid-cols-3, .lg\:grid-cols-3, .lg\:grid-cols-4) {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (forced-colors: active) {
    :focus-visible { outline: 3px solid CanvasText; }
    .ui-table-scroll, button, input, select, textarea { border: 1px solid CanvasText; }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}
