:root {
    --primary: #4F46E5;
    --primary-light: #818CF8;
    --primary-dark: #3730A3;
    --secondary: #10B981;
    --danger: #EF4444;
    --warning: #F59E0B;
    --bg: #0F172A;
    --bg-card: #1E293B;
    --bg-input: #334155;
    --text: #F1F5F9;
    --text-muted: #94A3B8;
    --border: #475569;
    --radius: 16px;
    --radius-sm: 10px;
    --shadow: 0 4px 24px rgba(0,0,0,0.3);
}

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

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

body {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

/* Screens */
.screen {
    display: none;
    flex-direction: column;
    flex: 1;
    padding: 16px;
    max-width: 480px;
    margin: 0 auto;
    width: 100%;
    animation: fadeIn 0.3s ease;
}

.screen.active {
    display: flex;
}

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

/* Logo & Header */
.logo {
    text-align: center;
    padding: 40px 0 20px;
}

.logo h1 {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary-light), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -0.5px;
}

.logo p {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-top: 4px;
}

/* Buttons */
.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    padding: 18px 24px;
    border: none;
    border-radius: var(--radius);
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 60px;
    touch-action: manipulation;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    box-shadow: 0 4px 16px rgba(79, 70, 229, 0.4);
}

.btn-primary:hover {
    box-shadow: 0 6px 24px rgba(79, 70, 229, 0.5);
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text);
    border: 1.5px solid var(--border);
}

.btn-secondary:hover {
    border-color: var(--primary-light);
}

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

.btn-success {
    background: var(--secondary);
    color: white;
}

.btn-sm {
    padding: 12px 16px;
    font-size: 0.95rem;
    min-height: 44px;
}

.btn-icon {
    font-size: 1.3rem;
}

/* Cards */
.card {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}

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

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

select, input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-input);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
    -webkit-appearance: none;
    appearance: none;
}

select:focus, input:focus {
    border-color: var(--primary-light);
}

/* Action Group */
.action-group {
    display: flex;
    flex-direction: column;
    gap: 14px;
    margin-top: 24px;
}

/* Spacer */
.spacer {
    flex: 1;
}

/* Divider */
.divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 20px 0;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.divider::before, .divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* Short Code Input */
.code-input-group {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.code-input-group input {
    width: 48px;
    height: 56px;
    text-align: center;
    font-size: 1.5rem;
    font-weight: 700;
    padding: 0;
    border-radius: var(--radius-sm);
}

/* QR Code Display */
.qr-container {
    text-align: center;
    padding: 20px;
}

.qr-container img {
    width: 180px;
    height: 180px;
    border-radius: 12px;
    background: white;
    padding: 8px;
}

.short-code-display {
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: 8px;
    text-align: center;
    color: var(--primary-light);
    padding: 16px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    margin: 12px 0;
    font-variant-numeric: tabular-nums;
}

/* Room Screen — fixed layout: header + scrollable chat + sticky controls */
#screen-room {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
}

.room-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0 16px;
}

.room-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

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

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

.status-dot.connected {
    background: var(--secondary);
    animation: pulse 2s infinite;
}

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

/* Conversation Area */
.conversation-area {
    flex: 1;
    min-height: 0; /* critical for flex overflow scrolling */
    overflow-y: auto;
    padding: 8px 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.message-bubble {
    max-width: 90%;
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 1rem;
    line-height: 1.5;
    animation: slideUp 0.2s ease;
    word-wrap: break-word;
}

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

.message-bubble.mine {
    align-self: flex-end;
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-bubble.peer {
    align-self: flex-start;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message-bubble .original {
    font-size: 0.8rem;
    color: rgba(255,255,255,0.6);
    margin-top: 4px;
}

.message-bubble.peer .original {
    color: var(--text-muted);
}

.message-bubble.interim {
    opacity: 0.6;
}

/* Controls Bar */
.controls-bar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 16px 0;
    border-top: 1px solid var(--border);
    background: var(--bg);
}

.control-btn {
    width: 56px;
    height: 56px;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1.4rem;
    touch-action: manipulation;
}

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

.control-btn.mic {
    background: var(--secondary);
    color: white;
    width: 68px;
    height: 68px;
    font-size: 1.6rem;
    box-shadow: 0 4px 16px rgba(16, 185, 129, 0.4);
}

.control-btn.mic.muted {
    background: var(--danger);
    box-shadow: 0 4px 16px rgba(239, 68, 68, 0.4);
}

.control-btn.mic.listening {
    animation: micPulse 1.5s infinite;
}

@keyframes micPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); }
    50% { box-shadow: 0 0 0 16px rgba(16, 185, 129, 0); }
}

.control-btn.tts {
    background: var(--bg-card);
    color: var(--primary-light);
    border: 1.5px solid var(--border);
}

.control-btn.tts.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.control-btn.end {
    background: var(--danger);
    color: white;
}

/* Speaking Indicator */
.speaking-indicator {
    flex-shrink: 0;
    display: none;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(79, 70, 229, 0.15);
    border-radius: 20px;
    font-size: 0.85rem;
    color: var(--primary-light);
    align-self: center;
}

.speaking-indicator.active {
    display: flex;
}

.speaking-dots {
    display: flex;
    gap: 3px;
}

.speaking-dots span {
    width: 6px;
    height: 6px;
    background: var(--primary-light);
    border-radius: 50%;
    animation: dot 1.4s infinite ease-in-out both;
}

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

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

/* QR Scanner */
.scanner-container {
    position: relative;
    width: 100%;
    max-width: 300px;
    margin: 0 auto;
    border-radius: var(--radius);
    overflow: hidden;
    background: #000;
}

.scanner-container video {
    width: 100%;
    display: block;
}

.scanner-overlay {
    position: absolute;
    inset: 0;
    border: 3px solid var(--primary-light);
    border-radius: var(--radius);
    pointer-events: none;
}

.scanner-line {
    position: absolute;
    left: 10%;
    right: 10%;
    height: 2px;
    background: var(--primary-light);
    box-shadow: 0 0 8px var(--primary-light);
    animation: scanLine 2s linear infinite;
}

@keyframes scanLine {
    0% { top: 10%; }
    100% { top: 90%; }
}

/* Toast */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--bg-card);
    color: var(--text);
    padding: 12px 24px;
    border-radius: 12px;
    font-size: 0.9rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    z-index: 1000;
    transition: transform 0.3s ease;
    white-space: nowrap;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
}

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

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

.modal-content {
    background: var(--bg-card);
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 24px;
    width: 100%;
    max-width: 480px;
    max-height: 80vh;
    overflow-y: auto;
    animation: slideUpModal 0.3s ease;
}

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

/* Language badge */
.lang-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background: var(--bg-input);
    border-radius: 20px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* Waiting state */
.waiting-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 24px;
    text-align: center;
}

.waiting-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--primary-light);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Trial info */
.trial-info {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    background: rgba(245, 158, 11, 0.1);
    border-radius: 20px;
    font-size: 0.8rem;
    color: var(--warning);
    margin-top: 12px;
}

/* Back button */
.back-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
    padding: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* UI Language Switcher */
.ui-lang-row {
    display: flex;
    justify-content: center;
    margin-top: -8px;
    margin-bottom: 8px;
}

.ui-lang-row select {
    background: var(--bg-input);
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    font-size: 0.8rem;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394A3B8' viewBox='0 0 16 16'%3E%3Cpath d='M1.5 5.5l6.5 6.5 6.5-6.5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    padding-right: 28px;
}

/* Room language change selector */
.lang-badge-btn {
    background: none;
    border: 1px solid transparent;
    color: var(--text);
    font-size: 0.75rem;
    padding: 4px 10px;
    border-radius: 12px;
    background: rgba(79, 70, 229, 0.15);
    cursor: pointer;
    transition: border-color 0.2s;
}

.lang-badge-btn:hover,
.lang-badge-btn:focus {
    border-color: var(--primary-light);
}

.lang-change-modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    animation: fadeIn 0.2s ease;
}

.lang-change-card {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 24px;
    width: 90%;
    max-width: 360px;
    box-shadow: var(--shadow);
}

.lang-change-card h3 {
    margin-bottom: 12px;
    font-size: 1.1rem;
}

.lang-change-card select {
    width: 100%;
    padding: 12px;
    background: var(--bg-input);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    margin-bottom: 16px;
}

.lang-change-card .btn-row {
    display: flex;
    gap: 8px;
}

.lang-change-card .btn-row button {
    flex: 1;
    padding: 10px;
    border-radius: var(--radius-sm);
    border: none;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
}

.lang-change-card .btn-cancel {
    background: var(--bg-input);
    color: var(--text-muted);
}

.lang-change-card .btn-confirm {
    background: var(--primary);
    color: white;
}

/* Hide on desktop wide, center content */
@media (min-width: 481px) {
    .screen {
        padding: 24px;
    }
}
