@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Thai:wght@400;500;600;700&display=swap");

/* โทนหมึกคราม บนกระดาษสมุดคะแนน */
:root {
  --ink: #1e2a78;
  --ink-2: #3949ab;
  --ink-3: #6c78c4;
  --paper: #f5f7fc;
  --surface: #ffffff;
  --wash: #e9edfa;
  --line: #d6dcec;
  --line-soft: #e7ebf5;
  --text: #1b2240;
  --muted: #5d6685;
  --ok: #17734b;
  --ok-bg: #e3f4ec;
  --warn: #9a5410;
  --warn-bg: #fdf0e1;
  --bad: #ae2a22;
  --bad-bg: #fcebea;
  --focus: #f2b632;
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --font: "IBM Plex Sans Thai", "Sarabun", "Noto Sans Thai", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font); font-size: 15px; line-height: 1.6; color: var(--text);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--ink-2); }
h1, h2, h3 { color: var(--ink); line-height: 1.3; margin: 0 0 .4em; font-weight: 600; }
h1 { font-size: 26px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }
p { margin: 0 0 .8em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- แถบบน ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 14px;
  padding: 10px 20px; background: var(--ink); color: #fff;
}
.topbar a { color: inherit; text-decoration: none; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 34px; height: 34px; border-radius: 50%; background: #fff; object-fit: cover; }
.brand strong { font-size: 16px; font-weight: 600; white-space: nowrap; }
.brand span { display: block; font-size: 12px; opacity: .75; white-space: nowrap; }
.topnav { display: flex; gap: 4px; margin-left: 18px; }
.topnav a { padding: 6px 12px; border-radius: 999px; font-size: 14px; opacity: .85; }
.topnav a:hover { background: rgba(255,255,255,.12); opacity: 1; }
.topnav a[aria-current="page"] { background: rgba(255,255,255,.18); opacity: 1; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.top-right select { background: rgba(255,255,255,.12); color: #fff; border: 1px solid rgba(255,255,255,.25); border-radius: 999px; padding: 5px 10px; font: inherit; font-size: 14px; }
.top-right select option { color: var(--text); }
.who { font-size: 13px; opacity: .85; white-space: nowrap; }
.linkish { white-space: nowrap; background: none; border: 0; color: inherit; font: inherit; font-size: 13px; cursor: pointer; text-decoration: underline; opacity: .85; padding: 4px; }

.page { max-width: 1180px; margin: 0 auto; padding: 28px 20px 60px; }
.page.wide { max-width: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 0; color: var(--muted); }
.crumb { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.crumb a { color: var(--muted); }

/* ---------- ปุ่ม/ฟอร์ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 38px; padding: 7px 15px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: var(--ink-2); }
.btn.danger { color: var(--bad); border-color: #efc4c1; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { min-height: 30px; padding: 3px 11px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; flex: none; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

label.field { display: grid; gap: 5px; font-size: 14px; color: var(--muted); }
input, select, textarea {
  font: inherit; font-size: 15px; color: var(--text);
  border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff;
  padding: 8px 10px; min-height: 38px; width: 100%;
}
textarea { min-height: 120px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--ink-2); outline: 2px solid #c7cff3; outline-offset: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--ink); }

/* ---------- พื้นผิว ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 20px; }
.panel + .panel { margin-top: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.panel-head h2 { margin: 0; }
.note { background: var(--wash); border-radius: var(--r-md); padding: 12px 14px; font-size: 14px; color: #34407a; }
.note.warn { background: var(--warn-bg); color: var(--warn); }
.note.bad { background: var(--bad-bg); color: var(--bad); }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); font-size: 16px; margin-bottom: 4px; }

/* ---------- ตารางทั่วไป ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r-md); background: #fff; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th { text-align: left; font-weight: 600; color: var(--muted); background: #f8f9fd; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.list td { padding: 9px 12px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
table.list tr:last-child td { border-bottom: 0; }
table.list td.num, table.list th.num { text-align: right; }

/* ---------- ป้ายเกรด ---------- */
.grade { display: inline-grid; place-items: center; min-width: 38px; height: 26px; padding: 0 8px; border-radius: 7px; font-weight: 600; font-variant-numeric: tabular-nums; background: var(--wash); color: var(--ink); }
.grade.g4, .grade.g35, .grade.g3 { background: var(--ok-bg); color: var(--ok); }
.grade.g0, .grade.gร, .grade.gมส { background: var(--bad-bg); color: var(--bad); }
.grade.gnone { background: transparent; color: var(--muted); font-weight: 400; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--wash); color: var(--ink-2); white-space: nowrap; }
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }

/* ---------- แถบความคืบหน้า ---------- */
.meter { height: 6px; border-radius: 999px; background: var(--line-soft); overflow: hidden; min-width: 80px; }
.meter > i { display: block; height: 100%; background: var(--ink-2); border-radius: inherit; }
.meter.done > i { background: var(--ok); }

/* ---------- แจ้งเตือนลอย ---------- */
.toast-zone { position: fixed; bottom: 18px; right: 18px; z-index: 400; display: grid; gap: 8px; width: min(380px, calc(100% - 24px)); }
.toast { background: var(--ink); color: #fff; padding: 11px 16px; border-radius: var(--r-md); font-size: 14px; box-shadow: 0 10px 30px rgba(20, 28, 80, .3); }
.toast.bad { background: var(--bad); }
@media (prefers-reduced-motion: no-preference) { .toast { animation: rise .2s ease-out; } }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }

/* ---------- กล่องโต้ตอบ ---------- */
dialog { border: 0; border-radius: var(--r-lg); padding: 0; width: min(640px, calc(100% - 24px)); max-height: calc(100dvh - 40px); box-shadow: 0 30px 80px rgba(20, 28, 80, .35); color: var(--text); }
dialog::backdrop { background: rgba(22, 30, 80, .45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px 0; }
.dlg-head h2 { margin: 0; }
.dlg-body { padding: 16px 22px; overflow: auto; max-height: calc(100dvh - 190px); }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px 18px; border-top: 1px solid var(--line-soft); }
.x { border: 0; background: var(--wash); width: 34px; height: 34px; border-radius: 50%; font-size: 20px; line-height: 1; cursor: pointer; color: var(--ink); }

/* ---------- แท็บ ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; font: inherit; font-size: 15px; padding: 10px 16px; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); font-weight: 600; }

/* ---------- หน้าเข้าสู่ระบบ ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px; background:
  repeating-linear-gradient(0deg, transparent 0 31px, #e3e8f6 31px 32px), var(--paper); }
.auth-card { width: min(400px, 100%); background: #fff; border: 1px solid var(--line); border-radius: var(--r-lg); padding: 30px 28px; box-shadow: 0 24px 60px rgba(30, 42, 120, .12); }
.auth-card img { width: 64px; height: 64px; border-radius: 50%; display: block; margin-bottom: 14px; }
.auth-card form { display: grid; gap: 14px; margin-top: 18px; }
.err { color: var(--bad); background: var(--bad-bg); border-radius: var(--r-sm); padding: 9px 12px; font-size: 14px; }

/* ---------- แดชบอร์ด ---------- */
.dash { display: grid; grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr); gap: 18px; align-items: start; }
.course-row { display: grid; grid-template-columns: 74px minmax(0, 1fr) 150px auto; gap: 14px; align-items: center; padding: 12px 4px; border-bottom: 1px solid var(--line-soft); color: inherit; text-decoration: none; }
.course-row:last-child { border-bottom: 0; }
.course-row:hover { background: #fafbff; }
.room-chip { font-weight: 600; color: var(--ink); background: var(--wash); border-radius: var(--r-sm); text-align: center; padding: 6px 0; font-variant-numeric: tabular-nums; }
.course-row .title { font-weight: 500; }
.course-row .sub { font-size: 13px; color: var(--muted); }
.stat-list { display: grid; gap: 10px; margin: 0; }
.stat-list div { display: flex; justify-content: space-between; border-bottom: 1px dashed var(--line); padding-bottom: 8px; }
.stat-list dt { color: var(--muted); }
.stat-list dd { margin: 0; font-weight: 600; color: var(--ink); }

/* ---------- ตารางกรอกคะแนน (สมุดคะแนน) ---------- */
.sheet-tools { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.save-state { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.save-state::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.save-state.pending::before { background: var(--focus); }
.save-state.error::before { background: var(--bad); }
.sheet-wrap { overflow: auto; max-height: calc(100dvh - 210px); border: 1px solid var(--line); border-radius: var(--r-md); background: #fff; }
table.sheet { border-collapse: separate; border-spacing: 0; font-size: 14px; }
.sheet th, .sheet td { border-right: 1px solid var(--line-soft); border-bottom: 1px solid var(--line-soft); padding: 0; height: 36px; }
.sheet thead th { position: sticky; top: 0; z-index: 3; background: #f8f9fd; font-weight: 500; color: var(--muted); vertical-align: bottom; padding: 6px 4px; min-width: 64px; max-width: 120px; }
.sheet thead tr:nth-child(2) th { top: 74px; }
.sheet thead th.col-head { height: 74px; }
.col-head .code { display: block; font-size: 12px; color: var(--ink-2); font-weight: 600; }
.col-head .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-size: 12px; line-height: 1.35; color: var(--text); }
.col-head .max { display: block; font-size: 12px; margin-top: 2px; }
.col-head.final { background: #eef1fb; }
.col-head button { border: 0; background: none; padding: 0; font: inherit; text-align: center; width: 100%; cursor: pointer; color: inherit; }
.sheet .stick { position: sticky; left: 0; z-index: 2; background: #fff; }
.sheet thead .stick { z-index: 4; background: #f8f9fd; }
.sheet .no { width: 44px; min-width: 44px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.sheet .name { left: 44px; min-width: 210px; max-width: 240px; padding: 0 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-right: 2px solid var(--line); }
.sheet td.cell input {
  border: 0; border-radius: 0; height: 35px; min-height: 0; width: 64px; padding: 0 6px; text-align: center;
  font-variant-numeric: tabular-nums; background: transparent; font-size: 15px;
}
.sheet td.cell input:focus { outline: 2px solid var(--ink-2); outline-offset: -2px; background: #f4f6ff; }
.sheet td.cell.final { background: #f7f8fe; }
.sheet td.cell.low input { color: var(--warn); font-weight: 600; }
.sheet td.cell.invalid input { background: var(--bad-bg); color: var(--bad); }
.sheet td.calc { padding: 0 10px; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; background: #fbfbfe; min-width: 70px; }
.sheet td.calc.grade-col { text-align: center; }
.sheet tr.inactive td { color: var(--muted); background: #fafafa; }
.sheet tr.inactive td.cell input { color: var(--muted); }
.sheet tbody tr:hover td.stick { background: #f6f8ff; }
.sheet tfoot td { background: #f8f9fd; font-size: 13px; color: var(--muted); padding: 4px 6px; text-align: center; position: sticky; bottom: 0; }
.sheet tfoot td.stick { z-index: 3; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: 10px; }

/* โครงสร้างคะแนน */
.item-list { display: grid; gap: 6px; }
.item { display: grid; grid-template-columns: 120px minmax(0, 1fr) 90px auto; gap: 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line-soft); border-radius: var(--r-sm); background: #fff; }
.item.final { background: #f7f8fe; }
.item .code { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.ratio-bar { display: flex; height: 30px; border-radius: var(--r-sm); overflow: hidden; font-size: 13px; color: #fff; margin: 8px 0 4px; }
.ratio-bar span { display: grid; place-items: center; }
.ratio-bar .c { background: var(--ink-2); }
.ratio-bar .f { background: var(--ink-3); }

/* ---------- การประเมินครูประจำชั้น ---------- */
.level-select { border: 0; background: transparent; height: 35px; min-height: 0; width: 84px; padding: 0 4px; border-radius: 0; text-align: center; font-size: 14px; }
.level-select.v3 { color: var(--ok); font-weight: 600; }
.level-select.v0, .level-select.vมผ { color: var(--bad); font-weight: 600; }

@media (max-width: 860px) {
  .dash { grid-template-columns: 1fr; }
  .topnav { display: none; }
  .brand span { display: none; }
  .who { display: none; }
  .course-row { grid-template-columns: 64px minmax(0, 1fr); }
  .course-row .meter-cell, .course-row .go { grid-column: 2; }
  .item { grid-template-columns: 1fr 80px auto; }
  .item .code { grid-column: 1 / -1; }
  .page { padding: 18px 12px 48px; }
  .sheet .name { min-width: 120px; max-width: 130px; }
  .topbar { padding: 8px 12px; gap: 8px; }
  .brand strong { font-size: 14px; }
  .top-right select { max-width: 132px; }
}
@media (max-width: 480px) {
  .brand div { display: none; }
  .sheet-wrap { max-height: calc(100dvh - 240px); }
}
@media print { .topbar, .no-print { display: none !important; } }
