/* =====================================================================
   Classroom Activity Log — Stylesheet
   Mobile-first, responsive, vanilla CSS (no framework)
   ===================================================================== */

:root {
    --primary: #2563EB;
    --primary-dark: #1D4ED8;
    --success: #16A34A;
    --danger: #DC2626;
    --neutral: #6B7280;
    --bg: #F3F4F6;
    --card: #FFFFFF;
    --text: #111827;
    --text-muted: #6B7280;
    --border: #E5E7EB;
    --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg: 0 10px 25px rgba(0,0,0,.12);
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-w: 240px;
}

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

html, body {
    font-family: 'Sarabun', sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.6;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ---------------------------------------------------------------------
   Layout
   --------------------------------------------------------------------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    background: #fff;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 40;
    transition: transform .25s ease;
}

.sidebar-brand {
    display: flex; align-items: center; gap: 10px;
    padding: 20px 18px;
    font-weight: 700; font-size: 18px;
    color: var(--primary);
    border-bottom: 1px solid var(--border);
}
.brand-icon { font-size: 22px; }

.sidebar-nav { padding: 12px 10px; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.nav-link {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 14px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-weight: 500;
    transition: background .15s, color .15s;
}
.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-icon { font-size: 18px; }
.nav-section {
    margin: 12px 14px 4px;
    font-size: 11px; font-weight: 700;
    text-transform: uppercase; letter-spacing: .05em;
    color: var(--text-muted);
}
.role-badge {
    display: inline-block; padding: 2px 8px; border-radius: 999px;
    font-size: 12px; font-weight: 600;
}
.role-admin { background: #EDE9FE; color: #6D28D9; }
.role-teacher { background: #F3F4F6; color: var(--neutral); }
.checklist {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
    max-height: 240px; overflow-y: auto;
    border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px;
}
.checklist label {
    display: flex; align-items: center; gap: 8px; margin: 0; font-weight: 400; cursor: pointer;
}
.checklist input { width: auto; }
.tag {
    display: inline-block; background: var(--bg); color: var(--text-muted);
    padding: 2px 9px; border-radius: 999px; font-size: 12px; margin: 2px;
}

.sidebar-footer { padding: 14px; border-top: 1px solid var(--border); }
.teacher-badge { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--primary); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600;
}
.teacher-name { font-weight: 600; font-size: 14px; }
.logout-link {
    display: block; text-align: center;
    padding: 8px; border-radius: var(--radius-sm);
    background: var(--bg); color: var(--danger);
    font-weight: 500; font-size: 14px;
}
.logout-link:hover { background: #FEE2E2; }

.main { flex: 1; margin-left: var(--sidebar-w); display: flex; flex-direction: column; min-width: 0; }

.topbar {
    display: flex; align-items: center; gap: 14px;
    padding: 16px 24px;
    background: #fff;
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 30;
}
.topbar-title { font-size: 20px; font-weight: 700; }
.menu-toggle {
    display: none; background: none; border: none;
    font-size: 24px; color: var(--text);
}

.content { padding: 24px; max-width: 1200px; width: 100%; }

/* ---------------------------------------------------------------------
   Cards & Stats
   --------------------------------------------------------------------- */
.card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}
.card + .card { margin-top: 20px; }
.card-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 16px; flex-wrap: wrap; gap: 12px;
}
.card-title { font-size: 17px; font-weight: 700; }
.card-subtitle { font-size: 13px; font-weight: 400; color: var(--text-muted); margin-top: 2px; }

/* ---------------------------------------------------------------------
   Segmented control (สลับมุมมองรายงาน)
   --------------------------------------------------------------------- */
.segmented {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 20px 0;          /* ช่องบน/ล่างเท่ากับระยะระหว่าง card */
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    box-shadow: var(--shadow);
}
.segmented button {
    border: 1px solid transparent; background: transparent;
    color: var(--text-muted); font-size: 15px; font-weight: 700;
    padding: 14px 18px; border-radius: var(--radius-sm);
    display: inline-flex; align-items: center; justify-content: center; gap: 9px;
    transition: background .15s, color .15s, box-shadow .15s, transform .08s;
}
.segmented button:hover { background: var(--bg); color: var(--text); }
.segmented button:active { transform: scale(.98); }
.segmented button.active {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #fff;
    box-shadow: 0 4px 12px rgba(37, 99, 235, .35);
}
.segmented button.active:hover { color: #fff; }
/* ---------------------------------------------------------------------
   Export dropdown menu
   --------------------------------------------------------------------- */
.export-menu { position: relative; }
.export-dropdown {
    position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
    background: var(--card); border: 1px solid var(--border);
    border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
    padding: 6px; min-width: 170px;
    display: flex; flex-direction: column; gap: 2px;
}
.export-dropdown[hidden] { display: none; }
.export-dropdown button {
    display: flex; align-items: center; gap: 10px;
    width: 100%; text-align: left; border: none; background: transparent;
    color: var(--text); font-size: 14px; font-weight: 500;
    padding: 9px 12px; border-radius: 6px;
    transition: background .12s;
}
.export-dropdown button:hover { background: var(--bg); }
.export-dropdown button span { font-size: 16px; width: 20px; text-align: center; }

.segmented .seg-icon {
    font-size: 18px;
    width: 32px; height: 32px; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--bg); border-radius: 50%;
    transition: background .15s;
}
.segmented button.active .seg-icon { background: rgba(255, 255, 255, .22); }

/* มุมมองรายงาน — fade-in ตอนสลับ */
.report-view[hidden] { display: none; }
.report-view { animation: viewFade .18s ease; }
@keyframes viewFade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

@media (max-width: 640px) {
    .segmented button { padding: 12px 8px; font-size: 14px; gap: 7px; }
    .segmented .seg-icon { width: 28px; height: 28px; font-size: 16px; }
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    display: flex; align-items: center; gap: 16px;
}
.stat-icon {
    width: 52px; height: 52px; border-radius: var(--radius-sm);
    display: flex; align-items: center; justify-content: center;
    font-size: 24px; flex-shrink: 0;
}
.stat-icon.blue { background: #DBEAFE; }
.stat-icon.green { background: #DCFCE7; }
.stat-icon.purple { background: #EDE9FE; }
.stat-icon.amber { background: #FEF3C7; }
.stat-value { font-size: 26px; font-weight: 700; line-height: 1.1; }
.stat-label { color: var(--text-muted); font-size: 14px; }

/* ---------------------------------------------------------------------
   Buttons
   --------------------------------------------------------------------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 10px 18px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-weight: 600; font-size: 14px;
    transition: background .15s, box-shadow .15s, opacity .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-secondary { background: var(--bg); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #E5E7EB; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-block { width: 100%; }

/* ---------------------------------------------------------------------
   Forms
   --------------------------------------------------------------------- */
.form-group { margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
label { display: block; font-weight: 500; margin-bottom: 6px; font-size: 14px; }
.form-control {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: inherit; font-size: 15px;
    background: #fff;
    transition: border-color .15s, box-shadow .15s;
}
.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
textarea.form-control { resize: vertical; min-height: 80px; }
.field-error { color: var(--danger); font-size: 13px; margin-top: 4px; min-height: 18px; }
.form-control.invalid { border-color: var(--danger); }

/* ---------------------------------------------------------------------
   Tables
   --------------------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th, .data-table td { padding: 12px 14px; text-align: left; }
.data-table thead th {
    background: var(--bg); color: var(--text-muted);
    font-weight: 600; font-size: 13px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.data-table tbody tr { border-bottom: 1px solid var(--border); transition: background .12s; }
.data-table tbody tr:hover { background: #F9FAFB; }
.data-table .actions { display: flex; gap: 6px; white-space: nowrap; }

/* Badges */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 12px; font-weight: 600;
}
.badge-positive { background: #DCFCE7; color: var(--success); }
.badge-negative { background: #FEE2E2; color: var(--danger); }
.badge-neutral { background: #F3F4F6; color: var(--neutral); }
.score-pos { color: var(--success); font-weight: 700; }
.score-neg { color: var(--danger); font-weight: 700; }
.score-zero { color: var(--neutral); }

/* ---------------------------------------------------------------------
   Filters bar
   --------------------------------------------------------------------- */
.filters {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    align-items: end;
    margin-bottom: 18px;
}
.filters .form-group { margin-bottom: 0; }

/* ---------------------------------------------------------------------
   Activity feed
   --------------------------------------------------------------------- */
.feed-item {
    display: flex; gap: 14px;
    padding: 14px 0;
    border-bottom: 1px solid var(--border);
}
.feed-item:last-child { border-bottom: none; }
.feed-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; flex-shrink: 0; }
.feed-dot.positive { background: var(--success); }
.feed-dot.negative { background: var(--danger); }
.feed-dot.neutral { background: var(--neutral); }
.feed-body { flex: 1; min-width: 0; }
.feed-title { font-weight: 600; }
.feed-meta { color: var(--text-muted); font-size: 13px; }

/* ---------------------------------------------------------------------
   Modal
   --------------------------------------------------------------------- */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(17,24,39,.5);
    display: none;
    align-items: flex-start; justify-content: center;
    padding: 40px 16px;
    z-index: 60;
    overflow-y: auto;
}
.modal-overlay.open { display: flex; }
.modal {
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%; max-width: 520px;
    animation: modalIn .2s ease;
}
@keyframes modalIn { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.modal-title { font-size: 18px; font-weight: 700; }
.modal-close { background: none; border: none; font-size: 24px; color: var(--text-muted); line-height: 1; }
.modal-body { padding: 20px; }
.modal-footer {
    display: flex; justify-content: flex-end; gap: 10px;
    padding: 16px 20px; border-top: 1px solid var(--border);
}

/* ---------------------------------------------------------------------
   Toast
   --------------------------------------------------------------------- */
.toast-container {
    position: fixed; bottom: 24px; right: 24px;
    display: flex; flex-direction: column; gap: 10px;
    z-index: 80;
}
.toast {
    background: #fff;
    border-left: 4px solid var(--primary);
    box-shadow: var(--shadow-lg);
    border-radius: var(--radius-sm);
    padding: 14px 18px;
    min-width: 260px; max-width: 360px;
    animation: toastIn .25s ease;
    font-weight: 500;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.fade-out { animation: toastOut .25s ease forwards; }
@keyframes toastIn { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes toastOut { to { transform: translateX(40px); opacity: 0; } }

/* ---------------------------------------------------------------------
   Pagination
   --------------------------------------------------------------------- */
.pagination {
    display: flex; gap: 6px; justify-content: center;
    margin-top: 18px; flex-wrap: wrap;
}
.pagination button {
    min-width: 38px; padding: 8px 12px;
    border: 1px solid var(--border); background: #fff;
    border-radius: var(--radius-sm); font-weight: 500;
}
.pagination button:hover:not(:disabled) { background: var(--bg); }
.pagination button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pagination button:disabled { opacity: .5; cursor: not-allowed; }

/* ---------------------------------------------------------------------
   Misc
   --------------------------------------------------------------------- */
/* ---------------------------------------------------------------------
   Attendance / roster table (เช็คชื่อ)
   --------------------------------------------------------------------- */
.req { color: var(--danger); }
.btn-mark-present { background: #DCFCE7; color: var(--success); border-color: #BBF7D0; }
.btn-mark-present:hover { background: #BBF7D0; }
.btn-mark-absent { background: #FEE2E2; color: var(--danger); border-color: #FECACA; }
.btn-mark-absent:hover { background: #FECACA; }

.roster-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.roster-table th, .roster-table td { padding: 11px 14px; border-bottom: 1px solid var(--border); }
.roster-table thead th {
    background: var(--bg); color: var(--text-muted);
    font-weight: 600; font-size: 13px; text-align: left;
}
.roster-table .col-no { width: 70px; text-align: center; }
.roster-table .col-status { width: 110px; text-align: center; }
.roster-table th.col-present { background: #DCFCE7; color: var(--success); }
.roster-table th.col-absent { background: #FEE2E2; color: var(--danger); }
.roster-table td.col-present { background: #F0FDF4; text-align: center; }
.roster-table td.col-absent { background: #FEF2F2; text-align: center; }
.roster-table tbody tr:hover td:first-child,
.roster-table tbody tr:hover td:nth-child(2) { background: #F9FAFB; }
.roster-table input[type="radio"] { width: 18px; height: 18px; cursor: pointer; accent-color: var(--primary); }
.roster-table td.col-absent input[type="radio"] { accent-color: var(--danger); }
.attend-summary { font-weight: 600; color: var(--text-muted); }
.attend-summary .pos { color: var(--success); }
.attend-summary .neg { color: var(--danger); }

.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.loading { text-align: center; padding: 40px; color: var(--text-muted); }
.spinner {
    width: 36px; height: 36px;
    border: 4px solid var(--border); border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .8s linear infinite;
    margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.text-muted { color: var(--text-muted); }
.mt-0 { margin-top: 0; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.chart-wrap { position: relative; height: 340px; }
.backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 35; }
.backdrop.show { display: block; }

/* ---------------------------------------------------------------------
   Login page
   --------------------------------------------------------------------- */
.login-page {
    min-height: 100vh;
    display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--primary), #1E3A8A);
    padding: 20px;
}
.login-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    width: 100%; max-width: 400px;
    padding: 36px 32px;
}
.login-logo { text-align: center; margin-bottom: 8px; font-size: 40px; }
.login-title { text-align: center; font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.login-sub { text-align: center; color: var(--text-muted); margin-bottom: 24px; font-size: 14px; }
.login-alert {
    background: #FEE2E2; color: var(--danger);
    padding: 10px 14px; border-radius: var(--radius-sm);
    font-size: 14px; margin-bottom: 16px; text-align: center;
}
.login-hint {
    margin-top: 18px; padding-top: 16px;
    border-top: 1px solid var(--border);
    font-size: 13px; color: var(--text-muted); text-align: center;
}

/* ---------------------------------------------------------------------
   Responsive
   --------------------------------------------------------------------- */
@media (max-width: 860px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .main { margin-left: 0; }
    .menu-toggle { display: block; }
    .form-row { grid-template-columns: 1fr; }
}
