/* =====================================================================
   Sistema de Diseño — Formularios
   Inspirado en Notion / Stripe / Vercel
   Cargado primero en todas las páginas.
   ===================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    /* === Tipografía === */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI',
                 Roboto, 'Helvetica Neue', Arial, sans-serif;
    --font-mono: ui-monospace, 'SF Mono', Menlo, Monaco, Consolas, monospace;

    /* === Paleta UndeltaForms ===
       Oliva #828D6F · Pizarra #2D3242 · Lima #85B036 · Crema #E6E0C5
       Azul gris #4B5976 · Ocre #C79B53 · Verde bosque #4E5F28 */

    /* === Color: superficies === */
    --bg:           #e6e0c5;   /* crema/beige claro */
    --bg-elevated:  #ffffff;   /* tarjetas */
    --bg-muted:     #f1eddc;
    --bg-hover:     #ece6d2;

    /* === Color: bordes === */
    --border:        #d7d0b7;
    --border-strong: #c7bf9f;
    --border-subtle: rgba(45, 50, 66, 0.08);

    /* === Color: texto === */
    --text:         #2d3242;   /* azul noche / pizarra */
    --text-muted:   #4b5976;   /* azul grisáceo */
    --text-subtle:  #828d6f;   /* oliva muted */
    --text-inverse: #ffffff;

    /* === Color: acciones === */
    --accent:             #4e5f28;   /* verde bosque (texto blanco encima) */
    --accent-hover:       #3c4a1f;
    --accent-text:        #ffffff;
    --accent-soft:        rgba(78, 95, 40, 0.07);
    --accent-tint:        rgba(78, 95, 40, 0.12);
    --accent-tint-strong: rgba(78, 95, 40, 0.20);

    --link:          #4b5976;
    --link-hover:    #2d3242;

    --success:       #85b036;   /* lima */
    --success-soft:  #e7f0d4;
    --danger:        #c0492f;
    --danger-soft:   #f6ddd5;
    --warning:       #c79b53;   /* ocre / mostaza */
    --warning-soft:  #f4e8d1;

    /* === Sombras === */
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.03);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.04);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.08), 0 4px 12px rgba(0, 0, 0, 0.04);

    /* === Radius === */
    --radius-sm: 6px;
    --radius:    8px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;

    /* === Transiciones === */
    --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 250ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* =====================================================================
   Reset
   ===================================================================== */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font-sans);
    font-size: 15px;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

img, svg { max-width: 100%; display: block; }

button { font-family: inherit; cursor: pointer; }

a { color: inherit; text-decoration: none; transition: color var(--transition); }

/* =====================================================================
   Tipografía
   ===================================================================== */
h1, h2, h3, h4, h5, h6 {
    margin: 0;
    font-weight: 600;
    letter-spacing: -0.015em;
    color: var(--text);
}

h1 { font-size: 28px; line-height: 1.25; letter-spacing: -0.02em; }
h2 { font-size: 22px; line-height: 1.3;  letter-spacing: -0.015em; }
h3 { font-size: 18px; line-height: 1.4; }
h4 { font-size: 16px; line-height: 1.4; }

p  { margin: 0; }

.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); font-size: 14px; }

/* =====================================================================
   Layouts
   ===================================================================== */
.container {
    max-width: 1180px;
    margin: 0 auto;
    padding: 0 24px;
}

.page {
    min-height: 100vh;
    padding: 32px 24px 64px;
}

/* =====================================================================
   Botones
   ===================================================================== */
.btn,
button.btn,
input[type="submit"].btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 38px;
    padding: 0 16px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    line-height: 1;
    color: var(--accent-text);
    background: var(--accent);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    transition: background var(--transition), transform var(--transition),
                box-shadow var(--transition), border-color var(--transition);
    white-space: nowrap;
}

.btn:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.btn:active { transform: translateY(0.5px); }

.btn:focus-visible {
    outline: 2px solid var(--link);
    outline-offset: 2px;
}

.btn .material-icons { font-size: 18px; }

/* Variantes */
.btn-secondary,
.btn.btn-secondary {
    color: var(--text);
    background: var(--bg-elevated);
    border-color: var(--border);
}
.btn-secondary:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
}

.btn-ghost {
    color: var(--text);
    background: transparent;
    border-color: transparent;
}
.btn-ghost:hover {
    background: var(--bg-hover);
}

.btn-danger {
    color: white;
    background: var(--danger);
    border-color: var(--danger);
}
.btn-danger:hover {
    background: #b91c1c;
    border-color: #b91c1c;
}

.btn-sm {
    height: 32px;
    padding: 0 12px;
    font-size: 13px;
}

.btn-lg {
    height: 44px;
    padding: 0 20px;
    font-size: 15px;
}

/* =====================================================================
   Inputs
   ===================================================================== */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="date"],
input[type="url"],
input[type="search"],
input[type="file"],
select,
textarea {
    width: 100%;
    height: 38px;
    padding: 0 12px;
    font-family: inherit;
    font-size: 14px;
    color: var(--text);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: border-color var(--transition), box-shadow var(--transition);
    appearance: none;
    -webkit-appearance: none;
}

textarea {
    height: auto;
    min-height: 96px;
    padding: 10px 12px;
    line-height: 1.5;
    resize: vertical;
}

input[type="file"] {
    padding: 7px 12px;
    line-height: 22px;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(78, 95, 40, 0.12);
}

input::placeholder,
textarea::placeholder { color: var(--text-subtle); }

select {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath fill='none' stroke='%2371717a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' d='M3 4.5l3 3 3-3'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 14px;
    padding-right: 32px;
}

label {
    display: inline-block;
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
}

/* =====================================================================
   Cards
   ===================================================================== */
.card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    transition: border-color var(--transition), box-shadow var(--transition),
                transform var(--transition);
}

.card:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-sm);
}

.card-padded { padding: 24px; }

/* =====================================================================
   Tablas
   ===================================================================== */
table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    font-size: 14px;
}

thead th {
    padding: 12px 16px;
    text-align: left;
    font-weight: 500;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-subtle);
    background: var(--bg-muted);
    border-bottom: 1px solid var(--border);
}

tbody td {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-subtle);
    color: var(--text);
    vertical-align: middle;
}

tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--bg-muted); }

/* =====================================================================
   Badges / Tags
   ===================================================================== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
    background: var(--bg-muted);
    border: 1px solid var(--border);
    border-radius: 999px;
    line-height: 1.4;
}

.badge-success { color: var(--success); background: var(--success-soft); border-color: transparent; }
.badge-danger  { color: var(--danger);  background: var(--danger-soft);  border-color: transparent; }
.badge-warning { color: var(--warning); background: var(--warning-soft); border-color: transparent; }

/* =====================================================================
   Header / Navbar
   ===================================================================== */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
    backdrop-filter: saturate(180%) blur(8px);
    -webkit-backdrop-filter: saturate(180%) blur(8px);
}

.app-header .brand {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text);
}

/* =====================================================================
   Utilidades
   ===================================================================== */
.stack > * + * { margin-top: 16px; }
.stack-sm > * + * { margin-top: 8px; }
.stack-lg > * + * { margin-top: 32px; }

.row {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.row-between {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.divider {
    height: 1px;
    background: var(--border);
    border: 0;
    margin: 24px 0;
}

.text-center { text-align: center; }
.text-right  { text-align: right; }

/* Iconos Material a tamaño "elegante" */
.material-icons {
    font-size: 20px;
    vertical-align: middle;
    line-height: 1;
}

/* =====================================================================
   Scrollbar discreta (WebKit)
   ===================================================================== */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 999px;
    border: 2px solid var(--bg);
}
::-webkit-scrollbar-thumb:hover { background: var(--text-subtle); }

/* =====================================================================
   Selection
   ===================================================================== */
::selection {
    background: var(--accent-tint-strong);
    color: var(--accent);
}

/* =====================================================================
   Responsive
   ===================================================================== */
@media (max-width: 640px) {
    body { font-size: 14px; }
    h1 { font-size: 24px; }
    h2 { font-size: 20px; }
    .page { padding: 24px 16px 48px; }
    .container { padding: 0 16px; }
    .app-header { padding: 14px 16px; }
}

/* =====================================================================
   Logo / branding
   ===================================================================== */

/* Logo en navbar (al lado del nombre) */
.brand-logo {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text);
}

.brand-logo img {
    width: 22px;
    height: 22px;
    max-width: 22px;
    max-height: 22px;
    object-fit: contain;
    display: block;
}

.brand-logo .brand-name {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--accent);
    line-height: 1;
}

/* Logo en páginas de auth (login, registro) — centrado arriba de la card */
.auth-logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.auth-logo img {
    width: 44px;
    height: 44px;
    max-width: 44px;
    max-height: 44px;
    object-fit: contain;
    display: block;
}

@media (max-width: 480px) {
    .auth-logo img { width: 38px; height: 38px; max-width: 38px; max-height: 38px; }
    .auth-logo { margin-bottom: 16px; }
}

/* =====================================================================
   Cartel de alerta (inline, arriba de formularios)
   Usado para errores de login, etc. — emergente y minimalista
   ===================================================================== */
.auth-alert {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 11px 14px;
    margin-bottom: 18px;
    font-size: 13px;
    font-weight: 500;
    line-height: 1.4;
    color: var(--danger);
    background: rgba(220, 38, 38, 0.06);
    border: 1px solid rgba(220, 38, 38, 0.18);
    border-left: 3px solid var(--danger);
    border-radius: var(--radius);
    animation: alert-slide 260ms cubic-bezier(0.4, 0, 0.2, 1);
}

.auth-alert::before {
    content: '';
    flex-shrink: 0;
    width: 6px;
    height: 6px;
    background: var(--danger);
    border-radius: 50%;
}

@keyframes alert-slide {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* =====================================================================
   Validación de formularios (custom — sin tooltip nativo)
   ===================================================================== */
input.field-invalid,
select.field-invalid,
textarea.field-invalid,
.field-invalid {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.10) !important;
}

input.field-invalid:focus,
select.field-invalid:focus,
textarea.field-invalid:focus {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.18) !important;
    outline: none !important;
}

/* Mensaje de error que aparece debajo del campo */
.field-error {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.4;
    color: var(--danger);
    animation: fade-in 160ms ease-out;
}

.field-error::before {
    content: '';
    display: inline-block;
    flex-shrink: 0;
    width: 6px;
    height: 6px;
    background: var(--danger);
    border-radius: 50%;
}

/* Label asociado a un campo inválido también se pone rojo */
label.field-label-invalid,
.question-text.field-label-invalid {
    color: var(--danger) !important;
}

/* =====================================================================
   Animaciones suaves
   ===================================================================== */
@keyframes fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.fade-in { animation: fade-in 250ms ease-out; }

/* Respeta usuarios con motion reducida */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* =====================================================================
   RESPONSIVE BASELINE GLOBAL
   ---------------------------------------------------------------------
   Capa de compatibilidad y rendimiento que aplica a toda la web.
   Evita los problemas más típicos de mobile (overflow horizontal,
   zoom de iOS, vh roto, área de toque chica, notch sin respetar,
   tablas sin scroll, etc.). Mobile-first. No rompe nada existente.
   ===================================================================== */

/* --- Sin overflow horizontal jamás --------------------------------- */
/* Usamos `clip` cuando está soportado (no crea containing block, así
   no rompe position:fixed en iOS). Fallback a `hidden` para navegadores
   viejos. */
html, body {
    max-width: 100%;
    overflow-x: hidden;
}
@supports (overflow-x: clip) {
    html, body { overflow-x: clip; }
}

/* --- 100vh real en iOS (100dvh con fallback) ----------------------- */
@supports (height: 100dvh) {
    .min-h-screen,
    body.min-h-screen { min-height: 100dvh; }
}

/* --- Soporte de safe-area (notch iOS / barras Android) ------------- */
@supports (padding: max(0px)) {
    body {
        padding-left:  env(safe-area-inset-left, 0);
        padding-right: env(safe-area-inset-right, 0);
    }
    .app-header,
    .navbar {
        padding-left:  max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }
}

/* --- Tipografía fluida con clamp() (escala suavemente entre breakpoints) */
h1 { font-size: clamp(22px, 4.5vw, 28px); }
h2 { font-size: clamp(18px, 3.6vw, 22px); }
h3 { font-size: clamp(16px, 3.0vw, 18px); }

/* --- Imágenes y multimedia siempre responsive ---------------------- */
img, picture, video, canvas, svg {
    max-width: 100%;
    height: auto;
}

/* --- Romper palabras largas (URLs, emails) en mobile --------------- */
h1, h2, h3, h4, p, li, span, label, a {
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* --- Botones / áreas táctiles: mínimo 44×44 en touch --------------- */
@media (pointer: coarse) {
    button, .btn, a.btn, .action-btn, .side-btn, [role="button"] {
        min-height: 44px;
    }
    input[type="checkbox"], input[type="radio"] {
        min-width: 20px;
        min-height: 20px;
    }
}

/* --- iOS: evitar zoom al focusear un input (font-size mínimo 16px) - */
@media (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="search"],
    input[type="url"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    input[type="file"],
    select,
    textarea {
        font-size: 16px;
    }
}

/* --- Contenedores fluidos: nunca exceder el viewport --------------- */
.container,
.dashboard-container,
.form-public-container,
.detalle-card,
.form-card,
.question-card,
.header-card {
    max-width: 100%;
}

/* --- Tablas con scroll horizontal contenido ------------------------ */
.table-scroll,
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

table {
    /* Permite scroll horizontal dentro del wrapper en mobile sin romper layout */
    min-width: 100%;
}

/* --- Iconos Material: nunca participar del flujo de texto fluido --- */
.material-icons {
    flex-shrink: 0;
    font-variation-settings: normal;
    user-select: none;
}

/* --- Mejor scroll en iOS dentro de elementos scrollables ----------- */
* {
    -webkit-overflow-scrolling: touch;
}

/* --- WebViews de Instagram/Facebook/TikTok: garantizar viewport ---- */
@supports (-webkit-touch-callout: none) {
    body {
        /* Estabiliza el alto en iOS WebView (in-app browsers) */
        min-height: -webkit-fill-available;
    }
}

/* --- will-change SÓLO en elementos que se animan continuamente.
   En .form-card / .question-card creaba un containing block que
   en algunos WebViews móviles rompía la posición fixed de modales. */
.toast-success { will-change: top, transform, opacity; }

/* --- Mejor rendimiento de fuentes: evitar repintar ----------------- */
body {
    font-feature-settings: "kern" 1, "liga" 1;
    text-rendering: optimizeLegibility;
}

/* =====================================================================
   BREAKPOINTS PROFESIONALES
   ---------------------------------------------------------------------
   sm  ≤ 480px  → móviles chicos (iPhone SE, Android compacto)
   md  ≤ 640px  → móviles grandes y phablets
   lg  ≤ 768px  → tablets verticales
   xl  ≤ 1024px → tablets horizontales / notebooks chicas
   2xl ≤ 1280px → desktops
   ===================================================================== */

/* --- Tablets y abajo: dashboard y headers se compactan ------------- */
@media (max-width: 1024px) {
    .page { padding: 24px 16px 64px; }
}

/* --- Móviles grandes ----------------------------------------------- */
@media (max-width: 640px) {
    .app-modal-actions { padding: 10px 12px; }
    .app-modal-btn { flex: 1; min-width: 0; }

    /* Toast adaptado al ancho real con safe area */
    .toast-success {
        left: max(12px, env(safe-area-inset-left));
        right: max(12px, env(safe-area-inset-right));
        max-width: none;
        transform: none;
        justify-content: center;
    }
    .toast-success.show { top: max(16px, env(safe-area-inset-top)); }
}

/* --- Móviles chicos (iPhone SE, Android 350px) --------------------- */
@media (max-width: 380px) {
    body { font-size: 13.5px; }
    .page { padding: 20px 12px 40px; }
    .container { padding: 0 12px; }
    .btn, button.btn { padding: 0 12px; }
    .material-icons { font-size: 18px; }
    h1 { font-size: 20px; }
    h2 { font-size: 17px; }
    .toast-success { padding: 10px 14px; font-size: 13px; }
}

/* --- Orientación landscape en mobile: aprovechar el ancho ---------- */
@media (max-height: 480px) and (orientation: landscape) {
    body { padding-top: env(safe-area-inset-top, 0); }
    .page { padding-top: 12px; padding-bottom: 24px; }
}

/* --- Pantallas ultra grandes (≥1600px): aire extra ----------------- */
@media (min-width: 1600px) {
    .container { max-width: 1280px; }
}

/* --- Print: limpiar UI ---------------------------------------------- */
@media print {
    .no-print, .sidebar, #floating-sidebar, .save-btn,
    .toast-success, .app-modal-overlay { display: none !important; }
    body { background: #fff; }
}

/* ============================================================
   OPTIMIZACIÓN DE ANIMACIONES (rendimiento móvil)
   ============================================================ */
/* Sin retardo táctil de ~300 ms en elementos interactivos. */
button, a, .side-btn, .save-btn, .switch, select {
    touch-action: manipulation;
}

/* Respetá "reducir movimiento" del sistema (accesibilidad + batería). */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}
