/* ========================================================================== */
/*  Vars                                                                      */
/* ========================================================================== */
:root {
    --ai-c1: #423DFC;
    --ai-c2: #7271FB;
    --ai-grad: linear-gradient(135deg, var(--ai-c1), var(--ai-c2));

    /* fake cursor vars (needed for size/offset) */
    --cursor-w: 24px;
    --cursor-h: 24px;
    --tip-x: 18px;
    --tip-y: 6px;
    --control-h: 44px;
}

/* ========================================================================== */
/*  Card                                                                       */
/* ========================================================================== */
.auto-email-demo {
    padding: 48px 0;
}

.mail-card {
    position: relative;
    width: min(860px, 100%);
    margin-inline: auto;
    background: rgba(255, 255, 255, .6);
    border: 1px solid rgba(11, 18, 32, .10);
    border-radius: 16px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, .12), inset 0 1px 0 rgba(255, 255, 255, .6);
    backdrop-filter: blur(12px) saturate(160%);
    -webkit-backdrop-filter: blur(12px) saturate(160%);
    overflow: hidden;
    color: #0b1220;
    display: grid;
    grid-template-rows: auto 1fr auto;
    /* header | body | genbar */
}

/* optional thin gradient outline wrapper */
.section-panel:has(.mail-outline) {
    overflow: visible;
}

.mail-outline {
    position: relative;
    isolation: isolate;
    width: min(860px, 100%);
    margin-inline: auto;
    border-radius: 18px;
}

.mail-outline::before {
    --ring: 2px;
    content: "";
    position: absolute;
    inset: 0;
    padding: var(--ring);
    border-radius: inherit;
    background: var(--ai-grad);
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
    box-shadow: 0 20px 50px rgba(0, 0, 0, .22);
}

/* ========================================================================== */
/*  Header (old structure), with nicer label colors                            */
/* ========================================================================== */
.mail-head {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    align-items: center;
    padding: 12px 16px;
    background: rgba(255, 255, 255, .5);
    border-bottom: 1px solid rgba(11, 18, 32, .06);
    font-size: 14px;
}

.mail-head strong {
    font-weight: 700;
    color: rgba(11, 18, 32, .65);
    letter-spacing: .02em;
}

/* ---- "To" pill (chip) ---------------------------------------------------- */
/* Wrap the address in <span class="chip">…</span> for the second row item */
.mail-head .chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 28px;
    padding: 0 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, .75);
    border: 1px solid rgba(11, 18, 32, .12);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, .75), 0 6px 16px rgba(0, 0, 0, .06);
    backdrop-filter: blur(6px) saturate(150%);
    -webkit-backdrop-filter: blur(6px) saturate(150%);
    font-size: 13px;
    color: #0b1220;
    white-space: nowrap;
}

.mail-head .chip .chip-avatar {
    width: 18px;
    height: 18px;
    border-radius: 999px;
    background: #d9deea;
    flex: 0 0 auto;
}

/* ========================================================================== */
/*  Body                                                                       */
/* ========================================================================== */
.mail-body {
    padding: 16px 16px 8px;
    line-height: 1.55;
}

.mail-body p {
    margin: 0 0 12px;
    color: rgba(11, 18, 32, .88);
}

/* ========================================================================== */
/*  Gen bar + buttons (working focus glow + locked during animation)          */
/* ========================================================================== */
.genbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) max-content max-content;
    /* input | regen | pause */
    gap: 12px;
    align-items: center;
    padding: 12px 16px;
}

/* Base: works if .gen-input is the field itself OR a wrapper */
.gen-input {
    min-width: 0;
    height: var(--control-h);
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid rgba(11, 18, 32, .14);
    background: #fff;
    box-sizing: border-box;
    outline: 0;
    transition: border-color .18s ease, box-shadow .18s ease, background-color .18s ease, opacity .18s ease, filter .18s ease;
}

/* If .gen-input wraps a real control, normalize child */
.gen-input input,
.gen-input textarea,
.gen-input [contenteditable="true"] {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    border: 0;
    outline: 0;
    background: transparent;
    line-height: 1.2;
    font: inherit;
    color: inherit;
    box-sizing: border-box;
}

/* Hover hint */
.gen-input:hover,
.gen-input:has(input:hover),
.gen-input:has(textarea:hover),
.gen-input:has([contenteditable="true"]:hover) {
    border-color: rgba(66, 61, 252, .28);
}

/* ===== Focus glow (only when NOT generating) ===== */
#mailCard:not(.is-generating) .gen-input:focus,
#mailCard:not(.is-generating) .gen-input:focus-visible,
#mailCard:not(.is-generating) .gen-input:focus-within {
    /* fallback color + modern mix */
    border-color: #5b56ff;
    border-color: color-mix(in srgb, var(--ai-c2) 70%, #5b56ff 30%);
    box-shadow:
        0 0 0 3px color-mix(in srgb, var(--ai-c2) 22%, transparent),
        0 8px 22px color-mix(in srgb, var(--ai-c1) 20%, transparent);
    background-color: #fff;
}

/* Older engines fallback outline (will not show if above wins) */
#mailCard:not(.is-generating) .gen-input:focus {
    outline: 2px solid rgba(114, 113, 251, .38);
    outline-offset: 2px;
}

/* ===== Locked during animation (truly unclickable) ===== */
#mailCard.is-generating .gen-input {
    pointer-events: none;
    /* blocks mouse/touch */
    caret-color: transparent;
    /* hide caret if any */
    user-select: none;
    /* avoid text selection */
    opacity: .6;
    /* visual cue */
    cursor: default;
    /* remove any glow if it had focus before lock */
    box-shadow: none !important;
    outline: 0 !important;
    border-color: rgba(11, 18, 32, .14);
}

/* If the child input was focused when the lock toggled, clear its ring on the wrapper */
#mailCard.is-generating .gen-input:focus,
#mailCard.is-generating .gen-input:focus-within {
    box-shadow: none !important;
    outline: 0 !important;
}

/* Disabled state (if you ever set the attribute on the element itself) */
.gen-input:disabled,
.gen-input[aria-disabled="true"],
.gen-input:has(:disabled) {
    pointer-events: none;
    opacity: .6;
    cursor: not-allowed;
}

/* Dark mode tuning */
@media (prefers-color-scheme:dark) {

    #mailCard:not(.is-generating) .gen-input:focus,
    #mailCard:not(.is-generating) .gen-input:focus-visible,
    #mailCard:not(.is-generating) .gen-input:focus-within {
        background-color: rgba(16, 20, 36, .92);
        border-color: color-mix(in srgb, var(--ai-c2) 80%, #e9edf7 20%);
        box-shadow:
            0 0 0 3px color-mix(in srgb, var(--ai-c2) 28%, transparent),
            0 10px 26px color-mix(in srgb, var(--ai-c1) 26%, transparent);
    }
}

/* Reduced motion */
@media (prefers-reduced-motion:reduce) {
    .gen-input {
        transition: none;
    }
}

/* ========================================================================== */
/*  Buttons (unchanged aside from grid tweak above)                           */
/* ========================================================================== */
.btn {
    appearance: none;
    border: 0;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 10px;
    font-weight: 600;
    position: relative;
}

#btnPause {
    text-align: center;
    border-radius: 8px;
    width: 20px;
    font-size: 12px;
    line-height: 1;
}

#btnPause[aria-pressed="true"] {
    filter: saturate(1.05);
    box-shadow: 0 0 0 3px rgba(66, 61, 252, .16) inset;
}

/* ========================= Regenerate button ============================= */
#btnRegenerate {
    padding: 6px 10px;
    height: var(--control-h);
    line-height: 1;
    font-size: 14px;
    border-radius: 8px;
    display: inline-grid;
    grid-auto-flow: column;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
    position: relative;
}

/* keep width stable */
#btnRegenerate::before {
    content: "Regenerate with AI";
    visibility: hidden;
    pointer-events: none;
    font: inherit;
    grid-area: 1/1;
}

#btnRegenerate .btn-text,
#btnRegenerate .btn-icon {
    grid-area: 1/1;
}

#btnRegenerate.is-busy,
#btnRegenerate[disabled] {
    cursor: progress;
    opacity: .95;
}

/* =========================== Sticky genbar while generating ============== */
.mail-card.is-generating {
    max-height: 640px;
    overflow: auto;
}

.mail-card.is-generating .genbar {
    position: sticky;
    bottom: 0;
    z-index: 30;
    background: rgba(255, 255, 255, .85);
    backdrop-filter: blur(8px) saturate(160%);
    -webkit-backdrop-filter: blur(8px) saturate(160%);
    border-top: 1px solid rgba(11, 18, 32, .08);
}

/* ================================ Small screens ========================== */
@media (max-width:640px) {
    .genbar {
        grid-template-columns: 1fr;
    }

    #btnRegenerate,
    #btnPause {
        justify-self: end;
    }
}

/* ========================================================================== */
/*  Edit targets (unchanged)                                                   */
/* ========================================================================== */
.edit-target {
    display: inline;
    position: relative;
    padding: 0;
    margin: 0;
    border-radius: .35em;
    line-height: inherit;
    vertical-align: baseline;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.edit-target.is-selected {
    background: rgba(114, 113, 251, .08);
    box-shadow: inset 0 0 0 2px rgba(114, 113, 251, .18), 0 8px 18px rgba(114, 113, 251, .16);
}

@property --ring-angle {
    syntax: "<angle>";
    initial-value: 0deg;
    inherits: false;
}

.edit-target.is-selected::before {
    content: "";
    position: absolute;
    left: -.24em;
    right: -.24em;
    top: -.16em;
    bottom: -.16em;
    border-radius: .55em;
    padding: 2px;
    background: conic-gradient(from var(--ring-angle), rgba(114, 113, 251, .15), rgba(114, 113, 251, .75),
            rgba(114, 113, 251, .15) 75%, rgba(114, 113, 251, .75));
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    animation: ring-spin 2.8s linear infinite;
    pointer-events: none;
    z-index: 1;
}

@keyframes ring-spin {
    to {
        --ring-angle: 360deg;
    }
}

/* ========================================================================== */
/*  AI popup + regen overlay (kept)                                            */
/* ========================================================================== */
.ai-popup {
    position: absolute;
    left: -9999px;
    top: -9999px;
    width: 300px;
    background: linear-gradient(0deg, rgba(255, 255, 255, .94), rgba(255, 255, 255, .94)) padding-box, var(--ai-grad) border-box;
    border: 1px solid transparent;
    border-radius: 14px;
    padding: 12px;
    opacity: 0;
    transform: translateY(6px);
    transition: opacity .24s ease, transform .24s ease;
    pointer-events: auto;
    backdrop-filter: blur(10px) saturate(140%);
    -webkit-backdrop-filter: blur(10px) saturate(140%);
    box-shadow: 0 20px 50px rgba(0, 0, 0, .18), 0 0 28px color-mix(in srgb, var(--ai-c2), transparent 55%), 0 0 18px color-mix(in srgb, var(--ai-c1), transparent 65%);
    z-index: 50;
}

.ai-popup.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.ai-popup .ai-head {
    cursor: grab;
    user-select: none;
}

.ai-popup.dragging .ai-head {
    cursor: grabbing;
}

.ai-row label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: rgba(11, 18, 32, .55);
}

.ai-pill {
    display: inline-flex;
    align-items: center;
    max-width: 100%;
    padding: 6px 10px;
    border-radius: 12px;
    background: linear-gradient(0deg, #f5f6ff, #f5f6ff) padding-box, var(--ai-grad) border-box;
    border: 1px solid transparent;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, .7);
    font-family: ui-monospace, Menlo, Monaco, monospace;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-input {
    width: 100%;
    padding: 10px 12px;
    border-radius: 12px;
    background: linear-gradient(0deg, #fff, #fff) padding-box, var(--ai-grad) border-box;
    border: 1px solid transparent;
    font-size: 14px;
    white-space: nowrap;
    overflow-x: auto;
}

.ai-input:focus {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--ai-c2), transparent 82%), inset 0 1px 0 rgba(255, 255, 255, .75);
}

.ai-input[readonly] {
    cursor: default;
    color: #0b1220;
}

.ai-popup::before,
.ai-popup::after,
.ai-popup.flip::before,
.ai-popup.flip::after {
    content: none !important;
    display: none !important;
}

.regen-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
    background: radial-gradient(60% 60% at 50% 40%, rgba(114, 113, 251, .08), transparent 70%),
        linear-gradient(120deg, transparent 0%, rgba(114, 113, 251, .12) 45%, transparent 60%) 0 0/200% 100%;
    transition: opacity .35s ease;
    z-index: 20;
}

.regen-overlay.show {
    opacity: 1;
    animation: sweep 2.6s ease-in-out infinite;
}

@keyframes sweep {
    0% {
        background-position: -100% 0
    }

    50% {
        background-position: 0 0
    }

    100% {
        background-position: 100% 0
    }
}

/* ========================================================================== */
/*  Fake cursor (unchanged, ensure on top)                                     */
/* ========================================================================== */
.mouse {
    position: absolute;
    z-index: 60;
    pointer-events: none;
    width: var(--cursor-w, 24px);
    height: var(--cursor-h, 24px);
    background: url("/assets/icons/cursor.svg") center/contain no-repeat;
    opacity: 0;
    transform: translate(calc(-1*var(--tip-x, 18px)), calc(-1*var(--tip-y, 6px)));
    transition: left 1.1s ease, top 1.1s ease, opacity .35s ease;
    filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .25));
}

.mouse::after {
    content: "";
    position: absolute;
    left: var(--tip-x, 18px);
    top: var(--tip-y, 6px);
    width: 14px;
    height: 14px;
    color: #0b1220;
    background: radial-gradient(circle at center, transparent 58%, currentColor 60% 62%, transparent 64%),
        repeating-conic-gradient(from 0turn, currentColor 0 6deg, transparent 6deg 22.5deg);
    border-radius: 50%;
    opacity: 0;
    transform: scale(.6);
    pointer-events: none;
}

.mouse.tap::after {
    animation: tapBurst .55s ease-out forwards;
    opacity: 1;
}

@keyframes tapBurst {
    to {
        transform: scale(1.6);
        opacity: 0
    }
}

/* ========================================================================== */
/*  Small screens                                                              */
/* ========================================================================== */
@media (max-width:640px) {
    .genbar {
        grid-template-columns: 1fr;
    }

    #btnRegenerate,
    #btnPause {
        justify-self: end;
    }
}

/* Dark mode tweaks for chip */
@media (prefers-color-scheme:dark) {
    .mail-head {
        background: rgba(10, 14, 25, .5);
    }

    .mail-head strong {
        color: rgba(233, 237, 247, .70);
    }

    .mail-head .chip {
        background: rgba(22, 27, 45, .9);
        border-color: rgba(233, 237, 247, .12);
        color: #e9edf7;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08), 0 6px 16px rgba(0, 0, 0, .35);
    }
}

/* Regenerate: bigger text + a bit wider */
#btnRegenerate {
    font-size: 14px;
    /* was 12px */
    padding: 0 16px;
    /* a touch wider */
    min-width: 200px;
    /* make the button a bit wider overall */
}

/* Keep it reasonable on small screens */
@media (max-width:640px) {
    #btnRegenerate {
        min-width: 170px;
    }
}

/* Make Pause same height as Regenerate, but slimmer */
#btnPause {
    height: var(--control-h);
    /* match input & regenerate height (44px) */
    width: 84px;
    /* slimmer fixed width */
    min-width: 84px;
    max-width: 84px;
    font-size: 13.5px;
    /* match regenerate font size */
    border-radius: 8px;

    /* center label perfectly */
    display: inline-flex;
    align-items: center;
    justify-content: center;

    /* avoid wrap/jitter when toggling Pause/Resume */
    white-space: nowrap;
    overflow: hidden;
    line-height: 1;
    padding: 0;
    /* let width control the size */
}