/* pages/chat.css — Trò chuyện lớp (templates/pages/chat.html, js/pages/chat.js), phong cách C.
   Máy tính: 2 cột (danh sách lớp | khung chat), cao đúng phần còn lại dưới thanh trên.
   Điện thoại (≤ 860px): 1 cột, #chat[data-view="list"|"room"]. */

.chat {
  display: grid; grid-template-columns: 300px minmax(0, 1fr);
  height: calc(100vh - var(--topbar-h)); height: calc(100dvh - var(--topbar-h));
  background: var(--bg);
}
.chat .tile-lg { width: 48px; height: 48px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); }

/* ── Danh sách lớp ─────────────────────────────────────────────── */
.chat-side { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg-side, var(--bg)); }
.chat-side-title { margin: 0; padding: 16px 16px 8px; font-size: 12px; font-weight: 600; color: var(--faint); text-transform: uppercase; letter-spacing: .04em; }
.chat-convs { list-style: none; margin: 0; padding: 0 8px 12px; overflow-y: auto; }
.chat-conv {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 10px 8px;
  border: 0; border-radius: var(--r-md); background: none; font: inherit; text-align: left; color: var(--text); cursor: pointer;
}
.chat-conv:hover { background: var(--bg-soft); }
.chat-conv.is-active { background: var(--accent-soft); }
.chat-conv-av {
  width: 40px; height: 40px; flex-shrink: 0; border-radius: 10px; display: grid; place-items: center;
  font-size: 14px; font-weight: 700;
}
.chat-conv-body { flex: 1; min-width: 0; }
.chat-conv-top { display: flex; align-items: baseline; gap: 8px; }
.chat-conv-name { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-conv-time { flex-shrink: 0; font-size: 12px; color: var(--faint); }
.chat-conv-bottom { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.chat-conv-preview { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-conv.has-unread .chat-conv-preview { color: var(--ink); font-weight: 500; }
.chat-badge {
  flex-shrink: 0; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: #e5383b; color: #fff; font-size: 11px; font-weight: 700; line-height: 20px; text-align: center;
}

.chat-empty, .chat-pick {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 32px 24px; text-align: center; color: var(--muted); font-size: 14px;
}
.chat-empty-title { margin: 4px 0 0; font-size: 16px; font-weight: 600; color: var(--ink); }
.chat-empty-text { margin: 0 0 6px; max-width: 280px; line-height: 1.5; }
.chat-pick p { margin: 0; }

/* ── Khung chat ────────────────────────────────────────────────── */
.chat-room { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}
.chat-head .chat-back { display: none; }
.chat-head-name { margin: 0; font-size: 15px; font-weight: 600; color: var(--ink); }
.chat-head-meta { margin: 0; font-size: 12px; color: var(--muted); }
.chat-status { margin-left: auto; font-size: 12px; color: var(--amber); }

.chat-scroll { position: relative; flex: 1; min-height: 0; overflow-y: auto; padding: 12px 16px 8px; overscroll-behavior: contain; }
.chat-sentinel { height: 1px; }
.chat-loading { display: flex; justify-content: center; padding: 12px; color: var(--faint); font-size: 13px; }
.chat-day { display: flex; align-items: center; gap: 12px; margin: 16px 0 8px; font-size: 12px; font-weight: 500; color: var(--faint); }
.chat-day::before, .chat-day::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.chat-none { margin: auto; padding: 48px 16px; text-align: center; color: var(--muted); font-size: 14px; }

/* Tin nhắn */
.msg { position: relative; display: flex; gap: 10px; padding: 2px 0; }
.msg.is-first { margin-top: 10px; }
.msg-av {
  width: 32px; height: 32px; flex-shrink: 0; border-radius: 50%; display: grid; place-items: center;
  font-size: 13px; font-weight: 700;
}
.msg-gutter { width: 32px; flex-shrink: 0; }
.msg-main { min-width: 0; max-width: min(620px, 78%); display: flex; flex-direction: column; align-items: flex-start; }
.msg-meta { display: flex; align-items: baseline; gap: 6px; margin-bottom: 2px; font-size: 12px; color: var(--faint); }
.msg-name { font-weight: 600; color: var(--ink); font-size: 13px; }
.msg-teacher { padding: 1px 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 600; }
.msg-bubble {
  padding: 7px 11px; border-radius: 12px; background: var(--bg-soft); color: var(--ink);
  font-size: 14px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-bubble a { color: var(--accent); text-decoration: underline; }
.msg-bubble.is-deleted { background: none; border: 1px dashed var(--line); color: var(--faint); font-style: italic; }
.msg-img {
  display: block; max-width: 280px; max-height: 320px; margin-top: 2px;   /* width/height đặt sẵn trong chat.js imgBox() */
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg-soft); cursor: zoom-in; object-fit: cover;
}
.msg-time { flex-shrink: 0; align-self: flex-end; font-size: 11px; color: var(--faint); opacity: 0; transition: opacity .12s; }
.msg:hover .msg-time { opacity: 1; }

/* Tin của mình: lệch phải */
.msg.is-mine { flex-direction: row-reverse; }
.msg.is-mine .msg-main { align-items: flex-end; }
.msg.is-mine .msg-bubble { background: var(--accent-soft); }
.msg.is-mine .msg-bubble.is-deleted { background: none; }

/* Cảm xúc */
.msg-reacts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.react-chip {
  display: inline-flex; align-items: center; gap: 3px; height: 24px; padding: 0 7px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg); font: inherit; font-size: 12px; color: var(--text); cursor: pointer;
}
.react-chip:hover { border-color: var(--faint); }
.react-chip.is-mine { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Thanh công cụ khi rê chuột */
.msg-tools {
  position: absolute; top: -14px; right: 0; z-index: 2; display: none; gap: 2px; padding: 3px;
  border: 1px solid var(--line); border-radius: var(--r-md); background: var(--bg); box-shadow: var(--shadow-pop);
}
.msg.is-mine .msg-tools { right: auto; left: 0; }
.msg:hover .msg-tools, .msg:focus-within .msg-tools, .msg.tools-open .msg-tools { display: flex; }
.msg-tool {
  width: 28px; height: 28px; display: grid; place-items: center; border: 0; border-radius: 6px;
  background: none; color: var(--muted); font-size: 16px; line-height: 1; cursor: pointer;
}
.msg-tool:hover { background: var(--bg-soft); color: var(--ink); }
.msg-tool.is-danger:hover { background: var(--red-bg); color: var(--red); }

/* Nút "↓ N tin mới" */
.chat-jump {
  position: sticky; bottom: 8px; left: 50%; z-index: 3; display: flex; margin: 0 auto; width: max-content;
  align-items: center; gap: 6px; height: 32px; padding: 0 14px; border: 0; border-radius: 16px;
  background: var(--accent); color: var(--on-accent); font: inherit; font-size: 13px; font-weight: 600;
  box-shadow: var(--shadow-pop); cursor: pointer;
}

/* Ô soạn */
.chat-compose { border-top: 1px solid var(--line); padding: 10px 16px 12px; background: var(--bg); }
.chat-guest { margin: 0; padding: 14px 16px; border-top: 1px solid var(--line); text-align: center; font-size: 14px; color: var(--muted); }
.chat-preview { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.chat-preview img { width: 56px; height: 56px; object-fit: cover; border: 1px solid var(--line); border-radius: var(--r-md); }
.chat-row { display: flex; align-items: flex-end; gap: 8px; }
.chat-iconbtn {
  width: 38px; height: 38px; flex-shrink: 0; display: grid; place-items: center;
  border: 0; border-radius: var(--r-md); background: none; color: var(--muted); cursor: pointer;
}
.chat-iconbtn:hover { background: var(--bg-soft); color: var(--ink); }
.chat-input {
  flex: 1; min-width: 0; min-height: 38px; max-height: 132px; padding: 8px 12px; resize: none;
  border: 1px solid var(--line); border-radius: 19px; background: var(--bg); font: inherit; font-size: 14px; line-height: 20px; color: var(--ink);
}
.chat-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chat-sendbtn {
  width: 38px; height: 38px; flex-shrink: 0; display: grid; place-items: center;
  border: 0; border-radius: 50%; background: var(--accent); color: var(--on-accent); cursor: pointer;
}
.chat-sendbtn:disabled { background: var(--accent-soft); color: var(--accent); cursor: default; }
.chat-hint { margin: 6px 0 0 46px; font-size: 11px; color: var(--faint); }

/* Xem ảnh to */
.chat-lightbox {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 24px;
  background: rgba(15, 23, 42, .82); cursor: zoom-out;
}
.chat-lightbox img { max-width: 100%; max-height: 100%; border-radius: var(--r-md); }
.chat-lightbox button {
  position: absolute; top: 16px; right: 16px; width: 40px; height: 40px; display: grid; place-items: center;
  border: 0; border-radius: 50%; background: rgba(255, 255, 255, .16); color: #fff; cursor: pointer;
}

/* Màu vòng tròn tên / ô lớp — cố định theo id */
.av-0 { background: var(--green-bg); color: var(--green); }
.av-1 { background: var(--amber-bg); color: var(--amber); }
.av-2 { background: var(--blue-bg); color: var(--blue); }
.av-3 { background: var(--violet-bg); color: var(--violet); }
.av-4 { background: var(--red-bg); color: var(--red); }
.av-5 { background: var(--accent-soft); color: var(--accent); }

/* ── Điện thoại ────────────────────────────────────────────────── */
@media (max-width: 860px) {
  .chat { grid-template-columns: minmax(0, 1fr); }
  .chat[data-view="list"] .chat-room, .chat[data-view="room"] .chat-side { display: none; }
  .chat-side { border-right: 0; }
  .chat-head .chat-back { display: grid; margin-left: -8px; }
  .chat-head { padding: 6px 12px; }
  .chat-scroll { padding: 8px 12px; }
  .chat-compose { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); }
  .chat-hint { display: none; }
  .msg-main { max-width: 84%; }
  .msg-time { opacity: 1; }
  .msg-img { max-width: 220px; }
}
@media (hover: none) {
  .msg:hover .msg-tools { display: none; }
  .msg.tools-open .msg-tools { display: flex; }
}
