* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f5f7fa;
    --header-bg: #2c3e6b;
    --header-text: #ffffff;
    --bot-bg: #e8eef6;
    --bot-border: #b8c9e0;
    --user-bg: #d4edda;
    --user-border: #a8d5b5;
    --accent: #3b6ea5;
    --accent-hover: #2d5580;
    --text: #2c3e50;
    --text-light: #6c7a89;
    --popup-bg: #ffffff;
    --input-bg: #ffffff;
    --input-border: #d0d7de;
    --danger: #e74c3c;
    --word-underline: #8ba3c7;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Header */
header {
    background: var(--header-bg);
    color: var(--header-text);
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    box-shadow: var(--shadow);
    z-index: 10;
}

header h1 {
    font-size: 1.3rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}

.controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.controls select {
    padding: 0.3rem 0.5rem;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.15);
    color: var(--header-text);
    font-size: 0.85rem;
    cursor: pointer;
}

.controls select option {
    color: var(--text);
    background: var(--input-bg);
}

.auto-tts-toggle {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.8rem;
    cursor: pointer;
    user-select: none;
    opacity: 0.9;
}

.auto-tts-toggle input {
    cursor: pointer;
}

/* Chat container */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    scroll-behavior: smooth;
}

/* Messages */
.message {
    max-width: 80%;
    padding: 0.7rem 1rem;
    border-radius: 12px;
    line-height: 1.5;
    position: relative;
    animation: fadeIn 0.3s ease;
}

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

.bot-message {
    align-self: flex-start;
    background: var(--bot-bg);
    border: 1px solid var(--bot-border);
    border-radius: 12px 12px 12px 2px;
}

.user-message {
    align-self: flex-end;
    background: var(--user-bg);
    border: 1px solid var(--user-border);
    border-radius: 12px 12px 2px 12px;
}

.message-name {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 0.25rem;
}

.user-message .message-name {
    color: #2d7a4f;
    text-align: right;
}

.message-content {
    font-size: 0.95rem;
}

.message-actions {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    margin-top: 0.4rem;
}

/* TTS loading indicator */
.tts-loading {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 0.15rem 0.4rem;
}

.tts-loading span {
    display: inline-block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--accent);
    animation: ttsWave 1s ease-in-out infinite;
}

.tts-loading span:nth-child(2) {
    animation-delay: 0.15s;
}

.tts-loading span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes ttsWave {
    0%, 60%, 100% { transform: scale(0.6); opacity: 0.3; }
    30% { transform: scale(1.2); opacity: 1; }
}

.replay-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    color: var(--text-light);
    transition: background 0.2s, color 0.2s;
}

.replay-btn:hover {
    background: rgba(0, 0, 0, 0.08);
    color: var(--accent);
}

/* Clickable words */
.word {
    cursor: pointer;
    border-bottom: 1px dotted var(--word-underline);
    transition: background 0.15s;
    border-radius: 2px;
}

.word:hover {
    background: rgba(59, 110, 165, 0.12);
}

/* Typing indicator */
.typing-indicator {
    align-self: flex-start;
    padding: 0.7rem 1rem;
    background: var(--bot-bg);
    border: 1px solid var(--bot-border);
    border-radius: 12px 12px 12px 2px;
}

.typing-dots {
    display: flex;
    gap: 4px;
}

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

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

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* Footer / Input area */
footer {
    flex-shrink: 0;
    padding: 0.5rem 1rem 0.75rem;
    background: var(--input-bg);
    border-top: 1px solid var(--input-border);
}

#speech-status {
    text-align: center;
    font-size: 0.8rem;
    color: var(--danger);
    padding: 0.3rem;
    font-weight: 500;
    animation: pulse 1.5s infinite;
}

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

.input-area {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

#message-input {
    flex: 1;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--input-border);
    border-radius: 10px;
    font-size: 0.95rem;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    line-height: 1.4;
    transition: border-color 0.2s;
}

#message-input:focus {
    outline: none;
    border-color: var(--accent);
}

#mic-btn,
#send-btn {
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
    flex-shrink: 0;
}

#mic-btn {
    background: var(--bg);
    color: var(--text-light);
}

#mic-btn:hover {
    background: #e0e4ea;
}

#mic-btn.listening {
    background: var(--danger);
    color: white;
    animation: pulse 1.5s infinite;
}

#send-btn {
    background: var(--accent);
    color: white;
}

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

#send-btn:active,
#mic-btn:active {
    transform: scale(0.95);
}

#send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Translation popup */
#translation-popup {
    position: fixed;
    background: var(--popup-bg);
    border: 1px solid var(--input-border);
    border-radius: 10px;
    padding: 0.6rem 0.8rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    z-index: 100;
    transform: translateX(-50%) translateY(-100%);
    min-width: 120px;
    max-width: 280px;
    pointer-events: none;
}

#translation-popup::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-top-color: var(--popup-bg);
}

.popup-word {
    font-weight: 700;
    color: var(--accent);
    font-size: 0.95rem;
    margin-bottom: 0.2rem;
}

.popup-translation {
    font-size: 0.9rem;
    color: var(--text);
}

.popup-grammar {
    font-size: 0.75rem;
    color: var(--text-light);
    margin-top: 0.15rem;
    font-style: italic;
}

.hidden {
    display: none !important;
}

/* Responsive */
@media (max-width: 600px) {
    header {
        flex-direction: column;
        gap: 0.5rem;
        padding: 0.5rem;
    }

    .controls {
        width: 100%;
        justify-content: space-between;
    }

    .controls select {
        font-size: 0.78rem;
        padding: 0.25rem 0.3rem;
    }

    .message {
        max-width: 90%;
    }

    #chat-container {
        padding: 0.5rem;
    }
}
