/* The class chat (chat.js). Wide screens: a «چت» tab beside the people in the side panel. Narrow screens: a «چت»
   button (the student's beside the big hand button, the teacher's in the bottom bar) and a sheet over the stage
   that ends at the bottom bar, so the hand button stays in sight; on a phone on its side the sheet takes the stage's
   half. Messages are plain text, each in its own direction. The teacher's messages carry «معلم» and a quiet
   turquoise ground. */

/* Read out, not shown. */
.chat-sr, .room[data-chat-wide="1"] .chat-head h2 {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* ---------- the side panel's tabs (wide) ---------- */
.chat-tabs { display: none; }
.room[data-chat-wide="1"] .chat-tabs {
  display: flex; gap: 4px;
  margin: -6px 0 10px;
  border-bottom: 1px solid var(--line);
}
.chat-tab {
  appearance: none; flex: 1 1 0;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 6px 10px; margin-bottom: -1px;
  border: 0; border-bottom: 3px solid transparent; border-radius: 10px 10px 0 0;
  background: none; color: var(--muted);
  font-size: 15px; font-weight: 800; cursor: pointer; touch-action: manipulation;
}
.chat-tab .ic { width: 18px; height: 18px; }
.chat-tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
@media (hover: hover) { .chat-tab:hover { background: var(--bg-sunken); color: var(--ink); } }
.chat-tab .count.chat-unread { background: var(--accent); color: var(--accent-ink); }
/* The tab says «توی کلاس»: the panel's own heading steps back; its search stays. */
.room[data-chat-wide="1"] .room-side .side-head h2 { display: none; }
.room[data-chat-wide="1"] .room-side .side-head:not(:has(input:not([hidden]))) { display: none; }
/* One tab at a time. */
.room[data-chat-wide="1"] .room-side[data-tab="chat"] > :not(.chat-tabs):not(.chat) { display: none !important; }
.room-side:not([data-tab="chat"]) > .chat { display: none; }
@media (min-width: 960px) {
  .room[data-chat-wide="1"] .room-side[data-tab="chat"] {
    display: flex; flex-direction: column;
    height: calc(100dvh - var(--rbar-h) - 40px);
    overflow: hidden;
  }
}

/* ---------- the view ---------- */
.chat { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }
.chat-head { display: flex; align-items: center; gap: 8px; min-height: var(--tap); margin-bottom: 4px; }
.chat-head h2 { flex: 1; display: flex; align-items: center; gap: 8px; margin: 0; font-size: 17px; font-weight: 800; }
.chat-head h2 .ic { width: 20px; height: 20px; color: var(--accent); }
.chat-head .chat-lockbtn { margin-inline-start: auto; }
.room[data-chat-wide="1"] .chat-head { min-height: 0; margin: 0; justify-content: flex-end; }
.room[data-chat-wide="1"] .chat-x { display: none; }
.chat-x { color: var(--muted); }
.chat-x .ic { width: 22px; height: 22px; }

.chat-state, .chat-note {
  margin: 0 0 8px; padding: 8px 12px;
  border-radius: var(--r);
  background: var(--warn-soft); color: var(--warn);
  font-size: 14px; font-weight: 700; line-height: 1.6;
}
.chat-note { margin: 8px 0 0; }
.chat-muted { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.chat-muted .btn { gap: 4px; padding-inline: 10px; }
.chat-muted .ic { width: 16px; height: 16px; }

.chat-list {
  list-style: none; margin: 0; padding: 2px 2px 8px;
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-list:focus-visible { outline-offset: -3px; border-radius: var(--r); }
.chat-empty { margin: 12px 0; color: var(--muted); font-size: 14px; text-align: center; }
/* No messages yet: the line sits in the middle of the box, not on the composer. */
.chat-list:empty { flex: 0 0 0; padding: 0; }
.chat-list:empty + .chat-empty { margin-block: auto; padding-block: 12px; }

/* One message. */
.cm { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 0 4px; padding: 6px 10px; border-radius: 12px; }
.cm:not(.grouped) { margin-top: 6px; }
.cm:first-child { margin-top: 0; }
.cm.grouped { padding-top: 0; }
.cm-body { flex: 1 1 0; min-width: 0; }
.cm-head { display: flex; align-items: baseline; gap: 6px; min-width: 0; font-size: 13px; line-height: 1.6; }
.cm-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 800; color: var(--ink); }
.cm-role { flex: none; }
.cm-time { flex: none; margin-inline-start: auto; color: var(--faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.cm-text {
  margin: 0; font-size: 15.5px; line-height: 1.75;
  white-space: pre-wrap; overflow-wrap: anywhere; text-align: start;
}
.cm[data-role="teacher"] { background: var(--accent-soft); }
.cm[data-role="teacher"] .cm-name { color: var(--accent-strong); }
.cm[data-role="teacher"] .cm-role { background: var(--card); color: var(--accent-strong); font-weight: 700; }
.cm[data-role="teacher"] + .cm[data-role="teacher"].grouped { margin-top: -2px; border-top-left-radius: 4px; border-top-right-radius: 4px; }
.cm[data-mine="1"] .cm-name { color: var(--muted); }
.cm[data-pending="1"] { opacity: .6; }
.cm-del { flex: none; align-self: center; color: var(--faint); padding: 0; }
.cm-del .ic { width: 18px; height: 18px; }
@media (hover: hover) { .cm-del:hover { color: var(--danger-ink); } }
.cm-ask {
  flex: 1 1 100%;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px;
  padding: 6px 0 2px; font-size: 14px; font-weight: 700;
}
.cm-ask span { margin-inline-end: auto; }

/* The composer. */
.chat-compose { position: relative; padding-top: 10px; border-top: 1px solid var(--line); }
.chat-form { display: flex; align-items: flex-end; gap: 8px; margin: 0; }
.chat-form[hidden] { display: none; }
.chat-input {
  flex: 1 1 auto; min-width: 0;
  min-height: 48px; max-height: 132px; margin: 0;
  padding: 11px 12px; resize: none;
  font-size: 16px; line-height: 1.6; /* 16px: iPhone never zooms into the box */
  overflow-y: auto;
}
.chat-send { flex: none; width: 48px; min-width: 48px; height: 48px; padding: 0; border-radius: var(--r); }
.chat-send .ic { width: 22px; height: 22px; }
.chat-count { position: absolute; top: -14px; inset-inline-end: 4px; padding: 0 6px; background: var(--card); color: var(--muted); font-size: 12px; }
.chat-err { margin: 6px 0 0; color: var(--danger-ink); font-size: 14px; font-weight: 700; }
.chat-err:empty { display: none; }

/* ---------- narrow: the student's button beside the big one ---------- */
.thumb-row { display: flex; align-items: stretch; gap: 8px; }
.thumb-row > .thumb-btn { flex: 1 1 auto; min-width: 0; }
.chat-slot { display: flex; }
.chat-slot:empty, .room[data-chat-wide="1"] .chat-slot { display: none; }
.chat-btn {
  appearance: none; position: relative; flex: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  width: 72px; min-height: 64px; padding: 4px;
  border: 1px solid var(--line-strong); border-radius: 18px;
  background: var(--card); color: var(--accent-strong);
  font-size: 13px; font-weight: 800; line-height: 1.4;
  cursor: pointer; touch-action: manipulation;
}
.chat-btn .ic { width: 24px; height: 24px; }
.chat-btn[aria-expanded="true"] { background: var(--accent-soft); border-color: var(--accent-line); }
@media (hover: hover) { .chat-btn:hover { border-color: var(--accent-line); } }
.chat-btn .nbadge, .tbar-btn .nbadge.chat-nbadge {
  position: absolute; top: 3px; inset-inline-end: 5px;
  min-width: 20px; height: 20px; padding: 0 5px;
  display: grid; place-items: center;
  border-radius: var(--r-pill);
  background: var(--accent); color: var(--accent-ink);
  font-size: 12px; font-weight: 800; line-height: 1;
}
.chat-btn .nbadge:empty, .tbar-btn .nbadge.chat-nbadge:empty { display: none; }
.tbar-btn.chat-jump[aria-expanded="true"] { background: var(--accent-soft); border-color: var(--accent-line); }
@media (max-width: 400px) {
  .tbar-jumps:has(.chat-jump) .tbar-btn { min-width: 56px; }
}

/* ---------- narrow: the sheet ---------- */
.chat-sheet {
  position: fixed; z-index: 14;
  inset-inline: 0; margin-inline: auto;
  width: 100%; max-width: 600px;
  top: var(--chat-top, max(calc(var(--rbar-h) + 8px), 26dvh));
  bottom: var(--chat-kb, var(--bar-h));
  display: flex; flex-direction: column;
  padding: 10px max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left));
  background: var(--card-raised); color: var(--ink);
  border: 1px solid var(--line); border-bottom: 0;
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow-2);
}
.chat-sheet[hidden] { display: none; }
.chat-sheet:focus { outline: none; }
.chat-sheet:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
/* The keyboard is up: the sheet fills what is left above it. */
.room[data-kb="1"] .chat-sheet { border-radius: 16px; border-bottom: 1px solid var(--line); }
/* A phone on its side: the sheet takes the stage's half; the floor and the hand button stay beside it. */
@media (max-width: 959px) and (max-height: 540px) and (orientation: landscape) {
  .chat-sheet {
    top: var(--chat-top, 8px);
    bottom: var(--chat-kb, calc(var(--bar-h) + 8px));
    inset-inline-start: max(8px, env(safe-area-inset-right)); inset-inline-end: auto;
    width: calc(50% - 16px); max-width: none; margin: 0;
    border-radius: 16px; border-bottom: 1px solid var(--line);
  }
}

/* ---------- a teacher's message while the chat is out of sight (students) ----------
   Just above the bottom bar, by the «چت» button that opens it: never over the stage's top, the header's banners
   («برای شنیدن… بزن») or the hand button. On a phone on its side, over the stage's half. */
.chat-toast {
  appearance: none;
  position: fixed; z-index: 13;
  bottom: calc(var(--bar-h) + 12px);
  inset-inline: 12px; margin-inline: auto;
  max-width: 520px;
  display: flex; align-items: flex-start; gap: 10px;
  min-height: var(--tap); padding: 10px 14px;
  border: 1px solid var(--accent-line); border-radius: 14px;
  background: var(--card-raised); color: var(--ink);
  box-shadow: var(--shadow-2);
  font-size: 15px; line-height: 1.6; text-align: start;
  cursor: pointer; touch-action: manipulation;
  animation: chat-in .18s ease-out;
}
.chat-toast[hidden] { display: none; }
.chat-toast .ic { width: 20px; height: 20px; margin-top: 2px; color: var(--accent); }
.chat-toast-body {
  min-width: 0;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-clamp: 2;
  overflow: hidden; overflow-wrap: anywhere;
}
.chat-toast-body b { color: var(--accent-strong); }
@media (min-width: 960px) {
  .chat-toast { bottom: 20px; inset-inline: auto 24px; width: min(420px, 100% - 48px); }
}
@media (max-width: 959px) and (max-height: 540px) and (orientation: landscape) {
  .chat-toast {
    bottom: 8px; inset-inline-start: max(8px, env(safe-area-inset-right)); inset-inline-end: auto;
    width: calc(50% - 16px); max-width: none; margin: 0;
  }
}
@keyframes chat-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) { .chat-toast { animation: none; } }

/* «بستن چت» makes four class tools: two by two rather than three and one. */
.tool-grid:has(#chat-lock-tool) { grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); }

/* ---------- after the class: the transcript beside attendance ---------- */
.chat-record h2 { display: flex; align-items: center; gap: 8px; }
.chat-record h2 .ic { width: 20px; height: 20px; color: var(--accent); }
.chat-record .hint { margin: 6px 0 12px; }
.chat-record-acts { display: flex; flex-wrap: wrap; gap: 8px; }
.chat-record-acts .btn { width: auto; }
@container (min-width: 860px) {
  .pg-att > #chat-record { grid-column: 1; }
}
