:root {
  --bg: #08090c;
  --surface: #101218;
  --surface-hover: #151821;
  --surface-active: #191c26;
  --text: #f2f3f5;
  --text-secondary: #9da3ad;
  --text-muted: #6f7580;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.12);
  --accent: #8b7cff;
  --accent-soft: rgba(139, 124, 255, 0.12);
  --danger: #d98989;
  --radius: 0.75rem;
  --sidebar: 320px;
  --info: 280px;
  --font: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow-x: clip;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9375rem;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

html {
  overflow-x: clip;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

a {
  color: inherit;
  text-decoration: none;
}

:focus {
  outline: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.skip-link {
  position: absolute;
  left: 1rem;
  top: 0.75rem;
  z-index: 40;
  padding: 0.4rem 0.75rem;
  border-radius: 0.5rem;
  background: var(--surface);
  transform: translateY(-160%);
}

.skip-link:focus {
  transform: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

.app[data-info="open"] {
  grid-template-columns: var(--sidebar) minmax(0, 1fr) var(--info);
}

.sidebar,
.chat-pane,
.info-panel {
  min-width: 0;
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1rem 0.65rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 500;
  letter-spacing: -0.03em;
}

.brand img {
  width: 28px;
  height: 28px;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border: 1px solid transparent;
  border-radius: 0.6rem;
  background: transparent;
  color: var(--text-secondary);
}

.icon-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.search {
  padding: 0.35rem 1rem 0.75rem;
}

.search input {
  width: 100%;
  height: 2.4rem;
  padding: 0 0.8rem;
  border: 1px solid var(--border);
  border-radius: 0.65rem;
  background: var(--surface);
}

.search input::placeholder {
  color: var(--text-muted);
}

.section-title {
  margin: 0;
  padding: 0.2rem 1rem 0.45rem;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.chat-list {
  flex: 1;
  margin: 0;
  padding: 0 0.55rem 0.55rem;
  overflow: auto;
  list-style: none;
}

.chat-item {
  display: grid;
  grid-template-columns: 2.5rem minmax(0, 1fr) auto;
  column-gap: 0.7rem;
  align-items: start;
  width: 100%;
  margin-bottom: 0.15rem;
  padding: 0.65rem 0.6rem;
  border: 0;
  border-radius: 0.7rem;
  background: transparent;
  text-align: left;
}

.chat-item:hover {
  background: var(--surface-hover);
}

.chat-item.is-active {
  background: var(--surface-active);
}

.chat-item .meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 0.18rem;
}

.chat-item .name {
  min-width: 0;
  overflow: hidden;
  color: var(--text);
  font-weight: 500;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item .preview,
.chat-item .handle {
  display: block;
  overflow: hidden;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-aside {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.4rem;
  min-width: 2.75rem;
}

.chat-item .time {
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 0.72rem;
  line-height: 1.4;
  white-space: nowrap;
}

.unread {
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--accent);
}

.avatar {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 500;
}

.avatar.lg {
  width: 3.5rem;
  height: 3.5rem;
  margin: 0 auto 0.7rem;
  font-size: 1rem;
}

.profile-btn {
  display: grid;
  grid-template-columns: 2.5rem minmax(0, 1fr);
  gap: 0.7rem;
  align-items: center;
  width: 100%;
  padding: 0.85rem 1rem;
  border: 0;
  border-top: 1px solid var(--border);
  background: transparent;
  text-align: left;
}

.profile-btn:hover {
  background: var(--surface-hover);
}

.profile-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.profile-name {
  font-weight: 500;
}

.profile-user {
  color: var(--text-secondary);
  font-size: 0.8125rem;
}

.chat-pane {
  display: grid;
  position: relative;
  background: var(--bg);
}

.empty-state,
.conversation {
  min-height: 0;
}

.empty-state {
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 0.35rem;
  padding: 2rem 1.5rem;
  text-align: center;
}

.empty-state h1 {
  margin: 0.55rem 0 0;
  font-size: 1.35rem;
  font-weight: 500;
  letter-spacing: -0.03em;
}

.empty-kicker,
.empty-state p {
  margin: 0;
  color: var(--text-secondary);
}

.empty-state p {
  max-width: 18rem;
}

.conversation {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100%;
}

.chat-header,
.panel-header {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-height: 3.6rem;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
}

.chat-heading {
  min-width: 0;
  margin-right: auto;
}

.chat-heading h2,
.panel-header h2,
.modal h2,
.settings-section h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  letter-spacing: -0.02em;
}

.chat-heading p,
.info-user {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.8125rem;
}

.back-btn {
  display: none;
}

.e2e-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  margin: 0;
  color: var(--text-muted);
  font-size: 0.75rem;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  padding: 1.1rem 1.15rem 1.25rem;
  overflow: auto;
}

.message-list::before {
  content: "";
  flex: 1 0 auto;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: min(68%, 34rem);
}

.message.is-in {
  align-self: flex-start;
}

.message.is-out {
  align-self: flex-end;
  align-items: flex-end;
}

.bubble {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.message.is-out .bubble {
  background: var(--accent-soft);
  border-color: transparent;
}

.meta-line {
  margin: 0.28rem 0.15rem 0;
  color: var(--text-muted);
  font-size: 0.7rem;
}

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 0.45rem;
  padding: 0.7rem 0.8rem 0.85rem;
  border-top: 1px solid var(--border);
}

.composer textarea {
  width: 100%;
  min-height: 2.4rem;
  max-height: 7.5rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 0.7rem;
  background: var(--surface);
  resize: none;
}

.composer-note {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--text-muted);
  font-size: 0.75rem;
}

.send-btn {
  color: var(--text);
}

.info-panel {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--bg);
  overflow: auto;
}

.info-identity {
  padding: 1.15rem 1rem 0.5rem;
  text-align: center;
}

.info-name {
  margin: 0;
  font-weight: 500;
}

.info-block,
.info-actions {
  padding: 0.75rem 1rem;
}

.info-block {
  border-top: 1px solid var(--border);
}

.info-block h3 {
  margin: 0 0 0.55rem;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.info-row {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0 0 0.4rem;
  line-height: 1.45;
}

.note,
.lede {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  line-height: 1.5;
}

.check {
  color: var(--text-secondary);
}

.info-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.65rem;
  margin-top: auto;
}

.text-danger {
  padding: 0;
  border: 0;
  background: none;
  color: var(--danger);
}

#overlay-pairing {
  z-index: 24;
}

#overlay-settings {
  z-index: 22;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 9, 12, 0.72);
}

.overlay[hidden],
.info-panel[hidden],
.conversation[hidden],
.empty-state[hidden],
.settings-section[hidden],
.scan-placeholder[hidden],
.composer-note[hidden] {
  display: none;
}

.modal {
  width: min(26rem, calc(100vw - 32px));
  max-width: calc(100vw - 32px);
  max-height: calc(100dvh - 20px);
  overflow: auto;
  overflow-x: clip;
  padding: 1.15rem 1.15rem 1.2rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.settings-modal {
  width: min(900px, calc(100vw - 32px));
  max-width: 900px;
}

.modal .panel-header,
.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 48px;
  padding: 0 0 0.75rem;
  border: 0;
}

.settings-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  letter-spacing: -0.02em;
}

.close-btn {
  width: 32px;
  height: 32px;
}

.modal label {
  display: block;
  margin: 0.7rem 0 0.35rem;
  color: var(--text-secondary);
  font-size: 0.8125rem;
}

.username-field {
  display: flex;
  align-items: center;
  height: 2.5rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.65rem;
  background: #0c0d12;
}

.username-field span {
  padding: 0 0 0 0.8rem;
  color: var(--text-muted);
}

.username-field input {
  width: 100%;
  height: 100%;
  padding: 0 0.7rem 0 0.2rem;
  border: 0;
  background: transparent;
}

.qr-alt {
  margin: 1rem 0 0.65rem;
  text-align: center;
}

.btn-secondary,
.btn-ghost {
  width: 100%;
  min-height: 2.5rem;
  margin-top: 0.55rem;
  border-radius: 0.65rem;
}

.btn-secondary {
  border: 1px solid var(--border-strong);
  background: #181b24;
}

.btn-ghost {
  border: 0;
  background: transparent;
  color: var(--text-secondary);
}

.settings-layout {
  display: grid;
  grid-template-columns: 10.5rem minmax(0, 1fr);
  gap: 1.25rem;
  min-width: 0;
}

.settings-switcher {
  min-width: 0;
}

.settings-current {
  display: none;
}

.settings-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.settings-nav button {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 0;
  border-radius: 0.5rem;
  background: transparent;
  color: var(--text-secondary);
  line-height: 1.4;
  text-align: left;
}

.settings-nav button.is-active,
.settings-nav button:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.settings-body,
.settings-section {
  min-width: 0;
}

.settings-section h3 {
  margin: 0 0 14px;
  font-size: 1rem;
  font-weight: 500;
  letter-spacing: -0.02em;
  line-height: 1.4;
}

.settings-section .info-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px 16px;
  align-items: start;
  margin: 0;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  line-height: 1.45;
}

.settings-section .info-row span:last-child {
  color: var(--text);
  text-align: right;
}

.settings-section .note {
  margin-top: 8px;
}

.device-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.device-list li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 16px;
  align-items: start;
  min-height: 64px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.device-name {
  grid-column: 1;
  grid-row: 1;
  font-weight: 500;
  line-height: 1.4;
}

.status-dot {
  grid-column: 2;
  grid-row: 1;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  line-height: 1.4;
  white-space: nowrap;
}

.device-desc {
  grid-column: 1;
  grid-row: 2;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  line-height: 1.45;
}

.btn-add-device {
  width: 100%;
  height: 44px;
  min-height: 44px;
  margin-top: 22px;
  border: 1px solid var(--border-strong);
  background: var(--surface-hover);
}

.qr-frame {
  display: grid;
  place-items: center;
  width: 11rem;
  aspect-ratio: 1;
  margin: 0.85rem auto;
  padding: 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.85rem;
  background: #0b0c10;
}

.qr-frame.lg {
  width: min(100%, 14.5rem);
  cursor: pointer;
}

.qr-svg {
  width: 100%;
  height: 100%;
}

.qr-svg rect {
  fill: #f2f3f5;
}

.scan-placeholder {
  margin-top: 0.75rem;
  text-align: center;
}

.pair-message {
  margin: 0.35rem 0 0.7rem;
  text-align: center;
}

.pair-status {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem 0.8rem;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--text-muted);
  font-size: 0.75rem;
}

.pair-status .is-done {
  color: var(--text-secondary);
}

.pair-status .is-current {
  color: var(--text);
}

@media (max-width: 800px) {
  .app,
  .app[data-info="open"] {
    grid-template-columns: 1fr;
  }

  .sidebar {
    border-right: 0;
  }

  .app[data-view="list"] .chat-pane,
  .app[data-view="list"] .info-panel {
    display: none;
  }

  .app[data-view="chat"] .sidebar {
    display: none;
  }

  .app[data-view="chat"] .chat-pane {
    display: grid;
  }

  .back-btn {
    display: inline-grid;
  }

  .info-panel {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(100%, 20rem);
    z-index: 8;
    border-right: 0;
    background: var(--surface);
  }

  .app[data-view="chat"][data-info="open"] .info-panel {
    right: 0;
    left: auto;
    border-left: 1px solid var(--border);
  }

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

@media (max-width: 700px) {
  .overlay {
    padding: 10px;
  }

  .modal,
  .settings-modal {
    width: calc(100vw - 20px);
    max-width: none;
    max-height: calc(100dvh - 20px);
  }

  .settings-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
  }

  .settings-current {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
    min-height: 44px;
    padding: 0.65rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 0.65rem;
    background: var(--surface-hover);
    color: var(--text);
    text-align: left;
  }

  .settings-current .chevron {
    flex-shrink: 0;
    color: var(--text-secondary);
  }

  .settings-switcher.is-open .chevron {
    transform: rotate(180deg);
  }

  .settings-nav {
    display: none;
    margin-top: 0.45rem;
    padding: 0.25rem;
    border: 1px solid var(--border);
    border-radius: 0.65rem;
    background: #0c0d12;
  }

  .settings-switcher.is-open .settings-nav {
    display: flex;
    flex-direction: column;
  }

  .device-list li {
    min-height: 68px;
  }
}

@media (max-width: 480px) {
  .sidebar-header,
  .search,
  .profile-btn {
    padding-left: 0.8rem;
    padding-right: 0.8rem;
  }

  .modal {
    padding: 1rem 0.9rem 1.05rem;
  }

  .pair-status {
    flex-direction: column;
    align-items: center;
  }
}

@media (max-width: 360px) {
  .chat-item .time {
    font-size: 0.68rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
