/* =====================================================================
   Premium Mobile-First Apple-Style Design System
   ===================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --vv-top: 0px;
    
    /* Світла тема Apple (HSL) */
    --bg-base: #f5f5f7;
    --bg-card: rgba(255, 255, 255, 0.75);
    --bg-card-opaque: #ffffff;
    --text-primary: #1d1d1f;
    --text-secondary: #86868b;
    --accent: #0071e3;
    --accent-hover: #0077ed;
    --border: rgba(0, 0, 0, 0.08);
    --border-focus: rgba(0, 113, 227, 0.4);
    --shadow-soft: 0 8px 30px rgba(0, 0, 0, 0.03), 0 1px 4px rgba(0, 0, 0, 0.01);
    --shadow-deep: 0 20px 40px rgba(0, 0, 0, 0.06);
    --glass-blur: blur(20px);
    
    /* Бульбашки повідомлень */
    --bubble-user: #0071e3;
    --bubble-user-text: #ffffff;
    --bubble-ai: rgba(0, 0, 0, 0.05);
    --bubble-ai-text: #1d1d1f;
    
    /* Інпути */
    --input-bg: rgba(0, 0, 0, 0.03);
    --input-placeholder: #86868b;
}

[data-theme="dark"] {
    /* Темна тема Apple (HSL) */
    --bg-base: #000000;
    --bg-card: rgba(22, 22, 23, 0.75);
    --bg-card-opaque: #161617;
    --text-primary: #f5f5f7;
    --text-secondary: #86868b;
    --accent: #0071e3;
    --accent-hover: #0a84ff;
    --border: rgba(255, 255, 255, 0.08);
    --border-focus: rgba(10, 132, 255, 0.5);
    --shadow-soft: 0 8px 30px rgba(0, 0, 0, 0.3), 0 1px 4px rgba(0, 0, 0, 0.2);
    --shadow-deep: 0 20px 40px rgba(0, 0, 0, 0.5);
    
    /* Бульбашки повідомлень */
    --bubble-user: #0071e3;
    --bubble-user-text: #ffffff;
    --bubble-ai: rgba(255, 255, 255, 0.08);
    --bubble-ai-text: #f5f5f7;
    
    /* Інпути */
    --input-bg: rgba(255, 255, 255, 0.06);
    --input-placeholder: #86868b;
}

/* =====================================================================
   Базові стилі та скидання
   ===================================================================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    width: 100%;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-base);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color 0.4s cubic-bezier(0.28, 0.11, 0.32, 1), color 0.4s cubic-bezier(0.28, 0.11, 0.32, 1);
    display: flex;
    justify-content: center;
    align-items: center;
}

/* =====================================================================
   Контейнери (Mobile-First адаптація)
   ===================================================================== */
.app-container {
    width: 100%;
    max-width: 640px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: var(--vv-height, 100dvh); /* Динамічна висота під мобільну клавіатуру та Visual Viewport */
    justify-content: center;
    min-height: 0;
}

.apple-card {
    background-color: var(--bg-card);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: var(--shadow-soft);
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    transition: box-shadow 0.3s ease;
    min-height: 0;
}

/* Екстремальна адаптація під мобільні екрани (смартфони) */
@media (max-width: 768px) {
    html, body {
        background-color: var(--bg-card-opaque);
        position: fixed;
        inset: 0;
        min-height: 100%;
        width: 100%;
    }

    body {
        align-items: stretch;
        justify-content: flex-start;
    }
    
    .app-container {
        position: fixed;
        top: var(--vv-top, 0px);
        left: 0;
        right: 0;
        padding: 0;
        height: var(--vv-height, 100dvh);
        max-width: none;
        transform: translateZ(0);
    }
    
    .apple-card {
        border-radius: 0;
        border: none;
        box-shadow: none;
        height: 100%;
    }

    body.keyboard-open .card-header {
        display: none;
    }
}

/* =====================================================================
   Шапка сторінки (Header)
   ===================================================================== */
.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--bg-card-opaque);
    z-index: 10;
    flex-shrink: 0;
}

.card-title {
    font-size: 16px;
    font-weight: 600;
    letter-spacing: -0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 55%;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.logout-btn {
    background: none;
    border: none;
    font-family: var(--font-sans);
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px 12px;
    border-radius: 10px;
    background-color: var(--input-bg);
    transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 32px;
}

.logout-btn:hover {
    color: #ff453a;
    background-color: rgba(255, 69, 58, 0.12);
}

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

.finish-btn {
    background-color: var(--accent) !important;
    color: #ffffff !important;
}

.finish-btn:hover {
    background-color: var(--accent-hover) !important;
    color: #ffffff !important;
}

.theme-toggle-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-primary);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--input-bg);
    transition: transform 0.2s ease, background-color 0.2s ease;
}

.theme-toggle-btn:hover {
    background-color: var(--border);
}

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

/* =====================================================================
   Форма спрощеного входу
   ===================================================================== */
.form-container {
    padding: 32px 24px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 100%;
    max-width: 420px;
    margin: 0 auto;
    width: 100%;
    text-align: center;
}

.form-header h1 {
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
}

.form-header p {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.4;
    margin-bottom: 28px;
}

.form-group {
    margin-bottom: 20px;
    text-align: left;
}

.form-label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 8px;
    padding-left: 4px;
}

/* Важливо: 16px запобігає примусовому масштабуванню в iOS */
.form-input {
    width: 100%;
    font-family: var(--font-sans);
    font-size: 16px;
    padding: 14px 16px;
    background-color: var(--input-bg);
    border: 1px solid transparent;
    border-radius: 14px;
    color: var(--text-primary);
    outline: none;
    transition: all 0.2s ease;
    height: 48px;
}

.form-input:focus {
    background-color: var(--bg-card-opaque);
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--border-focus);
}

.form-input::placeholder {
    color: var(--input-placeholder);
}

.apple-btn {
    width: 100%;
    font-family: var(--font-sans);
    font-size: 15px;
    font-weight: 600;
    padding: 14px;
    background-color: var(--accent);
    color: var(--bubble-user-text);
    border: none;
    border-radius: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 8px;
    height: 48px;
}

.apple-btn:hover {
    background-color: var(--accent-hover);
}

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

.apple-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* =====================================================================
   Інтерфейс чату (Чат-панель)
   ===================================================================== */
.chat-container {
    flex-direction: column;
    height: 100%;
    overflow: hidden; /* Запобігає вильоту форми за межі екрану */
    display: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    min-height: 0;
}

.chat-history {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background-color: var(--bg-card-opaque);
}

/* Кастомний скролбар */
.chat-history::-webkit-scrollbar {
    width: 5px;
}
.chat-history::-webkit-scrollbar-track {
    background: transparent;
}
.chat-history::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.12);
    border-radius: 10px;
}
[data-theme="dark"] .chat-history::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.12);
}

.chat-bubble-wrapper {
    display: flex;
    width: 100%;
    animation: bubbleAppear 0.25s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

.chat-bubble-wrapper.user {
    justify-content: flex-end;
}

.chat-bubble-wrapper.assistant {
    justify-content: flex-start;
    align-items: flex-end;
    gap: 8px;
}

.assistant-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex: 0 0 32px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.chat-bubble {
    max-width: 85%;
    padding: 12px 16px;
    font-size: 15px;
    line-height: 1.45;
    border-radius: 18px;
    word-break: break-word;
}

.chat-bubble-wrapper.user .chat-bubble {
    background-color: var(--bubble-user);
    color: var(--bubble-user-text);
    border-bottom-right-radius: 4px;
}

.chat-bubble-wrapper.assistant .chat-bubble {
    max-width: calc(85% - 40px);
    background-color: var(--bubble-ai);
    color: var(--bubble-ai-text);
    border-bottom-left-radius: 4px;
}

.chat-system-message {
    align-self: center;
    max-width: 86%;
    color: var(--text-secondary);
    background-color: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 7px 12px;
    font-size: 12px;
    line-height: 1.3;
    text-align: center;
    animation: bubbleAppear 0.25s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

.chat-bubble p {
    margin-bottom: 8px;
}
.chat-bubble p:last-child {
    margin-bottom: 0;
}
.chat-bubble ul, .chat-bubble ol {
    margin-left: 20px;
    margin-bottom: 8px;
}

/* Нижнє поле введення повідомлення */
.chat-input-area {
    padding: 16px 20px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom)); /* Адаптація під "чубчики" смартфонів */
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    background-color: var(--bg-card-opaque);
    flex-shrink: 0; /* Гарантує, що форма завжди буде закріплена знизу */
    width: 100%;
}

body.keyboard-open .chat-input-area {
    padding-top: 8px;
    padding-bottom: 8px;
}

.chat-input-wrapper {
    flex: 1;
    min-width: 0;
    width: 100%;
    position: relative;
    display: flex;
    align-items: flex-end;
}

/* Важливо: 16px запобігає авто-зуму при дотику */
.chat-input {
    width: 100%;
    font-family: var(--font-sans);
    font-size: 16px;
    padding: 12px 48px 12px 16px;
    background-color: var(--input-bg);
    border: 1px solid transparent;
    border-radius: 20px;
    color: var(--text-primary);
    outline: none;
    transition: all 0.2s ease;
    resize: none;
    min-height: 44px;
    height: 44px;
    max-height: 120px;
    line-height: 1.3;
    overflow-y: auto;
    display: block;
}

.chat-input:focus {
    background-color: var(--bg-base);
    border-color: var(--accent);
}

.chat-send-btn {
    position: absolute;
    right: 5px;
    bottom: 5px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background-color: var(--accent);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    transition: all 0.2s ease;
}

.chat-send-btn:hover {
    background-color: var(--accent-hover);
}

.chat-send-btn:active {
    transform: scale(0.9);
}

.chat-send-btn svg {
    width: 16px;
    height: 16px;
    transform: rotate(45deg);
    margin-right: 2px;
    margin-top: -2px;
}

@media (max-width: 480px) {
    .card-header {
        padding: 12px 14px;
        gap: 8px;
    }

    .card-title {
        max-width: 48%;
        font-size: 15px;
    }

    .header-actions {
        gap: 6px;
    }

    .logout-btn {
        padding: 6px 9px;
        max-width: 96px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .theme-toggle-btn {
        width: 34px;
        height: 34px;
    }

    .chat-history {
        padding: 14px 12px;
        gap: 10px;
    }

    .chat-bubble {
        max-width: 90%;
        font-size: 14px;
        padding: 10px 13px;
    }

    .assistant-avatar {
        width: 28px;
        height: 28px;
        flex-basis: 28px;
    }

    .chat-bubble-wrapper.assistant .chat-bubble {
        max-width: calc(90% - 36px);
    }

    .chat-input-area {
        padding: 10px 10px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
        align-items: flex-end;
    }

    body.keyboard-open .chat-input-area {
        padding-top: 8px;
        padding-bottom: 8px;
    }

    .chat-input {
        border-radius: 18px;
        padding: 11px 46px 11px 14px;
        min-height: 42px;
        height: 42px;
        max-height: 104px;
    }

    .chat-send-btn {
        right: 4px;
        bottom: 4px;
        width: 34px;
        height: 34px;
    }
}

/* =====================================================================
   Анімації (Animations & Loaders)
   ===================================================================== */
@keyframes bubbleAppear {
    from {
        opacity: 0;
        transform: translateY(8px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Ефект друку */
.typing-indicator {
    display: flex;
    gap: 4px;
    align-items: center;
    height: 18px;
}

.typing-dot {
    width: 5px;
    height: 5px;
    background-color: var(--text-secondary);
    border-radius: 50%;
    opacity: 0.4;
    animation: typingBounce 1.4s infinite ease-in-out;
}

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

@keyframes typingBounce {
    0%, 100%, 80% { transform: translateY(0); opacity: 0.4; }
    40% { transform: translateY(-5px); opacity: 1; }
}

/* Помилка */
.alert-error {
    background-color: rgba(255, 69, 58, 0.1);
    border: 1px solid rgba(255, 69, 58, 0.2);
    color: #ff453a;
    padding: 12px 16px;
    border-radius: 12px;
    margin-bottom: 20px;
    font-size: 14px;
    text-align: left;
    display: none;
    line-height: 1.4;
    animation: bubbleAppear 0.3s ease;
}
