:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1c2129;
  --muted: #5d6673;
  --line: #e2e5ea;
  --accent: #1f5fbf;
  --accent-bg: #e7f0fc;
  --ok: #1e7a45;      --ok-bg: #e3f4e9;
  --late: #93600a;    --late-bg: #fcf0d6;
  --miss: #b42318;    --miss-bg: #fde8e6;
  --revise: #6b3fb5;  --revise-bg: #efe8fb;
  --wait: #7a828e;    --wait-bg: #f0f1f3;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418; --card: #1b1e24; --text: #e8eaee; --muted: #9aa3ae; --line: #2c3139;
    --accent: #7fb0ff; --accent-bg: #1c2b44;
    --ok: #6fd39a; --ok-bg: #173323; --late: #f2c36b; --late-bg: #3a2e14;
    --miss: #ff9089; --miss-bg: #401a18; --revise: #c3a6f5; --revise-bg: #2c2245;
    --wait: #8d96a2; --wait-bg: #23272e;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 600; }
h2 { font-size: 18px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 16px; margin: 0 0 8px; font-weight: 600; }
.container { max-width: 1100px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 720px; margin: 0 auto; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.pad { padding: 32px 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--card); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 16px; padding: 8px 16px; flex-wrap: wrap;
}
.brand { font-weight: 600; color: var(--text); text-decoration: none; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tabs a { padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; white-space: nowrap; font-size: 15px; }
.tabs a.on { background: var(--accent-bg); color: var(--accent); font-weight: 500; }
.who { display: flex; gap: 8px; align-items: center; font-size: 14px; color: var(--muted); }
@media (max-width: 640px) {
  .topbar { gap: 8px; }
  .tabs { order: 3; flex-basis: 100%; }
  .who { margin-left: auto; }
  .who span { display: none; }
}

/* controls */
.btn {
  font: inherit; font-size: 15px; border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 8px; padding: 8px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  text-decoration: none; min-height: 40px;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #0b1630; } }
.btn.danger { color: var(--miss); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: 14px; }
.btn.block { width: 100%; justify-content: center; }
.btn[disabled] { opacity: .6; cursor: progress; }
label.field { display: block; }
label.field > span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], textarea, select {
  font: inherit; font-size: 16px; width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text); min-height: 42px;
}
textarea { min-height: 120px; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.check { display: flex; gap: 8px; align-items: center; }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; }
.stat .n { font-size: 26px; font-weight: 600; }
.stat .l { font-size: 13px; color: var(--muted); }
.error { color: var(--miss); font-size: 14px; }

/* homework list */
.hw-list { display: grid; gap: 10px; }
.hw {
  display: block; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; color: inherit; text-decoration: none;
}
.hw:hover { border-color: var(--muted); }
.hw .subj { font-size: 13px; color: var(--muted); }
.hw .title { font-weight: 500; margin: 2px 0 6px; }
.section-title { display: flex; align-items: baseline; gap: 8px; margin: 20px 0 10px; }
.section-title .count { font-size: 14px; color: var(--muted); }

/* chips */
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.c-ok { background: var(--ok-bg); color: var(--ok); }
.c-late { background: var(--late-bg); color: var(--late); }
.c-miss { background: var(--miss-bg); color: var(--miss); }
.c-revise { background: var(--revise-bg); color: var(--revise); }
.c-wait { background: var(--wait-bg); color: var(--wait); }
.c-accent { background: var(--accent-bg); color: var(--accent); }

/* files */
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.file {
  position: relative; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--bg);
  aspect-ratio: 1; display: flex; flex-direction: column; color: inherit; text-decoration: none;
}
.file img { width: 100%; height: 100%; object-fit: cover; }
.file .doc { flex: 1; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 600; color: var(--muted); }
.file .name { font-size: 12px; padding: 4px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: var(--card); border-top: 1px solid var(--line); }
.file .x {
  position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border-radius: 50%; border: none;
  background: rgba(0,0,0,.6); color: #fff; font-size: 16px; cursor: pointer; line-height: 1;
}
.file.uploading { opacity: .6; }
.file .bar { position: absolute; left: 0; bottom: 0; height: 4px; background: var(--accent); }
.picker { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.picker input[type=file] { display: none; }

/* matrix */
.matrix-wrap { overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); max-height: 70vh; }
table.matrix { border-collapse: separate; border-spacing: 0; font-size: 13px; }
.matrix th, .matrix td { padding: 4px; text-align: center; border-bottom: 1px solid var(--line); background: var(--card); }
.matrix thead th { position: sticky; top: 0; z-index: 2; font-weight: 500; vertical-align: bottom; }
.matrix th.hwcol { min-width: 44px; max-width: 44px; height: 120px; }
.matrix th.hwcol div { writing-mode: vertical-rl; transform: rotate(180deg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-height: 112px; margin: 0 auto; color: var(--muted); }
.matrix .name { position: sticky; left: 0; z-index: 1; text-align: left; white-space: nowrap; padding: 4px 10px; }
.matrix thead .name { z-index: 3; }
.matrix tfoot td { position: sticky; bottom: 0; font-weight: 500; }
.cell { width: 30px; height: 30px; border-radius: 6px; border: none; cursor: pointer; font: inherit; font-size: 14px; font-weight: 600; }
.cell:hover { outline: 2px solid var(--accent); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 4px; }

/* table */
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
.list th, .list td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.list th { color: var(--muted); font-weight: 500; }
.table-wrap { overflow-x: auto; }

/* login */
.login { max-width: 400px; margin: 8vh auto; }
.login .card { padding: 24px; }
.divider { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin: 16px 0; }
.divider::before, .divider::after { content: ''; flex: 1; border-top: 1px solid var(--line); }

/* dialog */
dialog {
  border: none; border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
#modalBody { padding: 20px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 15px; z-index: 100; max-width: calc(100vw - 32px);
}
