@font-face {
  font-family: "Sarabun";
  src: local("Sarabun"), url("fonts/Sarabun-regular.ttf") format("truetype");
  font-weight: 400;
}
@font-face {
  font-family: "Sarabun";
  src: local("Sarabun Bold"), url("fonts/Sarabun-bold.ttf") format("truetype");
  font-weight: 700;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Sarabun", "Noto Sans Thai", sans-serif;
  background: #f4f6f8;
  color: #222;
  padding-bottom: 80px;
}

[hidden] { display: none !important; }

/* ---------- หัวเว็บ + แท็บ ---------- */
.app-header {
  position: relative;
  background: #1f3a5f;
  color: white;
  padding: 24px 20px;
}
.account-bar {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.admin-badge { padding: 2px 9px; border-radius: 999px; background: #f2c14e; color: #222; font-size: 0.85rem; }
.login-screen { max-width: 420px; margin: 12vh auto; padding: 28px; text-align: center; background: white; border-radius: 14px; box-shadow: 0 1px 8px rgba(0,0,0,.12); }
.login-screen h1 { color: #1f3a5f; }
.login-screen p { margin: 20px 0; }
.auth-form { display: flex; flex-direction: column; gap: 12px; text-align: left; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; }
.auth-form input { width: 100%; padding: 10px 12px; font: inherit; border: 1px solid #c5ccd3; border-radius: 8px; }
.auth-actions { display: flex; gap: 10px; }
.auth-actions .btn { flex: 1; }
.text-button { align-self: center; border: 0; background: none; color: #1f6fd1; font: inherit; cursor: pointer; }
.app-header h1 { margin: 0 0 4px; }
.app-header p { margin: 0; opacity: 0.85; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 12px 20px 0;
  border-bottom: 1px solid #d5dbe1;
  background: white;
}
.tab {
  font: inherit;
  font-size: 1rem;
  padding: 10px 18px;
  border: none;
  background: none;
  cursor: pointer;
  color: #556;
  border-bottom: 3px solid transparent;
}
.tab.active { color: #1f3a5f; font-weight: 700; border-bottom-color: #1f3a5f; }
.count {
  display: inline-block;
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: #e3e7eb;
  font-size: 0.8rem;
  text-align: center;
}
.count:empty { display: none; }

.catalog-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 18px 20px 0;
}
.catalog-toolbar h2 { margin: 0; color: #1f3a5f; }
.catalog-toolbar p { margin: 2px 0 0; color: #667; }
.catalog-wrap { padding: 0 20px 40px; overflow-x: auto; }
.catalog-table { width: 100%; border-collapse: collapse; background: white; border-radius: 10px; overflow: hidden; }
.catalog-table th, .catalog-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #e6eaee; }
.catalog-table th { background: #eef3f9; color: #445; white-space: nowrap; }
.catalog-table td:first-child { font-weight: 700; color: #1f3a5f; }
.catalog-actions { display: flex; gap: 6px; white-space: nowrap; }
.layer-title {
  grid-column: 1 / -1;
  margin: 8px 0 -4px;
  padding: 8px 12px;
  color: #1f3a5f;
  background: #e8eef6;
  border-left: 4px solid #1f6fd1;
  border-radius: 6px;
}
.layer-cards { display: contents; }

/* ---------- ตัวกรอง / ปุ่ม ---------- */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 16px 20px;
}
.filters input,
.filters select {
  padding: 10px 12px;
  font: inherit;
  border: 1px solid #c5ccd3;
  border-radius: 8px;
  background: white;
}
.filters input { flex: 1; min-width: 220px; }

.btn {
  font: inherit;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}
.btn.primary { background: #1f6fd1; color: white; }
.btn.primary:hover { background: #185bb0; }
.btn.ghost { background: white; border-color: #c5ccd3; color: #333; }
.btn.ghost:hover { background: #eef1f4; }
.btn.ghost.light { background: transparent; border-color: rgba(255,255,255,.5); color: white; }
.btn.danger { color: #a8341f; }
.btn.small { padding: 5px 10px; font-size: 0.85rem; }

/* ---------- การ์ดอุปกรณ์ ---------- */
#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 0 20px 40px;
}
.card {
  position: relative;
  display: block;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  border: 2px solid transparent;
  cursor: pointer;
}
.card.busy { cursor: not-allowed; }
.card.busy .photo { filter: grayscale(0.7); opacity: 0.7; }
.card.selected { border-color: #1f6fd1; box-shadow: 0 0 0 3px rgba(31,111,209,.2); }
.card .pick {
  position: absolute;
  top: 10px; left: 10px;
  width: 22px; height: 22px;
  accent-color: #1f6fd1;
  cursor: inherit;
}
.card .photo {
  width: 100%;
  height: 160px;
  object-fit: cover;
  background: #e3e7eb;
  display: block;
}
.card .body { padding: 12px 14px 14px; }
.card h3 { margin: 0 0 4px; font-size: 1.05rem; }
.card .meta { color: #667; font-size: 0.85rem; margin: 2px 0; }

.badge {
  display: inline-block;
  margin-top: 8px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
}
.badge.free { background: #d9f2e1; color: #17703a; }
.badge.busy { background: #fde2dd; color: #a8341f; }
.empty { grid-column: 1 / -1; text-align: center; color: #667; padding: 40px; }

/* ---------- แถบเลือกด้านล่าง ---------- */
.selection-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: #1f3a5f;
  color: white;
  box-shadow: 0 -2px 8px rgba(0,0,0,.2);
}

/* ---------- ฟอร์ม (dialog) ---------- */
dialog {
  width: min(680px, 94vw);
  max-height: 92vh;
  border: none;
  border-radius: 14px;
  padding: 0;
  font-family: inherit;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { padding: 22px; }
dialog h2 { margin: 0 0 14px; }
dialog h3 { margin: 18px 0 8px; font-size: 1rem; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: #445; }
.form-grid input {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid #c5ccd3;
  border-radius: 8px;
}
.form-grid textarea {
  width: 100%;
  resize: vertical;
  font: inherit;
  padding: 9px 10px;
  border: 1px solid #c5ccd3;
  border-radius: 8px;
}
.full-width { grid-column: 1 / -1; }
.field-hint { display: block; color: #667; font-size: 0.82rem; }
.image-preview-wrap { min-height: 0; }
.equipment-image-preview {
  width: 150px;
  height: 110px;
  object-fit: contain;
  border: 1px solid #d5dbe1;
  border-radius: 8px;
  background: #f4f6f8;
}
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
@media (max-width: 560px) {
  .account-bar { position: static; margin-top: 12px; flex-wrap: wrap; }
  .full-width { grid-column: auto; }
  .catalog-toolbar { align-items: flex-start; flex-direction: column; }
}

.notice {
  margin: 12px 0 0;
  padding: 10px 12px;
  background: #fff6db;
  border: 1px solid #f0d98a;
  border-radius: 8px;
  font-size: 0.9rem;
}

.item-list { display: flex; flex-direction: column; gap: 8px; }
.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  background: #f4f6f8;
  border-radius: 8px;
}
.item-row.has-quantity { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) auto; }
.item-row .nm { font-size: 0.95rem; }
.item-row .nm small { color: #667; display: block; }
.item-row input {
  font: inherit;
  padding: 7px 9px;
  border: 1px solid #c5ccd3;
  border-radius: 6px;
  min-width: 0;
}
.quantity-input { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.item-row .item-quantity { width: 76px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ---------- รายการยืม ---------- */
#loanList { padding: 0 20px 40px; display: flex; flex-direction: column; gap: 16px; }
.loan {
  background: white;
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.1);
  overflow: hidden;
}
.loan-head {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 14px 16px;
  background: #eef3f9;
}
.loan-head h3 { margin: 0 0 4px; }
.loan-head p { margin: 2px 0; font-size: 0.9rem; color: #445; }
.loan-head .actions { display: flex; gap: 8px; align-items: flex-start; flex-wrap: wrap; }

.loan table { width: 100%; border-collapse: collapse; }
.loan th, .loan td {
  padding: 9px 12px;
  text-align: left;
  border-top: 1px solid #e6eaee;
  font-size: 0.95rem;
}
.loan th { background: #fafbfc; color: #556; font-weight: 700; }
.loan td.c, .loan th.c { text-align: center; width: 60px; }
.loan .table-wrap { overflow-x: auto; }
.tag-done { color: #17703a; }
.tag-open { color: #a8341f; font-weight: 700; }

/* ---------- แจ้งเตือน ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: 90px;
  transform: translateX(-50%);
  z-index: 30;
  background: #222;
  color: white;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 2px 10px rgba(0,0,0,.3);
}

/* ---------- ใบเบิก (พิมพ์ / PDF) ---------- */
#printArea { display: none; }

.sheet { font-family: "Sarabun", sans-serif; font-size: 12pt; color: #000; }
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.sheet-head h1 { font-size: 18pt; margin: 0; }
.sheet-head img { height: 17mm; width: auto; }
.sheet-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 24px;
  margin-bottom: 6px;
}
.sheet-rule { border-top: 1px dashed #000; margin: 8px 0 16px; }
.sheet table { width: 100%; border-collapse: collapse; }
.sheet th, .sheet td { border: 1px solid #cfcfcf; padding: 6px 8px; vertical-align: top; }
.sheet th { text-align: center; font-weight: 400; }
.sheet td.c { text-align: center; }
.sheet .item-photo { display: block; width: 28mm; height: 20mm; object-fit: contain; margin: auto; }
.sheet thead { display: table-header-group; }
.sheet tr { break-inside: avoid; }
.sheet-sign {
  width: 78mm;
  margin: 34px 0 0 auto;
  break-inside: avoid;
}
.sheet-sign .t { text-align: right; margin-bottom: 8px; }
.sheet-sign .line { margin: 12px 0; display: flex; gap: 6px; }
.sheet-sign .dots { flex: 1; border-bottom: 1px dotted #000; }

@media print {
  @page { size: A4; margin: 18mm 20mm; }
  body { background: white; padding: 0; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; }
}
