/* style.css - Оптимизированная версия */

/* ---------------------------------------------------- */
/* --- 1. Сброс и Глобальные Настройки (Full Screen) --- */
/* ---------------------------------------------------- */

* {
    box-sizing: border-box;
}

/* Полная блокировка прокрутки и позиционирование на весь экран */
html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    
    /* Агрессивная блокировка прокрутки на уровне документа */
    overflow: hidden !important;
    position: fixed; 
    overscroll-behavior: none;
    
    /* Устранена конфликтность: устанавливаем 'none' для полной блокировки */
    -webkit-overflow-scrolling: none !important;
}

body {
    /* Позиционирование и фон */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    
    /* Flex-контейнер для центрирования содержимого */
    display: flex;
    justify-content: center;
    align-items: center;
    
    /* Общие стили */
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    
    /* Консолидация отключения выделения и тач-действий */
    touch-action: none !important;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Отключаем выделение только там, где неактивны поля ввода */
body:not(.text-input-active) {
    user-select: none !important;
    -webkit-user-select: none !important;
    -moz-user-select: none !important;
    -ms-user-select: none !important;
}

/* Разрешаем выделение внутри полей ввода */
input, textarea {
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
}

/* Декоративный фон (оставлен без изменений как оптимальный инлайновый SVG) */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="20" cy="20" r="2" fill="white" opacity="0.5"/><circle cx="80" cy="80" r="2" fill="white" opacity="0.5"/></svg>');
    pointer-events: none;
    z-index: 0;
}


/* ---------------------------------------------------- */
/* --- 2. Контейнер и Основные Стили --- */
/* ---------------------------------------------------- */

.container {
    text-align: center;
    padding: 30px;
    background-color: #fff;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 600px;
    width: 90%; 
    max-height: 100vh;
    overflow-y: auto; 
    position: relative;
    z-index: 1;
    animation: slideIn 0.5s ease-out;
    /* Плавная прокрутка внутри контейнера для iOS */
    -webkit-overflow-scrolling: touch; 
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

h1 {
    color: #667eea;
    margin-bottom: 10px;
    font-size: 32px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

h2 {
    color: #333;
    font-size: 24px;
    margin-bottom: 20px;
}

/* Стили для главного меню */
.menu-screen {
    padding: 20px 0;
}

.score-display {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 15px;
    color: #667eea;
    background: linear-gradient(135deg, #667eea20 0%, #764ba220 100%);
    padding: 15px;
    border-radius: 8px;
    border-left: 4px solid #667eea;
}

/* Кнопки выбора уровня */
.difficulty-buttons {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(60px, 1fr)); 
    gap: 10px;
    margin: 20px 0;
}

.difficulty-buttons button {
    padding: 12px 8px;
    border-radius: 8px;
    border: 2px solid #ddd;
    font-size: 16px;
    cursor: pointer;
    background-color: #f8f9fa;
    font-weight: bold;
    transition: all 0.3s ease;
    color: #333;
}

.difficulty-buttons button:hover {
    background-color: #e9ecef;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    border-color: #667eea;
}

.difficulty-buttons button.selected {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-weight: bold;
    border-color: #667eea;
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
    transform: scale(1.05);
}

/* Элементы управления (Controls) */
.controls {
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.controls > div {
    display: flex;
    gap: 10px;
    flex: 1;
}

.controls > div > button {
    flex: 1;
    min-width: 110px;
}

/* Общий стиль для всех кнопок управления */
#start-game-btn, #new-game-btn, #main-menu-btn, #pencil-btn, #leaderboard-btn {
    padding: 12px 16px;
    border-radius: 8px;
    border: none;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    flex: 1;
    min-width: 110px;
}

/* Цвета кнопок */
#start-game-btn, #new-game-btn {
    background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
    color: white;
}

#leaderboard-btn {
    background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
    color: white;
}

#main-menu-btn, #pencil-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

/* Ховер-эффекты */
#start-game-btn:hover, #new-game-btn:hover {
    background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(76, 175, 80, 0.4);
}

#main-menu-btn:hover, #pencil-btn:hover, #leaderboard-btn:hover {
    transform: translateY(-2px);
}

/* Отдельный ховер для меню/карандаша */
#main-menu-btn:hover, #pencil-btn:hover {
    background: linear-gradient(135deg, #764ba2 0%, #5f3a8f 100%);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

#leaderboard-btn:hover {
    box-shadow: 0 6px 20px rgba(255, 152, 0, 0.4);
}

/* Active (клик) эффект */
#start-game-btn:active, #new-game-btn:active, #leaderboard-btn:active, #main-menu-btn:active, #pencil-btn:active {
    transform: translateY(0);
}

/* Режим карандаша */
#pencil-btn.active {
    background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
    box-shadow: 0 6px 20px rgba(255, 152, 0, 0.4);
}

/* Таймер */
.timer {
    font-size: 24px;
    font-weight: bold;
    color: #667eea;
    padding: 8px 16px;
    background: linear-gradient(135deg, #667eea20 0%, #764ba220 100%);
    border-radius: 8px;
    min-width: 100px;
    border-left: 4px solid #667eea;
}

/* ---------------------------------------------------- */
/* --- 3. Поле Судоку и Ячейки --- */
/* ---------------------------------------------------- */

.board {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    width: 90vmin; 
    height: 90vmin; 
    max-width: 460px; 
    max-height: 460px; 
    border: 3px solid #333;
    box-sizing: content-box;
    margin: 20px auto;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    border-radius: 4px;
}

.cell {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px; 
    font-weight: bold;
    cursor: pointer;
    background-color: #fff;
    user-select: none;
    position: relative;
    border: 1px solid #ccc;
}

/* Границы между большими квадратами 3x3 */

/* Внутренние вертикальные границы (после 3-й, 6-й колонки, кроме последней) */
.cell:nth-child(3n):not(:nth-child(9n)) { 
    border-right: 3px solid #333; 
}

/* Горизонтальные границы (после 3-й и 6-й строк) */
.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) { 
    border-bottom: 3px solid #333 !important; 
}

/* Внешние границы доски */
.cell:nth-child(n + 1):nth-child(-n + 9) { 
    border-top: 3px solid #333 !important; 
}
.cell:nth-child(n + 73):nth-child(-n + 81) { 
    border-bottom: 3px solid #333 !important; 
}
.cell:nth-child(9n + 1) { 
    border-left: 3px solid #333 !important; 
}
.cell:nth-child(9n) { 
    border-right: 3px solid #333 !important; 
}

/* Стиль для режима карандаша (Кандидаты) */
.cell.pencil-mode {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    font-size: 10px; 
    font-weight: normal;
    padding: 2px; 
    text-align: center;
    background-color: #fafafa;
}

.cell.pencil-mode > span {
    display: flex;
    justify-content: center;
    align-items: center;
    color: #999;
    font-weight: 500;
}

/* Состояния ячеек */
.initial-value {
    background-color: #f0f0f0;
    color: #333;
    cursor: default;
    font-weight: bold;
}

.user-input {
    color: #667eea;
}

.cell:hover:not(.initial-value) {
    background-color: #e3f2fd;
    box-shadow: inset 0 0 10px rgba(102, 126, 234, 0.2);
}

.selected {
    background: linear-gradient(135deg, #ffd700 0%, #ffb700 100%) !important;
    box-shadow: inset 0 0 10px rgba(255, 215, 0, 0.4) !important;
    color: #333 !important;
    font-weight: bold !important;
}

.highlight-value {
    background-color: #DCF8C6 !important; 
    border-color: #43A047 !important; 
}

.conflict-cell {
    background-color: #FFD6D6 !important; 
    border-color: #E53935 !important; 
    color: #B71C1C !important; 
    animation: pulseRed 0.5s ease-out;
}

@keyframes pulseRed {
    0% { box-shadow: 0 0 0 0 rgba(229, 57, 53, 0.4); }
    70% { box-shadow: 0 0 0 6px rgba(229, 57, 53, 0); }
    100% { box-shadow: 0 0 0 0 rgba(229, 57, 53, 0); }
}

/* Сообщения */
.message {
    margin-top: 20px;
    font-size: 18px;
    font-weight: bold;
    min-height: 30px;
    padding: 10px;
    border-radius: 8px;
    animation: messageAppear 0.3s ease-out;
}

@keyframes messageAppear {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.message.success-msg {
    background: linear-gradient(135deg, #DCF8C6 0%, #B9F6CA 100%);
    color: #1B5E20;
    border-left: 4px solid #43A047;
    box-shadow: 0 4px 12px rgba(67, 160, 71, 0.3);
}

/* ---------------------------------------------------- */
/* --- 4. Мобильный ввод чисел (Controls) --- */
/* ---------------------------------------------------- */

.mobile-input-controls {
    margin-top: 15px;
    padding: 10px;
    border-top: 1px solid #ddd;
    background-color: #f8f8f8;
    border-radius: 0 0 16px 16px;
}

.mobile-input-controls .number-buttons {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    gap: 8px;
    margin-bottom: 10px;
}

.mobile-input-controls button {
    padding: 15px 5px;
    font-size: 20px;
    font-weight: bold;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s;
    background-color: #e9ecef;
    color: #333;
}

.mobile-input-controls button:active {
    background-color: #dee2e6;
    transform: scale(0.98);
}

#delete-btn {
    width: 100%;
    background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%) !important; 
    color: white !important;
    padding: 12px;
}

/* ---------------------------------------------------- */
/* --- 5. Модальные окна (Modal) --- */
/* ---------------------------------------------------- */

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    animation: fadeIn 0.3s ease-out;
}

.modal-content {
    background: white;
    border-radius: 16px;
    padding: 0;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: slideUp 0.4s ease-out;
}

.win-modal {
    background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
    border: 3px solid #4CAF50;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 30px 10px;
    border-bottom: 1px solid #eee;
}

.modal-header h2 {
    color: #4CAF50;
    margin: 0;
    font-size: 28px;
}

.close-modal {
    background: none;
    border: none;
    font-size: 36px;
    color: #999;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.3s;
}

.close-modal:hover {
    background: #f5f5f5;
    color: #333;
}

.modal-body {
    padding: 30px;
    text-align: center;
}

.win-icon {
    font-size: 60px;
    margin-bottom: 20px;
    animation: bounce 1s ease infinite alternate;
}

@keyframes bounce {
    from { transform: translateY(0); }
    to { transform: translateY(-10px); }
}

.modal-body p {
    font-size: 20px;
    color: #333;
    margin-bottom: 30px;
    font-weight: 500;
}

.win-stats {
    background: linear-gradient(135deg, #f8fff8 0%, #e8f5e9 100%);
    border-radius: 12px;
    padding: 20px;
    border: 1px solid #c8e6c9;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #e0e0e0;
}

.stat-item:last-child {
    border-bottom: none;
}

.stat-item.total {
    margin-top: 10px;
    padding-top: 15px;
    border-top: 2px solid #4CAF50;
}

.stat-label {
    font-size: 16px;
    color: #666;
    font-weight: 500;
}

.stat-value {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.stat-item.total .stat-value {
    color: #4CAF50;
    font-size: 22px;
}

.modal-footer {
    display: flex;
    gap: 15px;
    padding: 20px 30px;
    border-top: 1px solid #eee;
}

.modal-btn {
    flex: 1;
    padding: 15px 20px;
    border-radius: 10px;
    border: none;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.modal-btn.primary {
    background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
    color: white;
}

.modal-btn.primary:hover {
    background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(76, 175, 80, 0.4);
}

.modal-btn.secondary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.modal-btn.secondary:hover {
    background: linear-gradient(135deg, #764ba2 0%, #5f3a8f 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

/* Анимации модального окна */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(50px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ---------------------------------------------------- */
/* --- 6. Адаптация (Media Queries) --- */
/* ---------------------------------------------------- */

@media (max-width: 650px) {
    /* Агрессивная фиксация для мобильных устройств, чтобы гарантировать отсутствие прокрутки */
    html, body {
        position: fixed !important;
        overflow: hidden !important;
    }
    
    .container {
        padding: 15px;
        width: 100%;
        max-width: 100%;
        min-height: 100vh;
        border-radius: 0;
        box-shadow: none;
        overflow-y: hidden !important; 
        -webkit-overflow-scrolling: auto; 
    }
    
    h1 {
        font-size: 24px; 
    }
    
    .board {
        width: 90vw; 
        height: 90vw; 
        margin: 10px auto;
        font-size: 4vw; 
    }
    
    .controls {
        flex-direction: row;
        justify-content: space-around;
        gap: 5px; 
    }
    
    .controls > div {
        flex-grow: 2;
        gap: 5px; 
    }
    
    .controls button {
        flex-grow: 1;
        margin: 5px 0;
        font-size: 12px; 
        padding: 10px 8px;
        min-width: 80px;
    }
    
    .cell.pencil-mode {
        font-size: 1.8vw;
        padding: 1px;
    }

    .difficulty-buttons {
        grid-template-columns: repeat(auto-fit, minmax(50px, 1fr)); 
    }
    
    /* Адаптация модального окна */
    .modal-content { 
        width: 95%; 
        max-height: 80vh; 
    }
    
    .modal-header { padding: 15px 20px 10px; }
    .modal-header h2 { font-size: 24px; }
    .modal-body { padding: 20px; }
    .win-icon { font-size: 50px; }
    .modal-footer { 
        flex-direction: column; 
        gap: 10px; 
        padding: 15px 20px; 
    }
    .modal-btn { padding: 12px; }
    .stat-item { padding: 10px 0; }
    .stat-label { font-size: 14px; }
    .stat-value { font-size: 16px; }
    .stat-item.total .stat-value { font-size: 18px; }
}   

/* Агрессивная адаптация для очень узких экранов (<= 375px) */
@media (max-width: 375px) {
    .container {
        padding: 3px 3px; 
    }
    
    h1 {
        font-size: 20px; 
    }
    
    .board {
        width: 85vw; 
        height: 85vw; 
        margin: 5px auto; 
        font-size: 3.5vw; 
    }
    
    .controls {
        margin-bottom: 5px; 
        gap: 5px;
    }

    .controls button {
        font-size: 11px; 
        padding: 8px 6px;
        min-width: 60px;
    }
    
    .timer {
        font-size: 18px; 
        padding: 6px 10px;
        min-width: 70px;
    }
    
    .mobile-input-controls {
        margin-top: 5px; 
        padding: 5px;
    }

    .mobile-input-controls .number-buttons {
        gap: 5px; 
    }
    
    .mobile-input-controls button {
        padding: 12px 5px; 
        font-size: 18px;
    }
}

/* ---------------------------------------------------- */
/* --- 7. ЭКРАН ЗАГРУЗКИ --- */
/* ---------------------------------------------------- */

.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

.loading-content {
    text-align: center;
    color: white;
    padding: 30px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    max-width: 400px;
    width: 90%;
    animation: slideIn 0.5s ease-out;
}

.spinner {
    width: 60px;
    height: 60px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    margin: 0 auto 20px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading-title {
    font-size: 24px;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.loading-text {
    font-size: 16px;
    opacity: 0.8;
    margin: 0;
}