/* CSS Reset and Base Styles */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary-color: #3b82f6;
    --primary-dark: #2563eb;
    --bg-color: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #252540;
    --text-color: #e4e4e7;
    --text-muted: #a1a1aa;
    --user-bubble: #3b82f6;
    --assistant-bubble: #374151;
    --border-color: #3f3f5a;
    --success-color: #22c55e;
    --warning-color: #f59e0b;
    --error-color: #ef4444;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
    --radius: 16px;
    --radius-sm: 8px;
    --transition: 0.2s ease;
}

html, body {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Chat Container */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 100%;
    margin: 0 auto;
    background: var(--bg-color);
}

/* Header */
.chat-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: 12px 16px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-color);
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning-color);
    transition: background var(--transition);
}

.connection-status.connected .status-dot {
    background: var(--success-color);
}

.connection-status.disconnected .status-dot {
    background: var(--error-color);
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 100%;
}

/* Message Bubbles */
.message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    align-self: flex-end;
    align-items: flex-end;
}

.message.assistant {
    align-self: flex-start;
    align-items: flex-start;
}

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius);
    word-wrap: break-word;
    overflow-wrap: break-word;
    box-shadow: var(--shadow);
}

.message.user .message-bubble {
    background: var(--user-bubble);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--assistant-bubble);
    color: var(--text-color);
    border-bottom-left-radius: 4px;
}

.message-time {
    font-size: 0.65rem;
    color: var(--text-muted);
    margin-top: 4px;
    padding: 0 4px;
}

/* Markdown Styles */
.message-bubble strong {
    font-weight: 600;
}

.message-bubble em {
    font-style: italic;
}

.message-bubble code {
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.875em;
}

.message-bubble pre {
    background: rgba(0, 0, 0, 0.4);
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}

.message-bubble pre code {
    background: none;
    padding: 0;
}

/* Typing Indicator */
.typing-indicator {
    display: none;
    padding: 8px 0;
}

.typing-indicator.visible {
    display: block;
}

.typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: var(--assistant-bubble);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) {
    animation-delay: 0s;
}

.typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes bounce {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-6px);
    }
}

/* Input Area */
.input-area {
    position: sticky;
    bottom: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.input-container {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    transition: border-color var(--transition);
}

.input-container:focus-within {
    border-color: var(--primary-color);
}

#messageInput {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-color);
    font-size: 1rem;
    line-height: 1.5;
    resize: none;
    max-height: 120px;
    min-height: 24px;
    outline: none;
    font-family: inherit;
}

#messageInput::placeholder {
    color: var(--text-muted);
}

.input-buttons {
    display: flex;
    gap: 4px;
}

.btn-voice, .btn-send {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    position: relative;
}

.btn-voice {
    background: var(--bg-secondary);
    color: var(--text-color);
}

.btn-voice:hover {
    background: var(--bg-color);
}

.btn-voice.recording {
    background: var(--error-color);
    color: white;
}

.btn-voice svg {
    width: 20px;
    height: 20px;
}

.recording-pulse {
    display: none;
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 2px solid var(--error-color);
    animation: pulse 1.5s infinite;
}

.btn-voice.recording .recording-pulse {
    display: block;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    100% {
        transform: scale(1.4);
        opacity: 0;
    }
}

.btn-send {
    background: var(--primary-color);
    color: white;
}

.btn-send:hover {
    background: var(--primary-dark);
}

.btn-send:disabled {
    background: var(--bg-color);
    color: var(--text-muted);
    cursor: not-allowed;
}

.btn-send svg {
    width: 18px;
    height: 18px;
}

.char-counter {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-align: right;
    margin-top: 4px;
    padding-right: 4px;
}

.char-counter.warning {
    color: var(--warning-color);
}

.char-counter.error {
    color: var(--error-color);
}

/* Audio Container */
.audio-container {
    display: none;
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-secondary);
    padding: 12px 16px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    z-index: 200;
    border: 1px solid var(--border-color);
}

.audio-container.visible {
    display: block;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

#audioPlayer {
    height: 36px;
    width: 250px;
    border-radius: var(--radius-sm);
}

#audioPlayer::-webkit-media-controls-panel {
    background: var(--bg-tertiary);
}

/* Processing Indicator */
.processing-message {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--assistant-bubble);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
    color: var(--text-muted);
    font-style: italic;
}

.processing-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--text-muted);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Responsive Design */
@media (min-width: 640px) {
    .chat-container {
        max-width: 640px;
        margin: 0 auto;
        border-left: 1px solid var(--border-color);
        border-right: 1px solid var(--border-color);
    }

    .message {
        max-width: 75%;
    }

    .chat-header {
        padding: 16px 20px;
    }

    .messages-container {
        padding: 20px;
    }

    .input-area {
        padding: 16px 20px;
    }
}

@media (min-width: 1024px) {
    .chat-container {
        max-width: 800px;
    }

    .message {
        max-width: 65%;
    }
}

/* Scrollbar Styling */
.messages-container::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-track {
    background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Dark mode is default, but add light mode support if needed */
@media (prefers-color-scheme: light) {
    /* Keep dark theme regardless of system preference */
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Focus styles for accessibility */
button:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}
