/*
 * ITEngine: estilos propios, sin paso de compilación.
 * Aspecto basado en Tailwind y Preline; paleta Material (como APP_BACKUP).
 * Personalización por cliente: basta con cambiar las variables de :root
 * (p. ej. --primary) en un CSS del cliente cargado después de este.
 */
@layer reset, tokens, base, layout, components, utilities;

/* Inclusive Sans (SIL OFL 1.1, assets/fonts/OFL.txt), servida por la propia app */
@font-face {
    font-family: "Inclusive Sans";
    font-style: italic;
    font-weight: 300 700;
    font-display: swap;
    src: url("fonts/inclusive-sans-italic-latin-ext.woff2") format("woff2");
    unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

@font-face {
    font-family: "Inclusive Sans";
    font-style: italic;
    font-weight: 300 700;
    font-display: swap;
    src: url("fonts/inclusive-sans-italic-latin.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Inclusive Sans";
    font-style: normal;
    font-weight: 300 700;
    font-display: swap;
    src: url("fonts/inclusive-sans-normal-latin-ext.woff2") format("woff2");
    unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

@font-face {
    font-family: "Inclusive Sans";
    font-style: normal;
    font-weight: 300 700;
    font-display: swap;
    src: url("fonts/inclusive-sans-normal-latin.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@layer tokens {
    :root {
        /* Paleta Material (500 y tonos claros para fondos de etiquetas) */
        --red: #f44336; --red-soft: #ffebee;
        --pink: #e91e63; --pink-soft: #fce4ec;
        --purple: #9c27b0; --purple-soft: #f3e5f5;
        --deep-purple: #673ab7; --deep-purple-soft: #ede7f6;
        --indigo: #3f51b5; --indigo-soft: #e8eaf6;
        --blue: #2196f3; --blue-soft: #e3f2fd;
        --light-blue: #03a9f4; --light-blue-soft: #e1f5fe;
        --cyan: #00bcd4; --cyan-soft: #e0f7fa;
        --teal: #009688; --teal-soft: #e0f2f1;
        --green: #4caf50; --green-soft: #e8f5e9;
        --light-green: #8bc34a; --light-green-soft: #f1f8e9;
        --lime: #cddc39; --lime-soft: #f9fbe7;
        --yellow: #ffeb3b; --yellow-soft: #fffde7;
        --amber: #ffc107; --amber-soft: #fff8e1;
        --orange: #ff9800; --orange-soft: #fff3e0;
        --deep-orange: #ff5722; --deep-orange-soft: #fbe9e7;
        --brown: #795548; --brown-soft: #efebe9;
        --grey: #9e9e9e; --grey-soft: #f5f5f5;
        --blue-grey: #607d8b; --blue-grey-soft: #eceff1;

        /* Significado */
        --primary: #1e88e5;
        --primary-text: #fff;
        --danger: #e53935;
        --warning: #fb8c00;
        --success: #43a047;
        --bg: #f8fafc;
        --surface: #fff;
        --surface-2: #f1f5f9;
        --text: #1e293b;
        --muted: #64748b;
        --border: #e2e8f0;
        --focus: color-mix(in srgb, var(--primary) 45%, transparent);

        /* Medidas */
        --font: "Inclusive Sans", Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
        --font-mono: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
        --text-sm: 0.8125rem;
        --text-base: 0.9375rem;
        --text-lg: 1.125rem;
        --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem; --space-6: 1.5rem;
        --radius: 0.5rem;
        --radius-sm: 0.375rem;
        --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 1px 3px rgb(15 23 42 / 10%);
        --shadow-lg: 0 10px 25px -5px rgb(15 23 42 / 15%), 0 8px 10px -6px rgb(15 23 42 / 10%);
        --header-height: 3.5rem;
        --nav-width: 15.5rem;
        --nav-width-compact: 4.375rem;
        --panel-width: min(28rem, 90vw);
        color-scheme: light dark;
    }

    /* Modo oscuro: el del sistema, salvo que la app fije data-theme */
    @media (prefers-color-scheme: dark) {
        :root:not([data-theme="light"]) {
            --bg: #0b1120; --surface: #111827; --surface-2: #1f2937;
            --text: #e2e8f0; --muted: #94a3b8; --border: #334155;
            --primary: #42a5f5; --primary-text: #0b1120;
        }
    }

    :root[data-theme="dark"] {
        --bg: #0b1120; --surface: #111827; --surface-2: #1f2937;
        --text: #e2e8f0; --muted: #94a3b8; --border: #334155;
        --primary: #42a5f5; --primary-text: #0b1120;
    }
}

@layer reset {
    *, *::before, *::after { box-sizing: border-box; }
    body, h1, h2, h3, p, ol, ul, dl, dd, figure { margin: 0; }
    ol, ul { padding: 0; list-style: none; }
    img, svg { display: block; max-width: 100%; }
    button, input, select, textarea { font: inherit; color: inherit; }
}

@layer base {
    html { font-size: 100%; -webkit-text-size-adjust: 100%; }

    body {
        font-family: var(--font);
        font-size: var(--text-base);
        line-height: 1.5;
        color: var(--text);
        background: var(--bg);
    }

    a { color: var(--primary); }
    :focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
    h1 { font-size: 1.375rem; } h2 { font-size: var(--text-lg); } h3 { font-size: 1rem; }
}

@layer layout {
    /* Cabecera, menú lateral, contenido y panel derecho ("companion") */
    .app {
        display: grid;
        grid-template: "header header header" var(--header-height) "nav main panel" 1fr / auto 1fr auto;
        height: 100dvh;
    }

    .app-header {
        grid-area: header;
        display: flex;
        align-items: center;
        gap: var(--space-3);
        padding-inline: var(--space-3);
        background: var(--surface);
        border-bottom: 1px solid var(--border);

        & .app-logo { max-height: calc(var(--header-height) - 1rem); }
        & .app-name { font-weight: 600; }
        & .app-logo { display: block; height: 1.75rem; width: auto; }
        & .app-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        & .app-user { margin-inline-start: auto; color: var(--muted); font-size: var(--text-sm); white-space: nowrap; }
    }

    .app-nav {
        grid-area: nav;
        width: var(--nav-width);
        overflow-y: auto;
        padding: var(--space-2);
        background: var(--surface);
        border-right: 1px solid var(--border);
        transition: width 0.2s ease;

        & a, & summary {
            display: flex;
            align-items: center;
            gap: var(--space-3);
            padding: var(--space-2) var(--space-3);
            border-radius: var(--radius-sm);
            color: var(--text);
            text-decoration: none;
            white-space: nowrap;
            overflow: hidden;
        }

        & a:hover, & summary:hover { background: var(--surface-2); }
        /* Grupos de informes (<details>): sin triángulo; el contenido, un poco a la derecha */
        & summary { cursor: pointer; list-style: none; font-weight: 500; }
        & summary::-webkit-details-marker { display: none; }
        & .nav-group > :not(summary) { margin-inline-start: var(--space-3); }
        & a[aria-current="page"] { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }
    }

    .app[data-nav="compact"] .app-nav { width: var(--nav-width-compact); }
    .app[data-nav="compact"] .app-nav .nav-label { display: none; }

    .app-main {
        grid-area: main;
        overflow: auto;
        padding: var(--space-4);
        display: grid;
        gap: var(--space-4);
        align-content: start;
    }

    .app-panel {
        grid-area: panel;
        width: var(--panel-width);
        overflow-y: auto;
        padding: var(--space-4);
        background: var(--surface);
        border-left: 1px solid var(--border);

        /* Vacío, o solo con el <template> que deja la respuesta de guardar */
        &:not(:has(> :not(template))) { display: none; }
    }

    /* Móvil: menú y panel por encima del contenido */
    @media (width < 48rem) {
        .app { grid-template: "header" var(--header-height) "main" 1fr / 1fr; }
        .app-nav, .app-panel { position: fixed; inset-block: var(--header-height) 0; z-index: 20; box-shadow: var(--shadow-lg); }
        .app-nav { inset-inline-start: 0; }
        .app-panel { inset-inline-end: 0; }
        .app:not([data-nav="open"]) .app-nav { display: none; }
        /* Sin sitio para el nombre del usuario: queda el botón de salir */
        .app-user { visibility: hidden; }
    }

    /* Página de un solo componente (compartido o en iframe): sin el resto de la app */
    .app-single { padding: var(--space-4); }
}

@layer components {
    .component {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        container-type: inline-size;
        min-width: 0;
    }

    .component-header {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--space-3);
        padding: var(--space-3) var(--space-4);
        border-bottom: 1px solid var(--border);
    }

    .component-title { font-size: 1rem; font-weight: 600; margin-inline-end: auto; }
    .component-loading { padding: var(--space-6); color: var(--muted); }

    /* Botones */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--space-2);
        padding: 0.4375rem 0.875rem;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        background: var(--surface);
        color: var(--text);
        font-size: var(--text-sm);
        font-weight: 500;
        line-height: 1.25rem;
        cursor: pointer;
        text-decoration: none;
        transition: background 0.15s, border-color 0.15s;

        &:hover { background: var(--surface-2); }
        &:disabled { opacity: 0.55; cursor: not-allowed; }
    }

    .btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
    .btn-primary:hover { background: color-mix(in srgb, var(--primary) 88%, black); }
    .btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
    .btn-small { padding: 0.1875rem 0.625rem; }
    .btn-icon { padding: 0.25rem 0.5rem; border-color: transparent; background: transparent; }

    /* Campos */
    .input, .select, .textarea {
        width: 100%;
        padding: 0.4375rem 0.75rem;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        background: var(--surface);
        font-size: var(--text-sm);

        &:focus { outline: 2px solid var(--focus); border-color: var(--primary); }
    }

    .input-search { width: min(18rem, 100%); }
    .field { display: grid; gap: var(--space-1); }
    .field label { font-size: var(--text-sm); font-weight: 500; }
    .field-help, .field-error { margin: 0; font-size: var(--text-sm); color: var(--muted); }
    .field-error { color: var(--danger); }
    .field-invalid :is(.input, .select, .textarea) { border-color: var(--danger); }
    .checkbox { display: flex; align-items: center; gap: var(--space-2); }
    .textarea { resize: vertical; }

    /* Formularios (en el panel) */
    .form { display: grid; gap: var(--space-4); padding: var(--space-4); }
    /* En el panel ya hay margen: sin el del formulario */
    .app-panel > .form { padding: 0; }
    .fieldset {
        display: grid;
        gap: var(--space-4);
        margin: 0;
        padding: var(--space-3) var(--space-4) var(--space-4);
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);

        & legend { padding-inline: var(--space-1); font-size: var(--text-sm); font-weight: 600; }
    }
    .form-actions { display: flex; gap: var(--space-2); }

    /* Aspecto: logo en el login y vista previa de las imágenes en la administración */
    .login-logo { display: block; max-height: 4rem; width: auto; margin-inline: auto; }
    .appearance-preview { display: block; max-height: 3rem; width: auto; }

    /* Diseñador: una fila por elemento, con sus campos en línea */
    .designer-section { display: grid; gap: var(--space-2); margin-block-end: var(--space-6); }
    .designer-row { display: flex; align-items: center; gap: var(--space-2); }
    .designer-row > .designer-fields { flex: 1; }
    .designer-fields {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--space-2);

        /* min-width 0: sin él, un campo numérico no baja de su ancho intrínseco */
        & .input { flex: 1 1 12rem; width: auto; min-width: 0; }
        & .input[name="icon"] { flex: 0 1 10rem; }
        & .input[type="number"] { flex: 0 0 5rem; }
        & .select { flex: 0 1 14rem; width: auto; }
    }
    /* Columnas de un componente: una rejilla con cabecera; en móvil, la expresión debajo */
    .designer-column {
        display: grid;
        grid-template-columns: 1.5rem minmax(7rem, 1fr) 4.5rem minmax(7rem, 1fr) 9rem minmax(10rem, 2fr);
        gap: var(--space-2);
        align-items: center;
    }
    .designer-column-head { font-size: var(--text-sm); font-weight: 600; color: var(--muted); }
    @media (width < 48rem) {
        .designer-column { grid-template-columns: 1.5rem 1fr 4.5rem; }
        .designer-column > :nth-child(n + 4) { grid-column: 2 / -1; }
        .designer-column-head > :nth-child(n + 4) { display: none; }
    }
    .designer-new { padding-block-start: var(--space-2); border-block-start: 1px solid var(--border); }

    /* <ite-select>: selector con búsqueda */
    ite-select { position: relative; display: block; }
    .ite-select-list {
        position: absolute;
        inset-inline: 0;
        top: calc(100% + 2px);
        z-index: 30;
        max-height: 16rem;
        margin: 0;
        padding: var(--space-1) 0;
        overflow: auto;
        list-style: none;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        background: var(--surface);
        box-shadow: var(--shadow-lg);
    }
    .ite-select-option {
        padding: var(--space-1) var(--space-3);
        font-size: var(--text-sm);
        cursor: pointer;

        &:hover, &[aria-selected="true"] { background: color-mix(in srgb, var(--primary) 12%, var(--surface)); }
    }

    /* <ite-expr>: editor de expresiones. Las sugerencias, justo debajo del campo (posición estática) */
    ite-expr { position: relative; display: grid; gap: var(--space-1); flex: 1 1 14rem; }
    ite-expr .ite-select-list { top: auto; margin-top: 2px; }
    .ite-expr-status:empty, .ite-expr-preview:empty { display: none; }
    .ite-expr-status { font-size: var(--text-sm); }
    .ite-expr-status code { white-space: pre-wrap; }
    .ite-expr-status mark { background: color-mix(in srgb, var(--danger) 25%, transparent); color: inherit; }
    .ite-expr-preview {
        display: grid;
        grid-template-columns: auto 1fr;
        gap: 0 var(--space-3);
        margin: 0;
        padding: 0;
        list-style: none;
        font-size: var(--text-sm);

        & li { display: contents; }
    }
    .ite-expr-advanced pre { margin: var(--space-1) 0 0; font-size: var(--text-sm); white-space: pre-wrap; }

    /* Tabla: cabecera fija; el navegador no pinta las filas fuera de pantalla */
    .table-wrap { overflow: auto; max-height: 70dvh; }

    .table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
        font-size: var(--text-sm);

        & th {
            position: sticky;
            top: 0;
            z-index: 1;
            background: var(--surface-2);
            text-align: start;
            font-weight: 600;
            color: var(--muted);
            white-space: nowrap;
            border-bottom: 1px solid var(--border);
        }

        & td { padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); vertical-align: middle; }
        & tbody tr { content-visibility: auto; contain-intrinsic-size: auto 2.5rem; }
        & tbody tr:hover { background: color-mix(in srgb, var(--primary) 5%, transparent); }
        & .table-more td { color: var(--muted); text-align: center; }
        & .cell-actions { width: 1%; text-align: end; }
    }

    .th-sort {
        width: 100%;
        padding: var(--space-2) var(--space-3);
        border: 0;
        background: none;
        color: inherit;
        font-weight: inherit;
        text-align: inherit;
        cursor: pointer;
    }

    /* Etiquetas de valores (value_labels con icono y color) */
    .badge {
        display: inline-flex;
        align-items: center;
        gap: var(--space-1);
        padding: 0.0625rem 0.5rem;
        border-radius: 999px;
        font-size: 0.75rem;
        font-weight: 500;
        white-space: nowrap;
        color: color-mix(in srgb, var(--badge) 75%, var(--text));
        background: color-mix(in srgb, var(--badge) 14%, var(--surface));
        --badge: var(--grey);
    }

    .badge-red { --badge: var(--red); } .badge-pink { --badge: var(--pink); }
    .badge-purple { --badge: var(--purple); } .badge-deep-purple { --badge: var(--deep-purple); }
    .badge-indigo { --badge: var(--indigo); } .badge-blue { --badge: var(--blue); }
    .badge-light-blue { --badge: var(--light-blue); } .badge-cyan { --badge: var(--cyan); }
    .badge-teal { --badge: var(--teal); } .badge-green { --badge: var(--green); }
    .badge-light-green { --badge: var(--light-green); } .badge-lime { --badge: var(--lime); }
    .badge-yellow { --badge: var(--yellow); } .badge-amber { --badge: var(--amber); }
    .badge-orange { --badge: var(--orange); } .badge-deep-orange { --badge: var(--deep-orange); }
    .badge-brown { --badge: var(--brown); } .badge-blue-grey { --badge: var(--blue-grey); }

    .icon { width: 1.125em; height: 1.125em; fill: currentColor; flex: none; }

    /* Menú de acciones «⋯» (también con clic derecho, desde ite.js) */
    .menu {
        position: relative;
        display: inline-block;

        & > summary { list-style: none; cursor: pointer; }
        & > summary::-webkit-details-marker { display: none; }
    }

    .menu-list {
        position: absolute;
        inset-inline-end: 0;
        z-index: 10;
        min-width: 11rem;
        padding: var(--space-1);
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow-lg);
    }

    .menu-item {
        display: block;
        width: 100%;
        padding: var(--space-2) var(--space-3);
        border: 0;
        border-radius: var(--radius-sm);
        background: none;
        text-align: start;
        cursor: pointer;

        &:hover { background: var(--surface-2); }
    }

    .menu-item-danger { color: var(--danger); }

    /* Avisos */
    .alert {
        display: grid;
        gap: var(--space-2);
        padding: var(--space-3) var(--space-4);
        border-radius: var(--radius);
        border: 1px solid;

        & .btn { justify-self: start; }
    }

    .alert-warning { border-color: color-mix(in srgb, var(--warning) 40%, transparent); background: color-mix(in srgb, var(--warning) 10%, var(--surface)); }
    .alert-success { border-color: color-mix(in srgb, var(--success) 40%, transparent); background: color-mix(in srgb, var(--success) 10%, var(--surface)); }
    .alert-danger { border-color: color-mix(in srgb, var(--danger) 40%, transparent); background: color-mix(in srgb, var(--danger) 10%, var(--surface)); }

    /* Historial de una fila */
    .history { display: grid; gap: var(--space-3); }
    .history-list { display: grid; gap: var(--space-3); }

    .history-entry {
        display: grid;
        gap: var(--space-2);
        padding: var(--space-3);
        border: 1px solid var(--border);
        border-radius: var(--radius);

        & header { display: flex; justify-content: space-between; gap: var(--space-2); }
        & time { color: var(--muted); font-size: var(--text-sm); }
        & dl { display: grid; grid-template-columns: auto 1fr; gap: var(--space-1) var(--space-3); font-size: var(--text-sm); }
        & dt { color: var(--muted); }
        & del { color: var(--danger); margin-inline-end: var(--space-2); }
        & ins { color: var(--success); text-decoration: none; }
    }

    /* Login */
    .login {
        display: grid;
        place-items: center;
        min-height: 100dvh;
        padding: var(--space-4);

        & form { display: grid; gap: var(--space-4); width: min(22rem, 100%); padding: var(--space-6); }
    }
}

@layer utilities {
    .muted { color: var(--muted); }

    .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    /* Mientras HTMX espera la respuesta */
    .htmx-request.component, .htmx-request .table-more { opacity: 0.6; }
}
