/* ==========================================================================
   JEREMIAH — everything the homepage guide and his defense mini-game need.
   Files: website/jeremiah/ (widget.mjs, chat.mjs, portrait.mjs, defense-ui.mjs)
   Loaded from website/index.html. Keep Jeremiah styling in this file only.
   ========================================================================== */

/* ---- Jeremiah Defense: the homepage mini-game (server-owned state) ---- */
#jeremiahDefenseDialog {
    position: fixed;
    z-index: 10001;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 0;
    color: #f5fbff;
    color-scheme: dark;
    background:
        radial-gradient(ellipse at 50% 42%, rgba(21, 83, 116, 0.60), transparent 54%),
        linear-gradient(160deg, #0d2941 0%, #071a2b 52%, #102b40 100%);
    font-family: 'Plus Jakarta Sans', sans-serif;
}

#jeremiahDefenseDialog::backdrop { background: rgba(3, 10, 18, 0.86); }
#jeremiahDefenseDialog:focus { outline: none; }

#laserCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.defense-hud {
    position: relative;
    z-index: 2;
    display: grid;
    grid-template-columns: minmax(180px, 1fr) auto auto;
    grid-template-areas:
        "title stats actions"
        "instructions instructions status";
    align-items: center;
    gap: 10px 16px;
    padding: 14px clamp(14px, 3vw, 32px) 12px;
    border-bottom: 1px solid rgba(92, 230, 224, 0.58);
    background: linear-gradient(125deg, rgba(7, 25, 40, 0.98), rgba(10, 37, 56, 0.97));
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28), 0 2px 20px rgba(60, 224, 220, 0.12);
}

.defense-hud-title { grid-area: title; min-width: 0; }
.defense-hud-kicker {
    margin: 0 0 3px;
    color: #93fff1;
    font-family: 'Orbitron', sans-serif;
    font-size: 0.68rem;
    font-weight: 800;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}
.defense-hud-title h2 {
    margin: 0;
    color: #fff;
    font-size: clamp(1.05rem, 2.2vw, 1.45rem);
    line-height: 1.2;
}
.defense-metrics { grid-area: stats; display: flex; gap: 8px; }
.defense-stat {
    min-width: 72px;
    padding: 7px 10px;
    border: 1px solid rgba(140, 218, 231, 0.45);
    border-radius: 11px;
    background: rgba(21, 51, 73, 0.96);
    text-align: center;
}
.defense-stat span { display: block; color: #d4e7f0; font-size: 0.66rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }
.defense-stat strong { display: block; margin-top: 2px; color: #7df7e8; font-size: 1.2rem; line-height: 1.1; }
.defense-stat--lives strong { color: #ff9aad; }
.defense-actions { grid-area: actions; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.defense-actions button,
.defense-result button,
.defense-score-actions button,
.defense-scores-close {
    min-height: 44px;
    padding: 9px 13px;
    border: 1px solid rgba(135, 230, 230, 0.65);
    border-radius: 999px;
    color: #f5fbff;
    background: #15364d;
    font: inherit;
    font-size: 0.84rem;
    font-weight: 800;
    cursor: pointer;
}
.defense-actions button:hover,
.defense-result button:hover,
.defense-score-actions button:hover,
.defense-scores-close:hover { background: #21506a; }
.defense-actions .defense-quit { border-color: rgba(255, 139, 158, 0.64); color: #ffe8ed; }
.defense-instructions { grid-area: instructions; margin: 0; color: #e0edf5; font-size: 0.82rem; line-height: 1.4; }
.defense-status { grid-area: status; margin: 0; color: #9ff9ee; font-size: 0.8rem; line-height: 1.4; text-align: right; }

.defense-jeremiah {
    position: absolute;
    z-index: 2;
    right: max(14px, env(safe-area-inset-right));
    bottom: max(14px, env(safe-area-inset-bottom));
    width: 64px;
    height: 64px;
    border: 2px solid rgba(116, 255, 233, 0.92);
    border-radius: 14px;
    background: #132a3d;
    box-shadow: 0 0 24px rgba(70, 242, 221, 0.65), 0 8px 25px rgba(0, 0, 0, 0.48);
    pointer-events: none;
}

.defense-result {
    position: absolute;
    z-index: 4;
    inset: 50% auto auto 50%;
    width: min(440px, calc(100vw - 28px));
    padding: clamp(22px, 5vw, 34px);
    border: 1px solid rgba(119, 246, 231, 0.78);
    border-radius: 22px;
    color: #f7fbff;
    background: rgba(6, 22, 36, 0.98);
    box-shadow: 0 28px 80px rgba(0, 0, 0, 0.62), 0 0 28px rgba(67, 239, 222, 0.18);
    text-align: center;
    transform: translate(-50%, -50%);
}
.defense-result[hidden] { display: none; }
.defense-result-kicker { margin: 0 0 7px; color: #8cf6e7; font-size: 0.72rem; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
.defense-result h3 { margin: 0 0 10px; color: #fff; font-size: clamp(1.35rem, 4vw, 1.8rem); }
.defense-result p { color: #d7e7f0; line-height: 1.55; }
.defense-result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 9px; margin-top: 18px; }
.defense-result .defense-replay { color: #031820; border-color: #7df7e8; background: #7df7e8; }
.defense-result a { align-self: center; color: #bafcf5; font-weight: 800; }
.defense-result a[hidden] { display: none; }

#jeremiahDefenseScoresDialog {
    width: min(560px, calc(100vw - 28px));
    max-width: calc(100vw - 28px);
    max-height: min(82vh, 720px);
    margin: auto;
    padding: 0;
    overflow: auto;
    border: 1px solid rgba(104, 236, 224, 0.72);
    border-radius: 20px;
    color: #f4faff;
    background: linear-gradient(155deg, rgba(12, 39, 58, 0.99), rgba(7, 20, 34, 0.99));
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.7), 0 0 28px rgba(29, 233, 226, 0.18);
}
#jeremiahDefenseScoresDialog::backdrop { background: rgba(3, 10, 18, 0.76); }
.defense-scores-card { padding: clamp(20px, 5vw, 30px); }
.defense-scores-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.defense-scores-head h2 { margin: 0; color: #fff; font-size: clamp(1.3rem, 4vw, 1.8rem); }
.defense-scores-card > p { color: #d9e7ef; line-height: 1.55; }
.defense-scores-close { flex: 0 0 auto; width: 44px; padding: 0; font-size: 1.1rem; }
.defense-scores-list { display: grid; gap: 8px; margin: 18px 0; padding: 0; list-style: none; }
.defense-scores-list li { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; align-items: center; gap: 12px; padding: 12px; border: 1px solid rgba(150, 211, 226, 0.32); border-radius: 12px; background: rgba(18, 48, 68, 0.88); }
.defense-scores-rank { color: #9ff7eb; font-weight: 900; }
.defense-scores-name { min-width: 0; overflow-wrap: anywhere; color: #fff; font-weight: 800; }
.defense-scores-value { color: #fff; font-variant-numeric: tabular-nums; font-weight: 900; text-align: right; }
.defense-scores-value small { display: block; color: #d2e4ed; font-size: 0.68rem; font-weight: 700; }
.defense-board-status { min-height: 1.4em; color: #b8eee9 !important; font-size: 0.86rem; }
.defense-score-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 9px; padding-top: 14px; border-top: 1px solid rgba(255, 255, 255, 0.15); }
.defense-score-actions a { color: #bafcf5; font-weight: 800; }
.defense-score-actions button { font-size: 0.78rem; }
.defense-scores-dialog button:focus-visible,
#jeremiahDefenseDialog button:focus-visible,
#jeremiahDefenseDialog a:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 3px;
}

/* In-game portrait: fills its 64px frame. The lean and blink come from portrait.mjs. */
.defense-jeremiah .jeremiah-portrait { width: 100%; height: 100%; border: 0; border-radius: 12px; }
.defense-jeremiah img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: top center; }

/* ---- Widget: one greeting button, a chat that opens above Jeremiah ----
   Structure (website/index.html): chat panel, optional "need a hand?" bubble,
   then a dock with the portrait and the greeting button. The outer box never
   takes clicks; only the visible pieces do, so the page underneath stays usable. */
@keyframes jerBreathe {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-3px); }
}
@keyframes jerTalkBob {
    0%, 100% { transform: translateY(0) scale(1); }
    50% { transform: translateY(-2px) scale(1.025); }
}
@keyframes jerChatIn {
    from { opacity: 0; transform: translateY(12px) scale(0.98); }
    to { opacity: 1; transform: none; }
}
@keyframes jerBubbleIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}
@keyframes jerDot {
    0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}
@keyframes jerRing {
    0% { box-shadow: 0 0 0 0 rgba(125, 247, 232, 0.6), 0 0 14px rgba(81, 235, 224, 0.34); }
    100% { box-shadow: 0 0 0 16px rgba(125, 247, 232, 0), 0 0 14px rgba(81, 235, 224, 0.34); }
}

#jeremiah-mascot-widget {
    position: fixed;
    right: 24px;
    bottom: 88px; /* clears the notifications bell, which sits at bottom 18 */
    z-index: 9999;
    width: max-content;
    max-width: calc(100vw - 32px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    pointer-events: none;
    font-family: 'Plus Jakarta Sans', sans-serif;
}
#jeremiah-mascot-widget.is-open { width: min(372px, calc(100vw - 32px)); }
#jeremiah-mascot-widget > * { pointer-events: auto; }

.jeremiah-dock {
    display: flex;
    align-items: flex-end;
    gap: 10px;
}

.jeremiah-portrait {
    position: relative;
    flex: 0 0 auto;
    width: 84px;
    height: 84px;
    border-radius: 16px;
    overflow: hidden;
    background: radial-gradient(circle at 50% 30%, #1a4159 0%, #090d16 72%);
    border: 1px solid rgba(29, 233, 226, 0.4);
    box-shadow: 0 12px 26px -8px rgba(0, 0, 0, 0.75), 0 0 22px rgba(29, 233, 226, 0.22);
    transform-origin: 50% 90%;
    will-change: transform;
}

#mascotImg {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top center;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    animation: jerBreathe 3.6s ease-in-out infinite;
}
#jeremiah-mascot-widget.is-talking #mascotImg { animation: jerTalkBob 0.6s ease-in-out infinite; }

.jeremiah-greet {
    align-self: flex-end;
    min-height: 44px;
    padding: 9px 16px;
    border: 1px solid #9bfff3;
    border-radius: 999px;
    background: linear-gradient(135deg, #82f6e8, #6ccef5);
    color: #05202a;
    box-shadow: 0 0 14px rgba(81, 235, 224, 0.34);
    font-family: 'Orbitron', sans-serif;
    font-size: 0.74rem;
    font-weight: 800;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 0.18s ease, transform 0.18s ease;
}
.jeremiah-greet:hover { background: #c5fff8; transform: translateY(-1px); }
.jeremiah-greet[aria-expanded="true"] { background: #c5fff8; }
.jeremiah-greet.is-nudging { animation: jerRing 1.6s ease-out infinite; }

/* "Need a hand?" speech bubble above the button. */
.jeremiah-nudge {
    position: relative;
    display: flex;
    align-items: center;
    gap: 4px;
    max-width: 252px;
    margin: 0 10px 2px 0;
    padding: 4px 4px 4px 4px;
    background: #f1fffd;
    color: #04242c;
    border-radius: 14px;
    box-shadow: 0 10px 24px -8px rgba(0, 0, 0, 0.6);
    animation: jerBubbleIn 0.3s ease-out both;
}
.jeremiah-nudge[hidden] { display: none; }
.jeremiah-nudge::after {
    content: '';
    position: absolute;
    right: 26px;
    bottom: -7px;
    width: 14px;
    height: 14px;
    background: #f1fffd;
    transform: rotate(45deg);
}
.jeremiah-nudge-open {
    flex: 1 1 auto;
    min-height: 40px;
    padding: 8px 10px;
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: 0.86rem;
    font-weight: 700;
    line-height: 1.35;
    text-align: left;
    cursor: pointer;
}
.jeremiah-nudge-close {
    flex: 0 0 auto;
    min-width: 36px;
    min-height: 36px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #3a5d66;
    font-size: 0.9rem;
    cursor: pointer;
}
.jeremiah-nudge-close:hover { background: rgba(4, 36, 44, 0.08); }

.jeremiah-status {
    margin: 0;
    max-width: 280px;
    color: #9ff9ee;
    font-size: 0.78rem;
    line-height: 1.4;
    text-align: right;
    text-shadow: 0 1px 6px rgba(0, 0, 0, 0.8);
}
.jeremiah-status[hidden] { display: none; }

/* ---- Chat panel: header, message log, option chips, and an input bar ---- */
.jeremiah-chat {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: min(540px, calc(100vh - 200px));
    padding: 12px;
    background: linear-gradient(170deg, rgba(15, 33, 48, 0.98), rgba(8, 15, 25, 0.98));
    border: 1px solid rgba(29, 233, 226, 0.35);
    border-radius: 18px;
    box-shadow: 0 22px 50px -12px rgba(0, 0, 0, 0.75), 0 0 26px rgba(29, 233, 226, 0.14);
    animation: jerChatIn 0.22s ease-out both;
}
.jeremiah-chat[hidden] { display: none; }
.jeremiah-chat:focus { outline: none; }

.jeremiah-chat-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-bottom: 9px;
    border-bottom: 1px solid rgba(29, 233, 226, 0.18);
}
.jeremiah-chat-avatar {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 11px;
    object-fit: cover;
    object-position: top center;
    background: #0a1420;
    border: 1px solid rgba(29, 233, 226, 0.4);
}
.jeremiah-chat-who {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
}
.jeremiah-chat-who strong {
    color: #e9fffc;
    font-family: 'Orbitron', sans-serif;
    font-size: 0.76rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}
.jeremiah-chat-who span {
    color: #8fd9d2;
    font-size: 0.72rem;
}
.jeremiah-chat-close {
    flex: 0 0 auto;
    min-width: 44px;
    min-height: 44px;
    margin: -8px -8px -8px 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #cfe9f1;
    font-size: 1rem;
    cursor: pointer;
}
.jeremiah-chat-close:hover { background: rgba(255, 255, 255, 0.08); }

.jeremiah-log {
    flex: 1 1 auto;
    min-height: 150px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 2px 4px 2px 0;
    overscroll-behavior: contain;
}

.jeremiah-bubble {
    max-width: 86%;
    margin: 0;
    padding: 9px 12px;
    border-radius: 14px;
    font-size: 0.88rem;
    line-height: 1.4;
    color: #e6f7f5;
    overflow-wrap: anywhere;
    animation: jerBubbleIn 0.25s ease-out both;
}
.jeremiah-bubble--jeremiah {
    align-self: flex-start;
    background: linear-gradient(160deg, #22506b, #183b52);
    border-bottom-left-radius: 4px;
}
.jeremiah-bubble--you {
    align-self: flex-end;
    background: linear-gradient(160deg, #2f7d5f, #22604a);
    border-bottom-right-radius: 4px;
}
.jeremiah-bubble-who {
    display: block;
    margin-bottom: 2px;
    color: #9ff9ee;
    font-family: 'Orbitron', sans-serif;
    font-size: 0.56rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.jeremiah-typing {
    align-self: flex-start;
    display: inline-flex;
    gap: 5px;
    padding: 11px 13px;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    background: linear-gradient(160deg, #22506b, #183b52);
}
.jeremiah-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ff9ee;
    animation: jerDot 1.1s ease-in-out infinite;
}
.jeremiah-typing span:nth-child(2) { animation-delay: 0.15s; }
.jeremiah-typing span:nth-child(3) { animation-delay: 0.3s; }

.jeremiah-options {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    max-height: 132px;
    overflow-y: auto;
    padding: 2px;
    overscroll-behavior: contain;
}
.jeremiah-options:empty { display: none; }
.jeremiah-option {
    min-height: 38px;
    padding: 7px 12px;
    border: 1px solid rgba(166, 235, 242, 0.55);
    border-radius: 999px;
    background: rgba(22, 52, 72, 0.98);
    color: #f4fbff;
    font: inherit;
    font-size: 0.8rem;
    line-height: 1.2;
    cursor: pointer;
    transition: background-color 0.18s ease, transform 0.18s ease;
}
.jeremiah-option:hover { background: #28617a; color: #fff; transform: translateY(-1px); }
.jeremiah-option--back { border-style: dashed; color: #9ff9ee; }
.jeremiah-option:disabled { opacity: 0.6; cursor: wait; }

.jeremiah-form {
    display: flex;
    gap: 6px;
    align-items: center;
    padding-top: 8px;
    border-top: 1px solid rgba(29, 233, 226, 0.18);
}
.jeremiah-form input {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 42px;
    padding: 0 12px;
    border: 1px solid rgba(166, 235, 242, 0.4);
    border-radius: 12px;
    background: #0b1a26;
    color: #f4fbff;
    font-size: 16px; /* 16px stops iPhones zooming into the field */
}
.jeremiah-form input::placeholder { color: #8fb3bf; }
.jeremiah-send {
    flex: 0 0 auto;
    min-height: 42px;
    padding: 0 16px;
    border: 1px solid #9bfff3;
    border-radius: 12px;
    background: linear-gradient(135deg, #82f6e8, #6ccef5);
    color: #05202a;
    font: inherit;
    font-size: 0.82rem;
    font-weight: 800;
    cursor: pointer;
}
.jeremiah-send:hover { background: #c5fff8; }

.jeremiah-sr {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

#jeremiah-mascot-widget button:focus-visible,
#jeremiah-mascot-widget input:focus-visible,
#jeremiah-mascot-widget .jeremiah-chat:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 3px;
}

@media (max-width: 640px) {
    #jeremiahDefenseDialog .defense-hud {
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "title actions"
            "stats stats"
            "instructions instructions"
            "status status";
        gap: 7px;
        padding: max(10px, env(safe-area-inset-top)) 12px 10px;
    }
    #jeremiahDefenseDialog .defense-hud-kicker { font-size: 0.58rem; }
    #jeremiahDefenseDialog .defense-hud-title h2 { font-size: 1rem; }
    #jeremiahDefenseDialog .defense-metrics { gap: 5px; }
    #jeremiahDefenseDialog .defense-stat { min-width: 0; flex: 1; padding: 5px 6px; }
    #jeremiahDefenseDialog .defense-stat span { font-size: 0.56rem; }
    #jeremiahDefenseDialog .defense-stat strong { font-size: 1.05rem; }
    #jeremiahDefenseDialog .defense-actions { gap: 5px; }
    #jeremiahDefenseDialog .defense-actions button { min-height: 44px; padding: 7px 9px; font-size: 0.72rem; }
    #jeremiahDefenseDialog .defense-instructions { font-size: 0.74rem; }
    #jeremiahDefenseDialog .defense-status { font-size: 0.72rem; text-align: left; }
}

/* Phone: the closed widget is a small dock (portrait + button) above the bottom
   bar. Opening the chat turns it into a sheet above the dock. The outer box
   is transparent and passes taps through, so the hero text stays tappable. */
@media (max-width: 640px) {
    #jeremiah-mascot-widget {
        right: max(12px, env(safe-area-inset-right));
        bottom: calc(76px + env(safe-area-inset-bottom));
        z-index: 890;
        gap: 8px;
        animation: none;
        backdrop-filter: none;
    }
    #jeremiah-mascot-widget.is-open { width: calc(100vw - 24px); }
    .jeremiah-dock { gap: 8px; }
    .jeremiah-portrait { width: 60px; height: 60px; border-radius: 13px; }
    .jeremiah-greet { min-height: 44px; padding: 8px 14px; font-size: 0.68rem; }
    .jeremiah-nudge { max-width: 214px; }
    .jeremiah-nudge-open { font-size: 0.8rem; }
    .jeremiah-status { display: none; }
    .jeremiah-chat {
        max-height: min(460px, calc(100dvh - 230px));
        padding: 10px;
    }
    .jeremiah-log { min-height: 110px; }
    .jeremiah-chat-avatar { width: 34px; height: 34px; }
}

@media (prefers-reduced-motion: reduce) {
    #jeremiah-mascot-widget,
    #jeremiah-mascot-widget * { animation: none !important; }
    .jeremiah-greet, .jeremiah-option, .jeremiah-form button { transition: none; }
}
