:root {
  color-scheme: light dark;
  --bg: #f2f5fa;
  --panel: #ffffff;
  --text: #141821;
  --muted: #7d8799;
  --accent: #2f7cf6;
  --accent-soft: #e7f0ff;
  --bubble: #ffffff;
  --own: #2f7cf6;
  --own-text: #ffffff;
  --line: #e5e9f1;
  --hover: #eef3fb;
  --shadow: 0 18px 50px rgba(21, 33, 61, 0.12);
  --shadow-soft: 0 2px 10px rgba(21, 33, 61, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141821;
    --panel: #1b202b;
    --text: #eef2fa;
    --muted: #8f9ab0;
    --accent: #5b9bff;
    --accent-soft: #22304a;
    --bubble: #222936;
    --own: #3d7ff0;
    --own-text: #ffffff;
    --line: #2a3242;
    --hover: #232b3a;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
    --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.22);
  }
}

* {
  box-sizing: border-box;
}

/* Авторские display:flex у секций иначе перебивают атрибут hidden. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
  overflow: hidden;
}

@media (min-width: 760px) {
  body {
    padding: 14px 0;
  }

  #app {
    height: calc(100dvh - 28px);
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: var(--shadow);
  }
}

#header {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 60px;
  padding: 0 10px;
  background: var(--panel);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 5;
}

.header-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  padding-left: 4px;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease;
}

.icon-button:hover {
  background: var(--hover);
}

.icon-button:active {
  transform: scale(0.96);
}

#content {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.rooms-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 10px 24px;
}

.group-title {
  margin: 18px 12px 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.group-title:first-child {
  margin-top: 4px;
}

.room {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 16px;
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease;
}

.room:hover {
  background: var(--panel);
  box-shadow: var(--shadow-soft);
}

.room:active {
  background: var(--hover);
}

.room-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--accent-soft);
  font-size: 22px;
  line-height: 1;
}

.room-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.room-title {
  display: block;
  font-size: 15px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-preview {
  display: block;
  margin-top: 2px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-preview.empty {
  opacity: 0.7;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 11px;
  background: var(--accent);
  color: #ffffff;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

.chat-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 14px 10px;
  scroll-behavior: smooth;
}

.readonly-note {
  flex: 0 0 auto;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
}

.message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin: 5px 0;
}

.message.new {
  animation: message-in 0.16s ease-out;
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message.own {
  align-items: flex-end;
}

.message-author {
  margin: 0 8px 2px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.bubble {
  max-width: min(78%, 560px);
  padding: 9px 13px;
  border-radius: 16px 16px 16px 6px;
  background: var(--bubble);
  box-shadow: var(--shadow-soft);
  font-size: 15px;
  line-height: 1.38;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.own .bubble {
  background: var(--own);
  color: var(--own-text);
  border-radius: 16px 16px 6px 16px;
}

.bubble.has-media {
  max-width: min(88%, 560px);
  padding: 6px;
}

.bubble-text {
  padding: 3px 7px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.media-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.media-image-link {
  display: block;
  line-height: 0;
}

.media-image {
  display: block;
  max-width: 100%;
  max-height: 360px;
  border-radius: 11px;
  object-fit: contain;
  background: rgba(0, 0, 0, 0.08);
}

.media-video {
  display: block;
  width: 100%;
  max-height: 360px;
  border-radius: 11px;
  background: #000000;
}

.media-audio {
  display: block;
  width: min(100%, 340px);
  max-width: 100%;
}

.media-file {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  padding: 9px 10px;
  border-radius: 11px;
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
}

.message.own .media-file {
  background: rgba(255, 255, 255, 0.16);
  color: var(--own-text);
}

.media-file-icon {
  flex: 0 0 auto;
  font-size: 22px;
}

.media-file-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.media-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}

.media-file-size {
  margin-top: 1px;
  font-size: 11px;
  opacity: 0.72;
}

.message-time {
  margin: 3px 8px 0;
  font-size: 10px;
  color: var(--muted);
}

.message.own .message-time {
  color: var(--muted);
}

.empty-chat {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 220px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.empty-chat .state-icon {
  font-size: 40px;
  margin-bottom: 8px;
}

.composer {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.attach-button {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: var(--muted);
}

.attachment-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 1px 2px 0;
}

.attachment-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  min-width: 0;
  padding: 6px 7px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
}

.attachment-chip.error {
  border-color: #dc6464;
}

.attachment-thumb {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--hover);
}

.attachment-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--panel);
  font-size: 19px;
}

.attachment-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  max-width: 190px;
}

.attachment-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  font-weight: 600;
}

.attachment-meta {
  font-size: 10px;
  color: var(--muted);
}

.attachment-progress {
  display: block;
  width: 100%;
  height: 3px;
  margin-top: 1px;
  overflow: hidden;
  border-radius: 2px;
  background: var(--line);
}

.attachment-progress > span {
  display: block;
  height: 100%;
  border-radius: 2px;
  background: var(--accent);
  transition: width 0.12s linear;
}

.attachment-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
}

.attachment-remove:hover {
  background: var(--hover);
  color: var(--text);
}

#message-input {
  flex: 1;
  min-height: 42px;
  max-height: 128px;
  padding: 10px 14px;
  border: 0;
  border-radius: 21px;
  outline: none;
  resize: none;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
}

#message-input::placeholder {
  color: var(--muted);
}

.send-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.send-button:disabled {
  opacity: 0.35;
  cursor: default;
}

.send-button:not(:disabled):active {
  transform: scale(0.94);
}

.state-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 32px;
  text-align: center;
  color: var(--muted);
}

.state-view h1 {
  margin: 8px 0 0;
  font-size: 20px;
  color: var(--text);
}

.state-view p {
  max-width: 360px;
  margin: 0;
  line-height: 1.45;
}

.state-icon {
  font-size: 44px;
}

.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(82px + env(safe-area-inset-bottom));
  z-index: 20;
  max-width: calc(100% - 40px);
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(18, 22, 31, 0.92);
  color: #ffffff;
  font-size: 13px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 380px) {
  .header-title {
    font-size: 17px;
  }

  .bubble {
    max-width: 86%;
  }
}
