:root {
  --tg-bg-app: #d7dfe6;
  --tg-bg-shell: #ffffff;
  --tg-bg-chat-pattern: #e8eff5;
  --tg-bg-chat-pattern-dot: rgba(120, 156, 186, 0.18);
  --tg-bg-in: #ffffff;
  --tg-bg-out: #eeffde;
  --tg-bg-hover: #f4f7fa;
  --tg-border: #d7dde3;
  --tg-border-strong: #c7d0d8;
  --tg-text: #1f2d3d;
  --tg-text-muted: #6b7885;
  --tg-accent: #3390ec;
  --tg-accent-press: #2e7fd0;
  --tg-shadow: 0 10px 34px rgba(28, 48, 74, 0.15);
  --tg-radius-shell: 16px;
  --tg-radius-bubble: 14px;
  --tg-radius-input: 24px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "SF Pro Text", "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  color: var(--tg-text);
  background:
    radial-gradient(circle at 20% 10%, #eef5fb 0%, rgba(238, 245, 251, 0) 35%),
    radial-gradient(circle at 80% 90%, #e9f0f6 0%, rgba(233, 240, 246, 0) 35%),
    var(--tg-bg-app);
}

.tg-page {
  padding: 16px;
}

.tg-shell {
  width: min(1180px, 100%);
  height: calc(100dvh - 32px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 300px 1fr;
  background: var(--tg-bg-shell);
  border: 1px solid var(--tg-border);
  border-radius: var(--tg-radius-shell);
  box-shadow: var(--tg-shadow);
  overflow: hidden;
}

.tg-sidebar {
  border-right: 1px solid var(--tg-border);
  background: linear-gradient(180deg, #fbfdff 0%, #f5f8fb 100%);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.tg-brand {
  color: #2d3f53;
  text-decoration: none;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.2px;
  padding: 12px;
  border-radius: 12px;
  min-height: 48px;
  display: flex;
  align-items: center;
}

.tg-brand:hover {
  background: var(--tg-bg-hover);
}

.tg-sidebar-card {
  border: 1px solid var(--tg-border);
  border-radius: 14px;
  padding: 12px;
  background: #ffffff;
}

.tg-sidebar-label {
  margin: 0;
  color: var(--tg-text-muted);
  font-size: 0.78rem;
}

.tg-sidebar-title {
  margin: 7px 0 2px;
  font-weight: 600;
}

.tg-sidebar-slug {
  margin: 0;
  color: var(--tg-text-muted);
  font-size: 0.82rem;
}

.tg-chat {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 25% 25%, var(--tg-bg-chat-pattern-dot) 1px, transparent 1px) 0 0 / 26px 26px,
    radial-gradient(circle at 75% 75%, rgba(95, 134, 168, 0.12) 1px, transparent 1px) 13px 13px / 26px 26px,
    var(--tg-bg-chat-pattern);
}

.tg-header {
  background: #ffffffea;
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--tg-border);
  min-height: 64px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.tg-back {
  text-decoration: none;
  color: #4a5d71;
  border: 1px solid transparent;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
}

.tg-back:hover {
  border-color: var(--tg-border);
  background: var(--tg-bg-hover);
}

.tg-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #56a8f9, #3388df);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.tg-header-meta h1 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.2;
}

.tg-header-meta p {
  margin: 2px 0 0;
  color: var(--tg-text-muted);
  font-size: 0.8rem;
}

.tg-history-wrap {
  overflow-y: auto;
  min-height: 0;
  overscroll-behavior: contain;
  padding: 14px 14px 6px;
  scroll-behavior: smooth;
}

.tg-day-separator {
  position: sticky;
  top: 0;
  z-index: 1;
  width: fit-content;
  margin: 0 auto 10px;
  font-size: 0.72rem;
  color: #eef5fb;
  background: rgba(74, 109, 137, 0.78);
  border-radius: 999px;
  padding: 4px 10px;
}

.tg-messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tg-msg-row {
  display: flex;
  width: 100%;
}

.tg-msg-row.user {
  justify-content: flex-end;
}

.tg-msg-row.bot {
  justify-content: flex-start;
}

.tg-bubble {
  max-width: min(82%, 560px);
  position: relative;
  border-radius: var(--tg-radius-bubble);
  border: 1px solid rgba(151, 169, 183, 0.3);
  padding: 8px 10px 6px;
  line-height: 1.38;
  font-size: 0.96rem;
  white-space: pre-wrap;
  word-break: break-word;
  animation: msg-in 160ms ease-out;
}

.tg-msg-row.bot .tg-bubble {
  background: var(--tg-bg-in);
  border-bottom-left-radius: 6px;
}

.tg-msg-row.user .tg-bubble {
  background: var(--tg-bg-out);
  border-bottom-right-radius: 6px;
}

.tg-bubble-meta {
  margin-top: 2px;
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  font-size: 0.7rem;
  color: #7b8793;
}

.tg-hub-link {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--tg-text);
  min-height: 62px;
  border: 1px solid var(--tg-border);
  border-radius: 14px;
  padding: 10px;
  background: #ffffffd1;
}

.tg-hub-link:hover {
  background: #fff;
  border-color: var(--tg-border-strong);
}

.tg-hub-dot {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(145deg, #62affb, #2e83dc);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.tg-hub-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.tg-hub-meta strong {
  font-size: 0.93rem;
}

.tg-hub-meta small {
  color: var(--tg-text-muted);
  font-size: 0.78rem;
}

.tg-hub-open {
  margin-left: auto;
  color: var(--tg-accent);
  font-size: 0.82rem;
}

.tg-reply-wrap {
  border-top: 1px solid var(--tg-border);
  background: #f7fafc;
  padding: 7px 8px;
}

.tg-reply-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 6px;
}

.tg-reply-grid button {
  min-height: 40px;
  border: 1px solid var(--tg-border);
  border-radius: 10px;
  background: #ffffff;
  color: #2a3b4e;
  font-size: 0.83rem;
  font-weight: 500;
  padding: 6px 8px;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
  line-height: 1.2;
}

.tg-reply-grid button:hover {
  background: var(--tg-bg-hover);
  border-color: var(--tg-border-strong);
}

.tg-reply-btn .tg-reply-icon {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  background: #eaf3fd;
  color: #2f79c7;
}

.tg-reply-btn .tg-reply-icon svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tg-reply-btn .tg-reply-text {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tg-reply-btn.danger .tg-reply-icon {
  background: #fdecec;
  color: #c34a45;
}

.tg-composer {
  display: flex;
  align-items: center;
  gap: 8px;
  position: sticky;
  bottom: 0;
  z-index: 3;
  border-top: 1px solid var(--tg-border);
  background: #f3f7fb;
  padding: 10px;
}

.tg-icon-btn,
.tg-send-btn {
  min-width: 48px;
  min-height: 48px;
  border-radius: 50%;
  border: 1px solid var(--tg-border);
  background: #fff;
  color: #587088;
  font-size: 1.1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.tg-send-btn {
  border: none;
  background: var(--tg-accent);
  color: #fff;
}

.tg-send-btn:hover {
  background: var(--tg-accent-press);
}

.tg-composer input {
  flex: 1;
  min-height: 48px;
  border-radius: var(--tg-radius-input);
  border: 1px solid var(--tg-border-strong);
  padding: 0 16px;
  font-size: 0.95rem;
  outline: none;
  background: #fff;
}

.tg-composer input:focus {
  border-color: var(--tg-accent);
  box-shadow: 0 0 0 3px rgba(51, 144, 236, 0.15);
}

.tg-error {
  margin: 0;
  padding: 0 14px 10px;
  color: #b3261e;
  font-size: 0.84rem;
}

.tg-login-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 30, 47, 0.52);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 20;
}

.tg-login-overlay[hidden] {
  display: none;
}

.tg-login-card {
  width: min(420px, 100%);
  border-radius: 16px;
  background: #fff;
  border: 1px solid var(--tg-border);
  box-shadow: var(--tg-shadow);
  padding: 18px;
}

.tg-login-card h2 {
  margin: 0;
  font-size: 1.05rem;
}

.tg-login-card p {
  margin: 6px 0 14px;
  color: var(--tg-text-muted);
  font-size: 0.88rem;
}

.tg-login-form {
  display: flex;
  gap: 8px;
}

.tg-login-form input {
  flex: 1;
  min-height: 48px;
  border: 1px solid var(--tg-border);
  border-radius: 12px;
  padding: 0 12px;
}

.tg-login-form button {
  min-height: 48px;
  border: none;
  border-radius: 12px;
  background: var(--tg-accent);
  color: #fff;
  padding: 0 14px;
  cursor: pointer;
}

.tg-typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 5px 2px;
}

.tg-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8ca0b1;
  animation: blink 1s infinite ease-in-out;
}

.tg-typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.tg-typing span:nth-child(3) {
  animation-delay: 0.35s;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes blink {
  0%,
  70%,
  100% {
    transform: translateY(0);
    opacity: 0.6;
  }
  35% {
    transform: translateY(-3px);
    opacity: 1;
  }
}

@media (max-width: 960px) {
  .tg-page {
    padding: 8px;
  }

  .tg-shell {
    grid-template-columns: 1fr;
    height: calc(100dvh - 16px);
    border-radius: 12px;
  }

  .tg-sidebar {
    display: none;
  }

  .tg-header {
    min-height: 60px;
  }

  .tg-history-wrap {
    padding: 10px 8px 6px;
  }

  .tg-bubble {
    max-width: 92%;
  }

  .tg-reply-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tg-reply-grid button {
    min-height: 38px;
    font-size: 0.8rem;
    padding: 6px;
  }

  .tg-reply-btn .tg-reply-icon {
    width: 20px;
    height: 20px;
    border-radius: 6px;
  }

  .tg-reply-btn .tg-reply-icon svg {
    width: 13px;
    height: 13px;
  }
}
