/* ═══ AI CHAT MODULE STYLES ═══ */

#hive-fab {
    position: absolute;
    left: 0;
    top: 0;
    width: 65px;
    height: 65px;
    background: var(--bg-card);
    border: 2px solid var(--orange);
    z-index: 5000;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 20px var(--orange-dim), inset 0 0 10px var(--orange-dim);
    clip-path: polygon(15% 0%, 85% 0%, 100% 15%, 100% 85%, 85% 100%, 15% 100%, 0% 85%, 0% 15%);
    will-change: transform;
    isolation: isolate;
    overflow: hidden;
    transition: background-color 220ms ease, box-shadow 220ms ease;
}

#hive-fab:hover {
    background: var(--orange-dim);
    box-shadow: 0 0 24px var(--orange-dim), inset 0 0 14px var(--orange-dim);
}

#hive-fab:focus-visible {
    outline: 1px solid var(--text-primary);
    outline-offset: 4px;
    box-shadow: 0 0 0 3px var(--bg), 0 0 24px var(--orange-dim), inset 0 0 14px var(--orange-dim);
}

#hive-fab::before,
#hive-fab::after {
    content: "";
    position: absolute;
    pointer-events: none;
}

#hive-fab::before {
    inset: -35% 35%;
    z-index: 5;
    background: linear-gradient(90deg, transparent, rgba(255, 190, 126, 0.45), transparent);
    transform: rotate(28deg) translateY(-80%);
    animation: fabShellScan 7.5s cubic-bezier(.4, 0, .2, 1) infinite;
}

#hive-fab::after {
    inset: 5px;
    z-index: 4;
    border: 1px solid rgba(232, 118, 43, 0.3);
    clip-path: polygon(15% 0%, 85% 0%, 100% 15%, 100% 85%, 85% 100%, 15% 100%, 0% 85%, 0% 15%);
}

#fab-shaker {
    --fab-pitch: 0deg;
    --fab-roll: 0deg;
    --fab-core-turn: 0deg;
    width: 35px;
    height: 35px;
    position: relative;
    z-index: 1;
    transform-style: preserve-3d;
    transform: rotateX(var(--fab-pitch)) rotateY(var(--fab-roll));
    transition: transform 160ms cubic-bezier(.2, .8, .2, 1);
}

#fab-shaker::before,
#fab-shaker::after {
    content: "";
    position: absolute;
    pointer-events: none;
}

#fab-shaker::before {
    inset: -5px;
    z-index: 0;
    border: 1px solid rgba(232, 118, 43, 0.48);
    transform: rotate(45deg);
    animation: fabCounterSpin 11s linear infinite reverse;
}

#fab-shaker::after {
    left: -8px;
    right: -8px;
    top: 50%;
    z-index: 4;
    height: 1px;
    background: linear-gradient(90deg, var(--orange) 0 10%, transparent 10% 90%, var(--orange) 90%);
    opacity: 0.55;
}

#fab-neural {
    position: absolute;
    inset: 0;
    z-index: 3;
    pointer-events: none;
    transform: rotate(var(--fab-core-turn));
    transition: transform 180ms ease, opacity 180ms ease;
}

.fab-core {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 6px;
    height: 6px;
    transform: translate(-50%, -50%) rotate(45deg);
    background: #fff3df;
    border: 1px solid var(--orange);
    box-shadow: 0 0 8px var(--orange);
    animation: fabCorePulse 2.8s ease-in-out infinite;
}

.fab-node {
    --link: 14px;
    --angle: 0deg;
    position: absolute;
    width: 3px;
    height: 3px;
    background: var(--orange);
    box-shadow: 0 0 4px var(--orange);
    animation: fabNodeData 3.1s ease-in-out infinite;
}

.fab-node::after {
    content: "";
    position: absolute;
    left: 1px;
    top: 1px;
    width: var(--link);
    height: 1px;
    transform: rotate(var(--angle));
    transform-origin: left center;
    background: linear-gradient(90deg, rgba(232, 118, 43, 0.75), transparent);
    animation: fabLinkPulse 3.1s ease-in-out infinite;
}

.node-a { left: 48%; top: 2%; --link: 17px; --angle: 90deg; }
.node-b { left: 88%; top: 32%; --link: 15px; --angle: 155deg; animation-delay: -0.7s; }
.node-c { left: 74%; top: 82%; --link: 15px; --angle: -128deg; animation-delay: -1.4s; }
.node-d { left: 10%; top: 66%; --link: 16px; --angle: -25deg; animation-delay: -2.1s; }
.node-e { left: 8%; top: 20%; --link: 17px; --angle: 34deg; animation-delay: -2.7s; }

.node-b::after { animation-delay: -0.7s; }
.node-c::after { animation-delay: -1.4s; }
.node-d::after { animation-delay: -2.1s; }
.node-e::after { animation-delay: -2.7s; }

#hive-fab:is(:hover, :focus-visible) #fab-shaker {
    transform: rotateX(var(--fab-pitch)) rotateY(var(--fab-roll)) scale(1.06);
}

#hive-fab:is(:hover, :focus-visible) #fab-cube {
    animation-duration: 4.6s;
}

#hive-fab.is-moving .fab-core {
    filter: brightness(1.25);
}

#hive-fab.is-pointer-near #fab-neural {
    opacity: 1;
}

#hive-fab.is-activating #fab-neural {
    transform: rotate(var(--fab-core-turn)) scale(0.55);
}

#hive-fab.is-thinking .fab-node,
#hive-fab.is-thinking .fab-node::after {
    animation-duration: 0.72s;
    opacity: 1;
}

#fab-cube {
    width: 100%;
    height: 100%;
    position: relative;
    z-index: 2;
    transform-style: preserve-3d;
    animation: cubeSpin 6s infinite linear;
}

@keyframes cubeSpin {
    from { transform: rotateX(0deg) rotateY(0deg); }
    to { transform: rotateX(360deg) rotateY(360deg); }
}

@keyframes fabCounterSpin {
    to { transform: rotate(405deg); }
}

@keyframes fabCorePulse {
    50% {
        opacity: 0.65;
        transform: translate(-50%, -50%) rotate(45deg) scale(0.8);
    }
}

@keyframes fabNodeData {
    50% { opacity: 0.35; }
}

@keyframes fabLinkPulse {
    0%, 100% { opacity: 0.18; transform: rotate(var(--angle)) scaleX(0.55); }
    50% { opacity: 0.82; transform: rotate(var(--angle)) scaleX(1); }
}

@keyframes fabShellScan {
    0%, 72% { opacity: 0; transform: rotate(28deg) translateY(-80%); }
    82% { opacity: 0.8; }
    100% { opacity: 0; transform: rotate(28deg) translateY(80%); }
}

.face {
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgba(232, 118, 43, 0.1);
    border: 1px solid var(--orange);
}

.face.front  { transform: translateZ(17.5px); }
.face.back   { transform: rotateY(180deg) translateZ(17.5px); }
.face.left   { transform: rotateY(-90deg) translateZ(17.5px); }
.face.right  { transform: rotateY(90deg) translateZ(17.5px); }
.face.top    { transform: rotateX(90deg) translateZ(17.5px); }
.face.bottom { transform: rotateX(-90deg) translateZ(17.5px); }

#chat-modal {
    position: absolute;
    left: 0;
    top: 0;
    width: 380px;
    height: 520px;
    background: rgba(19, 19, 19, 0.9);
    backdrop-filter: blur(15px);
    border: 2px solid var(--border);
    border-top: 4px solid var(--orange);
    z-index: 5001;
    display: none;
    flex-direction: column;
    box-shadow: 0 20px 50px rgba(0,0,0,0.8);
    clip-path: polygon(0% 0%, 100% 0%, 100% 90%, 90% 100%, 0% 100%);
    animation: modalPop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    pointer-events: auto;
}

/* Grain Overlay for Chat */
#chat-modal::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)'/%3E%3C/svg%3E");
    opacity: 0.04;
    pointer-events: none;
    z-index: 1;
}

/* CRT power-on: the terminal snaps open from a hot horizontal scanline and
   unfolds. Animates clip-path/opacity/filter only — transform belongs to the
   JS positioner and must never be animated here. */
@keyframes modalPop {
    0% {
        opacity: 0;
        clip-path: polygon(0% 48%, 100% 48%, 100% 54%, 90% 54%, 0% 54%);
        filter: brightness(2.2);
    }
    55% {
        opacity: 1;
        clip-path: polygon(0% 0%, 100% 0%, 100% 90%, 90% 100%, 0% 100%);
        filter: brightness(1.35);
    }
    100% {
        opacity: 1;
        clip-path: polygon(0% 0%, 100% 0%, 100% 90%, 90% 100%, 0% 100%);
        filter: brightness(1);
    }
}

.hive-motion-off #chat-modal {
    animation-duration: 0.01s;
}

.chat-header {
    padding: 15px 20px;
    background: rgba(232, 118, 43, 0.05);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h3 {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: var(--orange);
    margin: 0;
}

.ai-status-indicator {
    font-family: var(--font-mono);
    font-size: 0.6rem;
    color: var(--orange);
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(232, 118, 43, 0.05);
    padding: 3px 10px;
    border: 1px solid var(--orange-dim);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.ai-status-indicator .status-dot.pulse {
    width: 6px;
    height: 6px;
    background: var(--orange);
    border-radius: 50%;
    box-shadow: 0 0 8px var(--orange);
    animation: statusPulse 2s infinite ease-in-out;
}

@keyframes statusPulse {
    0%, 100% { opacity: 0.4; transform: scale(0.9); }
    50% { opacity: 1; transform: scale(1.2); }
}

.chat-close-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.2rem;
    cursor: pointer;
}

.chat-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.chat-body::-webkit-scrollbar { width: 4px; }
.chat-body::-webkit-scrollbar-track { background: transparent; }
.chat-body::-webkit-scrollbar-thumb { background: var(--border); }

.chat-message {
    padding: 10px 15px;
    font-size: 0.85rem;
    max-width: 85%;
    line-height: 1.5;
    position: relative;
}

.chat-message.bot {
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.05);
    border-left: 2px solid var(--orange);
    color: var(--text-primary);
}

.chat-message.user {
    align-self: flex-end;
    background: var(--orange-dim);
    border-right: 2px solid var(--orange);
    color: var(--orange);
    text-align: right;
}

.typing-indicator {
    padding: 5px 20px;
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--text-dim);
    font-style: italic;
    display: none;
}

.chat-input-area {
    padding: 15px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
}

.chat-input {
    flex: 1;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border);
    padding: 10px;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.8rem;
    outline: none;
}

.chat-input:focus {
    border-color: var(--orange);
}

.chat-send-btn {
    background: var(--orange);
    border: none;
    color: #000;
    padding: 0 15px;
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 700;
    cursor: pointer;
}

.chat-send-btn:hover {
    filter: brightness(1.15);
}

/* ═══ FAB PARTICLES ═══ */
.fab-particle {
    position: absolute;
    pointer-events: none;
    z-index: 4999;
    border: 1px solid color-mix(in srgb, var(--orange) 75%, #ffffff);
    border-radius: 3px;
    opacity: 0.9;
    background: radial-gradient(circle, color-mix(in srgb, var(--orange) 45%, #ffffff) 0%, color-mix(in srgb, var(--orange) 22%, transparent) 80%);
    transform-style: preserve-3d;
    box-shadow: 0 0 8px var(--orange), 0 0 16px color-mix(in srgb, var(--orange) 50%, transparent);
    will-change: transform, opacity;
}

.fab-particle[data-accent] {
    border-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0 0 12px var(--orange), 0 0 24px color-mix(in srgb, var(--orange) 85%, transparent);
    background: radial-gradient(circle, #ffffff 0%, color-mix(in srgb, var(--orange) 75%, transparent) 75%);
}

/* ═══ ENERGY SHOCKWAVES ═══ */
.fab-shockwave {
    position: absolute;
    pointer-events: none;
    z-index: 4998;
    border: 1px solid var(--orange);
    background: transparent;
    opacity: 0.6;
}

/* ═══ R10 — CHAT INTERIOR MOTION ═══ */
/* Entry animations key off .msg-in, which ai-chat.js adds only to messages
   appended live — reopening the modal must not replay the whole history. */
.chat-message.user.msg-in {
    animation: chat-user-in 0.28s cubic-bezier(0.2, 1.2, 0.4, 1);
}
@keyframes chat-user-in {
    0% { opacity: 0; transform: translateX(16px); }
    60% { opacity: 1; transform: translateX(-2px); }
    100% { opacity: 1; transform: translateX(0); }
}
.chat-message.bot.msg-in {
    animation: chat-bot-in 0.34s ease-out;
}
@keyframes chat-bot-in {
    0% { opacity: 0.25; clip-path: inset(0 0 100% 0); filter: brightness(2.1); }
    100% { opacity: 1; clip-path: inset(0 0 0% 0); filter: brightness(1); }
}
.chat-message.bot.msg-in::after {
    content: ''; position: absolute;
    left: 0; right: 0; top: 0;
    height: 1px;
    background: linear-gradient(90deg, var(--orange), transparent 85%);
    opacity: 0;
    animation: chat-bot-scan 0.34s ease-out;
    pointer-events: none;
}
@keyframes chat-bot-scan {
    0% { top: 0; opacity: 0.9; }
    100% { top: 100%; opacity: 0; }
}

/* Typing indicator: the italic status line gains a live oscilloscope wave. */
.typing-indicator { align-items: center; gap: 8px; }
.typing-wave {
    display: inline-flex;
    align-items: flex-end;
    gap: 2px;
    height: 10px;
    flex-shrink: 0;
}
.typing-wave i {
    width: 2px;
    height: 3px;
    background: var(--orange);
    animation: typing-osc 0.9s ease-in-out infinite;
}
.typing-wave i:nth-child(2) { animation-delay: 0.12s; }
.typing-wave i:nth-child(3) { animation-delay: 0.24s; }
.typing-wave i:nth-child(4) { animation-delay: 0.36s; }
.typing-wave i:nth-child(5) { animation-delay: 0.48s; }
@keyframes typing-osc {
    0%, 100% { height: 3px; opacity: 0.6; }
    50% { height: 10px; opacity: 1; }
}

/* SEND: a single EM ring escapes the button on every transmission. */
.chat-send-btn { position: relative; }
.send-ping {
    position: absolute; inset: 0;
    border: 1px solid var(--orange);
    opacity: 0.85;
    pointer-events: none;
    animation: send-ping-out 0.45s ease-out forwards;
}
@keyframes send-ping-out {
    0% { transform: scale(1); opacity: 0.85; }
    100% { transform: scale(1.9); opacity: 0; }
}

/* R10 chat motion guards */
html.hive-motion-off .chat-message.msg-in,
html.hive-motion-off .chat-message.bot.msg-in::after,
html.hive-motion-off .typing-wave i {
    animation: none !important;
}
html.hive-motion-off .send-ping { display: none; }
@media (prefers-reduced-motion: reduce) {
    html:not(.force-motion) .chat-message.msg-in,
    html:not(.force-motion) .chat-message.bot.msg-in::after,
    html:not(.force-motion) .typing-wave i {
        animation: none !important;
    }
    html:not(.force-motion) .send-ping { display: none; }
}

/* ═══ RESPONSIVE — TABLET ═══ */
@media (max-width: 900px) {
    #chat-modal {
        width: calc(100vw - 32px) !important;
        max-width: 400px;
        height: 460px;
    }
    #hive-fab {
        width: 55px;
        height: 55px;
    }
    #fab-shaker {
        width: 28px;
        height: 28px;
    }
    .face.front  { transform: translateZ(14px); }
    .face.back   { transform: rotateY(180deg) translateZ(14px); }
    .face.left   { transform: rotateY(-90deg) translateZ(14px); }
    .face.right  { transform: rotateY(90deg) translateZ(14px); }
    .face.top    { transform: rotateX(90deg) translateZ(14px); }
    .face.bottom { transform: rotateX(-90deg) translateZ(14px); }
}

/* ═══ RESPONSIVE — MOBILE ═══ */
@media (max-width: 600px) {
    #chat-modal {
        width: calc(100vw - 16px) !important;
        max-width: none;
        height: calc(100vh - 120px);
        max-height: 500px;
    }
    .chat-header { padding: 12px 16px; }
    .chat-header h3 { font-size: 0.7rem; }
    .chat-body { padding: 14px; gap: 12px; }
    .chat-message { font-size: 0.8rem; max-width: 90%; }
    .chat-input-area { padding: 12px; gap: 8px; }
    .chat-input { font-size: 16px; padding: 8px; } /* 16px prevents iOS focus-zoom */
    .chat-send-btn { padding: 0 12px; font-size: 0.7rem; }
    .model-selector { font-size: 0.55rem; }
    #hive-fab {
        width: 50px;
        height: 50px;
    }
    #fab-shaker {
        width: 24px;
        height: 24px;
    }
    .face.front  { transform: translateZ(12px); }
    .face.back   { transform: rotateY(180deg) translateZ(12px); }
    .face.left   { transform: rotateY(-90deg) translateZ(12px); }
    .face.right  { transform: rotateY(90deg) translateZ(12px); }
    .face.top    { transform: rotateX(90deg) translateZ(12px); }
    .face.bottom { transform: rotateX(-90deg) translateZ(12px); }
}

/* ═══ SAFE AREA ═══ */
@supports (padding: env(safe-area-inset-bottom)) {
    .chat-input-area { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}

/* ── LIGHT THEME CHAT OVERRIDES ── */
[data-theme="light"] #hive-fab {
    border-color: #076eae;
    box-shadow: 0 0 18px rgba(7, 110, 174, 0.22), inset 0 0 10px rgba(7, 110, 174, 0.16);
}

[data-theme="light"] #hive-fab::after,
[data-theme="light"] #fab-shaker::before {
    border-color: rgba(7, 110, 174, 0.52);
}

[data-theme="light"] .fab-core {
    background: #e8f8ff;
    border-color: #076eae;
    box-shadow: 0 0 7px rgba(7, 110, 174, 0.5);
}

[data-theme="light"] .fab-node {
    background: #076eae;
    box-shadow: 0 0 4px rgba(7, 110, 174, 0.48);
}

[data-theme="light"] .fab-node::after {
    background: linear-gradient(90deg, rgba(7, 110, 174, 0.78), transparent);
}

[data-theme="light"] .face {
    border-color: #076eae;
    background: rgba(7, 110, 174, 0.11);
}

/* No light-theme override for .fab-particle: it flattened the accent tier back
   to one glow (same specificity, later in the file) and hard-coded a third blue.
   Both tiers derive from var(--orange), which flips to #076eae on its own. */

[data-theme="light"] .fab-shockwave {
    border-color: #076eae;
}

[data-theme="light"] #chat-modal {
    background: rgba(60, 60, 65, 0.98); /* Slightly lighter gray */
    border-color: rgba(255, 255, 255, 0.1);
    box-shadow: 0 20px 50px rgba(0,0,0,0.4);
}

[data-theme="light"] .chat-header h3,
[data-theme="light"] .chat-message.bot,
[data-theme="light"] .chat-input,
[data-theme="light"] .ai-status-indicator,
[data-theme="light"] .typing-indicator,
[data-theme="light"] .chat-close-btn {
    color: #FFFFFF !important;
}

[data-theme="light"] .chat-message.user {
    color: #FFFFFF !important;
    background: rgba(7, 110, 174, 0.4); /* Stronger blue accent for contrast */
    border-right-color: #FFFFFF; /* White border for user messages in light mode */
}

[data-theme="light"] .chat-input {
    background: rgba(0, 0, 0, 0.2);
    border-color: rgba(255, 255, 255, 0.2);
}

[data-theme="light"] .chat-input::placeholder {
    color: rgba(255, 255, 255, 0.5);
}

[data-theme="light"] .ai-status-indicator {
    background: rgba(0, 0, 0, 0.3);
    border-color: rgba(255, 255, 255, 0.2);
}

html.hive-motion-off #hive-fab::before,
html.hive-motion-off #fab-shaker::before,
html.hive-motion-off #fab-cube,
html.hive-motion-off .fab-core,
html.hive-motion-off .fab-node,
html.hive-motion-off .fab-node::after {
    animation: none !important;
}

@media (prefers-reduced-motion: reduce) {
    html:not(.force-motion) #hive-fab::before,
    html:not(.force-motion) #fab-shaker::before,
    html:not(.force-motion) #fab-cube,
    html:not(.force-motion) .fab-core,
    html:not(.force-motion) .fab-node,
    html:not(.force-motion) .fab-node::after {
        animation: none !important;
    }
}
