:root {
    --primary: #2196F3;
    --primary-dark: #1976D2;
    --header-bg: #FFF44F;
    --header-text: #333;
    --border-color: #e5e5e5;
    --text-color: #333;
    --bg-color: #f5f5f5;
    --table-bg-day: #f9ef92;
    --table-bg-night: #e0e0e0;
    --card-shadow: 0 2px 10px rgba(0,0,0,0.1);
    --btn-size: 40px;
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

* { 
    box-sizing: border-box; 
    -webkit-tap-highlight-color: transparent; 
}

body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    margin: 0; 
    padding: 0;
    background: #f5f5f5;
    color: #333;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100%;
}

/* ===== ШАПКА ===== */
header {
    background: white;
    padding: 6px 14px;
    padding-top: calc(6px + var(--safe-area-top));
    height: 44px;
    min-height: 44px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    flex-shrink: 0;
    z-index: 1000;
}

.header-left-block {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    flex-wrap: nowrap;
    width: 100%;
}

.header-buttons {
    display: flex;
    gap: 5px;
    align-items: center;
    flex-shrink: 0;
}

.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 30px;
    background: transparent;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s;
    user-select: none;
    color: #555;
    flex-shrink: 0;
    padding: 0 12px;
    font-weight: 500;
}

.icon-btn:hover { 
    background: #f0f0f0; 
}

.icon-btn:active { 
    transform: scale(0.97); 
}

.icon-btn.back { 
    font-size: 16px; 
    padding: 0 8px; 
}

.icon-btn.back svg { 
    transition: stroke 0.2s, transform 0.2s; 
}

.icon-btn.back:hover svg { 
    stroke: #2196F3; 
    transform: translateX(-2px); 
}

.icon-btn.refresh {
    color: #333;
    font-size: 18px;
    padding: 0 8px;
    transition: all 0.3s ease;
    position: relative;
}

.icon-btn.refresh svg {
    transition: stroke 0.3s ease, transform 0.8s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.icon-btn.refresh:hover svg {
    stroke: #2196F3;
    transform: rotate(180deg);
}

.icon-btn.refresh:active svg {
    transform: rotate(360deg);
}

.user-info-block {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    margin-left: auto;
}

.user-display {
    font-size: 12px;
    color: #555;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    padding: 4px 12px;
    border-radius: 6px;
    border: 1px solid #ddd;
    background: #f5f5f5;
    transition: all 0.15s;
    height: 30px;
    display: flex;
    align-items: center;
}

.user-display:hover { 
    background: #e8e8e8; 
    border-color: #bbb; 
}

.user-display:active { 
    transform: scale(0.97); 
}

.user-menu {
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    min-width: 140px;
    padding: 4px 0;
    z-index: 9999;
}

.user-menu .menu-item {
    padding: 8px 16px;
    cursor: pointer;
    font-size: 13px;
    color: #333;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.15s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}

.user-menu .menu-item:hover { 
    background: #f0f0f0; 
}

.user-menu .menu-item.logout { 
    color: #d32f2f; 
}

.user-menu .menu-item.logout:hover { 
    background: #ffebee; 
}

/* ===== ВКЛАДКИ ===== */
.tabs-container {
    background: white;
    border-bottom: 1px solid #eee;
    padding: 5px 10px 6px 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex-shrink: 0;
    z-index: 10;
}

.tab-row-wrapper {
    display: flex;
    align-items: center;
    gap: 5px;
    position: relative;
}

#machines-wrapper .tab-scroll-btn {
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    width: 24px;
    height: 26px;
    display: none !important;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 12px;
    color: #888;
    flex-shrink: 0;
    user-select: none;
    transition: opacity 0.2s;
}

#machines-wrapper .tab-scroll-btn:hover { 
    background: #f5f5f5; 
    border-color: #bbb; 
}

#machines-wrapper .tab-scroll-btn:active { 
    background: #e0e0e0; 
}

#machines-wrapper .tab-scroll-btn.visible {
    display: flex !important;
}

#machines-wrapper .tab-scroll-btn.disabled {
    opacity: 0.3;
    pointer-events: none;
}

.tab-row {
    display: flex;
    align-items: center;
    gap: 5px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 2px 0;
    flex: 1;
}

.tab-row::-webkit-scrollbar { 
    display: none; 
}

.tab-item {
    padding: 0 14px;
    background: #f5f5f5;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    font-size: 12px;
    height: 26px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    white-space: nowrap;
    color: #666;
    transition: all 0.2s;
    flex-shrink: 0;
    user-select: none;
    font-weight: 500;
}

.tab-item:hover { 
    background: #eee; 
    border-color: #ccc; 
}

.tab-item.active {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
    font-weight: 600;
}

.tab-item.dragging { 
    opacity: 0.5; 
    cursor: grabbing; 
}

.tab-item.drag-over { 
    border-color: var(--primary); 
    background: #e6f0ff; 
}

/* ===== ОСНОВНАЯ ТАБЛИЦА ===== */
main {
    flex: 1;
    overflow: hidden;
    padding: 0;
    background: #f5f5f5;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.table-wrapper {
    background: white;
    border-radius: 10px;
    box-shadow: var(--card-shadow);
    border: 1px solid var(--border-color);
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    min-height: 0;
    margin: 6px;
    margin-top: 3px;
    scrollbar-width: thin;
    scrollbar-color: rgba(0,0,0,0.15) transparent;
}

.table-wrapper::-webkit-scrollbar {
    width: 5px;
    height: 5px;
    background: transparent;
}

.table-wrapper::-webkit-scrollbar-track { 
    background: transparent; 
}

.table-wrapper::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.15);
    border-radius: 3px;
    transition: background 0.3s;
}

.table-wrapper::-webkit-scrollbar-thumb:hover { 
    background: rgba(0,0,0,0.3); 
}

.table-wrapper::-webkit-scrollbar-corner { 
    background: transparent; 
}

table {
    border-collapse: collapse;
    width: 100%;
    font-size: 12px;
    table-layout: fixed;
    min-width: 750px;
}

thead th {
    position: sticky;
    top: 0;
    z-index: 30;
    background: var(--header-bg);
    color: var(--header-text);
    padding: 6px 4px;
    font-size: 11px;
    font-weight: 600;
    text-align: center !important;
    vertical-align: middle;
    white-space: normal;
    word-wrap: break-word;
    hyphens: auto;
    line-height: 1.3;
    border: 1px solid #d0d0d0;
    border-top: 1px solid var(--header-bg);
    border-bottom: 2px solid #ccc;
    min-height: 34px;
}

thead th:first-child { 
    border-left: 1px solid #d0d0d0; 
}

thead th:last-child { 
    border-right: 1px solid #d0d0d0; 
}

thead { 
    z-index: 31; 
}

thead th { 
    z-index: 31; 
}

.date-filter-wrapper, .fio-filter-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0px;
    width: 100%;
    height: 100%;
}

.date-filter-wrapper span, .fio-filter-wrapper span {
    display: inline-block;
    text-align: center;
}

.filter-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 10px;
    padding: 0 3px;
    color: #555;
    border-radius: 3px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    line-height: 1;
    margin-left: 1px;
}

.filter-btn:hover { 
    background: rgba(0,0,0,0.1); 
}

.filter-btn.active { 
    background: rgba(33, 150, 243, 0.2); 
    color: var(--primary); 
}

/* ===== ВЫПАДАЮЩИЕ МЕНЮ ФИЛЬТРОВ ===== */
#dateFilterDropdown, #fioFilterDropdown {
    display: none;
    position: fixed;
    border: 1px solid #ccc;
    background: white;
    box-shadow: 0 8px 16px rgba(0,0,0,0.2);
    border-radius: 6px;
    padding: 6px 0;
    min-width: 200px;
    max-height: 400px;
    overflow-y: auto;
    z-index: 99999;
}

#dateFilterDropdown.open, #fioFilterDropdown.open { 
    display: block; 
}

.tree-node { 
    user-select: none; 
    padding: 0; 
}

.tree-node-header {
    padding: 5px 12px;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 4px;
    color: #333;
    transition: background 0.1s;
    border-bottom: 1px solid #f5f5f5;
}

.tree-node-header:hover { 
    background: #f0f0f0; 
}

.tree-node-toggle { 
    font-size: 10px; 
    color: #888; 
    width: 14px; 
    text-align: center; 
    flex-shrink: 0; 
}

.tree-node-header .check-wrapper { 
    display: flex; 
    align-items: center; 
    gap: 4px; 
    flex: 1; 
    cursor: pointer; 
}

.tree-node-header input[type="checkbox"] { 
    margin: 0; 
    transform: scale(1.1); 
    pointer-events: none; 
}

.tree-children { 
    display: none; 
    padding-left: 16px; 
}

.tree-children.open { 
    display: block; 
}

.tree-leaf {
    padding: 4px 12px 4px 10px;
    cursor: pointer;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    border-bottom: 1px solid #fafafa;
}

.tree-leaf:hover { 
    background: #f5f9ff; 
}

.tree-leaf input[type="checkbox"] { 
    margin: 0; 
    transform: scale(1.1); 
    pointer-events: none; 
}

.filter-actions {
    padding: 5px 12px;
    border-top: 1px solid #eee;
    display: flex;
    gap: 6px;
    justify-content: flex-end;
    background: #fafafa;
    position: sticky;
    bottom: 0;
}

.filter-actions button {
    padding: 5px 12px;
    border: 1px solid #ccc;
    background: white;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.filter-actions button:hover { 
    background: #f0f0f0; 
}

.filter-actions button.clear-btn { 
    color: #d32f2f; 
}

.filter-actions button.clear-btn:hover { 
    background: #ffebee; 
}

/* ===== ЯЧЕЙКИ ТАБЛИЦЫ ===== */
td {
    padding: 3px 4px;
    border: 1px solid var(--border-color);
    text-align: center;
    vertical-align: middle;
    height: 28px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 12px;
    transition: background 0.15s;
    position: relative;
}

td[title] { 
    cursor: help; 
}

tbody tr:hover td { 
    filter: brightness(0.97); 
}

td.col-left { 
    text-align: left !important; 
    padding-left: 5px; 
}

#js-hover-frame {
    position: absolute;
    border: 1px solid #ff1744;
    pointer-events: none;
    z-index: 25;
    display: none;
    box-sizing: border-box;
    background-color: rgba(255, 23, 68, 0.03);
    transition: none;
    left: 0;
    top: 0;
}

.w-num {
    overflow: visible !important;
    text-overflow: clip !important;
    font-size: 11px;
    padding: 2px 2px;
    white-space: nowrap;
}

.deviation-negative { 
    color: #d32f2f !important; 
    font-weight: 600; 
}

.deviation-positive { 
    color: #1b5e20 !important; 
    font-weight: 600; 
}

.deviation-zero { 
    color: #333 !important; 
}

.shift-day td.shift-bg, .shift-day td.date-col, .shift-day td.gos-col, 
.shift-day td.fio-col, .shift-day td.shift-col, .shift-day td.piecework-col, 
.shift-day td.roadworks-col { 
    background-color: var(--table-bg-day); 
}

.shift-night td.shift-bg, .shift-night td.date-col, .shift-night td.gos-col, 
.shift-night td.fio-col, .shift-night td.shift-col, .shift-night td.piecework-col, 
.shift-night td.roadworks-col { 
    background-color: var(--table-bg-night); 
}

.tral-group.group-top td { 
    border-top: 2px solid #ccc !important; 
}

.tral-group.group-bottom td { 
    border-bottom: 2px solid #ccc !important; 
}

.tral-group:not(.group-top) td { 
    border-top: 1px solid #e5e5e5 !important; 
}

.cargo-col { 
    background-color: transparent !important; 
    border-top: 1px solid #e5e5e5 !important; 
    border-bottom: 1px solid #e5e5e5 !important; 
}

td.empty { 
    background-color: transparent !important; 
}

td[rowspan] { 
    border-bottom: 1px solid #e5e5e5 !important; 
}

/* ===== ШИРИНА КОЛОНОК ===== */
.w-date { 
    width: 76px; 
    min-width: 76px; 
    max-width: 76px; 
}

.w-gos { 
    width: 54px; 
    min-width: 54px; 
    max-width: 54px; 
}

.w-fio { 
    width: 110px; 
    min-width: 90px; 
}

.w-num { 
    width: 60px; 
    min-width: 50px; 
    max-width: 74px; 
}

.w-refueler { 
    width: 78px; 
    min-width: 64px; 
}

.w-note { 
    width: 130px; 
    min-width: 80px; 
    max-width: 130px; 
}

.w-shift { 
    width: 48px; 
    min-width: 48px; 
    max-width: 48px; 
}

.w-object { 
    width: 110px; 
    min-width: 74px; 
}

/* ===== СОСТОЯНИЯ ===== */
.loading-state, .error-state { 
    text-align: center; 
    padding: 30px; 
    color: #888; 
    font-size: 13px; 
}

.error-state { 
    color: #d32f2f; 
}

/* ===== TOAST ===== */
#toastNotification {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 13px;
    z-index: 99999;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    opacity: 0;
    transition: opacity 0.5s;
    max-width: 90%;
    text-align: center;
    pointer-events: none;
}

#toastNotification.show { 
    opacity: 1; 
}

#toastNotification.hide { 
    opacity: 0; 
}

#toastNotification.success { 
    background: #333; 
}

#toastNotification.info { 
    background: #2196F3; 
}

#toastNotification.error { 
    background: #d32f2f; 
}

/* ===== КНОПКА ЗАГРУЗКИ ===== */
#btnLoadMoreDynamic .btn-inner {
    display: inline-block;
    background: var(--primary);
    color: white;
    border: none;
    padding: 8px 20px;
    border-radius: 8px !important;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    pointer-events: auto;
    box-shadow: 0 2px 10px rgba(33, 150, 243, 0.3);
    transition: background 0.2s, transform 0.2s, box-shadow 0.2s;
}

#btnLoadMoreDynamic .btn-inner:hover {
    background: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(33, 150, 243, 0.4);
}

#btnLoadMoreDynamic .btn-inner.loading {
    background: #90a4ae;
    pointer-events: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    border-radius: 8px !important;
}

#btnLoadMoreDynamic {
    z-index: 99999 !important;
    visibility: hidden;
}

/* ===== МОДАЛЬНОЕ ОКНО ЭКСПОРТА ===== */
.export-modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 999999;
    animation: fadeIn 0.2s ease;
}

.export-modal {
    background: white;
    border-radius: 12px;
    padding: 24px 28px 28px 28px;
    max-width: 420px;
    width: 94%;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    animation: slideUp 0.25s ease;
}

.export-modal h3 {
    margin: 0 0 16px 0;
    font-size: 18px;
    color: #222;
}

.export-modal .field-group {
    margin-bottom: 14px;
}

.export-modal .field-group label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: #555;
    margin-bottom: 4px;
}

.export-modal .field-group select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 13px;
    background: white;
    color: #333;
    min-height: 38px;
}

.export-modal .field-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(33,150,243,0.15);
}

.export-modal .period-row {
    display: flex;
    gap: 10px;
}

.export-modal .period-row select {
    flex: 1;
}

.export-modal .checkbox-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 6px 0 12px 0;
}

.export-modal .checkbox-row input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.export-modal .checkbox-row label {
    font-size: 13px;
    color: #444;
    cursor: pointer;
}

.export-modal .btn-row {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 18px;
    padding-top: 14px;
    border-top: 2px solid #f0f0f0;
}

.export-modal .btn-row button {
    padding: 10px 28px;
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid #ddd;
    background: white;
    color: #333;
}

.export-modal .btn-row button:hover { 
    background: #f0f0f0; 
}

.export-modal .btn-row .btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.export-modal .btn-row .btn-primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
}

.export-modal .btn-row .btn-primary:disabled {
    background: #90a4ae;
    border-color: #90a4ae;
    cursor: not-allowed;
}

.export-modal .progress-container {
    margin-top: 12px;
    display: none;
}

.export-modal .progress-container.active {
    display: block;
}

.export-modal .progress-container .progress-bar {
    width: 100%;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    overflow: hidden;
    margin-top: 4px;
}

.export-modal .progress-container .progress-bar .progress-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    width: 0%;
    transition: width 0.3s ease;
}

.export-modal .progress-container .progress-text {
    font-size: 11px;
    color: #666;
    margin-top: 2px;
}

/* ===== АНИМАЦИИ ===== */
@keyframes fadeIn { 
    from { opacity: 0; } 
    to { opacity: 1; } 
}

@keyframes slideUp { 
    from { transform: translateY(20px); opacity: 0; } 
    to { transform: translateY(0); opacity: 1; } 
}

/* ===== АДАПТИВ ===== */
@media (max-width: 768px) {
    header { 
        padding: 4px 8px; 
        padding-top: calc(4px + var(--safe-area-top)); 
        height: auto; 
        min-height: 40px; 
    }
    
    .icon-btn { 
        height: 28px; 
        font-size: 12px; 
        padding: 0 8px; 
    }
    
    .icon-btn.back { 
        font-size: 14px; 
        padding: 0 6px; 
    }
    
    .icon-btn.refresh { 
        font-size: 16px; 
        padding: 0 6px; 
    }
    
    .user-display { 
        font-size: 11px; 
        height: 26px; 
        padding: 3px 10px; 
    }
    
    .tabs-container { 
        padding: 4px 6px 5px 6px; 
    }
    
    .tab-item { 
        font-size: 10px; 
        padding: 0 10px; 
        height: 22px; 
    }
    
    .tab-row { 
        gap: 4px; 
    }
    
    #machines-wrapper .tab-scroll-btn { 
        width: 20px; 
        height: 22px; 
        font-size: 11px; 
    }
    
    main { 
        padding: 0; 
    }
    
    .table-wrapper { 
        margin: 0 6px 6px 6px; 
        margin-top: 2px; 
        border-radius: 4px; 
    }
    
    td { 
        font-size: 10px; 
        padding: 2px 3px; 
        height: 22px; 
    }
    
    td.w-num { 
        font-size: 10px; 
        padding: 2px 2px; 
    }
    
    thead th { 
        font-size: 9px; 
        padding: 3px 3px; 
        min-height: 28px; 
    }
    
    .w-date { 
        width: 54px; 
        min-width: 54px; 
    }
    
    .w-gos { 
        width: 36px; 
        min-width: 36px; 
    }
    
    .w-fio { 
        width: 68px; 
        min-width: 54px; 
    }
    
    .w-num { 
        width: 44px; 
        min-width: 36px; 
        max-width: 54px; 
    }
    
    .w-refueler { 
        width: 50px; 
        min-width: 40px; 
    }
    
    .w-note { 
        width: 58px; 
        min-width: 44px; 
        max-width: 58px; 
    }
    
    table { 
        min-width: 540px; 
    }
    
    #toastNotification { 
        font-size: 12px; 
        padding: 8px 16px; 
        bottom: 16px; 
        max-width: 90%; 
        border-radius: 6px; 
    }
    
    #btnLoadMoreDynamic .btn-inner { 
        font-size: 11px !important; 
        padding: 6px 14px !important; 
        border-radius: 8px !important; 
    }
    
    .table-wrapper::-webkit-scrollbar { 
        width: 3px; 
        height: 3px; 
    }
    
    .export-modal { 
        padding: 18px 16px 20px 16px; 
    }
    
    .export-modal .period-row { 
        flex-direction: column; 
        gap: 6px; 
    }

    #dateFilterDropdown, #fioFilterDropdown {
        position: fixed;
        top: 50% !important; 
        left: 50% !important;
        transform: translate(-50%, -50%);
        width: 90%; 
        max-width: 400px; 
        max-height: 70vh;
        box-shadow: 0 10px 30px rgba(0,0,0,0.3);
        border-radius: 12px;
        z-index: 99999;
        min-width: auto;
    }
    
    body.filter-open::after {
        content: ''; 
        position: fixed; 
        top: 0; 
        left: 0; 
        right: 0; 
        bottom: 0;
        background: rgba(0,0,0,0.5); 
        z-index: 99998;
    }
}