/**
 * GŁÓWNY ARKUSZ STYLÓW APLIKACJI SERVICE DESK
 * * Spis treści:
 * 1. Zmienne globalne i Reset (Design System)
 * 2. Główny Layout (Flexbox Architecture)
 * 3. Komponent Tabeli (Fixed Layout Strategy)
 * 4. Komponenty UI i Modale
 * 5. System Zakładek (Tab System)
 * 6. Responsywność (Mobile Breakpoints)
 * 7. Style wydruku (Print Media)
 */

/* ==========================================================================
   1. ZMIENNE GLOBALNE I RESET
   Definiuje paletę kolorów, typografię oraz podstawowe zachowanie box-modelu.
   ========================================================================== */
:root {
    /* Paleta kolorów (Brand & UI) */
    --primary-blue: #3b82f6;
    --primary-blue-hover: #2563eb;
    
    /* Tła i Obramowania */
    --light-gray-bg: #f4f6f8;
    --medium-gray-bg: #e9ecef; 
    --gray-border: #dee2e6; 
    
    /* Typografia */
    --text-dark: #1f2937;
    --text-light: #6b7280;
    --white: #ffffff;
    
    /* Kolory statusów (Semantic colors) */
    --danger-red: #ef4444;
    --status-nieprzeczytane: #ffc107; 
    --status-otwarte: #28a745; 
    --status-zamknięte: #6c757d; 
    
    /* Metryki */
    --border-radius: 8px;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

/* Globalny reset box-sizing dla przewidywalnego wymiarowania elementów */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Konfiguracja głównego widoku: blokada scrolla na body, scrollowanie wewnątrz kontenerów */
body {
    font-family: 'Inter', sans-serif;
    background-color: var(--light-gray-bg);
    color: var(--text-dark);
    margin: 0;
    padding: 0;
    height: 100vh;
    overflow: hidden; /* Zapobiega podwójnym paskom przewijania */
}

a {
    text-decoration: none;
    color: var(--primary-blue);
    transition: color 0.2s;
}

a:hover {
    color: var(--primary-blue-hover);
}

.hidden {
    display: none !important;
}

/* ==========================================================================
   2. GŁÓWNY UKŁAD (LAYOUT)
   Struktura oparta na Flexbox: Header (góra) + Body (dół), gdzie Body dzieli się
   na Sidebar (lewo) i Main Content (prawo).
   ========================================================================== */
.app-layout {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* --- Header --- */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    background-color: var(--white);
    border-bottom: 1px solid var(--gray-border);
    flex-shrink: 0; /* Zapobiega zmniejszaniu nagłówka przy braku miejsca */
    z-index: 20;
}

.header-title {
    font-size: 18px;
    font-weight: 600;
}

.header-user-info {
    display: flex;
    align-items: center;
    gap: 16px;
}

.btn-logout {
    background-color: transparent;
    border: 1px solid var(--gray-border);
    color: var(--text-dark);
    padding: 6px 12px;
    border-radius: var(--border-radius);
    cursor: pointer;
    font-weight: 500;
    font-size: 13px;
}
.btn-logout:hover { background-color: var(--light-gray-bg); }

/* --- Body Container --- */
.app-body {
    display: flex;
    flex-grow: 1;
    overflow: hidden;
    position: relative;
}

/* Kontener centrujący (używany np. przy ekranie logowania lub błędach) */
.app-body-centered {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-grow: 1;
    overflow-y: auto;
}

/* --- Sidebar (Nawigacja boczna) --- */
.sidebar {
    width: 220px;
    background-color: var(--white);
    padding: 20px;
    border-right: 1px solid var(--gray-border);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.nav-item {
    display: block;
    padding: 10px 12px;
    border-radius: var(--border-radius);
    font-weight: 500;
    color: var(--text-dark);
    font-size: 14px;
}

.nav-item.active, .nav-item:hover {
    background-color: var(--medium-gray-bg);
    color: var(--text-dark);
}

/* --- Main Content (Główna treść) --- */
.main-content {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto; /* Scrollowanie tylko w obszarze treści */
    display: flex;
    flex-direction: column;
    width: 100%;
}

.main-content h1 {
    font-size: 22px;
    margin-bottom: 16px;
}

/* Toolbar (Pasek narzędzi nad tabelą) */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 12px;
}

.search-container {
    display: flex;
    gap: 8px;
    align-items: center;
}

.search-select {
    padding: 6px 10px;
    border: 1px solid var(--gray-border);
    border-radius: var(--border-radius);
    background-color: var(--white);
    font-size: 13px;
    height: 32px;
    cursor: pointer;
}

#search-input {
    padding: 6px 10px;
    border: 1px solid var(--gray-border);
    border-radius: var(--border-radius);
    min-width: 200px;
    font-size: 13px;
    height: 32px;
}

#search-btn {
    height: 32px;
    padding: 0 16px;
    display: flex;
    align-items: center;
}

.app-footer {
    padding: 8px 16px;
    background-color: var(--white);
    border-top: 1px solid var(--gray-border);
    color: var(--text-light);
    font-size: 11px;
    text-align: center;
    flex-shrink: 0;
}

/* ==========================================================================
   3. KOMPONENT TABELI ZGŁOSZEŃ
   Implementuje strategię 'table-layout: fixed' dla wydajności i kontroli
   szerokości kolumn. Obsługuje zawijanie długich tekstów (e-maile, tytuły).
   ========================================================================== */
.table-container {
    background-color: var(--white);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    width: 100%;
}

.ticket-table {
    width: 100%;
    border-collapse: collapse;
    /* Kluczowe dla wydajności przy dużej liczbie wierszy i sztywnych kolumnach */
    table-layout: fixed; 
}

.ticket-table th, .ticket-table td {
    padding: 8px 6px;
    text-align: left;
    border-bottom: 1px solid var(--gray-border);
    font-size: 12px;
    vertical-align: top;
    
    /* Zapobiega rozpychaniu tabeli przez długie ciągi znaków */
    white-space: normal;
    overflow-wrap: break-word; 
}

/* Specjalne reguły łamania tekstu dla kolumn z adresami e-mail */
.ticket-table td:nth-child(4),
.ticket-table td:nth-child(6) {
    word-break: normal;
    overflow-wrap: break-word; 
    line-height: 1.35;
}

/* Poprawa czytelności dat (data i godzina w osobnych liniach wizualnych dzięki line-height) */
.ticket-table td:nth-child(8),
.ticket-table td:nth-child(9),
.ticket-table td:nth-child(10) {
    line-height: 1.4;
}

.ticket-table thead th {
    background-color: var(--light-gray-bg);
    font-weight: 700;
    color: var(--text-light);
    text-transform: uppercase;
    font-size: 11px;
}

/* Definicja szerokości kolumn (Suma musi wynosić 100%) */
.ticket-table th:nth-child(1) { width: 6%; }  /* ID */
.ticket-table th:nth-child(2) { width: 9%; }  /* Status */
.ticket-table th:nth-child(3) { width: 14%; } /* Tytuł */
.ticket-table th:nth-child(4) { width: 11%; } /* User */
.ticket-table th:nth-child(5) { width: 9%; }  /* Kategoria */
.ticket-table th:nth-child(6) { width: 11%; } /* Odpowiedzialny */
.ticket-table th:nth-child(7) { width: 10%; } /* Grupa */
.ticket-table th:nth-child(8) { width: 10%; } /* Utworzono */
.ticket-table th:nth-child(9) { width: 10%; } /* SLA */
.ticket-table th:nth-child(10){ width: 10%; } /* Zamknięto */

/* Wyróżnianie wierszy ze względu na status SLA */
.ticket-table tbody tr.sla-warning { background-color: #fef9c3; font-weight: 500; }
.ticket-table tbody tr.sla-danger { background-color: #fee2e2; font-weight: 500; }
.ticket-table tbody tr.sla-warning:hover { background-color: #fef08a; }
.ticket-table tbody tr.sla-danger:hover { background-color: #fecaca; }
.ticket-table tbody tr:hover { background-color: #f9fafb; }
.ticket-table tbody tr.ticket-row { cursor: pointer; }

/* Badges (Etykiety statusów) */
.status-badge {
    display: inline-block; padding: 2px 6px; border-radius: 4px;
    font-weight: 600; font-size: 10px; color: var(--text-dark); text-transform: uppercase;
}
.status-badge.status-nieprzeczytane { background-color: var(--status-nieprzeczytane); }
.status-badge.status-otwarte { background-color: var(--status-otwarte); }
.status-badge.status-zamknięte { background-color: var(--status-zamknięte); color: white; }

/* ==========================================================================
   4. MODALE, FORMULARZE I ELEMENTY UI
   ========================================================================== */

/* --- Login & Error Panels --- */
.login-container {
    max-width: 450px; width: 90%; background-color: var(--white); padding: 40px;
    border-radius: var(--border-radius); box-shadow: var(--shadow); text-align: center;
}
.login-instruction { color: var(--text-light); margin: 16px 0 32px; line-height: 1.6; }
.button-login {
    display: inline-block; width: 100%; background-color: var(--primary-blue); color: var(--white);
    padding: 12px; border-radius: var(--border-radius); font-size: 16px; font-weight: 500;
    cursor: pointer; transition: background-color 0.2s; border: none;
}
.button-login:hover { background-color: var(--primary-blue-hover); }

.panel {
    width: 90%; max-width: 500px; background-color: var(--white); border-radius: var(--border-radius);
    box-shadow: var(--shadow); padding: 40px; text-align: center; margin: 20px auto;
}
.panel h2 { font-size: 24px; font-weight: 600; margin-bottom: 16px; }
.logout-button { background-color: var(--danger-red); color: var(--white); border: none; padding: 12px; border-radius: var(--border-radius); display: inline-block; margin-top: 16px;}

/* --- Paginacja --- */
.pagination {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.pagination-info {
    font-size: 13px;
    color: var(--text-light);
    font-weight: 500;
}

.pagination-buttons {
    display: flex;
    gap: 10px;
}

.pagination button {
    background-color: var(--primary-blue);
    color: var(--white);
    border: none;
    padding: 8px 16px;
    border-radius: var(--border-radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, opacity 0.2s;
}

.pagination button:hover:not(:disabled) {
    background-color: var(--primary-blue-hover);
}

.pagination button:disabled {
    background-color: #cbd5e1;
    color: #64748b;
    cursor: not-allowed;
    opacity: 0.7;
}

/* --- Formularze --- */
.form-container { max-width: 800px; width: 100%; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; font-weight: 500; margin-bottom: 8px; font-size: 14px; }
.form-control {
    width: 100%; padding: 10px; border: 1px solid var(--gray-border);
    border-radius: var(--border-radius); font-size: 14px; font-family: 'Inter', sans-serif;
}

/* Custom File Input Styling */
.file-input-wrapper { position: relative; display: inline-block; width: 100%; }
.file-input-label {
    display: inline-block; width: 100%; padding: 10px; background-color: var(--light-gray-bg);
    border: 1px solid var(--gray-border); border-radius: var(--border-radius); cursor: pointer; text-align: center; font-size: 14px;
}
.file-input { opacity: 0; width: 0.1px; height: 0.1px; position: absolute; }

.btn-primary {
    background-color: var(--primary-blue); color: var(--white); border: none; padding: 10px 20px;
    border-radius: var(--border-radius); font-size: 14px; font-weight: 500; cursor: pointer;
}
.btn-primary:hover { background-color: var(--primary-blue-hover); }
.btn-primary:disabled { background-color: var(--gray-border); cursor: not-allowed; }
.form-status.success { color: #155724; }
.form-status.error { color: #721c24; }

/* --- Modale --- */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.5); display: flex;
    justify-content: center; align-items: center; z-index: 1000;
}
.modal-content {
    width: 90%; max-width: 1200px; height: 85vh;
    background-color: var(--white); border-radius: var(--border-radius);
    box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 16px 24px; border-bottom: 1px solid var(--gray-border); flex-shrink: 0;
}
.modal-header h2 { font-size: 18px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 80%; }
/* .btn-close { background: transparent; border: none; font-size: 24px; cursor: pointer; color: var(--text-light); } */
.btn-close { background: transparent; border: none; font-size: 24px; cursor: pointer; color: var(--text-dark); }
.modal-body { display: flex; flex-grow: 1; overflow: hidden; }

/* Panel akcji bocznych w modalu */
.modal-actions-panel {
    width: 280px; padding: 24px; border-right: 1px solid var(--gray-border);
    display: flex; flex-direction: column; gap: 16px; flex-shrink: 0; overflow-y: auto;
}
.btn-action {
    width: 100%; padding: 10px; text-align: left; background-color: var(--light-gray-bg);
    border: 1px solid var(--gray-border); border-radius: var(--border-radius); cursor: pointer; font-size: 14px;
}

/* Logika widoczności przycisków w zależności od statusu zgłoszenia */
.modal-actions-panel.is-closed > *:not([data-action="open-ticket"]) { display: none; }
.modal-actions-panel:not(.is-closed) .btn-action[data-action="open-ticket"] { display: none; }

.modal-details-panel {
    flex-grow: 1; padding: 24px; display: flex; flex-direction: column; min-height: 0; overflow-y: auto;
}
.modal-meta-info { margin-bottom: 24px; font-size: 14px; }
.modal-status { padding: 12px 24px; border-top: 1px solid var(--gray-border); font-size: 14px; }
.modal-status.success { color: #155724; background-color: #d4edda; }
.modal-status.error { color: #721c24; background-color: #f8d7da; }

/* --- Sekcja Komentarzy (Chat-style) --- */
.comments-section {
    margin-top: 32px; display: flex; flex-direction: column; flex-grow: 1; min-height: 0; 
}
.comments-list {
    flex-grow: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; margin-bottom: 24px; padding-right: 10px; 
}
.comment {
    background-color: #f9fafb; border: 1px solid var(--gray-border); border-radius: var(--border-radius); padding: 16px;
}
.comment.system-comment { background-color: #fffbe6; border-left: 4px solid #ffc107; }
.comment-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.comment-date { font-size: 12px; color: var(--text-light); }
.comment-text { line-height: 1.6; word-break: break-word; font-size: 14px; }
.comment.system-comment .comment-text { font-style: italic; color: var(--text-light); }
.comment-attachment { margin-top: 8px; padding: 8px; background-color: var(--light-gray-bg); border-radius: var(--border-radius); border: 1px solid var(--gray-border); }
.comment-form { margin-top: auto; flex-shrink: 0; }
.comment-form textarea {
    width: 100%; min-height: 80px; padding: 12px; border: 1px solid var(--gray-border);
    border-radius: var(--border-radius); resize: vertical; margin-bottom: 8px; font-family: inherit;
}
.comment-form button { float: right; }
.radio-group { display: flex; gap: 24px; margin-bottom: 16px; }
.radio-group label { display: flex; align-items: center; gap: 8px; cursor: pointer; }

/* --- System Powiadomień (Dropdown) --- */
.notification-wrapper { position: relative; margin-right: 16px; display: flex; align-items: center; }
.notification-btn { background: none; border: none; cursor: pointer; padding: 8px; position: relative; color: var(--text-dark); }
.notification-btn:hover { color: var(--primary-blue); background-color: var(--light-gray-bg); border-radius: 50%; }
.notification-badge {
    position: absolute; top: 0; right: 0; background-color: var(--danger-red); color: white;
    font-size: 10px; font-weight: bold; height: 16px; min-width: 16px; border-radius: 10px;
    display: flex; align-items: center; justify-content: center; border: 2px solid var(--white); transform: translate(25%, -25%);
}
.notification-dropdown {
    position: absolute; top: 100%; right: -50px; width: 300px; background-color: var(--white);
    border: 1px solid var(--gray-border); border-radius: var(--border-radius);
    box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); z-index: 1000; display: none; margin-top: 8px;
}
.notification-dropdown.show { display: block; }
.notification-header { padding: 12px; font-weight: 600; background: var(--light-gray-bg); border-bottom: 1px solid var(--gray-border); font-size: 14px; }
.notification-list { max-height: 300px; overflow-y: auto; }
.notification-item { padding: 12px; border-bottom: 1px solid var(--gray-border); cursor: pointer; display: flex; flex-direction: column; gap: 4px; font-size: 13px;}
.notification-item:hover { background-color: #f0f9ff; }
.notification-item.unread { background-color: #fefce8; border-left: 3px solid var(--primary-blue); }
.notification-time { font-size: 11px; color: var(--text-light); }
.notification-empty { padding: 24px; text-align: center; color: var(--text-light); font-size: 13px; }

/* ==========================================================================
   5. SYSTEM ZAKŁADEK (TABS)
   Implementacja przełączania widoków (Szczegóły vs Historia) wewnątrz modalu.
   Kluczowe: Kontrolowanie widoczności za pomocą klas 'active'.
   ========================================================================== */

/* Pasek nawigacji zakładek */
.modal-tabs {
    display: flex;
    border-bottom: 1px solid var(--gray-border);
    background-color: var(--light-gray-bg);
    padding: 0 16px;
    flex-shrink: 0;
}

.tab-btn {
    padding: 12px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-light);
    border-bottom: 3px solid transparent;
    transition: all 0.2s;
}

.tab-btn:hover {
    color: var(--primary-blue);
    background-color: rgba(59, 130, 246, 0.05);
}

.tab-btn.active {
    color: var(--primary-blue);
    border-bottom-color: var(--primary-blue);
    font-weight: 600;
}

/* Kontenery treści zakładek */
.tab-content {
    display: none; /* Domyślnie ukryte */
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    padding: 24px;
}

/* Aktywacja widoczności */
.tab-content.active {
    display: flex;
}

/* --- Konfiguracja poszczególnych zakładek --- */

/* Zakładka Szczegóły: Przewijanie treści, formatowanie opisu */
#tab-details {
    overflow-y: auto;
    gap: 24px;
}

.ticket-content-box {
    background: white;
    padding: 16px;
    border: 1px solid var(--gray-border);
    border-radius: 8px;
    min-height: 120px;
    white-space: pre-wrap; /* Zachowuje formatowanie nowej linii */
    line-height: 1.6;
    word-wrap: break-word;
    font-size: 14px;
}

#tab-details h4 {
    margin-bottom: 12px;
    color: var(--text-dark);
    font-weight: 600;
}

#modal-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

/* Zakładka Historia: Usuwa padding, aby czat wypełnił całą przestrzeń */
#tab-history {
    padding: 0; 
}

/* Reset kontenera nadrzędnego dla zakładek, aby umożliwić pełne wypełnienie */
.modal-details-panel {
    padding: 0 !important; 
    display: flex;
    flex-direction: column;
    overflow: hidden; 
}

/* Nadpisanie listy komentarzy dla zakładki historii */
#tab-history .comments-list {
    background-color: #f9fafb;
    padding: 20px;
}
#tab-history .comment-form {
    background-color: var(--white);
    border-top: 1px solid var(--gray-border);
    padding: 16px;
}

/* ==========================================================================
   6. RESPONSYWNOŚĆ (MOBILE BREAKPOINTS)
   Dostosowanie układu dla urządzeń o szerokości <= 768px.
   ========================================================================== */
@media (max-width: 768px) {
    /* Reset wysokości na mobile */
    .app-layout { height: auto; min-height: 100vh; }
    .app-body { flex-direction: column; height: auto; overflow: visible; }
    .app-body-centered { overflow-y: visible; }

    /* Header w pionie */
    .app-header { flex-direction: column; gap: 12px; padding: 16px; }
    .header-user-info { width: 100%; justify-content: space-between; }
    .notification-dropdown { right: -100px; }

    /* Sidebar jako pasek poziomy lub zwijany */
    .sidebar { width: 100%; border-right: none; border-bottom: 1px solid var(--gray-border); padding: 12px; overflow-x: auto; }
    .sidebar-nav { flex-direction: row; gap: 8px; }
    .nav-item { white-space: nowrap; padding: 8px 12px; font-size: 13px; }

    /* Content */
    .main-content { padding: 16px; height: auto; overflow: visible; padding-bottom: 100px; }
    .main-content h1 { font-size: 20px; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .search-container { flex-direction: column; }
    #search-input { min-width: 0; width: 100%; }
    #search-btn { width: 100%; }

    /* Tabela: Włączenie scrolla poziomego na małych ekranach */
    .table-container { overflow-x: auto; display: block; }
    .ticket-table { min-width: 800px; }

    /* Modal na pełny ekran mobile */
    .modal-content { width: 100%; height: 100%; max-width: none; border-radius: 0; }
    .modal-body { flex-direction: column; overflow-y: auto; }
    .modal-actions-panel { width: 100%; border-right: none; border-bottom: 1px solid var(--gray-border); padding: 16px; height: auto; }
    .modal-details-panel { padding: 16px; overflow-y: visible; }
    .floating-save-bar { padding: 12px; flex-direction: column; gap: 8px; align-items: stretch; }
    #save-status { margin-right: 0; text-align: center; margin-bottom: 8px; }
    .floating-save-bar button { width: 100%; }
}

/* ==========================================================================
   7. STYLE WYDRUKU (PRINT MEDIA)
   Optymalizacja widoku pod kątem generowania raportów i drukowania.
   ========================================================================== */
@media print {
    tr {
        page-break-inside: avoid;
        break-inside: avoid;
    }
    
    /* Ukrywanie elementów interfejsu (nawigacja, przyciski) */
    .sidebar, .app-header, .report-controls, .app-footer, .floating-save-bar {
        display: none !important;
    }
    
    /* Reset kontenerów do pełnej szerokości papieru */
    .app-body, .main-content, .table-container {
        padding: 0;
        margin: 0;
        overflow: visible !important;
        height: auto !important;
        width: 100% !important;
    }
}