:root {
  --bg: #eef3fb;
  --surface: #ffffff;
  --surface-soft: #f8fbff;
  --surface-accent: #dfeafc;
  --border: #d6e2f4;
  --text: #1f2c46;
  --text-soft: #6f819f;
  --primary: #3390ec;
  --primary-strong: #2783dc;
  --ok: #2db66d;
  --danger: #df4f55;
  --shadow: 0 8px 24px rgba(40, 75, 130, 0.12);
  --mine-msg: #d9ecff;
  --other-msg: #ffffff;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--text); }
body.sidebar-open { overflow: hidden; touch-action: none; }
.hidden { display: none !important; }

#login { max-width: 360px; margin: 64px auto; padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
#login h1 { margin: 0 0 14px; font-size: 20px; color: #22314e; }
.video-room-hint { margin-bottom: 12px; font-size: 13px; color: #6f819f; line-height: 1.45; }
.video-room-hint a { color: #3390ec; font-weight: 600; }
#login input, #login button { width: 100%; margin: 8px 0; padding: 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); }
#login button { cursor: pointer; background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
#login .err { color: var(--danger); min-height: 18px; font-size: 13px; }

#app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; }
#sidebar { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; }
#sidebarHeader { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#whoami { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#btnCloseSidebar { display: none; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); border-radius: 8px; padding: 6px 9px; cursor: pointer; flex: 0 0 auto; }
#sidebarBackdrop { display: none; position: fixed; inset: 0; background: rgba(17, 29, 50, 0.34); opacity: 0; pointer-events: none; transition: opacity 0.2s ease; z-index: 25; }
#sidebarTop { padding: 12px; border-bottom: 1px solid var(--border); }
#sidebarTop .row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
#sidebarTop button { flex: 1 1 calc(50% - 8px); min-width: 120px; padding: 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); cursor: pointer; }

#chatList { overflow: auto; flex: 1; }
.chatItem { padding: 12px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background 0.15s ease; }
.chatItem:hover { background: var(--surface-soft); }
.chatItem.active { background: var(--surface-accent); }
.chatTitleRow { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.chatTitle { font-weight: 600; color: #22314e; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatDeleteBtn { border: 1px solid #c6d7ef; background: #fff; color: #3b577f; border-radius: 6px; font-size: 11px; padding: 3px 6px; cursor: pointer; flex: 0 0 auto; }
.chatMeta { margin-top: 4px; color: var(--text-soft); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--primary); color: #fff; border-radius: 12px; padding: 1px 7px; font-size: 11px; margin-left: 8px; }

#main { display: flex; flex-direction: column; min-width: 0; position: relative; background: #f3f7ff; }
#main.drag-over { outline: 2px dashed #6eaffe; outline-offset: -8px; }
#dropHint { position: absolute; inset: 56px 12px 70px 12px; display: none; align-items: center; justify-content: center; border: 2px dashed #6eaffe; border-radius: 12px; color: #2e5f9a; font-weight: 700; background: rgba(110, 175, 254, 0.15); pointer-events: none; z-index: 5; }
#main.drag-over #dropHint { display: flex; }

#topBar { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--surface); }
#chatPresence { font-size: 12px; color: var(--text-soft); min-width: 100px; }
#chatPresence.online { color: var(--ok); font-weight: 600; }
#topBar input, #topBar select { flex: 1; padding: 9px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); }
#btnToggleSidebar { display: none; }
#topBar button { padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); cursor: pointer; }
#langSelect { max-width: 84px; flex: 0 0 auto; cursor: pointer; }

#messages { flex: 1; overflow: auto; padding: 14px; position: relative; -webkit-overflow-scrolling: touch; }
.msg { max-width: 72%; margin: 8px 0; padding: 10px; border-radius: 12px; border: 1px solid #d8e4f7; background: var(--other-msg); box-shadow: 0 1px 4px rgba(52, 93, 152, 0.1); }
.msg.mine { margin-left: auto; border-color: #c4def8; background: var(--mine-msg); }
.msgHead { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.msgMeta { font-size: 12px; color: var(--text-soft); margin-bottom: 4px; }
.msgDeleteBtn { border: 1px solid #c6d7ef; background: #fff; color: #3b577f; border-radius: 6px; font-size: 11px; padding: 3px 6px; cursor: pointer; }
.attachmentBtn { margin-top: 7px; background: #fff; border: 1px solid var(--border); color: #33547f; border-radius: 8px; padding: 6px 8px; cursor: pointer; }
.attachmentPreviewLink { display: inline-block; margin-top: 8px; }
.attachmentPreview { display: block; max-width: min(320px, 60vw); max-height: 240px; border-radius: 10px; border: 1px solid #cfe0f8; box-shadow: 0 6px 16px rgba(48, 84, 132, 0.16); object-fit: cover; background: #f4f8ff; }

#composer { display: flex; gap: 8px; border-top: 1px solid var(--border); padding: 10px; background: var(--surface); padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
#composer input[type="text"] { flex: 1; padding: 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); }
#composer button, #composer label { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); cursor: pointer; }
#composer input[type="file"] { display: none; }

#toastStack { position: fixed; right: 12px; bottom: 12px; z-index: 40; display: flex; flex-direction: column; gap: 8px; max-width: 420px; }
.toast { border: 1px solid #d9e6f8; border-left: 4px solid var(--danger); background: #fff; color: var(--text); border-radius: 10px; padding: 10px 12px; box-shadow: var(--shadow); font-size: 14px; }

#callOverlay {
  position: fixed;
  inset: 0;
  background: #111;
  z-index: 50;
  padding: 0;
  display: none;
  flex-direction: column;
}
#callOverlay.visible { display: flex; }
#callTop {
  padding: 12px 16px;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  color: #fff;
  z-index: 10;
}
#callTopControls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
#callTopControls button { padding: 8px 10px; border-radius: 8px; border: 1px solid rgba(205, 220, 245, 0.55); background: rgba(24, 35, 56, 0.8); color: #fff; cursor: pointer; font-size: 12px; }
#callTopControls button.active-off { background: rgba(224, 86, 86, 0.86); border-color: rgba(255, 190, 190, 0.8); }
#callBody {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#callGrid {
  display: grid;
  width: 100%;
  height: 100%;
  gap: 8px;
}
.callCell {
  background: #222;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  border: 1px solid #333;
  cursor: pointer;
}
.callCell video { width: 100%; height: 100%; object-fit: cover; display: block; background: #000; }

#callGrid[data-peers="1"] { grid-template-columns: 1fr; grid-template-rows: 1fr; }
#callGrid[data-peers="2"] { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
@media (min-width: 640px) {
  #callGrid[data-peers="2"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
}
#callGrid[data-peers="3"], #callGrid[data-peers="4"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
#callGrid[data-peers="5"], #callGrid[data-peers="6"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; }
.callCell .callLabel { position: absolute; bottom: 6px; left: 6px; font-size: 11px; padding: 2px 6px; background: rgba(0, 0, 0, 0.65); border-radius: 4px; color: #fff; max-width: calc(100% - 12px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; pointer-events: none; }
.callCell .callHint { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 12px; color: #666; pointer-events: none; text-align: center; padding: 8px; }
#callFocusLayer { display: none; position: absolute; inset: 0; background: #000; z-index: 2; flex-direction: column; }
#callOverlay.focus-active #callFocusLayer { display: flex; }
#callOverlay.focus-active #callGrid { display: none; }
#btnFocusBack { position: absolute; top: 8px; left: 8px; z-index: 3; border: 1px solid rgba(205, 220, 245, 0.55); background: rgba(24, 35, 56, 0.8); color: #fff; border-radius: 8px; padding: 8px 10px; cursor: pointer; }
#focusVideoMount { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
#focusVideoMount video { width: 100%; height: 100%; object-fit: contain; background: #000; }
#focusCaption { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); max-width: calc(100% - 16px); font-size: 12px; padding: 5px 10px; background: rgba(0, 0, 0, 0.7); color: #fff; border-radius: 8px; text-align: center; pointer-events: none; }

@media (max-width: 920px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: min(86vw, 340px); height: auto; min-height: 0; transform: translateX(-100%); transition: transform 0.2s ease; z-index: 30; box-shadow: 0 10px 28px rgba(16, 35, 66, 0.28); }
  #app.sidebar-open #sidebar { transform: translateX(0); }
  #sidebarBackdrop { display: block; }
  #app.sidebar-open #sidebarBackdrop { opacity: 1; pointer-events: auto; }
  #btnToggleSidebar { display: inline-flex; flex: 0 0 auto; }
  #btnCloseSidebar { display: inline-flex; }
  .attachmentPreview { max-width: min(260px, 72vw); }
  #topBar { flex-wrap: wrap; row-gap: 6px; }
  #chatTitle { flex: 1 1 auto; min-width: 120px; }
  #chatPresence { min-width: auto; }
  #searchInput { flex: 1 1 100%; order: 20; }
  #messages { padding: 12px; }
  .msg { max-width: 84%; }
}

@media (max-width: 640px) {
  #login { margin: 24px 12px; padding: 16px; }
  #sidebar { width: 90vw; max-width: 340px; }
  #sidebarTop { padding: 10px; }
  #sidebarTop button { flex: 1 1 calc(50% - 8px); min-width: 0; padding: 9px 8px; }
  #topBar { padding: 8px; gap: 6px; }
  #topBar button { padding: 8px 10px; }
  #langSelect { max-width: 70px; }
  #messages { padding: 10px; }
  .msg { max-width: 92%; margin: 7px 0; }
  #dropHint { inset: 52px 8px 76px 8px; font-size: 14px; text-align: center; padding: 0 10px; }
  #composer { flex-wrap: wrap; padding: 8px; gap: 6px; }
  #composer input[type="text"] { flex: 1 1 100%; min-width: 0; }
  #composer label, #composer button { flex: 1 1 calc(50% - 3px); text-align: center; }
  #toastStack { left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); max-width: none; }
  .toast { font-size: 13px; }
}

/* --- ІНДИКАТОР ТА КНОПКА АКТИВНОГО ДЗВІНКА --- */
.chatCallIcon {
  display: inline-block;
  margin-right: 6px;
  font-size: 13px;
  color: var(--ok);
  animation: pulseCall 1.5s infinite;
}
#btnJoinCall {
  background: var(--ok) !important;
  color: white !important;
  border-color: var(--ok) !important;
  font-weight: bold;
  animation: pulseCall 2s infinite;
}
@keyframes pulseCall {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.85; }
  100% { transform: scale(1); opacity: 1; }
}
