/* ORIGINAL THEME */
:root {
    --bg-color: #0d0205;
    --panel-bg: #1a0508;
    --accent-color: #ff5580;
    --text-primary: #ffe6ea;
    --text-secondary: #995566;
    --border-color: #772244;

    --chat-user-bg: rgba(51, 10, 21, 0.7);
    --chat-ai-bg: rgba(34, 8, 14, 0.7);

    --font-mono: 'Space Mono', monospace;
    --font-head: 'Syncopate', sans-serif;
    --brand-color: #ff3366;
}

/* ORIGINAL DARK RED THEME (Backup)
:root {
    --bg-color: #0d0205;
    --panel-bg: #1a0508;
    --accent-color: #ff5580;
    --text-primary: #ffe6ea;
    --text-secondary: #995566;
    --border-color: #772244;
    --chat-user-bg: rgba(51, 10, 21, 0.7);
    --chat-ai-bg: rgba(34, 8, 14, 0.7);
    --font-mono: 'Space Mono', monospace;
    --font-head: 'Syncopate', sans-serif;
    --brand-color: #ff3366;
}
*/

body.broken {
    --accent-color: #ff0000;
    --brand-color: #ff0000;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: var(--font-mono);
    height: 100dvh;
    overflow: hidden;
}

/* CONFIG MODAL */
.config-modal {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(10, 5, 5, 0.95);
    border: 1px solid var(--accent-color);
    padding: 20px;
    z-index: 10000;
    width: 300px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
    text-align: center;
    font-size: 0.8rem;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.config-modal.active {
    display: block;
    animation: fadeIn 0.8s ease-in forwards;
}

.config-modal h3 {
    color: var(--accent-color);
    margin-bottom: 15px;
    font-size: 0.9rem;
}

.config-modal input {
    width: 100%;
    background: #000;
    border: 1px solid #333;
    color: white;
    padding: 8px;
    margin-bottom: 10px;
    font-family: monospace;
}

.config-modal button {
    background: var(--accent-color);
    border: none;
    color: white;
    padding: 5px 15px;
    cursor: pointer;
    font-family: var(--font-mono);
}

.modal-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 9999;
}

.modal-backdrop.active {
    display: block;
    animation: fadeIn 0.8s ease-in forwards;
}

/* --- CRASH OVERLAY --- */
.crack-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none"><path d="M0,0 L30,40 L10,60 L40,50 L60,80 L80,20 L100,0" fill="none" stroke="rgba(255,255,255,0.1)" vector-effect="non-scaling-stroke"/></svg>');
    background-size: cover;
    pointer-events: none;
    z-index: 9999;
    opacity: 0;
    mix-blend-mode: overlay;
    transition: opacity 0.2s;
}

body.broken .crack-overlay {
    opacity: 1;
    background: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(255, 0, 0, 0.1) 10px, rgba(255, 0, 0, 0.1) 20px);
}

/* --- GRID LAYOUT --- */
.app-container {
    display: grid;
    grid-template-columns: 1fr 400px;
    height: 100%;
}

/* --- Chat --- */
.chat-section {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    background: var(--bg-color);
    position: relative;
    z-index: 10;
    height: 100%;
    overflow: hidden;
}

/* THE HEART */
.heart-display {
    position: absolute;
    top: 53%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.95);
    font-size: 85vh;
    color: var(--brand-color);
    z-index: 0;
    pointer-events: none;
    opacity: 0.2;
    mix-blend-mode: screen;
    transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s;

    /* Fix for iOS Safari: mix-blend-mode clips glyphs that exceed their line-box. 
       Adding uniform padding expands the compositing layer bounds without shifting the optical center. */
    line-height: 1;
    padding: 10vh;
}

.heart-display.pump {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 0.6;
    transition: transform 0.05s ease-out, opacity 0.05s ease-out;
}

.heart-display.idle-pulse {
    animation: heartbeat 1.0s infinite ease-in-out;
}

.heart-display.beat-anim {
    animation: heartbeat var(--beat-duration, 1.0s) ease-in-out;
}

@keyframes heartbeat {
    0% {
        transform: translate(-50%, -50%) scale(0.95);
        opacity: 0.2;
    }

    15% {
        transform: translate(-50%, -50%) scale(1.1);
        opacity: 0.6;
    }

    30% {
        transform: translate(-50%, -50%) scale(0.95);
        opacity: 0.2;
    }

    100% {
        transform: translate(-50%, -50%) scale(0.95);
        opacity: 0.2;
    }
}

#heartIcon.broken {
    animation: none !important;
    transform: translate(-50%, -50%) rotate(15deg) scale(0.9);
    opacity: 1.0; /* Increased opacity so it's not pale */
    top: 50% !important;
}

#heartIcon.broken::after {
    content: "⚡";
    position: absolute;
    top: 0vh; /* relative to padding box */
    left: 20vh; 
    color: #ffcc00;
    font-size: 20vh;
    filter: drop-shadow(0 0 10px rgba(255, 204, 0, 0.4));
}

.chat-header {
    background: rgba(26, 5, 8, 0.8);
    backdrop-filter: blur(5px);
    z-index: 2;
    padding: 20px;
}

.chat-header h1 {
    font-family: var(--font-head);
    font-size: 2.5rem;
    color: var(--brand-color);
}

.header-status {
    display: flex;
    line-height: 1;
    margin-bottom: 5px;
}

.status-text {
    font-size: 0.7rem;
    color: var(--text-secondary);
    letter-spacing: 1px;
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    z-index: 2;
    min-height: 0;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE and Edge */
}

.chat-history::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Opera */
    width: 0;
    height: 0;
}

#infoModal {
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE and Edge */
}

#infoModal::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Opera */
    width: 0;
    height: 0;
}

.info-scroll-fade {
    position: sticky;
    bottom: -30px;
    left: 0;
    right: 0;
    height: 135px;
    margin-top: -135px;
    margin-left: -30px;
    margin-right: -30px;
    margin-bottom: -30px;
    background: linear-gradient(to top, rgba(10, 5, 5, 0.98) 0%, rgba(10, 5, 5, 0.88) 35%, rgba(10, 5, 5, 0.45) 70%, rgba(10, 5, 5, 0) 100%);
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 10;
    border-bottom-left-radius: 12px;
    border-bottom-right-radius: 12px;
}

.info-scroll-fade.no-fade {
    opacity: 0;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
}

.message.ai {
    align-self: flex-start;
}

.message.user {
    align-self: flex-end;
}

.bubble {
    padding: 12px 18px;
    border-radius: 12px;
    font-size: 0.95rem;
    line-height: 1.5;
    backdrop-filter: blur(2px);

    /* FIX: Typewriter support */
    white-space: pre-wrap;
}

/* Specific Bubble for Typing Indicator (Minimal Padding) */
/* Specific Bubble for Typing Indicator */
.bubble.typing-mode {
    padding: 12px 18px;
    /* Standard Padding */
    min-height: 48px;
    /* Match Text Height */
    display: inline-flex;
    align-items: center;
    width: auto;
}

/* TYPING INDICATOR */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 0;
    align-items: center;
    justify-content: center;
    height: auto;
}

.typing-dot {
    width: 6px;
    height: 6px;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

.message.ai .bubble {
    background: var(--chat-ai-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
}

.message.user .bubble {
    background: var(--chat-user-bg);
    border: 1px solid var(--accent-color);
    color: #fff;
}

.chat-input-area {
    padding: 30px;
    background: rgba(26, 5, 8, 0.9);
    z-index: 2;
    display: flex;
    gap: 15px;
    flex-shrink: 0;
    min-height: 100px;
    align-items: stretch;
    border-top: 1px solid var(--border-color);
}

textarea {
    flex: 1;
    min-width: 0;
    max-width: 100%;
    background: #0d0205;
    border: 1px solid var(--border-color);
    color: var(--accent-color);
    padding: 19px 16px 14px 16px;
    font-family: var(--font-mono);
    font-size: 1rem;
    line-height: 1.4;
    min-height: 60px;
    resize: none;
    border-radius: 4px;
    transition: border-color 0.2s;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}

textarea::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

textarea:focus {
    outline: none;
    border-color: var(--accent-color);
}

textarea:hover {
    border-color: var(--accent-color);
}

textarea::placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

textarea::-moz-placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

#sendBtn {
    background: transparent;
    color: var(--text-secondary);
    font-family: var(--font-mono);
    font-size: 1rem;
    padding: 10px 20px;
    cursor: pointer;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    transition: border-color 0.2s, color 0.2s;
}

#sendBtn:hover,
#sendBtn:focus {
    border-color: var(--accent-color);
    color: var(--accent-color);
}

#sendBtn:disabled {
    opacity: 0.35 !important;
    cursor: not-allowed;
}

#sendBtn .send-icon {
    display: none !important;
}

#sendBtn .send-text {
    display: inline !important;
}

/* --- Monitor --- */
.monitor-section {
    background: #080203;
    padding: 20px;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--border-color);
    gap: 10px;
    z-index: 10;
    overflow: hidden;
}

.monitor-screen {
    background: #000;
    border: 1px solid var(--border-color);
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    overflow: hidden;
}

.monitor-header {
    padding: 10px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    white-space: nowrap;
    overflow: hidden;
}

.monitor-header h2 {
    font-size: 1rem;
    font-family: var(--font-head);
    color: var(--text-primary);
    margin: 0;
}

.monitor-header h2 .sub-text {
    font-size: 0.7em;
    opacity: 0.7;
}

/* --- DEBUG INFO (Fine-Tuning) --- */
.debug-info {
    font-family: 'Courier New', monospace;
    font-size: 0.75rem;
    color: var(--accent-color);
    margin-top: 4px;
    padding: 4px 8px;
    border-left: 2px solid var(--accent-color);
    background: rgba(0, 0, 0, 0.2);
    display: block;
    white-space: pre-wrap;
    opacity: 0.8;
}

.canvas-wrapper {
    flex: 1;
    background: #000;
    position: relative;
    overflow: hidden;
    min-height: 0;
}

canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.monitor-readout {
    padding: 15px;
    border-top: 1px solid var(--border-color);
}

.reboot-btn {
    margin-top: 10px;
    width: 100%;
    padding: 10px;
    background: transparent;
    border: 1px solid #ff0000;
    color: #ff0000;
    font-family: var(--font-head);
    display: none;
    cursor: pointer;
}

body.broken .reboot-btn {
    display: block;
    animation: blink 0.5s infinite;
}

@keyframes blink {
    50% {
        opacity: 0;
    }
}

/* Custom Scrollbar (Webkit) */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #0d0205;
}

::-webkit-scrollbar-thumb {
    background: var(--accent-color);
    border-radius: 4px;
    border: 1px solid var(--border-color);
}

::-webkit-scrollbar-thumb:hover {
    background: #ff0044;
}

/* Firefox Scrollbar */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--accent-color) #0d0205;
}

.mobile-reboot-btn {
    display: none !important;
}

/* --- ATTENTION PULSE FOR INFO BUTTON --- */
.attention-pulse {
    animation: buttonHeartbeat 1s ease-in-out 10;
}

@keyframes buttonHeartbeat {
    0% {
        transform: scale(1);
    }
    15% {
        transform: scale(1.15);
        box-shadow: 0 0 10px 2px rgba(255, 85, 128, 0.5);
        color: var(--accent-color);
        border-color: var(--accent-color);
    }
    30% {
        transform: scale(1);
    }
    100% {
        transform: scale(1);
    }
}

/* --- MOBILE RESPONSIVENESS --- */
@media (max-width: 850px) {
    body {
        height: var(--visual-vh, 100dvh) !important;
        max-height: var(--visual-vh, 100dvh) !important;
    }

    .app-container {
        grid-template-columns: 1fr;
        height: var(--visual-vh, 100dvh) !important;
        max-height: var(--visual-vh, 100dvh) !important;
    }

    .chat-section {
        height: var(--visual-vh, 100dvh) !important;
        max-height: var(--visual-vh, 100dvh) !important;
    }

    .monitor-section {
        display: none !important;
    }

    .chat-header {
        padding: 15px;
    }

    .mobile-bpm {
        display: inline-block !important;
        will-change: transform;
    }

    /* MOBILE BPM BOUNCE ANIMATIONS */
    .mobile-bpm.bounce-right-small { animation: bpmBounceRightSmall 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
    .mobile-bpm.bounce-right-med   { animation: bpmBounceRightMed 0.42s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
    .mobile-bpm.bounce-right-large { animation: bpmBounceRightLarge 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }

    /* Falling pulse: Significantly slower and more soothing the larger the drop */
    .mobile-bpm.bounce-left-small  { animation: bpmBounceLeftSmall 0.75s cubic-bezier(0.25, 1, 0.5, 1) forwards; }
    .mobile-bpm.bounce-left-med    { animation: bpmBounceLeftMed 1.15s cubic-bezier(0.25, 1, 0.5, 1) forwards; }
    .mobile-bpm.bounce-left-large  { animation: bpmBounceLeftLarge 1.6s cubic-bezier(0.25, 1, 0.5, 1) forwards; }

    @keyframes bpmBounceRightSmall {
        0%   { transform: translateX(0); }
        35%  { transform: translateX(5px); }
        100% { transform: translateX(0); }
    }
    @keyframes bpmBounceRightMed {
        0%   { transform: translateX(0); }
        30%  { transform: translateX(12px); }
        70%  { transform: translateX(-2px); }
        100% { transform: translateX(0); }
    }
    @keyframes bpmBounceRightLarge {
        0%   { transform: translateX(0); }
        25%  { transform: translateX(22px); }
        60%  { transform: translateX(-4px); }
        100% { transform: translateX(0); }
    }

    @keyframes bpmBounceLeftSmall {
        0%   { transform: translateX(0); }
        25%  { transform: translateX(-4px); }
        100% { transform: translateX(0); }
    }
    @keyframes bpmBounceLeftMed {
        0%   { transform: translateX(0); }
        22%  { transform: translateX(-10px); }
        100% { transform: translateX(0); }
    }
    @keyframes bpmBounceLeftLarge {
        0%   { transform: translateX(0); }
        20%  { transform: translateX(-18px); }
        100% { transform: translateX(0); }
    }

    /* Make overlays fit screen */
    .config-modal {
        width: 95vw !important;
        max-width: none !important;
        padding: 15px;
    }

    /* Ensure text input area accommodates mobile keyboards */
    .chat-input-area {
        min-height: auto;
        padding: 10px;
        gap: 0;
        align-items: center;
    }

    textarea {
        height: 62px !important;
        min-height: 62px !important;
        max-height: 62px !important;
        padding: 10px 12px;
        font-size: 0.95rem;
        line-height: 1.35;
        box-sizing: border-box;
        border-radius: 4px;
    }

    #sendBtn {
        display: flex !important;
        align-items: center;
        justify-content: center;
        background: #000000 !important;
        color: #ffffff !important;
        border: 1px solid #ffffff !important;
        box-shadow: none !important;
        width: 42px !important;
        min-width: 42px !important;
        height: 62px !important;
        min-height: 62px !important;
        max-height: 62px !important;
        border-radius: 4px !important;
        margin-left: 10px !important;
        margin-right: 12px !important;
        padding: 0 !important;
        flex-shrink: 0;
        box-sizing: border-box;
        cursor: pointer;
    }

    #sendBtn .send-text {
        display: none !important;
    }

    #sendBtn .send-icon {
        display: block !important;
        stroke: var(--text-secondary, #995566) !important;
        width: 18px !important;
        height: 18px !important;
    }

    #sendBtn:hover,
    #sendBtn:focus,
    #sendBtn:active {
        background: #000000 !important;
        border: 1px solid #ffffff !important;
        box-shadow: none !important;
        transform: none !important;
    }

    #sendBtn:hover svg,
    #sendBtn:focus svg,
    #sendBtn:active svg {
        stroke: var(--text-secondary, #995566) !important;
    }

    .feedback-actions {
        border-left: none !important;
        padding-left: 0 !important;
        flex-shrink: 0;
        height: 62px;
        box-sizing: border-box;
    }

    /* Background heart: fixed to screen, decoupled from keyboard resizing */
    #heartIcon {
        position: fixed !important;
        left: 50% !important;
        font-size: 50vh !important;
        font-size: 50lvh !important;
        font-size: calc(var(--screen-h, 100vh) * 0.50) !important;
        top: calc(var(--screen-h, 100vh) * 0.55) !important;
    }

    #heartIcon.broken {
        top: calc(var(--screen-h, 100vh) * 0.50) !important;
    }

    #heartIcon.broken::after {
        font-size: 15vh !important;
        left: 8vh !important;
    }

    /* Floating Reanimate Button */
    body.broken .mobile-reboot-btn {
        display: block !important;
        width: calc(100% - 32px);
        margin: 12px auto max(18px, env(safe-area-inset-bottom, 18px)) auto;
        padding: 18px 20px;
        font-size: 1.2rem;
        font-weight: normal !important;
        letter-spacing: 2px;
        border: 2px solid var(--accent-color) !important;
        color: var(--accent-color) !important;
        box-shadow: 0 0 20px var(--accent-color);
        animation: blink 0.5s infinite;
        border-radius: 8px;
        box-sizing: border-box;
    }

    /* Hide the normal input field on mobile when crashed */
    body.broken .chat-input-area {
        display: none !important;
    }
}