/* ================================
   Grundlayout
   ================================ */

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f3f4f6;
    color: #111827;
}

h1 {
    margin: 0;
    padding: 16px 24px;
    background-color: #111827;
    color: #f9fafb;
    font-size: 20px;
}

h2 {
    font-size: 18px;
    margin: 16px 0 8px 0;
}

h3 {
    font-size: 16px;
    margin: 12px 0 8px 0;
}

body > h1 + * {
    padding: 16px 24px 24px 24px;
}

/* ================================
   Flikar
   ================================ */

.tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 16px;
    border-bottom: 1px solid #d1d5db;
}

.tab-button {
    border: none;
    background: none;
    padding: 8px 14px;
    cursor: pointer;
    font-size: 14px;
    border-radius: 6px 6px 0 0;
    color: #374151;
}

.tab-button.active {
    background-color: #ffffff;
    border: 1px solid #d1d5db;
    border-bottom-color: #ffffff;
    color: #111827;
    font-weight: 600;
}

.tab-content {
    display: none;
    background-color: #ffffff;
    border: 1px solid #d1d5db;
    padding: 16px;
    border-radius: 0 6px 6px 6px;
    position: relative;
}

.tab-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1200px;
    margin: 0 auto 16px auto;
    padding: 0 16px;
}

.tab-header-row h2 {
    margin: 0;
}

.refresh-btn {
    background-color: #ffffff;
    border: 1px solid #d1d5db;
    color: #374151;
    font-weight: 500;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 4px;
}

.refresh-btn:hover {
    background-color: #f9fafb;
    border-color: #9ca3af;
}

.loading-spinner {
    width: 30px;
    height: 30px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 40px auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

#tab-trains.tab-content h2 {
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
}

#tab-trains.tab-content .trains-actions {
    margin: 0;
    padding: 0;
}

#trains-message {
    display: block;
    max-width: 1200px;
    margin: 0 auto 10px auto;
    padding: 0 16px;
}

.tab-content.active {
    display: block;
}

/* ================================
   Global tabellbas (INGEN layout)
   ================================ */

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

th, td {
    border: 1px solid #e5e7eb;
    padding: 6px 8px;
    text-align: left;
    white-space: normal;
}

th {
    background-color: #f9fafb;
    font-weight: 600;
}

tbody tr:nth-child(even) {
    background-color: #f9fafb;
}

/* ================================
   DEPÅ-TABELLER – UNIFIERAT SYSTEM (Tåg i verkstad)
   ================================ */

.train-depot-table {
    table-layout: auto;
    width: 100%;
    border-collapse: collapse;
}

/* 
   Gemensamma kolumn-inställningar för alla depå-tabeller.
   Vi använder min-width, width (för preferred) och max-width för att styra flexibiliteten.
*/

/* 1. Tåg - Small */
.train-depot-table th:nth-child(1), .train-depot-table td:nth-child(1) {
    min-width: 60px;
    width: 60px;
    max-width: 80px;
}

/* 2. Fordon - Small */
.train-depot-table th:nth-child(2), .train-depot-table td:nth-child(2) {
    min-width: 80px;
    width: 80px;
    max-width: 100px;
}

/* 3. Spår - Small */
.train-depot-table th:nth-child(3), .train-depot-table td:nth-child(3) {
    min-width: 50px;
    width: 50px;
    max-width: 70px;
}

/* 4. Plats - Medium, adaptiv */
.train-depot-table th:nth-child(4), .train-depot-table td:nth-child(4) {
    min-width: 120px;
    width: 140px;
    max-width: 180px;
}

/* 5. Titel - Medium, kontrollerad max-bredd */
.train-depot-table th:nth-child(5), .train-depot-table td:nth-child(5) {
    min-width: 180px;
    width: 250px;
    max-width: 350px;
}

/* 6. Ordernr - Small */
.train-depot-table th:nth-child(6), .train-depot-table td:nth-child(6) {
    min-width: 90px;
    width: 90px;
    max-width: 110px;
}

/* 7. Status - Small */
.train-depot-table th:nth-child(7), .train-depot-table td:nth-child(7) {
    min-width: 100px;
    width: 100px;
    max-width: 110px;
}

/* 8. Mekaniker - Medium, adaptiv */
.train-depot-table th:nth-child(8), .train-depot-table td:nth-child(8) {
    min-width: 120px;
    width: 140px;
    max-width: 180px;
}

/* 9. Åtgärd - Small fixed */
.train-depot-table th:nth-child(9), .train-depot-table td:nth-child(9) {
    min-width: 80px;
    width: 80px;
    max-width: 80px;
}

.train-depot-table td {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 10px 8px;
    border-bottom: 1px solid #f3f4f6;
}

.train-depot-table th {
    padding: 12px 8px;
    background-color: #f3f4f6;
    color: #374151;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.train-depot-table .open-detail-btn {
    width: 100%;
    padding: 4px 8px;
    text-align: center;
}

/* ================================
   ARBETSORDRAR – LAYOUT
   ================================ */

.workorder-table {
    table-layout: fixed;
    width: 100%;
}

.workorder-table th:nth-child(1) { width: 180px; } /* Titel */
.workorder-table th:nth-child(2) { width: 140px; } /* Kund */
.workorder-table th:nth-child(3) { width: 60px; }  /* Tåg */
.workorder-table th:nth-child(4) { width: 80px; }  /* Fordon */
.workorder-table th:nth-child(5) { width: 70px; }  /* Spår */
.workorder-table th:nth-child(6) { width: 90px; }  /* Status */
.workorder-table th:nth-child(7) { width: 120px; } /* Mekaniker */
.workorder-table th:nth-child(8) { width: 90px; }  /* Skapad */
.workorder-table th:nth-child(9) { width: 170px; } /* Åtgärder */

.workorder-table td {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Behållare för grupperade listor (Används på Arbetsordrar sidan) */
.train-location-block {
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    background-color: #ffffff;
    margin-bottom: 16px;
    overflow: hidden;
}

.train-location-title {
    background-color: #f3f4f6;
    padding: 6px 10px;
    font-weight: 600;
    font-size: 13px;
    border-bottom: 1px solid #e5e7eb;
}

/* Responsiv behållare för tabeller i Tåg i verkstad-vyn */
.depot-view-block {
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background-color: #ffffff;
    margin: 0 auto 24px auto; /* Centrerad */
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    max-width: 1200px; /* Max-bredd för att förhindra för bred layout */
}

.table-responsive-wrapper {
    overflow-x: auto;
    width: 100%;
}

.depot-view-title {
    background-color: #ffffff;
    padding: 12px 16px;
    font-weight: 700;
    font-size: 15px;
    border-bottom: 1px solid #e5e7eb;
    color: #111827;
}

/* Mobil-optimering */
@media (max-width: 1024px) {
    .train-depot-table {
        min-width: 100%;
    }
    
    .col-low-priority {
        display: none;
    }

    /* På mindre skärmar låter vi kolumnerna ta den plats de behöver */
    .train-depot-table th, .train-depot-table td {
        min-width: auto !important;
        width: auto !important;
        max-width: none !important;
        white-space: nowrap !important; /* Säkerställ att vi inte får radbrytning även mobilt */
    }

    .depot-view-block {
        max-width: 100%; /* Full bredd på mindre skärmar */
    }
}

@media (max-width: 768px) {
    .train-location-title {
        font-size: 14px;
        padding: 10px 12px;
    }
}

/* ================================
   Status-badges
   ================================ */

.status-badge {
    background-image: none !important;
    padding-right: 10px !important;
    cursor: default;
}

.status-badge, .status-select-unified {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    border: 1px solid transparent;
    cursor: pointer;
    outline: none;
    appearance: none; /* Ta bort standard-pil i vissa webbläsare om man vill ha helt egen stil */
    -webkit-appearance: none;
    position: relative;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 20 20' fill='currentColor'%3E%3Cpath fill-rule='evenodd' d='M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z' clip-rule='evenodd'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    padding-right: 24px;
}

.status-select-unified:disabled {
    cursor: default;
    opacity: 0.8;
    background-image: none;
    padding-right: 10px;
}

.status-OPEN {
    background-color: #dbeafe;
    color: #1d4ed8;
}

.status-IN_PROGRESS {
    background-color: #fef3c7;
    color: #c2410c;
}

.status-DONE {
    background-color: #dcfce7;
    color: #15803d;
}

.status-READY_FOR_INVOICING {
    background-color: #e0f2fe;
    color: #0369a1;
}

.status-INVOICED {
    background-color: #ede9fe;
    color: #5b21b6;
}

.status-CANCELLED {
    background-color: #fee2e2;
    color: #b91c1c;
}
/* ================================
   Dagar i verkstad färg
   ================================ */
.ws-green {
    background-color: #dcfce7;
    color: #15803d;
    font-weight: 600;
}

.ws-yellow {
    background-color: #fef3c7;
    color: #92400e;
    font-weight: 600;
}

.ws-red {
    background-color: #fee2e2;
    color: #b91c1c;
    font-weight: 600;
}

/* ================================
   Popup / Modal
   ================================ */

.order-detail-view {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    max-width: 1000px;
    height: 90vh;
    max-height: 900px;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
    padding: 16px 20px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.order-info-box {
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    padding: 8px 10px;
    background-color: #f9fafb;
    margin-bottom: 12px;
}

.order-info-box .info-row {
    display: flex;
    flex-direction: row;
    gap: 20px;
    margin-bottom: 5px;
}

.detail-section {
    margin-top: 12px;
}

.detail-section table {
    table-layout: auto;
}

.table-footer {
    text-align: right;
    margin-top: 4px;
}

.grand-total {
    text-align: right;
    font-weight: 600;
    margin-top: 10px;
}

.detail-actions {
    display: flex;
    gap: 12px;
    margin-top: 12px;
}

/* ================================
   Formulär (VERTIKALT)
   ================================ */

form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

label {
    display: flex;
    flex-direction: column;
    font-size: 13px;
}

input,
textarea,
select {
    padding: 6px 8px;
    font-size: 13px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
}

/* ================================
   Knappar
   ================================ */

button {
    padding: 6px 10px;
    font-size: 13px;
    border-radius: 4px;
    border: 1px solid #d1d5db;
    background-color: #e5e7eb;
    cursor: pointer;
}

button:hover {
    background-color: #d1d5db;
}

#save-detail {
    background-color: #16a34a;
    color: white;
}

.overlay-backdrop {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
}

.detail-message {
    font-size: 14px;
    font-weight: 500;
    align-self: center;
}
