:root {
    --primary-color: #C1121F;
    --secondary-color: #1D3557;
    --primary-contrast: #ffffff;
    --bg: #f4f6f8;
    --surface: #ffffff;
    --text: #14202e;
    --muted: #5c6b7a;
    --border: #dbe3ea;
    --danger: #c1121f;
    --success: #2a9d8f;
    --warning: #e9c46a;
    --radius: 16px;
    --shadow: 0 10px 30px rgba(20, 32, 46, 0.08);
    --touch: 52px;
    --font: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.45;
    min-height: 100vh;
    max-width: 100%;
    overflow-x: hidden;
}

img { max-width: 100%; height: auto; }
a { color: var(--primary-color); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }
.container { width: min(1120px, calc(100% - 24px)); margin: 0 auto; }
.container-narrow { width: min(640px, calc(100% - 24px)); margin: 0 auto; }

.topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 64px;
    padding: 8px 12px;
    gap: 10px;
    flex-wrap: wrap;
    max-width: 100%;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 800; min-width: 0; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand img { width: 40px; height: 40px; object-fit: contain; border-radius: 10px; flex: 0 0 auto; }
.brand-logo {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    background: transparent;
}
.brand-logo img { width: 100%; height: 100%; object-fit: contain; border-radius: 0; }
.brand:hover { text-decoration: none; }

.nav-toggle {
    width: var(--touch);
    height: var(--touch);
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 5px;
    cursor: pointer;
}
.nav-toggle span { display: block; width: 20px; height: 2px; background: var(--text); }

.nav-drawer {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(300px, 84vw);
    z-index: 70;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 14px 14px calc(24px + env(safe-area-inset-bottom));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform .22s ease, visibility .22s ease;
    box-shadow: 0 0 40px rgba(20, 32, 46, .25);
}
.nav-drawer.open { transform: translateX(0); visibility: visible; }
.nav-drawer .drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
}
.drawer-close {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
    flex: 0 0 auto;
}
.nav-overlay {
    position: fixed;
    inset: 0;
    background: rgba(17, 24, 39, .5);
    z-index: 60;
    opacity: 0;
    visibility: hidden;
    transition: opacity .22s ease, visibility .22s ease;
}
.nav-overlay.open { opacity: 1; visibility: visible; }
body.nav-open { overflow: hidden; }
.nav-drawer a, .side-nav a {
    display: flex;
    align-items: center;
    min-height: var(--touch);
    padding: 0 14px;
    border-radius: 12px;
    color: var(--text);
    font-weight: 600;
}
.nav-drawer a.active, .side-nav a.active {
    background: #f1f3f6;
    background: color-mix(in srgb, var(--primary-color) 12%, white);
    color: var(--primary-color);
    text-decoration: none;
}

.layout-admin, .layout-account {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    min-height: 100vh;
    max-width: 100%;
}
.layout-admin > *, .layout-account > * { min-width: 0; max-width: 100%; }
.sidebar {
    display: none;
    background: var(--secondary-color);
    color: #fff;
    padding: 20px 14px;
}
.sidebar .brand { color: #fff; margin-bottom: 18px; }
.side-nav a { color: #fff; }
.side-nav a.active { background: var(--primary-color); background: color-mix(in srgb, var(--primary-color) 80%, black); color: #fff; }
.main { padding: 16px 0 96px; flex: 1; min-width: 0; max-width: 100%; }
@media (max-width: 899px) {
    .layout-admin .main { padding-left: 12px; padding-right: 12px; padding-bottom: 48px; }
}

.page-head { margin: 8px 0 18px; align-items: center; }
.page-head h1 { margin: 0 0 6px; font-size: 1.6rem; }
.page-head .btn { align-self: center; }
.page-head.with-action { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.page-head.with-action > div { min-width: 0; flex: 1 1 240px; }
.page-head.with-action p { margin: 0; }
.muted { color: var(--muted); }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    margin-bottom: 16px;
    max-width: 100%;
    min-width: 0;
    overflow-wrap: break-word;
    word-wrap: break-word;
}
.card h2, .card h3 { margin-top: 0; }

.grid { display: grid; gap: 12px; min-width: 0; }
.grid > * { min-width: 0; }
.grid-2 { grid-template-columns: minmax(0, 1fr); }
.stats { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.stat {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    border: 1px solid var(--border);
}
.stat b { display: block; font-size: 1.6rem; color: var(--primary-color); }

label { display: block; font-weight: 700; margin: 0 0 6px; }
.hint { font-weight: 400; color: var(--muted); font-size: .9rem; }
.field { margin-bottom: 16px; }
input:not([type]),
input[type="text"], input[type="email"], input[type="password"], input[type="tel"],
input[type="number"], input[type="date"], input[type="datetime-local"], input[type="time"],
input[type="url"], input[type="color"], input[type="search"],
select, textarea {
    width: 100%;
    max-width: 100%;
    min-height: var(--touch);
    border: 1.5px solid var(--border);
    border-radius: 14px;
    padding: 12px 14px;
    font-size: 16px;
    background: #fff;
    color: var(--text);
    -webkit-appearance: none;
    appearance: none;
}
select {
    background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%235c6b7a'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 22px 22px;
    padding-right: 40px;
}
input, select, textarea, button { font-family: inherit; }
input[type="date"] { min-height: var(--touch); line-height: 1.2; display: block; }
input[type="color"] { padding: 4px; height: var(--touch); width: 72px; min-width: 72px; cursor: pointer; }
input[type="file"] {
    width: 100%;
    max-width: 100%;
    min-height: var(--touch);
    border: 1.5px dashed var(--border);
    border-radius: 14px;
    padding: 12px 14px;
    font-size: .95rem;
    background: #fff;
    color: var(--muted);
}
textarea { min-height: 120px; resize: vertical; }
.field > label > input[type="checkbox"], label > input[type="checkbox"] { width: auto; min-height: 0; margin-right: 8px; }

.filters { display: grid; gap: 12px; align-items: end; grid-template-columns: minmax(0, 1fr); }
.filters .field { margin: 0; min-width: 0; }
.filters .field-action .btn { width: 100%; }
@media (min-width: 720px) {
    .filters { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) auto; }
    .filters.filters-single { grid-template-columns: minmax(0, 1fr) auto; }
    .filters .field-action .btn { min-width: 150px; }
}

.color-row { display: flex; gap: 10px; align-items: center; }
.color-row input[type="text"] { flex: 1 1 auto; min-width: 0; text-transform: uppercase; }

.range-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.range-row output { font-weight: 800; min-width: 56px; text-align: right; }
input[type="range"] { width: 100%; accent-color: var(--primary-color); }
.logo-preview {
    width: 96px;
    height: 96px;
    border: 1px solid var(--border);
    border-radius: 16px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
}
.logo-preview img { width: 100%; height: 100%; object-fit: contain; }
input:focus, select:focus, textarea:focus {
    outline: 3px solid #e6eaf0;
    outline: 3px solid color-mix(in srgb, var(--primary-color) 25%, white);
    border-color: var(--primary-color);
}
.field.error input, .field.error textarea, .field.error select { border-color: var(--danger); }
.error-text { color: var(--danger); font-size: .9rem; margin-top: 4px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 48px;
    padding: 12px 20px;
    border-radius: 14px;
    border: 0;
    font-weight: 800;
    font-size: 1rem;
    cursor: pointer;
    text-decoration: none;
    width: 100%;
    max-width: 100%;
    line-height: 1.2;
    -webkit-appearance: none;
    appearance: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.btn:hover { text-decoration: none; filter: brightness(.96); }
.btn-primary { background: var(--primary-color); color: var(--primary-contrast); }
.btn-secondary { background: var(--secondary-color); color: #fff; }
.btn-ghost { background: #edf1f5; color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-compact { min-height: 44px; width: auto; padding: 8px 14px; font-size: .9rem; }
.btn-row { display: grid; gap: 10px; }
.btn-big { min-height: 56px; font-size: 1.05rem; letter-spacing: .02em; }
.sidebar .btn { width: 100%; max-width: 100%; }

.password-row { display: flex; gap: 8px; align-items: stretch; flex-wrap: wrap; }
.password-row input { flex: 1 1 180px; min-width: 0; }
.password-row .btn { width: auto; min-width: 92px; flex: 0 0 auto; }

.contact-editor .table-wrap { margin-bottom: 10px; }
.contact-table { min-width: 0; width: 100%; table-layout: fixed; }
.contact-table th, .contact-table td { padding: 6px 4px; }
.contact-table th:last-child, .contact-table td:last-child { width: 84px; }
.contact-table input { min-height: 44px; padding: 10px; }
.profile-url { word-break: break-all; font-weight: 800; }
.yesno-text { margin-top: 10px; }
.emergency-page .contact-table { color: #fff; }
.emergency-page .contact-table th, .emergency-page .contact-table td { border-color: #374151; }

.alert {
    border-radius: 14px;
    padding: 14px 16px;
    margin-bottom: 14px;
    font-weight: 600;
}
.alert-success { background: #e8f6f3; color: #0f5c53; }
.alert-error { background: #fde8ea; color: #8a1018; }

.table-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; min-width: 640px; }
th, td { text-align: left; padding: 12px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .85rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 4px 10px;
    font-size: .8rem;
    font-weight: 800;
    background: #edf1f5;
}
.badge-public { background: #e8f6f3; color: #0f5c53; }
.badge-sensitive { background: #fde8ea; color: #8a1018; }
.badge-new { background: #e8f0fe; color: #1d4ed8; }
.badge-converted { background: #e8f6f3; color: #0f5c53; }
.badge-rejected { background: #fde8ea; color: #8a1018; }

.hero {
    background: var(--secondary-color);
    background: linear-gradient(160deg, var(--secondary-color), var(--primary-color));
    background: linear-gradient(160deg, var(--secondary-color), color-mix(in srgb, var(--primary-color) 55%, var(--secondary-color)));
    color: #fff;
    padding: 28px 0 36px;
}
.hero h1 { margin: 0 0 8px; font-size: 1.8rem; }
.hero p { margin: 0; opacity: .92; }

.emergency-page { background: #111827; color: #fff; min-height: 100vh; }
.emergency-page .card { background: #1f2937; border-color: #374151; color: #fff; }
.emergency-name { font-size: 1.8rem; margin: 0 0 8px; }
.kv { padding: 12px 0; border-bottom: 1px solid #374151; }
.kv .k { color: #9ca3af; font-size: .85rem; font-weight: 700; text-transform: uppercase; }
.kv .v { font-size: 1.15rem; font-weight: 700; margin-top: 4px; }
.code-box {
    font-family: ui-monospace, Consolas, monospace;
    font-size: 1.4rem;
    letter-spacing: .18em;
    background: #111;
    color: #fff;
    padding: 14px;
    border-radius: 12px;
    text-align: center;
}

.bottom-nav {
    position: sticky;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.bottom-nav a {
    text-align: center;
    color: var(--muted);
    font-size: .75rem;
    font-weight: 800;
    padding: 8px 4px;
    text-decoration: none;
}
.bottom-nav a.active { color: var(--primary-color); }

.vis-toggle { display: flex; gap: 8px; }
.vis-toggle label {
    flex: 1;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1.5px solid var(--border);
    border-radius: 12px;
    font-weight: 700;
    margin: 0;
    cursor: pointer;
}
.vis-toggle input { margin-right: 6px; }

.token { font-family: ui-monospace, Consolas, monospace; letter-spacing: .08em; font-weight: 800; }
.hidden { display: none !important; }
.scanner { background: #000; border-radius: 16px; overflow: hidden; min-height: 220px; }
#qr-video { width: 100%; display: block; object-fit: cover; }
.qr-block { display: grid; gap: 10px; justify-items: center; margin: 12px 0; }
.qr-box {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 16px;
    min-height: 180px;
    min-width: 180px;
}
.qr-box canvas, .qr-box img { max-width: 100%; height: auto; display: block; }
.qr-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.qr-actions .btn { width: auto; }
.qr-caption { font-size: .85rem; color: var(--muted); text-align: center; }

.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.topbar-actions form { margin: 0; }
.topbar-actions .btn { width: auto; min-width: 0; }

.public-nav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.public-nav form { margin: 0; }
.public-nav a, .public-nav .btn { width: auto; min-height: 44px; }
@media (max-width: 400px) {
    .public-nav .btn { padding: 8px 10px; font-size: .82rem; min-width: 0; }
    .brand span { font-size: .95rem; }
}

/* Safari en iOS calcula 100vh incluyendo la barra de direcciones. */
@supports (-webkit-touch-callout: none) {
    body, .app-shell, .layout-admin, .layout-account, .emergency-page { min-height: -webkit-fill-available; }
}

@media (min-width: 900px) {
    .nav-toggle { display: none; }
    .nav-drawer, .nav-overlay { display: none !important; }
    .layout-admin, .layout-account { grid-template-columns: 260px minmax(0, 1fr); }
    .sidebar { display: block; }
    .topbar .mobile-only { display: none; }
    .bottom-nav { display: none; }
    .grid-2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .btn {
        width: auto;
        min-width: 160px;
        max-width: 280px;
        min-height: 48px;
        padding: 12px 22px;
    }
    .btn-big { min-width: 220px; max-width: 360px; }
    .page-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 12px; }
    .page-head .btn { min-height: 44px; min-width: 150px; max-width: 220px; padding: 10px 18px; font-size: .95rem; }
    .btn-compact { min-width: 0; max-width: none; }
    .btn-row { display: flex; flex-wrap: wrap; align-items: center; }
    .btn-row .btn { width: auto; flex: 0 0 auto; }
    .btn-row.inline { display: flex; flex-wrap: wrap; width: auto; }
    .password-row .btn { min-width: 120px; max-width: 160px; }
    .sidebar .btn { width: 100%; max-width: 100%; }
    .main { padding: 24px 28px 48px; }
    .hero h1 { font-size: 2.4rem; }
}
