/* ==========================================================================
   1. RESET & BIẾN HỆ THỐNG
   ========================================================================== */
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;700&display=swap');

:root {
    --workspace-font: 'JetBrains Mono', monospace;
    --system-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --primary-bg: #34495e;
    --border-color: #cfe2f3;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: var(--system-font);
}

body {
    display: flex;
    flex-direction: column;
    height: 100vh; 
    background-color: #f4f8fa; 
    color: #333;
    overflow: hidden;
}

/* ==========================================================================
   2. HEADER, NAV & FOOTER
   ========================================================================== */
header, nav {
    background-color: var(--primary-bg);
    flex-shrink: 0;
}

header {
    padding: 6px 20px; 
    text-align: center;
}

header h1 {
    font-size: 1.1rem; 
    color: #ffffff; 
}

nav {
    display: flex;
    border-bottom: 2px solid #e6f0fa;
    justify-content: center;
}

.menu-item {
    padding: 8px 25px; 
    color: #ecf0f1; 
    text-decoration: none;
    font-weight: 500;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}

.menu-item:hover, 
.menu-item.active {
    background-color: #1abc9c; 
    color: #ffffff;
    outline: none;
}

footer {
    background-color: var(--primary-bg); 
    text-align: center;
    padding: 6px; 
    font-size: 0.8rem;
    color: #bdc3c7; 
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

/* ==========================================================================
   3. THANH CÔNG CỤ SUB-NAVIGATION
   ========================================================================== */
#sub-navigation {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 2px 10px;
    background: #eaeff2;
    border-bottom: 1px solid var(--border-color);
    height: 32px;
    box-sizing: border-box;
    flex-shrink: 0;
}

#openDifferentFile {
    cursor: pointer;
    background: transparent;
    border: none;
}

#barOpenInput {
    display: none;
}

#fem-btn-zoomall {
    height: 22px;
    padding: 0 8px;
    font-size: 11px;
    cursor: pointer;
    background: transparent;
    border: 1px solid #ccc;
    border-radius: 3px;
}

#fem-btn-zoomall:hover {
    background-color: #d0e4f7;
    color: #1a446c;
}

#plotOptions {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 10px;
}

#plotOptions .plot-label {
    font-size: 11px;
    font-weight: bold;
    color: #495057;
}

#diagramSelector {
    height: 22px;
    padding: 0 4px;
    font-size: 11px;
    cursor: pointer;
    border-radius: 3px;
    border: 1px solid #ccc;
}

#plotOptions .btn-clear {
    height: 22px;
    padding: 0 8px;
    font-size: 11px;
    cursor: pointer;
    background-color: #d32f2f;
    color: white;
    border: none;
    border-radius: 3px;
    font-weight: bold;
}

#plotOptions .scale-container {
    display: flex;
    align-items: center;
    font-size: 11px;
    gap: 4px;
    margin-left: 10px;
}

#plotOptions .scale-label {
    color: #333;
}

#scale {
    width: 36px;
    height: 20px;
    text-align: center;
    font-size: 11px;
    border: 1px solid #cccccc;
    border-radius: 2px;
    font-family: var(--workspace-font);
}

/* State */
/* Badge ở sub-menu */
#run-status-badge {
    margin-left: auto;
    font-size: 12px;
    color: #1abc9c;
}

/* Modal Popup phủ giữa màn hình */
#fem-modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.2); /* Lớp mờ nhẹ chặn click */
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.fem-modal-hidden {
    display: none !important;
}

.fem-modal-box {
    background: #fff;
    padding: 16px 24px;
    border-radius: 6px;
    border: 1px solid #ccc;
    position: relative;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    font-size: 13px;
    color: black;
}

#fem-modal-close {
    position: absolute;
    top: 2px;
    right: 6px;
    border: none;
    background: transparent;
    font-size: 16px;
    cursor: pointer;
    color: #888;
}

/* ==========================================================================
   4. LAYOUT KHUNG CHÍNH (MAIN CONTAINER & RESIZER)
   ========================================================================== */
.main-container {
    display: flex;
    width: 100%;
    height: calc(100vh - 100px);
    overflow: hidden;
    position: relative;
    flex: 1;
}

.resizer {
    width: 4px;                   
    cursor: col-resize;
    background-color: var(--border-color);      
    flex-shrink: 0;
    transition: background-color 0.2s;
}

.resizer:hover { 
    background-color: #1abc9c; 
}

/* ==========================================================================
   5. SIDEBAR (BẢNG NHẬP LIỆU & KẾT QUẢ)
   ========================================================================== */
.sidebar#sidebar-content {
    display: flex;
    flex-direction: column;
    width: 400px;
    min-width: 250px;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    background-color: #ffffff;
}

#bar-scrollable-content {
    flex-grow: 1;
    overflow-y: auto;
    overflow-x: auto;
    padding: 10px;
    box-sizing: border-box;
}

#bar-scrollable-content::-webkit-scrollbar { width: 6px; height: 6px; }
#bar-scrollable-content::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 3px;
}

.tabcontent {
    display: block;
    padding: 0;
}

.tabcontent .tab-title {
    color: #000000;
    margin-top: 0;
    margin-bottom: 6px;
    font-size: 12px !important;
    font-weight: bold;
}

.hidden-select {
    display: none;
}

#sidebar-content, 
#sidebar-content *,
#sidebar-content p,
#sidebar-content label,
#sidebar-content span {
    font-family: var(--system-font);
    font-size: 10px !important;
}

/* --- QUY TẮC CHUNG CHO CÁC BẢNG NHẬP LIỆU --- */
#sidebar-content table {
    border-collapse: collapse;
    margin-bottom: 4px;
}

#sidebar-content th {
    background-color: #f0f0f0;
    color: #000000;
    font-weight: bold;
    padding: 2px 4px;
    border: 1px solid #b0b0b0;
    text-align: center;
    font-size: 10px !important;
    white-space: nowrap;
}

#sidebar-content td {
    border: 1px solid #d0d0d0;
    padding: 0;
    text-align: center;
    font-size: 10px !important;
}

#sidebar-content input[type="text"] { 
    width: 100%; 
    height: 20px;  
    text-align: center;
    font-size: 10px !important;
    font-family: var(--workspace-font);
    border: none;
    outline: none;
    background: transparent;
    padding: 0 2px;
}

#sidebar-content input[type="text"]:focus {
    background-color: #e8f4fc;
}

#sidebar-content select {
    height: 20px;
    font-size: 10px !important;
    border: none;
    outline: none;
    background: transparent;
    text-align: center;
    cursor: pointer;
    padding: 0;
}

.select-w50 {
    width: 100% !important;
}

/* --- Project title --- */
.box { display: flex; align-items: center; gap: 8px; }
.box input { border: 1px solid #ccc !important; }

/* --- BẢNG NÚT (#nodeTable) --- */
#nodeTable {
    width: 180px !important;
    table-layout: fixed;
}

#nodeTable th:nth-child(1),
#nodeTable td:nth-child(1) { width: 50px; }

#nodeTable th:nth-child(2),
#nodeTable td:nth-child(2),
#nodeTable th:nth-child(3),
#nodeTable td:nth-child(3) { width: 65px; }

/* --- BẢNG PHẦN TỬ (#elementTable) --- */
#elementTable {
    width: 320px !important;
    table-layout: fixed;
}

#elementTable th:nth-child(1), #elementTable td:nth-child(1) { width: 45px; }
#elementTable th:nth-child(2), #elementTable td:nth-child(2) { width: 35px; }
#elementTable th:nth-child(3), #elementTable td:nth-child(3) { width: 35px; }
#elementTable th:nth-child(4), #elementTable td:nth-child(4) { width: 55px; }
#elementTable th:nth-child(5), #elementTable td:nth-child(5) { width: 55px; }
#elementTable th:nth-child(6), #elementTable td:nth-child(6) { width: 95px; }

/* --- NÚT BẤM (add, rem, show) --- */
#sidebar-content input[type="button"],
#sidebar-content button { 
    height: 20px;
    width: 50px;
    padding: 0;
    font-size: 10px !important;
    cursor: pointer;
    background-color: #eef2f5;
    border: 1px solid #b0b0b0;
    border-radius: 2px;
    margin: 2px 1px;
}

#sidebar-content input[type="button"]:hover,
#sidebar-content button:hover {
    background-color: #d8e2ea;
}

#applyOutput {
    margin: 8px 0;
    padding: 3px 12px !important;
    width: auto !important;
    background-color: #1a446c !important;
    color: white !important;
    border: 1px solid #0f2b45 !important;
    border-radius: 2px !important;
    font-weight: bold !important;
    cursor: pointer;
}

#applyOutput:hover {
    background-color: #153758 !important;
}

/* ==========================================================================
   5.1 BỐ TRÍ KHU VỰC KẾT QUẢ (RESULTS)
   ========================================================================== */
#Bar h4 {
    font-size: 11px !important;
    font-weight: bold;
    color: #000000;
    margin: 12px 0 6px 0;
    text-align: left; /* Căn trái tiêu đề Results */
}

#Bar p[id$="_text"] {
    font-size: 10px !important;
    font-weight: bold;
    color: #333;
    margin: 8px 0 2px 0;
    text-align: left; /* Căn trái tiêu đề từng bảng kết quả */
}

.result-table-wrapper {
    margin-bottom: 8px;
    overflow-x: auto;
    display: flex;
    justify-content: center; /* Vẫn giữ bảng kết quả căn giữa khung */
}

.result-table-wrapper table {
    border-collapse: collapse;
    width: auto;
    margin: 0 auto;
}

.result-table-wrapper table td,
.result-table-wrapper table th {
    border: 1px solid #b0b0b0;
    padding: 2px 5px;
    font-size: 10px !important;
    font-family: var(--workspace-font) !important;
    text-align: center; /* Căn giữa dữ liệu số bên trong các ô bảng kết quả */
    background-color: #ffffff;
}

/* ==========================================================================
   6. MAIN CONTENT (CANVAS KHU VỰC VẼ)
   ========================================================================== */
.main-content#main-body {
    flex-grow: 1;
    position: relative;
    height: 100%;
    overflow: hidden;
    box-sizing: border-box;
    background-color: #ffffff;
}

#static {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    background: transparent;
    pointer-events: auto; /* Kích hoạt nhận sự kiện Zoom/Pan */
    cursor: default;
}

#dynamic {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Bỏ qua sự kiện để nhường cho lớp static */
}

#cv {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 0;
    width: 100%;
    height: 100%;
    display: none;
}

/* ==========================================================================
   7. RESPONSIVE MOBILE INTEGRATION (< 768px)
   ========================================================================== */
@media (max-width: 768px) {
    body { 
        height: auto; 
        overflow: auto; 
    }
    nav { 
        flex-wrap: wrap; 
    }
    .menu-item { 
        flex: 1 1 auto; 
        text-align: center; 
    }
    #sub-navigation { 
        height: auto; 
        padding: 8px 20px; 
        flex-wrap: wrap;
    }
    .main-container { 
        flex-direction: column; 
        height: auto;
    }

    /* --- ĐẢO THỨ TỰ HIỂN THỊ --- */
    .main-content#main-body { 
        order: 1;               /* Đồ họa / Kết quả hiển thị trước */
        width: 100%; 
        height: 60vh; 
        flex: none; 
    }

    .sidebar#sidebar-content { 
        order: 2;               /* Sidebar hiển thị phía dưới */
        width: 100%; 
        height: 40vh; 
        flex: none; 
    }

    .resizer { 
        display: none; 
    }
}

/* ==========================================================================
   8. PRINT STYLES
   ========================================================================== */
@media print {
    /* 1. Mở khóa chiều cao & cuộn cho các khung bao chính (BỎ áp dụng vị trí static tràn lan) */
    html, body, 
    .main-container, 
    .sidebar, 
    .main-content {
        overflow: visible !important;
        height: auto !important;
        max-height: none !important;
        background: #fff !important;
        color: #000 !important;
    }

    /* 2. Đưa container về dạng Flexbox cột để đảo thứ tự: Hình vẽ TRƯỚC, Bảng dữ liệu SAU */
    .main-container {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* 3. VÙNG HÌNH VẼ (MAIN-CONTENT) - Hiển thị đầu tiên (order: 1) */
    .main-content {
        order: 1 !important;
        width: 100% !important;
        display: block !important;
        position: relative !important;
        margin-bottom: 30px !important; /* Tạo khoảng hở an toàn bên dưới hình vẽ */
        page-break-after: always !important; /* Ngắt trang: Đẩy toàn bộ bảng dữ liệu sang trang sau */
        break-after: page !important;
    }

    /* 4. VÙNG BẢNG DỮ LIỆU (SIDEBAR) - Hiển thị tiếp theo (order: 2) */
    .sidebar {
        order: 2 !important;
        width: 100% !important;
        display: block !important;
        position: relative !important;
        clear: both !important; /* Đảm bảo tách biệt hoàn toàn khỏi các phần tử phía trên */
        page-break-inside: auto;
    }

    /* 5. Mở trọn vẹn các bảng / danh sách Node, Element, BC, Spring bị thu gọn */
    .sidebar .collapse, 
    .sidebar .accordion-collapse,
    .sidebar [style*="display: none"],
    .node-list, 
    .element-list, 
    .bc-list, 
    .spring-list,
    .scrollable-area {
        display: block !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* 6. Ẩn các nút bấm và thanh điều hướng không cần xuất PDF */
    header, 
    #main-navigation, 
    #sub-navigation, 
    footer, 
    .resizer,
    .btn-clear,
    input[type="button"],
    button,
    .fem-btn-print {
        display: none !important;
    }

    /* 7. Giữ tỷ lệ hình vẽ Canvas/Ảnh, tránh xé đôi hình hoặc chồng lấp */
    canvas, img {
        display: block !important;
        max-width: 100% !important;
        height: auto !important;
        object-fit: contain !important;
        margin: 0 auto 20px auto !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }

    /* 8. Đảm bảo bảng biểu không bị xé dọc/chồng lấp */
    table {
        width: 100% !important;
        border-collapse: collapse !important;
        page-break-inside: auto;
    }

    tr, td, th {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }
}
