* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    background: #fcfcf4;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.container {
    max-width: 700px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: 20px;
    padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}

header {
    text-align: center;
    color: #141413;
    margin-bottom: 20px;
}

header h1 {
    font-size: 2.5rem;
    margin-bottom: 5px;
}

.subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
}

.chat-container {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    min-height: 100px;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    animation: slideIn 0.3s ease-out;
    margin-left: 10px;
    margin-right: 10px;
}

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

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

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

.message-content {
    padding: 12px 18px;
    border-radius: 18px;
    line-height: 1.5;
    word-wrap: break-word;
    position: relative;
}

.message.user .message-content {
    background: #f5f5f5;
    color: #141413;
}

/* Speech bubble tail pointing right for user messages */
.message.user .message-content::after {
    content: '';
    position: absolute;
    right: -8px;
    top: 15px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 10px 0 10px 12px;
    border-color: transparent transparent transparent #f5f5f5;
}

.message.assistant .message-content {
    background: #f5f5f5;
    color: #D81B60;
}

/* Speech bubble tail pointing left for assistant messages */
.message.assistant .message-content::before {
    content: '';
    position: absolute;
    left: -8px;
    top: 15px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 10px 12px 10px 0;
    border-color: transparent #f5f5f5 transparent transparent;
}

.message-content p {
    margin: 0 0 8px;
}

.message-content ul,
.message-content ol {
    margin: 0 0 10px 18px;
    padding-left: 18px;
}

.message-content li {
    margin: 4px 0;
    line-height: 1.5;
}

.message-content a {
    color: #1565c0;
    text-decoration: underline;
}

.message-content code {
    background: #ececec;
    padding: 2px 6px;
    border-radius: 6px;
    font-family: 'SFMono-Regular', Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
    font-size: 0.95em;
}

.message-content pre {
    background: #111;
    color: #f5f5f5;
    padding: 10px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 0 0 12px;
    font-family: 'SFMono-Regular', Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
    font-size: 0.95em;
}

.message-content pre code {
    background: transparent;
    padding: 0;
}

.message.image .message-content {
    padding: 12px;
}

.image-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 10px;
}

.image-grid img {
    width: 100%;
    height: auto;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.image-grid img:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.input-container {
    border-top: 1px solid #e0e0e0;
    padding: 20px;
    background: #fafafa;
    flex: 0 0 auto;
}

.input-wrapper {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

#userInput {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 25px;
    font-size: 15px;
    font-family: inherit;
    resize: none;
    outline: none;
    transition: border-color 0.3s;
    max-height: 120px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

#userInput:focus {
    border-color: #141413;
    -webkit-user-select: text;
    user-select: text;
}

.button-group {
    display: flex;
    gap: 10px;
}

.mic-button, .voice-chat-button, .send-button {
    padding: 12px 20px;
    border: none;
    border-radius: 25px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    outline: none;
}

.mic-button {
    background: #f1f3f4;
    color: #333;
    width: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.mic-button:hover {
    background: #e0e0e0;
}

.mic-button.recording {
    background: #ef4444;
    color: white;
    animation: pulse 1.5s infinite;
}

/* Voice Chat Button */
.voice-chat-button {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    width: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.voice-chat-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}

.voice-chat-button.listening {
    background: #3b82f6;
    animation: pulse-slow 2s ease-in-out infinite;
}

.voice-chat-button.speaking {
    background: #ef4444;
    animation: pulse-fast 0.8s ease-in-out infinite;
}

.voice-chat-button.processing {
    background: #f59e0b;
    animation: spin 1s linear infinite;
}

.voice-chat-button.ai-speaking {
    background: #10b981;
    animation: wave 1.5s ease-in-out infinite;
}

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

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

@keyframes pulse-fast {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.6;
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

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

.send-button {
    background: #141413;
    color: white;
    width: 50px;
    padding: 12px;
}

.send-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(20, 20, 19, 0.2);
}

.send-button:active {
    transform: translateY(0);
}

.send-button:disabled, .mic-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.button-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 15px;
    flex-wrap: wrap;
}

.settings-toggle, .clear-button, .replay-button, .stop-audio-button, .download-audio-button, .insert-image-button, .auto-play-button {
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid #e0e0e0;
    border-radius: 12px;
    background: white;
    color: #666;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    outline: none;
}

.settings-toggle:hover, .clear-button:hover, .replay-button:hover, .download-audio-button:hover, .insert-image-button:hover, .auto-play-button:hover {
    background: #f5f5f5;
    border-color: #141413;
    color: #141413;
    transform: translateY(-1px);
}


.settings-toggle.active {
    background: #141413;
    border-color: #141413;
    color: white;
}

.stop-audio-button {
    background: #ef4444 !important;
    border-color: #ef4444 !important;
    color: white !important;
}

.stop-audio-button:hover {
    background: #dc2626 !important;
    border-color: #dc2626 !important;
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(239, 68, 68, 0.3);
}

/* Settings Panel */
.settings-panel {
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transition: max-height 0.4s ease, opacity 0.3s ease, padding 0.3s ease;
    background: white;
    border-radius: 12px;
}

.settings-panel.show {
    max-height: 500px;
    opacity: 1;
    padding: 0;
    overflow-y: auto;
    border: 1px solid #e0e0e0;
    margin-top: 8px;
}

/* Tabs Navigation */
.tabs-navigation {
    display: flex;
    gap: 0;
    border-bottom: 2px solid #e0e0e0;
    background: #fafafa;
    border-radius: 12px 12px 0 0;
}

.tab-button {
    flex: 1;
    padding: 12px 16px;
    border: none;
    background: transparent;
    color: #666;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}

.tab-button:first-child {
    border-radius: 12px 0 0 0;
}

.tab-button:last-child {
    border-radius: 0 12px 0 0;
}

.tab-button:hover {
    background: #f0f0f0;
    color: #141413;
}

.tab-button.active {
    background: white;
    color: #141413;
}

.tab-button.active::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    right: 0;
    height: 2px;
    background: #141413;
}

/* Tab Content */
.tab-content {
    display: none;
    padding: 20px;
    gap: 16px;
    flex-direction: column;
}

.tab-content.active {
    display: flex;
}

/* Setting Items */
.setting-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.setting-item.full-width {
    width: 100%;
}

.setting-item.checkbox-item {
    flex-direction: row;
    align-items: center;
}

.setting-label {
    font-size: 13px;
    font-weight: 600;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.setting-select {
    padding: 10px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    cursor: pointer;
    background: white;
    color: #141413;
    transition: border-color 0.2s;
}

.setting-select:hover {
    border-color: #141413;
}

.setting-select:focus {
    border-color: #141413;
}

.setting-textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;
    outline: none;
    transition: border-color 0.2s;
    line-height: 1.5;
}

.setting-textarea:focus {
    border-color: #141413;
}

.reset-button {
    align-self: flex-start;
    padding: 8px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    background: white;
    color: #666;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.reset-button:hover {
    background: #f5f5f5;
    border-color: #141413;
    color: #141413;
}

/* Control Buttons */
.control-buttons {
    display: flex;
    align-items: center;
    gap: 8px;
    justify-content: flex-start;
}

.control-btn {
    width: 36px;
    height: 36px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    background: white;
    color: #141413;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.control-btn:hover {
    border-color: #141413;
    background: #f5f5f5;
}

.control-btn:active {
    transform: scale(0.95);
}

.control-value {
    min-width: 50px;
    text-align: center;
    font-weight: 600;
    color: #141413;
    font-size: 14px;
}

/* Checkbox */
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #141413;
    cursor: pointer;
    font-size: 14px;
}

.checkbox-label input[type="checkbox"] {
    cursor: pointer;
    width: 18px;
    height: 18px;
}

/* Provider Controls */
.provider-controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.status-indicator {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 20px;
    border-radius: 25px;
    background: #333;
    color: white;
    font-size: 14px;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
    z-index: 1000;
}

.status-indicator.show {
    opacity: 1;
}

.status-indicator.error {
    background: #ef4444;
}

.status-indicator.success {
    background: #10b981;
}

.status-indicator.info {
    background: #3b82f6;
}

/* Scrollbar styling */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Loading animation */
.message.loading .message-content::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #666;
    animation: loading 1.4s infinite;
    margin-left: 4px;
}

@keyframes loading {
    0%, 80%, 100% {
        opacity: 0;
    }
    40% {
        opacity: 1;
    }
}

/* Responsive design */
@media (max-width: 768px) {
    .container {
        padding: 10px;
        padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    }

    header h1 {
        font-size: 2rem;
    }

    .chat-container {
        border-radius: 8px;
    }

    .chat-messages {
        padding: 15px 10px;
        gap: 12px;
    }

    .message {
        max-width: 85%;
        margin-left: 5px;
        margin-right: 5px;
    }

    .message-content {
        padding: 10px 14px;
        font-size: 15px;
    }

    .input-container {
        padding: 12px;
    }

    .input-wrapper {
        flex-direction: row;
        gap: 8px;
        margin-bottom: 12px;
    }

    #userInput {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 10px 14px;
        min-height: 44px; /* Minimum touch target size */
    }

    .button-group {
        display: flex;
        gap: 8px;
        flex-shrink: 0;
    }

    .send-button, .mic-button, .voice-chat-button {
        min-height: 44px; /* Minimum touch target size */
    }

    .mic-button {
        width: 44px;
        min-width: 44px;
        flex-shrink: 0;
        padding: 10px;
    }

    .voice-chat-button {
        width: 44px;
        min-width: 44px;
        flex-shrink: 0;
        padding: 10px;
    }

    .send-button {
        width: 44px;
        min-width: 44px;
        flex-shrink: 0;
        padding: 10px;
    }

    .button-controls {
        gap: 6px;
        margin-bottom: 10px;
    }

    .settings-toggle,
    .clear-button,
    .replay-button,
    .stop-audio-button,
    .download-audio-button,
    .insert-image-button,
    .auto-play-button {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px; /* Minimum touch target size */
        flex: 0 0 auto;
    }

    .settings-toggle svg,
    .clear-button svg,
    .replay-button svg,
    .stop-audio-button svg,
    .download-audio-button svg,
    .insert-image-button svg,
    .auto-play-button svg {
        width: 20px;
        height: 20px;
    }

    .settings-panel.show {
        max-height: 600px;
    }

    .tab-button {
        padding: 14px 12px;
        font-size: 13px;
        min-height: 48px;
    }

    .tab-content {
        padding: 16px;
        gap: 14px;
    }

    .setting-label {
        font-size: 12px;
    }

    .setting-select {
        width: 100%;
        padding: 12px 14px;
        font-size: 16px; /* Prevent zoom on iOS */
        min-height: 44px;
    }

    .setting-textarea {
        font-size: 16px; /* Prevent zoom on iOS */
        min-height: 100px;
        padding: 12px 14px;
    }

    .control-btn {
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
        font-size: 20px;
    }

    .control-value {
        min-width: 60px;
        font-size: 15px;
    }

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

    .checkbox-label {
        font-size: 15px;
    }

    .checkbox-label input[type="checkbox"] {
        width: 20px;
        height: 20px;
    }

    .status-indicator {
        top: 10px;
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        text-align: center;
        font-size: 13px;
        padding: 10px 16px;
    }
}

/* Landscape mode on mobile */
@media (max-width: 768px) and (orientation: landscape) {
    .container {
        padding: 5px;
    }

    .chat-messages {
        padding: 10px;
    }

    .input-container {
        padding: 8px;
    }

    .settings-panel.show {
        max-height: 350px;
    }

    .tab-content {
        padding: 12px;
    }
}
