:root {
  --mobile-app-height: 100dvh;
  --app-bg-top: #00a884;
  --app-bg-bottom: #111b21;
  --sidebar-bg: #111b21;
  --sidebar-top: #202c33;
  --sidebar-border: #222d34;
  --contact-hover: #202c33;
  --contact-active: #2a3942;
  --contact-muted: #8696a0;
  --search-bg: #202c33;
  --search-input: #111b21;
  --chip-bg: #111b21;
  --chip-border: #2a3942;
  --chip-active-bg: rgba(0, 168, 132, 0.18);
  --chip-active-text: #00d5a1;
  --chat-top: #202c33;
  --chat-bg: #0b141a;
  --chat-bg-pattern: rgba(255, 255, 255, 0.035);
  --chat-footer: #202c33;
  --text: #e9edef;
  --text-strong: #ffffff;
  --text-muted: #8696a0;
  --accent: #00a884;
  --ai-bubble: #202c33;
  --human-bubble: #005c4b;
  --shadow: 0 12px 34px rgba(0, 0, 0, 0.28);
  --scroll: rgba(134, 150, 160, 0.36);
}

[data-theme="light"] {
  --app-bg-top: #d9dbd6;
  --app-bg-bottom: #f0f2f5;
  --sidebar-bg: #ffffff;
  --sidebar-top: #f0f2f5;
  --sidebar-border: #e9edef;
  --contact-hover: #f5f6f6;
  --contact-active: #f0f2f5;
  --contact-muted: #667781;
  --search-bg: #f0f2f5;
  --search-input: #ffffff;
  --chip-bg: #ffffff;
  --chip-border: #d1d7db;
  --chip-active-bg: rgba(0, 168, 132, 0.12);
  --chip-active-text: #017561;
  --chat-top: #f0f2f5;
  --chat-bg: #efeae2;
  --chat-bg-pattern: rgba(17, 27, 33, 0.03);
  --chat-footer: #f0f2f5;
  --text: #111b21;
  --text-strong: #111b21;
  --text-muted: #667781;
  --accent: #00a884;
  --ai-bubble: #ffffff;
  --human-bubble: #d9fdd3;
  --shadow: 0 8px 24px rgba(17, 27, 33, 0.12);
  --scroll: rgba(102, 119, 129, 0.34);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "Inter", "Segoe UI", sans-serif;
  color: var(--text);
  background:
    linear-gradient(180deg, var(--app-bg-top) 0 120px, var(--app-bg-bottom) 120px 100%);
}

body {
  min-height: 100vh;
  padding: 18px;
}

button,
input {
  font: inherit;
}

.app-shell {
  height: calc(100vh - 36px);
  display: grid;
  grid-template-columns: 30% 70%;
  overflow: hidden;
  border-radius: 14px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.mobile-statusbar,
.mobile-sidebar-hero,
.mobile-back-button,
.chat-composer {
  display: none;
}

.sidebar,
.chat-panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
}

.sidebar-topbar,
.chat-topbar {
  flex: 0 0 auto;
  background: var(--sidebar-top);
}

.sidebar-topbar,
.chat-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 16px;
}

.brand-lockup,
.chat-contact {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-avatar,
.chat-avatar,
.contact-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex: 0 0 auto;
  font-weight: 600;
}

.brand-avatar,
.chat-avatar {
  width: 40px;
  height: 40px;
  background: #374248;
  color: #f6f8f9;
}

.brand-lockup h1,
.chat-contact-copy h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-strong);
}

.brand-lockup p,
.chat-contact-copy p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-button {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.icon-button svg {
  width: 19px;
  height: 19px;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

.icon-button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
}

.icon-button:disabled {
  cursor: default;
  opacity: 0.8;
}

.search-wrap {
  flex: 0 0 auto;
  margin: 8px 12px 10px;
  padding: 7px 12px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--search-bg);
}

.search-icon {
  color: var(--text-muted);
  font-size: 18px;
}

.search-wrap input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}

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

.sidebar-meta {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 0 16px 10px;
  font-size: 12px;
  color: var(--text-muted);
}

.contact-list,
.message-list {
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.contact-list {
  flex: 1;
}

.contact-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 16px;
  border: 0;
  border-bottom: 1px solid rgba(134, 150, 160, 0.08);
  background: transparent;
  text-align: left;
  color: inherit;
  cursor: pointer;
  transition: background 120ms ease;
}

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

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

.contact-avatar {
  width: 49px;
  height: 49px;
  background: #374248;
  color: #f7fafc;
  font-size: 16px;
}

.contact-content {
  min-width: 0;
  flex: 1;
}

.contact-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.contact-row.secondary {
  margin-top: 5px;
}

.contact-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
  font-weight: 500;
  color: var(--text-strong);
}

.contact-time {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--text-muted);
}

.contact-preview {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  color: var(--contact-muted);
}

.chat-panel {
  position: relative;
  background: var(--chat-bg);
}

.chat-topbar {
  background: var(--chat-top);
  border-left: 1px solid rgba(255, 255, 255, 0.02);
}

.chat-contact-copy {
  min-width: 0;
}

.chat-contact-copy h2,
.chat-contact-copy p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-composer {
  flex: 0 0 auto;
  display: block;
  padding: 12px 18px 16px;
  background: var(--chat-bg);
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  width: 100%;
}

.composer-input {
  flex: 1;
  width: 100%;
  min-width: 0;
  height: 48px;
  min-height: 48px;
  padding: 13px 16px;
  border: 0;
  border-radius: 18px;
  outline: none;
  resize: none;
  overflow-y: hidden;
  display: block;
  font: inherit;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  line-height: 1.4;
  font-size: 14px;
  scrollbar-width: thin;
  scrollbar-color: rgba(134, 150, 160, 0.55) transparent;
  overscroll-behavior: contain;
  overflow-x: hidden;
}

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

.composer-input::-webkit-scrollbar {
  width: 8px;
}

.composer-input::-webkit-scrollbar-track {
  background: transparent;
}

.composer-input::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: rgba(134, 150, 160, 0.55);
}

.composer-input::-webkit-scrollbar-thumb:hover {
  background: rgba(134, 150, 160, 0.76);
}

.composer-input.is-disabled {
  cursor: default;
  opacity: 0.8;
}

.send-button {
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(0, 168, 132, 0.22);
  transition: transform 140ms ease, box-shadow 140ms ease, opacity 140ms ease, background 140ms ease;
  -webkit-tap-highlight-color: transparent;
}

.send-button svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.send-button:disabled {
  cursor: default;
  opacity: 0.45;
  box-shadow: none;
}

.send-button:not(:disabled):hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(0, 168, 132, 0.3);
}

.send-button.is-sending {
  opacity: 0.72;
}

.message-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px 24px;
  position: relative;
  background-color: var(--chat-bg);
  background-image:
    radial-gradient(circle at 25px 25px, var(--chat-bg-pattern) 2px, transparent 2px),
    radial-gradient(circle at 75px 75px, var(--chat-bg-pattern) 2px, transparent 2px),
    radial-gradient(circle at 50px 10px, var(--chat-bg-pattern) 1.5px, transparent 1.5px),
    radial-gradient(circle at 12px 60px, var(--chat-bg-pattern) 1.5px, transparent 1.5px);
  background-size: 100px 100px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: min(72%, 820px);
}

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

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

.bubble {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  line-height: 1.42;
  padding: 8px 10px 7px;
  border-radius: 8px;
  font-size: 14.2px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}

.bubble-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble-time {
  align-self: flex-end;
  min-height: 14px;
  font-size: 11px;
  line-height: 1;
  color: rgba(233, 237, 239, 0.78);
}

[data-theme="light"] .bubble-time {
  color: rgba(17, 27, 33, 0.58);
}

.bubble-time:empty {
  display: none;
}

.message.is-ai .bubble {
  background: var(--ai-bubble);
  color: var(--text);
  border-top-left-radius: 0;
}

.message.is-human .bubble {
  background: var(--human-bubble);
  color: var(--text-strong);
  border-top-right-radius: 0;
}

.message.is-operator .bubble {
  background: #0a6e5b;
}

[data-theme="light"] .message.is-operator .bubble {
  background: #c8f2cf;
}

.date-indicator {
  position: sticky;
  top: 10px;
  z-index: 5;
  align-self: center;
  margin: 0 auto;
  padding: 6px 12px;
  border-radius: 8px;
  background: rgba(17, 27, 33, 0.86);
  color: #e9edef;
  font-size: 12px;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.24);
  opacity: 0;
  transform: translateY(-8px);
  pointer-events: none;
  transition: opacity 140ms ease, transform 140ms ease;
}

.date-indicator.is-visible {
  opacity: 1;
  transform: translateY(0);
}

[data-theme="light"] .date-indicator {
  background: rgba(241, 242, 244, 0.94);
  color: #54656f;
}

[data-theme="light"] .chat-composer {
  background: var(--chat-bg);
  border-top-color: rgba(17, 27, 33, 0.06);
}

[data-theme="light"] .composer-input {
  background: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(17, 27, 33, 0.06);
  scrollbar-color: rgba(102, 119, 129, 0.42) transparent;
}

[data-theme="light"] .composer-input::-webkit-scrollbar-thumb {
  background: rgba(102, 119, 129, 0.42);
}

[data-theme="light"] .composer-input::-webkit-scrollbar-thumb:hover {
  background: rgba(102, 119, 129, 0.62);
}

.message-day-separator {
  align-self: center;
  margin: 6px auto 2px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(17, 27, 33, 0.78);
  color: #f5f7f8;
  font-size: 12px;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.16);
}

[data-theme="light"] .message-day-separator {
  background: rgba(255, 255, 255, 0.92);
  color: #54656f;
  box-shadow: 0 4px 14px rgba(17, 27, 33, 0.08);
}

.empty-state,
.status-card {
  margin: auto;
  max-width: 460px;
  padding: 26px;
  text-align: center;
  border-radius: 16px;
  background: rgba(32, 44, 51, 0.92);
  color: var(--text);
  box-shadow: var(--shadow);
}

[data-theme="light"] .empty-state,
[data-theme="light"] .status-card {
  background: rgba(255, 255, 255, 0.96);
}

.empty-state p,
.status-card p {
  margin: 10px 0 0;
  color: var(--text-muted);
  line-height: 1.5;
}

.contact-list::-webkit-scrollbar,
.message-list::-webkit-scrollbar {
  width: 10px;
}

.contact-list::-webkit-scrollbar-track,
.message-list::-webkit-scrollbar-track {
  background: transparent;
}

.contact-list::-webkit-scrollbar-thumb,
.message-list::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: var(--scroll);
}

.scroll-bottom-button {
  position: absolute;
  right: 24px;
  bottom: 88px;
  width: 42px;
  height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(32, 44, 51, 0.94);
  color: #d9e2e6;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 140ms ease, transform 140ms ease, background 140ms ease;
}

.scroll-bottom-button.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.scroll-bottom-button:hover {
  background: #2a3942;
}

[data-theme="light"] .scroll-bottom-button {
  background: rgba(255, 255, 255, 0.96);
  color: #54656f;
}

[data-theme="light"] .scroll-bottom-button:hover {
  background: #ffffff;
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 380px minmax(0, 1fr);
  }
}

@media (max-width: 900px) {
  body {
    padding: 0;
    background: var(--chat-bg);
    overflow: hidden;
  }

  body.mobile-chat-open {
    background: var(--chat-bg);
  }

  body.keyboard-open {
    background: var(--chat-bg);
  }

  .app-shell {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    height: var(--mobile-app-height);
    display: block;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    background: #0b1014;
    overflow: hidden;
  }

  body.mobile-chat-open .app-shell {
    background: var(--chat-bg);
  }

  body.keyboard-open .app-shell {
    background: var(--chat-bg);
  }

  .sidebar {
    height: var(--mobile-app-height);
    min-height: 0;
    border-right: 0;
    overflow: hidden;
  }

  .chat-panel {
    height: var(--mobile-app-height);
    min-height: 0;
    display: none;
    overflow: hidden;
  }

  body.mobile-chat-open .sidebar {
    display: none;
  }

  body.mobile-chat-open .chat-panel {
    display: flex;
  }

  .message {
    max-width: 86%;
  }

  .sidebar-topbar {
    padding: calc(env(safe-area-inset-top, 0px) + 12px) 18px 6px;
    min-height: 58px;
    align-items: flex-start;
    background: var(--sidebar-bg);
  }

  .brand-lockup {
    display: none;
  }

  .topbar-actions {
    margin-left: auto;
    gap: 12px;
  }

  .icon-button {
    width: 38px;
    height: 38px;
    border-radius: 19px;
    background: transparent;
    color: var(--text-strong);
    font-size: 18px;
    transition: transform 150ms ease, background 150ms ease, color 150ms ease;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-sidebar-hero {
    display: block;
    padding: 6px 18px 14px;
    background: var(--sidebar-bg);
  }

  .mobile-sidebar-hero h2 {
    margin: 0;
    font-size: 58px;
    line-height: 0.96;
    color: var(--text-strong);
    letter-spacing: -0.04em;
    font-weight: 700;
  }

  .mobile-sidebar-hero p {
    margin: 8px 0 0;
    color: var(--text-muted);
    font-size: 14px;
  }

  .search-wrap {
    margin: 0 18px 14px;
    padding: 12px 16px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.09);
  }

  .search-wrap input {
    font-size: 16px;
  }

  .sidebar-meta {
    display: none;
  }

  .contact-list {
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }

  .contact-item {
    padding: 15px 18px;
    gap: 14px;
    border-bottom-color: rgba(255, 255, 255, 0.06);
    transition: transform 150ms ease, background 150ms ease;
    -webkit-tap-highlight-color: transparent;
  }

  .contact-avatar {
    width: 58px;
    height: 58px;
    font-size: 19px;
  }

  .contact-name {
    font-size: 18px;
    font-weight: 600;
  }

  .contact-preview {
    font-size: 15px;
    color: rgba(233, 237, 239, 0.62);
  }

  .chat-panel {
    background: var(--chat-bg);
    transition: transform 180ms ease;
    will-change: transform;
  }

  .chat-panel.is-swipe-gesturing {
    transition: none;
  }

  .chat-topbar {
    padding: calc(env(safe-area-inset-top, 0px) + 4px) 14px 8px;
    min-height: 66px;
    background: #47524c;
    align-items: center;
  }

  .mobile-back-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    margin-right: 0;
    border: 0;
    background: transparent;
    color: var(--text-strong);
    padding: 0;
    cursor: pointer;
    transition: transform 150ms ease, opacity 150ms ease;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-back-button svg {
    width: 24px;
    height: 24px;
    stroke: currentColor;
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    fill: none;
  }

  .chat-avatar {
    width: 42px;
    height: 42px;
    font-size: 16px;
  }

  .chat-contact-copy h2 {
    font-size: 17px;
    font-weight: 600;
    line-height: 1.1;
  }

  .chat-contact-copy p {
    margin-top: 3px;
    font-size: 12px;
    color: rgba(233, 237, 239, 0.7);
  }

  .message-list {
    padding: 14px 8px 14px;
    background-size: 78px 78px;
  }

  .bubble {
    font-size: 16px;
    padding: 9px 11px 8px;
    box-shadow: none;
  }

  .bubble-time {
    font-size: 12px;
  }

  .message.is-ai .bubble::before,
  .message.is-human .bubble::before {
    content: "";
    position: absolute;
    top: 0;
    width: 12px;
    height: 13px;
    background: inherit;
  }

  .message.is-ai .bubble::before {
    left: -5px;
    clip-path: polygon(100% 0, 100% 100%, 0 0);
  }

  .message.is-human .bubble::before {
    right: -5px;
    clip-path: polygon(0 0, 100% 0, 0 100%);
  }

  .date-indicator {
    top: 8px;
  }

  .message-day-separator {
    margin-top: 8px;
    margin-bottom: 4px;
    background: rgba(17, 27, 33, 0.8);
  }

  .scroll-bottom-button {
    right: 16px;
    bottom: calc(82px + env(safe-area-inset-bottom, 0px));
  }

  .icon-button:active,
  .mobile-back-button:active,
  .scroll-bottom-button:active {
    transform: scale(0.92);
  }

  .contact-item:active {
    transform: scale(0.985);
  }

  .chat-composer {
    display: block;
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--chat-bg);
  }

  body.keyboard-open .chat-composer {
    padding: 8px 10px 4px;
  }

  body.keyboard-open .message-list {
    padding-bottom: 8px;
  }

  .composer-form {
    gap: 10px;
    align-items: flex-end;
  }

  .composer-input {
    min-height: 44px;
    padding: 11px 15px;
    border-radius: 22px;
    background: rgba(255, 255, 255, 0.09);
    font-size: 15px;
  }

  .send-button {
    width: 44px;
    height: 44px;
    box-shadow: none;
  }

  .empty-state,
  .status-card {
    margin: auto 14px;
  }

  [data-theme="light"] body {
    background: var(--chat-bg);
  }

  [data-theme="light"] body.mobile-chat-open {
    background: var(--chat-bg);
  }

  [data-theme="light"] body.keyboard-open {
    background: var(--chat-bg);
  }

  [data-theme="light"] .app-shell {
    background: var(--chat-bg);
  }

  [data-theme="light"] body.mobile-chat-open .app-shell {
    background: var(--chat-bg);
  }

  [data-theme="light"] body.keyboard-open .app-shell {
    background: var(--chat-bg);
  }

  [data-theme="light"] .sidebar {
    background: #ffffff;
  }

  [data-theme="light"] .sidebar-topbar,
  [data-theme="light"] .mobile-sidebar-hero {
    background: #ffffff;
  }

  [data-theme="light"] .icon-button {
    color: #54656f;
  }

  [data-theme="light"] .search-wrap {
    background: #f0f2f5;
  }

  [data-theme="light"] .contact-item {
    border-bottom-color: rgba(17, 27, 33, 0.06);
  }

  [data-theme="light"] .contact-preview {
    color: #667781;
  }

  [data-theme="light"] .chat-topbar {
    background: #e9edef;
  }

  [data-theme="light"] .chat-contact-copy p {
    color: #667781;
  }

  [data-theme="light"] .message-day-separator {
    background: rgba(255, 255, 255, 0.94);
  }

  [data-theme="light"] .chat-composer {
    background: var(--chat-bg);
  }

  [data-theme="light"] .composer-input {
    background: #ffffff;
    box-shadow: inset 0 0 0 1px rgba(17, 27, 33, 0.08);
  }
}

@media (max-width: 520px) {
  .mobile-sidebar-hero h2 {
    font-size: 46px;
  }
}
