:root {
    --bg-color: #0d0205;
    --panel-bg: #1a0508;
    --accent-color: #ff3366;
    --text-primary: #ffe6ea;
    --text-secondary: #995566;
    --border-color: #441122;

    --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;
}

body.broken {
    --accent-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%);
    font-size: 85vh;
    color: var(--accent-color);
    z-index: 0;
    pointer-events: none;
    opacity: 0.1;
    mix-blend-mode: screen;
    animation: heartbeat 1.5s infinite ease-in-out;

    /* 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;
}

@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: 0.2;
    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(--accent-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;
}

.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: center;
    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: 15px;
    font-family: var(--font-mono);
    font-size: 1rem;
    height: 60px;
    resize: none;
}

textarea::placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

textarea::-moz-placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

/* --- 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;
}

/* --- MOBILE RESPONSIVENESS --- */
@media (max-width: 850px) {
    .app-container {
        grid-template-columns: 1fr;
    }

    .monitor-section {
        display: none !important;
    }

    .chat-header {
        padding: 15px;
    }

    .mobile-bpm {
        display: inline !important;
    }

    /* 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 {
        padding: 15px;
    }

    /* Shrink the background heart to fix viewport spilling and prevent cutoff by input area */
    #heartIcon {
        font-size: 50vh !important;
        top: 55% !important;
    }

    #heartIcon.broken {
        top: 50% !important;
    }

    #heartIcon.broken::after {
        font-size: 15vh !important;
        left: 8vh !important;
    }

    /* Floating Reanimate Button */
    body.broken .mobile-reboot-btn {
        display: block !important;
        width: 100%;
        padding: 25px;
        font-size: 1.3rem;
        box-shadow: 0 0 20px #ff0044;
        animation: blink 0.5s infinite;
        margin-top: 15px;
        border-radius: 8px;
    }

    /* Hide the normal input field on mobile when crashed */
    body.broken .chat-input-area {
        display: none !important;
    }
}