/* ===== Базовые стили ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #6c5ce7;
    --primary-dark: #5a4bd1;
    --primary-light: #a29bfe;
    --accent: #fd79a8;
    --bg: #f6f4ff;
    --card: #ffffff;
    --text: #2d3436;
    --text-light: #636e72;
    --border: #e2defa;
    --success: #00b894;
    --danger: #e17055;
    --radius: 16px;
    --shadow: 0 8px 30px rgba(108, 92, 231, 0.12);
}

body {
    font-family: 'Nunito', 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, #f6f4ff 0%, #eef0ff 50%, #fdf0f6 100%);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
}

.hidden { display: none !important; }

/* ===== Кнопки ===== */
.btn {
    font-family: inherit;
    font-size: 1rem;
    font-weight: 700;
    padding: 12px 22px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--primary-light);
    color: #fff;
}
.btn-secondary:hover {
    background: var(--primary);
}

.btn-ghost {
    background: transparent;
    color: var(--text-light);
    border: 1px solid var(--border);
}
.btn-ghost:hover {
    background: var(--bg);
    color: var(--text);
}

.btn-block { width: 100%; }
.btn-next { min-width: 180px; }

/* ===== Экраны ===== */
.screen {
    display: none;
    min-height: 100vh;
}
.screen.active { display: block; }

/* ===== Экран авторизации ===== */
/* Внимание: display задаём только через .active, чтобы экран корректно
   скрывался базовым правилом .screen { display: none } после входа. */
#auth-screen {
    align-items: center;
    justify-content: center;
    padding: 20px;
}
#auth-screen.active { display: flex; }

.auth-card {
    background: var(--card);
    border-radius: 24px;
    box-shadow: var(--shadow);
    padding: 40px;
    width: 100%;
    max-width: 440px;
}

.logo { text-align: center; margin-bottom: 24px; }
.logo-icon {
    font-size: 48px;
    margin-bottom: 8px;
}
.logo h1 {
    font-size: 1.8rem;
    color: var(--primary);
    margin-bottom: 4px;
}
.tagline { color: var(--text-light); font-size: 0.95rem; }

.tabs {
    display: flex;
    background: var(--bg);
    border-radius: 12px;
    padding: 4px;
    margin-bottom: 20px;
}
.tab-btn {
    flex: 1;
    padding: 10px;
    border: none;
    background: transparent;
    border-radius: 9px;
    font-family: inherit;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    color: var(--text-light);
    transition: all 0.2s;
}
.tab-btn.active {
    background: var(--card);
    color: var(--primary);
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

/* ===== Поля форм ===== */
.field { margin-bottom: 16px; }
.field label {
    display: block;
    font-weight: 700;
    font-size: 0.9rem;
    margin-bottom: 6px;
    color: var(--text);
}
.field input,
.add-child-form input,
.clone-row input,
textarea {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    font-family: inherit;
    font-size: 1rem;
    transition: border-color 0.2s;
    background: #fff;
    color: var(--text);
}
.field input:focus,
.add-child-form input:focus,
.clone-row input:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
}

.error-msg {
    background: #ffe3e0;
    color: var(--danger);
    padding: 12px;
    border-radius: 10px;
    margin-top: 16px;
    font-weight: 600;
    font-size: 0.9rem;
}

/* ===== Верхняя панель ===== */
.topbar {
    background: var(--card);
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.logo-icon.small { font-size: 28px; margin: 0; }
.brand { font-weight: 800; color: var(--primary); font-size: 1.1rem; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.user-email { color: var(--text-light); font-size: 0.9rem; }

/* ===== Контейнер ===== */
.container {
    max-width: 860px;
    margin: 0 auto;
    padding: 24px 20px 60px;
}

/* ===== Шаги ===== */
.steps {
    display: flex;
    justify-content: space-between;
    margin-bottom: 28px;
    flex-wrap: wrap;
    gap: 8px;
}
.step {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-light);
    font-weight: 700;
    font-size: 0.9rem;
    opacity: 0.6;
}
.step-num {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    transition: all 0.3s;
}
.step.active { opacity: 1; color: var(--primary); }
.step.active .step-num {
    background: var(--primary);
    color: #fff;
}
.step.done { opacity: 0.8; }
.step.done .step-num {
    background: var(--success);
    color: #fff;
}

/* ===== Панели ===== */
.panel {
    background: var(--card);
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 32px;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}
.panel h2 { margin-bottom: 6px; color: var(--text); }
.panel h3.subtitle { margin: 24px 0 12px; color: var(--text); }
.hint { color: var(--text-light); margin-bottom: 20px; font-size: 0.95rem; }

.panel-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 28px;
    gap: 12px;
    flex-wrap: wrap;
}

/* ===== Список детей ===== */
.children-list {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
}
.child-card {
    border: 2px solid var(--border);
    border-radius: 14px;
    padding: 14px 18px;
    cursor: pointer;
    transition: all 0.2s;
    background: #fff;
    min-width: 150px;
    position: relative;
}
.child-card:hover { border-color: var(--primary-light); }
.child-card.selected {
    border-color: var(--primary);
    background: #f0edff;
}
.child-card .child-name { font-weight: 800; font-size: 1.05rem; }
.child-card .child-age { color: var(--text-light); font-size: 0.85rem; }
.child-card .child-del {
    position: absolute;
    top: 6px;
    right: 8px;
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    font-size: 1rem;
    opacity: 0;
    transition: opacity 0.2s;
}
.child-card:hover .child-del { opacity: 1; }

.add-child-form {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.add-child-form input[type="text"] { flex: 2; min-width: 160px; }
.add-child-form input[type="date"] { flex: 1; min-width: 150px; }

/* ===== Drop-зона ===== */
.drop-zone {
    border: 3px dashed var(--primary-light);
    border-radius: 16px;
    padding: 40px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: #faf9ff;
}
.drop-zone:hover, .drop-zone.dragover {
    border-color: var(--primary);
    background: #f0edff;
}
.drop-icon { font-size: 48px; margin-bottom: 10px; }
.drop-content p { color: var(--text-light); }

.preview { text-align: center; margin-top: 16px; }
.preview img {
    max-width: 100%;
    max-height: 380px;
    border-radius: 14px;
    box-shadow: var(--shadow);
    margin-bottom: 12px;
}

/* ===== Голоса ===== */
.voice-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
}
.voice-tab {
    padding: 10px 18px;
    border: 2px solid var(--border);
    background: #fff;
    border-radius: 10px;
    font-family: inherit;
    font-weight: 700;
    cursor: pointer;
    color: var(--text-light);
    transition: all 0.2s;
}
.voice-tab.active {
    border-color: var(--primary);
    color: var(--primary);
    background: #f0edff;
}

.voices-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
}
.voice-card {
    position: relative;
    border: 2px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: #fff;
}
.voice-card:hover { border-color: var(--primary-light); }
.voice-card.selected {
    border-color: var(--primary);
    background: #f0edff;
}
.voice-card .voice-emoji { font-size: 28px; }
.voice-card .voice-name { font-weight: 700; margin-top: 6px; }
.voice-card .voice-badge {
    display: inline-block;
    font-size: 0.7rem;
    background: var(--accent);
    color: #fff;
    padding: 2px 8px;
    border-radius: 8px;
    margin-top: 4px;
}
.voice-card .voice-delete {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    line-height: 1;
    border: none;
    border-radius: 50%;
    background: #eee;
    color: #888;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.voice-card .voice-delete:hover {
    background: #e74c3c;
    color: #fff;
}

.loading { color: var(--text-light); padding: 20px; text-align: center; }

.clone-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}
.clone-row input[type="text"] { flex: 2; min-width: 200px; }
.clone-row input[type="file"] { flex: 1; min-width: 200px; padding: 8px; }

.status-msg { margin-top: 12px; font-weight: 600; }
.status-msg.success { color: var(--success); }
.status-msg.error { color: var(--danger); }

/* ===== Типы сказок ===== */
.story-types {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 12px;
    margin-bottom: 8px;
}
.story-type-card {
    border: 2px solid var(--border);
    border-radius: 14px;
    padding: 16px;
    cursor: pointer;
    transition: all 0.2s;
    background: #fff;
}
.story-type-card:hover { border-color: var(--primary-light); transform: translateY(-2px); }
.story-type-card.selected {
    border-color: var(--primary);
    background: #f0edff;
    box-shadow: 0 4px 16px rgba(108,92,231,0.15);
}
.story-type-card .st-emoji { font-size: 30px; }
.story-type-card .st-title { font-weight: 800; margin-top: 8px; }
.story-type-card .st-subtitle { color: var(--text-light); font-size: 0.85rem; margin-top: 2px; }
.story-type-card .st-desc { font-size: 0.85rem; color: var(--text); margin-top: 6px; }

.topic-block {
    background: var(--bg);
    border-radius: 12px;
    padding: 16px;
    margin: 16px 0;
}
.topic-block label { font-weight: 700; display: block; margin-bottom: 8px; }
.topic-block textarea { resize: vertical; }
.char-count { text-align: right; color: var(--text-light); font-size: 0.8rem; margin-top: 4px; }

/* ===== Музыка ===== */
.music-options {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 12px;
}
.music-card {
    border: 2px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    cursor: pointer;
    transition: all 0.2s;
    background: #fff;
    text-align: center;
}
.music-card:hover { border-color: var(--primary-light); }
.music-card.selected {
    border-color: var(--primary);
    background: #f0edff;
}
.music-card .m-emoji { font-size: 26px; }
.music-card .m-title { font-weight: 700; margin-top: 6px; }
.music-card .m-subtitle { color: var(--text-light); font-size: 0.8rem; }

/* ===== Генерация ===== */
.generating { text-align: center; padding: 40px 20px; }
.spinner {
    width: 56px;
    height: 56px;
    border: 5px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    margin: 0 auto 20px;
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Результат ===== */
.result-drawing { text-align: center; margin-bottom: 20px; }
.result-drawing img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 14px;
    box-shadow: var(--shadow);
}

.audio-player {
    background: var(--bg);
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 16px;
}
.audio-player audio { width: 100%; }

.story-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}
.meta-chip {
    background: #f0edff;
    color: var(--primary-dark);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 700;
}

.story-text-block {
    background: var(--bg);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 20px;
}
.story-text-block summary {
    cursor: pointer;
    font-weight: 700;
    color: var(--primary);
}
.story-text-block p {
    margin-top: 12px;
    white-space: pre-wrap;
    line-height: 1.7;
    color: var(--text);
}

.bonus-block {
    border-top: 2px solid var(--border);
    padding-top: 20px;
}
.bonus-buttons {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}
.color-result { text-align: center; margin-top: 16px; }
.color-result img {
    max-width: 100%;
    max-height: 400px;
    border-radius: 14px;
    box-shadow: var(--shadow);
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: #fff;
    padding: 14px 24px;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    z-index: 1000;
    max-width: 90%;
    text-align: center;
    animation: fadeIn 0.3s ease;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ===== Кнопки скачивания картинок ===== */
.btn-sm { padding: 8px 14px; font-size: 0.85rem; }
.img-actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 12px;
    flex-wrap: wrap;
}

/* ===== Архив сказок ===== */
.archive-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 6px;
}
.archive-head h2 { color: var(--text); }

.archive-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-top: 16px;
}
.archive-card {
    background: var(--card);
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 24px;
    animation: fadeIn 0.3s ease;
}
.archive-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}
.archive-card-title { font-weight: 800; font-size: 1.1rem; color: var(--text); }
.archive-card-date { color: var(--text-light); font-size: 0.85rem; }
.archive-card-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}
.archive-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}
.archive-media { text-align: center; }
.archive-media img {
    max-width: 100%;
    max-height: 260px;
    border-radius: 14px;
    box-shadow: var(--shadow);
}
.archive-media .media-label {
    font-weight: 700;
    color: var(--text-light);
    font-size: 0.85rem;
    margin-bottom: 8px;
}
.archive-audio audio { width: 100%; }
.archive-empty {
    text-align: center;
    color: var(--text-light);
    padding: 40px 20px;
    background: var(--card);
    border-radius: 20px;
    box-shadow: var(--shadow);
    margin-top: 16px;
}

/* ===== Адаптивность ===== */

/* Средние экраны и планшеты */
@media (max-width: 860px) {
    .panel { padding: 26px; }
    .topbar { padding: 12px 16px; }
    .container { padding: 20px 14px 50px; }
}

/* Мобильные устройства */
@media (max-width: 600px) {
    .panel { padding: 20px; }
    .auth-card { padding: 24px; }
    .steps { font-size: 0.75rem; }
    .step-num { width: 24px; height: 24px; font-size: 0.75rem; }
    .panel-actions { flex-direction: column; }
    .panel-actions .btn { width: 100%; }
    .archive-grid { grid-template-columns: 1fr; }

    /* Верхняя панель: компактная и переносимая, чтобы ничего не вылезало */
    .topbar {
        padding: 10px 12px;
        flex-wrap: wrap;
        gap: 8px;
    }
    .brand { font-size: 0.95rem; }
    .logo-icon.small { font-size: 24px; }
    .topbar-right { gap: 8px; flex-wrap: wrap; }
    .user-email { display: none; } /* прячем email на мобильном, чтобы кнопки влезали */
    .topbar .btn { padding: 8px 12px; font-size: 0.85rem; }

    /* Шаги-индикатор: компактнее */
    .steps { gap: 4px; margin-bottom: 18px; }
    .step { gap: 4px; font-size: 0.72rem; }
    .step-num { width: 22px; height: 22px; font-size: 0.7rem; }

    /* Сетки карточек: одна колонка на узких экранах */
    .story-types { grid-template-columns: 1fr; }
    .music-options { grid-template-columns: 1fr; }
    .voices-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }

    /* Форма добавления ребёнка: в столбик */
    .add-child-form { flex-direction: column; }
    .add-child-form input[type="text"],
    .add-child-form input[type="date"] { min-width: 0; width: 100%; }
    .add-child-form .btn { width: 100%; }

    /* Клонирование голоса: поля в столбик */
    .clone-row { flex-direction: column; }
    .clone-row input[type="text"],
    .clone-row input[type="file"] { min-width: 0; width: 100%; }
    #clone-btn { width: 100%; }

    /* Вкладки голосов: на всю ширину */
    .voice-tabs { flex-direction: column; }
    .voice-tab { width: 100%; text-align: center; }

    /* Кнопки навигации: на всю ширину */
    .btn-next { min-width: 0; width: 100%; }
    .btn-back { width: 100%; }

    /* Бонус-кнопки: в столбик */
    .bonus-buttons { flex-direction: column; }
    .bonus-buttons .btn { width: 100%; }

    /* Архив: компактнее */
    .archive-card { padding: 18px; }
    .archive-card-head { flex-direction: column; align-items: flex-start; }
    .archive-head { flex-direction: column; align-items: flex-start; }
    .archive-head .btn { width: 100%; }

    /* Результат: ограничиваем высоту картинки */
    .result-drawing img { max-height: 240px; }
}

/* Очень узкие экраны */
@media (max-width: 400px) {
    .container { padding: 16px 10px 40px; }
    .panel { padding: 16px; }
    .topbar-right .btn { padding: 8px 10px; }
    .story-type-card, .music-card, .voice-card { padding: 12px; }
}
