/* ================================================================= */
/* ⏱️ OSNOVA TIMER MODULE (ISOLATED & NAMESPACED)                    */
/* ================================================================= */

/* --- 1. GLOBAL HELPERS (Body State) --- */

/* Блокировка скролла только когда таймер активен */
body.timer-fullscreen-mode {
    overflow: hidden !important;
    touch-action: none;
    overscroll-behavior: none;
}

/* --- 2. MAIN SCREEN CONTAINER --- */

#screen-timer {
    display: none !important; /* <--- ДОБАВЛЕНО !important */
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100dvh;
    background: var(--bg);
    z-index: 9000;
    /* Сброс внешних трансформаций приложения */
    zoom: 1 !important;
    transform: none !important;
    flex-direction: column;
    justify-content: space-between;
    padding-bottom: env(safe-area-inset-bottom);
    overflow: hidden;
    box-sizing: border-box;
}

    #screen-timer.active {
        display: flex !important;
    }

    /* Все элементы внутри таймера наследуют box-sizing */
    #screen-timer *,
    #screen-timer *::before,
    #screen-timer *::after {
        box-sizing: border-box;
    }

    /* --- 3. HEADER ZONE (Scoped to #screen-timer) --- */

    #screen-timer .timer-top-bar {
        position: relative;
        width: 100%;
        padding-top: max(10px, env(safe-area-inset-top));
        padding-bottom: 10px;
        z-index: 9500;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 15px;
        background: linear-gradient(to bottom, var(--bg) 60%, transparent);
        pointer-events: none;
    }

    #screen-timer .timer-close-zone {
        pointer-events: auto;
        cursor: pointer;
        display: flex;
        justify-content: center;
        width: 100%;
        padding: 5px 0;
    }

    #screen-timer .timer-close-pill {
        width: 60px;
        height: 24px;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--text-sec);
        box-shadow: 0 2px 8px rgba(0,0,0,0.2);
        transition: transform 0.1s;
    }

        #screen-timer .timer-close-pill:active {
            transform: scale(0.95);
        }

    /* Tabs */
    #screen-timer .timer-tabs-container {
        pointer-events: auto;
        display: flex;
        justify-content: center;
    }

    #screen-timer .timer-tabs-pill {
        display: flex;
        background: var(--surface);
        padding: 4px;
        border-radius: 20px;
        border: 1px solid var(--border);
        gap: 2px;
    }

    #screen-timer .timer-tab {
        padding: 8px 16px;
        font-size: 11px;
        font-weight: 700;
        color: var(--text-sec);
        border-radius: 16px;
        cursor: pointer;
        transition: all 0.2s;
        user-select: none;
        white-space: nowrap;
    }

        #screen-timer .timer-tab.active {
            background: var(--primary);
            color: white;
        }

    /* --- 4. DISPLAY ZONE (Scoped to #screen-timer) --- */

    #screen-timer .timer-main-display {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 100%;
        position: relative;
        z-index: 9100;
        pointer-events: none;
    }

    /* Таймер: Центровка и размер */
    #screen-timer #timer-display {
        order: 1;
        font-size: calc(var(--timer-font-scale, 22) * 1vmin);
        font-weight: 700;
        line-height: 1;
        color: var(--text);
        font-variant-numeric: tabular-nums;
        text-shadow: 0 5px 30px rgba(0,0,0,0.2);
        white-space: nowrap;
        margin: 0;
        margin-bottom: 5px;
        transition: font-size 0.1s ease-out;
    }

    /* Раунды: Позиция под таймером */
    #screen-timer #timer-rounds-display {
        order: 2;
        display: flex;
        align-items: baseline;
        justify-content: center;
        gap: 4px;
        margin-top: 10px;
        margin-bottom: 0;
        background: rgba(255, 255, 255, 0.05);
        padding: 8px 20px;
        border-radius: 16px;
        border: 1px solid rgba(255, 255, 255, 0.1);
    }

    #screen-timer .round-curr {
        font-size: 8vmin;
        line-height: 1;
        font-weight: 800;
        color: var(--primary);
    }

    #screen-timer .round-sep {
        font-size: 5vmin;
        color: var(--text-sec);
        opacity: 0.5;
    }

    #screen-timer .round-total {
        font-size: 5vmin;
        color: var(--text-sec);
        font-weight: 800;
        opacity: 0.5;
    }

    /* LAPS (Отсечки): Список */
    #screen-timer #timer-laps-container {
        position: relative;
        top: auto;
        left: auto;
        transform: none;
        order: 3;
        width: 60%;
        margin: 20px auto 0 auto;
        max-height: 25vh;
        overflow-y: auto;
        z-index: 9200;
        pointer-events: auto;
        scrollbar-width: none;
    }

        #screen-timer #timer-laps-container::-webkit-scrollbar {
            display: none;
        }

    #screen-timer #timer-laps-list {
        display: flex;
        flex-direction: column;
        gap: 4px;
        align-items: center;
    }

    #screen-timer .lap-item {
        display: flex;
        align-items: center;
        gap: 10px;
        background: rgba(0,0,0,0.5);
        padding: 4px 12px;
        border-radius: 12px;
        backdrop-filter: blur(4px);
        border: 1px solid rgba(255,255,255,0.1);
        animation: slideDown 0.3s ease-out;
    }

    #screen-timer .lap-idx {
        font-size: 10px;
        color: var(--text-sec);
        width: 20px;
    }

    #screen-timer .lap-split {
        font-size: 11px;
        color: var(--primary);
        font-family: 'Courier Prime', monospace;
    }

    #screen-timer .lap-time {
        font-size: 14px;
        font-weight: bold;
        color: white;
        font-family: 'Oswald', sans-serif;
    }

    /* --- 5. CONTROLS (Scoped to #screen-timer) --- */

    #screen-timer .timer-bottom-controls {
        flex-shrink: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 15px;
        padding: 10px 20px 40px 20px;
        width: 100%;
        max-width: 380px;
        margin: 0 auto;
        z-index: 9600;
        background: var(--bg);
        pointer-events: auto;
    }

    #screen-timer .timer-center-stack {
        display: flex;
        align-items: center;
        gap: 15px;
    }

    #screen-timer .timer-ctrl-btn {
        border-radius: 50%;
        border: none;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: transform 0.1s;
        position: relative;
        outline: none;
        -webkit-tap-highlight-color: transparent;
        flex-shrink: 0;
    }

        #screen-timer .timer-ctrl-btn:active {
            transform: scale(0.92);
        }

        /* Primary (Play) */
        #screen-timer .timer-ctrl-btn.primary {
            width: 80px;
            height: 80px;
            background: var(--primary);
            color: white;
            box-shadow: 0 10px 30px rgba(145, 71, 255, 0.4);
        }

            #screen-timer .timer-ctrl-btn.primary.active {
                background: var(--surface);
                border: 2px solid var(--border);
                color: var(--text);
                box-shadow: none;
            }

        /* Secondary (Reset, Settings, Lap) */
        #screen-timer .timer-ctrl-btn.secondary {
            width: 56px;
            height: 56px;
            background: var(--surface);
            border: 1px solid var(--border);
            color: var(--text-sec);
        }

            #screen-timer .timer-ctrl-btn.secondary:hover {
                color: var(--text);
                border-color: var(--text);
            }

    /* Lap Specific */
    #screen-timer #timer-btn-lap:disabled {
        opacity: 0.3;
        pointer-events: none;
    }

    #screen-timer #timer-btn-lap:active {
        background: var(--primary) !important;
        color: white !important;
    }

    /* Video Button */
    #screen-timer .timer-video-btn {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: #29B6F6;
        color: white;
        border: none;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 4px 15px rgba(41, 182, 246, 0.4);
        cursor: pointer;
        transition: all 0.2s;
    }

        #screen-timer .timer-video-btn:active {
            transform: scale(0.95);
        }

        #screen-timer .timer-video-btn.active-mode {
            background: rgba(255,255,255,0.15);
            border: 1px solid rgba(255,255,255,0.3);
            box-shadow: none;
            backdrop-filter: blur(5px);
        }

        #screen-timer .timer-video-btn.recording {
            background: #FF1744;
            box-shadow: 0 0 20px rgba(255, 23, 68, 0.6);
            animation: pulse-red 2s infinite;
        }

/* --- 6. VIDEO LAYER & ELEMENTS (Global IDs) --- */

#timer-video-layer {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    z-index: 9050;
    background: #000;
    display: none;
}

    /* --- FIX: Добавляем этот класс --- */
    #timer-video-layer.sensor-mode-hidden {
        opacity: 0 !important; /* Делаем полностью прозрачным */
        background: transparent !important;
        pointer-events: none !important; /* Ключевое: пропускаем клики и скролл сквозь слой */
    }

#wod-canvas {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#wod-rec-indicator {
    position: absolute;
    top: max(20px, env(safe-area-inset-top));
    left: 20px;
    display: none;
    align-items: center;
    gap: 8px;
    z-index: 9600;
    background: rgba(0,0,0,0.6);
    padding: 6px 12px;
    border-radius: 20px;
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255,255,255,0.1);
}

#video-mode-close-btn {
    position: absolute;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(0,0,0,0.5);
    color: white;
    border: 1px solid rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 9600;
}

.rec-dot {
    width: 10px;
    height: 10px;
    background: #FF1744;
    border-radius: 50%;
    animation: blink 1s infinite;
}

/* STATE: Recording Active (Hides UI) */
body.video-recording-active #screen-timer .timer-main-display,
body.video-recording-active #screen-timer .timer-top-bar {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

body.video-recording-active #screen-timer .timer-bottom-controls {
    background: transparent !important;
    padding-bottom: max(30px, env(safe-area-inset-bottom));
}

body.video-recording-active #screen-timer .timer-ctrl-btn.secondary {
    opacity: 0;
    pointer-events: none;
}

/* --- 7. SETTINGS MODAL (Scoped to ID) --- */

#modal-timer-setup.timer-modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100dvh;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    z-index: 20000;
    align-items: flex-end;
    justify-content: center;
    zoom: 1 !important;
}

#modal-timer-setup .timer-modal-card.compact-mode {
    background: var(--surface);
    width: 100%;
    max-width: 420px;
    border-radius: 24px 24px 0 0;
    padding: 20px;
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    max-height: 85vh;
    overflow-y: auto;
    border-top: 1px solid var(--border);
    animation: slideUp 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
    box-shadow: 0 -10px 40px rgba(0,0,0,0.6);
}

#modal-timer-setup .timer-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

/* Settings Controls */
#modal-timer-setup .settings-group.compact {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    padding: 10px 12px;
    margin-bottom: 10px;
}

#modal-timer-setup .stepper-wrapper {
    display: flex;
    align-items: center;
    background: rgba(0,0,0,0.3);
    border-radius: 8px;
    padding: 3px;
    border: 1px solid rgba(255,255,255,0.1);
}

#modal-timer-setup .stepper-btn {
    width: 32px;
    height: 32px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-weight: bold;
    font-size: 16px;
}

#modal-timer-setup .stepper-val {
    width: 40px;
    text-align: center;
    background: transparent;
    border: none;
    color: var(--text);
    font-weight: 800;
    font-size: 16px;
    margin: 0;
    padding: 0;
    outline: none;
}

/* --- ВРЕМЯ: Контейнеры и Поля (Updated Layout) --- */

/* Контейнер строки (сближаем элементы) */
.time-input-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2px;
    width: 100%;
}

/* Разделитель (Двоеточие) */
.time-sep {
    font-size: 32px;
    font-weight: 300;
    color: var(--text-sec);
    line-height: 1;
    margin: 0 2px;
    padding-bottom: 6px;
    flex-shrink: 0;
}

/* Квадратные и широкие поля ввода */
#modal-timer-setup .time-input-compact {
    background: transparent !important;
    border: none !important;
    color: var(--text) !important;
    font-size: 20px !important;
    font-weight: 700 !important;
    width: 32px !important;
    text-align: center !important;
    padding: 0 !important;
    margin: 0 !important;
    outline: none;
    -webkit-appearance: none;
}

    #modal-timer-setup .time-input-compact.lg {
        /* Геометрия квадрата */
        width: 120px !important;
        height: 64px !important;
        border-radius: 12px !important;
        /* Текст */
        font-size: 36px !important;
        font-weight: 600 !important;
        text-align: center !important;
        /* Визуальный стиль */
        background: rgba(255, 255, 255, 0.05) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        color: var(--text) !important;
        /* Сброс */
        padding: 0 !important;
        margin: 0 !important;
        outline: none;
        -moz-appearance: textfield;
    }

        #modal-timer-setup .time-input-compact.lg::-webkit-outer-spin-button,
        #modal-timer-setup .time-input-compact.lg::-webkit-inner-spin-button {
            -webkit-appearance: none;
            margin: 0;
        }

        #modal-timer-setup .time-input-compact.lg:focus {
            background: rgba(255, 255, 255, 0.1) !important;
            border-color: var(--primary) !important;
            box-shadow: 0 0 0 2px rgba(145, 71, 255, 0.2);
        }

/* Work/Rest Cards */
#modal-timer-setup .wr-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    padding: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

    #modal-timer-setup .wr-card.work {
        border-color: rgba(0, 230, 118, 0.3);
        background: rgba(0, 230, 118, 0.05);
    }

    #modal-timer-setup .wr-card.rest {
        border-color: rgba(255, 152, 0, 0.3);
        background: rgba(255, 152, 0, 0.05);
    }

    #modal-timer-setup .wr-card.cap {
        border-color: rgba(41, 182, 246, 0.3);
        background: rgba(41, 182, 246, 0.05);
    }

#modal-timer-setup .wr-label {
    font-size: 10px;
    font-weight: 800;
    margin-bottom: 2px;
    letter-spacing: 1px;
    text-transform: uppercase;
}

#modal-timer-setup .wr-card.work .wr-label {
    color: #00E676;
}

#modal-timer-setup .wr-card.rest .wr-label {
    color: #FF9800;
}

#modal-timer-setup .wr-card.cap .wr-label {
    color: #29B6F6;
}

/* Unified Controls Scoped */
#modal-timer-setup .bottom-controls-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 15px;
}

#modal-timer-setup .input-with-icon,
#modal-timer-setup .select-with-icon {
    position: relative;
    width: 100%;
    height: 42px;
}

    /* Slider Fix */
    #modal-timer-setup .input-with-icon:has(input[type="range"]) {
        display: flex !important;
        align-items: center !important;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 10px;
        overflow: hidden;
    }

#modal-timer-setup input[type=range] {
    margin: 0 0 0 40px !important;
    width: calc(100% - 50px) !important;
    height: 100% !important;
    cursor: pointer;
    background: transparent;
}

/* Icons */
#modal-timer-setup .input-with-icon .icon,
#modal-timer-setup .select-with-icon .icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 20px !important;
    color: var(--text-sec);
    pointer-events: none;
    z-index: 2;
    line-height: 1;
}

/* Button & Selects */
#modal-timer-setup .settings-btn-unified {
    width: 100%;
    height: 42px !important;
    padding: 0 12px 0 40px !important;
    margin: 0 !important;
    background: rgba(255, 255, 255, 0.05) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    border-radius: 10px !important;
    color: var(--text) !important;
    font-family: var(--font);
    font-size: 13px !important;
    font-weight: 600 !important;
    outline: none;
    transition: all 0.2s;
    -webkit-appearance: none;
    appearance: none;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    white-space: nowrap;
    position: relative;
}

#modal-timer-setup select.settings-btn-unified {
    cursor: pointer;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23666'%3e%3cpath d='M7 10l5 5 5-5z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 24px;
    padding-right: 30px !important;
}

#modal-timer-setup .settings-btn-unified:focus {
    border-color: var(--primary) !important;
    background: rgba(145, 71, 255, 0.15) !important;
}

/* Mute Button Specifics */
#setting-btn-mute .icon {
    position: absolute !important;
    left: 12px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    margin: 0 !important;
    font-size: 20px !important;
    color: var(--text-sec);
}

#setting-btn-mute span:not(.icon) {
    line-height: 1;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* --- 8. PREVIEW MODAL --- */
#modal-video-preview.modal-overlay-fixed {
    display: none;
    z-index: 21000;
}

/* --- 9. ANIMATIONS --- */
@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse-red {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 23, 68, 0.7);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(255, 23, 68, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(255, 23, 68, 0);
    }
}

@keyframes blink {
    50% {
        opacity: 0.3;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* ================================================================= */
/* 🚫 LANDSCAPE BLOCKER (Mobile Only)                                */
/* ================================================================= */
#landscape-blocker {
    display: none;
}

@media (orientation: landscape) and (max-height: 600px) {
    #screen-timer {
        display: none !important;
    }

    #landscape-blocker {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: var(--bg);
        z-index: 20000;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: var(--text);
    }
}

/* Если модальное окно видео открыто (style="display: flex"), скрываем блокировщик */
#modal-video-player[style*="display: flex"] ~ #landscape-blocker,
#modal-video-player[style*="display:flex"] ~ #landscape-blocker {
    display: none !important;
}