/* ==========================================================================
   Diálogos y avisos propios (<dialog> nativo). Sin dependencias.
   En móvil son hojas que suben desde abajo; en escritorio, centrados.
   ========================================================================== */

.dlg {
    /* El reset `* { margin: 0 }` de app.css mata el centrado nativo del <dialog>:
       sin este margin: auto se pega arriba a la izquierda. */
    margin: auto;
    padding: 0;
    border: none;
    border-radius: var(--r-lg);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--sh-3);
    width: min(460px, calc(100% - 32px));
    max-height: calc(100dvh - 48px);
    overflow: visible;
}

/* Fondo congelado con un diálogo abierto. El `top` negativo lo pone dialogos.js
   para que la página no salte a la primera línea; al cerrar se restaura. */
body.dlg-abierto {
    position: fixed;
    left: 0;
    right: 0;
    width: 100%;
    overflow: hidden;
}

.dlg::backdrop {
    background: rgba(19, 32, 29, .45);
    backdrop-filter: blur(2px);
}

.dlg[open] {
    animation: dlg-entrar .18s ease-out;
}

.dlg[open]::backdrop {
    animation: dlg-fundido .18s ease-out;
}

@keyframes dlg-entrar {
    from { opacity: 0; transform: translateY(8px) scale(.98); }
    to   { opacity: 1; transform: none; }
}

@keyframes dlg-fundido {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.dlg-form {
    display: flex;
    flex-direction: column;
    max-height: calc(100dvh - 48px);
}

.dlg-titulo {
    font-size: var(--fs-lg);
    font-weight: 650;
    letter-spacing: -.015em;
    padding: var(--sp-5) var(--sp-5) var(--sp-3);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}

/* Recibe el foco al abrir el diálogo (ver dialogos.js) pero no es interactivo:
   no debe salir recuadro de foco */
.dlg-titulo:focus,
.dlg-titulo:focus-visible {
    outline: none;
}

.dlg-titulo i {
    color: var(--sage);
    font-weight: 400;
}

.dlg.peligro .dlg-titulo i {
    color: var(--rose);
}

.dlg-cuerpo {
    padding: 0 var(--sp-5) var(--sp-4);
    overflow-y: auto;
    /* Poner overflow-y:auto vuelve `auto` también el eje X, y entonces Safari deja
       arrastrar la lista de lado. Solo debe moverse en vertical. */
    overflow-x: hidden;
    touch-action: pan-y;
    /* Al llegar al final de la lista de campos, el gesto no se propaga afuera */
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: var(--sp-4);
}

.dlg-texto {
    color: var(--ink-2);
    font-size: var(--fs-md);
    line-height: 1.5;
}

.dlg-campo {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

/* --------------------------------------------------------------------------
   Lista de opciones ("¿qué quieres apuntar?"). Botones grandes, uno por línea:
   se leen y se aciertan con el dedo sin apuntar.
   -------------------------------------------------------------------------- */

.dlg-opciones {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
}

.dlg-opcion {
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    width: 100%;
    text-align: left;
    padding: 14px var(--sp-4);
    background: var(--surface-2);
    border: 1.5px solid var(--line-fuerte);
    border-radius: var(--r-md);
    color: var(--ink);
    cursor: pointer;
    transition: background .15s, border-color .15s, transform .1s;
}

.dlg-opcion:hover,
.dlg-opcion:focus-visible {
    background: var(--sage-50);
    border-color: var(--sage);
}

.dlg-opcion:active {
    transform: scale(.99);
}

.dlg-opcion > i:first-child {
    font-size: 1.5rem;
    color: var(--sage);
    flex-shrink: 0;
}

.dlg-opcion-textos {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.dlg-opcion-textos strong {
    font-size: var(--fs-md);
    font-weight: 600;
}

.dlg-opcion-textos small {
    color: var(--ink-2);
    font-size: var(--fs-xs);
}

.dlg-opcion-flecha {
    color: var(--ink-2);
    flex-shrink: 0;
}

/* Atajo dentro de un formulario (leer la medición de una foto) */
.dlg-accion {
    font-family: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    width: 100%;
    padding: 12px var(--sp-4);
    font-size: var(--fs-md);
    font-weight: 600;
    color: var(--sage-600);
    background: var(--sage-50);
    border: 1.5px dashed var(--sage);
    border-radius: var(--r-md);
    cursor: pointer;
    transition: background .15s, opacity .15s;
}

.dlg-accion:hover:not(:disabled) {
    background: var(--surface-2);
}

.dlg-accion:disabled {
    opacity: .6;
    cursor: progress;
}

.dlg-accion i {
    font-size: 1.25rem;
}

.dlg-etiqueta {
    font-size: var(--fs-xs);
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--ink-2);
}

.dlg-control {
    font-family: inherit;
    font-size: var(--fs-md);
    color: var(--ink);
    background: var(--surface-2);
    border: 1.5px solid var(--line-fuerte);
    border-radius: var(--r-md);
    padding: 12px 14px;
    width: 100%;
    transition: border-color .15s, box-shadow .15s, background .15s;
}

textarea.dlg-control {
    resize: vertical;
    min-height: 70px;
    line-height: 1.4;
}

select.dlg-control {
    appearance: none;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath fill='%234a5d59' d='M4 6l4 4 4-4z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    padding-right: 38px;
}

.dlg-control:focus {
    outline: none;
    border-color: var(--sage);
    background: var(--surface);
    box-shadow: 0 0 0 3px var(--sage-50);
}

.dlg-control.con-error {
    border-color: var(--rose);
}

.dlg-control.con-error:focus {
    box-shadow: 0 0 0 3px var(--rose-50);
}

.dlg-ayuda {
    font-size: var(--fs-xs);
    color: var(--ink-3);
}

.dlg-error:empty {
    display: none;
}

.dlg-error {
    font-size: var(--fs-xs);
    font-weight: 600;
    color: var(--rose);
}

.dlg-acciones {
    display: flex;
    gap: var(--sp-2);
    padding: var(--sp-4) var(--sp-5) var(--sp-5);
    border-top: 1px solid var(--line);
    background: var(--surface);
    border-radius: 0 0 var(--r-lg) var(--r-lg);
}

.dlg-btn {
    font-family: inherit;
    flex: 1;
    white-space: nowrap;
    padding: 14px var(--sp-4);
    font-size: var(--fs-md);
    font-weight: 600;
    border-radius: var(--r-md);
    border: 1.5px solid transparent;
    cursor: pointer;
    transition: background .15s, border-color .15s, color .15s;
}

.dlg-btn-secundario {
    background: var(--surface);
    border-color: var(--line-fuerte);
    color: var(--ink-2);
}

.dlg-btn-secundario:hover {
    background: var(--surface-2);
    color: var(--ink);
}

.dlg-btn-principal {
    flex: 1.5;
    background: var(--sage);
    color: #fff;
}

.dlg-btn-principal:hover {
    background: var(--sage-600);
}

.dlg.peligro .dlg-btn-principal {
    background: var(--rose);
}

.dlg.peligro .dlg-btn-principal:hover {
    background: #8a2f2f;
}

/* --------------------------------------------------------------------------
   Avisos (reemplazan los toast de SweetAlert2 y los alert() del sistema)
   -------------------------------------------------------------------------- */

.avisos {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: calc(90px + env(safe-area-inset-bottom));
    z-index: 600;
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    width: min(420px, calc(100% - 32px));
    pointer-events: none;
}

.aviso {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 14px 18px;
    border-radius: var(--r-md);
    font-size: var(--fs-sm);
    font-weight: 600;
    box-shadow: var(--sh-3);
    animation: aviso-entrar .22s ease-out;
}

.aviso i {
    font-size: 20px;
}

.aviso-ok {
    background: var(--sage);
    color: #fff;
}

.aviso-error {
    background: var(--rose);
    color: #fff;
}

.aviso.saliendo {
    opacity: 0;
    transform: translateY(6px);
    transition: opacity .25s, transform .25s;
}

@keyframes aviso-entrar {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: none; }
}

/* --------------------------------------------------------------------------
   Móvil: pantalla completa con las acciones ARRIBA, como los formularios
   modales de iOS.

   El porqué: el teclado de iOS encoge solo el viewport visual, no el layout
   (`interactive-widget=resizes-content` es estándar pero Safari no lo
   implementa). Cualquier cosa anclada abajo acaba debajo del teclado. Con los
   botones arriba y el formulario desplazándose debajo, el teclado no puede
   tapar nada y no hay que medirlo.

   El DOM no cambia: el mismo <form> se recoloca con grid, y `display: contents`
   saca los dos botones de su fila para que entren en la rejilla junto al título.
   -------------------------------------------------------------------------- */

@media (max-width: 640px) {
    /* Base: hoja inferior. La usan los avisos y las confirmaciones, que no
       abren teclado y agradecen no comerse la pantalla entera. */
    .dlg {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: auto;
        width: 100%;
        max-width: none;
        margin: 0;
        border-radius: var(--r-lg) var(--r-lg) 0 0;
        max-height: 92dvh;
    }

    .dlg[open] {
        animation: dlg-subir .22s ease-out;   /* sube como una hoja de iOS */
    }

    @keyframes dlg-subir {
        from { transform: translateY(100%); }
        to   { transform: none; }
    }

    .dlg:not(.dlg-completo) .dlg-acciones {
        padding-bottom: calc(var(--sp-5) + env(safe-area-inset-bottom));
        border-radius: 0;
    }

    /* Formularios: pantalla completa */
    .dlg-completo {
        inset: 0;
        top: 0;
        height: 100%;
        max-height: none;
        border-radius: 0;
    }

    .dlg-completo .dlg-form {
        display: grid;
        grid-template-columns: auto 1fr auto;
        grid-template-rows: auto 1fr;
        grid-template-areas:
            "cancelar titulo guardar"
            "cuerpo   cuerpo cuerpo";
        align-items: center;
        height: 100%;
        max-height: none;
    }

    /* Los hijos (los dos botones) pasan a formar parte de la rejilla del form */
    .dlg-completo .dlg-acciones {
        display: contents;
    }

    .dlg-completo .dlg-titulo {
        grid-area: titulo;
        justify-content: center;
        text-align: center;
        font-size: var(--fs-md);
        padding: 0 var(--sp-2);
        /* Con «Guardar cambios» a un lado el hueco es justo: antes de deformar la
           barra, el título se recorta */
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .dlg-completo .dlg-btn-secundario { grid-area: cancelar; }
    .dlg-completo .dlg-btn-principal  { grid-area: guardar; }

    /* En la barra los botones son texto, no bloques: es lo que hace iOS */
    .dlg-completo .dlg-btn {
        background: none;
        border: none;
        color: var(--sage-600);
        padding: 0 var(--sp-3);
        font-size: var(--fs-md);
        flex: none;   /* en la rejilla cada botón ocupa lo que mide su texto */
    }

    .dlg-completo .dlg-btn-principal {
        font-weight: 700;
    }

    /* La barra: los tres elementos comparten fondo, alto y línea inferior */
    .dlg-completo .dlg-titulo,
    .dlg-completo .dlg-btn {
        min-height: 56px;
        padding-top: env(safe-area-inset-top);
        background: var(--surface);
        border-bottom: 1px solid var(--line);
        display: flex;
        align-items: center;
    }

    .dlg-completo .dlg-cuerpo {
        grid-area: cuerpo;
        align-self: stretch;
        padding-top: var(--sp-4);
        /* Aire al final para que el último campo pueda subir por encima del
           teclado cuando iOS desplaza el formulario al enfocarlo */
        padding-bottom: 50vh;
    }
}

/* Foto del tensiómetro dentro del diálogo de confirmación */
.dlg-imagen {
    width: 100%;
    max-height: 230px;
    object-fit: contain;
    border-radius: var(--r-md);
    background: var(--surface-2);
}

.dlg-error-global:empty {
    display: none;
}

.dlg-error-global {
    padding: 10px 12px;
    background: var(--rose-50);
    border-radius: var(--r-sm);
}

.dlg-nota {
    font-size: var(--fs-sm);
    color: var(--ink-2);
}

/* La valoración usada como nota se centra dentro del diálogo */
.dlg-cuerpo > .medicion-estado {
    align-self: flex-start;
    margin: 0;
}

/* --- Grabar una cita hablando -------------------------------------------- */

.voz-estado {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-3);
    padding: var(--sp-5) var(--sp-4);
    margin-top: var(--sp-3);
    font-size: var(--fs-md);
    font-weight: 600;
    color: var(--ink-2);
    background: var(--sage-50);
    border-radius: var(--r-md);
}

.voz-punto {
    width: 14px;
    height: 14px;
    border-radius: var(--r-full);
    background: var(--line-fuerte);
    flex: none;
}

.voz-estado.grabando .voz-punto {
    background: var(--rose);
    animation: voz-latido 1.2s ease-in-out infinite;
}

.voz-estado.grabando .voz-reloj {
    color: var(--rose);
    font-variant-numeric: tabular-nums;
}

@keyframes voz-latido {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%      { opacity: .35; transform: scale(.8); }
}

/* Quien tenga el movimiento reducido ve el punto rojo fijo, sin parpadeo */
@media (prefers-reduced-motion: reduce) {
    .voz-estado.grabando .voz-punto { animation: none; }
}

/* Lo que se ha entendido de un dictado, para leerlo antes que los campos */
.dlg-intro {
    margin: 0 0 var(--sp-4);
    padding: var(--sp-3) var(--sp-4);
    font-size: var(--fs-sm);
    line-height: 1.45;
    color: var(--ink-2);
    background: var(--sage-50);
    border-left: 3px solid var(--sage);
    border-radius: var(--r-sm);
}
