/* ============================================================
   ระบบจัดตารางเรียนตารางสอน ระดับอาชีวศึกษา (Vocational Timetable)
   Styles: Modern, Responsive, Drag-and-Drop, Print-Ready
   ============================================================ */

:root {
    --primary: #1e40af;
    --primary-light: #3b82f6;
    --primary-dark: #1e3a8a;
    --secondary: #0f766e;
    --accent: #f59e0b;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --success: #16a34a;
    --success-light: #dcfce7;
    --warning: #d97706;
    --bg-page: #f8fafc;
    --bg-card: #ffffff;
    --border-color: #e2e8f0;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Sarabun', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-page);
    color: var(--text-main);
    line-height: 1.5;
    font-size: 15px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.main-header {
    background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
    color: #ffffff;
    box-shadow: var(--shadow-md);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0.75rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1.5rem;
    flex-wrap: wrap;
}

.brand-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    color: #ffffff;
}

.brand-icon {
    font-size: 2rem;
    background: rgba(255, 255, 255, 0.15);
    padding: 6px 10px;
    border-radius: var(--radius-md);
}

.brand-title {
    font-size: 1.15rem;
    font-weight: 700;
    display: block;
    letter-spacing: -0.2px;
}

.brand-sub {
    font-size: 0.8rem;
    color: #93c5fd;
    display: block;
}

.main-nav ul {
    display: flex;
    list-style: none;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.main-nav a {
    color: #e2e8f0;
    text-decoration: none;
    padding: 0.45rem 0.85rem;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.main-nav a:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #ffffff;
}

.main-nav a.active {
    background: #2563eb;
    color: #ffffff;
    box-shadow: 0 0 10px rgba(37, 99, 235, 0.5);
}

.term-badge {
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.25);
    padding: 0.35rem 0.85rem;
    border-radius: 9999px;
    font-size: 0.85rem;
    font-weight: 600;
    color: #fef08a;
}

/* Layout */
.main-content {
    flex: 1;
    max-width: 1400px;
    width: 100%;
    margin: 1.5rem auto;
    padding: 0 1.5rem;
}

.main-footer {
    background: #0f172a;
    color: #94a3b8;
    padding: 1.25rem 1.5rem;
    text-align: center;
    font-size: 0.85rem;
    margin-top: auto;
    border-top: 1px solid #1e293b;
}

/* Cards & Containers */
.card {
    background: var(--bg-card);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    margin-bottom: 1.5rem;
    overflow: hidden;
}

.card-header {
    padding: 1rem 1.25rem;
    background: #ffffff;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.card-title {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-body {
    padding: 1.25rem;
}

/* Stats Grid on Dashboard */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1.25rem;
    margin-bottom: 1.5rem;
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.25rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
}

.stat-icon.blue { background: #dbeafe; color: #1e40af; }
.stat-icon.green { background: #dcfce7; color: #166534; }
.stat-icon.amber { background: #fef3c7; color: #92400e; }
.stat-icon.purple { background: #f3e8ff; color: #6b21a8; }
.stat-icon.indigo { background: #e0e7ff; color: #3730a3; }

.stat-data h4 {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
}

.stat-number {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.2;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.45rem 0.9rem;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s ease;
    font-family: inherit;
}

.btn-primary { background: #2563eb; color: #ffffff; }
.btn-primary:hover { background: #1d4ed8; }

.btn-secondary { background: #f1f5f9; color: #334155; border-color: #cbd5e1; }
.btn-secondary:hover { background: #e2e8f0; }

.btn-success { background: #16a34a; color: #ffffff; }
.btn-success:hover { background: #15803d; }

.btn-danger { background: #dc2626; color: #ffffff; }
.btn-danger:hover { background: #b91c1c; }

.btn-warning { background: #f59e0b; color: #ffffff; }
.btn-warning:hover { background: #d97706; }

.btn-sm { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
.btn-lg { padding: 0.65rem 1.25rem; font-size: 1rem; }

/* Form Controls */
.form-group {
    margin-bottom: 1rem;
}

.form-label {
    display: block;
    margin-bottom: 0.35rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: #334155;
}

.form-control, .form-select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius-sm);
    background-color: #ffffff;
    font-family: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.form-control:focus, .form-select:focus {
    border-color: #3b82f6;
    outline: 0;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.filter-bar {
    background: #ffffff;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

/* ============================================================
   Schedule Workspace (Drag & Drop)
   ============================================================ */
.schedule-workspace {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 1.25rem;
    align-items: start;
}

@media (max-width: 1024px) {
    .schedule-workspace {
        grid-template-columns: 1fr;
    }
}

/* Sidebar - Unassigned Subjects */
.subjects-sidebar {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    position: sticky;
    top: 5rem;
    max-height: calc(100vh - 6rem);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 0.85rem 1rem;
    background: #f8fafc;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h3 {
    font-size: 1rem;
    font-weight: 700;
    color: #1e293b;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.block-duration-selector {
    margin-top: 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.block-duration-selector select {
    padding: 0.2rem 0.5rem;
    font-size: 0.85rem;
}

.subject-list {
    padding: 0.75rem;
    overflow-y: auto;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

/* Draggable Subject Item */
.subject-card-drag {
    background: #ffffff;
    border: 1px solid #cbd5e1;
    border-left: 4px solid #2563eb;
    border-radius: var(--radius-sm);
    padding: 0.75rem;
    cursor: grab;
    user-select: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    transition: all 0.2s ease;
}

.subject-card-drag:hover {
    box-shadow: var(--shadow-md);
    border-color: #3b82f6;
    transform: translateY(-1px);
}

.subject-card-drag.dragging {
    opacity: 0.45;
    cursor: grabbing;
    background: #eff6ff;
    border-style: dashed;
}

.subject-card-drag.completed {
    border-left-color: #16a34a;
    background: #f8fafc;
    opacity: 0.8;
}

.subj-code-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 700;
    background: #eff6ff;
    color: #1d4ed8;
    padding: 0.15rem 0.45rem;
    border-radius: 4px;
    margin-bottom: 0.25rem;
}

.subj-name {
    font-size: 0.9rem;
    font-weight: 600;
    color: #0f172a;
    margin-bottom: 0.35rem;
    line-height: 1.3;
}

.subj-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.subj-tpn {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    color: #0f766e;
    font-weight: 600;
}

.subj-progress {
    margin-top: 0.5rem;
    background: #e2e8f0;
    border-radius: 9999px;
    height: 6px;
    overflow: hidden;
}

.subj-progress-bar {
    background: #2563eb;
    height: 100%;
    transition: width 0.3s;
}

.subj-progress-bar.full {
    background: #16a34a;
}

/* ============================================================
   Timetable Grid
   ============================================================ */
.timetable-container {
    background: #ffffff;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    overflow-x: auto;
    padding: 0.5rem;
}

.timetable {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    min-width: 980px;
}

.timetable th, .timetable td {
    border: 1px solid #cbd5e1;
    text-align: center;
    padding: 0.35rem;
    vertical-align: top;
}

/* Day Column Header */
.timetable th.day-col {
    width: 100px;
    background: #f1f5f9;
    font-weight: 700;
    font-size: 0.85rem;
    color: #1e293b;
}

/* Period Header */
.timetable th.period-header {
    background: #f8fafc;
    padding: 0.5rem 0.25rem;
    font-size: 0.8rem;
}

.p-number {
    font-weight: 700;
    color: #1e293b;
    font-size: 0.85rem;
    display: block;
}

.p-time {
    font-size: 0.7rem;
    color: #64748b;
    display: block;
}

/* Lunch Column */
.lunch-col {
    background: #fef3c7 !important;
    color: #92400e;
    width: 50px !important;
    vertical-align: middle !important;
    font-size: 0.75rem;
    font-weight: 600;
    writing-mode: vertical-rl;
    text-orientation: upright;
    letter-spacing: 2px;
}

/* Day Label in Row */
.day-label {
    font-weight: 700;
    font-size: 0.9rem;
    padding: 0.75rem 0.5rem;
    text-align: center;
    border-left: 6px solid #2563eb;
}

/* Grid Cell / Drop Zone */
.slot-cell {
    height: 95px;
    position: relative;
    background: #ffffff;
    transition: background-color 0.15s, outline 0.15s;
    padding: 2px !important;
}

.slot-cell.drag-over {
    background-color: rgba(59, 130, 246, 0.18) !important;
    outline: 2px dashed #2563eb !important;
    outline-offset: -2px;
}

.slot-cell.drag-invalid {
    background-color: rgba(220, 38, 38, 0.15) !important;
    outline: 2px dashed #dc2626 !important;
}

/* Scheduled Block inside Cell */
.schedule-block {
    background: linear-gradient(135deg, #1e40af 0%, #2563eb 100%);
    color: #ffffff;
    border-radius: var(--radius-sm);
    padding: 0.45rem;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-shadow: 0 2px 4px rgba(30, 64, 175, 0.2);
    position: relative;
    text-align: left;
    overflow: hidden;
    font-size: 0.8rem;
    border-left: 3px solid #60a5fa;
}

/* Color variations by subject type */
.schedule-block.type-theory {
    background: linear-gradient(135deg, #0369a1 0%, #0284c7 100%);
    border-left-color: #38bdf8;
}

.schedule-block.type-practice {
    background: linear-gradient(135deg, #0f766e 0%, #0d9488 100%);
    border-left-color: #2dd4bf;
}

.schedule-block.type-activity {
    background: linear-gradient(135deg, #9333ea 0%, #a855f7 100%);
    border-left-color: #d8b4fe;
}

.block-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.25rem;
}

.block-code {
    font-weight: 700;
    font-size: 0.75rem;
    color: #fef08a;
}

.block-delete-btn {
    background: rgba(0, 0, 0, 0.25);
    border: none;
    color: #ffffff;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    line-height: 1;
    transition: background 0.15s;
}

.block-delete-btn:hover {
    background: #dc2626;
}

.block-name {
    font-weight: 600;
    font-size: 0.8rem;
    line-height: 1.25;
    margin: 0.2rem 0;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.block-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    font-size: 0.72rem;
    color: #e0f2fe;
    border-top: 1px solid rgba(255, 255, 255, 0.2);
    padding-top: 0.2rem;
}

.block-teacher, .block-room {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    white-space: nowrap;
}

/* Modals - ป้องกันการล้นหน้าจอ (Responsive & Scrollable) */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.65);
    backdrop-filter: blur(2px);
    align-items: center;
    justify-content: center;
    z-index: 10000;
    overflow-y: auto;
    padding: 12px;
    box-sizing: border-box;
}

.modal-backdrop[style*="display: none"],
.modal-backdrop[style*="display:none"],
.modal-backdrop:not(.show):not([style*="display: flex"]):not([style*="display:flex"]) {
    display: none !important;
}

.modal-backdrop.show,
.modal-backdrop[style*="display: flex"],
.modal-backdrop[style*="display:flex"] {
    display: flex !important;
}

.modal-box {
    background: #ffffff;
    border-radius: var(--radius-md, 12px);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3), 0 10px 10px -5px rgba(0, 0, 0, 0.2);
    width: 95%;
    max-width: 560px;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin: auto;
    position: relative;
    animation: modalSlide 0.2s ease-out;
}

@keyframes modalSlide {
    from { opacity: 0; transform: translateY(-20px) scale(0.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-header {
    flex-shrink: 0;
    padding: 1rem 1.25rem;
    background: #f8fafc;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.modal-danger .modal-header {
    background: #fee2e2;
    border-bottom-color: #fecaca;
}

.modal-danger .modal-header h3 {
    color: #991b1b;
}

.modal-header h3 {
    font-size: 1.1rem;
    font-weight: 700;
    flex: 1;
}

.modal-icon {
    font-size: 1.4rem;
}

.close-btn {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #64748b;
    line-height: 1;
}

.close-btn:hover {
    color: #0f172a;
}

.modal-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(88vh - 130px);
    padding: 1.25rem;
    font-size: 0.95rem;
    line-height: 1.6;
    color: #334155;
}

.modal-footer {
    flex-shrink: 0;
    padding: 0.85rem 1.25rem;
    background: #f8fafc;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 2000;
}

.toast {
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-sm);
    color: #ffffff;
    font-size: 0.9rem;
    font-weight: 500;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 250px;
    animation: toastFade 0.25s ease-out;
}

@keyframes toastFade {
    from { opacity: 0; transform: translateY(15px); }
    to { opacity: 1; transform: translateY(0); }
}

.toast.toast-success { background: #16a34a; }
.toast.toast-error { background: #dc2626; }
.toast.toast-info { background: #2563eb; }

/* Tables */
.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th, .data-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    text-align: left;
    font-size: 0.9rem;
}

.data-table th {
    background: #f8fafc;
    font-weight: 600;
    color: #475569;
}

.data-table tr:hover {
    background-color: #f8fafc;
}

/* Badges */
.badge {
    display: inline-block;
    padding: 0.2rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-primary { background: #dbeafe; color: #1e40af; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-secondary { background: #f1f5f9; color: #475569; }

/* ============================================================
   Print Stylesheet (สำหรับพิมพ์ตารางสอนระดับอาชีวศึกษา)
   ============================================================ */
@page {
    margin: 0mm;
}

@media print {
    @page {
        size: A4 landscape;
        margin: 0mm;
    }

    body {
        background: #ffffff !important;
        color: #000000 !important;
        font-size: 11pt !important;
    }

    .no-print {
        display: none !important;
    }

    .main-content {
        margin: 0 !important;
        padding: 0 !important;
        max-width: 100% !important;
    }

    .timetable-container {
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
    }

    .timetable {
        min-width: 100% !important;
    }

    .timetable th, .timetable td {
        border: 1px solid #000000 !important;
        color: #000000 !important;
        padding: 2px !important;
    }

    .schedule-block {
        background: none !important;
        color: #000000 !important;
        border: 1px solid #333333 !important;
        box-shadow: none !important;
    }

    .block-delete-btn {
        display: none !important;
    }

    .block-code {
        color: #000000 !important;
        font-weight: bold !important;
    }

    .block-footer {
        color: #333333 !important;
        border-top: 1px dashed #666666 !important;
    }

    .print-signatures {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
        margin-top: 30px;
        text-align: center;
        page-break-inside: avoid;
    }
}
