:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --rail: #f5f7fb;
  --border: #e6ebf5;
  --text: #343a40;
  --muted: #7a7f9a;
  --primary: #7269ef;
  --primary-soft: rgba(114, 105, 239, 0.12);
  --primary-dark: #6159da;
  --customer-bg: #e6ebf5;
  --agent-bg: #7269ef;
  --online: #06d6a0;
  --danger: #ef476f;
  --shadow: 0 2px 4px rgba(15, 34, 58, 0.04);
  --radius: 10px;
  --font: "Public Sans", "Segoe UI", system-ui, sans-serif;
  --rail-w: 72px;
  --inbox-w: 340px;
  --surface: #ffffff;
  --surface-hover: #f0f2f8;
  --chip-bg: #e6ebf5;
  --system-bg: #eeedfe;
  --system-text: #7269ef;
  --transcript-bg: #f5f7fb;
  --transcript-bubble: #ffffff;
  --btn-surface: #ffffff;
  --badge-muted: #98a0b3;
  --on-primary: #ffffff;
  --avatar-ring: #ffffff;
  --logo-bg: #7269ef;
}

html[data-theme="light-sand"] {
  --bg: #f3eee4;
  --panel: #faf6ee;
}

html[data-theme="light-ice"] {
  --bg: #e6edf5;
  --panel: #f2f6fb;
}

html[data-theme^="dark"] {
  color-scheme: dark;
  --bg: #323c4d;
  --panel: #1d2636;
  --rail: #162030;
  --border: #29354a;
  --text: #e8edf5;
  --muted: #9aa6b8;
  --primary: #5c6bc0;
  --primary-soft: rgba(92, 107, 192, 0.22);
  --primary-dark: #7986cb;
  --customer-bg: #3a4558;
  --agent-bg: #3f51b5;
  --online: #26c281;
  --danger: #f06292;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
  --surface: #283142;
  --surface-hover: #2f3a4d;
  --chip-bg: #3a4558;
  --system-bg: #3f4a63;
  --system-text: #c5cae9;
  --transcript-bg: #283142;
  --transcript-bubble: #323c4d;
  --btn-surface: #283142;
  --badge-muted: #5a6578;
  --on-primary: #ffffff;
  --avatar-ring: #1d2636;
  --logo-bg: #162030;
}

html[data-theme="dark-midnight"] {
  --bg: #1e2838;
  --panel: #121820;
}

html[data-theme="dark-graphite"] {
  --bg: #3c3f45;
  --panel: #26282c;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}

button,
input,
textarea {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: var(--rail-w) var(--inbox-w) 1fr;
  height: 100%;
  min-height: 0;
}

/* —— Rail —— */
.rail {
  background: var(--rail);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem 0.5rem;
  gap: 1rem;
}

.rail__logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--logo-bg);
  color: var(--on-primary);
  box-shadow: none;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  user-select: none;
}

.rail__nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.rail__btn {
  position: relative;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: 0.15s ease;
}

.rail__btn:hover {
  background: var(--surface-hover);
  color: var(--primary);
}

.rail__btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}

.rail__badge {
  position: absolute;
  top: 6px;
  right: 6px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--danger);
  color: var(--on-primary);
  font-size: 0.65rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.rail__foot {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

/* —— Inbox —— */
.inbox {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.inbox__head {
  padding: 1.25rem 1.25rem 0.75rem;
}

.inbox__shop {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.inbox__shop-avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--primary);
  color: var(--on-primary);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.95rem;
  flex-shrink: 0;
  overflow: hidden;
}

.inbox__shop-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.inbox__shop-text {
  min-width: 0;
}

.inbox__shop-text h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inbox__subtitle {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
}

.inbox__tenant {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.72rem;
  word-break: break-all;
}

.inbox__search {
  margin: 0.5rem 1rem 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--chip-bg);
  border-radius: 999px;
  padding: 0.55rem 0.9rem;
  color: var(--muted);
}

.inbox__search input {
  border: 0;
  background: transparent;
  outline: none;
  width: 100%;
  color: var(--text);
}

.inbox__tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
}

.inbox__tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 0.7rem 0.55rem 0.75rem;
  cursor: pointer;
}

.inbox__tab:hover {
  color: var(--text);
}

.inbox__tab.active {
  color: var(--primary);
}

.inbox__tab.active::after {
  content: "";
  position: absolute;
  left: 0.35rem;
  right: 0.35rem;
  bottom: -1px;
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: var(--primary);
}

.inbox__tab-badge {
  min-width: 1.35rem;
  height: 1.35rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  color: var(--on-primary);
  background: var(--badge-muted);
}

.inbox__tab.active .inbox__tab-badge:not(#badgeOpen) {
  background: var(--primary);
}

#badgeOpen.inbox__tab-badge--busy {
  background: var(--primary);
}

#badgeOpen {
  background: var(--badge-muted);
}

.inbox__section-label {
  padding: 0.35rem 1.25rem 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

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

.conv-item {
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.75rem 0.7rem;
  border-radius: 12px;
  cursor: pointer;
  color: inherit;
}

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

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

.conv-item__body {
  min-width: 0;
}

.conv-item__name {
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-item__preview {
  margin-top: 0.15rem;
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-item__time {
  color: var(--muted);
  font-size: 0.72rem;
  white-space: nowrap;
  align-self: start;
  padding-top: 0.15rem;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--on-primary);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.9rem;
  position: relative;
  flex-shrink: 0;
}

.avatar--lg {
  width: 42px;
  height: 42px;
}

.avatar__dot {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--online);
  border: 2px solid var(--avatar-ring);
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.9rem;
}

/* —— Chat —— */
.chat {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.chat-empty {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 2rem;
}

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

.chat-empty__card {
  text-align: center;
  max-width: 360px;
}

.chat-empty h2 {
  margin: 0 0 0.5rem;
  font-size: 1.35rem;
  font-weight: 700;
}

.chat-empty p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

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

.chat__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.chat__peer {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.chat__peer h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.chat__status {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--online);
  display: inline-block;
}

.messages {
  flex: 1;
  overflow: auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.msg {
  display: flex;
  gap: 0.65rem;
  max-width: min(520px, 85%);
  width: fit-content;
  align-self: flex-start;
}

.msg--agent {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg--system {
  align-self: center;
  max-width: 90%;
  justify-content: center;
}

.msg__bubble {
  width: fit-content;
  max-width: 100%;
  padding: 0.55rem 0.8rem 0.45rem;
  border-radius: 14px;
  line-height: 1.4;
  box-shadow: var(--shadow);
}

.msg__text {
  display: block;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg--customer .msg__bubble {
  background: var(--customer-bg);
  border-top-left-radius: 4px;
}

.msg--agent .msg__bubble {
  background: var(--agent-bg);
  color: var(--on-primary);
  border-top-right-radius: 4px;
}

.msg--system .msg__bubble,
.msg__bubble--system {
  background: var(--system-bg);
  box-shadow: none;
  color: var(--system-text);
  font-size: 0.8rem;
  font-weight: 500;
  text-align: center;
  padding: 0.45rem 0.95rem;
  border-radius: 999px;
  white-space: nowrap;
}

.day-separator {
  align-self: center;
  display: flex;
  justify-content: center;
  margin: 0.25rem 0 0.15rem;
}

.day-separator span {
  background: var(--chip-bg);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

.msg__meta {
  display: block;
  margin-top: 0.3rem;
  font-size: 0.7rem;
  opacity: 0.75;
}

.msg--transcript {
  max-width: min(640px, 92%);
  width: min(640px, 92%);
}

.msg--transcript .msg__bubble {
  width: 100%;
  max-width: none;
  padding: 0.75rem 0.85rem 0.65rem;
  background: var(--transcript-bg);
  border: 1px solid var(--border);
}

.transcript {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.transcript__header {
  text-align: center;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--muted);
}

.transcript__thread {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.transcript__row {
  display: flex;
  width: 100%;
}

.transcript__row--cliente {
  justify-content: flex-start;
}

.transcript__row--bot {
  justify-content: flex-end;
}

.transcript__bubble {
  width: fit-content;
  max-width: 88%;
  padding: 0.4rem 0.65rem 0.45rem;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
  box-shadow: var(--shadow);
}

.transcript__row--cliente .transcript__bubble {
  background: var(--transcript-bubble);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
}

.transcript__row--bot .transcript__bubble {
  background: var(--primary-soft);
  color: var(--text);
  border: 1px solid rgba(92, 107, 192, 0.35);
  border-top-right-radius: 4px;
}

.transcript__who {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted);
}

.transcript__row--bot .transcript__who {
  color: var(--primary-dark);
  align-self: flex-end;
}

.transcript__text {
  font-size: 0.9rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
  align-items: end;
  padding: 0.85rem 1.1rem 1.1rem;
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.composer textarea {
  resize: none;
  min-height: 44px;
  max-height: 140px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.7rem 0.9rem;
  background: var(--chip-bg);
  color: var(--text);
  outline: none;
}

.composer textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.btn {
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  padding: 0.65rem 1rem;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--outline {
  background: var(--btn-surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.btn--outline:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}

.btn--send {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  background: var(--primary);
  color: var(--on-primary);
  border-radius: 12px;
}

.btn--send:hover:not(:disabled) {
  background: var(--primary-dark);
}

.app.is-settings .inbox,
.app.is-settings .chat {
  display: none !important;
}

/* —— Settings page —— */
.settings-page {
  grid-column: 2 / -1;
  min-height: 0;
  overflow: auto;
  background: var(--bg);
}

.settings-page[hidden] {
  display: none !important;
}

.settings-page__inner {
  width: min(1100px, 100%);
  margin: 0;
  padding: 2rem 1.5rem 3rem;
}

.settings-page__head h1 {
  margin: 0 0 0.35rem;
  font-size: 1.5rem;
  font-weight: 700;
}

.settings-page__lead {
  margin: 0 0 1.75rem;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.45;
}

.settings-section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.25rem 1.25rem 1.35rem;
  box-shadow: var(--shadow);
  min-width: 0;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  align-items: start;
}

.settings-section h2 {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.settings-section__lead {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.settings-section__body {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.92rem;
}

.settings-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.92rem;
  padding: 0.35rem 0.15rem;
}

.settings-field select,
.settings-field input[type="range"] {
  width: 100%;
}

.settings-field select {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
}

.settings-field em {
  font-style: normal;
  color: var(--muted);
  font-weight: 600;
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.settings-hint {
  margin: 0.25rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  min-height: 1.2em;
}

.theme-picker {
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
}

.theme-picker__label {
  margin: 0 0 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.theme-picker__grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.7rem;
}

.theme-card {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.45rem;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.theme-card:hover {
  border-color: var(--primary-soft);
}

.theme-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.theme-card.is-selected,
.theme-card[aria-checked="true"] {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.theme-card__preview {
  display: grid;
  grid-template-columns: 10px 32% 1fr;
  height: 68px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid rgba(15, 34, 58, 0.08);
}

.theme-card__rail,
.theme-card__panel,
.theme-card__chat {
  display: block;
  min-width: 0;
}

.theme-card__name {
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0 0.15rem 0.1rem;
}

.theme-card[data-theme="light"] {
  --preview-rail: #f5f7fb;
  --preview-panel: #ffffff;
  --preview-bg: #f5f7fb;
}

.theme-card[data-theme="light-sand"] {
  --preview-rail: #f0f2f7;
  --preview-panel: #faf6ee;
  --preview-bg: #f3eee4;
}

.theme-card[data-theme="light-ice"] {
  --preview-rail: #f0f2f7;
  --preview-panel: #f2f6fb;
  --preview-bg: #e6edf5;
}

.theme-card[data-theme="dark"] {
  --preview-rail: #162030;
  --preview-panel: #1d2636;
  --preview-bg: #323c4d;
}

.theme-card[data-theme="dark-midnight"] {
  --preview-rail: #162030;
  --preview-panel: #121820;
  --preview-bg: #1e2838;
}

.theme-card[data-theme="dark-graphite"] {
  --preview-rail: #162030;
  --preview-panel: #26282c;
  --preview-bg: #3c3f45;
}

.theme-card__rail {
  background: var(--preview-rail);
}

.theme-card__panel {
  background: var(--preview-panel);
}

.theme-card__chat {
  background: var(--preview-bg);
}

@media (max-width: 960px) {
  .app {
    grid-template-columns: var(--rail-w) 1fr;
  }
  .inbox {
    display: none;
  }
  .app.show-inbox .inbox {
    display: flex;
    position: absolute;
    inset: 0 0 0 var(--rail-w);
    z-index: 5;
  }
  .app.show-inbox .chat {
    display: none;
  }
  .app.is-settings .inbox,
  .app.is-settings.show-inbox .inbox,
  .app.is-settings .chat {
    display: none !important;
  }
  .settings-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  :root {
    --rail-w: 60px;
  }
}
