:root {
  --bg: #121318;
  --surface: #1C1E26;
  --surface-2: #262935;
  --text: #F2F3F7;
  --text-dim: #9397A6;
  --accent: #FF6B4A;
  --ok: #34C759;
  --danger: #FF3B30;
  --border: #2E313E;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #FFFFFF;
    --surface: #F5F6FA;
    --surface-2: #ECEEF4;
    --text: #1A1A1F;
    --text-dim: #6B6F7B;
    --border: #E2E4EC;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  height: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 480px;
  margin: 0 auto;
}

.screen { flex: 1; padding: 20px 16px 90px; }
.hidden { display: none !important; }

h1 { font-size: 22px; margin: 8px 0 20px; }
p.hint { color: var(--text-dim); font-size: 14px; margin-top: -8px; }

.field {
  margin-bottom: 14px;
}
.field label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}
.field input:focus { outline: 2px solid var(--accent); }

.btn {
  display: block;
  width: 100%;
  padding: 13px;
  border-radius: 14px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.6; }
.btn.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.error-text {
  color: var(--danger);
  font-size: 14px;
  min-height: 18px;
  margin: 4px 0 12px;
}

.switch-link {
  text-align: center;
  margin-top: 16px;
  font-size: 14px;
  color: var(--text-dim);
}
.switch-link a { color: var(--accent); text-decoration: none; }

/* --- нижний таб-бар --- */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.tabbar button {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 10px 0 8px;
  font-size: 11px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}
.tabbar button.active { color: var(--accent); }
.tabbar .icon { font-size: 20px; line-height: 1; }

/* --- профиль --- */
.avatar-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
.avatar-wrap .avatar-edit {
  position: relative;
  width: 96px;
  height: 96px;
}
.avatar-wrap img {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
}
.avatar-wrap .pencil {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  border: 2px solid var(--bg);
}

.profile-name { text-align: center; font-size: 18px; font-weight: 600; margin: 0; }
.profile-nick { text-align: center; color: var(--text-dim); font-size: 14px; margin: 2px 0 24px; }

.placeholder-tab {
  text-align: center;
  color: var(--text-dim);
  margin-top: 80px;
  font-size: 15px;
}

/* --- списки чатов/контактов --- */
.list-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.list-row img {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--surface-2);
}
.avatar-dot-wrap { position: relative; display: inline-block; flex-shrink: 0; }
.online-dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--ok);
  border: 2px solid var(--bg);
}
.list-row .meta { flex: 1; min-width: 0; }
.list-row .name { font-weight: 600; font-size: 15px; }
.list-row .preview {
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.list-row .time { color: var(--text-dim); font-size: 12px; flex-shrink: 0; }

/* --- экран чата --- */
#screen-chat { padding: 0; display: flex; flex-direction: column; }
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.chat-back { background: none; border: none; color: var(--text); font-size: 20px; cursor: pointer; padding: 0 4px; }
.chat-peer-avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--surface-2); }
.chat-peer-name { font-weight: 600; font-size: 15px; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bubble {
  max-width: 78%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.35;
  word-break: break-word;
}
.bubble.mine {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.bubble.theirs {
  align-self: flex-start;
  background: var(--surface);
  border-bottom-left-radius: 4px;
}
.bubble.pending { opacity: 0.6; }
.bubble .attachment {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0,0,0,0.12);
  border-radius: 10px;
  padding: 8px 10px;
  margin-top: 4px;
  text-decoration: none;
  color: inherit;
}
.bubble .attachment .fname { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bubble .attachment .fsize { font-size: 11px; opacity: 0.8; }
.bubble .sender-label { font-size: 11px; color: var(--accent); font-weight: 600; margin-bottom: 2px; }

.chat-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
}
.chat-input-row input[type="text"],
.chat-input-row input:not([type]) {
  flex: 1;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
.chat-icon-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-dim);
  cursor: pointer;
  flex-shrink: 0;
}
.chat-icon-btn.chat-send { color: var(--accent); }
.chat-call-btn { margin-left: auto; color: var(--ok); font-size: 20px; }

/* --- оверлеи звонка (входящий/исходящий/активный) --- */
.call-overlay {
  position: fixed;
  inset: 0;
  background: #0A0B0E;
  color: #fff;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.call-avatar {
  width: 128px;
  height: 128px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 12px;
}
.call-name { font-size: 22px; font-weight: 600; }
.call-status { color: #9397A6; font-size: 15px; margin-bottom: 30px; }
.call-actions { display: flex; gap: 50px; }
.call-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  font-size: 26px;
  color: #fff;
  cursor: pointer;
}
.call-btn.accept { background: var(--ok); }
.call-btn.reject { background: var(--danger); }

/* --- активный звонок --- */
.active-call { justify-content: flex-end; padding-bottom: 30px; background: #000; }
.call-videos {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  align-content: center;
}
.call-videos video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #15161B;
}
.call-videos .tile { position: relative; }
.call-videos .tile.speaking { outline: 3px solid var(--accent); outline-offset: -3px; }
.call-videos .tile.local-pip {
  position: absolute;
  width: 100px;
  height: 140px;
  right: 16px;
  bottom: 100px;
  border-radius: 12px;
  overflow: hidden;
  z-index: 2;
}
.call-controls {
  position: relative;
  z-index: 3;
  display: flex;
  gap: 24px;
  background: rgba(20,20,24,0.7);
  padding: 12px 20px;
  border-radius: 40px;
}
.call-ctrl-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
}
.call-ctrl-btn.off { background: rgba(255,255,255,0.35); opacity: 0.6; }
.call-ctrl-btn.hangup { background: var(--danger); }

.tab-header-row { display: flex; align-items: center; justify-content: space-between; }
.tab-header-row h1 { margin: 0; }
.btn-pill {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.screen .btn + .btn { margin-top: 10px; }

/* --- модальное окно (добавить участника) --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 900;
  display: flex;
  align-items: flex-end;
}
.modal-sheet {
  background: var(--bg);
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  border-radius: 20px 20px 0 0;
  padding: 16px;
  max-height: 70vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 14px;
}
