:root {
    --ink: #172026;
    --muted: #65737f;
    --line: #d9e1e7;
    --bg: #f5f7f8;
    --panel: #ffffff;
    --brand: #1b7f79;
    --brand-strong: #0f5f5a;
    --accent: #c25a32;
    --ok: #21865b;
    --warn: #bd7b16;
    --bad: #b83f45;
    --radius: 8px;
    font-family: "Tahoma", "Noto Sans Thai", system-ui, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--bg); }
a { color: inherit; text-decoration: none; }
button, .button {
    border: 0;
    border-radius: 6px;
    background: var(--brand);
    color: white;
    padding: 10px 14px;
    font: inherit;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
}
button:hover, .button:hover { background: var(--brand-strong); }
button.secondary { background: #eef3f4; color: var(--ink); border: 1px solid var(--line); }
button.danger { background: var(--bad); }
button:disabled { opacity: .45; cursor: not-allowed; }
input, select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 10px 11px;
    min-height: 40px;
    font: inherit;
    background: white;
}
label { display: grid; gap: 6px; color: var(--muted); font-size: 14px; }
label.is-disabled { opacity: .55; }
label.is-disabled select { cursor: not-allowed; background: #eef3f4; }

.login-body {
    min-height: 100vh;
    background: linear-gradient(135deg, #eef6f5, #f8f1ec);
    display: grid;
    place-items: center;
}
.login-shell { width: min(460px, calc(100vw - 28px)); position: relative; }
.login-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 30px;
    box-shadow: 0 18px 55px rgba(23, 32, 38, .12);
}
.login-card h1 { margin: 0 0 4px; font-size: 26px; }
.login-card h2 { margin: 0 0 8px; font-size: 20px; color: var(--brand-strong); }
.login-card p { color: var(--muted); margin: 0 0 24px; }
.login-card form { display: grid; gap: 16px; }
.login-logo { width: 72px; height: 72px; object-fit: contain; margin-bottom: 14px; }
.admin-gear {
    position: fixed;
    right: 22px;
    top: 22px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--ink);
    color: white;
    z-index: 10;
    box-shadow: 0 14px 30px rgba(23, 32, 38, .18);
}
.scan-entry { display: block; margin-top: 18px; color: var(--brand-strong); font-weight: 700; }

.app-shell {
    display: grid;
    grid-template-columns: 270px minmax(0, 1fr);
    min-height: 100vh;
    transition: grid-template-columns .22s ease;
}
.app-shell.sidebar-collapsed { grid-template-columns: 72px minmax(0, 1fr); }
.sidebar {
    background: #102528;
    color: white;
    padding: 18px;
    display: grid;
    grid-template-rows: auto 1fr auto;
    gap: 14px;
    position: sticky;
    top: 0;
    height: 100vh;
    width: 100%;
    transition: width .22s ease, padding .22s ease;
    z-index: 4;
    overflow: visible;
}
.sidebar.collapsed { padding: 18px 10px; }
.sidebar.collapsed .brand div,
.sidebar.collapsed .profile { font-size: 0; }
.sidebar.collapsed .brand { justify-content: center; }
.sidebar.collapsed nav a {
    width: 46px;
    height: 46px;
    padding: 0;
    margin: 0 auto;
    overflow: hidden;
    justify-content: center;
}
.sidebar.collapsed .nav-label { display: none; }
.sidebar.collapsed .nav-icon { margin: 0; }
.brand { display: flex; align-items: center; gap: 10px; min-height: 52px; }
.brand img { width: 44px; height: 44px; object-fit: contain; background: white; border-radius: 6px; }
.brand strong, .brand span { display: block; }
.brand span { color: #bdd0d3; font-size: 13px; margin-top: 3px; }
.sidebar-edge-toggle {
    position: absolute;
    top: 50%;
    right: -24px;
    width: 24px;
    height: 66px;
    transform: translateY(-50%);
    border-radius: 0 6px 6px 0;
    background: #e4e8eb;
    color: #26333b;
    border: 1px solid var(--line);
    box-shadow: 0 6px 16px rgba(23, 32, 38, .10);
    font-size: 24px;
    padding: 0;
    z-index: 8;
}
.sidebar-edge-toggle:hover { background: #d7dde1; color: #172026; }
.sidebar nav {
    display: grid;
    gap: 7px;
    align-content: start;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 2px;
}
.sidebar nav::-webkit-scrollbar { width: 6px; }
.sidebar nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,.22); border-radius: 999px; }
.sidebar nav a {
    color: #d6e3e4;
    padding: 11px 12px;
    border-radius: 6px;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 44px;
}
.sidebar nav a.active, .sidebar nav a:hover { background: rgba(255,255,255,.13); color: white; }
.nav-icon {
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
    color: #aebcc2;
}
.sidebar nav a.active .nav-icon,
.sidebar nav a:hover .nav-icon { color: white; }
.nav-label {
    overflow: hidden;
    text-overflow: ellipsis;
}
.profile {
    border-top: 1px solid rgba(255,255,255,.15);
    padding-top: 14px;
    display: grid;
    gap: 8px;
    color: #d6e3e4;
    background: #102528;
    position: relative;
    z-index: 2;
}
.profile a { color: white; font-weight: 700; }
.profile-logout {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.profile-logout .action-icon {
    width: 20px;
    height: 20px;
    flex-basis: 20px;
}
.content { padding: 24px; min-width: 0; }
.page-title { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 18px; }
.page-title h1 { margin: 0; font-size: 26px; }
.page-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.scan-link {
    color: var(--brand-strong);
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.logout-link {
    color: var(--bad);
    font-weight: 700;
    border: 1px solid #f0c4c8;
    background: #fff0f1;
    border-radius: 6px;
    padding: 9px 12px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.action-icon {
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
}

.alert { padding: 12px 14px; border-radius: 6px; margin-bottom: 16px; background: #eaf7f1; color: var(--ok); border: 1px solid #c7e8db; }
.alert.error { background: #fff0f1; color: var(--bad); border-color: #f0c4c8; }
.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
    margin-bottom: 18px;
}
.panel h2 { margin: 0 0 14px; font-size: 20px; }
.panel-head { display: flex; justify-content: space-between; gap: 14px; align-items: center; margin-bottom: 14px; }
.grid-form { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; align-items: end; }
.grid-form.compact { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.inline-form, .toolbar, .actions { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; }
.row-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row-actions form { margin: 0; }
.toolbar label { min-width: 170px; }
fieldset { border: 1px solid var(--line); border-radius: 6px; padding: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
legend { color: var(--muted); padding: 0 6px; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--ink); }
.check input { width: auto; min-height: auto; }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.stats-grid.compact-stats { grid-template-columns: repeat(6, minmax(120px, 1fr)); margin-top: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-left: 6px solid var(--brand); border-radius: var(--radius); padding: 18px; }
.stat span { color: var(--muted); }
.stat strong { display: block; font-size: 34px; margin-top: 8px; }
.stat.absent { border-left-color: var(--bad); }
.stat.late { border-left-color: var(--warn); }
.stat.leave { border-left-color: var(--accent); }
.muted-text { color: var(--muted); margin: 0; }
.report-shortcuts {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
}
.report-shortcuts a {
    border: 1px solid var(--line);
    border-left: 5px solid var(--brand);
    border-radius: 6px;
    padding: 14px;
    color: var(--ink);
    text-decoration: none;
    background: #fff;
}
.report-shortcuts a:hover {
    border-color: var(--brand);
    box-shadow: 0 10px 24px rgba(16, 24, 40, .08);
}
.report-shortcuts strong {
    display: block;
    margin-bottom: 6px;
}
.report-shortcuts span {
    display: block;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.4;
}
.status-badge {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    border-radius: 999px;
    padding: 3px 10px;
    font-weight: 700;
    font-size: 13px;
    background: #eef3f4;
    color: var(--muted);
}
.status-badge.present, .status-badge.face_scan { background: #e7f5ee; color: var(--ok); }
.status-badge.absent { background: #fff0f1; color: var(--bad); }
.status-badge.late { background: #fff6e8; color: var(--warn); }
.status-badge.leave { background: #fff1eb; color: var(--accent); }
.status-badge.manual { background: #edf4ff; color: #2f5e9e; }
.status-badge.unchecked { background: #eef3f4; color: var(--muted); }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 6px; }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
th, td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { background: #f0f4f5; color: var(--muted); font-weight: 700; }
.yearly-report-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}
.yearly-report-title h2 { margin: 0; font-size: 18px; }
.yearly-report-title span { color: var(--muted); font-weight: 700; }
.yearly-table-wrap { border-color: #293348; border-radius: 0; }
.yearly-report-table {
    min-width: 1360px;
    font-size: 14px;
}
.yearly-report-table th,
.yearly-report-table td {
    border: 1px solid #293348;
    padding: 7px 8px;
    text-align: center;
}
.yearly-report-table thead th {
    background: #2446b8;
    color: #fff;
}
.yearly-report-table thead tr:first-child th {
    background: #2140a8;
}
.yearly-report-table .student-name {
    text-align: left;
    white-space: nowrap;
}
.yearly-report-table tbody tr:nth-child(even) td { background: #eef6ff; }
.yearly-report-table tfoot th {
    background: #d8e8ff;
    color: #10243e;
}
.empty { text-align: center; color: var(--muted); }
.segmented { display: inline-grid; grid-template-columns: repeat(4, 58px); gap: 6px; }
.segmented input { display: none; }
.segmented span { display: block; text-align: center; border: 1px solid var(--line); border-radius: 6px; padding: 8px 6px; color: var(--muted); }
.segmented input:checked + span { background: var(--brand); border-color: var(--brand); color: white; }
.danger-zone { margin-top: 14px; }

.scan-body { background: #edf3f2; }
.scan-layout { width: min(1180px, calc(100vw - 28px)); margin: 0 auto; padding: 20px 0 36px; }
.scan-header { display: flex; justify-content: space-between; align-items: center; gap: 18px; margin-bottom: 18px; }
.scan-header h1 { margin: 0; }
.scan-header p { margin: 4px 0 0; color: var(--muted); }
.scan-header nav { display: flex; gap: 10px; font-weight: 700; color: var(--brand-strong); }
.scan-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) 360px; gap: 18px; }
.camera-panel, .scan-control {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
}
.camera-panel { display: grid; gap: 12px; }
.camera-frame {
    position: relative;
    width: 100%;
    aspect-ratio: 16/10;
    background: #172026;
    border-radius: 6px;
    overflow: hidden;
}
#camera, #enroll-camera {
    width: 100%;
    height: 100%;
    background: #172026;
    object-fit: cover;
}
#scan-canvas, #enroll-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}
.camera-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.scan-control form { display: grid; gap: 14px; }
.gps-status { border-radius: 6px; padding: 12px; font-weight: 700; }
.gps-status.pending { background: #fff6e8; color: var(--warn); }
.gps-status.ok { background: #e7f5ee; color: var(--ok); }
.gps-status.bad { background: #fff0f1; color: var(--bad); }
.confidence { display: flex; justify-content: space-between; align-items: center; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 12px; }
.confidence strong { color: var(--ink); font-size: 22px; }
.scan-message { margin-top: 12px; color: var(--brand-strong); font-weight: 700; }
.scan-note code { background: #eef3f4; padding: 2px 5px; border-radius: 4px; }
.face-enroll { display: grid; grid-template-columns: minmax(0, 1.4fr) 360px; gap: 18px; }
.face-progress { display: flex; justify-content: space-between; align-items: center; border: 1px solid var(--line); border-radius: 6px; padding: 12px; color: var(--muted); }
.face-progress strong { color: var(--ink); font-size: 22px; }

@media (max-width: 860px) {
    .app-shell { display: block; }
    .sidebar {
        position: sticky;
        height: auto;
        top: 0;
        z-index: 5;
        grid-template-columns: 1fr auto;
        grid-template-rows: auto auto;
    }
    .sidebar nav { grid-column: 1 / -1; display: flex; overflow-x: auto; }
    .sidebar nav a { flex: 0 0 auto; }
    .profile { display: none; }
    .sidebar-edge-toggle { display: none; }
    .content { padding: 16px; }
    .grid-form, .grid-form.compact, .stats-grid, .stats-grid.compact-stats, .report-shortcuts, .scan-grid, .face-enroll { grid-template-columns: 1fr; }
    .panel-head, .page-title, .scan-header { align-items: flex-start; flex-direction: column; }
    .page-actions { width: 100%; }
    .segmented { grid-template-columns: repeat(4, minmax(48px, 1fr)); width: 100%; }
}

@media print {
    .sidebar, .page-title, .toolbar, .no-print, .scan-link, .logout-link, .actions { display: none !important; }
    .app-shell { display: block; }
    .content { padding: 0; }
    .panel { border: 0; padding: 0; margin: 0; }
    .table-wrap { overflow: visible; }
    .yearly-report-table { min-width: 0; width: 100%; font-size: 11px; }
    .yearly-report-table th, .yearly-report-table td { padding: 4px 5px; }
}
