:root {
    color-scheme: light;
    --navy-950: #071b2f;
    --navy-900: #0c2946;
    --navy-800: #123a60;
    --navy-700: #18507f;
    --teal-600: #008f86;
    --teal-500: #0ba89c;
    --teal-100: #dff6f3;
    --blue-100: #e9f2fb;
    --ink: #152a3d;
    --muted: #647487;
    --line: #dce4ec;
    --surface: #ffffff;
    --canvas: #f3f6f9;
    --danger: #bd3d48;
    --danger-soft: #fff0f1;
    --warning: #9a6415;
    --warning-soft: #fff6df;
    --success: #167659;
    --success-soft: #e6f7ef;
    --shadow-sm: 0 1px 2px rgba(7, 27, 47, .05), 0 5px 16px rgba(7, 27, 47, .04);
    --shadow-lg: 0 18px 50px rgba(7, 27, 47, .16);
    --radius: 14px;
    --font: "Segoe UI", Inter, system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }
/* Los componentes con display:flex/grid no deben anular hidden. */
[hidden] { display: none !important; }
html { min-height: 100%; font-size: 100%; }
body {
    min-height: 100vh;
    margin: 0;
    background: var(--canvas);
    color: var(--ink);
    font-family: var(--font);
    font-size: 1rem;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: var(--navy-700); text-decoration: none; }
a:hover { color: var(--teal-600); }
img, canvas { max-width: 100%; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: .45rem; color: var(--navy-950); font-size: clamp(1.75rem, 3vw, 2.35rem); line-height: 1.12; letter-spacing: -.035em; }
h2 { margin-bottom: .35rem; color: var(--navy-950); font-size: 1.2rem; line-height: 1.25; }
h3 { margin-bottom: .35rem; color: var(--navy-950); }
code { font-family: "Cascadia Mono", Consolas, monospace; font-size: .78rem; overflow-wrap: anywhere; }

:focus-visible { outline: 3px solid rgba(11, 168, 156, .3); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.muted { color: var(--muted); }
.eyebrow { margin-bottom: .5rem; color: var(--teal-600); font-size: .75rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm); }

.topbar {
    position: sticky;
    z-index: 20;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 70px;
    padding: .65rem max(1.25rem, calc((100vw - 1240px) / 2));
    background: rgba(255, 255, 255, .96);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(12px);
}
.brand { display: inline-flex; align-items: center; gap: .65rem; color: var(--navy-950); }
.brand:hover { color: var(--navy-950); }
.brand-mark {
    display: grid;
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    place-items: center;
    color: white;
    background: linear-gradient(145deg, var(--navy-800), var(--teal-500));
    border-radius: 10px 10px 10px 3px;
    font-size: 1.25rem;
    font-weight: 800;
    box-shadow: 0 6px 16px rgba(12, 67, 101, .18);
}
.brand > span:last-child { display: flex; flex-direction: column; line-height: 1.15; }
.brand strong { font-size: 1rem; letter-spacing: .01em; }
.brand small { margin-top: .15rem; color: var(--muted); font-size: .7rem; }
.brand-inverse, .brand-inverse:hover { color: white; }
.brand-inverse small { color: rgba(255, 255, 255, .68); }
.centered-brand { justify-content: center; }
.topnav { display: flex; min-width: 0; align-items: center; gap: .85rem; }
.topnav-links { display: flex; align-items: center; gap: .85rem; }
.topnav-links a { color: var(--ink); font-size: .78rem; font-weight: 650; white-space: nowrap; }
.user-menu { position: relative; }
.user-menu summary { padding: .5rem; cursor: pointer; color: var(--ink); font-size: .88rem; font-weight: 600; list-style: none; }
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary::after { content: "⌄"; margin-left: .35rem; color: var(--muted); }
.user-menu-panel { position: absolute; right: 0; top: calc(100% + .6rem); min-width: 170px; padding: .35rem; background: white; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lg); }
.user-menu-panel > a { display: block; padding: .55rem .7rem; color: var(--ink); border-radius: 7px; font-size: .86rem; font-weight: 650; }
.user-menu-panel > a:hover { background: var(--blue-100); }
.mobile-menu-links { display: none; }
.mobile-menu-links a { display: block; padding: .55rem .7rem; color: var(--ink); border-radius: 7px; font-size: .86rem; font-weight: 650; }
.mobile-menu-links a:hover { background: var(--blue-100); }
.user-menu form { margin: 0; }
.user-menu button { width: 100%; padding: .55rem .7rem; cursor: pointer; color: var(--danger); background: none; border: 0; border-radius: 7px; text-align: left; }
.user-menu button:hover { background: var(--danger-soft); }
.app-main { width: min(1240px, calc(100% - 2.5rem)); margin: 0 auto; padding: 2.3rem 0 4rem; }
.external-main { min-height: 100vh; }

.button {
    display: inline-flex;
    min-height: 42px;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    padding: .65rem 1rem;
    cursor: pointer;
    border: 1px solid transparent;
    border-radius: 9px;
    font-weight: 700;
    line-height: 1;
    transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button:disabled { cursor: not-allowed; opacity: .55; transform: none; }
.button-primary { color: white; background: var(--navy-800); border-color: var(--navy-800); box-shadow: 0 5px 14px rgba(18, 58, 96, .18); }
.button-primary:hover { color: white; background: var(--navy-700); }
.button-secondary { color: var(--ink); background: white; border-color: var(--line); }
.button-secondary:hover { color: var(--navy-800); background: var(--blue-100); border-color: #cbd9e6; }
.button-danger { color: white; background: var(--danger); border-color: var(--danger); }
.button-danger:hover { color: white; background: #a72e39; }
.button-small { min-height: 34px; padding: .45rem .7rem; font-size: .82rem; }
.button-block { width: 100%; }
.button.loading { pointer-events: none; opacity: .68; }

.field-label { display: flex; min-width: 0; flex-direction: column; gap: .4rem; color: var(--ink); font-size: .875rem; font-weight: 700; }
input, select, textarea {
    width: 100%;
    min-height: 44px;
    padding: .68rem .75rem;
    color: var(--ink);
    background: white;
    border: 1px solid #c8d3de;
    border-radius: 8px;
    font-weight: 400;
    outline: none;
}
input:hover, select:hover, textarea:hover { border-color: #9fb1c2; }
input:focus, select:focus, textarea:focus { border-color: var(--teal-600); box-shadow: 0 0 0 3px rgba(0, 143, 134, .12); }
.form-error { margin: 0; padding: .65rem .75rem; color: #9d2934; background: var(--danger-soft); border: 1px solid #f1c9cd; border-radius: 8px; font-size: .86rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .65rem; padding-top: .35rem; }
.stack-form { display: grid; gap: 1rem; }

.page-heading { margin-bottom: 1.7rem; }
.page-heading > p:last-child { margin-bottom: 0; color: var(--muted); }
.heading-actions { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; }
.back-link { display: inline-block; margin-bottom: 1.35rem; color: var(--muted); font-size: .88rem; font-weight: 700; }
.back-link:hover { color: var(--teal-600); }
.section-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.section-heading h2 { margin: 0; }
.section-heading p { margin: .25rem 0 0; color: var(--muted); font-size: .88rem; }

.status { display: inline-flex; width: fit-content; align-items: center; padding: .26rem .55rem; color: var(--muted); background: #eef2f5; border-radius: 999px; font-size: .72rem; font-weight: 800; white-space: nowrap; }
.status-draft { color: #566576; background: #edf1f4; }
.status-pending, .status-viewed { color: var(--warning); background: var(--warning-soft); }
.status-partially_signed { color: #2c6190; background: var(--blue-100); }
.status-completed, .status-signed { color: var(--success); background: var(--success-soft); }
.status-cancelled, .status-expired, .status-rejected { color: var(--danger); background: var(--danger-soft); }

.toast-region { position: fixed; z-index: 1000; right: 1.2rem; bottom: 1.2rem; display: grid; gap: .5rem; pointer-events: none; }
.toast { max-width: min(380px, calc(100vw - 2rem)); padding: .8rem 1rem; color: white; background: var(--navy-950); border-radius: 9px; box-shadow: var(--shadow-lg); font-size: .88rem; animation: toast-in .2s ease-out; }
.toast.error { background: #922d38; }
.action-dialog { width: min(560px, calc(100% - 1.25rem)); padding: 0; border: 0; border-radius: 14px; box-shadow: var(--shadow-lg); }
.action-dialog::backdrop { background: rgba(7, 27, 47, .68); backdrop-filter: blur(3px); }
.action-dialog form { display: grid; padding: 1.2rem; gap: 1rem; }
.modal-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.modal-heading h2, .modal-heading p { margin-bottom: 0; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1050px) {
    .topnav-links { display: none; }
    .mobile-menu-links { display: block; padding-bottom: .3rem; margin-bottom: .3rem; border-bottom: 1px solid var(--line); }
}

@media (max-width: 720px) {
    .topbar { min-height: 62px; padding: .6rem 1rem; }
    .topnav-links { display: none; }
    .mobile-menu-links { display: block; padding-bottom: .3rem; margin-bottom: .3rem; border-bottom: 1px solid var(--line); }
    .topnav { gap: .4rem; }
    .topnav .button { display: none; }
    .user-menu summary { max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .app-main { width: min(100% - 1.25rem, 1240px); padding: 1.4rem 0 3rem; }
    .heading-actions { align-items: stretch; flex-direction: column; }
    .heading-actions > .button { width: 100%; }
    .form-actions { flex-direction: column-reverse; }
    .form-actions .button { width: 100%; }
}
