:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --primary: #1A1A1A;
  --accent: #C41E3A;
  --background: #FFFFFF;
  --surface: #F5F5F5;
  --danger: #f73730;
  --success: #41d33e;
  --warning: #f8d313;
  --accent-soft: rgba(196, 30, 58, 0.10);
  --accent-border: rgba(196, 30, 58, 0.24);
  --muted: #696469;
  color: var(--primary);
  background: var(--background);
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  min-width: 280px;
  background:
    radial-gradient(circle at 10% 0%, rgba(196, 30, 58, 0.08), transparent 28rem),
    var(--background);
}

button,
textarea,
input {
  font: inherit;
}

button,
label {
  -webkit-tap-highlight-color: transparent;
}

.chat-shell {
  width: 100%;
  height: 100dvh;
  min-height: 420px;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  background: var(--background);
}

.topbar {
  min-height: 72px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: #fff;
  background: var(--primary);
  border-bottom: 3px solid var(--accent);
  box-shadow: 0 6px 20px rgba(33, 31, 38, 0.16);
  z-index: 2;
}

.brand-mark {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.32);
  border-radius: 12px 12px 12px 4px;
  color: #fff;
  background: var(--accent);
  font-size: 1.25rem;
  font-weight: 850;
  letter-spacing: -0.04em;
}

.brand-copy {
  min-width: 0;
}

.clear-chat-button {
  min-height: 38px;
  margin-left: auto;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 10px;
  color: #f3faf6;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 680;
}

.clear-chat-button:hover {
  border-color: rgba(255, 255, 255, 0.52);
  background: rgba(196, 30, 58, 0.72);
}

.clear-chat-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.clear-chat-button svg {
  width: 17px;
  height: 17px;
  fill: currentColor;
}

.brand-copy h1 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.3;
  font-weight: 720;
  letter-spacing: -0.015em;
}

.brand-copy p {
  margin: 3px 0 0;
  display: flex;
  align-items: center;
  gap: 6px;
  color: #ddd7e1;
  font-size: 0.78rem;
  line-height: 1.2;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warning);
  box-shadow: 0 0 0 3px rgba(248, 211, 19, 0.15);
}

.is-online .status-dot {
  background: var(--success);
  box-shadow: 0 0 0 3px rgba(65, 211, 62, 0.15);
}

.is-offline .status-dot {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(247, 55, 48, 0.15);
}

.messages {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 22px max(16px, calc((100% - 780px) / 2)) 30px;
  scrollbar-color: rgba(196, 30, 58, 0.34) transparent;
}

.message {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  margin: 0 0 18px;
}

.message.user {
  justify-content: flex-end;
}

.message-avatar {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  display: grid;
  place-items: center;
  border-radius: 9px 9px 9px 3px;
  color: #fff;
  background: var(--accent);
  font-size: 0.78rem;
  font-weight: 850;
}

.bubble {
  max-width: min(82%, 650px);
  padding: 11px 14px;
  border: 1px solid var(--accent-border);
  border-radius: 6px 17px 17px 17px;
  background: var(--surface);
  box-shadow: 0 4px 14px rgba(33, 31, 38, 0.055);
}

.user .bubble {
  color: #fff;
  border-color: var(--primary);
  border-radius: 17px 6px 17px 17px;
  background: var(--primary);
}

.bubble-content {
  min-height: 1.45em;
  font-size: 1rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.bubble-content p {
  margin: 0 0 0.75em;
}

.bubble-content p:last-child {
  margin-bottom: 0;
}

.bubble-content pre {
  margin: 10px 0;
  padding: 12px;
  overflow-x: auto;
  border-radius: 9px;
  color: #F5F5F5;
  background: var(--primary);
  font: 0.88rem/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.bubble-content code {
  padding: 0.12em 0.32em;
  border-radius: 4px;
  background: var(--accent-soft);
  font: 0.9em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.user .bubble-content code {
  color: #fff;
  background: rgba(255, 255, 255, 0.15);
}

.message-time {
  margin-top: 7px;
  color: var(--muted);
  font-size: 0.72rem;
}

.user .message-time {
  color: #ddd7e1;
  text-align: right;
}

.attachment-label {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--accent-border);
  font-size: 0.78rem;
  font-weight: 650;
}

.typing .bubble-content::after {
  content: "";
  display: inline-block;
  width: 0.55em;
  height: 1em;
  margin-left: 3px;
  vertical-align: -0.15em;
  background: var(--accent);
  animation: blink 0.85s steps(1) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.error-banner {
  margin: 0 max(14px, calc((100% - 780px) / 2)) 10px;
  padding: 10px 12px;
  border: 1px solid rgba(247, 55, 48, 0.34);
  border-radius: 10px;
  color: var(--danger);
  background: rgba(247, 55, 48, 0.07);
  font-size: 0.88rem;
}

.composer {
  padding: 12px max(14px, calc((100% - 780px) / 2)) calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--accent-border);
  background: rgba(245, 245, 245, 0.96);
  backdrop-filter: blur(12px);
}

.composer-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: end;
  gap: 8px;
  padding: 7px;
  border: 1px solid var(--accent-border);
  border-radius: 16px;
  background: var(--background);
  box-shadow: 0 6px 22px rgba(33, 31, 38, 0.07);
}

.composer-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea {
  width: 100%;
  max-height: 140px;
  min-height: 42px;
  padding: 9px 4px 7px;
  resize: none;
  border: 0;
  outline: 0;
  color: var(--primary);
  background: transparent;
  font-size: 1rem;
  line-height: 1.45;
}

textarea::placeholder {
  color: #8a838f;
}

.attach-button,
.voice-button,
.send-button,
.stop-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
}

.attach-button {
  color: var(--accent);
  background: var(--surface);
  font-size: 1.45rem;
  line-height: 1;
}

.attach-button:hover {
  background: var(--accent-soft);
}

.voice-button {
  position: relative;
  color: var(--accent);
  background: var(--surface);
}

.voice-button:hover {
  background: var(--accent-soft);
}

.voice-button svg {
  width: 21px;
  height: 21px;
  fill: currentColor;
}

.voice-button.is-listening {
  color: #fff;
  background: var(--danger);
  box-shadow: 0 0 0 4px rgba(247, 55, 48, 0.13);
  animation: recording-pulse 1.4s ease-in-out infinite;
}

.voice-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.voice-status {
  margin: 0 0 8px;
  padding: 8px 11px;
  border: 1px solid rgba(247, 55, 48, 0.28);
  border-radius: 10px;
  color: var(--primary);
  background: rgba(247, 55, 48, 0.07);
  font-size: 0.8rem;
  font-weight: 620;
}

.voice-status-dot {
  width: 8px;
  height: 8px;
  margin-right: 7px;
  display: inline-block;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(247, 55, 48, 0.14);
}

@keyframes recording-pulse {
  50% { box-shadow: 0 0 0 7px rgba(247, 55, 48, 0.08); }
}

.send-button {
  color: #fff;
  background: var(--accent);
}

.send-button:hover {
  background: #9f182f;
}

.send-button:disabled,
.attach-button.is-disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

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

.stop-button {
  background: rgba(247, 55, 48, 0.10);
}

.stop-button span {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: var(--danger);
}

.composer-note {
  margin: 7px 4px 0;
  color: var(--muted);
  font-size: 0.74rem;
  text-align: center;
}

.attachment-preview {
  margin: 0 0 8px;
  padding: 8px;
  border: 1px solid var(--accent-border);
  border-radius: 12px;
  background: var(--surface);
}

.attachment-list {
  display: grid;
  gap: 6px;
}

.attachment-item {
  min-width: 0;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  padding: 4px;
  border-radius: 9px;
  background: var(--background);
}

.attachment-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--accent-border);
  border-radius: 8px;
  color: var(--accent);
  background: var(--accent-soft);
  font-size: 0.67rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.attachment-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attachment-details {
  min-width: 0;
}

.attachment-details strong,
.attachment-details span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-details strong {
  font-size: 0.82rem;
}

.attachment-details span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.72rem;
}

.remove-attachment {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 9px;
  color: var(--danger);
  background: rgba(247, 55, 48, 0.08);
  cursor: pointer;
  font-size: 1.3rem;
}

.remove-all-attachments {
  margin: 7px 4px 1px auto;
  padding: 4px 7px;
  display: block;
  border: 0;
  color: var(--danger);
  background: transparent;
  cursor: pointer;
  font-size: 0.72rem;
  font-weight: 650;
}

.remove-all-attachments:hover {
  color: #c92520;
  text-decoration: underline;
}

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

@media (min-width: 850px) {
  body {
    padding: 12px;
  }

  .chat-shell {
    max-width: 980px;
    height: calc(100dvh - 24px);
    min-height: 520px;
    margin: 0 auto;
    overflow: hidden;
    border: 1px solid var(--accent-border);
    border-radius: 20px;
    box-shadow: 0 18px 60px rgba(33, 31, 38, 0.12);
  }
}

@media (max-width: 520px) {
  .topbar {
    min-height: 66px;
    padding: 11px 13px;
  }

  .messages {
    padding: 17px 12px 24px;
  }

  .bubble {
    max-width: 88%;
  }

  .composer {
    padding-left: 9px;
    padding-right: 9px;
  }

  .clear-chat-button {
    width: 38px;
    padding: 0;
  }

  .clear-chat-button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
