/* ── Reset & Base ── */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0f0f0f;
    --bg-card: #1a1a2e;
    --bg-input: #16213e;
    --accent: #0f3460;
    --accent-light: #533483;
    --text: #e0e0e0;
    --text-muted: #888;
    --user-bg: #1a3a5c;
    --bot-bg: #1a1a2e;
    --border: #2a2a4a;
    --danger: #e94560;
    --success: #00b894;
    --radius: 12px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
}

/* ── App Layout ── */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* mobile viewport */
    max-width: 800px;
    margin: 0 auto;
}

/* ── Header ── */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-left h1 {
    font-size: 18px;
    font-weight: 600;
}

.logo {
    font-size: 24px;
}

.icon-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: rgba(255,255,255,0.1);
}

/* ── Status Bar ── */
.status-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 16px;
    font-size: 12px;
    color: var(--text-muted);
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
    flex-shrink: 0;
}

.status-dot.listening {
    background: var(--danger);
    animation: pulse 1s infinite;
}

.status-dot.thinking {
    background: #f39c12;
    animation: pulse 0.8s infinite;
}

.status-dot.error {
    background: var(--danger);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

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

.chat-messages {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* ── Messages ── */
.message {
    display: flex;
    flex-direction: column;
    max-width: 88%;
    animation: fadeIn 0.3s ease;
}

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

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

.message.bot {
    align-self: flex-start;
}

.message.system {
    align-self: center;
    max-width: 95%;
}

.message-label {
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 4px;
    padding: 0 4px;
}

.message-content {
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.5;
    font-size: 15px;
    white-space: pre-wrap;
    word-break: break-word;
}

.message.user .message-content {
    background: var(--user-bg);
    border-bottom-right-radius: 4px;
}

.message.bot .message-content {
    background: var(--bot-bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message.system .message-content {
    background: rgba(255,255,255,0.05);
    text-align: center;
    font-size: 13px;
    color: var(--text-muted);
}

/* ── Document message ── */
.doc-download-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 8px;
    transition: background 0.2s;
    width: fit-content;
}

.doc-download-btn:hover {
    background: var(--accent-light);
}

.doc-content {
    max-height: 400px;
    overflow-y: auto;
    font-size: 14px;
    line-height: 1.6;
}

/* ── Typing indicator ── */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typingBounce 1.4s infinite;
}

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

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

/* ── Input Area ── */
.input-area {
    padding: 12px 16px;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

textarea {
    flex: 1;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 14px;
    color: var(--text);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 120px;
    line-height: 1.4;
    transition: border-color 0.2s;
}

textarea:focus {
    border-color: var(--accent-light);
}

.send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s;
}

.send-btn:hover {
    background: var(--accent-light);
}

/* ── Voice Button ── */
.voice-row {
    margin-top: 10px;
    display: flex;
    justify-content: center;
}

.mic-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-input);
    border: 2px solid var(--border);
    border-radius: 24px;
    padding: 10px 20px;
    color: var(--text);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    width: 100%;
    justify-content: center;
}

.mic-btn:hover {
    border-color: var(--accent-light);
    background: rgba(83, 52, 131, 0.2);
}

.mic-btn.active {
    border-color: var(--danger);
    background: rgba(233, 69, 96, 0.25);
    animation: micPulse 1.2s infinite;
    transform: scale(1.03);
}

.mic-icon {
    font-size: 20px;
}

.mic-btn.active .mic-label {
    color: var(--danger);
    font-weight: 600;
}

@keyframes micPulse {
    0%, 100% { box-shadow: 0 0 20px rgba(233, 69, 96, 0.4); }
    50% { box-shadow: 0 0 40px rgba(233, 69, 96, 0.7), 0 0 60px rgba(233, 69, 96, 0.3); }
}

/* ── Listening state: textarea + status bar ── */
textarea.listening {
    border-color: var(--danger) !important;
    box-shadow: 0 0 12px rgba(233, 69, 96, 0.4);
    animation: textareaPulse 1.5s infinite;
}

@keyframes textareaPulse {
    0%, 100% { box-shadow: 0 0 12px rgba(233, 69, 96, 0.3); }
    50% { box-shadow: 0 0 24px rgba(233, 69, 96, 0.6); }
}

.status-bar.listening-active {
    background: rgba(233, 69, 96, 0.1);
    border-bottom-color: var(--danger);
}

textarea.thinking {
    border-color: #f39c12 !important;
    box-shadow: 0 0 12px rgba(243, 156, 18, 0.3);
}

/* ── Input Hints ── */
.input-hints {
    display: flex;
    justify-content: space-between;
    margin-top: 6px;
    font-size: 11px;
    color: var(--text-muted);
}

#voiceHint {
    color: var(--danger);
    font-weight: 500;
}

/* ── Settings Modal ── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    z-index: 100;
    justify-content: center;
    align-items: flex-end;
}

.modal-overlay.open {
    display: flex;
}

.modal {
    background: var(--bg-card);
    width: 100%;
    max-width: 800px;
    max-height: 85vh;
    border-radius: 16px 16px 0 0;
    overflow-y: auto;
    padding: 20px;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal h2 {
    font-size: 18px;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal .close-btn {
    font-size: 24px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
}

/* ── Form Elements ── */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 6px;
    font-weight: 500;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: var(--accent-light);
}

.form-group textarea {
    min-height: 80px;
    resize: vertical;
}

.form-group .hint {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.btn-save {
    width: 100%;
    padding: 12px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 8px;
    transition: background 0.2s;
}

.btn-save:hover {
    background: var(--accent-light);
}

.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--success);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    z-index: 200;
    animation: fadeIn 0.3s ease;
}

/* ── Scrollbar ── */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

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

/* ── Header Titles ── */
.header-titles {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.agent-label {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--accent-light);
    font-weight: 500;
    background: rgba(83, 52, 131, 0.15);
    padding: 2px 8px;
    border-radius: 10px;
    width: fit-content;
}

.agent-icon {
    font-size: 12px;
}

/* ── Voice Row (mic + finish) ── */
.voice-row {
    margin-top: 10px;
    display: flex;
    gap: 10px;
    align-items: stretch;
}

.voice-row .mic-btn {
    flex: 1;
}

/* ── Session Button ── */
.session-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--bg-input);
    border: 2px solid var(--border);
    border-radius: 24px;
    padding: 10px 16px;
    color: var(--text);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    white-space: nowrap;
    flex-shrink: 0;
}

.session-btn:hover {
    border-color: var(--accent-light);
    background: rgba(83, 52, 131, 0.2);
}

.session-btn.active {
    border-color: var(--danger);
    background: rgba(233, 69, 96, 0.2);
    animation: sessionPulse 2s infinite;
}

.session-btn.active .session-label {
    color: var(--danger);
    font-weight: 600;
}

@keyframes sessionPulse {
    0%, 100% { box-shadow: 0 0 8px rgba(233, 69, 96, 0.3); }
    50% { box-shadow: 0 0 20px rgba(233, 69, 96, 0.6); }
}

/* ── Finish Button ── */
.finish-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--bg-input);
    border: 2px solid var(--border);
    border-radius: 24px;
    padding: 10px 18px;
    color: var(--text);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    white-space: nowrap;
    flex-shrink: 0;
}

.finish-btn:hover {
    border-color: var(--success);
    background: rgba(0, 184, 148, 0.15);
    color: var(--success);
}

.finish-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    border-color: var(--border);
    background: var(--bg-input);
    color: var(--text-muted);
}

/* ── Mobile tweaks ── */
@media (max-width: 600px) {
    .header-left h1 {
        font-size: 16px;
    }
    
    .message {
        max-width: 95%;
    }
    
    .modal {
        max-height: 90vh;
        padding: 16px;
    }
    
    .form-row {
        grid-template-columns: 1fr;
    }
}
