:root {
  --bg:        #f4f6f8;
  --surface:   #ffffff;
  --line:      #e3e8ef;
  --text:      #1f2937;
  --muted:     #6b7280;
  --primary:   #1c5fb0;
  --primary-d: #164b8c;
  --up:        #2f9e44;
  --down:      #e03131;
  --radius:    12px;
  --shadow:    0 1px 3px rgba(16,24,40,.08), 0 8px 24px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* ★ ห้ามลบ ★ กัน display ของคลาสอื่นมาทับคำสั่งซ่อน */
html, body { margin: 0; padding: 0; }
body {
  font-family: 'IBM Plex Sans Thai', -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font-family: inherit; font-size: 15px; }

/* ===== แถบหัว ===== */
.topbar {
  background: linear-gradient(135deg, #12386b, #1c5fb0);
  color: #fff;
  padding: 14px 18px 0;
}
.brand { display: flex; gap: 12px; align-items: center; max-width: 1180px; margin: 0 auto; }
.logo { font-size: 30px; line-height: 1; }
.topbar h1 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: .2px; }
.topbar p  { margin: 2px 0 0; font-size: 13px; opacity: .82; }
.tabs { display: flex; gap: 6px; max-width: 1180px; margin: 12px auto 0; }
.tab {
  background: transparent; color: rgba(255,255,255,.78); border: 0;
  border-bottom: 3px solid transparent; padding: 9px 14px;
  cursor: pointer; font-weight: 600; border-radius: 8px 8px 0 0;
}
.tab:hover { color: #fff; background: rgba(255,255,255,.08); }
.tab.is-on { color: #fff; border-bottom-color: #ffd43b; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 16px 14px 60px; }

/* ===== แถบควบคุม ===== */
.controls {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; margin-bottom: 14px; box-shadow: var(--shadow);
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
}
.monthnav { display: flex; align-items: center; gap: 6px; }
.nav {
  width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff; cursor: pointer; font-size: 20px; line-height: 1; color: var(--text);
}
.nav:hover { background: #eef2f7; }
.monthlabel { min-width: 170px; text-align: center; font-weight: 700; font-size: 17px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filters input[type=search], .filters select {
  border: 1px solid var(--line); border-radius: 9px; padding: 7px 10px; background: #fff; color: var(--text);
}
.filters input[type=search] { min-width: 190px; }

.btn-primary {
  background: var(--primary); color: #fff; border: 0; border-radius: 9px;
  padding: 8px 15px; font-weight: 600; cursor: pointer;
}
.btn-primary:hover { background: var(--primary-d); }
.btn-ghost {
  background: #fff; border: 1px solid var(--line); border-radius: 9px;
  padding: 7px 13px; cursor: pointer; color: var(--text);
}
.btn-ghost:hover { background: #eef2f7; }
.btn-danger {
  background: #fff5f5; color: var(--down); border: 1px solid #ffc9c9;
  border-radius: 9px; padding: 7px 13px; cursor: pointer;
}
.btn-danger:hover { background: #ffe3e3; }

/* ===== สรุปหัวเดือน ===== */
.summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.sumchip {
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 5px 13px; font-size: 13.5px; font-weight: 600; color: var(--muted);
}
.sumchip b { color: var(--text); font-size: 15px; }
.sumchip.up   b { color: var(--up); }
.sumchip.down b { color: var(--down); }

/* ===== ปฏิทิน ===== */
.calendar {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
  display: grid; grid-template-columns: repeat(7, 1fr);
}
.dow {
  text-align: center; padding: 9px 0; font-size: 13px; font-weight: 700;
  color: var(--muted); background: #f8fafc; border-bottom: 1px solid var(--line);
}
.grid { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(7, 1fr); }
.cell {
  min-height: 104px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  padding: 5px 6px 7px; cursor: pointer; position: relative; background: #fff;
  display: flex; flex-direction: column; gap: 3px;
}
.cell:nth-child(7n) { border-right: 0; }
.cell:hover { background: #f5f9ff; }
.cell.out { background: #fafbfc; }
.cell.out .daynum { color: #c3cad3; }
.cell.weekend .daynum { color: #b0487a; }
.cell.today { background: #fffbea; }
.cell.today .daynum {
  background: var(--primary); color: #fff; border-radius: 50%;
  width: 24px; height: 24px; display: grid; place-items: center;
}
.daynum { font-size: 13.5px; font-weight: 700; color: var(--text); }
.chip {
  display: flex; align-items: center; gap: 4px; font-size: 11.5px; line-height: 1.3;
  padding: 2px 5px; border-radius: 6px; background: #f1f3f5;
  border-left: 3px solid var(--muted); overflow: hidden;
}
.chip .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip .ar { font-weight: 700; flex: none; }
.more { font-size: 11px; color: var(--muted); font-weight: 600; }

.legend { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.legend span { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

/* ===== รายการ ===== */
.listwrap { display: flex; flex-direction: column; gap: 9px; }
.lgroup { font-weight: 700; color: var(--muted); font-size: 13px; margin: 8px 2px 0; }
.empty {
  background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius);
  padding: 34px 18px; text-align: center; color: var(--muted);
}

/* ===== การ์ดบันทึก ===== */
.note {
  background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--muted);
  border-radius: 10px; padding: 11px 13px; box-shadow: 0 1px 2px rgba(16,24,40,.04);
}
.note-top { display: flex; align-items: flex-start; gap: 8px; flex-wrap: wrap; }
.note-title { font-weight: 700; flex: 1 1 200px; }
.tag {
  font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: #f1f3f5; color: var(--muted); white-space: nowrap;
}
.tag.impact-up   { background: #ebfbee; color: var(--up); }
.tag.impact-down { background: #fff5f5; color: var(--down); }
.note-meta { font-size: 12.5px; color: var(--muted); margin-top: 3px; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.note-detail { margin-top: 7px; white-space: pre-wrap; font-size: 14px; }
.note-acts { display: flex; gap: 8px; margin-top: 9px; }
.note-acts button {
  background: none; border: 0; color: var(--primary); cursor: pointer;
  font-size: 13px; font-weight: 600; padding: 2px 4px; border-radius: 6px;
}
.note-acts button:hover { background: #eef4fc; }
.note-acts .del { color: var(--down); }
.note-acts .del:hover { background: #fff0f0; }

/* ===== แผงลอย ===== */
.overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 50;
  padding: 0; backdrop-filter: blur(2px);
}
/* ★ สำคัญ ★ ถ้าไม่มีบรรทัดนี้ display:flex ข้างบนจะไปทับคำสั่งซ่อนของเบราว์เซอร์
   ทำให้แผงลอยเปิดค้างตลอดเวลา กดปิดไม่ได้ */
.overlay[hidden] { display: none; }
.panel {
  background: var(--bg); width: 100%; max-width: 620px; max-height: 92vh;
  border-radius: 16px 16px 0 0; display: flex; flex-direction: column; overflow: hidden;
}
.panel-head {
  background: var(--surface); border-bottom: 1px solid var(--line);
  padding: 13px 16px; display: flex; align-items: center; justify-content: space-between;
}
.panel-head h2 { margin: 0; font-size: 16.5px; }
.close { background: none; border: 0; font-size: 19px; cursor: pointer; color: var(--muted); }
.panel-body { padding: 14px 16px 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }

/* ===== ฟอร์ม ===== */
.form label, .form .field { display: flex; flex-direction: column; gap: 4px; font-size: 13.5px; font-weight: 600; color: var(--muted); }
.form .lbl { font-size: 13.5px; font-weight: 600; }
.form input, .form select, .form textarea {
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px;
  background: #fff; color: var(--text); font-weight: 400; width: 100%;
}
.form textarea { resize: vertical; }
.form .row { display: flex; gap: 10px; }
.form .row > label { flex: 1; }
.form small { font-weight: 400; }
.req { color: var(--down); }
.impactpick { display: flex; gap: 8px; }
.pick { flex: 1; }
.pick input { display: none; }
.pick span {
  display: block; text-align: center; padding: 9px 4px; border-radius: 9px;
  border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 600; font-size: 13.5px;
}
.pick.up    input:checked + span { background: #ebfbee; border-color: var(--up);   color: var(--up); }
.pick.down  input:checked + span { background: #fff5f5; border-color: var(--down); color: var(--down); }
.pick.none  input:checked + span { background: #f1f3f5; border-color: var(--muted); color: var(--muted); }
.formbar { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.formbar .spacer { flex: 1; }
.formerr {
  background: #fff5f5; border: 1px solid #ffc9c9; color: var(--down);
  font-size: 14px; font-weight: 600; margin: 0; padding: 10px 12px; border-radius: 9px;
}
.form input.bad, .form select.bad, .form textarea.bad {
  border-color: var(--down); background: #fff8f8;
  box-shadow: 0 0 0 3px rgba(224,49,49,.12);
}

.addhere {
  background: var(--surface); border: 1px dashed var(--primary); color: var(--primary);
  border-radius: 10px; padding: 11px; font-weight: 600; cursor: pointer; text-align: center;
}
.addhere:hover { background: #eef4fc; }

/* ===== แจ้งเตือน ===== */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 90; box-shadow: var(--shadow);
}

/* ===== จอเล็ก ===== */
@media (max-width: 720px) {
  .topbar p { display: none; }
  .controls { gap: 9px; }
  .monthnav { width: 100%; justify-content: space-between; }
  .filters { width: 100%; }
  .filters input[type=search] { flex: 1 1 100%; min-width: 0; }
  .filters select { flex: 1; }
  .filters .btn-primary { flex: 1 1 100%; }
  .cell { min-height: 68px; padding: 3px 3px 5px; }
  .chip { font-size: 0; padding: 0; height: 6px; border-radius: 3px; border-left: 0; }
  .chip .ar, .chip .t { display: none; }
  .more { font-size: 10px; }
  .monthlabel { font-size: 16px; min-width: 0; flex: 1; }
}
@media (min-width: 721px) {
  .overlay { align-items: center; }
  .panel { border-radius: 16px; }
}


/* ===== ตัวเลือกสาขา (ติ๊กได้หลายสาขา) ===== */
.picker {
  width: 100%; text-align: left; background: #fff; color: var(--text);
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  cursor: pointer; font-weight: 400;
}
.picker:hover { background: #f8fafc; }
.picker.has-pick { border-color: var(--primary); color: var(--primary); font-weight: 600; }
.picker .caret { color: var(--muted); flex: none; }

.picker-list {
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
  padding: 9px; margin-top: 7px;
}
.picker-list input[type=search] {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-weight: 400;
}
.opts {
  max-height: 230px; overflow-y: auto; margin-top: 8px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 3px;
}
.form label.opt, .opt {
  /* ต้องระบุ row ทับกฎ .form label ที่สั่งให้เรียงลงล่าง ไม่งั้นช่องติ๊กจะไปอยู่เหนือชื่อสาขา */
  display: flex; flex-direction: row; align-items: center; gap: 8px; padding: 7px 8px;
  border-radius: 8px; cursor: pointer; font-weight: 400; font-size: 14px; color: var(--text);
}
.opt:hover { background: #f1f5fb; }
.opt input { width: 18px; height: 18px; accent-color: var(--primary); flex: none; margin: 0; }
.opt span { display: flex; align-items: baseline; gap: 5px; }
.opt b { font-weight: 700; }
.opt i { font-style: normal; color: var(--muted); font-size: 12px; }
.opt.all { grid-column: 1 / -1; background: #f8fafc; border: 1px dashed var(--line); }
.opt input:checked ~ span b { color: var(--primary); }
.picker-foot { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; }
