* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f0f2f5;
  --card: #ffffff;
  --border: #e4e6eb;
  --text: #1c1e21;
  --muted: #65676b;
  --primary: #1877f2;
  --primary-dark: #145dbf;
  --green: #31a24c;
  --red: #e41e3f;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans Thai', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ---------- Navbar ---------- */
.navbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 24px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.navbar .logo {
  font-weight: 700;
  font-size: 18px;
  color: var(--primary);
  display: flex;
  align-items: center;
  gap: 8px;
}
.navbar a.nav-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 15px;
  padding: 6px 12px;
  border-radius: 8px;
}
.navbar a.nav-link:hover { background: var(--bg); }
.navbar a.nav-link.active { color: var(--primary); font-weight: 600; background: #e7f0fe; }
.navbar .spacer { flex: 1; }

.container { max-width: 1200px; margin: 0 auto; padding: 24px; }

/* ---------- Buttons / forms ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  font-family: inherit;
  white-space: nowrap;
}
.btn:hover { background: var(--primary-dark); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn.secondary { background: var(--bg); color: var(--text); }
.btn.secondary:hover { background: var(--border); }
.btn.danger { background: transparent; color: var(--red); }
.btn.danger:hover { background: #fdeaee; }
.btn.small { padding: 6px 12px; font-size: 13px; }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); border-color: transparent; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}

.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
  background: #e7f0fe;
  color: var(--primary);
}
.badge.green { background: #e5f5e9; color: var(--green); }

.muted { color: var(--muted); font-size: 13px; }
.error-text { color: var(--red); font-size: 14px; }

/* ---------- Connect page ---------- */
.page-list { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.page-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.avatar-img {
  width: 44px; height: 44px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}
.page-item img.avatar, .avatar-fallback {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 18px;
  flex-shrink: 0;
  object-fit: cover;
}
.page-item .info { flex: 1; min-width: 0; }
.page-item .info .name { font-weight: 600; font-size: 15px; }

/* ---------- Inbox layout ---------- */
.inbox-layout {
  display: grid;
  grid-template-columns: 290px 360px 1fr;
  gap: 0;
  height: calc(100vh - 56px);
  background: var(--card);
}

/* ---------- Calendar panel ---------- */
.cal-panel {
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

/* ---------- Page list (เหนือปฏิทิน) ---------- */
.page-list-panel {
  border-bottom: 1px solid var(--border);
  padding: 10px 8px;
  max-height: 42vh;
  overflow-y: auto;
}
.page-list-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.page-list-item:hover { background: var(--bg); }
.page-list-item.active { background: #e7f0fe; color: var(--primary); font-weight: 600; }
.page-list-item span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-avatar, .page-avatar-fallback {
  width: 26px; height: 26px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

/* จอกว้าง: ใช้รายการเพจแทน dropdown */
@media (min-width: 1101px) {
  #pageFilter { display: none; }
}
.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
}
.cal-title { font-weight: 700; font-size: 15px; }
.cal-nav {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  width: 30px; height: 30px;
  font-size: 16px;
  cursor: pointer;
  color: var(--text);
}
.cal-nav:hover { background: var(--bg); }
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 3px;
  padding: 8px 12px;
}
.cal-dow {
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  padding: 4px 0;
}
.cal-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  min-height: 44px;
  padding: 3px 0;
}
.cal-cell .cal-day { font-size: 13px; color: var(--text); }
.cal-cell .cal-count {
  font-size: 11px;
  font-weight: 700;
  color: var(--primary);
  background: #e7f0fe;
  border-radius: 999px;
  padding: 0 6px;
  margin-top: 2px;
}
.cal-cell.has-count { cursor: pointer; }
.cal-cell.has-count:hover { background: var(--bg); }
.cal-cell.today { outline: 1.5px solid var(--primary); outline-offset: -1.5px; }
.cal-cell.selected { background: var(--primary); }
.cal-cell.selected .cal-day { color: #fff; font-weight: 700; }
.cal-cell.selected .cal-count { background: rgba(255,255,255,0.25); color: #fff; }
.conv-panel {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.conv-panel .panel-head { padding: 16px; border-bottom: 1px solid var(--border); }
.conv-panel .panel-head h2 { font-size: 20px; margin-bottom: 12px; }
.conv-panel .filters { display: flex; gap: 8px; }
.conv-list { overflow-y: auto; flex: 1; }

.conv-item {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--bg);
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: #e7f0fe; }
.conv-item .conv-body { flex: 1; min-width: 0; }
.conv-item .top-row { display: flex; justify-content: space-between; gap: 8px; }
.conv-item .cust-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.conv-item .preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv-item .page-tag { font-size: 11px; color: var(--primary); font-weight: 600; margin-top: 4px; }
.conv-item .unread-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--primary); align-self: center; flex-shrink: 0;
}

/* ---------- Message thread ---------- */
.thread-panel { display: flex; flex-direction: column; min-height: 0; }
.thread-head {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}
.thread-head .name { font-weight: 700; font-size: 16px; }
.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg);
}
.msg-row { display: flex; }
.msg-row.from-page { justify-content: flex-end; }
.bubble {
  max-width: 65%;
  padding: 9px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.45;
  background: var(--card);
  border: 1px solid var(--border);
  word-wrap: break-word;
}
.msg-row.from-page .bubble { background: var(--primary); color: #fff; border-color: var(--primary); }
.bubble .meta { font-size: 11px; opacity: 0.75; margin-top: 4px; }
.bubble .meta-time { font-size: 14px; font-weight: 700; opacity: 1; }
.bubble .meta-latency { font-size: 11px; font-style: italic; }
.bubble img.attach { max-width: 240px; border-radius: 10px; display: block; margin-top: 6px; }

/* ---------- Lightbox ---------- */
.bubble img.attach { cursor: zoom-in; }
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
  cursor: zoom-out;
}
.lightbox.show { display: flex; }
.lightbox img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
}

/* ---------- Tags ---------- */
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  background: #fff3e0;
  color: #e65100;
  border-radius: 999px;
  padding: 1px 8px;
  margin: 3px 4px 0 0;
}
.tag-chip b { cursor: pointer; font-weight: 700; opacity: 0.7; }
.tag-chip b:hover { opacity: 1; }
.tag-add {
  border: 1px dashed var(--border);
  background: none;
  border-radius: 999px;
  font-size: 11px;
  padding: 1px 8px;
  cursor: pointer;
  color: var(--muted);
  margin-top: 3px;
  font-family: inherit;
}
.tag-add:hover { background: var(--bg); }
.tag-input {
  width: 130px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  margin: 3px 0 0 4px;
  font-family: inherit;
}

.reply-bar {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--card);
}
.reply-bar input { flex: 1; border-radius: 20px; }

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--muted);
  text-align: center;
  padding: 40px;
}
.empty-state .big { font-size: 44px; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* ---------- Stats panel (คอลัมน์ขวาสุด) ---------- */
.stats-panel {
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: none;
  background: var(--card);
}
.inbox-layout.stats-open { grid-template-columns: 290px 340px 1fr 320px; }
.inbox-layout.stats-open .stats-panel { display: block; }

.stats-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  position: sticky;
  top: 0;
  background: var(--card);
}
.stats-body { padding: 14px 16px; display: flex; flex-direction: column; gap: 16px; }
.stats-cust { display: flex; align-items: center; gap: 10px; }
.stat-block { border-bottom: 1px solid var(--bg); padding-bottom: 14px; font-size: 14px; }
.stat-block:last-of-type { border-bottom: none; }
.stat-label { font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.stat-block textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
}

/* กราฟช่วงเวลา 00-24 น. */
.hour-chart {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 64px;
  background: var(--bg);
  border-radius: 8px;
  padding: 6px 6px 0;
}
.hour-bar {
  flex: 1;
  background: #a8c7f0;
  border-radius: 2px 2px 0 0;
  min-height: 2px;
}
.hour-bar.peak { background: var(--primary); }
.hour-axis {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--muted);
  padding: 2px 4px 0;
}

@media (max-width: 1250px) {
  .inbox-layout.stats-open { grid-template-columns: 290px 340px 1fr; }
  .inbox-layout.stats-open .stats-panel { display: none; }
}

@media (max-width: 1100px) {
  .inbox-layout, .inbox-layout.stats-open { grid-template-columns: 360px 1fr; }
  .cal-panel { display: none; }
}

@media (max-width: 800px) {
  .inbox-layout, .inbox-layout.stats-open { grid-template-columns: 1fr; }
  .thread-panel { display: none; }
  .inbox-layout.thread-open .conv-panel { display: none; }
  .inbox-layout.thread-open .thread-panel { display: flex; }
}
