/* ============================================
   ระบบสารบรรณ — Design Tokens
   ============================================ */
:root {
  /* Warm green / cream school palette */
  --bg: oklch(0.975 0.018 90);          /* paper cream */
  --bg-soft: oklch(0.96 0.022 88);
  --bg-deep: oklch(0.93 0.025 88);
  --surface: oklch(1 0 0);
  --surface-2: oklch(0.985 0.01 90);
  --ink: oklch(0.22 0.015 110);
  --ink-2: oklch(0.38 0.015 110);
  --ink-3: oklch(0.55 0.018 105);
  --muted: oklch(0.72 0.015 105);
  --line: oklch(0.88 0.018 95);
  --line-2: oklch(0.82 0.02 95);

  --green: oklch(0.45 0.085 150);       /* primary — school green */
  --green-2: oklch(0.55 0.09 148);
  --green-soft: oklch(0.93 0.04 145);
  --green-deep: oklch(0.32 0.07 152);

  --amber: oklch(0.72 0.14 70);         /* urgency */
  --amber-soft: oklch(0.94 0.05 80);
  --amber-deep: oklch(0.5 0.13 60);

  --red: oklch(0.55 0.18 25);
  --red-soft: oklch(0.94 0.04 25);

  --blue: oklch(0.5 0.1 245);
  --blue-soft: oklch(0.94 0.03 240);

  --purple: oklch(0.5 0.12 295);
  --purple-soft: oklch(0.94 0.03 295);

  --accent: var(--green);
  --accent-soft: var(--green-soft);
  --accent-deep: var(--green-deep);

  --r-xs: 4px;
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-xl: 20px;

  --shadow-sm: 0 1px 2px oklch(0.3 0.02 110 / 0.06);
  --shadow-md: 0 2px 6px oklch(0.3 0.02 110 / 0.08), 0 1px 2px oklch(0.3 0.02 110 / 0.04);
  --shadow-lg: 0 12px 28px oklch(0.3 0.02 110 / 0.12), 0 4px 8px oklch(0.3 0.02 110 / 0.05);
  --shadow-xl: 0 24px 48px oklch(0.3 0.02 110 / 0.18), 0 6px 12px oklch(0.3 0.02 110 / 0.06);

  --density-y: 14px;
  --density-x: 16px;
}

[data-accent="indigo"] {
  --accent: oklch(0.46 0.13 270);
  --accent-soft: oklch(0.94 0.04 268);
  --accent-deep: oklch(0.34 0.13 270);
}
[data-accent="burgundy"] {
  --accent: oklch(0.42 0.13 25);
  --accent-soft: oklch(0.94 0.04 25);
  --accent-deep: oklch(0.3 0.13 25);
}
[data-accent="teal"] {
  --accent: oklch(0.5 0.1 200);
  --accent-soft: oklch(0.94 0.04 200);
  --accent-deep: oklch(0.36 0.11 200);
}

[data-density="compact"] {
  --density-y: 8px;
  --density-x: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

#root { min-height: 100vh; }

button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }

::selection { background: var(--accent-soft); color: var(--accent-deep); }

/* ============================================
   App shell
   ============================================ */
.app {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  grid-template-rows: 56px 1fr;
  grid-template-areas:
    "topbar topbar"
    "sidebar main";
  min-height: 100vh;
  background: var(--bg);
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: 0.01em;
}

.brand-mark {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-deep));
  color: white;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  box-shadow: var(--shadow-sm);
}

.brand-text { display: flex; flex-direction: column; line-height: 1.05; }
.brand-text .t1 { font-size: 14px; font-weight: 700; }
.brand-text .t2 { font-size: 11px; color: var(--ink-3); font-weight: 500; }

.topbar__spacer { flex: 1; }

.topbar__search {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 6px 12px;
  width: 320px;
  color: var(--ink-3);
}
.topbar__search input {
  border: none;
  background: transparent;
  outline: none;
  flex: 1;
  font-size: 13px;
  color: var(--ink);
}
.topbar__search kbd {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--ink-3);
}

.icon-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  width: 36px; height: 36px;
  display: grid; place-items: center;
  color: var(--ink-2);
  position: relative;
  transition: background .12s, border-color .12s;
}
.icon-btn:hover { background: var(--bg-soft); border-color: var(--line); }
.icon-btn .dot {
  position: absolute;
  top: 7px; right: 7px;
  width: 8px; height: 8px;
  background: var(--amber-deep);
  border-radius: 50%;
  border: 2px solid var(--surface);
}

.role-switcher {
  display: flex;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 3px;
  gap: 2px;
}
.role-switcher button {
  border: none;
  background: transparent;
  padding: 5px 12px;
  font-size: 13px;
  color: var(--ink-2);
  border-radius: calc(var(--r-md) - 3px);
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}
.role-switcher button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.role-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--accent-deep);
  border: 1px solid color-mix(in oklch, var(--accent) 35%, transparent);
  border-radius: 999px;
  padding: 5px 13px;
  font-size: 13px;
  font-weight: 600;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 4px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.user-chip .avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: grid; place-items: center;
  font-size: 12px;
  font-weight: 700;
}
.user-chip .meta { line-height: 1.1; }
.user-chip .name { font-size: 12px; font-weight: 600; }
.user-chip .role { font-size: 10px; color: var(--ink-3); }

/* hamburger + ฉากหลังเมนู — โผล่เฉพาะมือถือ (ดู media query ด้านล่าง) */
.menu-btn { display: none; }
.nav-scrim { display: none; pointer-events: none; }

/* ============================================
   Sidebar
   ============================================ */
.sidebar {
  grid-area: sidebar;
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
}

.side-section { margin-top: 12px; }
.side-label {
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-3);
  padding: 8px 12px 4px;
  font-weight: 700;
}

.side-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--r-md);
  color: var(--ink-2);
  font-size: 13.5px;
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  font-weight: 500;
  transition: background .1s;
  white-space: nowrap;
}
.side-item .label-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item:hover { background: var(--bg-soft); }
.side-item[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent-deep);
  font-weight: 600;
}
.side-item[aria-current="page"] svg { color: var(--accent); }
.side-item .count {
  margin-left: auto;
  background: var(--bg-deep);
  color: var(--ink-2);
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  font-weight: 600;
}
.side-item[aria-current="page"] .count {
  background: var(--accent);
  color: white;
}
.side-item.urgent .count { background: var(--amber-deep); color: white; }

.sidebar__footer {
  margin-top: auto;
  padding: 12px;
  background: var(--accent-soft);
  border-radius: var(--r-md);
  border: 1px solid color-mix(in oklch, var(--accent) 20%, transparent);
}
.sidebar__footer .ft-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-deep);
  margin-bottom: 4px;
}
.sidebar__footer .ft-body {
  font-size: 11.5px;
  color: var(--ink-2);
  line-height: 1.45;
}

/* ============================================
   Main
   ============================================ */
.main {
  grid-area: main;
  overflow-y: auto;
  overflow-x: auto;
  min-width: 0;
}
.page {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px 28px 80px;
  min-width: 0;
}
.page__head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 20px;
}
.page__title h1 {
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.page__title p {
  margin: 0;
  color: var(--ink-3);
  font-size: 13.5px;
}

/* ============================================
   Cards, surfaces
   ============================================ */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.card__head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card__head h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.01em;
}
.card__body { padding: 16px 18px; }

/* ============================================
   Buttons
   ============================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--r-md);
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 600;
  transition: background .12s, border-color .12s, transform .06s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-soft); }
.btn:active { transform: translateY(1px); }
.btn--primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn--primary:hover { background: var(--accent-deep); border-color: var(--accent-deep); }
.btn--ghost { border-color: transparent; background: transparent; }
.btn--ghost:hover { background: var(--bg-soft); }
.btn--danger {
  background: var(--red);
  color: white;
  border-color: var(--red);
}
.btn--sm { padding: 5px 10px; font-size: 12.5px; gap: 4px; }
.btn--lg { padding: 10px 20px; font-size: 14.5px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ============================================
   Stat tiles
   ============================================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 16px 18px;
  position: relative;
  overflow: hidden;
}
.stat__label {
  font-size: 12px;
  color: var(--ink-3);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.stat__value {
  font-size: 30px;
  font-weight: 700;
  margin: 6px 0 0;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat__sub {
  font-size: 12px;
  color: var(--ink-3);
  margin-top: 4px;
}
.stat--accent { background: var(--accent-soft); border-color: color-mix(in oklch, var(--accent) 18%, transparent); }
.stat--accent .stat__value { color: var(--accent-deep); }
.stat--amber { background: var(--amber-soft); border-color: color-mix(in oklch, var(--amber-deep) 22%, transparent); }
.stat--amber .stat__value { color: var(--amber-deep); }

/* ============================================
   Inbox table
   ============================================ */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.toolbar .tabs {
  display: flex;
  gap: 2px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 3px;
}
.toolbar .tab {
  border: none;
  background: transparent;
  padding: 6px 12px;
  font-size: 13px;
  border-radius: calc(var(--r-md) - 3px);
  color: var(--ink-2);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}
.toolbar .tab[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}
.toolbar .tab .num {
  background: var(--bg-deep);
  font-size: 11px;
  padding: 0 6px;
  border-radius: 999px;
  font-weight: 600;
}
.toolbar .tab[aria-pressed="true"] .num { background: var(--accent-soft); color: var(--accent-deep); }
.toolbar .spacer { flex: 1; }
.toolbar .field {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 6px 10px;
  color: var(--ink-3);
}
.toolbar .field input, .toolbar .field select {
  border: none;
  background: transparent;
  outline: none;
  font-size: 13px;
  color: var(--ink);
  min-width: 140px;
}

.doctable-wrap {
  width: 100%;
  overflow-x: auto;
  border-radius: var(--r-lg);
}
.doctable {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13.5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  overflow: hidden;
  min-width: 640px;
}
@media (max-width: 1100px) {
  .doctable th.col-ref, .doctable td.col-ref { display: none; }
}
@media (max-width: 900px) {
  .doctable th.col-from, .doctable td.col-from,
  .doctable th.col-date, .doctable td.col-date { display: none; }
  .doctable { min-width: 0; }
}
.doctable thead th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
  padding: 10px var(--density-x);
  background: var(--bg-soft);
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  white-space: nowrap;
}
.doctable tbody td {
  padding: var(--density-y) var(--density-x);
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.doctable tbody tr:last-child td { border-bottom: none; }
.doctable tbody tr { cursor: pointer; transition: background .08s; }
.doctable tbody tr:hover { background: var(--bg-soft); }
.doctable .docnum {
  font-family: 'JetBrains Mono', 'Sarabun', monospace;
  font-weight: 700;
  font-size: 13px;
  color: var(--ink);
  font-feature-settings: "tnum";
}
.doctable .subject {
  font-weight: 600;
  color: var(--ink);
  max-width: 380px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.doctable .from { color: var(--ink-2); font-size: 12.5px; }
.doctable .date {
  white-space: nowrap;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
}
.doctable .urg {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
}
.doctable .urg.urgent { background: var(--amber-soft); color: var(--amber-deep); }
.doctable .urg.normal { background: var(--bg-deep); color: var(--ink-3); }

/* status badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor;
}
.badge.pending_director { background: var(--amber-soft); color: var(--amber-deep); }
.badge.director_signed { background: var(--purple-soft); color: oklch(0.38 0.13 295); }
.badge.assigned { background: var(--blue-soft); color: oklch(0.36 0.12 245); }
.badge.sent_to_teacher { background: var(--green-soft); color: var(--green-deep); }
.badge.archived { background: var(--bg-deep); color: var(--ink-3); }

/* card layout */
.doc-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}
.doc-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  transition: border-color .12s, transform .12s;
}
.doc-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.doc-card .top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.doc-card .docnum {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 13px;
}
.doc-card h4 {
  margin: 0;
  font-size: 14.5px;
  font-weight: 700;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.doc-card .from { font-size: 12.5px; color: var(--ink-2); }
.doc-card .foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
  color: var(--ink-3);
  margin-top: 4px;
  border-top: 1px dashed var(--line);
  padding-top: 8px;
}

/* ============================================
   Detail / drawer
   ============================================ */
.drawer-wrap {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  grid-template-columns: minmax(0, 1fr) min(960px, 92vw);
}
.drawer-wrap__scrim {
  background: oklch(0.2 0.02 110 / 0.4);
  backdrop-filter: blur(2px);
}
.drawer {
  background: var(--bg);
  border-left: 1px solid var(--line-2);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  animation: drawerIn .22s cubic-bezier(.2,.7,.3,1) both;
  min-width: 0;
}
@keyframes drawerIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.drawer__head {
  background: var(--surface);
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 12px;
}
.drawer__head .docnum {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  background: var(--accent-soft);
  color: var(--accent-deep);
  padding: 4px 10px;
  border-radius: var(--r-sm);
}
.drawer__head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drawer__body { flex: 1; overflow-y: auto; padding: 0; }
.drawer__tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 0 20px;
  gap: 4px;
}
.drawer__tabs button {
  background: transparent;
  border: none;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--ink-2);
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.drawer__tabs button[aria-selected="true"] {
  color: var(--accent-deep);
  border-bottom-color: var(--accent);
  font-weight: 700;
}

.drawer-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  min-height: 100%;
}
.drawer-grid .pdf-area {
  background: var(--bg-deep);
  padding: 24px;
  overflow-y: auto;
}
.drawer-grid .side-panel {
  background: var(--surface);
  border-left: 1px solid var(--line);
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* meta list */
.meta-list { display: flex; flex-direction: column; gap: 10px; }
.meta-row {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 10px;
  font-size: 13px;
  align-items: baseline;
}
.meta-row .k { color: var(--ink-3); font-weight: 500; }
.meta-row .v { color: var(--ink); font-weight: 600; }
.meta-row .v.mono { font-family: 'JetBrains Mono', monospace; font-size: 12.5px; }

/* ============================================
   PDF mockup
   ============================================ */
.pdf-doc {
  background: white;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  aspect-ratio: 1 / 1.414;
  box-shadow: var(--shadow-lg);
  padding: 56px 64px;
  border-radius: 4px;
  position: relative;
  font-size: 14px;
  color: oklch(0.18 0.01 100);
  border: 1px solid var(--line-2);
  overflow: hidden;
}
.pdf-doc__crest {
  text-align: center;
  margin-bottom: 16px;
}
.pdf-doc__crest .seal {
  width: 56px; height: 56px;
  border-radius: 50%;
  border: 2px solid oklch(0.18 0.01 100);
  display: inline-grid;
  place-items: center;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.05;
  text-align: center;
}
.pdf-doc__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 18px;
  font-size: 13px;
}
.pdf-doc__header .left, .pdf-doc__header .right { line-height: 1.5; }
.pdf-doc h1.pdf-subject {
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  margin: 0 0 18px;
}
.pdf-doc__greeting { margin-bottom: 12px; }
.pdf-doc__body { text-indent: 32px; margin-bottom: 12px; text-align: justify; line-height: 1.7; }
.pdf-doc__signoff {
  margin-top: 28px;
  text-align: right;
  line-height: 1.6;
}
.pdf-doc__stamp-area {
  position: absolute;
  top: 60px;
  right: 60px;
  width: 130px;
}
.pdf-doc__signature-area {
  position: absolute;
  bottom: 80px;
  right: 100px;
  width: 180px;
  height: 120px;
  display: grid;
  place-items: center;
}
.pdf-doc__signature-area img,
.pdf-doc__signature-area canvas {
  max-width: 100%;
  max-height: 90px;
}
.pdf-doc__sig-line {
  border-top: 1px solid oklch(0.18 0.01 100);
  width: 100%;
  margin-top: 90px;
  text-align: center;
  padding-top: 4px;
  font-size: 12px;
}
.pdf-doc__received-stamp {
  position: absolute;
  top: 36px;
  left: 32px;
  border: 2px solid var(--red);
  color: var(--red);
  padding: 6px 10px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 11px;
  line-height: 1.4;
  transform: rotate(-4deg);
  background: oklch(1 0 0 / 0.85);
}
.pdf-doc__received-stamp .label { font-size: 10px; opacity: 0.8; font-weight: 600; }
.pdf-doc__received-stamp .num { font-size: 16px; font-family: 'JetBrains Mono', monospace; }

.pdf-frame {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.pdf-frame .filename {
  font-weight: 600;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* annotation overlay for signature */
.sig-overlay {
  position: absolute;
  border: 2px dashed var(--accent);
  background: color-mix(in oklch, var(--accent) 5%, transparent);
  border-radius: 4px;
  cursor: crosshair;
}

/* ============================================
   Forms
   ============================================ */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px 16px;
}
.field-row { display: flex; flex-direction: column; gap: 5px; }
.field-row.col-2 { grid-column: span 2; }
.field-row label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
}
.field-row label .req { color: var(--red); }
.field-row .hint { font-size: 11.5px; color: var(--ink-3); }
.input, textarea.input, select.input {
  border: 1px solid var(--line-2);
  background: var(--surface);
  border-radius: var(--r-md);
  padding: 9px 12px;
  font-size: 14px;
  color: var(--ink);
  outline: none;
  transition: border-color .12s, box-shadow .12s;
  width: 100%;
  font-family: inherit;
}
.input:focus, textarea.input:focus, select.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in oklch, var(--accent) 18%, transparent);
}
textarea.input { resize: vertical; min-height: 80px; line-height: 1.5; }
.input.mono { font-family: 'JetBrains Mono', monospace; font-size: 13.5px; }

.urgency-toggle {
  display: flex;
  gap: 8px;
}
.urgency-toggle .opt {
  flex: 1;
  border: 1px solid var(--line-2);
  border-radius: var(--r-md);
  padding: 10px 14px;
  background: var(--surface);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-start;
  font-size: 13px;
  color: var(--ink-2);
  font-family: inherit;
}
.urgency-toggle .opt[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-deep);
  font-weight: 600;
}
.urgency-toggle .opt.amber[aria-pressed="true"] {
  border-color: var(--amber-deep);
  background: var(--amber-soft);
  color: var(--amber-deep);
}
.urgency-toggle .opt .t { font-size: 14px; font-weight: 700; }
.urgency-toggle .opt .s { font-size: 11.5px; color: var(--ink-3); }
.urgency-toggle .opt[aria-pressed="true"] .s { color: currentColor; opacity: 0.8; }

/* file dropzone */
.dropzone {
  border: 2px dashed var(--line-2);
  border-radius: var(--r-lg);
  padding: 32px;
  text-align: center;
  background: var(--bg-soft);
  color: var(--ink-3);
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.dropzone:hover, .dropzone.dragover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-deep); }
.dropzone__title { font-size: 14px; font-weight: 700; color: var(--ink); margin-top: 8px; }
.dropzone__hint { font-size: 12.5px; margin-top: 4px; }
.dropzone__icon { font-size: 28px; }
.uploaded-file {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 10px 14px;
}
.uploaded-file .ico {
  width: 36px; height: 44px;
  border-radius: 4px;
  background: var(--red-soft);
  color: var(--red);
  display: grid; place-items: center;
  font-weight: 700;
  font-size: 11px;
}
.uploaded-file .meta { flex: 1; line-height: 1.25; }
.uploaded-file .name { font-weight: 600; font-size: 13.5px; }
.uploaded-file .size { font-size: 11.5px; color: var(--ink-3); }

/* assignment teacher chips */
.teacher-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
}
.teacher-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
  cursor: pointer;
}
.teacher-chip:hover { border-color: var(--accent); }
.teacher-chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.teacher-chip .avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: grid; place-items: center;
  font-size: 12px;
  font-weight: 700;
}
.teacher-chip .info { flex: 1; line-height: 1.2; }
.teacher-chip .info .n { font-weight: 600; font-size: 13px; }
.teacher-chip .info .r { font-size: 11px; color: var(--ink-3); }
.teacher-chip .check {
  width: 18px; height: 18px;
  border-radius: 4px;
  border: 1.5px solid var(--line-2);
  display: grid; place-items: center;
}
.teacher-chip[aria-pressed="true"] .check {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* ============================================
   Signature canvas
   ============================================ */
.sig-pad {
  background: white;
  border: 1px solid var(--line-2);
  border-radius: var(--r-md);
  position: relative;
  overflow: hidden;
}
.sig-pad canvas {
  display: block;
  width: 100%;
  cursor: crosshair;
  touch-action: none;
}
.sig-pad__hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  color: var(--ink-3);
  font-size: 13px;
  font-style: italic;
}
.sig-pad__hint.hidden { display: none; }
.sig-pad__baseline {
  position: absolute;
  left: 12%;
  right: 12%;
  bottom: 28%;
  border-top: 1px dashed var(--line-2);
  pointer-events: none;
}

/* ============================================
   Timeline / audit
   ============================================ */
.timeline {
  position: relative;
  padding-left: 22px;
}
.timeline::before {
  content: '';
  position: absolute;
  left: 8px;
  top: 6px;
  bottom: 6px;
  width: 2px;
  background: var(--line);
  border-radius: 1px;
}
.tl-item { position: relative; padding: 0 0 16px; }
.tl-item:last-child { padding-bottom: 0; }
.tl-dot {
  position: absolute;
  left: -22px;
  top: 6px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 3px solid var(--surface);
  background: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.tl-dot.muted { background: var(--ink-3); box-shadow: 0 0 0 1px var(--ink-3); }
.tl-dot.amber { background: var(--amber-deep); box-shadow: 0 0 0 1px var(--amber-deep); }
.tl-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.tl-head .who { font-weight: 700; font-size: 13.5px; }
.tl-head .what { font-size: 13px; color: var(--ink-2); }
.tl-when { font-size: 11.5px; color: var(--ink-3); margin-top: 1px; }
.tl-note {
  margin-top: 6px;
  background: var(--bg-soft);
  border-left: 3px solid var(--accent);
  padding: 8px 12px;
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink-2);
}

/* notes */
.note-card {
  background: var(--amber-soft);
  border-left: 3px solid var(--amber-deep);
  border-radius: 0 var(--r-md) var(--r-md) 0;
  padding: 10px 14px;
  font-size: 13.5px;
  line-height: 1.55;
  color: oklch(0.32 0.07 60);
}
.note-card .label {
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--amber-deep);
  margin-bottom: 4px;
}
.note-card.director {
  background: var(--purple-soft);
  border-left-color: oklch(0.38 0.13 295);
  color: oklch(0.28 0.1 295);
}
.note-card.director .label { color: oklch(0.38 0.13 295); }

/* ============================================
   Notification popover + center
   ============================================ */
.popover {
  position: absolute;
  right: 20px;
  top: 56px;
  width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xl);
  z-index: 80;
  overflow: hidden;
  max-height: 480px;
  display: flex;
  flex-direction: column;
}
.popover__head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.popover__head h4 { margin: 0; font-size: 14px; }
.popover__list { overflow-y: auto; }
.notif-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 10px;
  cursor: pointer;
  position: relative;
}
.notif-item:hover { background: var(--bg-soft); }
.notif-item.unread { background: color-mix(in oklch, var(--accent) 5%, transparent); }
.notif-item .ico {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-deep);
  display: grid; place-items: center;
  flex: 0 0 32px;
}
.notif-item .body { flex: 1; min-width: 0; }
.notif-item .title { font-weight: 600; font-size: 13px; line-height: 1.3; }
.notif-item .desc { font-size: 12px; color: var(--ink-3); margin-top: 2px; line-height: 1.3; }
.notif-item .when { font-size: 11px; color: var(--ink-3); margin-top: 4px; }
.notif-item .unread-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  position: absolute;
  top: 16px; right: 14px;
}

/* telegram preview */
.tg-preview {
  background: oklch(0.22 0.02 250);
  border-radius: var(--r-md);
  padding: 14px;
  max-width: 420px;
}
.tg-msg {
  background: oklch(0.32 0.02 250);
  border-radius: 8px;
  padding: 10px 12px;
  color: white;
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 6px;
}
.tg-msg .head { font-weight: 700; color: oklch(0.85 0.1 145); display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.tg-msg .row { display: flex; gap: 6px; align-items: baseline; }
.tg-msg .row .k { opacity: 0.6; font-size: 12px; }
.tg-msg .row .v { font-size: 13px; }
.tg-msg .urg-tag {
  display: inline-block;
  background: oklch(0.55 0.15 30);
  color: white;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  margin-top: 6px;
}
.tg-buttons { display: flex; gap: 4px; margin-top: 8px; }
.tg-buttons .btn {
  flex: 1;
  background: oklch(0.42 0.02 250);
  border: none;
  color: oklch(0.75 0.1 220);
  font-weight: 600;
  font-size: 12.5px;
  padding: 6px;
  border-radius: 6px;
}

/* progress stepper */
.stepper {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 0;
  font-size: 11.5px;
  color: var(--ink-3);
}
.stepper .step {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  position: relative;
}
.stepper .step .num {
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--bg-deep);
  color: var(--ink-3);
  display: grid; place-items: center;
  font-size: 11px;
  font-weight: 700;
  z-index: 2;
}
.stepper .step.done .num { background: var(--accent); color: white; }
.stepper .step.current .num { background: var(--accent); color: white; box-shadow: 0 0 0 4px color-mix(in oklch, var(--accent) 25%, transparent); }
.stepper .step.current .label { color: var(--accent-deep); font-weight: 700; }
.stepper .connector {
  flex: 1;
  height: 2px;
  background: var(--bg-deep);
  margin-top: -16px;
  align-self: center;
}
.stepper .connector.done { background: var(--accent); }
.stepper .step .label { white-space: nowrap; font-size: 11.5px; }

/* ============================================
   utilities
   ============================================ */
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mt-6 { margin-top: 24px; }
.muted { color: var(--ink-3); }
.mono { font-family: 'JetBrains Mono', monospace; }
.tnum { font-variant-numeric: tabular-nums; }
.divider { height: 1px; background: var(--line); margin: 14px 0; }

/* toast */
.toast-wrap {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: oklch(0.22 0.02 110);
  color: white;
  padding: 12px 18px;
  border-radius: var(--r-md);
  font-size: 13.5px;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: toastIn .3s cubic-bezier(.2,.7,.3,1);
  pointer-events: auto;
  max-width: 480px;
}
.toast.success { background: var(--accent-deep); }
.toast.amber { background: var(--amber-deep); }
@keyframes toastIn {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ============================================
   Responsive (iPad portrait)
   ============================================ */
.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 16px;
}
.intake-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
}

@media (max-width: 1100px) {
  .dashboard-grid, .intake-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 900px) {
  /* เมนูซ้ายกลายเป็นแผงเลื่อนออก (hamburger) แสดงข้อความเต็ม */
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "topbar" "main"; }
  .menu-btn { display: grid; }
  .sidebar {
    position: fixed; top: 56px; left: 0; bottom: 0;
    width: 78vw; max-width: 290px;
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 60; overflow-y: auto;
    box-shadow: var(--shadow-xl);
  }
  .sidebar.is-open { transform: translateX(0); }
  .nav-scrim {
    display: block; position: fixed; inset: 56px 0 0 0;
    background: rgba(0, 0, 0, .4); z-index: 55;
    opacity: 0; pointer-events: none; transition: opacity .25s ease;
  }
  .nav-scrim.is-open { opacity: 1; pointer-events: auto; }
  .topbar__search { display: none; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .drawer-wrap { grid-template-columns: 0 1fr; }
  .drawer-grid { grid-template-columns: 1fr; }
  .drawer-grid .side-panel { border-left: none; border-top: 1px solid var(--line); }
  .form-grid { grid-template-columns: 1fr; }
  .field-row.col-2 { grid-column: span 1; }
  .page { padding: 16px; }
  .role-switcher button .role-text { display: none; }
}

/* ---- แท็บเล็ตเล็ก: ลดของในแถบบนเพื่อไม่ให้ล้น ---- */
@media (max-width: 760px) {
  .topbar { padding: 0 12px; gap: 10px; }
  .topbar__brand .t2 { display: none; }     /* ซ่อนชื่อโรงเรียน/ปี ใต้โลโก้ */
  .user-chip .meta { display: none; }        /* เหลือเฉพาะรูปวงกลม */
  .user-chip { padding: 2px 4px; }
}

/* ---- มือถือ ---- */
@media (max-width: 640px) {
  .page { padding: 12px; }
  .page__head { flex-direction: column; align-items: stretch; gap: 12px; }
  .page__head .btn--lg { width: 100%; justify-content: center; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .toolbar { flex-wrap: wrap; }
  .toolbar .field { flex: 1; min-width: 140px; }
  .role-switcher button { padding: 7px 9px; }
  /* หัวลิ้นชัก: ให้ชื่อเรื่องลงบรรทัดใหม่เต็มความกว้าง */
  .drawer__head { flex-wrap: wrap; gap: 8px; padding: 12px 14px; }
  .drawer__head h2 { order: 10; flex-basis: 100%; font-size: 15px; }
  .drawer { padding-bottom: env(safe-area-inset-bottom); }
  .login-screen { padding: 16px; }
  .login-card { padding: 24px 20px 20px; }
}

/* ---- มือถือเล็ก ---- */
@media (max-width: 460px) {
  .topbar { gap: 6px; padding: 0 8px; }
  .topbar__brand .brand-text { display: none; }   /* เหลือเฉพาะไอคอนโลโก้ */
  .stats-grid { grid-template-columns: 1fr; }
}

/* ============================================
   Login screen
   ============================================ */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}
.login-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(900px 500px at 15% -10%, var(--green-soft), transparent 60%),
    radial-gradient(800px 480px at 110% 110%, var(--amber-soft), transparent 55%),
    var(--bg);
  z-index: 0;
}
.login-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-xl);
  padding: 30px 30px 24px;
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}
.login-title { font-size: 19px; font-weight: 800; letter-spacing: -0.01em; }
.login-sub { font-size: 13px; color: var(--ink-3); font-weight: 500; }
.login-lead { margin: 0 0 18px; font-size: 13.5px; color: var(--ink-2); }
.login-form { display: flex; flex-direction: column; gap: 14px; }

.input-affix { position: relative; display: flex; align-items: center; }
.input-affix > svg { position: absolute; left: 12px; color: var(--ink-3); pointer-events: none; }
.input-affix .input { padding-left: 36px; }
.input-eye {
  position: absolute; right: 6px;
  border: none; background: transparent; color: var(--ink-3);
  padding: 6px; border-radius: var(--r-sm); display: grid; place-items: center;
}
.input-eye:hover { background: var(--bg-soft); color: var(--ink); }

.login-error {
  display: flex; align-items: center; gap: 7px;
  background: var(--red-soft); color: var(--red);
  border-radius: var(--r-md); padding: 9px 12px;
  font-size: 12.5px; font-weight: 600;
}

.login-demo { margin-top: 20px; border-top: 1px solid var(--line); padding-top: 16px; }
.login-demo__label { font-size: 11.5px; color: var(--ink-3); font-weight: 600; margin-bottom: 9px; }
.login-demo__chips { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.login-chip {
  display: flex; flex-direction: column; gap: 2px; text-align: left;
  border: 1px solid var(--line); background: var(--surface-2);
  border-radius: var(--r-md); padding: 8px 10px; transition: border-color .12s, background .12s;
}
.login-chip:hover { border-color: var(--accent); background: var(--accent-soft); }
.login-chip__role { font-size: 12.5px; font-weight: 700; color: var(--ink); }
.login-chip__cred { font-size: 11px; color: var(--ink-3); }
.login-foot { margin-top: 18px; text-align: center; font-size: 11px; color: var(--muted); }
.login-credit {
  position: absolute;
  z-index: 1;
  bottom: 22px;
  left: 0; right: 0;
  text-align: center;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-3);
  letter-spacing: 0.01em;
}

/* ============================================
   Admin backend — user management
   ============================================ */
.admin-avatar {
  width: 36px; height: 36px; flex: none;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-deep));
  color: white; display: grid; place-items: center;
  font-size: 13px; font-weight: 700;
}
.admin-table-wrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.admin-table thead th {
  text-align: left; font-size: 11.5px; font-weight: 700; color: var(--ink-3);
  text-transform: uppercase; letter-spacing: 0.03em;
  padding: 11px 18px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.admin-table tbody td { padding: 12px 18px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.admin-table tbody tr:last-child td { border-bottom: none; }
.admin-table tbody tr:hover { background: var(--bg-soft); }

.chip-toggle {
  border: 1px solid var(--line); background: var(--surface);
  color: var(--ink-3); border-radius: 999px;
  padding: 5px 13px; font-size: 12.5px; font-weight: 600;
  transition: background .12s, border-color .12s, color .12s;
}
.chip-toggle:hover { background: var(--bg-soft); }
.chip-toggle.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-deep); }
