/* ==========================================================================
   Descuento Locatario: mobile-first, modo claro/oscuro
   ========================================================================== */

:root {
    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --text: #0f172a;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --primary: #1d4ed8;
    --primary-hover: #1e40af;
    --primary-contrast: #ffffff;
    --primary-soft: #dbeafe;
    --success: #15803d;
    --success-soft: #dcfce7;
    --danger: #b91c1c;
    --danger-soft: #fee2e2;
    --warning: #92400e;
    --warning-soft: #fef3c7;
    --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 8px 24px rgb(15 23 42 / .08);
    --radius: 16px;
    --radius-sm: 10px;
    --gutter: 16px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1120;
        --surface: #111827;
        --surface-2: #1f2937;
        --text: #f1f5f9;
        --text-muted: #94a3b8;
        --border: #263244;
        --primary: #3b82f6;
        --primary-hover: #60a5fa;
        --primary-contrast: #0b1120;
        --primary-soft: #172554;
        --success: #4ade80;
        --success-soft: #052e16;
        --danger: #f87171;
        --danger-soft: #450a0a;
        --warning: #fbbf24;
        --warning-soft: #422006;
        --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 8px 24px rgb(0 0 0 / .35);
    }
}

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

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

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 var(--font);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: none;
}

[hidden] { display: none !important; }

h1, h2, p { margin: 0; }

svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

/* ---------- Estructura ---------- */

.app {
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
    padding: var(--gutter);
    padding-bottom: calc(var(--gutter) + var(--safe-bottom));
}

body:not(.con-sesion) .app { padding-top: calc(var(--gutter) + var(--safe-top)); }

.screen {
    display: flex;
    flex-direction: column;
    gap: var(--gutter);
    animation: entrar .2s ease-out;
}

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

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}

.card__title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 16px;
}

/* ---------- Barra superior ---------- */

.appbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: calc(10px + var(--safe-top)) var(--gutter) 10px;
    background: var(--primary);
    color: var(--primary-contrast);
}

@media (prefers-color-scheme: dark) {
    .appbar { background: var(--surface); color: var(--text); border-bottom: 1px solid var(--border); }
}

.appbar__info {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.25;
}

.appbar__local {
    font-weight: 700;
    font-size: 1.05rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.appbar__codigo { font-size: .85rem; opacity: .85; }

.icon-btn {
    flex: none;
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: inherit;
    cursor: pointer;
}

.icon-btn:hover { background: rgb(255 255 255 / .15); }

/* ---------- Banner sin conexión ---------- */

.offline-banner {
    position: sticky;
    top: 0;
    z-index: 30;
    padding: calc(6px + var(--safe-top)) var(--gutter) 6px;
    background: var(--warning-soft);
    color: var(--warning);
    font-size: .9rem;
    font-weight: 600;
    text-align: center;
}

/* ---------- Formularios ---------- */

form { display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.field > span:first-child {
    font-size: .9rem;
    font-weight: 600;
    color: var(--text-muted);
}

input {
    width: 100%;
    min-height: 48px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text);
    /* 16px evita el zoom automático de iOS al enfocar */
    font: inherit;
    font-size: 16px;
}

input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.field__password { position: relative; display: block; }
.field__password input { padding-right: 52px; }

.field__toggle {
    position: absolute;
    top: 50%;
    right: 4px;
    transform: translateY(-50%);
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border: 0;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}

.field__toggle[aria-pressed="true"] { color: var(--primary); }

.form-error {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    background: var(--danger-soft);
    color: var(--danger);
    font-size: .95rem;
    font-weight: 500;
}

.hint { color: var(--text-muted); font-size: .9rem; }

.grid-2 { display: grid; gap: 14px; }

@media (min-width: 420px) {
    .grid-2 { grid-template-columns: 1fr 1fr; }
}

/* ---------- Botones ---------- */

.btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 52px;
    padding: 12px 20px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font: inherit;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color .15s, transform .05s;
    user-select: none;
}

.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn--block { width: 100%; }

.btn--primary { background: var(--primary); color: var(--primary-contrast); }
.btn--primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn--secondary { background: var(--primary-soft); color: var(--primary); }

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

.btn--danger { background: var(--danger-soft); color: var(--danger); }

.btn.cargando .btn__label { visibility: hidden; }

.btn.cargando::after {
    content: "";
    position: absolute;
    width: 22px;
    height: 22px;
    border: 3px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: girar .7s linear infinite;
}

@keyframes girar { to { transform: rotate(360deg); } }

.actions {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 10px;
    margin-top: 4px;
}

/* ---------- Login ---------- */

.screen--login { min-height: calc(100dvh - 2 * var(--gutter)); justify-content: center; }

.login__brand { text-align: center; margin-bottom: 24px; }

.login__brand img { border-radius: 18px; margin-bottom: 12px; }

.login__brand h1 { font-size: 1.5rem; font-weight: 800; }

.login__brand p { color: var(--text-muted); margin-top: 4px; }

.install-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-radius: var(--radius);
    background: var(--primary-soft);
    color: var(--text);
}

.install-card > div { display: flex; flex-direction: column; flex: 1; min-width: 0; font-size: .9rem; }
.install-card strong { font-size: 1rem; }
.install-card .btn { min-height: 44px; flex: none; }

/* ---------- Escáner ---------- */

.scanner {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    max-height: 62dvh;
    margin: 0 auto;
    border-radius: var(--radius);
    overflow: hidden;
    background: #000;
    box-shadow: var(--shadow);
}

.scanner__video,
.scanner__video video {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover;
}

/* La guía visual la dibujamos nosotros; se oculta la de la librería */
.scanner__video #qr-shaded-region { display: none !important; }

.scanner__frame {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    pointer-events: none;
}

.scanner__frame span {
    width: 70%;
    aspect-ratio: 1;
    border-radius: 20px;
    box-shadow: 0 0 0 100vmax rgb(0 0 0 / .45);
    outline: 3px solid rgb(255 255 255 / .9);
    outline-offset: -3px;
}

.scanner.leido .scanner__frame span { outline-color: #4ade80; }

.scanner__hint {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 12px;
    padding: 8px 12px;
    border-radius: 999px;
    background: rgb(0 0 0 / .6);
    color: #fff;
    font-size: .9rem;
    font-weight: 600;
    text-align: center;
}

.scanner__hint.error { background: rgb(185 28 28 / .9); }

.scan-actions { display: flex; flex-direction: column; gap: 12px; }

.manual {
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

/* ---------- Confirmación ---------- */

.ticket {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 14px 16px;
    margin-bottom: 18px;
    border: 2px dashed var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
}

.ticket__label { font-size: .8rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; }

.ticket__code {
    font-family: var(--mono);
    font-size: clamp(.95rem, 4.2vw, 1.15rem);
    font-weight: 600;
    word-break: break-all;
}

/* ---------- Resultado ---------- */

.result { text-align: center; }

.result__icon {
    display: grid;
    place-items: center;
    width: 76px;
    height: 76px;
    margin: 4px auto 14px;
    border-radius: 50%;
}

.result__icon svg { width: 40px; height: 40px; stroke-width: 2.5; }

.result.ok .result__icon { background: var(--success-soft); color: var(--success); }
.result.error .result__icon { background: var(--danger-soft); color: var(--danger); }

.result__title { font-size: 1.35rem; font-weight: 800; }
.result__message { color: var(--text-muted); margin-top: 6px; }

.amounts {
    margin: 20px 0;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    text-align: left;
}

.amounts > div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.amounts > div:first-child { border-top: 0; }
.amounts dt { color: var(--text-muted); }
.amounts dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

.amounts__total { background: var(--success-soft); }
.amounts__total dt, .amounts__total dd { color: var(--success); font-size: 1.15rem; font-weight: 800; }

.result .btn { margin-top: 20px; }
.result .amounts:not([hidden]) + .btn { margin-top: 0; }

/* ---------- Hoja inferior (menú, instrucciones iOS) ---------- */

.sheet {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.sheet__backdrop {
    position: absolute;
    inset: 0;
    background: rgb(15 23 42 / .55);
    animation: aparecer .15s ease-out;
}

.sheet__panel {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    max-width: 560px;
    padding: 22px var(--gutter) calc(var(--gutter) + var(--safe-bottom));
    border-radius: 22px 22px 0 0;
    background: var(--surface);
    box-shadow: var(--shadow);
    animation: subir .2s ease-out;
}

.sheet__title { font-size: 1.2rem; font-weight: 800; }
.sheet__user { color: var(--text-muted); margin-bottom: 8px; word-break: break-all; }

.steps { margin: 4px 0 12px; padding-left: 22px; display: flex; flex-direction: column; gap: 10px; }

.inline-icon { width: 20px; height: 20px; vertical-align: -4px; color: var(--primary); }

@keyframes aparecer { from { opacity: 0; } }
@keyframes subir { from { transform: translateY(100%); } }

/* ---------- Toast ---------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(20px + var(--safe-bottom));
    z-index: 60;
    max-width: calc(100% - 2 * var(--gutter));
    padding: 12px 18px;
    border-radius: 999px;
    background: var(--text);
    color: var(--surface);
    font-weight: 600;
    text-align: center;
    transform: translateX(-50%);
    animation: aparecer .15s ease-out;
}

/* ---------- Pantallas grandes ---------- */

@media (min-width: 768px) {
    :root { --gutter: 24px; }

    .app { padding-top: 32px; }

    .card { padding: 28px; }

    .scanner { max-width: 480px; }

    .sheet { align-items: center; }

    .sheet__panel { max-width: 420px; border-radius: 22px; padding-bottom: 22px; }
}

/* Teléfono en horizontal: cámara y acciones lado a lado */
@media (orientation: landscape) and (max-height: 520px) {
    .screen--scan {
        display: grid;
        grid-template-columns: auto 1fr;
        align-items: start;
    }

    .scanner { width: auto; height: calc(100dvh - 120px); max-height: none; }

    .screen--login { min-height: auto; }
}
