/* ============================================================================
   Tenant canonical modal primitives — `.tenant-modal*`
   Mirrors the .set-modal / .qbm-modal language used by settings/quotes/invoices
   so every global tenant modal (page-help, support, theme-picker, analytics-
   email, first-win pickers, guided-tour, import-pdf-loading) shares identical
   chrome, inputs, footer slots and mobile bottom-sheet behaviour.
   Values lifted from public/assets/css/settings-shell.css `.set-modal` block.

   Loaded UNCONDITIONALLY on every tenant page from app.php (after style.css,
   page-help-modal.css and support-modal.css), so source order + .modal.tenant-modal
   selectors win against any earlier page-specific chrome rules.
   ============================================================================ */

.tenant-modal {
    --tm-accent: var(--accent, #c49510);
    --tm-accent-soft: var(--accent-soft, rgba(196, 149, 16, 0.1));
    --tm-accent-strong: rgba(196, 149, 16, 0.34);
    --tm-ink: var(--ink, #1a1815);
    --tm-ink-muted: var(--ink-3, #6b6458);
    --tm-ink-faint: var(--ink-4, #9a9188);
    --tm-border: var(--rule, rgba(26, 24, 21, 0.08));
    --tm-border-rule: rgba(26, 24, 21, 0.07);
    --tm-border-input: rgba(26, 24, 21, 0.14);
    --tm-bg-warm: #f7f5f0;
    --tm-surface: var(--surface-card, #ffffff);
    --tm-surface-soft: #fcfaf4;

    /* --ipi-* token aliases so canonical primitives (.ipi-tabs, .ipi-tab,
       .ipi-filter-search) render correctly inside the modal scope. */
    --ipi-bg: var(--canvas, #f7f5f0);
    --ipi-bg-inset: var(--surface-inset, #edeae3);
    --ipi-bg-card: var(--surface-card, #ffffff);
    --ipi-bg-hover: rgba(26, 24, 21, 0.04);
    --ipi-rule: var(--rule, rgba(26, 24, 21, 0.08));
    --ipi-rule-h: rgba(26, 24, 21, 0.11);
    --ipi-ink: var(--ink, #1a1815);
    --ipi-ink-2: #3d3a34;
    --ipi-ink-3: var(--ink-3, #6b6458);
    --ipi-ink-4: var(--ink-4, #9a9188);
    --ipi-accent: var(--accent, #c49510);
    --ipi-accent-bg: rgba(196, 149, 16, 0.08);
    --ipi-accent-border: rgba(196, 149, 16, 0.34);
    --ipi-shadow-sm: 0 1px 3px rgba(26, 24, 21, 0.06), 0 1px 2px rgba(26, 24, 21, 0.04);
    --ipi-shadow-card: 0 1px 4px rgba(26, 24, 21, 0.06), 0 0 0 1px rgba(26, 24, 21, 0.05);
}

/* Default dialog: content-driven height. Sizes to fit content up to max-height.
   Use the --stable modifier on tabbed/multi-state modals where switching
   would otherwise cause jarring resizes. */
.modal.tenant-modal .tenant-modal__dialog {
    width: min(880px, calc(100vw - 32px));
    max-height: min(90vh, 960px);
    border-radius: 16px;
    border: 1px solid var(--tm-border);
    background: var(--tm-surface);
    box-shadow:
        0 20px 60px rgba(0, 0, 0, 0.18),
        0 4px 16px rgba(0, 0, 0, 0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0;
}

/* Stable height variant — pin the dialog so tab/step switches don't resize. */
.modal.tenant-modal .tenant-modal__dialog--stable {
    height: min(85vh, 760px);
    min-height: min(60vh, 520px);
}

.modal.tenant-modal .tenant-modal__dialog--narrow {
    width: min(560px, calc(100vw - 32px));
}

.modal.tenant-modal .tenant-modal__dialog--wide {
    width: min(1280px, calc(100vw - 32px));
}

/* Form wrapping body+footer must be flex so body's overflow-y:auto has a
   constrained parent. */
.modal.tenant-modal form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    margin: 0;
    width: 100%;
}

.modal.tenant-modal .tenant-modal__header {
    padding: 20px 22px 16px;
    border-bottom: 1px solid var(--tm-border-rule);
    flex-shrink: 0;
    background: var(--tm-surface);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
}

.modal.tenant-modal .tenant-modal__header > div:first-child {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    flex: 1 1 auto;
}

.modal.tenant-modal .tenant-modal__header-tools {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.modal.tenant-modal .tenant-modal__header .eyebrow {
    margin: 0;
    color: var(--tm-accent);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.modal.tenant-modal .tenant-modal__header .modal__title,
.modal.tenant-modal .tenant-modal__header h3 {
    margin: 0;
    font-family: var(--font-display, 'Space Grotesk', system-ui, sans-serif);
    font-size: 1.28rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.2;
    color: var(--tm-ink);
}

.modal.tenant-modal .tenant-modal__header .text-muted,
.modal.tenant-modal .tenant-modal__header > div > p:not(.eyebrow) {
    margin: 4px 0 0;
    max-width: 60ch;
    color: var(--tm-ink-muted);
    font-size: 13px;
    font-weight: 400;
    line-height: 1.45;
}

.modal.tenant-modal .modal__close {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--surface-inset, #edeae3);
    border: none;
    color: var(--tm-ink-muted);
    font-size: 18px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s ease, color 0.15s ease;
}

.modal.tenant-modal .modal__close:hover {
    background: rgba(26, 24, 21, 0.1);
    color: var(--tm-ink);
}

.modal.tenant-modal .tenant-modal__body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 20px 22px 22px;
    background: var(--tm-bg-warm);
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.modal.tenant-modal .tenant-modal__footer {
    padding: 13px 22px 14px;
    border-top: 1px solid var(--tm-border-rule);
    background: var(--tm-surface);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.modal.tenant-modal .tenant-modal__footer-copy {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.modal.tenant-modal .tenant-modal__footer-copy strong {
    font-family: var(--font-display, 'Space Grotesk', system-ui, sans-serif);
    font-size: 13px;
    font-weight: 700;
    color: var(--tm-ink);
}

.modal.tenant-modal .tenant-modal__footer-copy p {
    margin: 0;
    font-size: 12px;
    color: var(--tm-ink-faint);
}

.modal.tenant-modal .tenant-modal__footer-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.modal.tenant-modal .tenant-modal__footer .btn {
    min-height: 40px;
    padding: 8px 18px;
}

/* Form controls */
.modal.tenant-modal :where(input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="tel"], input[type="url"], input[type="search"], input[type="date"], input[type="time"], input[type="datetime-local"], select, textarea) {
    min-height: 40px;
    padding: 10px 13px;
    border-radius: 9px;
    border: 1px solid var(--tm-border-input);
    background: var(--tm-surface);
    font-family: var(--font-sans, inherit);
    font-size: 13.5px;
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
    color: var(--tm-ink);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    box-sizing: border-box;
    width: 100%;
}

.modal.tenant-modal :where(input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="tel"], input[type="url"], input[type="search"], input[type="date"], input[type="datetime-local"], select, textarea):focus {
    border-color: var(--tm-accent-strong);
    box-shadow: 0 0 0 3px var(--tm-accent-soft);
    outline: none;
}

.modal.tenant-modal textarea {
    min-height: 72px;
    resize: vertical;
    line-height: 1.5;
}

.modal.tenant-modal select {
    padding-right: 34px;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%239a9188' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 5l3 3 3-3'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 12px;
    cursor: pointer;
}

/* --------------------------------------------------------------------------
   .tenant-modal--danger — modifier for destructive actions (delete, archive)
   Applies a red eyebrow + red primary button without requiring bespoke
   .qdm-* classes. Use alongside the narrow dialog modifier.
   -------------------------------------------------------------------------- */
/* `!important` required to override the canonical modal eyebrow color at
   `style.css:5003` which forces `color: var(--accent) !important` on every
   `.modal--sheet .modal__header .eyebrow`. Without !important here the
   danger modifier renders gold instead of red. */
.modal.tenant-modal.tenant-modal--danger .tenant-modal__header .eyebrow {
    color: var(--danger, #b03030) !important;
}

.modal.tenant-modal.tenant-modal--danger .tenant-modal__footer-actions .btn-danger {
    background: var(--danger, #b03030);
    color: #ffffff;
    border: none;
    font-weight: 600;
}

.modal.tenant-modal.tenant-modal--danger .tenant-modal__footer-actions .btn-danger:hover {
    background: #952828;
}

html[data-theme="dark"] .modal.tenant-modal.tenant-modal--danger .tenant-modal__header .eyebrow {
    color: #f87171 !important;
}

html[data-theme="dark"] .modal.tenant-modal.tenant-modal--danger .tenant-modal__footer-actions .btn-danger {
    background: #dc2626;
}

html[data-theme="dark"] .modal.tenant-modal.tenant-modal--danger .tenant-modal__footer-actions .btn-danger:hover {
    background: #b91c1c;
}

/* Dark mode */
html[data-theme="dark"] .modal.tenant-modal {
    --tm-surface: #0f172a;
    --tm-surface-soft: rgba(15, 23, 42, 0.6);
    --tm-bg-warm: #0b1220;
    --tm-ink: #e2e8f0;
    --tm-ink-muted: #94a3b8;
    --tm-ink-faint: #94a3b8;
    --tm-border: rgba(148, 163, 184, 0.22);
    --tm-border-rule: rgba(148, 163, 184, 0.18);
    --tm-border-input: rgba(148, 163, 184, 0.28);
    --tm-accent: #e3b008;
    --tm-accent-soft: rgba(227, 176, 8, 0.18);
    --tm-accent-strong: rgba(244, 217, 139, 0.5);

    --ipi-bg: rgba(15, 23, 42, 0.55);
    --ipi-bg-inset: rgba(15, 23, 42, 0.62);
    --ipi-bg-card: rgba(15, 23, 42, 0.7);
    --ipi-bg-hover: rgba(148, 163, 184, 0.08);
    --ipi-rule: rgba(148, 163, 184, 0.22);
    --ipi-rule-h: rgba(148, 163, 184, 0.28);
    --ipi-ink: #e2e8f0;
    --ipi-ink-2: #cbd5e1;
    --ipi-ink-3: #94a3b8;
    --ipi-ink-4: #94a3b8;
    --ipi-accent: #e3b008;
    --ipi-accent-bg: rgba(227, 176, 8, 0.18);
    --ipi-accent-border: rgba(244, 217, 139, 0.5);
    --ipi-shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
    --ipi-shadow-card: 0 1px 4px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(148, 163, 184, 0.1);
}

html[data-theme="dark"] .modal.tenant-modal .tenant-modal__dialog {
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
}

html[data-theme="dark"] .modal.tenant-modal .modal__close {
    background: rgba(148, 163, 184, 0.16);
    color: rgba(226, 232, 240, 0.92);
}

html[data-theme="dark"] .modal.tenant-modal .modal__close:hover {
    background: rgba(148, 163, 184, 0.28);
}

/* Mobile behaviour (≤900px) — canonical full-screen + gold-spec chrome in
   style.css. Only tenant-modal-specific content layout lives here. */
@media (max-width: 900px) {
    /* Per-family base rule at line 50 (.modal.tenant-modal .tenant-modal__dialog)
       has equal specificity (0,3,0) to the canonical mobile rule, so we
       re-assert the canonical full-screen values here with !important.
       Matches the Invoices precedent. */
    .modal.modal--sheet.tenant-modal .tenant-modal__dialog,
    .modal.modal--sheet.tenant-modal .tenant-modal__dialog--stable,
    .modal.modal--sheet.tenant-modal .tenant-modal__dialog--narrow,
    .modal.modal--sheet.tenant-modal .tenant-modal__dialog--wide {
        width: 100% !important;
        max-width: 100% !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        border: none !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        margin: 0 !important;
    }

    .modal.tenant-modal .tenant-modal__body {
        gap: 12px;
    }

    /* Footer must stretch so the button grid can fill the full row. */
    .modal.tenant-modal .tenant-modal__footer {
        justify-content: stretch;
    }

    .modal.tenant-modal .tenant-modal__footer-actions {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        width: 100%;
        flex: 1 1 auto;
    }

    .modal.tenant-modal .tenant-modal__footer-actions > *:last-child:nth-child(odd) {
        grid-column: 1 / -1;
    }

    .modal.tenant-modal .tenant-modal__footer-actions .btn {
        width: 100%;
        justify-content: center;
        min-height: 44px;
        font-size: 13px;
        padding-left: 6px;
        padding-right: 6px;
    }
}
