@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap');

:root {
    --primary-color: #4f46e5; 
    --secondary-color: #6b7280; 
    --success-color: #16a34a;
    --danger-color: #dc2626; 
    --warning-color: #f97316; 
    --light-color: #f9fafb;
    --dark-color: #111827; 
    --border-color: #e5e7eb; 
    --sidebar-bg: #ffffff;
    --sidebar-text: #374151; 
    --sidebar-hover-bg: #f3f4f6; 
    --sidebar-active-bg: #eef2ff;
    --body-bg: #f9fafb;
}

html, body {
    height: 100%;
    margin: 0;
    font-family: 'Sarabun', sans-serif;
    background-color: var(--body-bg);
    overflow: hidden;
    font-size: 16px;
}

.app-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Sidebar Styles */
.sidebar {
    width: 250px;
    background-color: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: width 0.3s;
    border-right: 1px solid var(--border-color);
}
.sidebar-header {
    padding: 1.25rem 1.5rem;
    text-align: center;
}
.sidebar-logo {
    display: block;
    width: 60px;
    height: 60px;
    margin: 0 auto 0.75rem;
    object-fit: contain;
}
.sidebar-header h1 {
    font-size: 18px;
    margin: 0 0 4px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--dark-color);
}
.sidebar-header p {
    font-size: 14px;
    margin: 0;
    color: var(--secondary-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sidebar-nav {
    flex-grow: 1;
    margin-top: 1rem;
}
.sidebar-btn {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: 100%;
    background: none;
    border: none;
    color: var(--sidebar-text);
    padding: 0.9rem 1.5rem;
    text-align: left;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s;
    border-left: 4px solid transparent;
    font-weight: 500;
}
.sidebar-btn:hover {
    background-color: var(--sidebar-hover-bg);
    color: var(--primary-color);
}
.sidebar-btn.active {
    background-color: var(--sidebar-active-bg);
    color: var(--primary-color);
    font-weight: 700;
    border-left-color: var(--primary-color);
}
.sidebar-btn svg { width: 20px; height: 20px; stroke-width: 2; }

/* Sidebar Footer */
.sidebar-footer {
    padding: 1rem;
    text-align: center;
    border-top: 1px solid var(--border-color);
    margin-top: auto;
}
.sidebar-footer .footer-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}
.sidebar-footer .footer-item:last-child { margin-bottom: 0; }
.sidebar-footer .icon-circle {
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background-color: var(--light-color);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.sidebar-footer svg { width: 16px; height: 16px; color: var(--secondary-color); }
.sidebar-footer .text-content { text-align: left; overflow: hidden; }
.sidebar-footer p { margin: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-footer .name { font-weight: 700; }

/* Main Content & Top Widget */
.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.top-widget {
    background-color: white;
    padding: 0.75rem 2rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-shrink: 0;
}
.top-widget-title {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 16px;
    font-weight: 700;
    color: var(--dark-color);
}
.top-widget-title svg { width: 25px; height: 25px; color: var(--primary-color); }
.user-info { display: flex; align-items: center; gap: 1.5rem; }
.user-details { text-align: right; }
.user-details .name { font-weight: 700; display: block; }
.user-details .role { font-size: 0.85rem; color: var(--secondary-color); }
.logout-btn {
    background: none; border: none; cursor: pointer; padding: 0.5rem; border-radius: 50%;
    display: flex; align-items: center; justify-content: center; transition: background-color 0.2s;
}
.logout-btn:hover { background-color: var(--sidebar-hover-bg); }
.logout-btn svg { width: 20px; height: 20px; color: var(--danger-color); }

/* Content Area & Cards */
.content-area { padding: 2rem; overflow-y: auto; flex-grow: 1; }
.view { display: none; }
.view.active { display: flex; flex-direction: column; flex-grow: 1; }
.card {
    background-color: white; border-radius: 12px; border: 1px solid var(--border-color);
    padding: 1.5rem; margin-bottom: 2rem; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05);
}
.card-header {
    margin-top: 0; padding-bottom: 1rem; margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color); font-size: 1.2rem; font-weight: 700;
}

/* Forms & Inputs */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}
select, input {
    box-sizing: border-box; width: 100%; padding: 0.6rem;
    border: 1px solid var(--border-color); border-radius: 8px; font-size: 1rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}
select:focus, input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.25); outline: none;
}
input:read-only { background-color: #f3f4f6; cursor: not-allowed; }

/* Buttons */
.btn {
    padding: 0.6rem 1.2rem; font-size: 1rem; border-radius: 8px; cursor: pointer;
    transition: all 0.2s; font-weight: 600; border: 1px solid transparent;
    display: inline-flex; align-items: center; gap: 0.5rem; justify-content: center; text-decoration: none;
}
.btn:disabled { background-color: var(--secondary-color); opacity: 0.65; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background-color: var(--primary-color); color: white; border-color: var(--primary-color); }
.btn-primary:hover { background-color: #4338ca; }
.btn-success { background-color: var(--success-color); color: white; border-color: var(--success-color); }
.btn-success:hover { background-color: #15803d; }
.btn-danger { background-color: var(--danger-color); color: white; border-color: var(--danger-color); }
.btn-danger:hover { background-color: #b91c1c; }
.btn-warning { background-color: var(--warning-color); color: white; border-color: var(--warning-color); }
.btn-warning:hover { background-color: #ea580c; }
.btn-secondary { background-color: var(--secondary-color); color: white; border-color: var(--secondary-color); }
.btn-secondary:hover { background-color: #52525b; }
.btn-outline-danger { color: var(--danger-color); border-color: var(--danger-color); background: white; }
.btn-outline-danger:hover { background: var(--danger-color); color: white; }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
.form-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.5rem; }

/* Tables */
.table-container {
    overflow-x: auto; border: 1px solid var(--border-color); border-radius: 8px; margin-bottom: 1.5rem;
}
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.8rem; text-align: left; border-bottom: 1px solid var(--border-color); white-space: nowrap; }
thead { background-color: var(--light-color); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background-color: #f3f4f6; }

/* Status Messages & Loading */
.status { font-weight: 600; padding: 0.8rem; border-radius: 6px; display: none; margin-top: 1rem; }
.status.success { color: var(--success-color); background-color: #dcfce7; }
.status.error { color: var(--danger-color); background-color: #fee2e2; }

.loading-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255,255,255,0.7); z-index: 9999; display: flex; justify-content: center; align-items: center; }
.spinner { border: 5px solid #f3f3f3; border-top: 5px solid var(--primary-color); border-radius: 50%; width: 50px; height: 50px; animation: spin 1s linear infinite; }
@keyframes spin { 0% { transform: rotate(0deg) } 100% { transform: rotate(360deg) } }

/* Login */
.login-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: var(--body-bg); z-index: 10001; display: flex; justify-content: center; align-items: center; }
.login-card { width: 90%; max-width: 400px; }
.login-card .card-header { text-align: center; border-bottom: none; margin-bottom: 0.5rem; }
.login-card .form-group { margin-bottom: 1rem; }
.login-card .btn { width: 100%; }

/* Admin View Navigation */
.admin-nav { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border-color); overflow-x: auto; white-space: nowrap; padding-bottom: 1px; }
.admin-nav::-webkit-scrollbar { height: 4px; }
.admin-nav::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; }
.admin-nav-btn { background: none; border: none; padding: 0.5rem 1rem; cursor: pointer; font-size: 1rem; font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px; display: inline-flex; align-items: center; gap: 0.5rem; }
.admin-nav-btn.active { border-bottom-color: var(--primary-color); color: var(--primary-color); }
.admin-sub-view { display: none; }
.admin-sub-view.active { display: flex; flex-direction: column; flex-grow: 1; }

/* Utilities */
.filter-bar { display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; }
.filter-bar .form-group { flex: 1; min-width: 150px; }
.button-group { display: flex; gap: 0.5rem; }

/* Dashboard specific */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1.5rem; margin-bottom: 2rem; }
.stat-card { background-color: white; border-radius: 12px; padding: 1.5rem; display: flex; align-items: center; gap: 1.5rem; border: 1px solid var(--border-color); }
.stat-icon { width: 50px; height: 50px; border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.stat-icon svg { width: 24px; height: 24px; }
.stat-icon.students { background-color: #e0f2fe; color: #0ea5e9; }
.stat-icon.teachers { background-color: #e0e7ff; color: #6366f1; }
.stat-icon.subjects { background-color: #dcfce7; color: #22c55e; }
.stat-icon.year { background-color: #ffedd5; color: #f97316; }
.stat-content h3 { margin: 0 0 4px 0; font-size: 2rem; font-weight: 700; color: var(--dark-color); }
.stat-content p { margin: 0; color: var(--secondary-color); font-size: 0.9rem; }
.dashboard-main-content { display: grid; grid-template-columns: 2fr 1fr; gap: 2rem; align-items: flex-start; }

.student-tabs-nav { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border-color); }
.student-tab-btn { background: none; border: none; padding: 0.5rem 1rem; cursor: pointer; font-size: 1rem; font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px; }
.student-tab-btn.active { border-bottom-color: var(--primary-color); color: var(--primary-color); }
.student-tab-panel { display: none; }
.student-tab-panel.active { display: block; }
.student-list-by-class { list-style: decimal; padding-left: 2rem; margin: 0; max-height: 40vh; overflow-y: auto; }
.student-list-by-class li { padding: 0.4rem 0; }
.top-student-list { list-style: none; padding: 0; margin: 0; }
.top-student-list li { display: flex; align-items: center; gap: 0.75rem; padding: 0.5rem 0; border-bottom: 1px solid var(--border-color); }
.top-student-list li:last-child { border-bottom: none; }
.top-student-list .rank { font-weight: 700; color: var(--primary-color); width: 20px; }

/* Promote View */
.promote-tabs-nav { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border-color); }
.promote-tab-btn { background: none; border: none; padding: 0.5rem 1rem; cursor: pointer; font-size: 1rem; font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px; }
.promote-tab-btn.active { border-bottom-color: var(--primary-color); color: var(--primary-color); }
.promote-tab-panel { display: none; } 
.promote-tab-panel.active { display: block; }
.promote-layout { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.5rem; }
.promote-class-card { display: flex; flex-direction: column; border: 1px solid var(--border-color); border-radius: 8px; background-color: white; overflow: hidden; }
.promote-class-header { display: flex; justify-content: space-between; align-items: center; padding: 0.75rem 1rem; background-color: var(--light-color); border-bottom: 1px solid var(--border-color); }
.promote-class-header label { font-weight: 600; display: flex; align-items: center; gap: 0.5rem; white-space: nowrap; }
.student-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; max-height: 65vh; }
.student-list-item { padding: 0.5rem 1rem; border-bottom: 1px solid #f0f0f0; display: flex; align-items: center; gap: 0.75rem; }
.student-list-item input[type="checkbox"] { width: auto; flex-shrink: 0; height: 1em; width: 1em; }
.student-list-item .student-info { display: flex; gap: 0.5rem; align-items: baseline; }
.student-list-item .student-id { font-size: 0.85em; color: var(--secondary-color); width: 50px; }
.student-list-item:last-child { border-bottom: none; }
.subject-input-group { display: flex; gap: 0.5rem; align-items: center; }
.subject-input-group input { flex-grow: 1; }
.subject-input-group button { flex-shrink: 0; }

/* Badges */
.badge { padding: 0.25rem 0.75rem; border-radius: 9999px; font-size: 0.85rem; font-weight: 600; display: inline-block; text-align: center; }
.badge-success { background-color: #dcfce7; color: #166534; border: 1px solid #bbf7d0; } 
.badge-warning { background-color: #fef08a; color: #854d0e; border: 1px solid #fde047; } 
.badge-danger { background-color: #fee2e2; color: #991b1b; border: 1px solid #fecaca; } 
.badge-secondary { background-color: #f3f4f6; color: #374151; border: 1px solid #e5e7eb; } 

.cart-panel { background-color: #f8fafc; border: 1px solid var(--border-color); border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }
.cart-panel h4 { margin-top: 0; margin-bottom: 0.75rem; color: var(--dark-color); font-size: 1rem; display: flex; align-items: center; gap: 0.5rem; }
.cart-chips { display: flex; flex-wrap: wrap; gap: 0.5rem; max-height: 120px; overflow-y: auto; padding-bottom: 5px; }
.chip { background-color: white; border: 1px solid var(--primary-color); color: var(--primary-color); padding: 0.3rem 0.6rem; border-radius: 16px; font-size: 0.85rem; display: inline-flex; align-items: center; gap: 0.4rem; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
.chip button { background: none; border: none; color: var(--danger-color); cursor: pointer; padding: 0 0 0 4px; font-weight: bold; display: flex; align-items: center; justify-content: center; }
.chip button:hover { color: #b91c1c; }

/* Modal Improvements */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); z-index: 10000; display: none; justify-content: center; align-items: center; padding: 1rem; }
.modal-content { background: white; padding: 2rem; border-radius: 12px; width: 95%; max-width: 800px; max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.modal-header h3 { margin: 0; font-size: 1.4rem;}
.close-btn { font-size: 2rem; font-weight: bold; cursor: pointer; border: none; background: none; line-height: 1; color: var(--secondary-color); }
.close-btn:hover { color: var(--danger-color); }
.modal-body { overflow-y: auto; flex-grow: 1; }
.modal-footer { margin-top: 1.5rem; text-align: right; display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--border-color); padding-top: 1rem;}
.modal-filters { display: flex; gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.modal-filters input, .modal-filters select { flex-grow: 1; min-width: 150px; padding: 0.5rem; }

.owner-badge { font-size: 0.75rem; padding: 0.2rem 0.5rem; border-radius: 4px; display: inline-block; white-space: nowrap; }
.owner-badge.available { background: #dcfce7; color: #166534; } 
.owner-badge.in-cart { background: #e0e7ff; color: #4338ca; font-weight: bold; } 
.owner-badge.locked { background: #fee2e2; color: #991b1b; } 

.modal-body .table-container tbody tr { cursor: pointer; transition: background-color 0.1s; }
.modal-body .table-container tbody tr:hover { background-color: #f9fafb; }
.modal-body .table-container tbody tr.locked { background-color: #f3f4f6; color: #9ca3af; cursor: not-allowed; }
.modal-body .table-container tbody tr.in-cart { background-color: #eef2ff; }

/* ------------------------------------------------------------------------- */
/* NEW: สไตล์เฉพาะระบบสอนร่วม (Co-Grading & Dynamic Multi-column Table)     */
/* ------------------------------------------------------------------------- */
.mode-badge {
    display: inline-flex; align-items: center; gap: 0.4rem; padding: 0.3rem 0.8rem;
    border-radius: 9999px; font-size: 0.85rem; font-weight: 700;
}
.mode-badge.green { background-color: #dcfce7; color: #15803d; border: 1px solid #86efac; }
.mode-badge.orange { background-color: #ffedd5; color: #c2410c; border: 1px solid #fdba74; }

/* หัวตารางซ้อน 2 ชั้น */
.table-container table thead tr.sub-header th {
    font-size: 0.8rem; padding: 0.4rem 0.6rem;
    background-color: #f1f5f9; color: var(--secondary-color); font-weight: 600; text-align: center;
}

/* ช่องคะแนนเพื่อนครู (Read-only สบายตา) */
.score-partner {
    background-color: #f8fafc; color: #64748b; font-weight: 600;
    text-align: center; display: inline-block; width: 100%;
    padding: 0.5rem 0.2rem; border-radius: 6px; border: 1px dashed #cbd5e1; user-select: none;
}

/* ช่องคะแนนของเรา */
.score-mine {
    font-weight: 700 !important; color: var(--primary-color) !important;
    text-align: center !important; border-color: #c7d2fe !important;
}

/* ช่องผลรวมคะแนนต่อภาคเรียน */
.score-total-sem {
    font-weight: 700; color: var(--dark-color);
    text-align: center; background-color: #f8fafc;
}

/* ----------------------------------------------------- */
/* Media Queries & Print Styles                          */
/* ----------------------------------------------------- */
@media (max-width: 992px) { 
    .dashboard-main-content { grid-template-columns: 1fr; } 
}

@media print { 
    @page { size: A4; margin: 0; } 
    body { -webkit-print-color-adjust: exact; print-color-adjust: exact; } 
    body * { visibility: hidden; } 
    .print-area, .print-area * { visibility: visible; } 
    .print-area { 
        position: absolute; left: 0; top: 0; width: 100%; padding: 1.5cm; box-sizing: border-box; 
        font-family: 'Sarabun', sans-serif; font-size: 16px; color: black; 
    } 
    .report-header { text-align: center; margin-bottom: 1rem; } 
    .report-header #printLogo { display: block; margin: 0 auto 1rem; width: 80px; height: auto; } 
    .report-header h3 { font-size: 18px; font-weight: 700; margin: 0; } 
    .report-header p { font-size: 16px; margin: 4px 0; } 
    .student-info-grid { text-align: center; margin-bottom: 1rem; font-size: 16px; } 
    .student-info-grid span { margin: 0 1em; } 
    .table-container { border: none; overflow: visible; } 
    table { font-size: 12px; width: 100%; } 
    th { font-size: 14px; font-weight: 700; } 
    td { padding: 5px; } 
    #studentGradesTable .col-subject { width: 35%; text-align: left; } 
    #studentGradesTable td { text-align: center; } 
    .report-footer { margin-top: 1.5rem; display: flex; justify-content: space-between; align-items: flex-start; page-break-inside: avoid; } 
    .summary-table { width: 55%; font-size: 14px; } 
    .summary-table td:nth-child(1) { font-weight: 700; text-align: left; } 
    .summary-table td:nth-child(2), .summary-table td:nth-child(3) { text-align: center; } 
    .signature-container { width: 40%; display: flex; flex-direction: column; gap: 40px; text-align: center; } 
    .signature-area { text-align: center; font-size: 16px; } 
    .signature-area p { margin: 5px 0; } 
}
/* ----------------------------------------------------------------- */
/* ล็อกปุ่มจัดการ (แก้ไข/ลบ) ให้ลอยติดขอบขวาตลอดเวลา บนจอขนาดเล็ก       */
/* ----------------------------------------------------------------- */
#teachersTable th:last-child,
#teachersTable td:last-child {
    position: sticky;
    right: 0;
    background-color: white;
    box-shadow: -4px 0 8px rgba(0, 0, 0, 0.06); /* เงาฟุ้งบอกว่ามีปุ่มลอยอยู่ */
    z-index: 5;
    text-align: center;
    min-width: 120px;
}

#teachersTable thead th:last-child {
    background-color: #f1f5f9;
    z-index: 6;
}

/* ช่องรายวิชาที่สอน: บังคับตัดคำขึ้นบรรทัดใหม่ ไม่ให้ดันตารางยาวเกินไป */
#teachersTable td:nth-child(4) {
    max-width: 320px;
    white-space: normal !important; /* ยอมให้ตัดคำ */
    word-break: break-word;
    font-size: 0.85rem;
    color: #475569;
    line-height: 1.5;
}
/* ----------------------------------------------------------------- */
/* ระบบย่อแถบเมนูด้านซ้าย (Sidebar Collapsed Mode) เหลือแต่ไอคอน      */
/* ----------------------------------------------------------------- */
.sidebar.collapsed {
    width: 75px !important;
}
.sidebar.collapsed .sidebar-header h1,
.sidebar.collapsed .sidebar-header p,
.sidebar.collapsed .sidebar-btn span,
.sidebar.collapsed .sidebar-footer .text-content,
.sidebar.collapsed #sidebarMobileQrBox span {
    display: none !important;
}
.sidebar.collapsed .sidebar-logo {
    width: 40px; height: 40px; margin-bottom: 0;
}
.sidebar.collapsed .sidebar-btn {
    justify-content: center; padding: 0.9rem 0;
}
.sidebar.collapsed .sidebar-footer .footer-item {
    justify-content: center;
}
.sidebar.collapsed #sidebarMobileQrBox {
    padding: 4px;
}
.sidebar.collapsed #sidebarMobileQrBox img {
    width: 42px !important; height: 42px !important;
}

/* ปุ่มกดสลับย่อ-ขยายเมนู */
.btn-toggle-sidebar {
    background: none; border: none; font-size: 1.25rem; cursor: pointer;
    padding: 0.3rem 0.6rem; border-radius: 6px; color: var(--sidebar-text);
    display: flex; align-items: center; justify-content: center;
}
.btn-toggle-sidebar:hover { background-color: var(--sidebar-hover-bg); color: var(--primary-color); }