:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --grid: #e8e7e3;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a;
  --good: #0ca30c; --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
  --good-ink: #006300; --critical-ink: #b02a2a; --warn-ink: #8a5a00;
  --leave: #8a8984;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --grid: #2b2b28;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70;
    --good-ink: #4cc94c; --critical-ink: #ef7a7a; --warn-ink: #f2c14e; --leave: #9a998f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --grid: #2b2b28;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70;
  --good-ink: #4cc94c; --critical-ink: #ef7a7a; --warn-ink: #f2c14e; --leave: #9a998f;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 "IBM Plex Sans Thai", system-ui, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0 0 .5rem; font-weight: 600; line-height: 1.3; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1.05rem; }
.muted { color: var(--text-2); } .small { font-size: .85rem; }
.error { color: var(--critical-ink); }
.num { font-variant-numeric: tabular-nums; text-align: right; }

/* layout */
.shell { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 600; }
.brand-lg { font-size: 1.4rem; justify-content: center; margin-bottom: .5rem; }
.logo { width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  -webkit-mask: none; position: relative; flex: none; }
.logo::after { content: ""; position: absolute; inset: 7px 5px 9px; background: #fff;
  clip-path: polygon(50% 0, 100% 45%, 50% 90%, 0 45%); }
.tabs { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
.tabs a { padding: .4rem .8rem; border-radius: 8px; color: var(--text-2); white-space: nowrap; }
.tabs a.active { background: var(--surface-2); color: var(--text); font-weight: 500; }
.user { display: flex; align-items: center; gap: .6rem; }
.view { max-width: 1200px; margin: 0 auto; padding: 1.25rem 16px 3rem; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head p { margin: 0; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.cards { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr)); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.1rem; box-shadow: var(--shadow); min-width: 0; }
.card h2 { font-size: 1rem; }
.card .sub { color: var(--text-2); font-size: .85rem; margin: -.3rem 0 .75rem; }
.course-card { display: flex; flex-direction: column; gap: .4rem; color: inherit; transition: border-color .15s; }
.course-card:hover { border-color: var(--accent); }
.course-card .meta { color: var(--text-2); font-size: .85rem; }
.course-card .stats { display: flex; gap: 1rem; margin-top: .4rem; font-size: .85rem; color: var(--text-2); }
.course-card .stats b { color: var(--text); font-weight: 600; }

/* KPI tiles */
.kpis { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.kpi .label { color: var(--text-2); font-size: .85rem; }
.kpi .value { font-size: 1.75rem; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.kpi .hint { color: var(--muted); font-size: .78rem; }

.chart-box { position: relative; height: 280px; }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .85rem; color: var(--text-2); margin-bottom: .5rem; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .35rem; vertical-align: -1px; }

/* buttons & forms */
.btn { font: inherit; font-weight: 500; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  padding: .45rem .95rem; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: transparent; color: var(--critical-ink); border-color: var(--border); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: .45rem .6rem; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { width: 100%; min-height: 140px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--text-2); }
.form-grid { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); }

/* tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .5rem .7rem; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { background: var(--surface-2); font-weight: 500; color: var(--text-2); position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
td input[type=number] { width: 80px; text-align: right; }
td .name { white-space: normal; min-width: 160px; }
.empty { padding: 2rem; text-align: center; color: var(--text-2); }

/* sub-tabs in course page */
.subtabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; overflow-x: auto; }
.subtabs button { font: inherit; background: none; border: 0; border-bottom: 2px solid transparent; padding: .55rem .9rem;
  color: var(--text-2); cursor: pointer; white-space: nowrap; }
.subtabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }

/* attendance status pills */
.status-group { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.status-group label { cursor: pointer; }
.status-group input { position: absolute; opacity: 0; pointer-events: none; }
.status-group span { display: inline-flex; align-items: center; gap: .3rem; padding: .3rem .6rem; font-size: .82rem; color: var(--text-2); border-right: 1px solid var(--border); }
.status-group label:last-child span { border-right: 0; }
.status-group span::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
.status-group input:checked + span { background: var(--surface-2); color: var(--text); font-weight: 500; box-shadow: inset 0 -2px 0 var(--c); }
.status-group input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

.badge { display: inline-flex; align-items: center; gap: .3rem; font-size: .78rem; padding: .1rem .5rem; border-radius: 999px; border: 1px solid var(--border); color: var(--text-2); }
.badge.good { color: var(--good-ink); } .badge.bad { color: var(--critical-ink); } .badge.warn { color: var(--warn-ink); }
.badge.good::before, .badge.bad::before, .badge.warn::before { content: ""; width: 7px; height: 7px; border-radius: 50%; }
.badge.good::before { background: var(--good); } .badge.bad::before { background: var(--critical); } .badge.warn::before { background: var(--warning); }

.dropzone { border: 2px dashed var(--border); border-radius: var(--radius); padding: 1.5rem; text-align: center; color: var(--text-2); cursor: pointer; }
.dropzone.over { border-color: var(--accent); background: var(--surface-2); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 2rem; max-width: 400px; width: 100%;
  text-align: center; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 1rem; align-items: center; }

/* overlays */
.loading { position: fixed; inset: 0; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 60%, transparent); z-index: 50; }
.loading:not(.on) { display: none; }
.spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface);
  padding: .6rem 1rem; border-radius: 8px; z-index: 60; max-width: calc(100% - 32px); }
.toast.err { background: var(--critical); color: #fff; }
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 1.25rem; width: min(560px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.2rem; }

@media (max-width: 640px) {
  .topbar { gap: .5rem; }
  .tabs { order: 3; flex-basis: 100%; }
  .user #user-name { display: none; }
  .kpi .value { font-size: 1.45rem; }
}
