/* Global Styles */
:root {
    --primary-color: #007bff;
    --primary-hover: #0056b3;
    --success-color: #28a745;
    --success-hover: #218838;
    --danger-color: #dc3545;
    --secondary-color: #6c757d;
    --secondary-hover: #5a6268;
    --dark-bg: #343a40;
    --light-bg: #f8f9fa;
    --border-color: #dee2e6;
    --text-color: #333;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 8px rgba(0,0,0,0.1);
    --radius: 8px;
    
    /* Semantic Colors (Light Mode Default) */
    --bg-body: #f0f2f5;
    --bg-surface: #ffffff;
    --text-primary: #333333;
    --text-secondary: #495057;
    --text-heading: #2c3e50;
    --input-bg: #ffffff;
    --input-border: #ced4da;
    --table-header-bg: #343a40;
    --table-header-text: #ffffff;
    --day-label-bg: #f8f9fa;
    --break-header-bg: #e9ecef;
    --break-cell-bg: #f8f9fa;
    --break-text-color: #adb5bd;
    --item-bg-default: #e3f2fd;
    --item-border-default: #90caf9;
    --gap-bg: #ffe6e6;
}

[data-theme="dark"] {
    --bg-body: #121212;
    --bg-surface: #1e1e1e;
    --text-primary: #e0e0e0;
    --text-secondary: #b0b0b0;
    --text-heading: #ffffff;
    --border-color: #444444;
    --input-bg: #2c2c2c;
    --input-border: #555555;
    --table-header-bg: #252525;
    --table-header-text: #e0e0e0;
    --day-label-bg: #2c2c2c;
    --break-header-bg: #2c2c2c;
    --break-cell-bg: #1e1e1e;
    --break-text-color: #666666;
    --light-bg: #2c2c2c;
    --dark-bg: #1e1e1e;
    --item-bg-default: #1a3b5c;
    --item-border-default: #1565c0;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.5);
    --shadow-md: 0 4px 8px rgba(0,0,0,0.5);
    --gap-bg: #4a2c2c;
}

body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-body);
    margin: 0;
    padding: 0;
    color: var(--text-primary);
    line-height: 1.6;
    zoom: 60%;
}

h1 {
    text-align: center;
    color: var(--text-heading);
    margin-top: 1rem;
    margin-bottom: 1rem;
    font-weight: 700;
}

h3 {
    margin-top: 0;
    color: var(--text-secondary);
    font-weight: 600;
}

/* --- Index / Form Page --- */
.container-narrow {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 0 1.5rem;
}

.form-container { 
    background: var(--bg-surface); 
    padding: 1.5rem; 
    border-radius: var(--radius); 
    box-shadow: var(--shadow-md); 
    display: flex;
    gap: 20px;
    align-items: center;
}

/* Subject Pool */
.pool-section { 
    margin-bottom: 0; 
    padding: 1rem; 
    background: var(--bg-surface); 
    border: 1px solid var(--border-color);
    border-radius: var(--radius); 
    flex: 0 0 240px;
    position: sticky;
    top: 20px;
    box-shadow: var(--shadow-sm);
}

.pool-controls { display: flex; gap: 8px; margin-bottom: 15px; }

.pool-controls input[type="text"] { 
    flex: 1; 
    padding: 8px 12px; 
    border: 1px solid var(--input-border); 
    border-radius: 4px; 
    font-size: 0.9rem;
    background: var(--input-bg);
    color: var(--text-primary);
}

.color-picker {
    flex: 0 0 40px;
    padding: 0;
    height: 38px;
    border: none;
    background: none;
    cursor: pointer;
}

.btn-add-subject { 
    background: var(--success-color); 
    color: white; 
    border: none; 
    padding: 8px 16px; 
    border-radius: 4px; 
    cursor: pointer; 
    font-weight: 600;
    transition: background 0.2s;
    white-space: nowrap;
}
.btn-add-subject:hover { background: var(--success-hover); }

.subject-list { display: flex; flex-wrap: wrap; gap: 8px; min-height: 40px; align-content: flex-start; }

.draggable-subject { 
    background: var(--primary-color); 
    color: white; 
    padding: 6px 12px; 
    border-radius: 20px; 
    cursor: grab; 
    font-size: 0.85rem; 
    box-shadow: var(--shadow-sm);
    display: inline-flex;
    align-items: center;
    transition: transform 0.1s, box-shadow 0.1s;
}
.draggable-subject:hover { transform: translateY(-1px); box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
.draggable-subject:active { cursor: grabbing; transform: translateY(0); }

.remove-subject {
    margin-left: 8px;
    color: rgba(255,255,255,0.8);
    cursor: pointer;
    font-weight: bold;
    font-size: 1.1em;
    line-height: 1;
}
.remove-subject:hover { color: white; }

.help-text { font-size: 0.85rem; color: #6c757d; margin-top: 10px; font-style: italic; }

/* Form Layout */
.form-container form {
    flex: 1;
    min-width: 0;
}

/* Schedule Grid */
/* Wrapper handles scrolling inside the flex layout */
.schedule-wrapper {
    flex: 1;
    min-width: 0; /* allow flex child to shrink and enable overflow */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.schedule-table { 
    width: 100%; 
    min-width: 720px; /* ensure table inherently large enough so wrapper can scroll */
    border-collapse: separate; 
    border-spacing: 0;
    table-layout: fixed; 
    font-size: 0.8rem; 
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    /* overflow moved to wrapper */
}

/* Make non-day columns a reasonable minimum so they don't collapse and force overflow */
.schedule-table th,
.schedule-table td:not(.day-label) {
    min-width: 140px;
}

/* Ensure cell content wraps and truncates safely */
.schedule-table td .scheduled-item {
    max-width: 100%;
    box-sizing: border-box;
    word-break: break-word;
    overflow: hidden;
}

.schedule-table td .scheduled-item strong {
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis;
    display: block;
}

.schedule-table th { 
    background: var(--table-header-bg); 
    color: var(--table-header-text); 
    padding: 8px; 
    font-weight: 600; 
    font-size: 0.8rem;
    text-transform: uppercase;
    border-bottom: 1px solid var(--border-color);
}

.schedule-table td { 
    border-right: 1px solid var(--border-color); 
    border-bottom: 1px solid var(--border-color); 
    height: 85px; 
    vertical-align: top; 
    padding: 4px; 
    background: var(--bg-surface); 
    transition: background 0.2s;
}
.schedule-table tr:last-child td { border-bottom: none; }
.schedule-table td:last-child { border-right: none; }

.schedule-table td.day-label { 
    background: var(--day-label-bg); 
    font-weight: 700; 
    vertical-align: middle; 
    text-align: center; 
    width: 80px; 
    color: var(--text-secondary);
}

.btn-copy-day {
    display: block;
    margin: 4px auto 0;
    font-size: 0.7rem;
    padding: 2px 6px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    color: var(--text-secondary);
    cursor: pointer;
}
.btn-copy-day:hover {
    background: var(--break-header-bg);
    color: var(--text-primary);
}

.btn-clear-day {
    display: block;
    margin: 4px auto 0;
    font-size: 0.7rem;
    padding: 2px 6px;
    background: transparent;
    border: 1px solid var(--danger-color);
    border-radius: 4px;
    color: var(--danger-color);
    cursor: pointer;
}
.btn-clear-day:hover {
    background: var(--danger-color);
    color: white;
}

.drop-zone.drag-over { background: var(--break-header-bg); box-shadow: inset 0 0 0 2px var(--primary-color); }

/* Dropped Items */
.scheduled-item { 
    background: var(--item-bg-default); border: 1px solid var(--item-border-default); 
    padding: 4px; margin-bottom: 2px; border-radius: 4px; 
    position: relative; 
    font-size: 0.75rem;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.scheduled-item .remove-btn { 
    position: absolute; top: 4px; right: 6px; 
    color: var(--danger-color); cursor: pointer; font-weight: bold; font-size: 1.1rem; line-height: 1;
    opacity: 0.6; transition: opacity 0.2s;
}
.scheduled-item .remove-btn:hover { opacity: 1; }

.scheduled-item strong { 
    display: block; margin-bottom: 2px; padding-right: 15px; 
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-primary) !important;
}
.scheduled-item select, .scheduled-item input { 
    width: 100%; margin-top: 2px; padding: 2px; 
    border: 1px solid var(--input-border); border-radius: 4px; font-size: 0.8rem; 
    background: var(--input-bg); color: var(--text-primary);
}

.btn-generate { 
    background-color: var(--primary-color); color: white; 
    flex: 1;
    padding: 10px; font-size: 1rem; border: none; 
    border-radius: var(--radius); cursor: pointer; font-weight: 600;
    transition: background 0.2s;
}
.btn-generate:hover { background-color: var(--primary-hover); }

.btn-secondary { background-color: var(--secondary-color); }
.btn-secondary:hover { background-color: var(--secondary-hover); }

.form-actions {
    display: flex;
    gap: 15px;
    margin-top: 20px;
}

/* --- Timetable View Page --- */
.container-wide {
    max-width: 1400px; margin: 2rem auto; padding: 0 1.5rem;
}
.table-wrapper { 
    overflow-x: auto; box-shadow: var(--shadow-md); 
    border-radius: var(--radius); background: var(--bg-surface); 
    border: 1px solid var(--border-color);
}

.view-table { width: 100%; border-collapse: collapse; min-width: 900px; table-layout: fixed; }
.view-table th { 
    background-color: var(--table-header-bg); color: var(--table-header-text); padding: 12px; 
    text-transform: uppercase; letter-spacing: 0.5px; font-size: 0.85rem; font-weight: 600;
    border-bottom: 2px solid var(--border-color);
}
.view-table td { 
    vertical-align: top; border-right: 1px solid var(--border-color); 
    padding: 6px; height: 100px; background-color: var(--bg-surface); transition: background-color 0.2s;
}
.view-table tr:last-child td { border-bottom: none; }
.view-table td:last-child { border-right: none; }

.view-table .day-label {
    vertical-align: middle;
    text-align: center;
    font-weight: 700;
    background-color: var(--day-label-bg);
    color: var(--text-secondary);
}

.class-block {
    background-color: var(--bg-color, #e3f2fd);
    border-left: 4px solid var(--border-color, #2196f3);
    padding: 8px 10px;
    margin-bottom: 0;
    border-radius: 4px;
    font-size: 0.9rem;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.class-block:hover { transform: translateY(-2px); box-shadow: 0 4px 6px rgba(0,0,0,0.12); z-index: 1; position: relative; }
.subject { font-weight: 700; color: var(--text-color, #1565c0); display: block; font-size: 0.95rem; margin-bottom: 3px; line-height: 1.2; }
.time { font-size: 0.75rem; color: #555; display: block; margin-bottom: 2px; opacity: 0.9; }
.info-label { font-size: 0.75rem; color: #666; font-style: normal; display: block; background: rgba(255,255,255,0.5); padding: 1px 4px; border-radius: 3px; align-self: flex-start; margin-top: 2px; }

/* Break Column Styles */
.break-header {
    width: 40px;
    background-color: var(--break-header-bg) !important;
    color: var(--text-secondary) !important;
    writing-mode: vertical-rl;
    text-orientation: mixed;
    padding: 5px 0 !important;
    vertical-align: middle;
    border-bottom: 2px solid var(--border-color) !important;
    cursor: default;
}

.break-cell {
    background-color: var(--break-cell-bg) !important;
    text-align: center;
    vertical-align: middle !important;
    padding: 0 !important;
}

.break-text {
    writing-mode: vertical-rl;
    margin: 0 auto;
    font-size: 0.75rem;
    color: var(--break-text-color);
    text-transform: uppercase;
    letter-spacing: 2px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Break Block Style */
.break-block {
    background-color: #fff3cd;
    border-left: 4px solid #ffc107;
}

.free-slot {
    text-align: center;
    color: var(--break-text-color);
    margin-top: 40px;
    font-style: italic;
    font-weight: 500;
}

.gap-slot {
    background-color: var(--gap-bg);
    vertical-align: middle;
    text-align: center;
}
.gap-label {
    color: var(--danger-color);
    font-weight: bold;
    font-size: 0.8rem;
    opacity: 0.7;
    font-style: italic;
}

.action-buttons {
    margin-top: 2rem;
    text-align: center;
    display: flex;
    justify-content: center;
    gap: 15px;
}

.btn-download {
    background-color: var(--success-color);
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 1rem;
    color: white;
    padding: 12px 24px;
    border-radius: 6px;
    font-weight: 600;
    transition: background 0.2s;
}
.btn-download:hover { background-color: var(--success-hover); }

.btn-back { 
    display: inline-block; text-align: center; background: var(--secondary-color); 
    color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; 
    font-weight: 600; transition: background 0.2s; 
}
.btn-back:hover { background: var(--secondary-hover); }

/* Responsive Adjustments */
@media (max-width: 1024px) {
    .form-container {
        flex-direction: column;
        align-items: stretch;
    }
    .pool-section {
        flex: none;
        width: auto;
        position: static;
        margin-bottom: 20px;
    }
    .schedule-table {
        min-width: 600px;
    }
    .table-wrapper {
        overflow-x: auto;
    }
}

/* Print Styles */
@media print {
    @page { size: landscape; margin: 0.5cm; }
    body { zoom: 100%; background-color: white; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .action-buttons, .btn-back, .btn-download, .theme-toggle { display: none !important; }
    .container-wide { margin: 0; padding: 0; max-width: 100%; width: 100%; }
    .table-wrapper { box-shadow: none; border: none; overflow: visible; }
    .view-table { min-width: 100%; width: 100%; table-layout: auto; }
    .view-table th, .view-table td { border: 1px solid #333; }
    h1 { margin-top: 0; margin-bottom: 10px; }
    .class-block { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* Theme Toggle Button */
.theme-toggle {
    position: fixed;
    top: 20px;
    right: 20px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: 20px;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    z-index: 1000;
    font-size: 1.2rem;
    line-height: 1;
}
.theme-toggle:hover { background: var(--day-label-bg); }

/* Dark mode adjustments for dynamic colors */
[data-theme="dark"] .class-block .subject {
    filter: brightness(1.3);
}
[data-theme="dark"] .class-block .time, 
[data-theme="dark"] .class-block .info-label {
    color: #ccc !important;
}
[data-theme="dark"] .info-label {
    background: rgba(0,0,0,0.3);
}

/* Help Button */
.btn-help {
    position: fixed;
    top: 20px;
    right: 70px; /* Left of theme toggle */
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: 20px;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    z-index: 1000;
    font-size: 1.2rem;
    line-height: 1;
}
.btn-help:hover { background: var(--day-label-bg); }

/* Help Modal */
.modal {
    display: none; 
    position: fixed; 
    z-index: 2000; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgba(0,0,0,0.5); 
    backdrop-filter: blur(2px);
}

.modal-content {
    background-color: var(--bg-surface);
    margin: 5% auto; 
    padding: 2rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    width: 80%; 
    max-width: 700px;
    box-shadow: var(--shadow-md);
    position: relative;
    color: var(--text-primary);
}

.close-modal {
    color: var(--text-secondary);
    float: right;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    line-height: 1;
}
.close-modal:hover { color: var(--text-primary); }

.help-section { margin-bottom: 1.5rem; }
.help-section h4 { margin-top: 0; color: var(--primary-color); margin-bottom: 0.5rem; }
.help-section ul { margin: 0; padding-left: 1.2rem; }
.help-section li { margin-bottom: 0.3rem; }

/* Break Settings Styling */
.break-toggle-wrapper {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
}

.toggle-label {
    margin-left: 10px;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* Switch Toggle */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
}

.switch input { 
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 22px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--primary-color);
}

input:focus + .slider {
    box-shadow: 0 0 1px var(--primary-color);
}

input:checked + .slider:before {
    transform: translateX(18px);
}

.break-row {
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 10px;
}

.break-row-field {
    margin-bottom: 8px;
}
.break-row-field:last-child {
    margin-bottom: 0;
}
.break-row-flex {
    display: flex;
    gap: 10px;
}
.break-row label {
    display: block;
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 2px;
    font-weight: 600;
}
.break-row input {
    width: 100%;
    padding: 6px;
    border: 1px solid var(--input-border);
    border-radius: 4px;
    background: var(--input-bg);
    color: var(--text-primary);
    font-size: 0.85rem;
    box-sizing: border-box;
}

.break-actions {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}