:root {
  --ink:#0f172a; --ink2:#334155; --muted:#64748b; --line:#e8eaf2; --bg:#f4f5fb; --card:#fff;
  --pri:#4338ca; --pri2:#6366f1; --prisoft:#eef0ff;
  --bad:#e11d48; --badsoft:#fff1f3; --good:#059669; --goodsoft:#e9f9f2; --warn:#d97706; --warnsoft:#fff7e6;
  --r:18px; --shadow:0 1px 2px rgba(15,23,42,.04), 0 6px 20px rgba(15,23,42,.06);
  --safe-b:env(safe-area-inset-bottom, 0px); --safe-t:env(safe-area-inset-top, 0px);
  --nav-h:64px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body { margin: 0; font: 16px/1.5 "Sarabun", system-ui, sans-serif; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); font-size: .9rem; }
.bad { color: var(--bad); } .good { color: var(--good); } .warn { color: var(--warn); }

/* ---------- ปุ่ม/ฟอร์ม ---------- */
.btn { border: 0; border-radius: 14px; padding: .6rem 1.1rem; cursor: pointer; font-weight: 700; background: #eceef5; color: var(--ink); transition: transform .08s, filter .15s; }
.btn:active { transform: scale(.97); } .btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, var(--pri), var(--pri2)); color: #fff; box-shadow: 0 6px 16px rgba(67,56,202,.28); }
.btn.danger { background: var(--bad); color: #fff; } .btn.good { background: var(--good); color: #fff; }
.btn.danger-soft { background: var(--badsoft); color: var(--bad); }
.btn.ghost { background: #eceef5; }
.btn.block { display: block; width: 100%; } .btn.lg { padding: .85rem 1.1rem; font-size: 1.02rem; }
.link { white-space: nowrap; border: 0; background: none; color: var(--pri); font-weight: 700; cursor: pointer; padding: .25rem 0; }
.field { display: block; font-weight: 600; margin: .75rem 0; font-size: .92rem; color: var(--ink2); }
.field input, .field select { display: block; width: 100%; font: inherit; font-size: 1rem; padding: .75rem .9rem; border: 1.5px solid var(--line); border-radius: 14px; background: #f8f9fc; margin-top: .35rem; color: var(--ink); outline: none; }
.field input:focus, .field select:focus { border-color: var(--pri2); background: #fff; }
.err { background: var(--badsoft); color: var(--bad); padding: .6rem .8rem; border-radius: 12px; margin: .5rem 0; font-size: .92rem; }

/* ---------- เข้าสู่ระบบ ---------- */
.auth { min-height: 100%; display: flex; flex-direction: column; background: linear-gradient(170deg, #3730a3 0%, #6366f1 45%, var(--bg) 45.1%); padding: calc(var(--safe-t) + 2.5rem) 1.2rem 2rem; align-items: center; }
.auth-hero { text-align: center; color: #fff; margin-bottom: 1.5rem; }
.auth-hero img { width: 84px; height: 84px; border-radius: 50%; background: #fff; padding: 4px; box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.auth-hero h1 { margin: .7rem 0 .1rem; font-size: 1.7rem; font-weight: 800; }
.auth-hero p { margin: 0; opacity: .85; font-size: .95rem; }
.auth-card { width: min(420px, 100%); background: #fff; border-radius: 24px; padding: 1.5rem; box-shadow: 0 20px 50px rgba(15,23,42,.14); }
.auth-card h2 { margin: 0; font-size: 1.3rem; } .auth-card .btn { margin-top: 1rem; }

/* ---------- โครงแอป ---------- */
.shell { min-height: 100%; }
.appbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: .7rem; padding: calc(var(--safe-t) + .6rem) 1rem .6rem;
  background: rgba(244,245,251,.86); backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px); border-bottom: 1px solid rgba(15,23,42,.05); }
.appbar img { width: 36px; height: 36px; border-radius: 50%; background: #fff; padding: 2px; box-shadow: var(--shadow); }
.appbar .ttl { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.appbar .ttl b { font-size: 1.15rem; font-weight: 800; }
.appbar .ttl small { color: var(--muted); font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.period { border: 0; background: var(--prisoft); color: var(--pri); font-weight: 700; font-size: .85rem; padding: .4rem .75rem; border-radius: 999px; cursor: pointer; white-space: nowrap; }
.period::after { content: " ▾"; }
.content { max-width: 760px; margin: 0 auto; padding: 1rem 1rem calc(var(--nav-h) + var(--safe-b) + 6rem); }

.nav { position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-around; height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(255,255,255,.92); backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px); border-top: 1px solid var(--line); }
.nav button { flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: #94a3b8; font-size: .74rem; font-weight: 700; cursor: pointer; }
.nav svg { width: 26px; height: 26px; fill: currentColor; padding: 2px 14px; box-sizing: content-box; border-radius: 999px; transition: background .2s; }
.nav button.on { color: var(--pri); } .nav button.on svg { background: var(--prisoft); }

/* ---------- ส่วนประกอบ ---------- */
.sec { font-size: 1rem; font-weight: 800; margin: 1.3rem .2rem .55rem; display: flex; align-items: baseline; gap: .4rem; }
.sec small { color: var(--muted); font-weight: 600; font-size: .85rem; }
.hero { border-radius: 24px; padding: 1.1rem 1.2rem; color: #fff; background: linear-gradient(135deg, #3730a3, #6366f1 60%, #818cf8); box-shadow: 0 12px 30px rgba(67,56,202,.28); position: relative; overflow: hidden; }
.hero::after { content: ""; position: absolute; right: -40px; top: -40px; width: 160px; height: 160px; border-radius: 50%; background: rgba(255,255,255,.1); }
.hero .lbl { opacity: .85; font-size: .88rem; font-weight: 600; }
.hero .big { font-size: 2.4rem; font-weight: 800; line-height: 1.1; }
.hero .big small { font-size: 1rem; font-weight: 600; opacity: .85; }
.hero .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; margin-top: .9rem; position: relative; z-index: 1; }
.hero .grid div { background: rgba(255,255,255,.14); border-radius: 14px; padding: .5rem .6rem; }
.hero .grid b { display: block; font-size: 1.25rem; font-weight: 800; } .hero .grid span { font-size: .78rem; opacity: .9; }
.search { display: flex; align-items: center; gap: .5rem; background: #fff; border-radius: 16px; padding: 0 .9rem; margin: .9rem 0 .2rem; box-shadow: var(--shadow); }
.search svg { width: 20px; height: 20px; fill: #94a3b8; flex: none; }
.search input { flex: 1; border: 0; outline: none; font: inherit; padding: .8rem 0; background: none; min-width: 0; }
.chips-scroll { display: flex; gap: .45rem; overflow-x: auto; margin: 0 -1rem .8rem; padding: 0 1rem .1rem; scrollbar-width: none; }
.chips-scroll::-webkit-scrollbar { display: none; }
.chips-scroll button { flex: none; border: 1.5px solid var(--line); background: #fff; border-radius: 999px; padding: .4rem .95rem; font-weight: 700; cursor: pointer; color: var(--ink2); }
.chips-scroll button.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.listhead { display: flex; justify-content: space-between; align-items: center; margin: .9rem .2rem .45rem; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); background: #fff; border-radius: var(--r); }
.info { background: var(--prisoft); color: #3730a3; border-radius: 16px; padding: .8rem 1rem; font-size: .9rem; }

/* การ์ดนักเรียน */
.cards { display: flex; flex-direction: column; gap: .5rem; }
.st { display: flex; align-items: center; gap: .75rem; background: var(--card); border-radius: var(--r); padding: .7rem .85rem; box-shadow: var(--shadow); cursor: pointer; border: 2px solid transparent; transition: border-color .15s, background .15s; user-select: none; }
.st:active { transform: scale(.99); }
.st.sel { border-color: var(--pri2); background: #f7f7ff; }
.st.left { opacity: .5; }
.st .chk { flex: none; width: 26px; height: 26px; border-radius: 50%; border: 2px solid #cbd5e1; display: grid; place-items: center; color: #fff; font-size: .8rem; font-weight: 800; background: #fff; }
.st.sel .chk { background: var(--pri2); border-color: var(--pri2); }
.st .av { flex: none; width: 42px; height: 42px; border-radius: 14px; display: grid; place-items: center; font-weight: 800; background: var(--prisoft); color: var(--pri); font-size: 1rem; }
.st.fail .av { background: var(--badsoft); color: var(--bad); }
.st .nm { flex: 1; min-width: 0; }
.st .nm b { display: block; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st .nm small { color: var(--muted); font-size: .8rem; }
.ring { flex: none; --p: 100; --c: var(--good); width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center;
  background: conic-gradient(var(--c) calc(var(--p) * 1%), #edf0f5 0); }
.ring span { width: 42px; height: 42px; border-radius: 50%; background: #fff; display: grid; place-items: center; font-weight: 800; font-size: 1rem; color: var(--c); }
.st.sel .ring span { background: #f7f7ff; }
.ring.mid { --c: var(--warn); } .ring.low { --c: var(--bad); }
.tag { display: inline-block; font-size: .72rem; font-weight: 700; padding: .05rem .5rem; border-radius: 999px; margin-left: .25rem; vertical-align: 1px; }
.tag.no { background: var(--bad); color: #fff; } .tag.room { background: #eceef5; color: var(--ink2); }

/* การ์ดห้อง */
.rooms { display: grid; grid-template-columns: repeat(2, 1fr); gap: .55rem; }
.rm { border: 0; text-align: left; background: #fff; border-radius: var(--r); padding: .85rem .95rem; box-shadow: var(--shadow); cursor: pointer; }
.rm:active { transform: scale(.98); }
.rm b { font-size: 1.15rem; font-weight: 800; display: block; }
.rm .meta { color: var(--muted); font-size: .82rem; }
.rm .bar { height: 6px; border-radius: 3px; background: #edf0f5; overflow: hidden; margin: .55rem 0 .35rem; }
.rm .bar i { display: block; height: 100%; background: var(--good); border-radius: 3px; }
.rm .bar i.mid { background: var(--warn); } .rm .bar i.low { background: var(--bad); }
.rm .f { font-size: .82rem; font-weight: 700; }

/* กลุ่มรายการแบบ iOS */
.group { background: #fff; border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; margin: .9rem 0; }
.item { display: flex; justify-content: space-between; align-items: center; gap: .8rem; padding: .85rem 1rem; border: 0; border-bottom: 1px solid var(--line); background: none; width: 100%; text-align: left; }
.item:last-child { border-bottom: 0; } button.item { cursor: pointer; } button.item::after { content: "›"; color: #cbd5e1; font-size: 1.4rem; line-height: 1; }
.item .who { min-width: 0; } .item .who b { display: block; font-weight: 600; } .item .who small { color: var(--muted); font-size: .8rem; }
.switch { position: relative; flex: none; width: 52px; height: 32px; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch i { position: absolute; inset: 0; border-radius: 999px; background: #e2e8f0; transition: background .2s; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.switch input:checked + i { background: var(--good); } .switch input:checked + i::after { transform: translateX(20px); }
.switch input:disabled { cursor: not-allowed; } .switch input:disabled + i { opacity: .55; }
.profile { display: flex; align-items: center; gap: .9rem; background: #fff; border-radius: var(--r); padding: 1rem; box-shadow: var(--shadow); }
.profile .av { width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center; background: linear-gradient(135deg, var(--pri), var(--pri2)); color: #fff; font-weight: 800; font-size: 1.4rem; }
.profile b { display: block; font-size: 1.1rem; }

/* แถบเลือกหลายคน */
.fab { position: fixed; z-index: 35; left: .75rem; right: .75rem; bottom: calc(var(--nav-h) + var(--safe-b) + .6rem); max-width: 560px; margin: 0 auto; display: flex; align-items: center; gap: .5rem;
  background: var(--ink); color: #fff; border-radius: 20px; padding: .5rem; box-shadow: 0 14px 36px rgba(15,23,42,.35); animation: up .2s ease-out; }
.fab .x { border: 0; background: rgba(255,255,255,.12); color: #fff; width: 36px; height: 36px; border-radius: 50%; cursor: pointer; flex: none; }
.fab span { flex: 1; font-weight: 700; font-size: .92rem; }
.fab .btn { padding: .55rem .85rem; border-radius: 14px; }
@keyframes up { from { transform: translateY(20px); opacity: 0; } }

/* ---------- Bottom sheet ---------- */
dialog.sheet { border: 0; padding: .5rem 1.2rem calc(1.2rem + var(--safe-b)); margin: auto 0 0; width: 100%; max-width: 100%; max-height: 92vh; border-radius: 26px 26px 0 0; background: #fff; box-shadow: 0 -10px 40px rgba(0,0,0,.2); overflow-y: auto; }
dialog.sheet[open] { animation: sheet .26s cubic-bezier(.2,.9,.3,1); }
dialog.sheet::backdrop { background: rgba(15,23,42,.45); }
@keyframes sheet { from { transform: translateY(100%); } }
.grab { width: 40px; height: 5px; border-radius: 3px; background: #dfe3ea; margin: .2rem auto .8rem; }
dialog.sheet h2 { margin: 0 0 .15rem; font-size: 1.25rem; font-weight: 800; }
.sheet-act { display: grid; grid-template-columns: 1fr 1.6fr; gap: .6rem; margin-top: 1rem; }
.presets { display: flex; flex-wrap: wrap; gap: .4rem; margin: .8rem 0 .2rem; }
.presets button { border: 1.5px solid var(--line); background: #f8f9fc; border-radius: 12px; padding: .45rem .7rem; cursor: pointer; font-size: .9rem; font-weight: 600; }
.presets button b { margin-left: .3rem; }
.presets button.on { border-color: var(--pri2); background: var(--prisoft); color: var(--pri); }
.stepper { display: grid; grid-template-columns: 52px 1fr 52px; gap: .4rem; margin-top: .35rem; }
.stepper button { border: 0; border-radius: 14px; background: #eceef5; font-size: 1.4rem; font-weight: 700; cursor: pointer; }
.stepper input { margin: 0 !important; text-align: center; font-size: 1.3rem !important; font-weight: 800; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: #eceef5; border-radius: 14px; padding: 4px; margin-top: .35rem; gap: 4px; }
.seg button { border: 0; background: none; border-radius: 11px; padding: .6rem; font-weight: 700; cursor: pointer; color: var(--muted); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 2px 6px rgba(15,23,42,.1); }

.hist-head { display: flex; align-items: center; gap: .9rem; }
.hist-head .ring { width: 70px; height: 70px; } .hist-head .ring span { width: 58px; height: 58px; font-size: 1.35rem; }
.hist-head b { display: block; font-size: 1.15rem; font-weight: 800; }
.hist-act { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin: 1rem 0 0; }
.rec { display: flex; gap: .75rem; align-items: center; padding: .7rem 0; border-bottom: 1px solid var(--line); }
.rec .pt { flex: none; width: 50px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-weight: 800; }
.rec .pt.bad { background: var(--badsoft); } .rec .pt.good { background: var(--goodsoft); }
.rec .tx { flex: 1; min-width: 0; } .rec .tx small { color: var(--muted); font-size: .8rem; display: block; }
.rec.cx { opacity: .5; } .rec.cx .rs { text-decoration: line-through; }
.rec .btn { padding: .35rem .7rem; font-size: .85rem; }
#hClose { margin-top: 1rem; }

.toast { position: fixed; z-index: 60; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 1rem); transform: translateX(-50%); background: var(--ink); color: #fff; padding: .7rem 1.2rem; border-radius: 14px; font-weight: 600; box-shadow: 0 10px 30px rgba(0,0,0,.25); animation: up .2s; max-width: 90vw; }

/* ---------- จอใหญ่: แถบนำทางด้านซ้าย + sheet กลางจอ ---------- */
@media (min-width: 900px) {
  :root { --nav-h: 0px; }
  .shell { padding-left: 96px; }
  .nav { top: 0; right: auto; width: 96px; height: 100%; flex-direction: column; justify-content: flex-start; gap: .5rem; padding: 5rem 0 0; border-top: 0; border-right: 1px solid var(--line); }
  .nav button { flex: none; height: 64px; }
  .content { padding-bottom: 7rem; }
  .rooms { grid-template-columns: repeat(3, 1fr); }
  .fab { left: 96px; bottom: 1.2rem; }
  .toast { bottom: 1.5rem; }
  dialog.sheet { margin: auto; width: min(520px, 94vw); border-radius: 26px; max-height: 88vh; }
  dialog.sheet[open] { animation: pop .2s ease-out; }
  .grab { display: none; } dialog.sheet { padding-top: 1.3rem; }
}
@keyframes pop { from { transform: scale(.96); opacity: 0; } }
@media print { .appbar, .nav, .fab, .search, .chips-scroll, .listhead { display: none !important; } .shell { padding: 0; } .st { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; } }
