:root {
  --red: #D42027;
  --gold: #90732C;
  --gold-soft: #f3ecdc;
  --ink: #161616;
  --muted: #6b6b6b;
  --line: #e2ddd2;
  --bg: #faf8f4;
  --card: #ffffff;
  --off: #fbe3e3;       /* วันหยุด */
  --leave: #fdebd3;     /* ลา */
  --absent: #2b2b2b;    /* ขาด */
  --pending: #f6c343;
  --ok: #2e7d4f;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Sarabun", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: #111; color: #fff;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.mark { color: var(--gold); font-weight: 700; letter-spacing: .18em; font-size: 18px; }
.mark b { color: var(--red); }
.brand-sub { color: #cfc6b3; font-size: 13px; white-space: nowrap; }
.who { font-size: 13px; color: #cfc6b3; text-align: right; }
.who button { margin-left: 8px; }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.narrow { max-width: 420px; margin: 24px auto; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 0 0 8px; }
.muted { color: var(--muted); font-size: 14px; }
label { display: block; font-weight: 600; margin: 14px 0 6px; }
input[type=text], input[type=password], input[type=tel], textarea, select {
  width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 18px;
}
input:focus, textarea:focus, select:focus, button:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.gold { background: var(--gold); border-color: var(--gold); color: #fff; }
.btn.ghost { background: transparent; color: inherit; border-color: transparent; }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 14px; }
.btn.block { width: 100%; margin-top: 18px; }
.btn:disabled { opacity: .5; cursor: default; }
.topbar .btn { color: #fff; border-color: #444; background: transparent; }

.error { color: var(--red); margin-top: 10px; font-weight: 600; }
.menu { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); margin-top: 16px; }
.menu a { text-decoration: none; color: inherit; }
.menu .card { display: block; min-height: 96px; }
.menu .card:hover { border-color: var(--gold); }
.menu .count { display: inline-block; min-width: 24px; padding: 0 8px; border-radius: 12px; background: var(--red); color: #fff; font-size: 14px; text-align: center; margin-left: 6px; }

/* ---------- roster ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 8px 0 12px; }
.toolbar .spacer { flex: 1; }
.status-line { font-size: 14px; padding: 8px 12px; border-radius: 8px; background: var(--gold-soft); margin-bottom: 12px; }
.status-line.closed { background: #eee; }
.grid-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; -webkit-overflow-scrolling: touch; }
table.roster { border-collapse: separate; border-spacing: 0; font-size: 14px; }
.roster th, .roster td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); text-align: center; padding: 0; }
.roster thead th { background: #fff; position: sticky; top: 0; z-index: 2; font-weight: 600; }
.roster .name { position: sticky; left: 0; z-index: 3; background: #fff; text-align: left; padding: 6px 10px; min-width: 128px; max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.roster thead .name { z-index: 4; }
.roster .name small { display: block; color: var(--muted); font-size: 12px; }
.roster .dow { font-size: 12px; color: var(--muted); display: block; }
.roster .dnum { font-size: 15px; display: block; }
.roster th.day { min-width: 40px; padding: 4px 0; }
.roster th.weekend, .roster th.holiday { background: var(--red); color: #fff; }
.roster th.weekend .dow, .roster th.holiday .dow { color: #ffdcdc; }
.roster th.editable { box-shadow: inset 0 -3px 0 var(--gold); }
.roster tr.dept td, .roster tr.dept th.name { background: #f1ede4; font-weight: 600; font-size: 13px; padding: 4px 10px; }
.cell { width: 40px; height: 40px; border: 0; background: transparent; font-weight: 700; font-size: 14px; cursor: default; position: relative; color: var(--ink); }
.cell.edit { cursor: pointer; }
.cell.edit:hover { outline: 2px solid var(--gold); outline-offset: -2px; }
.cell.lock { opacity: .55; }
.cell.c-off { background: var(--off); color: var(--red); }
.cell.c-leave { background: var(--leave); color: #9a5a00; }
.cell.c-absence { background: var(--absent); color: #fff; }
.cell.s-pending { box-shadow: inset 0 0 0 3px var(--pending); }
.cell.s-pending::after { content: "⏳"; position: absolute; right: 1px; top: -2px; font-size: 10px; }
.cell.s-rejected { text-decoration: line-through; color: var(--muted); background: #eee; }
.roster .notes td { height: 32px; }
.note-btn { width: 40px; height: 32px; border: 0; background: transparent; cursor: pointer; font-size: 14px; }
.note-btn.has { color: var(--gold); font-weight: 700; }

.legend { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-top: 16px; }
.legend dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 14px; }
.legend dt { font-weight: 700; }
.legend dd { margin: 0; color: var(--muted); }

/* ---------- bottom sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden] { display: none; }
.sheet-body { background: #fff; width: 100%; max-width: 560px; border-radius: 16px 16px 0 0; padding: 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); max-height: 85vh; overflow-y: auto; }
.sheet-title { font-weight: 700; font-size: 17px; }
.code-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; margin: 10px 0 14px; }
.code-btn { min-height: 52px; border-radius: 8px; border: 1px solid var(--line); background: #fff; cursor: pointer; padding: 6px; text-align: center; }
.code-btn b { display: block; font-size: 17px; }
.code-btn span { display: block; font-size: 12px; color: var(--muted); }
.code-btn.c-off { background: var(--off); }
.code-btn.c-leave { background: var(--leave); }
.code-btn.c-absence { background: var(--absent); color: #fff; }
.code-btn.c-absence span { color: #ddd; }
.code-btn.current { outline: 3px solid var(--gold); }
.warn { background: #fff6dd; border: 1px solid var(--pending); border-radius: 8px; padding: 8px 10px; font-size: 14px; }

/* ---------- approvals ---------- */
.list { display: grid; gap: 10px; margin-top: 12px; }
.item { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.item .tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--gold-soft); color: var(--gold); font-weight: 700; margin-right: 6px; }
.item .actions { display: flex; gap: 8px; }
.btn.yes { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.no { background: #fff; border-color: var(--red); color: var(--red); }
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60; background: #111; color: #fff; padding: 10px 16px; border-radius: 8px; max-width: calc(100% - 32px); font-size: 15px; }
.toast.bad { background: var(--red); }

@media (max-width: 520px) {
  main { padding: 12px; }
  .brand-sub { display: none; }
  .roster .name { min-width: 104px; max-width: 112px; }
}

/* ---------- staff pages ---------- */
input[type=date], input[type=number], input[type=datetime-local], input[type=file] { width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 16px; }
.card + .card, .card + .stats, .stats + .card, .narrow + .narrow { margin-top: 12px; }
.menu .card.hero { background: #111; color: #fff; border-color: #111; }
.menu .card.hero .muted { color: #cfc6b3; }
.center { text-align: center; }
.big-time { font-size: 44px; font-weight: 700; letter-spacing: .04em; margin: 4px 0 12px; font-variant-numeric: tabular-nums; }
.clock-btn { width: 200px; height: 200px; border-radius: 50%; border: 0; font-size: 28px; font-weight: 700; color: #fff; cursor: pointer; margin: 8px auto 16px; display: block; box-shadow: 0 6px 20px rgba(0,0,0,.18); }
.clock-btn.in { background: var(--ok); }
.clock-btn.out { background: var(--red); }
.clock-btn:disabled { opacity: .7; font-size: 18px; }
.history { text-align: left; border-top: 1px solid var(--line); margin-top: 16px; padding-top: 10px; font-size: 15px; display: grid; gap: 4px; }
.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 8px; }
.wk { border: 1px solid var(--line); border-radius: 8px; text-align: center; padding: 6px 0; background: #fff; }
.wk .dow { display: block; font-size: 12px; color: var(--muted); }
.wk b { font-size: 16px; }
.wk.today { outline: 2px solid var(--gold); }
.wk.c-off { background: var(--off); color: var(--red); }
.wk.c-leave { background: var(--leave); }
.wk.c-absence { background: var(--absent); color: #fff; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.stat span { display: block; font-size: 13px; color: var(--muted); }
.stat b { display: block; font-size: 22px; }
.stat small { color: var(--muted); }
table.simple { width: 100%; border-collapse: collapse; font-size: 14px; }
table.simple th, table.simple td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; vertical-align: middle; }
table.simple tr.dept td { background: #f1ede4; font-weight: 600; }
table.staff input.ob { width: 80px; padding: 6px; font-size: 15px; }
.row { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); align-items: center; }
.bad { color: var(--red); font-weight: 700; }
table.staff { min-width: 640px; }
table.staff td:first-child { min-width: 180px; }
