:root {
  --bg-top: #edf7ff;
  --bg-bottom: #d6ebff;
  --surface: #ffffff;
  --surface-soft: #f6fbff;
  --text: #17324d;
  --muted: #6b8299;
  --border: #d7e8f6;
  --primary: #1f7ac0;
  --primary-dark: #155f9a;
  --green: #2e9e6b;
  --orange: #d9822b;
  --purple: #7b61c9;
  --red: #d9544d;
  --grey: #7890a5;
  --shadow: 0 10px 26px rgba(31, 122, 192, 0.10);
  --radius: 16px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", "Noto Sans Thai", "Sarabun", Tahoma, sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

button, input, select { font-family: inherit; }

/* ---------- ล็อกอิน ---------- */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(100%, 420px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 24px 60px rgba(31, 122, 192, 0.18);
  padding: 40px 34px;
  text-align: center;
}

.login-logo {
  width: 68px;
  height: 68px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  font-size: 34px;
  border-radius: 20px;
  background: linear-gradient(135deg, #dff1ff, #c7e5ff);
}

.login-card h1 { margin: 0 0 6px; font-size: 21px; }
.login-company { margin: 0 0 28px; color: var(--muted); font-size: 14px; }
.login-card .field-label { text-align: left; display: block; }

.pin-input {
  text-align: center;
  letter-spacing: 12px;
  font-size: 22px;
  font-weight: 700;
  padding-left: 18px;
}

.error-text { min-height: 20px; margin: 8px 0 4px; color: var(--red); font-size: 13px; }

/* ---------- โครงสร้างหลัก ---------- */
.app { max-width: 1440px; margin: 0 auto; padding: 20px 24px 40px; }

.topbar {
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  position: sticky;
  top: 12px;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-icon {
  width: 46px; height: 46px; flex: 0 0 auto;
  display: grid; place-items: center;
  font-size: 24px; border-radius: 14px;
  background: linear-gradient(135deg, #dff1ff, #c4e3ff);
}
.brand h1 { margin: 0; font-size: 18px; line-height: 1.25; }
.brand p { margin: 3px 0 0; font-size: 12px; color: var(--muted); }

.top-actions { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }
.date-picker { display: flex; flex-direction: column; gap: 5px; }
.date-picker span { font-size: 12px; color: var(--muted); font-weight: 600; }
.date-picker .input { min-width: 170px; padding: 9px 12px; }

.layout {
  margin-top: 20px;
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  gap: 22px;
  align-items: start;
}

.sidebar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: sticky;
  top: 108px;
}

.nav-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  text-align: left;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: 0.18s ease;
  white-space: nowrap;
}
.nav-btn:hover { background: var(--surface-soft); }
.nav-btn.active {
  background: linear-gradient(135deg, #e3f2ff, #d3eaff);
  color: var(--primary-dark);
  box-shadow: inset 3px 0 0 var(--primary);
}

.main { min-width: 0; }
.view { display: none; animation: fadeIn 0.25s ease; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  margin: 4px 2px 18px;
}
.section-head h2 { margin: 0; font-size: 21px; }
.section-head .muted { margin: 5px 0 0; }

.muted { color: var(--muted); font-size: 13px; }
.count-badge {
  background: #e6f3ff;
  color: var(--primary-dark);
  border: 1px solid #c9e4fa;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
}
.count-badge strong { font-size: 16px; }

/* ---------- ปุ่ม/ฟอร์ม ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: 0.18s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn.primary { background: var(--primary); color: #fff; box-shadow: 0 6px 14px rgba(31, 122, 192, 0.22); }
.btn.primary:hover { background: var(--primary-dark); transform: translateY(-1px); }
.btn.ghost { background: #fff; color: var(--primary-dark); border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-soft); }
.btn.danger { background: #fff0ef; color: var(--red); border-color: #f6cfcd; padding: 7px 11px; }
.btn.danger:hover { background: #ffe1df; }
.btn.small { padding: 7px 12px; font-size: 13px; }
.btn.block { width: 100%; margin-top: 18px; padding: 13px; }

.card, .panel, .list, .card-grid > .item-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card { padding: 20px; margin-bottom: 18px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; align-items: end; }
.form-row { display: flex; gap: 14px; align-items: end; }
.field { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.field.grow { flex: 1; }
.field-label { font-size: 13px; font-weight: 700; color: #34516b; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: 0.18s ease;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(31, 122, 192, 0.13); background: #fff; }
select.input { cursor: pointer; }

.note, .hint-box {
  font-size: 13px;
  color: var(--muted);
  margin: -6px 2px 16px;
}
.hint-box {
  color: #2c628f;
  background: #e9f5ff;
  border: 1px solid #cfe7fa;
  padding: 11px 14px;
  border-radius: 12px;
  margin: 0 0 16px;
}

/* ---------- การ์ดสถิติ ---------- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(158px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas: "icon label" "icon value";
  gap: 2px 12px;
  align-items: center;
  border-left: 4px solid var(--primary);
}
.stat-card.blue { border-left-color: var(--primary); }
.stat-card.green { border-left-color: var(--green); }
.stat-card.orange { border-left-color: var(--orange); }
.stat-card.purple { border-left-color: var(--purple); }
.stat-card.red { border-left-color: var(--red); }
.stat-card.grey { border-left-color: var(--grey); }
.stat-icon { grid-area: icon; font-size: 26px; }
.stat-label { grid-area: label; font-size: 12px; color: var(--muted); font-weight: 600; }
.stat-value { grid-area: value; font-size: 28px; line-height: 1.1; }
.stat-unit { display: none; }

.panel-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.panel { padding: 18px; }
.panel h3 { margin: 0 0 12px; font-size: 15px; }

.chip-list { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
}
.chip small { color: var(--muted); margin-left: 5px; }
.empty-inline { color: var(--muted); font-size: 13px; }

/* ---------- รายการ ---------- */
.list { overflow: hidden; }
.list-row {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid #edf4fa;
}
.list-row:last-child { border-bottom: 0; }
.list-row:hover { background: var(--surface-soft); }
.row-index {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: #e9f4fe;
  color: var(--primary-dark);
  font-size: 12px;
  font-weight: 700;
}
.row-main { min-width: 0; }
.row-main strong { display: block; font-size: 14px; overflow-wrap: anywhere; }
.row-main small { display: block; margin-top: 3px; font-size: 12px; color: var(--muted); }
.row-actions { display: flex; gap: 7px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.attend-row { grid-template-columns: minmax(0, 1fr) auto; }
.status-group { display: flex; gap: 6px; flex-wrap: wrap; }
.status-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--muted);
  border-radius: 9px;
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: 0.15s ease;
}
.status-btn:hover { border-color: var(--primary); color: var(--primary); }
.status-btn.present.active { background: #e4f7ee; border-color: #a8ddc5; color: #207a52; }
.status-btn.absent.active { background: #fff2e3; border-color: #f4cda1; color: #b86a1f; }
.status-btn.none.active { background: #eef2f6; border-color: #cfdae4; color: #60768a; }

.time-badge {
  background: #fff0ee;
  color: var(--red);
  border: 1px solid #f7cfca;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 700;
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.summary-strip > div {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.summary-strip span { font-size: 12px; color: var(--muted); }
.summary-strip strong { font-size: 24px; line-height: 1; }
.summary-strip small { font-size: 11px; color: var(--muted); }

.empty-state {
  padding: 44px 20px;
  text-align: center;
  color: var(--muted);
}
.empty-state .empty-icon { display: block; font-size: 34px; margin-bottom: 8px; }
.empty-state strong { display: block; color: var(--text); margin-bottom: 5px; }
.empty-state p { margin: 0 0 14px; font-size: 13px; }

/* ---------- สแปร์ ---------- */
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.item-card { padding: 18px; }
.item-card .card-head { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
.item-card .card-head strong { font-size: 15px; overflow-wrap: anywhere; }
.item-card .card-head small { display: block; color: var(--muted); margin-top: 3px; font-size: 12px; }
.thumb {
  width: 52px; height: 52px; flex: 0 0 auto;
  border-radius: 14px;
  object-fit: cover;
  background: #e9f4fe;
  border: 1px solid var(--border);
}
.thumb-placeholder {
  width: 52px; height: 52px; flex: 0 0 auto;
  border-radius: 14px;
  display: grid; place-items: center;
  font-size: 22px;
  background: #e9f4fe;
  border: 1px solid var(--border);
}
.photo-preview { margin-top: 10px; }
.photo-preview img {
  width: 84px; height: 84px;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--border);
}

/* ---------- อื่น ๆ ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: #17324d;
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.22);
  z-index: 100;
}
.toast.error { background: var(--red); }
.footer-note { text-align: center; color: var(--muted); font-size: 12px; margin-top: 30px; }

/* ---------- จอเล็ก / มือถือ ---------- */
@media (max-width: 860px) {
  .app { padding: 14px 14px 30px; }
  .topbar { position: static; flex-direction: column; align-items: stretch; padding: 14px; }
  .top-actions { justify-content: space-between; }
  .date-picker { flex: 1; }
  .date-picker .input { min-width: 0; width: 100%; }
  .layout { grid-template-columns: 1fr; gap: 16px; }
  .sidebar {
    position: static;
    flex-direction: row;
    overflow-x: auto;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
  }
  .nav-btn { flex: 0 0 auto; padding: 10px 12px; }
  .nav-btn.active { box-shadow: inset 0 -3px 0 var(--primary); }
  .section-head h2 { font-size: 19px; }
  .form-row { flex-direction: column; align-items: stretch; }
  .list-row { padding: 13px 14px; gap: 10px; }
  .attend-row { grid-template-columns: 1fr; }
  .status-group { width: 100%; }
  .status-btn { flex: 1; text-align: center; }
  .stat-card { padding: 13px; }
  .stat-value { font-size: 24px; }
}

@media (max-width: 480px) {
  .brand h1 { font-size: 16px; }
  .brand p { font-size: 11px; }
  .nav-btn { font-size: 13px; }
  .nav-btn span { display: none; }
  .nav-btn { justify-content: center; font-size: 18px; padding: 10px 14px; }
  .card { padding: 15px; }
  .row-actions { justify-content: flex-start; }
  .summary-strip { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- พิมพ์ ---------- */
@media print {
  body { background: #fff; }
  .no-print, .sidebar, .top-actions, .footer-note, .toast, #loginView { display: none !important; }
  .app { padding: 0; max-width: none; }
  .topbar { position: static; box-shadow: none; border: 0; padding: 0 0 12px; }
  .layout { display: block; margin-top: 0; }
  .card, .panel, .list, .stat-card, .summary-strip > div { box-shadow: none; break-inside: avoid; }
}
