:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-soft: #f7f9fb;
  --surface-strong: #e8eef4;
  --border: #d6dee7;
  --border-strong: #aebbc8;
  --text: #17212b;
  --muted: #667483;
  --muted-strong: #3d4a57;
  --primary: #1b6fb8;
  --primary-dark: #145a94;
  --accent: #0c846d;
  --danger: #b83342;
  --shadow: 0 22px 54px rgba(18, 34, 50, 0.16);
  font-family: Inter, "SF Pro Text", "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app-shell {
  display: grid;
  grid-template-columns: 336px minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  display: grid;
  min-height: 0;
  grid-template-rows: auto auto minmax(0, 1.1fr) minmax(0, 0.9fr);
  gap: 14px;
  padding: 16px;
  border-right: 1px solid var(--border);
  background: #fbfcfd;
}

.brand {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  background: var(--primary);
  font-weight: 800;
  letter-spacing: 0;
}

.brand-copy {
  min-width: 0;
}

.brand h1,
.brand p,
.section-title h2,
.chat-header h2,
.chat-header p,
.empty-state h3 {
  margin: 0;
}

.brand h1 {
  font-size: 18px;
  line-height: 1.2;
}

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

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--border-strong);
}

.status-dot.connected {
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(12, 132, 109, 0.12);
}

.status-dot.error {
  background: var(--danger);
  box-shadow: 0 0 0 4px rgba(184, 51, 66, 0.12);
}

.sidebar-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.button {
  display: inline-grid;
  min-height: 38px;
  place-items: center;
  border: 0;
  border-radius: 7px;
  padding: 0 16px;
  font-weight: 750;
  text-decoration: none;
}

.button.compact {
  min-height: 36px;
  padding: 0 10px;
  font-size: 13px;
}

.button.primary {
  color: #fff;
  background: var(--primary);
}

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

.button.secondary {
  border: 1px solid var(--border);
  color: var(--muted-strong);
  background: #fff;
}

.button.danger {
  color: #fff;
  background: var(--danger);
}

.nav-section {
  display: grid;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.section-title h2 {
  color: var(--muted-strong);
  font-size: 12px;
  font-weight: 800;
}

.room-list,
.device-list {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.room-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px;
  gap: 6px;
  align-items: center;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 4px;
}

.room-item.active {
  border-color: rgba(27, 111, 184, 0.28);
  background: rgba(27, 111, 184, 0.08);
}

.room-main {
  display: grid;
  min-width: 0;
  gap: 3px;
  border: 0;
  border-radius: 7px;
  padding: 8px;
  color: var(--text);
  background: transparent;
  text-align: left;
}

.room-main:hover,
.device-item:hover {
  background: var(--surface-strong);
}

.room-main strong,
.room-main span,
.device-meta strong,
.device-meta span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-main strong {
  font-size: 13px;
}

.room-main span,
.device-meta span {
  color: var(--muted);
  font-size: 12px;
}

.device-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px;
  color: var(--text);
  background: transparent;
  text-align: left;
}

.device-item.active {
  border-color: rgba(12, 132, 109, 0.28);
  background: rgba(12, 132, 109, 0.08);
}

.device-avatar,
.message-avatar {
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  background: #506070;
  font-weight: 800;
}

.device-avatar {
  width: 34px;
  height: 34px;
  font-size: 13px;
}

.device-meta {
  display: grid;
  min-width: 0;
  gap: 2px;
}

.device-meta strong {
  font-size: 13px;
}

.chat-area {
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-title {
  min-width: 0;
}

.eyebrow {
  margin-bottom: 3px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
}

.chat-header h2 {
  overflow: hidden;
  font-size: 19px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-header p {
  overflow: hidden;
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions,
.message-actions,
.attachment-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--muted-strong);
  background: #fff;
}

.icon-button:hover {
  border-color: var(--border-strong);
  background: var(--surface-strong);
}

.icon-button.small {
  width: 32px;
  height: 32px;
}

.icon-button.mini {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.icon-button svg,
.attachment-preview svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.messages-wrap {
  position: relative;
  min-height: 0;
  overflow: auto;
  padding: 20px 22px;
}

.message-list {
  display: grid;
  gap: 12px;
  max-width: 1040px;
  margin: 0 auto;
}

.message {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 10px;
  align-items: flex-start;
}

.message.own {
  grid-template-columns: minmax(0, 1fr) 38px;
}

.message.own .message-avatar {
  grid-column: 2;
}

.message.own .message-body {
  grid-column: 1;
  grid-row: 1;
  justify-self: end;
}

.message-avatar {
  width: 38px;
  height: 38px;
  font-size: 13px;
}

.message-body {
  min-width: 0;
  max-width: min(780px, 100%);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--surface);
  box-shadow: 0 8px 20px rgba(30, 45, 60, 0.05);
}

.message.own .message-body {
  border-color: rgba(27, 111, 184, 0.28);
  background: #eef6fd;
}

.message.deleted .message-body {
  background: var(--surface-soft);
}

.message-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  margin-bottom: 7px;
}

.message-head strong {
  overflow: hidden;
  color: var(--muted-strong);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-head time {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.message-actions {
  opacity: 0;
  transition: opacity 0.15s ease;
}

.message-body:hover .message-actions,
.message-actions:focus-within {
  opacity: 1;
}

.message-text {
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.message-text.muted,
.forwarded-label {
  color: var(--muted);
}

.forwarded-label {
  margin-bottom: 6px;
  font-size: 12px;
  font-weight: 800;
}

.attachment-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.attachment {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.attachment-preview {
  display: grid;
  width: 100%;
  min-height: 118px;
  max-height: 260px;
  place-items: center;
  border: 0;
  color: var(--primary);
  background: var(--surface-soft);
  overflow: hidden;
}

.attachment-image .attachment-preview {
  background: #0e1620;
}

.attachment-preview img,
.attachment-preview video {
  display: block;
  width: 100%;
  max-height: 260px;
  object-fit: contain;
}

.attachment-preview pre {
  width: 100%;
  max-height: 200px;
  margin: 0;
  overflow: hidden;
  padding: 12px;
  color: var(--muted-strong);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  line-height: 1.45;
  text-align: left;
  white-space: pre-wrap;
}

.attachment-info {
  display: grid;
  gap: 2px;
  min-width: 0;
  padding: 9px 10px 0;
}

.attachment-info strong,
.attachment-info span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-info strong {
  font-size: 13px;
}

.attachment-info span {
  color: var(--muted);
  font-size: 12px;
}

.attachment-actions {
  padding: 8px 10px 10px;
}

.text-button {
  border: 0;
  border-radius: 6px;
  padding: 6px 8px;
  color: var(--primary-dark);
  background: rgba(27, 111, 184, 0.08);
  font-size: 12px;
  font-weight: 750;
  text-decoration: none;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.empty-state h3 {
  color: var(--muted-strong);
  font-size: 18px;
}

.empty-state p {
  margin: 8px 0 0;
}

.empty-state[hidden],
[hidden] {
  display: none !important;
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 18px 16px;
}

.composer-main,
.selected-files {
  max-width: 1040px;
  margin: 0 auto;
}

.composer-main {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

textarea,
input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  background: #fff;
  outline: none;
  transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

input {
  height: 38px;
  padding: 0 10px;
}

textarea {
  max-height: 180px;
  min-height: 42px;
  resize: none;
  padding: 11px 12px;
  line-height: 1.45;
}

input:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(27, 111, 184, 0.13);
}

.selected-files {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.selected-file {
  display: grid;
  min-width: 190px;
  max-width: min(320px, 100%);
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px;
  background: var(--surface-soft);
}

.selected-file strong,
.selected-file span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selected-file strong {
  font-size: 13px;
}

.selected-file span {
  color: var(--muted);
  font-size: 12px;
}

.dialog {
  width: min(560px, calc(100vw - 28px));
  border: 0;
  border-radius: 8px;
  padding: 0;
  color: var(--text);
  background: transparent;
}

.dialog::backdrop {
  background: rgba(15, 25, 35, 0.42);
}

.dialog-card,
.viewer-card {
  display: grid;
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  background: #fff;
  box-shadow: var(--shadow);
}

.dialog-card header,
.viewer-card header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-card h2,
.viewer-card h2 {
  margin: 0;
  font-size: 17px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.dialog-card footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.split-footer {
  display: grid !important;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
}

.fingerprint {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  background: var(--surface-soft);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  word-break: break-all;
}

.target-list {
  display: grid;
  max-height: 360px;
  gap: 8px;
  overflow: auto;
}

.target-group-title {
  margin-top: 4px;
  color: var(--muted-strong);
  font-size: 12px;
  font-weight: 800;
}

.target-option {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px;
  color: var(--text);
  background: var(--surface-soft);
}

.target-option input {
  width: auto;
  height: auto;
}

.target-option span {
  min-width: 0;
}

.target-option strong,
.target-option small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.target-option small {
  color: var(--muted);
}

.viewer-dialog {
  width: min(920px, calc(100vw - 28px));
}

.viewer-card {
  max-height: calc(100vh - 56px);
}

.viewer-body {
  display: grid;
  min-height: 160px;
  place-items: center;
  overflow: auto;
  border-radius: 8px;
  background: var(--surface-soft);
}

.viewer-body img,
.viewer-body video {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 170px);
  object-fit: contain;
}

.viewer-body audio {
  width: min(680px, 100%);
  margin: 24px;
}

.viewer-body pre {
  width: 100%;
  max-height: calc(100vh - 170px);
  margin: 0;
  overflow: auto;
  padding: 16px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(360px, calc(100vw - 36px));
  transform: translateY(16px);
  border-radius: 8px;
  padding: 11px 13px;
  color: #fff;
  background: #1f2c3a;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

@media (max-width: 940px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 42vh) minmax(0, 1fr);
  }

  .sidebar {
    grid-template-rows: auto auto minmax(0, 1fr);
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .nav-section:last-child {
    display: none;
  }
}

@media (max-width: 620px) {
  .sidebar,
  .messages-wrap {
    padding: 12px;
  }

  .chat-header {
    align-items: flex-start;
    padding: 12px;
  }

  .header-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .composer {
    padding: 10px 12px 12px;
  }

  .composer-main {
    grid-template-columns: 40px minmax(0, 1fr);
  }

  .composer-main .button {
    grid-column: 1 / -1;
    width: 100%;
  }

  .message,
  .message.own {
    grid-template-columns: 34px minmax(0, 1fr);
  }

  .message.own .message-avatar {
    grid-column: 1;
  }

  .message.own .message-body {
    grid-column: 2;
    justify-self: stretch;
  }

  .message-head {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .message-actions {
    grid-column: 1 / -1;
    opacity: 1;
  }

  .attachment-grid {
    grid-template-columns: 1fr;
  }
}
