:root {
  color-scheme: light dark;
  --page: var(--tg-theme-bg-color, #f3f5fa);
  --surface: var(--tg-theme-secondary-bg-color, rgba(255, 255, 255, 0.86));
  --surface-strong: #ffffff;
  --text: var(--tg-theme-text-color, #162033);
  --muted: var(--tg-theme-hint-color, #738097);
  --line: rgba(105, 120, 145, 0.16);
  --accent: var(--tg-theme-button-color, #635bff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --cyan: #25c7d9;
  --success: #28b779;
  --danger: #e75266;
  --shadow: 0 18px 55px rgba(28, 43, 71, 0.12);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: var(--tg-theme-bg-color, #0c1220);
    --surface: var(--tg-theme-secondary-bg-color, rgba(24, 33, 52, 0.88));
    --surface-strong: #182034;
    --text: var(--tg-theme-text-color, #f4f7ff);
    --muted: var(--tg-theme-hint-color, #91a0ba);
    --line: rgba(177, 194, 224, 0.12);
    --shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
  background: var(--page);
  color: var(--text);
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

.ambient {
  position: fixed;
  z-index: 0;
  width: 280px;
  height: 280px;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.2;
  pointer-events: none;
}

.ambient-one {
  top: -120px;
  right: -100px;
  background: var(--accent);
}

.ambient-two {
  bottom: -140px;
  left: -130px;
  background: var(--cyan);
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(100%, 720px);
  min-height: 100dvh;
  margin: 0 auto;
}

.is-hidden {
  display: none !important;
}

.auth-view {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 42px 28px calc(34px + var(--safe-bottom));
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin-bottom: 26px;
  border-radius: 22px;
  color: white;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.08em;
  background: linear-gradient(145deg, var(--cyan), var(--accent) 72%);
  box-shadow: 0 14px 35px color-mix(in srgb, var(--accent) 34%, transparent);
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

.auth-view h1 {
  margin-bottom: 18px;
  font-size: clamp(34px, 10vw, 52px);
  line-height: 0.98;
  letter-spacing: -0.055em;
}

.auth-copy {
  max-width: 460px;
  margin-bottom: 28px;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.55;
}

.auth-form {
  display: grid;
  gap: 12px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 26px;
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.auth-form label {
  padding-left: 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.key-row {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 54px;
  padding: 0 8px 0 16px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: color-mix(in srgb, var(--surface-strong) 84%, transparent);
}

.key-row:focus-within {
  border-color: color-mix(in srgb, var(--accent) 65%, transparent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent);
}

.key-row input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
}

.icon-button,
.back-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 13px;
  background: color-mix(in srgb, var(--surface-strong) 72%, transparent);
  cursor: pointer;
}

.primary-button {
  min-height: 54px;
  border: 0;
  border-radius: 17px;
  color: var(--accent-text);
  font-weight: 750;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 68%, var(--cyan)));
  cursor: pointer;
  box-shadow: 0 10px 25px color-mix(in srgb, var(--accent) 24%, transparent);
}

.primary-button:disabled,
.send-button:disabled {
  opacity: 0.5;
  cursor: wait;
}

.form-error {
  min-height: 18px;
  margin: 0 4px;
  color: var(--danger);
  font-size: 13px;
}

.security-note {
  margin: 18px 4px 0;
  color: var(--muted);
  font-size: 12px;
}

.security-note span {
  color: var(--success);
}

.screen {
  min-height: 100dvh;
  padding: 24px 18px calc(28px + var(--safe-bottom));
}

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

.topbar {
  padding: 8px 4px 22px;
}

.topbar h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1;
  letter-spacing: -0.04em;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  background: var(--surface);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #d3a327;
  box-shadow: 0 0 0 4px rgba(211, 163, 39, 0.13);
}

.status-ready .status-dot {
  background: var(--success);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--success) 15%, transparent);
}

.status-degraded .status-dot,
.status-auth_required .status-dot {
  background: var(--danger);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--danger) 14%, transparent);
}

.search-box {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 50px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 17px;
  color: var(--muted);
  background: var(--surface);
  box-shadow: 0 8px 28px rgba(42, 59, 89, 0.06);
}

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

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 25px 4px 10px;
}

.section-heading h2 {
  margin: 0;
  font-size: 14px;
  letter-spacing: 0.01em;
}

.section-heading span {
  color: var(--muted);
  font-size: 13px;
}

.chat-list {
  display: grid;
  gap: 8px;
}

.chat-card {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) auto;
  align-items: center;
  gap: 13px;
  width: 100%;
  min-height: 70px;
  padding: 11px 13px;
  border: 1px solid transparent;
  border-radius: 20px;
  color: inherit;
  text-align: left;
  background: transparent;
  cursor: pointer;
}

.chat-card:hover,
.chat-card:focus-visible {
  border-color: var(--line);
  outline: 0;
  background: var(--surface);
}

.avatar {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  border-radius: 17px;
  color: white;
  font-weight: 800;
  background: linear-gradient(145deg, var(--avatar-a, var(--cyan)), var(--avatar-b, var(--accent)));
}

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

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

.chat-title {
  overflow: hidden;
  font-size: 15px;
  font-weight: 720;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-time {
  color: var(--muted);
  font-size: 11px;
}

.chat-preview {
  overflow: hidden;
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-chevron {
  color: var(--muted);
  font-size: 22px;
}

.empty-state {
  display: grid;
  justify-items: center;
  padding: 70px 26px;
  text-align: center;
}

.empty-icon {
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin-bottom: 18px;
  border-radius: 22px;
  color: var(--accent);
  font-size: 26px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.empty-state h2 {
  margin-bottom: 8px;
  font-size: 18px;
}

.empty-state p {
  max-width: 340px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

.max-login-panel {
  display: grid;
  justify-items: center;
  gap: 12px;
  margin-top: 18px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 22px;
  text-align: center;
  background: var(--surface);
}

.max-login-panel h2,
.max-login-panel p {
  margin: 0;
}

.max-login-panel p {
  max-width: 430px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.max-login-panel img {
  display: block;
  width: min(100%, 520px);
  min-height: 220px;
  border-radius: 16px;
  background: #dcefff;
}

.conversation-view {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  height: 100dvh;
  padding: 0;
  overflow: hidden;
}

.conversation-header {
  min-height: 74px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--page) 86%, transparent);
  backdrop-filter: blur(18px);
}

.back-button {
  font-size: 32px;
  line-height: 1;
}

.conversation-header .avatar {
  width: 42px;
  height: 42px;
  border-radius: 15px;
}

.conversation-title {
  flex: 1;
  min-width: 0;
}

.conversation-title h1 {
  overflow: hidden;
  margin: 0;
  font-size: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  padding: 18px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.message-row {
  display: flex;
}

.message-row.outgoing {
  justify-content: flex-end;
}

.message-bubble {
  width: fit-content;
  max-width: min(84%, 520px);
  padding: 10px 12px 7px;
  border: 1px solid var(--line);
  border-radius: 18px 18px 18px 6px;
  background: var(--surface-strong);
  box-shadow: 0 6px 22px rgba(24, 38, 66, 0.06);
}

.outgoing .message-bubble {
  border-color: transparent;
  border-radius: 18px 18px 6px 18px;
  background: color-mix(in srgb, var(--accent) 15%, var(--surface-strong));
}

.message-sender {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 720;
}

.message-text {
  margin: 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.42;
}

.message-time {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 10px;
  text-align: right;
}

.attachment {
  margin-bottom: 8px;
}

.attachment img,
.attachment video {
  display: block;
  width: min(100%, 420px);
  max-height: 420px;
  border-radius: 13px;
  object-fit: cover;
  background: var(--page);
}

.attachment audio {
  display: block;
  width: min(100%, 320px);
}

.video-note video {
  width: min(64vw, 280px);
  aspect-ratio: 1;
  border-radius: 50%;
}

.file-link {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 650;
  text-decoration: none;
}

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

.load-more {
  justify-self: center;
  margin: 8px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--page) 90%, transparent);
  backdrop-filter: blur(18px);
}

.composer textarea {
  flex: 1;
  max-height: 128px;
  min-height: 46px;
  padding: 12px 15px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 17px;
  outline: 0;
  color: var(--text);
  background: var(--surface-strong);
}

.composer textarea:focus {
  border-color: color-mix(in srgb, var(--accent) 54%, transparent);
}

.send-button {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 16px;
  color: var(--accent-text);
  font-size: 23px;
  font-weight: 800;
  background: var(--accent);
  cursor: pointer;
}

.toast {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  max-width: calc(100% - 40px);
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: 13px;
  color: var(--text);
  font-size: 13px;
  background: var(--surface-strong);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity 160ms ease, transform 160ms ease;
}

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

@media (min-width: 760px) {
  .app-shell {
    min-height: calc(100dvh - 32px);
    margin: 16px auto;
    border: 1px solid var(--line);
    border-radius: 30px;
    overflow: hidden;
    background: color-mix(in srgb, var(--page) 82%, transparent);
    box-shadow: var(--shadow);
  }

  .screen,
  .auth-view,
  .conversation-view {
    min-height: calc(100dvh - 32px);
  }

  .conversation-view {
    height: calc(100dvh - 32px);
  }
}

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