* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    font-family: Arial, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: #f5f7fa;
}

/* Main Chat Container */
.chat-container {
    width: 100%;
    height: 100%;
    background: #fff;
    display: flex;
    flex-direction: column;
}

/* Header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 32px;
    background: #fff;
    border-bottom: 1px solid #e5e5e5;
    flex-shrink: 0;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 14px;
}

.status-dot {
    width: 10px;
    height: 10px;
    background: #22c55e;
    border-radius: 50%;
}

.header-text h1 {
    font-size: 1.25em;
    font-weight: 600;
    color: #1a1a1a;
}

.status-text {
    font-size: 0.85em;
    color: #666;
}

.icon-btn {
    background: none;
    border: none;
    padding: 10px;
    cursor: pointer;
    color: #666;
    border-radius: 8px;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: #f5f5f5;
    color: #333;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Chat Messages Area */
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 32px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    background: #f8f9fa;
    -webkit-overflow-scrolling: touch;
}

.message {
    display: flex;
    max-width: 75%;
}

.message.user {
    align-self: flex-end;
    margin-left: auto;
}

.message.assistant {
    align-self: flex-start;
    max-width: 85%;
}

.message-content {
    padding: 18px 24px;
    border-radius: 16px;
    line-height: 1.7;
    font-size: 1em;
    word-wrap: break-word;
    overflow-wrap: break-word;
    overflow: hidden;
    max-width: 100%;
}

.message.user .message-content {
    background: #0066ff;
    color: white;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: #fff;
    color: #1a1a1a;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* User Image Message */
.message.user .message-image {
    max-width: 200px;
    max-height: 150px;
    border-radius: 12px;
    margin-bottom: 8px;
    display: block;
}

/* Processing indicator */
.processing-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 20px;
    background: #fff;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    color: #666;
    font-size: 0.9em;
}

.processing-indicator svg {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Response formatting */
.message-content strong {
    display: block;
    margin-top: 20px;
    margin-bottom: 10px;
    color: #0066ff;
    font-size: 1em;
}

.message-content strong:first-child {
    margin-top: 0;
}

.message-content ul {
    margin: 12px 0;
    padding-left: 24px;
}

.message-content li {
    margin: 8px 0;
    line-height: 1.6;
}

/* ========== CODE BLOCKS ========== */
.simple-code-block {
    background: #1e1e2e;
    border-radius: 8px;
    margin: 12px 0;
    border-left: 3px solid #6366f1;
    /* CRITICAL: Contain within parent */
    max-width: 100%;
    width: 100%;
    overflow: hidden;
    box-sizing: border-box;
}

.simple-code-block .code-label {
    background: #2d2d3d;
    color: #8888ff;
    font-size: 0.75em;
    padding: 8px 14px;
    text-transform: uppercase;
    font-weight: 600;
}

/* SCROLL CONTAINER - Critical for horizontal scrolling */
.simple-code-block pre {
    margin: 0;
    padding: 14px;
    background: transparent;
    /* Horizontal scroll when content overflows */
    overflow-x: scroll;
    overflow-y: auto;
    max-height: 400px;
    -webkit-overflow-scrolling: touch;
    /* Ensure it doesn't exceed parent */
    max-width: 100%;
    box-sizing: border-box;
}

.simple-code-block code {
    font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
    font-size: 0.88em;
    line-height: 1.5;
    color: #e0e0e0;
    /* CRITICAL: Preserve whitespace, enable scrolling */
    white-space: pre;
    word-wrap: normal;
    display: block;
    /* Force content to be its natural width for scrolling */
    min-width: max-content;
}

/* Inline code */
.message-content code.inline-code {
    background: #e8f0fe;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.88em;
    color: #1a73e8;
    font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
}

.message.assistant .message-content code.inline-code {
    background: #f0f4f8;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 5px;
    padding: 18px 24px;
    background: #fff;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: #ccc;
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); background: #ccc; }
    30% { transform: translateY(-5px); background: #999; }
}

/* Input Area */
.chat-input-area {
    padding: 20px 32px 24px;
    background: #fff;
    border-top: 1px solid #e5e5e5;
    flex-shrink: 0;
}

.input-container {
    max-width: 900px;
    margin: 0 auto;
}

/* Image Preview */
.image-preview-container {
    display: none;
    margin-bottom: 12px;
    padding: 12px;
    background: #f0f4f8;
    border-radius: 12px;
    align-items: center;
    gap: 12px;
}

.image-preview-container.active {
    display: flex;
}

.image-preview-wrapper {
    position: relative;
    display: inline-block;
}

.image-preview-wrapper img {
    max-width: 120px;
    max-height: 80px;
    border-radius: 8px;
    object-fit: cover;
}

.remove-image-btn {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #ef4444;
    color: white;
    border: 2px solid white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.image-preview-label {
    font-size: 0.85em;
    color: #666;
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background: #f5f5f5;
    border-radius: 28px;
    padding: 10px 12px 10px 24px;
}

.input-wrapper:focus-within {
    box-shadow: 0 0 0 2px rgba(0, 102, 255, 0.2);
}

.input-wrapper textarea {
    flex: 1;
    border: none;
    background: none;
    resize: none;
    font-family: inherit;
    font-size: 1em;
    line-height: 1.5;
    padding: 10px 0;
    max-height: 150px;
    outline: none;
    color: #1a1a1a;
}

.input-wrapper textarea::placeholder {
    color: #999;
}

.mic-btn, .send-btn, .image-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.image-btn {
    background: #e8e8e8;
    color: #555;
}

.image-btn:hover {
    background: #ddd;
}

.mic-btn {
    background: #e8e8e8;
    color: #555;
}

.mic-btn:hover {
    background: #ddd;
}

.mic-btn.listening {
    background: #ef4444;
    color: white;
    animation: pulse-mic 1.5s infinite;
}

@keyframes pulse-mic {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
    50% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
}

.send-btn {
    background: #0066ff;
    color: white;
}

.send-btn:hover {
    background: #0052cc;
}

.send-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.input-hint {
    text-align: center;
    font-size: 0.8em;
    color: #999;
    margin-top: 10px;
}

/* Error message */
.error-message {
    color: #ef4444;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 0.9em;
    z-index: 2000;
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast-info { background: #1a1a1a; color: white; }
.toast-success { background: #22c55e; color: white; }
.toast-error { background: #ef4444; color: white; }

/* Drag Over */
.input-wrapper.drag-over {
    border: 2px dashed #0066ff !important;
    background: #f0f7ff;
}

/* Scrollbar */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 3px;
}

/* ========== TABLET (768px - 1023px) ========== */
@media (min-width: 768px) and (max-width: 1023px) {
    .chat-header {
        padding: 14px 24px;
    }
    
    .chat-messages {
        padding: 24px;
        gap: 20px;
    }
    
    .message {
        max-width: 85%;
    }
    
    .message-content {
        padding: 16px 20px;
    }
    
    .chat-input-area {
        padding: 16px 24px 20px;
    }
}

/* ========== MOBILE (< 768px) ========== */
@media (max-width: 767px) {
    html, body {
        height: 100%;
        height: 100dvh;
        overflow: hidden;
    }
    
    .chat-container {
        height: 100%;
        height: 100dvh;
    }
    
    .chat-header {
        padding: 12px 16px;
    }
    
    .header-info {
        gap: 10px;
    }
    
    .status-dot {
        width: 8px;
        height: 8px;
    }
    
    .header-text h1 {
        font-size: 1.05em;
    }
    
    .status-text {
        font-size: 0.7em;
    }
    
    .icon-btn {
        padding: 8px;
        min-width: 44px;
        min-height: 44px;
    }
    
    /* SCROLLABLE CHAT */
    .chat-messages {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 12px;
        gap: 12px;
    }
    
    .message {
        max-width: 92%;
    }
    
    .message.assistant {
        max-width: 98%;
    }
    
    .message-content {
        padding: 12px 14px;
        font-size: 0.95em;
        line-height: 1.6;
        border-radius: 14px;
        overflow: hidden;
        max-width: 100%;
    }
    
    .message-content strong {
        margin-top: 14px;
        margin-bottom: 8px;
        font-size: 0.95em;
    }
    
    .message-content ul {
        padding-left: 18px;
        margin: 8px 0;
    }
    
    .message-content li {
        margin: 6px 0;
    }
    
    .message.user .message-image {
        max-width: 150px;
        max-height: 100px;
    }
    
    /* CODE BLOCKS - MOBILE */
    .simple-code-block {
        margin: 8px 0;
        border-radius: 6px;
        border-left: 3px solid #6366f1;
        max-width: 100%;
        width: 100%;
        overflow: hidden;
        box-sizing: border-box;
    }
    
    .simple-code-block .code-label {
        font-size: 0.65em;
        padding: 6px 10px;
    }
    
    .simple-code-block pre {
        padding: 10px;
        max-height: 250px;
        /* CRITICAL: scroll not auto for mobile */
        overflow-x: scroll;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
        box-sizing: border-box;
    }
    
    .simple-code-block code {
        font-size: 0.75em;
        line-height: 1.4;
        white-space: pre;
        word-wrap: normal;
        min-width: max-content;
    }
    
    .message-content code.inline-code {
        font-size: 0.82em;
        padding: 2px 5px;
    }
    
    /* Input Area */
    .chat-input-area {
        padding: 10px 12px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
    }
    
    .input-container {
        max-width: 100%;
    }
    
    .image-preview-container {
        padding: 10px;
        margin-bottom: 10px;
        gap: 10px;
    }
    
    .image-preview-wrapper img {
        max-width: 80px;
        max-height: 60px;
    }
    
    .remove-image-btn {
        width: 22px;
        height: 22px;
        top: -6px;
        right: -6px;
    }
    
    .input-wrapper {
        padding: 6px 6px 6px 12px;
        gap: 6px;
        border-radius: 24px;
    }
    
    .input-wrapper textarea {
        font-size: 16px;
        padding: 8px 0;
        min-height: 40px;
    }
    
    .mic-btn, .send-btn, .image-btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
    }
    
    .mic-btn svg, .send-btn svg, .image-btn svg {
        width: 18px;
        height: 18px;
    }
    
    .input-hint {
        font-size: 0.7em;
        margin-top: 8px;
    }
    
    .toast {
        bottom: 80px;
        font-size: 0.85em;
    }
    
    .processing-indicator {
        padding: 12px 16px;
        font-size: 0.85em;
    }
}

/* ========== SMALL PHONES (< 400px) ========== */
@media (max-width: 399px) {
    .chat-header {
        padding: 10px 12px;
    }
    
    .header-text h1 {
        font-size: 0.95em;
    }
    
    .status-text {
        display: none;
    }
    
    .chat-messages {
        padding: 10px;
        gap: 10px;
    }
    
    .message {
        max-width: 95%;
    }
    
    .message.assistant {
        max-width: 100%;
    }
    
    .message-content {
        padding: 10px 12px;
        font-size: 0.9em;
        overflow: hidden;
        max-width: 100%;
    }
    
    .simple-code-block {
        max-width: 100%;
        width: 100%;
        border-left: 2px solid #6366f1;
        overflow: hidden;
        box-sizing: border-box;
    }
    
    .simple-code-block .code-label {
        font-size: 0.6em;
        padding: 5px 8px;
    }
    
    .simple-code-block pre {
        padding: 8px;
        max-height: 200px;
        max-width: 100%;
        overflow-x: scroll;
        -webkit-overflow-scrolling: touch;
        box-sizing: border-box;
    }
    
    .simple-code-block code {
        font-size: 0.7em;
        min-width: max-content;
    }
    
    .chat-input-area {
        padding: 8px 10px;
        padding-bottom: calc(8px + env(safe-area-inset-bottom, 0));
    }
    
    .input-wrapper {
        padding: 5px 5px 5px 10px;
        gap: 4px;
    }
    
    .mic-btn, .send-btn, .image-btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
    }
    
    .input-hint {
        font-size: 0.65em;
    }
}

/* ========== LANDSCAPE PHONES ========== */
@media (max-width: 767px) and (orientation: landscape) {
    .chat-header {
        padding: 8px 16px;
    }
    
    .status-text {
        display: none;
    }
    
    .chat-messages {
        padding: 10px 16px;
        gap: 10px;
    }
    
    .simple-code-block pre {
        max-height: 150px;
    }
    
    .chat-input-area {
        padding: 8px 16px;
    }
    
    .input-hint {
        display: none;
    }
}

/* ========== SAFE AREA (Notched Phones) ========== */
@supports (padding: env(safe-area-inset-bottom)) {
    @media (max-width: 767px) {
        .chat-input-area {
            padding-bottom: calc(10px + env(safe-area-inset-bottom));
        }
    }
}
