/**
 * Responsive Forms - Глобальная система адаптивных форм
 * 
 * Решает проблему: поля форм вылезают за края при сужении окна
 * Применяется: ко всем формам в проекте
 * 
 * Использование:
 * - .form-grid-2 → 2 колонки на desktop, адаптируется
 * - .form-grid-3 → 3 колонки на desktop, адаптируется
 * - .form-grid-4 → 4 колонки на desktop, адаптируется
 */

/* ============================================
   БАЗОВЫЕ GRID LAYOUT (Auto-Responsive)
   ============================================ */

.form-grid-2,
.form-grid-3,
.form-grid-4 {
    display: grid;
    gap: 12px;
    margin-bottom: 12px;
    width: 100%;
}

/**
 * Desktop first approach с min(100%, Xpx)
 * Колонки НИКОГДА не станут больше 100% контейнера
 * auto-fit автоматически переносит на новую строку если не влезают
 */

.form-grid-2 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
}

.form-grid-3 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
}

.form-grid-4 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
}

/* Убираем overflow на grid-элементах */
.form-grid-2 > *,
.form-grid-3 > *,
.form-grid-4 > * {
    min-width: 0; /* Позволяет элементам сжиматься */
    max-width: 100%;
}

/* ============================================
   TABLET (до 1024px)
   ============================================ */

@media (max-width: 1024px) {
    /* На планшетах form-grid-3 и form-grid-4 становятся меньше */
    .form-grid-3 {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
    }
    
    .form-grid-4 {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
    }
}

/* ============================================
   MOBILE (до 768px)
   ============================================ */

@media (max-width: 768px) {
    /* На мобильных все grid становятся 1 колонкой */
    .form-grid-2,
    .form-grid-3,
    .form-grid-4 {
        grid-template-columns: 1fr;
        gap: 16px; /* Больше gap для лучшей читаемости на мобильных */
    }
}

/* ============================================
   SMALL MOBILE (до 480px)
   ============================================ */

@media (max-width: 480px) {
    .form-grid-2,
    .form-grid-3,
    .form-grid-4 {
        gap: 12px;
    }
}

/* ============================================
   FORM CONTROLS (Responsive inputs)
   ============================================ */

.form-control,
input[type="text"],
input[type="email"],
input[type="tel"],
input[type="number"],
input[type="date"],
input[type="datetime-local"],
textarea {
    width: 100%;
    max-width: 100%;
    padding: 8px 10px;
    font-size: 0.85rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    transition: all 0.2s ease;
    background: white;
    min-height: 36px;
    box-sizing: border-box;
}

/* Отдельные стили для select - не переопределяем background полностью,
   чтобы сохранить Bootstrap стрелку выпадающего списка */
.form-select,
select {
    width: 100%;
    max-width: 100%;
    padding: 8px 10px;
    font-size: 0.85rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    transition: all 0.2s ease;
    background-color: white;
    min-height: 36px;
    box-sizing: border-box;
}

/* Focus state */
.form-control:focus,
.form-select:focus,
input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* Readonly state */
.form-control[readonly],
input[readonly],
select[disabled],
textarea[readonly] {
    background-color: #f3f4f6;
    color: #6b7280;
    cursor: not-allowed;
}

/* ============================================
   FORM GROUPS (Labels + Inputs)
   ============================================ */

.form-group {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.form-group label {
    margin-bottom: 4px;
    font-weight: 600;
    color: #374151;
    font-size: 0.8rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============================================
   SELECT WITH LINK (select + button combo)
   ============================================ */

.select-with-link {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    max-width: 100%;
}

.select-with-link select {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 100%;
}

.select-with-link a.select-link,
.select-with-link button.select-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    color: #374151;
    background: #ffffff;
    transition: all 0.2s ease;
    text-decoration: none;
}

.select-with-link a.select-link:hover,
.select-with-link button.select-link:hover {
    color: #1f2937;
    border-color: #9ca3af;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.08);
    transform: translateY(-1px);
}

/* ============================================
   FORM SECTION (Grouped fields with title)
   ============================================ */

.form-section {
    margin-bottom: 12px;
    padding: 12px;
    background: #f8fafc;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
}

.form-section h3,
.form-section h4 {
    font-size: 1.125rem;
    font-weight: 700;
    color: #2d3748;
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid #e2e8f0;
}

@media (max-width: 768px) {
    .form-section {
        padding: 16px;
    }
    
    .form-section h3,
    .form-section h4 {
        font-size: 1rem;
    }
}

/* ============================================
   HORIZONTAL FORM GROUPS (checkbox + label)
   ============================================ */

.form-group-horizontal {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 8px;
    flex-wrap: wrap;
}

.form-group-horizontal label {
    margin-bottom: 0;
    font-weight: normal;
}

/* ============================================
   STICKY ACTIONS (Save/Cancel buttons)
   ============================================ */

.sticky-actions {
    position: sticky;
    bottom: 0;
    background: #ffffffcc;
    backdrop-filter: blur(4px);
    border-top: 1px solid #e2e8f0;
    padding: 8px 12px;
    margin-top: 8px;
    z-index: 10;
    border-radius: 8px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

@media (max-width: 768px) {
    .sticky-actions {
        flex-direction: column;
    }
    
    .sticky-actions .btn {
        width: 100%;
        justify-content: center;
    }
}

/* ============================================
   UTILITIES
   ============================================ */

/* Предотвращение overflow */
.no-overflow {
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
}

/* Full width на мобильных */
@media (max-width: 768px) {
    .mobile-full-width {
        width: 100% !important;
        max-width: 100% !important;
    }
}

/* Responsive text */
@media (max-width: 768px) {
    body {
        font-size: 14px;
    }
    
    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.25rem; }
    h3 { font-size: 1.1rem; }
}
