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

:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --bg-tertiary: #e0e0e0;
    --text-primary: #000000;
    --text-secondary: #666666;
    --accent: #007AFF;
    --accent-hover: #0051D5;
    --danger: #FF3B30;
    --success: #34C759;
    --border: #e0e0e0;
    --shadow: rgba(0, 0, 0, 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    -webkit-font-smoothing: antialiased;
    position: relative;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-secondary);
}

/* Заголовок редактора */
.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 1px 3px var(--shadow);
}

.slide-navigation {
    display: flex;
    align-items: center;
    gap: 12px;
}

.nav-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.nav-btn:active {
    background: var(--bg-tertiary);
    transform: scale(0.95);
}

.slide-counter {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    min-width: 50px;
    text-align: center;
}

.editor-actions {
    display: flex;
    gap: 8px;
}

.action-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.action-btn:active {
    background: var(--bg-tertiary);
    transform: scale(0.95);
}

.action-btn.danger:active {
    border-color: var(--danger);
    color: var(--danger);
}

/* Предпросмотр слайда */
.slide-preview {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    overflow: auto;
    background: var(--bg-secondary);
    -webkit-overflow-scrolling: touch;
}

.slide-content {
    width: 100%;
    max-width: 400px;
    aspect-ratio: 4/5;
    /* background управляется через JavaScript для поддержки цветов и градиентов */
    border-radius: 16px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    position: relative;
    overflow: hidden;
}

/* Подложка для фона */
.background-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 1;
}

.slide-header,
.slide-body,
.slide-footer {
    position: relative;
    z-index: 2;
}

.slide-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    font-size: 12px;
    color: var(--text-secondary);
}

.username {
    font-weight: 600;
}

.slide-number {
    opacity: 0.7;
}

.slide-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.slide-title {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.3;
    margin-bottom: 16px;
    color: var(--text-primary);
    outline: none;
    min-height: 40px;
    column-count: 1 !important;
    column-width: auto !important;
    columns: 1 !important;
    display: block !important;
    /* Fallback для шрифтов - будет перезаписан inline стилями из JS */
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.slide-title:empty:before {
    content: "Введи заголовок...";
    color: var(--text-secondary);
    opacity: 0.5;
}

.slide-text {
    font-size: 16px;
    line-height: 1.6;
    color: var(--text-secondary);
    outline: none;
    min-height: 24px;
    column-count: 1 !important;
    column-width: auto !important;
    columns: 1 !important;
    display: block !important;
    white-space: pre-line; /* Сохраняем переносы строк от <br> */
    word-wrap: break-word;
    overflow-wrap: break-word;
    /* Fallback для шрифтов - будет перезаписан inline стилями из JS */
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Стили для правильного отображения списков через <br> */
.slide-text br {
    display: block !important;
    content: "" !important;
    margin: 0.4em 0 !important;
    line-height: 1.4 !important;
    height: 0.4em !important;
}

/* Дополнительные стили для списков - добавляем отступы для читаемости */
.slide-text:has(br) {
    line-height: 1.8;
}

/* Альтернативный способ для браузеров без поддержки :has() */
.slide-text br + * {
    margin-top: 0.3em;
}

.slide-text:empty:before {
    content: "Введи текст...";
    color: var(--text-secondary);
    opacity: 0.5;
}

.slide-footer {
    margin-top: auto;
    padding-top: 16px;
}

.cta-text {
    font-size: 14px;
    color: var(--accent);
    font-weight: 600;
}

/* Панель настроек */
/* Панель свойств элемента (для админ-панели) */
.element-properties-panel {
    position: fixed;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
    width: 320px;
    max-height: 80vh;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    z-index: 100000;
    overflow-y: auto;
    padding: 16px;
    display: none;
}

.properties-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

.properties-header h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.close-properties-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.close-properties-btn:hover {
    color: var(--text-primary);
}

.properties-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.property-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.property-group label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
}

.settings-panel {
    max-height: 300px;
    overflow-y: auto;
    padding: 16px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
    -webkit-overflow-scrolling: touch;
}

.settings-section {
    display: none;
}

.settings-section.active {
    display: block;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

.settings-header h3 {
    font-size: 16px;
    margin: 0;
    color: var(--text-primary);
    font-weight: 600;
}

.close-settings-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 28px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s;
}

.close-settings-btn:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.close-settings-btn:active {
    transform: scale(0.9);
}

.settings-section h3 {
    font-size: 16px;
    margin-bottom: 16px;
    color: var(--text-primary);
    font-weight: 600;
}

/* Контейнер шаблонов */
.templates-container {
    margin-top: 16px;
}

.templates-carousel {
    display: flex;
    gap: 16px;
    overflow-x: auto;
    padding: 8px 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.templates-carousel::-webkit-scrollbar {
    height: 6px;
}

.templates-carousel::-webkit-scrollbar-track {
    background: var(--bg-secondary);
    border-radius: 3px;
}

.templates-carousel::-webkit-scrollbar-thumb {
    background: var(--text-secondary);
    border-radius: 3px;
}

.template-item {
    min-width: 120px;
    max-width: 120px;
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s;
    border: 2px solid transparent;
}

.template-item:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.template-item:active {
    transform: translateY(0);
}

.template-preview {
    width: 100%;
    aspect-ratio: 1/1;
    background: var(--bg-tertiary);
    border-radius: 8px;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: var(--text-secondary);
    overflow: hidden;
    position: relative;
}

.template-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.template-name {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-primary);
    text-align: center;
    margin: 0;
    line-height: 1.2;
}

.templates-loading {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
    font-size: 14px;
}

.setting-group {
    margin-bottom: 20px;
}

.setting-group label {
    display: block;
    font-size: 14px;
    margin-bottom: 8px;
    color: var(--text-primary);
    font-weight: 500;
}

.slider-container {
    display: flex;
    align-items: center;
    gap: 12px;
}

.slider-container input[type="range"] {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-tertiary);
    border-radius: 3px;
    outline: none;
}

.slider-container input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
}

.slider-container input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.slider-container span {
    font-size: 14px;
    color: var(--text-secondary);
    min-width: 50px;
    text-align: right;
}

.setting-group input[type="color"] {
    width: 100%;
    height: 50px;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

.setting-group input[type="checkbox"] {
    width: 24px;
    height: 24px;
    cursor: pointer;
    accent-color: var(--accent);
}

.button-group {
    display: flex;
    gap: 8px;
}

.type-btn,
.align-btn,
.text-transform-btn {
    flex: 1;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
    white-space: nowrap;
}

.text-transform-btn {
    font-size: 12px;
    padding: 10px 12px;
    line-height: 1.3;
    color: #333333 !important;
    flex-shrink: 0;
    min-width: fit-content;
    background: #e0e0e0 !important;
    border: 1px solid #cccccc !important;
}

.text-transform-carousel-container {
    width: 100%;
    overflow: hidden;
}

.text-transform-carousel {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding-bottom: 2px;
}

.text-transform-carousel::-webkit-scrollbar {
    display: none;
}

.type-btn:active,
.align-btn:active,
.text-transform-btn:active {
    transform: scale(0.98);
}

.type-btn.active,
.align-btn.active,
.text-transform-btn.active {
    background: var(--accent) !important;
    border-color: var(--accent) !important;
    color: white !important;
}

.action-button {
    width: 100%;
    padding: 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.action-button:active {
    background: var(--bg-tertiary);
    transform: scale(0.98);
}

/* Панель действий */
.action-bar {
    padding: 12px 16px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
}

.apply-toggle {
    margin-bottom: 12px;
}

.apply-toggle label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--text-primary);
    cursor: pointer;
}

.apply-toggle input[type="checkbox"] {
    width: 20px;
    height: 20px;
    accent-color: var(--accent);
}

.action-buttons {
    display: none; /* Скрыты по умолчанию */
    gap: 12px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.action-buttons.visible {
    display: flex; /* Показываем только когда видимы */
}

.cancel-btn,
.save-btn {
    flex: 1;
    padding: 14px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
}

.cancel-btn {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.cancel-btn:active {
    background: var(--bg-tertiary);
    transform: scale(0.98);
}

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

.save-btn:active {
    background: var(--accent-hover);
    transform: scale(0.98);
}

/* Панель инструментов (табы внизу) */
.toolbar {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px 0;
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
    box-shadow: 0 -1px 3px var(--shadow);
    position: relative;
    z-index: 10;
}

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
    min-width: 60px;
    font-size: 11px;
    position: relative;
}

.tool-btn::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 3px;
    background: var(--accent);
    border-radius: 3px 3px 0 0;
    transition: width 0.2s;
}

.tool-btn.active {
    color: var(--accent);
}

.tool-btn.active::after {
    width: 60%;
}

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

.tool-btn svg {
    stroke-width: 2;
    width: 24px;
    height: 24px;
}

/* Модальное окно */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-primary);
    border-radius: 16px;
    padding: 24px;
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-header h2 {
    font-size: 20px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 28px;
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition: all 0.2s;
}

.modal-close:active {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.export-btn {
    width: 100%;
    padding: 14px;
    margin-bottom: 12px;
    background: var(--accent);
    border: none;
    border-radius: 12px;
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.export-btn:active {
    background: var(--accent-hover);
    transform: scale(0.98);
}

/* Модальное окно предпросмотра слайдов */
.slides-preview-modal {
    max-width: 90%;
    max-height: 90vh;
    width: 100%;
    padding: 0;
}

.slides-preview-body {
    max-height: calc(90vh - 140px);
    overflow-y: auto;
    padding: 20px;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
}

.slide-preview-item {
    background: var(--bg-secondary);
    border-radius: 12px;
    overflow: visible;
    border: 2px solid var(--border);
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
}

.slide-preview-item:active {
    transform: scale(0.98);
}

.slide-preview-image {
    width: 100%;
    aspect-ratio: 1080 / 1350; /* Вертикальный формат, соответствует canvas */
    object-fit: contain; /* Показываем полностью, без обрезки */
    display: block;
    border-radius: 12px 12px 0 0;
    background: var(--bg-secondary);
}

.slide-preview-info {
    padding: 12px;
    text-align: center;
    background: var(--bg-secondary);
    border-radius: 0 0 12px 12px;
    flex-shrink: 0;
}

.slide-preview-number {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.slide-download-btn {
    width: 100%;
    padding: 10px;
    background: var(--accent);
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.slide-download-btn:active {
    background: var(--accent-hover);
    transform: scale(0.98);
}

.modal-footer {
    padding: 20px;
    border-top: 1px solid var(--border);
    background: var(--bg-primary);
    border-radius: 0 0 16px 16px;
}

.download-all-btn {
    margin-bottom: 0;
    font-size: 18px;
    padding: 16px;
}

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    text-align: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--bg-tertiary);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

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

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

.export-btn:last-child {
    margin-bottom: 0;
}

/* Адаптивность для мобильных */
@media (max-width: 768px) {
    .slide-preview {
        padding: 12px;
    }

    .slide-content {
        max-width: 100%;
        padding: 20px;
    }

    .settings-panel {
        max-height: 250px;
        padding: 12px;
    }

    .toolbar {
        padding: 6px 0;
    }

    .tool-btn {
        min-width: 50px;
        padding: 6px 8px;
        font-size: 10px;
    }

    .tool-btn svg {
        width: 20px;
        height: 20px;
    }

    /* Исправление отображения слайдов на мобильных */
    .slides-preview-body {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        gap: 12px;
        padding: 12px;
    }

    .slides-preview-modal {
        max-width: 100%;
        max-height: 100vh;
    }

    .slide-preview-item {
        width: 100%;
        max-width: 100%;
        flex-shrink: 0;
        order: 0;
    }
}

/* Индикатор загрузки */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    opacity: 1;
    visibility: visible;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.loading-overlay.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loading-content {
    text-align: center;
    color: #ffffff;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

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

.loading-text {
    font-size: 16px;
    font-weight: 500;
    color: #ffffff;
    margin: 0;
}

/* Кнопка логирования видна для отладки */
#debugLogBtn {
    display: block;
}

/* Запрет колонок для элементов шаблона */
.template-element,
.template-text,
.template-element * {
    column-count: 1 !important;
    column-width: auto !important;
    columns: 1 !important;
    white-space: pre-line !important; /* Сохраняем переносы строк от <br> */
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
}

/* Стили для правильного отображения списков через <br> в элементах шаблона */
.template-element br,
.template-text br {
    display: block !important;
    content: "" !important;
    margin: 0 !important;
    line-height: 1 !important;
    height: 0 !important;
}

/* Дополнительные стили для списков в элементах шаблона */
.template-element:has(br),
.template-text:has(br) {
    line-height: 1.3 !important;
}

.template-element br + *,
.template-text br + * {
    margin-top: 0;
}
