/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   Portal TRC — Rêve Cosmique Theme
   Deep cosmic dark theme with animated starfield
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */

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

:root {
    --bg-primary: #06060f;
    --bg-secondary: #0c0c1d;
    --bg-chat: rgba(12, 12, 29, 0.6);
    --bg-bubble-user: rgba(107, 33, 168, 0.25);
    --bg-bubble-ai: rgba(15, 15, 35, 0.7);
    --bg-input: rgba(20, 20, 45, 0.8);
    --border-subtle: rgba(139, 92, 246, 0.15);
    --border-glow: rgba(139, 92, 246, 0.3);
    --accent-gold: #d4a574;
    --accent-purple: #8b5cf6;
    --accent-deep-purple: #6b21a8;
    --accent-teal: #0d9488;
    --accent-amber: #f59e0b;
    --text-primary: #e8e8f0;
    --text-secondary: #a0a0b8;
    --text-muted: #6b6b82;
    --glass-bg: rgba(255, 255, 255, 0.03);
    --glass-border: rgba(255, 255, 255, 0.06);
    --shadow-glow: 0 0 40px rgba(139, 92, 246, 0.08);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-xl: 24px;
    --font-body: 'Inter', system-ui, -apple-system, sans-serif;
    --font-display: 'Playfair Display', Georgia, serif;
    --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

html, body {
    height: 100%;
    overflow: hidden;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* ── Starfield ────────────────────────────────────────────── */
.starfield {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}

.star {
    position: absolute;
    width: 2px;
    height: 2px;
    background: white;
    border-radius: 50%;
    animation: twinkle var(--duration) ease-in-out infinite;
    opacity: 0;
}

@keyframes twinkle {
    0%, 100% { opacity: 0; transform: scale(0.5); }
    50% { opacity: var(--max-opacity); transform: scale(1); }
}

/* Nebula gradient overlay */
.starfield::after {
    content: '';
    position: absolute;
    inset: 0;
    background: 
        radial-gradient(ellipse at 20% 50%, rgba(107, 33, 168, 0.06) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 20%, rgba(13, 148, 136, 0.04) 0%, transparent 50%),
        radial-gradient(ellipse at 50% 80%, rgba(212, 165, 116, 0.03) 0%, transparent 40%);
    pointer-events: none;
}

/* ── App Container ────────────────────────────────────────── */
.app-container {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    height: 100dvh; /* Use dynamic viewport height to prevent mobile clipping */
    max-width: 800px;
    margin: 0 auto;
    padding: 0 16px;
}

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

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

.header-icon {
    font-size: 24px;
    filter: drop-shadow(0 0 8px rgba(139, 92, 246, 0.4));
}

.header-title {
    font-family: var(--font-display);
    font-size: 22px;
    font-weight: 500;
    font-style: italic;
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-subtitle {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    padding-left: 10px;
    border-left: 1px solid var(--border-subtle);
}

.lang-toggle {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-secondary);
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 1px;
    transition: var(--transition);
}

.lang-toggle:hover {
    border-color: var(--accent-purple);
    color: var(--text-primary);
    box-shadow: 0 0 12px rgba(139, 92, 246, 0.15);
}

/* ── Chat Area ────────────────────────────────────────────── */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: rgba(139, 92, 246, 0.5) transparent;
}

.chat-area::-webkit-scrollbar {
    width: 6px; /* Slightly wider for usability */
}
.chat-area::-webkit-scrollbar-track {
    background: transparent;
}
.chat-area::-webkit-scrollbar-thumb {
    background: rgba(139, 92, 246, 0.5); /* Higher opacity for visibility */
    border-radius: 3px;
}

/* ── Welcome Screen ───────────────────────────────────────── */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
    text-align: center;
    padding: 20px;
    position: relative;
}

.welcome-glow {
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(139, 92, 246, 0.08), transparent 70%);
    filter: blur(40px);
    pointer-events: none;
}

.welcome-title {
    font-family: var(--font-display);
    font-size: 32px;
    font-weight: 500;
    font-style: italic;
    background: linear-gradient(135deg, var(--text-primary), var(--accent-gold));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 12px;
    position: relative;
}

.welcome-desc {
    color: var(--text-secondary);
    max-width: 480px;
    font-size: 14px;
    line-height: 1.7;
    margin-bottom: 36px;
    position: relative;
}

.starter-prompts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    max-width: 520px;
    width: 100%;
    position: relative;
}

.starter-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-secondary);
    padding: 14px 16px;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 13px;
    text-align: left;
    transition: var(--transition);
    line-height: 1.4;
}

.starter-btn:hover {
    background: rgba(139, 92, 246, 0.08);
    border-color: var(--border-glow);
    color: var(--text-primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow-glow);
}

.starter-icon {
    font-size: 18px;
    flex-shrink: 0;
}

/* ── Messages ─────────────────────────────────────────────── */
.messages {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-bottom: 30px; /* Give the last message some breathing room above the input box */
}

.message {
    display: flex;
    flex-direction: column;
    gap: 4px;
    animation: messageIn 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

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

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

.message-ai {
    align-items: flex-start;
}

.message-bubble {
    max-width: 85%;
    max-height: 65dvh;
    overflow-y: auto;
    padding: 14px 18px;
    border-radius: var(--radius-lg);
    font-size: 14px;
    line-height: 1.7;
    word-wrap: break-word;
    overflow-wrap: break-word;
    scrollbar-width: thin;
    scrollbar-color: rgba(139, 92, 246, 0.5) transparent;
}

.message-bubble::-webkit-scrollbar {
    width: 6px;
}
.message-bubble::-webkit-scrollbar-track {
    background: transparent;
}
.message-bubble::-webkit-scrollbar-thumb {
    background: rgba(139, 92, 246, 0.5);
    border-radius: 3px;
}

.message-user .message-bubble {
    background: var(--bg-bubble-user);
    border: 1px solid rgba(139, 92, 246, 0.2);
    border-bottom-right-radius: 4px;
    color: var(--text-primary);
}

.message-ai .message-bubble {
    background: var(--bg-bubble-ai);
    border: 1px solid var(--glass-border);
    border-bottom-left-radius: 4px;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #e2e8f0;
}

.message-bubble p {
    margin: 0 0 14px 0;
    line-height: 1.8;
}

.message-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble strong {
    color: #fcd34d;
    font-weight: 600;
    text-shadow: 0 0 12px rgba(251, 191, 36, 0.25);
}

.message-bubble em {
    color: #c4b5fd;
    font-style: italic;
}

.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
    font-family: var(--font-display);
    color: #ffffff;
    margin: 18px 0 10px 0;
    font-weight: 500;
    font-style: italic;
    letter-spacing: 0.02em;
}

.message-bubble ul,
.message-bubble ol {
    margin: 10px 0 16px 22px;
    padding: 0;
}

.message-bubble li {
    margin-bottom: 8px;
    line-height: 1.7;
    color: #e2e8f0;
}

.message-bubble li::marker {
    color: var(--accent-purple);
}

.message-bubble blockquote {
    border-left: 3px solid var(--accent-purple);
    margin: 14px 0;
    padding: 10px 16px;
    background: rgba(139, 92, 246, 0.1);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    font-style: italic;
    color: #cbd5e1;
}

.message-bubble hr {
    border: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(139, 92, 246, 0.35), transparent);
    margin: 18px 0;
}

/* Layer mode badges */
.mode-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    padding: 3px 10px;
    border-radius: 20px;
    margin-top: 6px;
}

.mode-badge.layer-3 {
    background: rgba(107, 33, 168, 0.2);
    color: var(--accent-purple);
    border: 1px solid rgba(139, 92, 246, 0.3);
    box-shadow: 0 0 12px rgba(139, 92, 246, 0.1);
}

.mode-badge.layer-3-partial {
    background: rgba(107, 33, 168, 0.15);
    color: var(--accent-purple);
    border: 1px solid var(--accent-amber);
}

.mode-badge.layer-2-fallback {
    background: rgba(13, 148, 136, 0.15);
    color: var(--accent-teal);
    border: 1px solid rgba(13, 148, 136, 0.3);
}

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 14px 18px;
    background: var(--bg-bubble-ai);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 4px;
    backdrop-filter: blur(12px);
    max-width: 80px;
}

.typing-dot {
    width: 6px;
    height: 6px;
    background: var(--accent-purple);
    border-radius: 50%;
    animation: typingBounce 1.4s ease-in-out infinite;
}

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

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

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

.input-container {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    background: var(--bg-input);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: 8px 8px 8px 18px;
    transition: var(--transition);
}

.input-container:focus-within {
    border-color: var(--accent-purple);
    box-shadow: 0 0 20px rgba(139, 92, 246, 0.1);
}

.chat-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 14px;
    line-height: 1.5;
    resize: none;
    max-height: 120px;
    padding: 6px 0;
}

.chat-input::placeholder {
    color: var(--text-muted);
}

.send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: none;
    background: var(--accent-deep-purple);
    color: white;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-purple);
    box-shadow: 0 0 16px rgba(139, 92, 246, 0.3);
    transform: scale(1.05);
}

.send-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.input-hint {
    text-align: center;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 8px;
    letter-spacing: 0.3px;
}

/* ── Footer ───────────────────────────────────────────────── */
.app-footer {
    flex-shrink: 0;
    text-align: center;
    padding: 8px 0 12px;
    font-size: 11px;
    color: var(--text-muted);
}

.app-footer a {
    color: var(--accent-gold);
    text-decoration: none;
    opacity: 0.7;
    transition: var(--transition);
}

.app-footer a:hover {
    opacity: 1;
    text-decoration: underline;
}

/* ── Rate limit message ───────────────────────────────────── */
.rate-limit-msg {
    text-align: center;
    padding: 12px 20px;
    background: rgba(245, 158, 11, 0.1);
    border: 1px solid rgba(245, 158, 11, 0.2);
    border-radius: var(--radius-md);
    color: var(--accent-amber);
    font-size: 13px;
    animation: messageIn 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 600px) {
    .app-container {
        padding: 0 12px;
    }

    .header-subtitle {
        display: none;
    }

    .header-title {
        font-size: 18px;
    }

    .welcome-title {
        font-size: 24px;
    }

    .starter-prompts {
        grid-template-columns: 1fr;
    }

    .message-bubble {
        max-width: 92%;
    }
}

/* ── Hidden state ─────────────────────────────────────────── */
.hidden {
    display: none !important;
}
