:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-2: #fbfaf8;
  --line: #e6e1db;
  --line-strong: #cfc7be;
  --text: #1f1a16;
  --muted: #6b625a;
  --accent: #d9531e;          /* ส้มอิฐ */
  --accent-strong: #b5400f;
  --accent-soft: #fdf0e8;
  --accent-line: #f3c9b1;
  --ok: #1f7a4a;
  --ok-soft: #e7f4ec;
  --bad: #b42318;
  --bad-soft: #fdecea;
  --warn: #8a5a00;
  --warn-soft: #fff4d6;
  --focus: #1d4ed8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(31, 26, 22, .06), 0 4px 16px rgba(31, 26, 22, .05);
  --font: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.6 var(--font);
}
h1, h2, h3 { line-height: 1.3; margin: 0; }
h1 { font-size: 1.5rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
p { margin: 0; }
a { color: var(--accent-strong); }
button, input, textarea, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.mono, .code { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.skip { position: absolute; left: -999px; top: 8px; background: #fff; padding: 8px 12px; z-index: 100; }
.skip:focus { left: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 6px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line-strong); background: var(--surface); font-weight: 500;
  text-decoration: none; color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.danger { color: var(--bad); border-color: #f0b8b2; }
.btn.danger:hover { background: var(--bad-soft); }
.btn.sm { min-height: 30px; padding: 2px 10px; font-size: .88rem; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 8px;
  border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent-strong); }
.icon-btn.del:hover { background: var(--bad-soft); color: var(--bad); }
.icon-btn svg { width: 18px; height: 18px; }
.plus-btn {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  border: 1px solid var(--accent-line); background: var(--accent-soft); color: var(--accent-strong); cursor: pointer;
}
.plus-btn:hover { background: var(--accent); color: #fff; }

/* ---------- forms ---------- */
label.field { display: grid; gap: 4px; font-weight: 500; }
.field .hint { font-weight: 400; color: var(--muted); font-size: .85rem; }
.field .req { color: var(--bad); font-weight: 400; font-size: .85rem; }
input[type=text], input[type=email], input[type=search], input[type=date], input[type=time], textarea, select {
  width: 100%; border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 12px;
  background: #fff; min-height: 40px;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); outline: 2px solid var(--accent-soft); outline-offset: 0; }
textarea { resize: vertical; min-height: 96px; line-height: 1.55; }
.search-input { position: relative; }
.search-input svg { position: absolute; left: 12px; top: 50%; translate: 0 -50%; width: 18px; color: var(--muted); }
.search-input input { padding-left: 38px; min-height: 46px; font-size: 1rem; }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { appearance: none; width: 42px; height: 24px; border-radius: 99px; background: var(--line-strong); position: relative; cursor: pointer; margin: 0; flex: none; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: left .15s; }
.switch input:checked { background: var(--ok); }
.switch input:checked::after { left: 21px; }

/* ---------- layout ---------- */
.topbar {
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 0 16px; min-height: 56px; display: flex; align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 700; }
.brand .mark { width: 28px; height: 28px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; color: #fff; font-size: .8rem; }
.term-badge { font-family: var(--mono); font-size: .8rem; padding: 1px 8px; border: 1px solid var(--accent-line); background: var(--accent-soft); color: var(--accent-strong); border-radius: 6px; }
.topnav { display: flex; gap: 4px; margin-left: 8px; }
.topnav a { padding: 6px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 500; }
.topnav a:hover { background: var(--surface-2); color: var(--text); }
.topnav a[aria-current=page] { color: var(--accent-strong); background: var(--accent-soft); }
.topbar .spacer { flex: 1; }
.who { font-size: .85rem; color: var(--muted); max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count-dot { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 99px; background: var(--accent); color: #fff; font-size: .75rem; margin-left: 4px; }

main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; }
main.narrow { max-width: 820px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .meta { color: var(--muted); margin-top: 4px; }
.crumbs { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: .88rem; color: var(--muted); margin-bottom: 10px; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--accent-strong); text-decoration: underline; }
.crumbs .sep { opacity: .6; }
.crumbs [aria-current] { color: var(--text); font-weight: 500; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px; }
.section-title { display: flex; align-items: center; gap: 10px; margin: 26px 0 10px; }
.section-title:first-child { margin-top: 0; }
.stack { display: grid; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; }
.split-check { display: grid; grid-template-columns: minmax(280px, 380px) minmax(0, 1fr); gap: 20px; align-items: start; }
.split-fb { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; align-items: start; }
.split-view { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.sticky { position: sticky; top: 76px; }

/* ---------- list rows ---------- */
.list { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-top: 1px solid var(--line); min-height: 56px; }
.list-row:first-child { border-top: 0; }
.list-row:hover { background: var(--surface-2); }
.list-row .main { flex: 1; min-width: 0; }
.list-row .title { font-weight: 600; color: var(--text); text-decoration: none; display: block; overflow: hidden; text-overflow: ellipsis; }
a.title:hover { color: var(--accent-strong); text-decoration: underline; }
.list-row .sub { font-size: .85rem; color: var(--muted); }
.list-row .actions { display: flex; gap: 2px; align-items: center; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; }

/* ---------- chips / status ---------- */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line-strong); background: var(--surface); padding: 6px 16px; border-radius: 99px;
  cursor: pointer; font-weight: 500; text-decoration: none; color: var(--text);
}
.chip:hover { border-color: var(--accent); }
.chip[aria-current=true], .chip[aria-pressed=true] { background: var(--accent); border-color: var(--accent); color: #fff; }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: .85rem; font-weight: 500; padding: 2px 10px; border-radius: 99px; white-space: nowrap; }
.status svg { width: 14px; height: 14px; }
.status.ok { color: var(--ok); background: var(--ok-soft); }
.status.bad { color: var(--bad); background: var(--bad-soft); }
.status.warn { color: var(--warn); background: var(--warn-soft); }
.status.neutral { color: var(--muted); background: var(--surface-2); border: 1px solid var(--line); }
.pill { font-size: .78rem; padding: 1px 8px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.pill.accent { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent-strong); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); background: var(--surface); }
.login-hero {
  position: relative; background: linear-gradient(150deg, #e2602a 0%, var(--accent) 45%, #a83a0d 100%); color: #fff;
  clip-path: polygon(0 0, 100% 0, 84% 100%, 0 100%); padding: 48px 18% 40px 48px;
  display: flex; flex-direction: column; justify-content: space-between; overflow: hidden;
}
.login-hero::after {
  content: "∫ f′(x) dx   lim   Σ   dy/dx   ∂"; position: absolute; right: 12%; bottom: 22%;
  font: 600 3.2rem/1.1 var(--mono); color: rgba(255,255,255,.08); white-space: pre-wrap; width: 60%; text-align: right; pointer-events: none;
}
.login-hero h1 { font-size: clamp(2rem, 4vw, 3rem); line-height: 1.1; letter-spacing: .5px; }
.login-hero .tag { opacity: .9; margin-top: 12px; max-width: 30ch; }
.login-hero .term { font-family: var(--mono); border: 1.5px solid rgba(255,255,255,.75); padding: 4px 16px; border-radius: 6px; align-self: flex-start; font-size: 1.05rem; }
.login-side { display: flex; align-items: center; justify-content: center; padding: 40px 24px; }
.login-box { width: 100%; max-width: 360px; display: grid; gap: 16px; }
.login-box .label { font-weight: 600; font-size: 1.1rem; }
.gbtn-wrap { min-height: 44px; }
.dev-login { border-top: 1px dashed var(--line-strong); padding-top: 16px; display: grid; gap: 8px; }
.dev-login .quick { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---------- center card (confirm/link) ---------- */
.center-wrap { min-height: calc(100vh - 57px); display: grid; place-items: center; padding: 24px 16px; }
.center-card { width: 100%; max-width: 440px; padding: 28px; display: grid; gap: 16px; }
.id-card { border: 1px solid var(--accent-line); background: var(--accent-soft); border-radius: 10px; padding: 14px 16px; display: grid; gap: 2px; }
.id-card .name { font-size: 1.15rem; font-weight: 600; }

/* ---------- student ---------- */
.student-head { background: var(--surface); border-bottom: 1px solid var(--line); }
.student-head .inner { max-width: 1040px; margin: 0 auto; padding: 16px; display: flex; align-items: center; gap: 18px; }
#app.student .topbar-inner, #app.student main { max-width: 1040px; }
.sec-box { display: grid; text-align: center; border-right: 1px solid var(--line); padding-right: 18px; line-height: 1.1; }
.sec-box small { color: var(--muted); font-size: .78rem; }
.sec-box b { font-size: 2rem; color: var(--accent-strong); font-family: var(--mono); }
.review-note { margin-top: 6px; font-size: .85rem; padding: 6px 10px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--line); }
.review-note.bad { background: var(--bad-soft); border-color: #f0c7c2; }
.emoji-badge { font-size: 1.5rem; width: 40px; text-align: center; flex: none; }

/* ---------- feedback view ---------- */
.overall { display: flex; align-items: center; gap: 14px; padding: 14px 18px; }
.overall .big { font-size: 2.4rem; line-height: 1; }
.item-card { padding: 16px 18px; display: grid; gap: 10px; }
.item-head { display: flex; align-items: center; gap: 10px; justify-content: space-between; flex-wrap: wrap; }
.md { overflow-wrap: anywhere; }
.md > * + * { margin-top: .6em; }
.md p, .md ul, .md ol { margin: 0; }
.md ul, .md ol { padding-left: 1.4em; }
.md code { font-family: var(--mono); background: var(--surface-2); border: 1px solid var(--line); padding: 0 4px; border-radius: 4px; font-size: .9em; }
.md pre { background: var(--surface-2); border: 1px solid var(--line); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md pre code { border: 0; padding: 0; }
.md blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--accent-line); color: var(--muted); }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.md table { border-collapse: collapse; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; }
.pdf-frame { width: 100%; height: 70vh; min-height: 420px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.no-pdf { padding: 40px 16px; text-align: center; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: 8px; }

/* ---------- admin: assignment detail ---------- */
.action-row { display: flex; gap: 10px; flex-wrap: wrap; }
.action-row .btn { min-height: 44px; padding: 8px 22px; }
.stats { display: flex; gap: 10px; flex-wrap: wrap; }
.stat { padding: 10px 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); min-width: 120px; }
.stat b { display: block; font-size: 1.3rem; font-family: var(--mono); }
.stat span { font-size: .8rem; color: var(--muted); }
.sub-row { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-top: 1px solid var(--line); }
.sub-row:first-child { border-top: 0; }
.sub-row .t { flex: 1; min-width: 0; }
.sub-row input { min-height: 34px; padding: 4px 8px; }

/* ---------- check ---------- */
.suggest { list-style: none; margin: 10px 0 0; padding: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); max-height: 52vh; overflow: auto; }
.suggest li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-top: 1px solid var(--line); cursor: pointer; }
.suggest li:first-child { border-top: 0; }
.suggest li[aria-selected=true] { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.suggest .who-col { flex: 1; min-width: 0; }
.suggest .who-col .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kbd-help { margin-top: 12px; font-size: .85rem; color: var(--muted); display: grid; gap: 4px; }
kbd { font-family: var(--mono); font-size: .78rem; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; background: #fff; color: var(--text); }
.table-wrap { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); overflow: auto; max-height: calc(100vh - 230px); }
table.data { width: 100%; border-collapse: collapse; }
table.data th { position: sticky; top: 0; background: var(--surface-2); text-align: left; font-size: .82rem; color: var(--muted); font-weight: 600; padding: 9px 12px; border-bottom: 1px solid var(--line); z-index: 1; white-space: nowrap; }
table.data td { padding: 8px 12px; border-top: 1px solid var(--line); vertical-align: middle; }
table.data tr.selected td { background: var(--accent-soft); }
table.data tr.selected td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
table.data tr.flash td { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: #ffe1cc; } to { background: transparent; } }
td.note { color: var(--muted); font-size: .88rem; max-width: 260px; }
.filter-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }

/* ---------- feedback editor ---------- */
.fb-student { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding: 14px 18px; }
.fb-student .name { font-weight: 600; font-size: 1.1rem; }
.emoji-pick { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.emoji-pick label { cursor: pointer; }
.emoji-pick input { position: absolute; opacity: 0; pointer-events: none; }
.emoji-pick span {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 99px;
  border: 1px solid var(--line-strong); background: #fff; font-size: .85rem;
}
.emoji-pick span .e { font-size: 1.2rem; line-height: 1; }
.emoji-pick input:checked + span { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent); font-weight: 600; }
.emoji-pick input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.preview { border: 1px dashed var(--line-strong); border-radius: 8px; padding: 10px 12px; background: var(--surface-2); }
.preview-empty { color: var(--muted); font-size: .88rem; }
.legend { display: grid; gap: 12px; padding: 16px; }
.legend .li { display: grid; grid-template-columns: 30px 1fr; gap: 8px; align-items: start; }
.legend .e { font-size: 1.35rem; line-height: 1.3; }
.savebar { position: sticky; bottom: 0; background: rgba(255,255,255,.96); border-top: 1px solid var(--line); padding: 12px 16px; margin: 20px -16px 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; z-index: 5; }
.savebar .state { flex: 1; font-size: .85rem; color: var(--muted); min-width: 200px; }
.upload-state { font-size: .85rem; }
.dropdown { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 30; box-shadow: var(--shadow); margin: 0; }

/* ---------- dialog / toast ---------- */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--text); }
dialog::backdrop { background: rgba(31, 26, 22, .45); }
dialog form { padding: 22px; display: grid; gap: 14px; }
dialog .dlg-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.toasts { position: fixed; bottom: 16px; left: 50%; translate: -50% 0; display: grid; gap: 8px; z-index: 1000; width: min(440px, calc(100vw - 32px)); }
.toast { background: #2a221c; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: .92rem; }
.toast.error { background: var(--bad); }
.toast.ok { background: var(--ok); }
.alert { padding: 10px 14px; border-radius: 8px; border: 1px solid; font-size: .92rem; }
.alert.error { background: var(--bad-soft); border-color: #f0c7c2; color: #7a1a12; }
.alert.info { background: var(--accent-soft); border-color: var(--accent-line); }
.alert.warn { background: var(--warn-soft); border-color: #f1d98c; color: #5c3d00; }
.loading-page { display: flex; gap: 10px; align-items: center; justify-content: center; min-height: 60vh; color: var(--muted); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .split-check, .split-fb, .split-view, .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .sticky { position: static; }
  .table-wrap { max-height: none; }
  .legend-card { order: -1; }
}
@media (max-width: 720px) {
  .login { grid-template-columns: 1fr; }
  .login-hero { clip-path: polygon(0 0, 100% 0, 100% 86%, 0 100%); padding: 32px 20px 56px; min-height: 42vh; }
  .login-hero::after { display: none; }
  .topnav { position: fixed; bottom: 0; left: 0; right: 0; background: var(--surface); border-top: 1px solid var(--line); margin: 0; padding: 6px; justify-content: space-around; z-index: 40; }
  .topnav a { font-size: .85rem; padding: 6px 8px; }
  main { padding-bottom: 96px; }
  .who { display: none; }
  .topbar-inner { gap: 10px; }
  .brand { font-size: .92rem; white-space: nowrap; }
  .term-badge { display: none; }
  .list-row { flex-wrap: wrap; }
  .list-row .main { flex-basis: calc(100% - 60px); }
  table.data.stack-mobile thead { display: none; }
  table.data.stack-mobile tr { display: grid; grid-template-columns: 1fr auto; padding: 8px 12px; border-top: 1px solid var(--line); gap: 2px 10px; }
  table.data.stack-mobile td { border: 0; padding: 0; }
  table.data.stack-mobile td.note { grid-column: 1 / -1; max-width: none; }
  .savebar { margin: 20px -16px 0; bottom: 56px; }
  .student-head .inner { gap: 12px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.ms-btn { width: 100%; max-width: 340px; min-height: 42px; justify-content: flex-start; gap: 12px; padding-left: 12px; font-weight: 500; border-color: #dadce0; white-space: normal; text-align: left; }
