:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --sun: #dc2626;
  --sat: #2563eb;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 8px 24px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  padding: 0;
}
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Noto Sans Thai', 'Sarabun', -apple-system, BlinkMacSystemFont, 'Segoe UI',
    Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1,
h2,
h3,
h4 {
  margin: 0;
}
p {
  margin: 0;
}

.page {
  min-height: 100vh;
}
.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 16px 64px;
}

/* top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}
.brand .logo {
  font-size: 28px;
}
.brand h1 {
  font-size: 20px;
  font-weight: 700;
}
.sub {
  color: var(--muted);
  font-size: 13px;
}

/* buttons */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover {
  background: #f8fafc;
}
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover {
  background: var(--primary-dark);
}
.btn.primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.btn.danger {
  color: var(--danger);
  border-color: #fecaca;
  background: #fef2f2;
}
.btn.danger:hover {
  background: #fee2e2;
}
.btn.sm {
  padding: 5px 10px;
  font-size: 13px;
}
.btn.block {
  width: 100%;
  margin-top: 4px;
}
.icon-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 18px;
  color: var(--muted);
  line-height: 1;
}

/* calendar */
.calendar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cal-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.cal-title {
  font-size: 18px;
  font-weight: 700;
}
.cal-nav {
  display: flex;
  gap: 8px;
}
.cal-grid {
  display: grid;
  /* minmax(0, 1fr) บังคับให้ทุกคอลัมน์กว้างเท่ากัน ไม่โดนเนื้อหายาว ๆ ดันให้บานออก
     ทำให้หัววัน (อา.–ส.) ตรงกับช่องวันที่เสมอ */
  grid-template-columns: repeat(7, minmax(0, 1fr));
}
.cal-dow {
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}
.dow {
  text-align: center;
  padding: 10px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.dow.sun {
  color: var(--sun);
}
.dow.sat {
  color: var(--sat);
}
.cell {
  min-height: 104px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.cell:nth-child(7n) {
  border-right: none;
}
.cell.empty {
  background: #fafbfc;
}
.cell.today {
  background: #eff6ff;
}
.cell-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 24px;
}
.daynum {
  font-size: 13px;
  font-weight: 600;
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.cell.today .daynum {
  background: var(--primary);
  color: #fff !important;
  border-radius: 50%;
}
.daynum.sun {
  color: var(--sun);
}
.daynum.sat {
  color: var(--sat);
}
.pencil {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  opacity: 0.55;
  padding: 2px;
  border-radius: 6px;
  line-height: 1;
}
.pencil:hover {
  opacity: 1;
  background: #e2e8f0;
}
.chips {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.chip {
  text-align: left;
  border: none;
  border-left: 3px solid var(--primary);
  background: #f1f5f9;
  padding: 3px 6px;
  border-radius: 5px;
  font-size: 12px;
  line-height: 1.35;
  cursor: pointer;
  /* ให้ชื่อคอร์สขึ้นบรรทัดใหม่ได้ และตัดคำได้ทุกตำแหน่ง (ภาษาไทยไม่มีช่องว่าง) */
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  font-family: inherit;
  color: var(--text);
}
.chip:hover {
  background: #e2e8f0;
}
.cal-loading {
  padding: 10px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}
.hint {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin-top: 16px;
}

/* calendar — มุมมองลิสต์แนวตั้งสำหรับมือถือ (แถวละวัน)
   ซ่อนไว้บนเดสก์ท็อป แล้วเปิดแทนกริดในจอแคบ (ดู @media ด้านล่าง) */
.cal-list {
  display: none;
  flex-direction: column;
}
.day-row {
  display: flex;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.day-row:last-child {
  border-bottom: none;
}
.day-row.today {
  background: #eff6ff;
}
/* วันที่ไม่มีคอร์ส — บีบให้เตี้ยลงเพื่อเลื่อนผ่านได้เร็ว */
.day-row.is-empty {
  padding-top: 8px;
  padding-bottom: 8px;
}
.day-row-date {
  flex: none;
  width: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.dr-dow {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.dr-num {
  font-size: 19px;
  font-weight: 700;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.day-row-date.sun .dr-dow,
.day-row-date.sun .dr-num {
  color: var(--sun);
}
.day-row-date.sat .dr-dow,
.day-row-date.sat .dr-num {
  color: var(--sat);
}
.day-row.today .dr-num {
  background: var(--primary);
  color: #fff !important;
  border-radius: 50%;
}
.day-row-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
}
/* ป้ายคอร์สในลิสต์ — เต็มความกว้าง แตะง่ายกว่าในกริด */
.day-row-body .chip {
  padding: 8px 10px;
  font-size: 14px;
  border-radius: 8px;
  border-left-width: 4px;
}
.dr-empty {
  color: #cbd5e1;
  font-size: 15px;
}
.dr-edit {
  flex: none;
  align-self: center;
  font-size: 16px;
}

/* modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow: auto;
}
.modal.modal-lg {
  max-width: 560px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 {
  font-size: 17px;
}
.modal-body {
  padding: 20px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

/* form fields */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}
.field > span {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.field input,
.field textarea,
select {
  font-family: inherit;
  font-size: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
}
.field input:focus,
.field textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.field input[type='color'] {
  padding: 4px;
  height: 42px;
}

/* course detail */
.course-color {
  height: 6px;
  border-radius: 99px;
  margin-bottom: 14px;
}
.course-desc {
  white-space: pre-wrap;
  line-height: 1.6;
  word-break: break-word;
}
.course-desc a {
  color: var(--primary);
  text-decoration: underline;
}
.course-desc a:hover {
  color: var(--primary-dark);
}

/* day editor */
.day-editor h4 {
  font-size: 14px;
  margin-bottom: 8px;
}
.day-editor ul {
  list-style: none;
  padding: 0;
  margin: 0 0 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.day-editor li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.grow {
  flex: 1;
  min-width: 0;
}
.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex: none;
}
.add-row {
  display: flex;
  gap: 10px;
}
.add-row select {
  flex: 1;
}
.day-editor-divider {
  border-top: 1px dashed var(--border);
  margin: 18px 0;
}
.tag-adhoc {
  margin-left: 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  background: #f1f5f9;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  white-space: nowrap;
}
.muted {
  color: var(--muted);
  font-size: 13px;
}

/* course manager — compact bar */
.course-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 20px;
  margin-bottom: 24px;
}
.course-bar-info {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}
.course-bar-info h2 {
  font-size: 17px;
}
.course-bar-actions {
  display: flex;
  gap: 8px;
  flex: none;
}

/* course manager — popup contents */
.cm-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.cm-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cm-list-scroll {
  max-height: 55vh;
  overflow-y: auto;
}
.cm-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.cm-name {
  font-weight: 600;
  font-size: 14px;
}
.cm-desc {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* login */
.center-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.center-screen[hidden] {
  display: none;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
  max-width: 380px;
}
.login-card h1 {
  font-size: 22px;
  margin-bottom: 4px;
}
.login-card .sub {
  margin-bottom: 20px;
}
.error {
  background: #fef2f2;
  color: var(--danger);
  border: 1px solid #fecaca;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 14px;
}

/* responsive */
@media (max-width: 640px) {
  /* บนมือถือ: ซ่อนกริดรายเดือน แล้วแสดงลิสต์แนวตั้งแถวละวันแทน */
  .cal-month {
    display: none;
  }
  .cal-list {
    display: flex;
  }
  .cell {
    min-height: 78px;
  }
  .chip {
    font-size: 11px;
  }
  .brand h1 {
    font-size: 17px;
  }
  .topbar {
    padding: 12px 16px;
  }
}
