/* ============================================================================
   ICOPAD — Modal system v1
   Accessible, themable, no framework.
   Works with native <dialog> or a <div class="modal"> fallback.
   ============================================================================ */

/* Prevent background scroll */
html.modal-open,
body.modal-open {
    overflow: hidden;
    /* Reserve scrollbar width so layout doesn't shift */
    padding-right: var(--modal-scrollbar-width, 0);
}

/* ═══════════════════════════════════════════════════════════════════════
   BASE MODAL
   ═══════════════════════════════════════════════════════════════════════ */

.modal {
    /* Fallback (non-dialog) path — hidden by default */
    position: fixed;
    inset: 0;
    z-index: 10050;
    display: none;
    align-items: center;
    justify-content: center;
    padding: clamp(12px, 4vw, 32px);
    opacity: 0;
    transition: opacity 220ms var(--ease-out, ease);
}

/* Native <dialog> reset */
dialog.modal {
    border: 0;
    background: transparent;
    padding: clamp(12px, 4vw, 32px);
    max-width: 100vw;
    max-height: 100dvh;
    width: 100%;
    height: 100%;
    overflow: visible;
}
dialog.modal::backdrop { background: transparent; }

/* Open state */
.modal.is-open { display: flex; }
.modal.is-visible { opacity: 1; }
.modal[hidden] { display: none; }

/* ═══════════════════════════════════════════════════════════════════════
   BACKDROP
   ═══════════════════════════════════════════════════════════════════════ */

.modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgb(11 10 10 / 0.55);
    backdrop-filter: blur(6px) saturate(140%);
    -webkit-backdrop-filter: blur(6px) saturate(140%);
    animation: modal-fade-in 220ms var(--ease-out, ease) forwards;
}

/* ═══════════════════════════════════════════════════════════════════════
   DIALOG PANEL
   ═══════════════════════════════════════════════════════════════════════ */

.modal__dialog {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-height: calc(100dvh - clamp(24px, 8vw, 64px));
    background: var(--surface, #fff);
    color: var(--text, #111);
    border: 1px solid var(--border, #e5e5e5);
    border-radius: 18px;
    box-shadow:
        0 32px 64px -16px rgb(22 20 19 / 0.30),
        0 8px 24px -8px rgb(22 20 19 / 0.15),
        0 0 0 1px rgb(22 20 19 / 0.02);
    overflow: hidden;
    animation: modal-rise 240ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
    transform-origin: center;
}

/* ── Size presets ──────────────────────────────────────────────────── */

.modal--sm   .modal__dialog { max-width: 440px; }
.modal--md   .modal__dialog { max-width: 620px; }
.modal--lg   .modal__dialog { max-width: 820px; }
.modal--xl   .modal__dialog { max-width: 1100px; }
.modal--full .modal__dialog {
    max-width: none;
    height: calc(100dvh - clamp(24px, 8vw, 64px));
    border-radius: 14px;
}

@media (max-width: 640px) {
    .modal__dialog { max-width: 100% !important; border-radius: 14px; }
    .modal { padding: 12px; }
}

/* ═══════════════════════════════════════════════════════════════════════
   HEADER / BODY / FOOTER
   ═══════════════════════════════════════════════════════════════════════ */

.modal__header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 18px 22px;
    border-bottom: 1px solid var(--border-subtle, #eaeaea);
    flex-shrink: 0;
    background: var(--surface, #fff);
}

.modal__title {
    margin: 0;
    font-size: 1.0625rem;
    font-weight: 700;
    letter-spacing: -0.015em;
    color: var(--text, #111);
    line-height: 1.3;
    flex: 1;
    min-width: 0;
}

.modal__eyebrow {
    display: block;
    font-size: .6875rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .14em;
    color: var(--brand, #0a4d8c);
    margin: 0 0 4px;
}

.modal__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    background: transparent;
    border: 1px solid var(--border, #e5e5e5);
    border-radius: 8px;
    color: var(--text-muted, #666);
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.modal__close:hover {
    background: var(--surface-hover, #f3f3f3);
    color: var(--text, #111);
    border-color: var(--border-strong, #ccc);
}
.modal__close:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand, #0a4d8c) 25%, transparent);
}

.modal__body {
    padding: 22px;
    overflow-y: auto;
    overscroll-behavior: contain;
    flex: 1 1 auto;
    min-height: 0;
    font-size: .9375rem;
    line-height: 1.6;
    color: var(--text, #111);
}

.modal__footer {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
    padding: 16px 22px;
    border-top: 1px solid var(--border-subtle, #eaeaea);
    background: var(--bg-subtle, #fafafa);
    flex-shrink: 0;
    flex-wrap: wrap;
}

@media (max-width: 640px) {
    .modal__header { padding: 14px 16px; }
    .modal__body   { padding: 16px; }
    .modal__footer { padding: 12px 16px; }
    .modal__footer .btn { flex: 1 1 auto; justify-content: center; }
}

/* Optional inline field (used by prompt()) */
.modal__field {
    padding: 0 22px 22px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.modal__field-label {
    font-size: .8125rem;
    font-weight: 600;
    color: var(--text, #111);
}
.modal__input {
    padding: 10px 12px;
    font: inherit;
    font-size: .9375rem;
    background: var(--surface, #fff);
    border: 1px solid var(--border-strong, #d4d4d4);
    border-radius: 9px;
    color: var(--text, #111);
    transition: border-color .15s ease, box-shadow .15s ease;
}
.modal__input:focus {
    outline: none;
    border-color: var(--brand, #0a4d8c);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand, #0a4d8c) 20%, transparent);
}
.modal__input[aria-invalid="true"] {
    border-color: #dc2626;
    box-shadow: 0 0 0 3px rgb(220 38 38 / 0.20);
}

/* ═══════════════════════════════════════════════════════════════════════
   VARIANTS
   ═══════════════════════════════════════════════════════════════════════ */

/* Danger (destructive actions) — reinforces the red accent */
.modal--danger .modal__eyebrow { color: #dc2626; }
.modal--danger .modal__header  { border-bottom-color: #fecaca; }

/* Attention (amber) — for moderated / warning contexts */
.modal--attention .modal__eyebrow { color: #b45309; }
.modal--attention .modal__header  { border-bottom-color: #fde68a; }

/* ═══════════════════════════════════════════════════════════════════════
   KEYFRAMES
   ═══════════════════════════════════════════════════════════════════════ */

@keyframes modal-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

@keyframes modal-rise {
    from { opacity: 0; transform: translateY(8px) scale(0.985); }
    to   { opacity: 1; transform: translateY(0)   scale(1); }
}

/* ═══════════════════════════════════════════════════════════════════════
   REDUCED MOTION
   ═══════════════════════════════════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
    .modal,
    .modal__backdrop,
    .modal__dialog {
        animation-duration: .01ms !important;
        transition-duration: .01ms !important;
    }
}