/* ============================================
   CSS Variables & Reset
   ============================================ */
:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-chat: #121220;
    --bg-user-msg: linear-gradient(135deg, #e9407a, #b82a6e);
    --bg-ai-msg: #232342;
    --text-primary: #f0f0f5;
    --text-secondary: #9898b8;
    --text-muted: #6b6b8d;
    --accent-primary: #e9407a;
    --accent-secondary: #b82a6e;
    --accent-glow: rgba(233, 64, 122, 0.3);
    --border-color: #2a2a4a;
    --shadow-color: rgba(0, 0, 0, 0.4);
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --transition-fast: 0.2s ease;
    --transition-normal: 0.3s ease;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    align-items: center;
}

/* ============================================
   App Container
   ============================================ */
.app-container {
    width: 100%;
    max-width: 480px;
    height: 100vh;   /* fallback for older browsers */
    height: 100dvh;
    max-height: 900px;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: 0 20px 60px var(--shadow-color);
    position: relative;
    /* safe-areas handled on inner elements (header, input-area, messages) */
}

/* Когда клавиатура открыта — position:fixed управляется из JS,
   сбрасываем лишние трансформации, которые могут мешать */
.app-container.keyboard-open {
    transform: none !important;
    border-radius: 0;
    border-left: none;
    border-right: none;
    max-width: 100%;
    max-height: none;
    box-shadow: none;
}

/* ============================================
   Header
   ============================================ */
.chat-header {
    padding: 16px 20px 12px;
    background: linear-gradient(180deg, #1c1c3a 0%, var(--bg-secondary) 100%);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.header-top {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
}

.header-title {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.header-badge {
    font-size: 10px;
    font-weight: 600;
    padding: 2px 8px;
    background: var(--accent-primary);
    border-radius: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.personality-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.personality-emoji {
    font-size: 20px;
    line-height: 1;
}

.personality-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
}

/* ============================================
   Chat Messages Area
   ============================================ */
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 16px 16px 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--bg-chat);
    scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

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

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

/* ============================================
   Loading
   ============================================ */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
}

.loading-spinner {
    width: 48px;
    height: 48px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.loading-text {
    color: var(--text-secondary);
    font-size: 14px;
}

/* ============================================
   Message Bubbles
   ============================================ */
.message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: messageIn 0.3s ease forwards;
    opacity: 0;
    transform: translateY(8px);
}

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

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

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

.message-bubble {
    padding: 10px 16px;
    font-size: 15px;
    line-height: 1.5;
    border-radius: var(--radius-md);
    word-wrap: break-word;
    white-space: pre-wrap;
}

.message-ai .message-bubble {
    background: var(--bg-ai-msg);
    border-bottom-left-radius: 4px;
    color: var(--text-primary);
}

.message-user .message-bubble {
    background: var(--bg-user-msg);
    border-bottom-right-radius: 4px;
    color: #fff;
}

.message-ai .message-sender {
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 4px;
    margin-left: 4px;
    font-weight: 500;
}

.message-time {
    font-size: 10px;
    color: var(--text-muted);
    margin-top: 4px;
    margin-left: 4px;
    opacity: 0.7;
}

.message-user .message-time {
    text-align: right;
    margin-right: 4px;
}

/* ============================================
   Typing Indicator
   ============================================ */
.typing-indicator {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 4px 20px 8px;
    font-size: 12px;
    color: var(--text-muted);
    flex-shrink: 0;
}

.typing-indicator.visible {
    display: flex;
}

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

.dot {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: dotBounce 1.4s ease-in-out infinite;
}

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

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

.typing-text {
    animation: blink 1.4s steps(2) infinite;
}

@keyframes blink {
    0% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ============================================
   Input Area
   ============================================ */
.chat-input-area {
    padding: 12px 16px 16px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
    position: relative;
}

.switch-btn {
    display: block;
    width: 100%;
    padding: 10px;
    margin-bottom: 10px;
    background: linear-gradient(135deg, rgba(233, 64, 122, 0.15), rgba(184, 42, 110, 0.15));
    border: 1px solid rgba(233, 64, 122, 0.3);
    border-radius: var(--radius-sm);
    color: var(--accent-primary);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-normal);
    font-family: inherit;
}

.switch-btn:hover {
    background: linear-gradient(135deg, rgba(233, 64, 122, 0.25), rgba(184, 42, 110, 0.25));
    border-color: var(--accent-primary);
    box-shadow: 0 0 20px var(--accent-glow);
    transform: translateY(-1px);
}

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

.switch-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.input-row {
    display: flex;
    gap: 8px;
}

.message-input {
    flex: 1;
    min-width: 0;
    padding: 12px 16px;
    background: var(--bg-chat);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 16px;
    font-family: inherit;
    outline: none;
    transition: border-color var(--transition-fast);
    touch-action: manipulation;
}

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

.message-input:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 12px var(--accent-glow);
}

.message-input:disabled {
    opacity: 0.5;
}

.send-btn {
    width: 46px;
    height: 46px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    border: none;
    border-radius: var(--radius-sm);
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    touch-action: manipulation;
}

.send-btn:hover:not(:disabled) {
    box-shadow: 0 0 20px var(--accent-glow);
    transform: scale(1.05);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

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

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

/* ============================================
   Switch Animation
   ============================================ */
.switch-notification {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(15, 15, 26, 0.95);
    border: 1px solid var(--accent-primary);
    border-radius: var(--radius-md);
    padding: 20px 32px;
    text-align: center;
    z-index: 100;
    animation: notifIn 0.4s ease forwards;
    box-shadow: 0 0 40px var(--accent-glow);
}

@keyframes notifIn {
    from {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.8);
    }
    to {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

.switch-notification .notif-emoji {
    font-size: 40px;
    display: block;
    margin-bottom: 8px;
}

.switch-notification .notif-text {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.switch-notification .notif-sub {
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* ============================================
   Emoji Picker
   ============================================ */
.emoji-picker {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 10px;
    gap: 4px;
    grid-template-columns: repeat(8, 1fr);
    z-index: 50;
    box-shadow: 0 -8px 30px var(--shadow-color);
    margin-bottom: 8px;
    max-height: 180px;
    overflow-y: auto;
}

.emoji-picker.visible {
    display: grid;
}

.emoji-picker::-webkit-scrollbar {
    width: 3px;
}

.emoji-picker::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.emoji-item {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    padding: 6px 4px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
    user-select: none;
}

.emoji-item:hover {
    background: rgba(233, 64, 122, 0.15);
}

/* ============================================
   Input Icon Buttons
   ============================================ */
.input-icon-btn {
    width: 42px;
    height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-chat);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
}

.input-icon-btn:hover {
    border-color: var(--accent-primary);
    background: rgba(233, 64, 122, 0.1);
    color: var(--text-primary);
}

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

/* ============================================
   Mic Button Recording
   ============================================ */
.mic-btn.recording {
    background: #ff4d6d;
    border-color: #ff4d6d;
    color: white;
    animation: micPulse 1s ease-in-out infinite;
    box-shadow: 0 0 12px rgba(255, 77, 109, 0.4);
}

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

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

/* Статус-бар записи */
.recording-status {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 4px 12px;
    font-size: 13px;
    color: #ff4d6d;
    animation: slideIn 0.2s ease;
}

.recording-status.visible {
    display: flex;
}

.recording-dot {
    width: 8px;
    height: 8px;
    background: #ff4d6d;
    border-radius: 50%;
    animation: micPulse 1s ease-in-out infinite;
}

/* ============================================
   Image Preview
   ============================================ */
.image-preview {
    display: none;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    margin-bottom: 8px;
    background: var(--bg-chat);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    position: relative;
}

.image-preview.visible {
    display: flex;
}

.preview-image {
    max-width: 60px;
    max-height: 60px;
    border-radius: 6px;
    object-fit: cover;
}

.preview-label {
    font-size: 13px;
    color: var(--text-secondary);
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.remove-image-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 16px;
    cursor: pointer;
    border-radius: 50%;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
}

.remove-image-btn:hover {
    background: rgba(233, 64, 122, 0.2);
    color: var(--accent-primary);
}

/* ============================================
   Reply Preview Bar
   ============================================ */
.reply-preview {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    margin: 4px 8px 0;
    background: var(--bg-secondary);
    border-left: 3px solid var(--accent-primary);
    border-radius: var(--radius-sm);
    animation: slideIn 0.2s ease;
}

.reply-preview.visible {
    display: flex;
}

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

.reply-preview-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.reply-preview-label {
    font-size: 11px;
    color: var(--accent-primary);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.reply-preview-text {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.reply-preview-close {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    border-radius: 50%;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
}

.reply-preview-close:hover {
    background: rgba(233, 64, 122, 0.2);
    color: var(--accent-primary);
}

/* ============================================
   Reply Quote in Messages
   ============================================ */
.message-reply-quote {
    font-size: 12px;
    color: var(--text-muted);
    padding: 4px 8px;
    margin-bottom: 4px;
    border-left: 2px solid var(--accent-primary);
    background: rgba(233, 64, 122, 0.08);
    border-radius: 4px;
    white-space: normal;
    word-wrap: break-word;
    max-width: 100%;
    max-height: 60px;
    overflow-y: auto;
    cursor: default;
}

.message-reply-quote::before {
    content: "↩ ";
    color: var(--accent-primary);
}

/* ============================================
   Message Row (bubble + reply button)
   ============================================ */
.message-row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
}

.message-ai .message-row {
    flex-direction: row;
}

.message-user .message-row {
    flex-direction: row-reverse;
}

/* ============================================
   Reply Button on Messages
   ============================================ */
.message-reply-btn {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 16px;
    cursor: pointer;
    border-radius: 50%;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
    opacity: 0.5;
}

.message-reply-btn:hover {
    opacity: 1;
    background: rgba(233, 64, 122, 0.15);
    color: var(--accent-primary);
    transform: scale(1.15);
}

/* ============================================
   Like Button on Messages
   ============================================ */
.message-like-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 15px;
    cursor: pointer;
    border-radius: 50%;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    padding: 0;
    line-height: 1;
    opacity: 0.4;
    margin-left: 2px;
}

.message-like-btn:hover {
    opacity: 1;
    background: rgba(233, 64, 122, 0.15);
    transform: scale(1.15);
}

.message-like-btn.liked {
    opacity: 1;
    color: #ff4d6d;
    text-shadow: 0 0 8px rgba(255, 77, 109, 0.5);
    animation: likePop 0.3s ease;
}

@keyframes likePop {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); }
    100% { transform: scale(1.15); }
}

.message-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-top: 2px;
}

/* ============================================
   Images in Chat
   ============================================ */
.message-image {
    padding: 4px !important;
    background: transparent !important;
    border: none !important;
    max-width: 240px;
}

.message-image img {
    display: block;
    width: 100%;
    border-radius: 10px;
    cursor: pointer;
    transition: opacity var(--transition-fast);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.message-image img:hover {
    opacity: 0.85;
}

.message-user .message-image img {
    border: 2px solid var(--accent-primary);
}

.message-ai .message-image img {
    border: 2px solid var(--bg-ai-msg);
}

/* ============================================
   Responsive
   ============================================ */
@media (max-width: 500px) {
    html, body {
        /* iOS Safari needs body scrollable when keyboard opens —
           overflow:hidden блокирует нативный скролл к фокусу.
           Визуальное обрезание берёт на себя .app-container */
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }
    .app-container {
        max-width: 100%;
        max-height: 100vh; /* fallback */
        max-height: 100dvh;
        border-radius: 0;
        border: none;
    }
    .input-icon-btn {
        width: 36px;
        height: 36px;
        font-size: 17px;
    }
    .send-btn {
        width: 40px;
        height: 40px;
        font-size: 18px;
    }
    .input-row {
        gap: 6px;
    }
    .chat-input-area {
        padding-top: 10px;
        padding-bottom: 14px;
    }
    .input-hint {
        display: none;
    }
}

@media (min-width: 768px) {
    .app-container {
        height: 90vh;
    }
}

/* ============================================
   iPhone safe-area adjustments
   ============================================ */
@supports (padding: max(0px)) {
    .chat-header {
        padding-left: max(20px, env(safe-area-inset-left, 0px));
        padding-right: max(20px, env(safe-area-inset-right, 0px));
    }
    .chat-input-area {
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        padding-right: max(16px, env(safe-area-inset-right, 0px));
        padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
    }
    .chat-messages {
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        padding-right: max(16px, env(safe-area-inset-right, 0px));
    }
}