/* ===== พื้นฐาน ===== */
:root {
  --bg: #f3f5fa;
  --card: #ffffff;
  --line: #e5e8f0;
  --line-strong: #cbd2e1;
  --ink: #1f2937;
  --muted: #6b7280;
  --brand: #2557d6;

  /* สีเวร */
  --m-bg: #fff1c2; --m-ink: #8a4b00;   /* เช้า */
  --a-bg: #d8e8ff; --a-ink: #173f9e;   /* บ่าย */
  --n-bg: #33307a; --n-ink: #ffffff;   /* ดึก */
  --hol-bg: #fff3f4; --hol-ink: #c0263b; /* วันหยุด */
  --ok: #157347; --bad: #c0263b;
}

html { font-size: 14px; position: relative; min-height: 100%; }
@media (min-width: 768px) { html { font-size: 15px; } }
body {
  margin-bottom: 40px;
  background: var(--bg);
  color: var(--ink);
  font-family: "Sarabun", "Leelawadee UI", "Noto Sans Thai", Tahoma, system-ui, sans-serif;
}
.navbar.bg-primary { background: linear-gradient(90deg, #1d3f9e, #2f6bf0) !important; box-shadow: 0 2px 8px rgba(29, 63, 158, .25); }
.navbar .nav-link { border-radius: 6px; }
.navbar-brand.brand { display: inline-flex; align-items: center; gap: .45rem; }
.brand-name { font-weight: 400; letter-spacing: .01em; font-size: 1.25rem; }
.brand-name b { font-weight: 800; }
.brand-sub { font-size: .72rem; opacity: .8; border-left: 1px solid rgba(255,255,255,.4); padding-left: .5rem; margin-left: .1rem; }
@media (max-width: 576px) { .brand-sub { display: none; } }
.navbar .nav-link:hover { background: rgba(255, 255, 255, .12); }
h1, .h3 { font-weight: 700; letter-spacing: -.01em; }
.card { border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 1px 3px rgba(16, 24, 40, .06); }
.card-header { background: #f8f9fc; border-bottom: 1px solid var(--line); font-weight: 600; border-radius: 12px 12px 0 0 !important; }

/* ===== กรอบตาราง ===== */
.table-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(16, 24, 40, .06);
  overflow: auto; /* จอแคบเลื่อนซ้าย-ขวาได้ */
  margin-bottom: 1.5rem;
}
.table-card > .table { margin: 0; }
.table-card .table > thead th {
  background: #f6f7fb;
  color: var(--muted);
  font-size: .8rem;
  font-weight: 600;
  letter-spacing: .02em;
  border-bottom: 1px solid var(--line-strong);
  padding-top: .6rem; padding-bottom: .6rem;
  white-space: nowrap;
}
.table-card .table > tbody td { border-color: var(--line); padding-top: .5rem; padding-bottom: .5rem; }
.table-card .table > tbody tr:last-child td { border-bottom: 0; }
.table-card .table-hover > tbody tr:hover > * { background: #f5f8ff; }
.table-card .row-off td { color: #9aa1ad; background: #fafbfc; }

/* ป้ายเวร (chip) ใช้ทุกที่ */
.chip {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 1.9em; height: 1.75em; padding: 0 .45em;
  border-radius: 6px; font-weight: 700; font-size: .85em; line-height: 1;
}
.chip.s-1 { background: var(--m-bg); color: var(--m-ink); }
.chip.s-2 { background: var(--a-bg); color: var(--a-ink); }
.chip.s-3 { background: var(--n-bg); color: var(--n-ink); }
.chip.s-4 { background: linear-gradient(135deg, var(--m-bg) 50%, var(--a-bg) 50%); color: #3b2f6b; }
.chip.va { background: #d5f5e3; color: #0f6b3e; border: 1px dashed #5fbf8a; }
.name-chip.va { background: #e9f9f0; border-color: #a6dfbf; color: #0f6b3e; }
.name-chip {
  display: inline-block; padding: .1em .55em; margin: .1em .15em .1em 0;
  border-radius: 999px; background: #f1f3f8; border: 1px solid var(--line); font-size: .82rem; white-space: nowrap;
}
.tag { display: inline-block; font-size: .7rem; padding: 0 .4em; border-radius: 4px; background: #eef0f5; color: var(--muted); margin-left: .2em; }

.legend { display: flex; flex-wrap: wrap; gap: .4rem .9rem; align-items: center; color: var(--muted); }
.legend .item { display: inline-flex; align-items: center; gap: .35rem; }
.legend .swatch { width: 14px; height: 14px; border-radius: 4px; display: inline-block; border: 1px solid rgba(0, 0, 0, .06); }
.legend .swatch.hol { background: var(--hol-bg); border-color: #f3c4cb; }
.legend .swatch.bad { background: #fff; border: 2px solid var(--bad); }

/* ===== ตารางเวรรวม (พยาบาล x วัน) ===== */
.sched-wrap {
  overflow: auto; /* จอแคบ: เลื่อนซ้าย-ขวา */
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 1px 3px rgba(16, 24, 40, .06);
}
table.sched { border-collapse: separate; border-spacing: 0; font-size: .85rem; width: max-content; min-width: 100%; }
table.sched th, table.sched td {
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  padding: 3px; text-align: center; white-space: nowrap; height: 34px; min-width: 34px;
}
table.sched thead th {
  position: sticky; top: 0; z-index: 2; background: #f6f7fb; color: var(--ink);
  font-weight: 600; line-height: 1.1; padding: 6px 3px; border-bottom: 1px solid var(--line-strong);
}
table.sched thead th small { display: block; font-weight: 400; color: var(--muted); font-size: .72rem; }
table.sched thead th.hol, table.sched thead th.hol small { color: var(--hol-ink); }
table.sched thead th.hol { background: #fdecef; }
table.sched thead th.today .d { background: var(--brand); color: #fff; border-radius: 999px; padding: 1px 6px; }
table.sched .name {
  position: sticky; left: 0; z-index: 1; background: var(--card); text-align: left;
  min-width: 150px; padding: 4px 10px; box-shadow: 2px 0 4px -2px rgba(16, 24, 40, .12);
}
table.sched thead th.name { z-index: 3; background: #f6f7fb; }
table.sched .name a { text-decoration: none; font-weight: 600; }
table.sched .name a:hover { color: var(--brand) !important; text-decoration: underline; }
table.sched td.hol { background: var(--hol-bg); }
table.sched td.va-day { background: #f0fbf5; }
table.sched tbody tr:hover td { background-color: #f5f8ff; }
table.sched tbody tr:hover td.name { background: #eef3ff; }
table.sched .wk { border-left: 2px solid var(--line-strong); }
table.sched td.today { box-shadow: inset 0 0 0 9999px rgba(37, 87, 214, .05); }
table.sched td.bad { box-shadow: inset 0 0 0 2px var(--bad); }
table.sched .tot { background: #f8f9fc; font-weight: 600; color: #374151; min-width: 38px; }
table.sched thead th.tot { background: #eef0f6; }
table.sched .tot.sum { color: var(--brand); }
table.sched tfoot th, table.sched tfoot td { background: #fbfbfd; font-size: .78rem; height: 28px; }
table.sched tfoot tr:first-child > * { border-top: 2px solid var(--line-strong); }
table.sched tfoot td.ok { color: var(--ok); }
table.sched tfoot td.short { background: #fde3e6; color: var(--bad); font-weight: 700; }
table.sched tfoot td.over { background: #fff4d6; color: #8a5a00; font-weight: 600; }

/* โหมดแก้ไข: ทั้งช่องเป็นสีตามเวร */
table.sched td.edit { padding: 0; }
table.sched td.edit.s-1 { background: var(--m-bg); }
table.sched td.edit.s-2 { background: var(--a-bg); }
table.sched td.edit.s-3 { background: var(--n-bg); }
table.sched td.edit.s-3 select { color: #fff; }
table.sched td.edit.s-4 { background: linear-gradient(135deg, var(--m-bg) 50%, var(--a-bg) 50%); }
table.sched select {
  border: 0; background: transparent; width: 100%; height: 32px; min-width: 38px;
  font-size: .85rem; font-weight: 700; text-align: center; cursor: pointer; appearance: none; padding: 0;
}
table.sched select:focus { outline: 2px solid var(--brand); outline-offset: -2px; }
table.sched select option { color: var(--ink); background: #fff; }

/* ===== ปฏิทิน (วันหยุด / เวรรายคน) ===== */
table.cal { width: 100%; table-layout: fixed; border-collapse: separate; border-spacing: 6px; }
table.cal th { text-align: center; color: var(--muted); font-weight: 600; font-size: .8rem; padding: 4px 0; }
table.cal th.we { color: var(--hol-ink); }
table.cal td {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  vertical-align: top; padding: 6px 7px; height: 78px; position: relative;
}
table.cal td.empty { background: transparent; border: 0; }
table.cal td.we { background: #fdf8f9; }
table.cal td.hol { background: var(--hol-bg); border-color: #f5c9d0; }
table.cal td.today { border: 2px solid var(--brand); }
table.cal td.va-day { background: #f0fbf5; border: 1px dashed #5fbf8a; }
table.cal .dn { font-weight: 700; font-size: .85rem; color: #4b5563; }
table.cal td.we .dn, table.cal td.hol .dn { color: var(--hol-ink); }
table.cal .hn { font-size: .7rem; color: var(--hol-ink); line-height: 1.15; margin-top: 2px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
table.cal .shift { display: flex; justify-content: center; margin-top: 4px; }
table.cal .shift .chip { font-size: .9rem; height: 2em; padding: 0 .7em; }
table.cal td.s-1 { border-color: #f4d77a; }
table.cal td.s-2 { border-color: #a9c8f7; }
table.cal td.s-3 { border-color: #6d69b8; }

/* การ์ดสรุปรายคน */
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: .55rem .9rem; min-width: 92px; }
.stat .v { font-size: 1.4rem; font-weight: 700; line-height: 1.1; }
.stat .l { font-size: .78rem; color: var(--muted); }
.stat.s-1 { border-left: 4px solid #f0b400; }
.stat.s-2 { border-left: 4px solid #3b7bea; }
.stat.s-3 { border-left: 4px solid var(--n-bg); }
.stat.s-4 { border-left: 4px solid #8b7bd8; }
.stat.va { border-left: 4px solid #2fa36b; }

/* ตารางรายวัน */
.daily td.date { white-space: nowrap; font-weight: 600; }
.daily td.date small { display: block; font-weight: 400; color: var(--hol-ink); }
.daily tr.hol td { background: var(--hol-bg); }
.daily th .chip { margin-right: .35em; }

/* ===== ขนาดช่องตาราง: ยืดตามความกว้างหน้าจอ ===== */
/* ทุกขนาดคำนวณจากความกว้างจอ (vw) และมีขั้นต่ำ/สูงสุด ตารางเต็มความกว้างพอดีจอ
   จอแคบกว่า ~900px จะเลื่อนซ้าย-ขวาได้ (ช่องเล็กกว่านี้อ่านไม่ออก) */
/* ตารางสูงเต็มตามจำนวนพยาบาล (ไม่จำกัดความสูง ไม่มี scroll ซ้อน)
   จอ >= 768px ตารางพอดีความกว้างแล้ว ใช้ overflow: clip เพื่อให้หัวตาราง (วันที่) ตรึงกับหน้าจอขณะเลื่อนหน้าลง */
@media (min-width: 768px) {
  .sched-wrap { overflow: clip; }
}
table.sched {
  width: 100%; min-width: 880px; table-layout: fixed;
  font-size: clamp(.68rem, .78vw, .95rem);
}
table.sched th, table.sched td {
  min-width: 0; height: clamp(30px, 2.7vw, 46px); padding: clamp(1px, .2vw, 4px);
  overflow: hidden;
}
table.sched thead th { padding: clamp(4px, .45vw, 8px) 1px; font-size: clamp(.72rem, .85vw, 1rem); }
table.sched thead th small { font-size: clamp(.6rem, .65vw, .78rem); margin-top: 1px; }
table.sched thead th.today .d { padding: 1px clamp(3px, .4vw, 8px); }
table.sched .name {
  width: clamp(92px, 10.5vw, 190px); min-width: 0;
  font-size: clamp(.75rem, .85vw, 1rem); padding: 4px clamp(6px, .7vw, 12px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
table.sched .tot { width: clamp(28px, 2.5vw, 46px); min-width: 0; font-size: clamp(.68rem, .78vw, .95rem); }
table.sched tbody .chip {
  min-width: 0; width: min(100%, 2.4em); height: clamp(20px, 2vw, 34px);
  font-size: clamp(.64rem, .8vw, 1rem); padding: 0; border-radius: clamp(4px, .45vw, 8px);
}
table.sched tfoot th, table.sched tfoot td { height: clamp(24px, 2.1vw, 36px); font-size: clamp(.62rem, .72vw, .9rem); }
table.sched tfoot th.name .chip { width: auto; height: 1.6em; padding: 0 .35em; }
table.sched select { height: clamp(28px, 2.6vw, 44px); min-width: 0; font-size: clamp(.68rem, .8vw, 1rem); }
table.sched .tag { font-size: .62rem; }
.legend .chip { font-size: .9rem; }

/* จอแคบ: ซ่อนยอดแยก ช/บ/ด/ควบ เหลือ VA รวม หยุด */
@media (max-width: 1280px) {
  table.sched .dtl { display: none; }
  table.sched .va-col { border-left: 2px solid var(--line-strong); }
  table.sched { min-width: 690px; }
}
/* มือถือ: ลดระยะขอบหน้า */
@media (max-width: 576px) {
  .container-fluid.px-4 { padding-left: .6rem !important; padding-right: .6rem !important; }
  table.sched .name { width: 84px; }
}

/* ปฏิทิน */
table.cal { border-spacing: clamp(2px, .45vw, 6px); }
table.cal th { font-size: clamp(.62rem, .8vw, .85rem); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.cal td { height: clamp(58px, 7vw, 96px); padding: clamp(3px, .6vw, 9px); border-radius: clamp(6px, .8vw, 10px); overflow: hidden; }
table.cal .dn { font-size: clamp(.72rem, .9vw, 1rem); }
table.cal .hn { font-size: clamp(.58rem, .7vw, .78rem); }
table.cal .shift { margin-top: clamp(2px, .6vw, 8px); }
table.cal .shift .chip {
  font-size: clamp(.62rem, 1vw, 1.05rem); height: clamp(1.7em, 2.4vw, 2.2em);
  padding: 0 clamp(.2em, .6vw, .9em); border-radius: 8px; max-width: 100%; white-space: nowrap;
}
@media (max-width: 576px) {
  table.cal .hn { display: none; }
}

/* ตารางรายวัน / รายการ */
.daily td, .daily th { font-size: clamp(.78rem, .85vw, .95rem); }
.name-chip { font-size: clamp(.72rem, .8vw, .9rem); padding: .15em .6em; }
.stat { min-width: 0; flex: 1 1 90px; }
@media (max-width: 768px) {
  .daily th:first-child, .daily td.date { width: 7.5em !important; white-space: normal; }
}

@media print {
  body { background: #fff; }
  header, footer, .no-print { display: none !important; }
  .container, .container-fluid { max-width: none !important; }
  .sched-wrap { max-height: none; overflow: visible; box-shadow: none; }
  table.sched .name, table.sched thead th { position: static; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
