/* ===== ระบบลาเรียนออนไลน์ — สไตล์กลาง ===== */
:root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #20242c;
    --surface-3: #2a2f39;
    --border: #2e333d;
    --text: #e6e8ec;
    --muted: #9aa1ad;
    --faint: #858d9a;
    --primary: #5aa2ea;               /* สีตัวอักษร/ไอคอน บนพื้นเข้ม */
    --primary-btn: #2872c4;           /* พื้นปุ่ม (ตัวอักษรขาวอ่านชัด) */
    --primary-hover: #2163ab;
    --primary-soft: rgba(90, 162, 234, .14);
    --green: #2fb56a;
    --green-btn: #1e7d45;
    --green-soft: rgba(47, 181, 106, .14);
    --red: #f07070;
    --red-btn: #c93c3c;
    --red-soft: rgba(226, 85, 85, .14);
    --amber: #e6a817;
    --amber-soft: rgba(230, 168, 23, .16);
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-w: 250px;
    --shadow: 0 10px 30px rgba(0, 0, 0, .35);
    --font: "IBM Plex Sans Thai", "Segoe UI", Tahoma, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    font-size: 15px;
}
h1, h2, h3 { margin: 0 0 .5em; line-height: 1.3; font-weight: 600; }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.1rem; }
a { color: var(--primary); }
small { font-size: .85rem; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- ฟอร์ม ---------- */
.field { margin-bottom: 16px; text-align: left; }
.field > label, .label { display: block; margin-bottom: 6px; font-size: .88rem; color: var(--muted); font-weight: 500; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; min-width: 0; }
.input-icon { position: relative; }
.input-icon > i:first-child {
    position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
    color: var(--faint); pointer-events: none;
}
input, select, textarea {
    width: 100%;
    padding: 11px 14px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font: inherit;
    transition: border-color .2s, box-shadow .2s;
}
.input-icon input, .input-icon select { padding-left: 42px; }
.input-icon.pw input { padding-right: 44px; }
textarea { resize: vertical; min-height: 80px; }
::placeholder { color: #8b93a0; opacity: 1; }
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
input[type="date"] { color-scheme: dark; }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 36px; }
.pw-toggle {
    position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
    background: none; border: 0; color: var(--faint); cursor: pointer; padding: 8px 10px; border-radius: 6px;
}
.pw-toggle:hover { color: var(--text); }
.hint { display: block; margin-top: 6px; font-size: .8rem; color: var(--faint); }
.hint.error { color: var(--red); }
.hint.ok { color: var(--green); }

/* ---------- ปุ่ม ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 10px 18px; border-radius: var(--radius-sm); border: 1px solid transparent;
    font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
    transition: background .2s, transform .1s, border-color .2s; white-space: nowrap;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary-btn); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-success { background: var(--green-btn); color: #fff; }
.btn-success:hover:not(:disabled) { filter: brightness(1.08); }
.btn-danger { background: var(--red-btn); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-3); }
.btn-soft-green { background: var(--green-soft); color: var(--green); }
.btn-soft-red { background: var(--red-soft); color: var(--red); }
.btn-soft-green:hover:not(:disabled), .btn-soft-red:hover:not(:disabled) { filter: brightness(1.25); }
.btn-sm { padding: 6px 12px; font-size: .85rem; }
.btn-block { width: 100%; padding: 13px; font-size: 1.02rem; }
.icon-btn {
    background: none; border: 0; color: var(--muted); cursor: pointer;
    width: 36px; height: 36px; border-radius: 8px; font-size: 1rem;
    display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.danger:hover { color: var(--red); }

/* ---------- หน้า auth (login / signup / forgot) ---------- */
.auth-wrap {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    padding: 24px 16px;
    background: radial-gradient(ellipse at top, rgba(59, 143, 224, .12), transparent 60%), var(--bg);
}
.auth-card {
    width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border);
    border-radius: 16px; padding: 36px 32px; box-shadow: var(--shadow); text-align: center;
}
.auth-card.wide { max-width: 470px; }
.auth-logo {
    width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
    background: var(--primary-soft); color: var(--primary); font-size: 1.9rem;
}
.auth-card h1 { font-size: 1.5rem; margin-bottom: 4px; }
.auth-sub { color: var(--muted); margin: 0 0 26px; }
.auth-links { margin-top: 22px; color: var(--muted); font-size: .92rem; display: grid; gap: 10px; }
.auth-links a { text-decoration: none; font-weight: 500; }
.auth-links a:hover { text-decoration: underline; }
.notice {
    text-align: left; padding: 12px 14px; border-radius: var(--radius-sm); margin-bottom: 18px;
    font-size: .92rem; border: 1px solid;
}
.notice-warn { background: var(--amber-soft); border-color: rgba(230, 168, 23, .4); color: #f3c45a; }
.notice-ok { background: var(--green-soft); border-color: rgba(47, 181, 106, .4); color: #5fd291; }
.notice-err { background: var(--red-soft); border-color: rgba(226, 85, 85, .4); color: #f08a8a; }
.notice .btn { margin-top: 10px; }

.role-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.role-pick input { position: absolute; opacity: 0; pointer-events: none; }
.role-pick label {
    display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 8px;
    border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
    cursor: pointer; color: var(--muted); font-weight: 500; transition: border-color .2s, background .2s, color .2s; margin: 0;
}
.role-pick label i { font-size: 1.4rem; }
.role-pick input:checked + label { border-color: var(--primary); color: var(--text); background: var(--primary-soft); }
.role-pick input:focus-visible + label { outline: 2px solid var(--primary); }
.pw-meter { height: 4px; border-radius: 4px; background: var(--surface-3); margin-top: 8px; overflow: hidden; }
.pw-meter > span { display: block; height: 100%; width: 0; transition: width .25s, background .25s; }

/* ---------- Layout dashboard ---------- */
.app-page .app { visibility: hidden; }
.app-page.ready .app { visibility: visible; }
.app-page:not(.ready)::after {
    content: ""; position: fixed; top: 50%; left: 50%; width: 40px; height: 40px; margin: -20px;
    border: 3px solid var(--surface-3); border-top-color: var(--primary); border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); z-index: 50;
    background: var(--surface); border-right: 1px solid var(--border);
    display: flex; flex-direction: column; overflow-y: auto;
}
.brand { padding: 20px; display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.08rem; border-bottom: 1px solid var(--border); }
.brand i { color: var(--primary); font-size: 1.3rem; }
.profile { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; gap: 12px; align-items: center; }
.profile .avatar { width: 42px; height: 42px; font-size: 1.05rem; }
.profile-name { font-weight: 600; word-break: break-word; }
.profile-meta { font-size: .78rem; color: var(--muted); word-break: break-all; }
.role-tag { display: inline-block; font-size: .7rem; padding: 1px 8px; border-radius: 10px; background: var(--primary-soft); color: var(--primary); margin-bottom: 2px; }
.nav { padding: 10px 0; display: flex; flex-direction: column; flex: 1; }
.nav-item {
    display: flex; align-items: center; gap: 14px; padding: 12px 20px; cursor: pointer;
    color: var(--muted); border: 0; border-left: 3px solid transparent; background: none;
    font: inherit; text-align: left; width: 100%; transition: background .2s, color .2s;
}
.nav-item i { width: 18px; text-align: center; }
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); border-left-color: var(--primary); font-weight: 600; }
.nav-item .count {
    margin-left: auto; min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px;
    background: var(--red-btn); color: #fff; font-size: .75rem; font-weight: 700;
    display: inline-flex; align-items: center; justify-content: center;
}
.nav-item.logout { margin-top: auto; color: #f08a8a; border-top: 1px solid var(--border); }
.nav-item.logout:hover { background: var(--red-soft); }

.topbar { display: none; }
.sidebar-backdrop { display: none; }
.main { margin-left: var(--sidebar-w); padding: 28px 32px 60px; min-height: 100vh; max-width: calc(var(--sidebar-w) + 1100px); }
.content-section { display: none; animation: fade .2s ease; }
.content-section.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.section-head h2 { margin: 0; }

/* ---------- Card / list ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.panel-title { font-size: 1rem; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }
.list { display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }
/* ชื่อ/ข้อความยาวที่ไม่มีช่องว่าง (เช่นภาษาไทยติดกันยาว ๆ) ให้ตัดบรรทัดได้ ไม่ดันหน้าจอให้กว้างเกิน */
.item-title, .item-sub, .leave-title, .leave-meta, .profile-name, .modal-msg { overflow-wrap: anywhere; }
td:first-child { min-width: 200px; overflow-wrap: anywhere; }
.leave-head > :first-child { flex: 1 1 auto; min-width: 0; }
.item {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 14px 16px; display: flex; align-items: center; gap: 14px;
}
.item.highlight { border-color: rgba(47, 181, 106, .5); background: linear-gradient(90deg, var(--green-soft), var(--surface) 60%); }
.item.dim { opacity: .7; }
.item-body { flex: 1; min-width: 0; }
.item-title { font-weight: 600; word-break: break-word; }
.item-sub { font-size: .85rem; color: var(--muted); word-break: break-all; }
.item-actions { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }
.avatar {
    width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0; color: #fff; font-weight: 600;
    display: inline-flex; align-items: center; justify-content: center; font-size: 1.1rem;
}
.avatar-sm { width: 32px; height: 32px; font-size: .85rem; }
.empty { text-align: center; padding: 34px 16px; color: var(--faint); border: 1px dashed var(--border); border-radius: var(--radius); }
.empty i { font-size: 2rem; display: block; margin-bottom: 10px; opacity: .6; }

.badge {
    display: inline-flex; align-items: center; gap: 6px; padding: 3px 11px; border-radius: 20px;
    font-size: .8rem; font-weight: 600; white-space: nowrap;
}
.badge-pending { background: var(--amber-soft); color: var(--amber); }
.badge-approved { background: var(--green-soft); color: var(--green); }
.badge-rejected { background: var(--red-soft); color: var(--red); }
.badge-info { background: var(--primary-soft); color: var(--primary); }
.badge-muted { background: var(--surface-3); color: var(--muted); }

/* ใบลา */
.leave-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.leave-card.is-pending { border-left: 3px solid var(--amber); }
.leave-card.is-approved { border-left: 3px solid var(--green); }
.leave-card.is-rejected { border-left: 3px solid var(--red); }
.leave-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.leave-title { font-weight: 600; font-size: 1.05rem; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.leave-meta { display: grid; gap: 4px; margin-top: 8px; font-size: .9rem; color: var(--muted); }
.leave-meta b { color: var(--text); font-weight: 500; }
.leave-reason { margin-top: 8px; white-space: pre-wrap; word-break: break-word; }
.teacher-note { margin-top: 10px; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--surface-2); font-size: .9rem; white-space: pre-wrap; word-break: break-word; }
.teacher-note i { color: var(--primary); margin-right: 6px; }
.leave-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.leave-time { font-size: .8rem; color: var(--faint); }
.thumb { width: 84px; height: 84px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; margin-top: 10px; display: block; background: var(--surface-2); }

/* สถิติ */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat-label { font-size: .82rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.stat-value { font-size: 1.6rem; font-weight: 700; margin-top: 2px; }
.stat-value small { font-size: .8rem; font-weight: 500; color: var(--muted); margin-left: 4px; }

/* ตัวกรอง */
.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.filters .grow { flex: 1 1 220px; }
.filters select { width: auto; min-width: 150px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
    padding: 6px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--surface);
    color: var(--muted); cursor: pointer; font: inherit; font-size: .88rem;
}
.chip:hover { color: var(--text); }
.chip.active { background: var(--primary-btn); border-color: var(--primary-btn); color: #fff; }

.search-row { display: flex; gap: 10px; }
.search-row .input-icon { flex: 1; }

/* ตาราง */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; background: var(--surface); min-width: 620px; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: .85rem; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: center; }
tbody tr:hover { background: var(--surface-2); }

/* อัปโหลดรูป */
.dropzone {
    position: relative; border: 2px dashed var(--border); border-radius: var(--radius);
    padding: 26px 16px; text-align: center; color: var(--muted); cursor: pointer; transition: border-color .2s, background .2s, color .2s;
    background: rgba(255, 255, 255, .02);
}
.dropzone:focus-within { outline: 2px solid var(--primary); outline-offset: 2px; }
.thumb-btn { background: none; border: 0; padding: 0; cursor: zoom-in; display: block; border-radius: 8px; }
.dropzone:hover, .dropzone.dragover { border-color: var(--primary); background: var(--primary-soft); color: var(--text); }
.dropzone i { font-size: 1.9rem; display: block; margin-bottom: 8px; }
.dropzone input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.preview { position: relative; display: inline-block; margin-top: 4px; }
.preview img { max-width: 100%; max-height: 220px; border-radius: var(--radius-sm); border: 1px solid var(--border); display: block; }
.preview .remove {
    position: absolute; top: -10px; right: -10px; width: 28px; height: 28px; border-radius: 50%;
    background: var(--red-btn); color: #fff; border: 0; cursor: pointer; box-shadow: 0 2px 6px rgba(0, 0, 0, .5);
}
.preview-info { font-size: .8rem; color: var(--faint); margin-top: 6px; }
.days-pill { display: inline-block; margin-top: 8px; font-size: .88rem; color: var(--primary); background: var(--primary-soft); padding: 4px 12px; border-radius: 20px; }
.days-pill.error { color: var(--red); background: var(--red-soft); }

/* ---------- Toast ---------- */
#toast-box { position: fixed; bottom: 16px; right: 16px; z-index: 1000; display: grid; gap: 10px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
    display: flex; gap: 10px; align-items: flex-start; padding: 12px 16px; border-radius: 10px;
    background: var(--surface-2); border: 1px solid var(--border); box-shadow: var(--shadow);
    opacity: 0; transform: translateY(12px); transition: .3s; cursor: pointer; font-size: .92rem;
}
.toast.show { opacity: 1; transform: none; }
.toast i { margin-top: 3px; }
.toast-success i { color: var(--green); }
.toast-error i { color: var(--red); }
.toast-warn i { color: var(--amber); }
.toast-info i { color: var(--primary); }
.toast-success { border-left: 3px solid var(--green); }
.toast-error { border-left: 3px solid var(--red); }
.toast-warn { border-left: 3px solid var(--amber); }
.toast-info { border-left: 3px solid var(--primary); }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed; inset: 0; z-index: 900; background: rgba(0, 0, 0, .65);
    display: flex; align-items: center; justify-content: center; padding: 16px; animation: fade .15s;
}
.modal { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 22px; box-shadow: var(--shadow); }
.modal h3 { margin-bottom: 8px; }
.modal-msg { color: var(--muted); margin: 0 0 14px; white-space: pre-wrap; }
.modal-input { margin-bottom: 4px; }
.modal-err { color: var(--red); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.lightbox { flex-direction: column; gap: 12px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 82vh; border-radius: 10px; }
.lightbox-open { color: #fff; font-size: .9rem; }
.load-error {
    position: fixed; inset: 0; z-index: 800; display: flex; flex-direction: column; gap: 14px;
    align-items: center; justify-content: center; text-align: center; padding: 24px; background: var(--bg);
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .topbar {
        display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 40;
        padding: 10px 14px; background: var(--surface); border-bottom: 1px solid var(--border);
    }
    .topbar .brand { border: 0; padding: 0; font-size: 1rem; flex: 1; }
    .topbar .count { background: var(--red-btn); color: #fff; border-radius: 10px; font-size: .72rem; padding: 1px 7px; margin-left: -6px; font-weight: 700; }
    .sidebar { transform: translateX(-100%); transition: transform .25s ease; box-shadow: var(--shadow); }
    .sidebar.open { transform: none; }
    .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 45; opacity: 0; pointer-events: none; transition: opacity .25s; }
    .sidebar-backdrop.show { opacity: 1; pointer-events: auto; }
    .main { margin-left: 0; padding: 20px 16px 60px; }
    input, select, textarea { font-size: 16px; }   /* กัน iOS ซูมหน้าเมื่อแตะช่องกรอก */
}
@media (max-width: 560px) {
    body { font-size: 14.5px; }
    .auth-card { padding: 28px 20px; }
    .row { flex-direction: column; gap: 0; }
    .search-row { flex-direction: column; }
    .item { flex-wrap: wrap; }
    .item-actions { width: 100%; justify-content: flex-end; }
    .filters select { flex: 1; }
    .stat-value { font-size: 1.35rem; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.evidence-btn { margin-top: 10px; }
