/* ============================================================
   ระบบพฤติกรรมนักเรียน โรงเรียนนาบอน
   ธีม: ทันสมัย สะอาด อ่านง่าย รองรับทุกอุปกรณ์
   ============================================================ */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    /* สีหลัก */
    --primary: #1d5fa9;
    --primary-dark: #14477f;
    --primary-light: #4a8ad4;
    --primary-soft: #eaf2fb;
    --accent: #0ea5a5;

    /* พื้นหลัง/ตัวอักษร */
    --bg: #f1f5f9;
    --bg-deep: #e6ecf4;
    --card: #ffffff;
    --text: #172033;
    --text-2: #46546b;
    --muted: #6b7a90;
    --border: #e3e9f0;
    --border-strong: #cdd7e3;

    /* สีกลุ่มพฤติกรรม */
    --normal: #0f7a3d;
    --normal-bg: #e6f7ec;
    --normal-line: #17a34a;
    --watch: #96650a;
    --watch-bg: #fdf4d5;
    --watch-line: #e0a821;
    --risk: #bd410d;
    --risk-bg: #ffeade;
    --risk-line: #f06d29;
    --critical: #b01818;
    --critical-bg: #fde5e5;
    --critical-line: #dc2b2b;

    --radius: 16px;
    --radius-sm: 12px;
    --shadow-xs: 0 1px 2px rgba(16, 26, 45, .06);
    --shadow-sm: 0 2px 8px rgba(16, 26, 45, .07);
    --shadow-md: 0 8px 24px rgba(16, 26, 45, .10);
    --shadow-lg: 0 24px 64px rgba(16, 26, 45, .26);

    --font: 'Sarabun', 'Noto Sans Thai', -apple-system, BlinkMacSystemFont, 'Segoe UI', Tahoma, sans-serif;
}

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    line-height: 1.6;
    letter-spacing: .1px;
    min-height: 100vh;
    overscroll-behavior-y: none;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
a { color: var(--primary); text-underline-offset: 3px; }
h1, h2, h3 { line-height: 1.35; font-weight: 700; }
:focus-visible { outline: 3px solid rgba(29, 95, 169, .45); outline-offset: 2px; border-radius: 8px; }
::selection { background: rgba(29, 95, 169, .18); }

/* ---------- ปุ่ม ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    padding: 10px 18px; border: none; border-radius: var(--radius-sm);
    font-size: 15px; font-family: inherit; font-weight: 600; cursor: pointer;
    min-height: 46px; text-decoration: none; white-space: nowrap; color: var(--text);
    transition: transform .1s ease, box-shadow .15s ease, background .15s ease, border-color .15s ease;
    -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px) scale(.99); }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.btn-primary {
    background: linear-gradient(165deg, #2a72bd, var(--primary-dark));
    color: #fff; box-shadow: 0 4px 14px rgba(29, 95, 169, .32);
}
.btn-primary:hover { box-shadow: 0 6px 20px rgba(29, 95, 169, .42); }
.btn-success { background: linear-gradient(165deg, #1ba552, #0f7a3d); color: #fff; box-shadow: 0 3px 12px rgba(15, 122, 61, .3); }
.btn-danger { background: linear-gradient(165deg, #dc3a3a, #b01818); color: #fff; box-shadow: 0 3px 12px rgba(176, 24, 24, .28); }
.btn-outline { background: #fff; border: 1.5px solid var(--border-strong); }
.btn-outline:hover { background: var(--primary-soft); border-color: var(--primary-light); color: var(--primary-dark); }
.btn-sm { padding: 7px 13px; font-size: 13.5px; min-height: 38px; border-radius: 10px; gap: 5px; }
.btn-block { width: 100%; margin-top: 20px; }
.btn-ghost-light { background: rgba(255, 255, 255, .14); color: #fff; border: 1px solid rgba(255, 255, 255, .3); backdrop-filter: saturate(1.4); }
.btn-ghost-light:hover { background: rgba(255, 255, 255, .26); }

/* ---------- หน้าเข้าสู่ระบบ ---------- */
.login-body {
    display: flex; align-items: center; justify-content: center; padding: 20px;
    background:
        radial-gradient(1100px 520px at 15% -10%, rgba(255, 255, 255, .22), transparent 60%),
        linear-gradient(155deg, #2a72bd 0%, #14477f 55%, #0d3260 100%);
}
.login-box {
    background: var(--card); border-radius: 24px; padding: 42px 36px;
    width: 100%; max-width: 420px; box-shadow: var(--shadow-lg); text-align: center;
}
.login-logo { font-size: 54px; margin-bottom: 8px; line-height: 1; }
.login-box h1 { font-size: 24px; color: var(--primary-dark); font-weight: 800; }
.login-sub { color: var(--muted); margin: 6px 0 24px; font-size: 14.5px; }
.login-box form { text-align: left; }
.login-box label { display: block; font-weight: 600; margin: 15px 0 6px; font-size: 14.5px; color: var(--text-2); }
.login-box input {
    width: 100%; padding: 14px 16px; border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm); font-size: 16px; font-family: inherit; background: #fafcfe;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.login-box input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .13); background: #fff; }
.login-footer { margin-top: 20px; font-size: 12.5px; color: var(--muted); }

/* ช่องค้นหาสำหรับนักเรียน บนหน้าเข้าสู่ระบบ */
.stu-search-block {
    margin-top: 26px; padding-top: 22px; border-top: 1px dashed var(--border);
    text-align: left;
}
.stu-search-title { font-weight: 700; font-size: 15px; color: var(--text); margin-bottom: 10px; }
.stu-search-form { display: flex; gap: 8px; }
.stu-search-form input {
    flex: 1; min-width: 0; padding: 13px 15px; border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm); font-size: 15.5px; font-family: inherit; background: #fafcfe;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.stu-search-form input:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .13); background: #fff;
}
.stu-search-form .btn { flex-shrink: 0; padding: 10px 16px; }
.stu-search-note { font-size: 12.5px; color: var(--muted); margin-top: 8px; }

@media (max-width: 400px) {
    .stu-search-form { flex-wrap: wrap; }
    .stu-search-form input { flex-basis: 100%; }
    .stu-search-form .btn { width: 100%; }
}

/* ---------- แถบหัว ---------- */
.topbar {
    background: linear-gradient(135deg, #2a72bd 0%, var(--primary-dark) 70%, #0f3a6b 100%);
    color: #fff; box-shadow: 0 4px 20px rgba(20, 71, 127, .28);
    position: sticky; top: 0; z-index: 50;
    padding-top: env(safe-area-inset-top);
}
.topbar-inner {
    max-width: 1280px; margin: 0 auto;
    padding: 13px clamp(14px, 3vw, 26px);
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
    font-size: 26px; width: 46px; height: 46px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    background: rgba(255, 255, 255, .16); border-radius: 14px;
    border: 1px solid rgba(255, 255, 255, .22);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.brand-title { font-size: clamp(16px, 2.2vw, 19px); font-weight: 800; letter-spacing: .2px; }
.brand-sub { font-size: 13px; color: rgba(255, 255, 255, .82); font-weight: 500; }

.user-area { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.user-chip {
    display: flex; align-items: center; gap: 9px;
    background: rgba(255, 255, 255, .13); border: 1px solid rgba(255, 255, 255, .2);
    padding: 5px 14px 5px 5px; border-radius: 999px;
}
.user-avatar {
    width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
    background: rgba(255, 255, 255, .92); color: var(--primary-dark);
    display: flex; align-items: center; justify-content: center;
    font-weight: 800; font-size: 16px;
}
.user-text { display: flex; flex-direction: column; line-height: 1.25; }
.user-name { font-size: 14px; font-weight: 600; }
.user-role { font-size: 11.5px; color: rgba(255, 255, 255, .8); }

.container { max-width: 1280px; margin: 0 auto; padding: 24px clamp(12px, 3vw, 26px) 72px; }

/* ---------- การ์ดสรุปกลุ่ม ---------- */
.summary-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(158px, 1fr));
    gap: 13px; margin-bottom: 20px;
}
.sum-card {
    position: relative; overflow: hidden;
    display: flex; flex-direction: column; align-items: flex-start; text-align: left;
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 15px 16px 14px; cursor: pointer; font-family: inherit; color: inherit;
    box-shadow: var(--shadow-xs);
    transition: transform .14s ease, box-shadow .14s ease, border-color .14s ease;
    -webkit-tap-highlight-color: transparent;
}
.sum-card::before {
    content: ''; position: absolute; inset: 0 auto 0 0; width: 5px; background: var(--border-strong);
}
.sum-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.sum-card.active { border-color: var(--primary-light); box-shadow: 0 0 0 3px rgba(29, 95, 169, .17), var(--shadow-sm); }
.sum-icon { font-size: 15px; line-height: 1; margin-bottom: 4px; opacity: .9; }
.sum-card .num {
    font-size: clamp(28px, 3.6vw, 36px); font-weight: 800; line-height: 1.1;
    font-variant-numeric: tabular-nums; letter-spacing: -.5px;
}
.sum-card .lbl { font-size: 15px; font-weight: 700; color: var(--text); margin-top: 1px; }
.sum-card .sub { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.sum-card.c-total::before { background: var(--primary); }        .sum-card.c-total .num { color: var(--primary); }
.sum-card.c-normal::before { background: var(--normal-line); }   .sum-card.c-normal .num { color: var(--normal); }
.sum-card.c-watch::before { background: var(--watch-line); }     .sum-card.c-watch .num { color: var(--watch); }
.sum-card.c-risk::before { background: var(--risk-line); }       .sum-card.c-risk .num { color: var(--risk); }
.sum-card.c-critical::before { background: var(--critical-line); } .sum-card.c-critical .num { color: var(--critical); }

/* ---------- แผงเครื่องมือ ---------- */
.panel {
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 14px; margin-bottom: 16px; box-shadow: var(--shadow-xs);
}
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.search-field { position: relative; flex: 1 1 260px; min-width: 190px; }
.search-field .search-icon {
    position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
    font-size: 15px; pointer-events: none; opacity: .65;
}
.search-field input { width: 100%; padding-left: 40px !important; }
.toolbar input[type=text], .toolbar select {
    padding: 11px 14px; border: 1.5px solid var(--border-strong); border-radius: var(--radius-sm);
    font-size: 15.5px; font-family: inherit; background: #fff; color: var(--text);
    min-height: 46px; transition: border-color .15s ease, box-shadow .15s ease;
}
.toolbar select { flex: 0 1 auto; cursor: pointer; }
.toolbar input[type=text]:focus, .toolbar select:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .12);
}
.action-bar {
    display: flex; flex-wrap: wrap; gap: 9px; align-items: center;
    margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--border);
}

/* ---------- ตาราง (จอใหญ่) ---------- */
.table-wrap {
    background: var(--card); border-radius: var(--radius); border: 1px solid var(--border);
    overflow: hidden; box-shadow: var(--shadow-xs);
}
.table-wrap > table { display: table; }
table { width: 100%; border-collapse: collapse; }
th {
    background: #f7f9fc; text-align: left; font-size: 12.5px; font-weight: 700;
    letter-spacing: .3px; color: var(--muted); padding: 13px 16px;
    border-bottom: 1.5px solid var(--border); white-space: nowrap; text-transform: none;
}
td { padding: 12px 16px; border-bottom: 1px solid #f2f5f9; font-size: 15.5px; vertical-align: middle; }
tbody tr { transition: background .12s ease; }
tbody tr:hover td { background: #f8fafd; }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: center; }
th.col-action { width: 1%; }
.score-val { font-weight: 800; font-size: 17px; font-variant-numeric: tabular-nums; }
.score-neg { color: var(--critical); }
.score-pos { color: var(--normal); }

.badge {
    display: inline-block; padding: 4px 13px; border-radius: 999px;
    font-size: 13px; font-weight: 700; white-space: nowrap; line-height: 1.55;
    border: 1px solid transparent;
}
.badge-normal { background: var(--normal-bg); color: var(--normal); border-color: #bfe6cd; }
.badge-watch { background: var(--watch-bg); color: var(--watch); border-color: #f0dda0; }
.badge-risk { background: var(--risk-bg); color: var(--risk); border-color: #fbcdb2; }
.badge-critical { background: var(--critical-bg); color: var(--critical); border-color: #f6c4c4; }

.row-actions { display: flex; gap: 6px; flex-wrap: nowrap; justify-content: flex-end; }
.count-info { margin-top: 10px; text-align: right; }

/* ---------- มุมมองการ์ด (จอเล็ก) ---------- */
.stu-list { display: none; }
.m-card {
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 14px; margin-bottom: 11px; box-shadow: var(--shadow-xs);
    border-left: 5px solid var(--border-strong);
}
.m-card.g-normal { border-left-color: var(--normal-line); }
.m-card.g-watch { border-left-color: var(--watch-line); }
.m-card.g-risk { border-left-color: var(--risk-line); }
.m-card.g-critical { border-left-color: var(--critical-line); }
.m-top { display: flex; align-items: center; gap: 12px; }
.m-avatar { width: 54px; height: 54px; border-radius: 15px; object-fit: cover; background: #eef2f7; flex-shrink: 0; }
.m-avatar-ph { display: flex; align-items: center; justify-content: center; font-size: 25px; color: #9aa8bb; }
.m-info { flex: 1; min-width: 0; }
.m-name { font-weight: 700; font-size: 16.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-meta { font-size: 13.5px; color: var(--muted); }
.m-score { text-align: right; flex-shrink: 0; display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.m-score .score-val { font-size: 23px; line-height: 1.1; }
.m-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); gap: 8px; margin-top: 12px; }
.m-actions .btn { min-height: 44px; padding: 8px 6px; font-size: 14px; }

/* ---------- โมดัล ---------- */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(12, 22, 42, .55);
    backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
    display: none; align-items: flex-start; justify-content: center;
    padding: 30px 14px; z-index: 100; overflow-y: auto; overscroll-behavior: contain;
}
.modal-overlay.show { display: flex; animation: fade .15s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
    background: #fff; border-radius: 20px; width: 100%; max-width: 580px;
    box-shadow: var(--shadow-lg); animation: pop .18s cubic-bezier(.2, .9, .3, 1);
    margin-bottom: 30px; overflow: hidden;
}
.modal.modal-lg { max-width: 900px; }
@keyframes pop { from { transform: translateY(14px) scale(.97); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head {
    padding: 18px 22px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    background: #fbfdff;
}
.modal-head h2 { font-size: 18.5px; font-weight: 700; }
.modal-close {
    background: #eef2f7; border: none; font-size: 19px; cursor: pointer; color: var(--muted);
    line-height: 1; width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; transition: background .12s ease;
}
.modal-close:hover { background: #dfe6ef; color: var(--text); }
.modal-body { padding: 22px; }
.modal-foot {
    padding: 15px 22px; border-top: 1px solid var(--border);
    display: flex; justify-content: flex-end; gap: 10px; background: #fbfdff;
}

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.form-grid .full { grid-column: 1 / -1; }
.fld label { display: block; font-weight: 600; font-size: 14.5px; margin-bottom: 6px; color: var(--text-2); }
.fld input, .fld select, .fld textarea {
    width: 100%; padding: 12px 14px; border: 1.5px solid var(--border-strong); border-radius: var(--radius-sm);
    font-size: 16px; font-family: inherit; background: #fafcfe; color: var(--text);
    transition: border-color .15s ease, box-shadow .15s ease;
}
.fld input:focus, .fld select:focus, .fld textarea:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .12); background: #fff;
}
.fld textarea { resize: vertical; min-height: 78px; }

.type-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.type-toggle button {
    padding: 13px; border-radius: var(--radius-sm); border: 2px solid var(--border); background: #fff;
    font-family: inherit; font-size: 16px; font-weight: 700; cursor: pointer; min-height: 52px;
    color: var(--muted); transition: all .14s ease;
}
.type-toggle button:hover { border-color: var(--border-strong); }
.type-toggle button.sel-deduct { border-color: var(--critical-line); background: var(--critical-bg); color: var(--critical); }
.type-toggle button.sel-add { border-color: var(--normal-line); background: var(--normal-bg); color: var(--normal); }

/* ---------- เกณฑ์กลุ่ม ---------- */
.thr-row {
    display: flex; align-items: center; gap: 13px;
    padding: 13px 0; border-bottom: 1px solid var(--border);
}
.thr-row:last-of-type { border-bottom: none; }
.thr-dot { width: 13px; height: 13px; border-radius: 50%; flex-shrink: 0; }
.dot-normal { background: var(--normal-line); }
.dot-watch { background: var(--watch-line); }
.dot-risk { background: var(--risk-line); }
.dot-critical { background: var(--critical-line); }
.thr-info { flex: 1; display: flex; flex-direction: column; line-height: 1.4; min-width: 0; }
.thr-info b { font-size: 15.5px; }
.thr-fixed { color: var(--muted); font-size: 13.5px; font-style: italic; }
.thr-input { display: flex; align-items: center; gap: 8px; }
.thr-input input {
    width: 92px; padding: 10px 12px; border: 1.5px solid var(--border-strong);
    border-radius: 10px; font-size: 17px; font-family: inherit; font-weight: 700;
    text-align: center; background: #fafcfe;
}
.thr-input input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .12); background: #fff; }
.thr-preview { margin-top: 16px; padding: 14px; background: var(--primary-soft); border-radius: var(--radius-sm); }
.thr-preview-title { font-size: 13px; font-weight: 700; color: var(--primary-dark); margin-bottom: 9px; }
.thr-scale { display: flex; flex-wrap: wrap; gap: 8px; }
.thr-warn { color: var(--risk); font-weight: 600; font-size: 14px; }

/* ---------- เพิ่มผู้ใช้งานหลายคน ---------- */
.role-pick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.role-opt {
    display: flex; flex-direction: column; align-items: center; text-align: center; gap: 2px;
    padding: 14px 10px; border: 2px solid var(--border); border-radius: var(--radius-sm);
    background: #fff; font-family: inherit; cursor: pointer; color: var(--muted);
    transition: all .14s ease;
}
.role-opt:hover { border-color: var(--border-strong); background: #fafcfe; }
.role-opt.sel { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-dark); box-shadow: 0 0 0 3px rgba(29, 95, 169, .12); }
.role-emoji { font-size: 22px; line-height: 1.2; }
.role-name { font-weight: 700; font-size: 15px; color: var(--text); }
.role-opt.sel .role-name { color: var(--primary-dark); }
.role-desc { font-size: 12px; line-height: 1.35; }
.or-line { display: flex; align-items: center; gap: 12px; margin: 16px 0 12px; color: var(--muted); font-size: 13.5px; }
.or-line::before, .or-line::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.bulk-area {
    width: 100%; min-height: 150px; padding: 13px 15px; border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm); font-size: 15px; font-family: ui-monospace, 'Sarabun', monospace;
    background: #fafcfe; resize: vertical; line-height: 1.75;
}
.bulk-area:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .12); background: #fff; }
.bulk-done { margin-top: 16px; padding: 13px 16px; background: var(--normal-bg); border: 1px solid #bfe6cd; border-radius: var(--radius-sm); color: var(--normal); font-size: 15px; }
.bulk-done .muted { color: #3d7a56; display: block; margin-top: 3px; }
.bulk-skip { margin-top: 14px; padding: 13px 16px; background: var(--watch-bg); border: 1px solid #f0dda0; border-radius: var(--radius-sm); color: var(--watch); font-size: 14.5px; }
.bulk-skip ul { margin: 7px 0 0 20px; }
.bulk-skip li { margin-bottom: 3px; }
#bulkResult code { background: #eef2f7; padding: 2px 8px; border-radius: 6px; font-size: 14px; font-weight: 600; color: var(--text); }

/* ---------- รูปหลักฐาน ---------- */
.evi-add { width: 100%; border-style: dashed !important; }
.evi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 9px; margin-top: 11px; }
.evi-grid:empty { display: none; }
.evi-item { position: relative; aspect-ratio: 1; }
.evi-item img {
    width: 100%; height: 100%; object-fit: cover; border-radius: 11px;
    border: 1px solid var(--border); cursor: zoom-in; background: #eef2f7;
}
.evi-del {
    position: absolute; top: -7px; right: -7px; width: 26px; height: 26px;
    border-radius: 50%; border: 2px solid #fff; background: var(--critical); color: #fff;
    font-size: 12px; line-height: 1; cursor: pointer; display: flex;
    align-items: center; justify-content: center; padding: 0; font-family: inherit;
    box-shadow: var(--shadow-sm);
}
.evi-del:hover { filter: brightness(1.12); }

/* รูปหลักฐานในประวัติ */
.hist-main { min-width: 0; flex: 1; }
.hist-photos { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 9px; align-items: center; }
.hist-photo { position: relative; display: inline-block; }
.hist-photo img {
    width: 72px; height: 72px; object-fit: cover; border-radius: 10px;
    border: 1px solid var(--border); cursor: zoom-in; background: #eef2f7;
    transition: transform .12s ease, box-shadow .12s ease;
}
.hist-photo img:hover { transform: scale(1.04); box-shadow: var(--shadow-sm); }
.hist-photo .evi-del { width: 22px; height: 22px; top: -6px; right: -6px; font-size: 11px; }
.hist-photo-count { font-size: 12.5px; color: var(--muted); align-self: flex-end; padding-bottom: 3px; }

/* รูปที่กดขยายได้ */
.zoomable { cursor: zoom-in; transition: transform .12s ease, box-shadow .12s ease; }
.zoomable:hover { transform: scale(1.08); box-shadow: 0 0 0 3px rgba(29, 95, 169, .25); }

/* ---------- Lightbox ---------- */
.lightbox {
    position: fixed; inset: 0; background: rgba(8, 14, 26, .93);
    display: none; flex-direction: column; align-items: center; justify-content: center;
    z-index: 300; padding: 68px 16px calc(28px + env(safe-area-inset-bottom));
    animation: fade .15s ease-out;
}
.lightbox.show { display: flex; }
.lb-stage { display: flex; align-items: center; justify-content: center; gap: 10px; max-width: 100%; max-height: 100%; }
.lightbox img {
    max-width: min(96vw, 1400px); max-height: calc(100vh - 160px);
    border-radius: 12px; box-shadow: 0 24px 70px rgba(0, 0, 0, .6);
    object-fit: contain; background: #10161f;
}
.lb-close {
    position: absolute; top: max(16px, env(safe-area-inset-top)); right: 16px;
    background: rgba(255, 255, 255, .16); color: #fff; border: 1px solid rgba(255, 255, 255, .3);
    padding: 10px 18px; border-radius: 999px; font-size: 15px; font-weight: 600;
    font-family: inherit; cursor: pointer; min-height: 44px; backdrop-filter: blur(6px);
    transition: background .12s ease;
}
.lb-close:hover { background: rgba(255, 255, 255, .3); }
.lb-nav {
    background: rgba(255, 255, 255, .14); color: #fff; border: 1px solid rgba(255, 255, 255, .26);
    width: 48px; height: 48px; border-radius: 50%; font-size: 30px; line-height: 1;
    cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
    padding: 0 0 4px; font-family: inherit; transition: background .12s ease;
}
.lb-nav:hover { background: rgba(255, 255, 255, .3); }
.lb-caption {
    color: #fff; margin-top: 16px; font-size: 15px; text-align: center;
    background: rgba(0, 0, 0, .4); padding: 8px 18px; border-radius: 999px; max-width: 92vw;
}
.lb-caption:empty { display: none; }

/* ---------- แจ้งเตือน / เบ็ดเตล็ด ---------- */
.alert { padding: 13px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 14.5px; line-height: 1.65; }
.alert-error { background: var(--critical-bg); color: var(--critical); border: 1px solid #f4c2c2; }
.alert-info { background: var(--primary-soft); color: #14477f; border: 1px solid #c9dcf3; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.footer { text-align: center; color: var(--muted); font-size: 13px; padding: 20px 16px calc(26px + env(safe-area-inset-bottom)); }
.muted { color: var(--muted); font-size: 13.5px; }

/* ประวัติ */
.hist-item { display: flex; justify-content: space-between; gap: 14px; padding: 13px 0; border-bottom: 1px solid #f2f5f9; align-items: flex-start; }
.hist-item:last-child { border-bottom: none; }
.hist-pts { font-weight: 800; white-space: nowrap; font-size: 17.5px; font-variant-numeric: tabular-nums; display: flex; align-items: center; gap: 6px; }
.hist-pts.add { color: var(--normal); }
.hist-pts.deduct { color: var(--critical); }

/* นำเข้า */
.drop-zone {
    border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 32px 20px;
    text-align: center; color: var(--muted); cursor: pointer; background: #fafcfe;
    transition: all .15s ease; font-size: 15px;
}
.drop-zone:hover { border-color: var(--primary); color: var(--primary-dark); background: var(--primary-soft); }
.import-preview { max-height: 330px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-top: 14px; -webkit-overflow-scrolling: touch; }
.import-preview table { min-width: 640px; }

/* toast */
#toast {
    position: fixed; bottom: calc(28px + env(safe-area-inset-bottom)); left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: #111d33; color: #fff; padding: 13px 24px; border-radius: 999px;
    font-size: 15px; opacity: 0; pointer-events: none;
    transition: opacity .22s ease, transform .22s ease; z-index: 200;
    max-width: 92vw; text-align: center; box-shadow: var(--shadow-md); font-weight: 500;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* รูปในตาราง */
.avatar { width: 38px; height: 38px; border-radius: 50%; object-fit: cover; vertical-align: middle; margin-right: 10px; background: #eef2f7; flex-shrink: 0; }
.avatar-ph { display: inline-flex; align-items: center; justify-content: center; font-size: 17px; color: #9aa8bb; }
.name-cell { display: flex; align-items: center; }

/* รูปในฟอร์มนักเรียน */
.photo-preview {
    width: 104px; height: 104px; border-radius: 18px; object-fit: cover;
    border: 1.5px solid var(--border-strong); background: #f6f9fc;
    display: flex; align-items: center; justify-content: center;
    font-size: 40px; color: #9aa8bb; overflow: hidden; flex-shrink: 0;
}
.photo-row { display: flex; gap: 17px; align-items: center; }

/* ---------- หน้าค้นหาสาธารณะ (นักเรียน) ---------- */
.search-body {
    background:
        radial-gradient(1100px 520px at 15% -10%, rgba(255, 255, 255, .2), transparent 60%),
        linear-gradient(155deg, #2a72bd 0%, #14477f 55%, #0d3260 100%);
    min-height: 100vh; padding: max(14px, env(safe-area-inset-top)) 16px 60px;
}
.search-topbar { max-width: 600px; margin: 0 auto 12px; display: flex; justify-content: flex-end; }
.search-page { max-width: 600px; margin: 0 auto; }
.search-head { text-align: center; color: #fff; margin-bottom: 22px; }
.search-head h1 { font-size: clamp(23px, 5vw, 28px); font-weight: 800; }
.search-head .login-sub { color: rgba(255, 255, 255, .86); margin-bottom: 0; }
.search-form { display: flex; gap: 8px; margin-bottom: 12px; }
.search-form input {
    flex: 1; padding: 17px 22px; border: none; border-radius: 18px;
    font-size: 17px; font-family: inherit; box-shadow: var(--shadow-md); color: var(--text);
}
.search-form input:focus { outline: 3px solid rgba(255, 255, 255, .55); }
.search-hint { color: rgba(255, 255, 255, .9); font-size: 14px; text-align: center; margin-bottom: 18px; }
.search-page .empty, .search-page .alert { background: #fff; border-radius: var(--radius); }
.stu-card {
    background: #fff; border-radius: 18px; padding: 16px;
    display: flex; gap: 16px; align-items: center; margin-bottom: 12px;
    box-shadow: var(--shadow-md); animation: pop .2s ease-out;
}
.stu-photo { width: 78px; height: 78px; border-radius: 16px; object-fit: cover; flex-shrink: 0; background: #eef2f7; }
.stu-photo-ph { display: flex; align-items: center; justify-content: center; font-size: 35px; color: #9aa8bb; }
.stu-info { min-width: 0; }
.stu-name { font-size: 18.5px; font-weight: 800; }
.stu-score { margin-top: 8px; display: flex; align-items: center; gap: 9px; flex-wrap: wrap; font-size: 15px; }
.search-foot { text-align: center; margin-top: 26px; color: #fff; }
.search-foot a { color: #fff; }

/* กล่องผลลัพธ์ + รายละเอียดพฤติกรรม */
.stu-card-wrap {
    background: #fff; border-radius: 18px; margin-bottom: 14px;
    box-shadow: var(--shadow-md); overflow: hidden; animation: pop .2s ease-out;
}
.stu-card-wrap .stu-card { margin-bottom: 0; box-shadow: none; border-radius: 0; animation: none; }
.rec-toggle {
    width: 100%; display: flex; align-items: center; gap: 8px;
    padding: 12px 17px; border: none; border-top: 1px solid var(--border);
    background: #f7f9fc; font-family: inherit; font-size: 14.5px; font-weight: 600;
    color: var(--text-2); cursor: pointer; text-align: left;
    transition: background .12s ease;
}
.rec-toggle:hover { background: #eef3f9; }
.rec-sum { margin-left: auto; display: flex; gap: 7px; font-variant-numeric: tabular-nums; }
.rec-sum-neg { color: var(--critical); font-weight: 800; }
.rec-sum-pos { color: var(--normal); font-weight: 800; }
.rec-list { display: none; padding: 4px 17px 12px; }
.rec-list.open { display: block; }
.rec-row { display: flex; align-items: center; gap: 11px; padding: 10px 0; border-bottom: 1px solid #f2f5f9; }
.rec-row:last-child { border-bottom: none; }
.rec-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.rec-dot-deduct { background: var(--critical-line); }
.rec-dot-add { background: var(--normal-line); }
.rec-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.4; }
.rec-cat { font-size: 15px; color: var(--text); }
.rec-date { font-size: 12.5px; color: var(--muted); }
.rec-pts { font-weight: 800; font-size: 16px; font-variant-numeric: tabular-nums; flex-shrink: 0; }
.rec-pts.deduct { color: var(--critical); }
.rec-pts.add { color: var(--normal); }
.rec-none {
    padding: 13px 17px; border-top: 1px solid var(--border);
    background: var(--normal-bg); color: var(--normal); font-size: 14.5px; font-weight: 600;
}

/* ---------- รายการพฤติกรรม ---------- */
.cat-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.cat-item { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid #f2f5f9; }
.cat-item:last-child { border-bottom: none; }
.cat-name { flex: 1; font-size: 15px; }
.cat-add-row { display: flex; gap: 8px; margin-top: 13px; }
.cat-add-row input[type=text] { flex: 1; min-width: 0; padding: 11px 13px; border: 1.5px solid var(--border-strong); border-radius: var(--radius-sm); font-size: 15.5px; font-family: inherit; }
.cat-add-row input[type=number] { width: 76px; padding: 11px 8px; border: 1.5px solid var(--border-strong); border-radius: var(--radius-sm); font-size: 15.5px; font-family: inherit; text-align: center; }
.cat-add-row input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29, 95, 169, .12); }
.cat-pts-input { width: 70px; padding: 8px; border: 1.5px solid var(--border-strong); border-radius: 10px; font-size: 15.5px; font-family: inherit; text-align: center; font-weight: 600; }
.readonly-pts { background: #eef2f7 !important; color: var(--text); font-weight: 800; text-align: center; pointer-events: none; border-color: var(--border) !important; }

/* ============================================================
   จอเล็ก (มือถือ / แท็บเล็ตแนวตั้ง)
   ============================================================ */
@media (max-width: 820px) {
    .table-wrap { display: none; }
    .stu-list { display: block; }
    .count-info { text-align: center; }
}
@media (max-width: 760px) {
    .summary-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .sum-card.c-total { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 12px; padding: 14px 16px; }
    .sum-card.c-total .sum-icon { margin-bottom: 0; font-size: 20px; }
    .sum-card.c-total .num { font-size: 30px; }
    .sum-card.c-total .lbl { margin-left: auto; }
    .sum-card.c-total .sub { display: none; }
    .toolbar { gap: 8px; }
    .search-field { flex-basis: 100%; }
    .toolbar select { flex: 1 1 30%; min-width: 0; }
    /* ปุ่มคำสั่ง: 2 คอลัมน์ ข้อความยาวขึ้นบรรทัดใหม่ได้ ไม่ซ่อนปุ่มใด */
    .action-bar { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
    .action-bar .btn {
        white-space: normal; text-align: center; line-height: 1.35;
        padding: 9px 10px; font-size: 14px; min-height: 50px;
    }
    .form-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
    .cat-cols { grid-template-columns: 1fr; gap: 20px; }
    .modal-overlay { padding: 12px 10px; }
    .modal { border-radius: 18px; }
    .modal-body { padding: 17px; }
    .modal-head, .modal-foot { padding: 15px 17px; }
    .modal-foot .btn { flex: 1; }
    .topbar-inner { padding: 11px 14px; }
    .brand-mark { width: 40px; height: 40px; font-size: 22px; }
    .user-area { width: 100%; }
    .user-chip { flex: 1; }
    .thr-row { flex-wrap: wrap; }
    .thr-input { margin-left: auto; }
    .lb-nav { width: 42px; height: 42px; font-size: 26px; }
    .lightbox { padding: 62px 10px calc(24px + env(safe-area-inset-bottom)); }
    .lightbox img { max-height: calc(100vh - 150px); }
    .hist-photo img { width: 62px; height: 62px; }
    .role-pick { grid-template-columns: 1fr; }
    .role-opt { flex-direction: row; align-items: center; text-align: left; gap: 11px; padding: 12px 14px; }
    .role-opt .role-desc { margin-left: auto; text-align: right; }
}
@media (max-width: 400px) {
    body { font-size: 15.5px; }
    .form-grid { grid-template-columns: 1fr; }
    .m-actions { grid-template-columns: repeat(2, 1fr); }
    .user-name { max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* จอกว้างมาก */
@media (min-width: 1500px) {
    .container, .topbar-inner { max-width: 1400px; }
}

/* ลดการเคลื่อนไหวตามการตั้งค่าเครื่อง */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
    html { scroll-behavior: auto; }
}

/* พิมพ์เอกสาร */
@media print {
    .topbar, .panel, .action-bar, .footer, #toast, .row-actions, .modal-overlay { display: none !important; }
    body { background: #fff; }
    .table-wrap { display: block !important; box-shadow: none; border: 1px solid #999; }
    .stu-list { display: none !important; }
}
