/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: #f5f5f5;
    color: #333;
    line-height: 1.6;
}

/* Container */
.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

/* Header */
h1 {
    text-align: center;
    color: #2c3e50;
    margin-bottom: 30px;
    font-size: 2.5em;
    flex-shrink: 0; /* Prevent header from shrinking */
}

/* Chat area */
.chat-area {
    flex: 1;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    padding: 20px;
    margin-bottom: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Important for flex child scrolling */
}

#conversation-display {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 10px;
    /* Smooth scrolling */
    scroll-behavior: smooth;
    /* Prevent height changes */
    min-height: 0;
    /* Always show scrollbar to prevent layout shifts */
    scrollbar-gutter: stable;
}

/* Custom scrollbar styling to ensure consistent appearance */
#conversation-display::-webkit-scrollbar {
    width: 8px;
}

#conversation-display::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

#conversation-display::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
}

#conversation-display::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Messages */
.message {
    margin-bottom: 20px;
    animation: fadeIn 0.3s ease-in;
}

.message.user {
    text-align: right;
}

.message.assistant {
    text-align: left;
}

.speaker {
    font-weight: bold;
    color: #666;
    font-size: 0.9em;
    margin-bottom: 5px;
}

.message.user .speaker {
    color: #3498db;
}

.message.assistant .speaker {
    color: #27ae60;
}

.text {
    display: inline-block;
    background: #f0f0f0;
    padding: 10px 15px;
    border-radius: 15px;
    max-width: 70%;
    word-wrap: break-word;
}

.message.user .text {
    background: #3498db;
    color: white;
}

.message.assistant .text {
    background: #ecf0f1;
    color: #2c3e50;
}

/* Controls */
.controls {
    text-align: center;
    flex-shrink: 0; /* Prevent controls from shrinking */
    padding: 10px 0;
}

.btn-primary {
    background: #3498db;
    color: white;
    border: none;
    padding: 15px 40px;
    font-size: 1.1em;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.btn-primary:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0,0,0,0.15);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-primary.active {
    background: #e74c3c;
}

.btn-primary.active:hover {
    background: #c0392b;
}

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

/* Model Loading UI */
.model-loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(5px);
}

.model-loading.hidden {
    display: none;
}

.loading-content {
    background: white;
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
    max-width: 500px;
    width: 90%;
    text-align: center;
}

.loading-content h2 {
    color: #2c3e50;
    margin-bottom: 15px;
}

.loading-content p {
    color: #7f8c8d;
    margin-bottom: 25px;
    line-height: 1.6;
}

.progress-container {
    width: 100%;
    height: 20px;
    background: #ecf0f1;
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 15px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #3498db, #2980b9);
    width: 0%;
    transition: width 0.3s ease;
    border-radius: 10px;
}

.progress-text {
    color: #7f8c8d;
    font-size: 0.9em;
}

/* Translation tooltip */
.translatable {
    position: relative;
    cursor: help;
    border-bottom: 1px dashed #3498db;
}

.translatable:hover {
    background-color: rgba(52, 152, 219, 0.1);
}

.translation-tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #2c3e50;
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.9em;
    white-space: nowrap;
    max-width: 300px;
    white-space: normal;
    word-wrap: break-word;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    margin-bottom: 5px;
}


/* Adjust message text positioning for tooltip */
.message.assistant .text {
    position: relative;
    margin-bottom: 10px;
}

/* Thinking bubble animation */
.thinking-bubble {
    display: inline-block;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px 25px;
    border-radius: 25px;
    position: relative;
    animation: float 2s ease-in-out infinite;
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.thinking-bubble::before {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 30px;
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
    animation: bubble-tail 2s ease-in-out infinite;
}

.thinking-bubble::after {
    content: '';
    position: absolute;
    bottom: -18px;
    left: 20px;
    width: 12px;
    height: 12px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
    animation: bubble-tail-small 2s ease-in-out infinite;
}

.thinking-dot {
    display: inline-block;
    width: 12px;
    height: 12px;
    margin: 0 4px;
    background: white;
    border-radius: 50%;
    animation: mokumoku 1.4s ease-in-out infinite;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.thinking-dot:nth-child(1) {
    animation-delay: 0s;
}

.thinking-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.thinking-dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* Mokumoku (fluffy) animations */
@keyframes mokumoku {
    0%, 60%, 100% {
        transform: scale(0.6) translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: scale(1.3) translateY(-10px);
        opacity: 1;
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0) scale(1);
    }
    50% {
        transform: translateY(-5px) scale(1.02);
    }
}

@keyframes bubble-tail {
    0%, 100% {
        transform: translate(0, 0) scale(1);
    }
    50% {
        transform: translate(-2px, -2px) scale(1.1);
    }
}

@keyframes bubble-tail-small {
    0%, 100% {
        transform: translate(0, 0) scale(1);
    }
    50% {
        transform: translate(-1px, -3px) scale(1.2);
    }
}

.message.thinking {
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* History Viewer Styles */
.history-viewer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.history-viewer.hidden {
    display: none;
}

.history-modal {
    width: 90%;
    max-width: 1000px;
    height: 85vh;
    background: white;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #ecf0f1;
    background: #f8f9fa;
}

.history-header h2 {
    margin: 0;
    font-size: 1.4em;
    color: #2c3e50;
}

.btn-close {
    background: none;
    border: none;
    font-size: 1.5em;
    cursor: pointer;
    color: #7f8c8d;
    transition: color 0.2s;
}

.btn-close:hover {
    color: #e74c3c;
}

.history-controls {
    display: flex;
    gap: 10px;
    padding: 15px 20px;
    border-bottom: 1px solid #ecf0f1;
}

.btn-secondary {
    background: #95a5a6;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 5px;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-secondary:hover {
    background: #7f8c8d;
}

.btn-danger {
    background: #e74c3c;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 5px;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-danger:hover {
    background: #c0392b;
}

/* Speech Recognition Display */
.speech-display {
    position: fixed;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(52, 152, 219, 0.95);
    color: white;
    padding: 15px 25px;
    border-radius: 25px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    min-width: 300px;
    max-width: 80vw;
    text-align: center;
    transition: all 0.3s ease;
    backdrop-filter: blur(10px);
}

.speech-display.hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
    pointer-events: none;
}

.speech-display.active {
    background: rgba(46, 204, 113, 0.95);
    transform: translateX(-50%) scale(1.02);
}

.speech-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 10px;
    font-size: 14px;
    font-weight: 500;
}

.listening-icon {
    font-size: 18px;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }
}

.interim-speech {
    font-size: 16px;
    font-weight: 600;
    min-height: 20px;
    line-height: 1.4;
    word-wrap: break-word;
    background: rgba(255, 255, 255, 0.1);
    padding: 8px 12px;
    border-radius: 12px;
    margin-top: 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.interim-speech:empty {
    display: none;
}

/* Voice Settings */
.voice-settings {
    padding: 20px;
    background: #f8f9fa;
    border-top: 1px solid #dee2e6;
    border-bottom: 1px solid #dee2e6;
}

.voice-settings h3 {
    margin: 0 0 15px 0;
    font-size: 1.1em;
    color: #2c3e50;
}

.voice-selector {
    margin-bottom: 15px;
}

.voice-selector label {
    display: inline-block;
    margin-right: 10px;
    font-weight: 500;
    color: #495057;
}

.voice-dropdown {
    padding: 8px 12px;
    border: 1px solid #ced4da;
    border-radius: 5px;
    background-color: white;
    font-size: 14px;
    min-width: 250px;
    cursor: pointer;
}

.voice-dropdown:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.voice-controls {
    display: flex;
    gap: 10px;
}

.voice-controls button {
    padding: 8px 16px;
    font-size: 14px;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    max-height: calc(85vh - 140px);
}

.date-group {
    margin-bottom: 25px;
}

.date-header {
    font-size: 0.9em;
    color: #7f8c8d;
    margin-bottom: 10px;
    padding-bottom: 5px;
    border-bottom: 1px solid #ecf0f1;
}

.conversation-item {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 10px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.conversation-item:hover {
    background: #e8ecef;
    transform: translateX(-5px);
}

.conversation-content {
    flex: 1;
    min-width: 0;
}

.btn-delete-item {
    background: none;
    border: none;
    font-size: 1.2em;
    cursor: pointer;
    padding: 5px 10px;
    opacity: 0.6;
    transition: all 0.2s;
    flex-shrink: 0;
}

.btn-delete-item:hover {
    opacity: 1;
    transform: scale(1.1);
    filter: drop-shadow(0 2px 4px rgba(231, 76, 60, 0.3));
}

.conversation-time {
    font-size: 0.8em;
    color: #95a5a6;
    margin-bottom: 5px;
}

.conversation-preview {
    color: #2c3e50;
    margin-bottom: 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.message-count {
    font-size: 0.8em;
    color: #95a5a6;
}

.has-audio {
    font-size: 1.2em;
}

.conversation-detail {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.conversation-detail.hidden {
    display: none;
}

.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #ecf0f1;
    background: #f8f9fa;
    gap: 10px;
    flex-shrink: 0;
}

.detail-header #play-audio {
    margin-left: auto;
}

.detail-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    max-height: calc(85vh - 180px);
}

.history-message {
    margin-bottom: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
}

.history-message.user-message {
    background: #e3f2fd;
    border-left: 3px solid #3498db;
}

.history-message.ai-message {
    background: #e8f5e9;
    border-left: 3px solid #27ae60;
}

.message-role {
    font-weight: bold;
    color: #666;
    font-size: 0.9em;
    margin-bottom: 8px;
}

.message-text {
    color: #2c3e50;
    line-height: 1.5;
    white-space: pre-wrap;
}

.message-time {
    font-size: 0.8em;
    color: #95a5a6;
    margin-top: 8px;
}

.detail-actions {
    padding: 15px 20px;
    border-top: 1px solid #ecf0f1;
    display: flex;
    gap: 10px;
    flex-shrink: 0;
}

.empty-state, .loading, .error {
    text-align: center;
    padding: 40px 20px;
    color: #95a5a6;
}

.error {
    color: #e74c3c;
}

/* History button for main UI */
.history-button {
    position: fixed;
    top: 20px;
    right: 20px;
    background: #3498db;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 25px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    transition: all 0.3s;
    z-index: 100;
}

.history-button:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

/* Responsive design */
@media (max-width: 768px) {
    .speech-display {
        bottom: 100px;
        min-width: 280px;
        max-width: 90vw;
        padding: 12px 20px;
        font-size: 14px;
    }
    
    .interim-speech {
        font-size: 15px;
        padding: 6px 10px;
    }
    
    .history-modal {
        width: 95%;
        height: 90vh;
    }
}

@media (max-width: 600px) {
    .container {
        padding: 10px;
    }
    
    h1 {
        font-size: 2em;
    }
    
    .chat-area {
        padding: 15px;
    }
    
    .text {
        max-width: 85%;
    }
    
    .btn-primary {
        padding: 12px 30px;
        font-size: 1em;
    }
    
    .loading-content {
        padding: 20px;
    }
    
    .translation-tooltip {
        max-width: 250px;
        font-size: 0.8em;
        left: 10px;
        transform: none;
    }
    
    .history-modal {
        width: 100%;
        height: 100vh;
        border-radius: 0;
    }
    
    .history-list {
        max-height: calc(100vh - 140px);
    }
    
    .detail-messages {
        max-height: calc(100vh - 180px);
    }
    
    .history-button {
        top: 10px;
        right: 10px;
        padding: 8px 16px;
        font-size: 0.9em;
    }
}