:root {
  color-scheme: light;
  --page-bg: #eef3f9;
  --surface: #ffffff;
  --surface-soft: #f5f7fa;
  --text: #172033;
  --text-muted: #667085;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eaf1ff;
  --error: #b42318;
  --error-bg: #fff1f0;
  --error-border: #fecdca;
  --shadow: 0 18px 50px rgba(15, 23, 42, 0.09);
  --radius-large: 20px;
  --radius-medium: 14px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--page-bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
textarea {
  font: inherit;
}

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

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-actions form {
  margin: 0;
}

.chat-app {
  width: min(100% - 40px, 1040px);
  height: min(880px, calc(100dvh - 40px));
  margin: 20px auto;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid rgba(226, 232, 240, 0.9);
  border-radius: var(--radius-large);
  box-shadow: var(--shadow);
}

.app-header {
  min-height: 82px;
  padding: 16px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.96);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  color: #ffffff;
  background: linear-gradient(145deg, #3b82f6, #2563eb);
  border-radius: 13px;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.2);
}

.brand-mark svg {
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand h1 {
  margin: 0;
  font-size: 1.08rem;
  line-height: 1.35;
  letter-spacing: -0.015em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--text-muted);
  font-size: 0.83rem;
}

.new-chat-button {
  min-height: 40px;
  padding: 9px 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #344054;
  background: var(--surface);
  border: 1px solid #d0d5dd;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.new-chat-button:hover {
  background: var(--surface-soft);
  border-color: #b8c0cc;
}

.new-chat-button:active {
  transform: translateY(1px);
}

.new-chat-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.logout-button {
  min-height: 40px;
  padding: 9px 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #475467;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  transition: color 160ms ease, background 160ms ease;
}

.logout-button:hover {
  color: var(--error);
  background: var(--error-bg);
}

.logout-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.login-page {
  min-height: 100%;
  padding: 24px;
  display: grid;
  place-items: center;
}

.login-card {
  width: min(100%, 430px);
  padding: clamp(28px, 6vw, 42px);
  background: var(--surface);
  border: 1px solid rgba(226, 232, 240, 0.9);
  border-radius: var(--radius-large);
  box-shadow: var(--shadow);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.login-brand h1,
.login-heading h2 {
  margin: 0;
  letter-spacing: -0.02em;
}

.login-brand h1 {
  font-size: 1.08rem;
}

.login-brand p,
.login-heading p {
  margin: 3px 0 0;
  color: var(--text-muted);
}

.login-brand p {
  font-size: 0.83rem;
}

.login-heading {
  margin: 34px 0 24px;
}

.login-heading h2 {
  font-size: 1.55rem;
}

.login-heading p {
  font-size: 0.9rem;
  line-height: 1.5;
}

.login-error {
  margin: 0 0 18px;
  padding: 11px 13px;
  color: var(--error);
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: 10px;
  font-size: 0.86rem;
  line-height: 1.45;
}

.login-form {
  display: grid;
  gap: 9px;
}

.login-form label {
  margin-top: 7px;
  color: #344054;
  font-size: 0.84rem;
  font-weight: 650;
}

.login-form input:not([type="hidden"]) {
  width: 100%;
  height: 46px;
  padding: 0 13px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid #d0d5dd;
  border-radius: 10px;
  outline: 0;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.login-form input:focus {
  border-color: #8bb1fb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.login-form button {
  height: 46px;
  margin-top: 15px;
  color: #ffffff;
  background: var(--primary);
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 650;
  transition: background 160ms ease, transform 160ms ease;
}

.login-form button:hover {
  background: var(--primary-hover);
}

.login-form button:active {
  transform: translateY(1px);
}

.conversation {
  overflow-y: auto;
  padding: 30px clamp(20px, 5vw, 62px);
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}

.conversation::-webkit-scrollbar {
  width: 6px;
}

.conversation::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 999px;
}

.message-row {
  width: 100%;
  margin-bottom: 22px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  animation: message-in 220ms ease-out;
}

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

.assistant-avatar {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  align-self: flex-start;
  color: var(--primary);
  background: var(--primary-soft);
  border-radius: 10px;
  font-size: 0.76rem;
  font-weight: 750;
}

.message {
  max-width: min(72%, 660px);
  padding: 12px 15px;
  border-radius: var(--radius-medium);
  font-size: 0.95rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.message-row.assistant .message {
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}

.message-row.user .message {
  color: #ffffff;
  background: var(--primary);
  border-bottom-right-radius: 5px;
  box-shadow: 0 5px 14px rgba(37, 99, 235, 0.15);
}

.message-row.error .message {
  color: var(--error);
  background: var(--error-bg);
  border-color: var(--error-border);
}

.message-row.error .assistant-avatar {
  color: var(--error);
  background: var(--error-bg);
}

.typing-indicator {
  min-width: 66px;
  min-height: 45px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: #98a2b3;
  border-radius: 50%;
  animation: typing 1.15s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 150ms;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 300ms;
}

.composer-area {
  padding: 16px clamp(20px, 5vw, 62px) 18px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.composer {
  min-height: 52px;
  padding: 6px 7px 6px 16px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface-soft);
  border: 1px solid #d7dee8;
  border-radius: 15px;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.composer:focus-within {
  border-color: #8bb1fb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.composer textarea {
  width: 100%;
  max-height: 126px;
  padding: 8px 0;
  resize: none;
  overflow-y: auto;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;
  line-height: 1.45;
}

.composer textarea::placeholder {
  color: #98a2b3;
}

.send-button {
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  color: #ffffff;
  background: var(--primary);
  border: 0;
  border-radius: 11px;
  cursor: pointer;
  transition: background 160ms ease, opacity 160ms ease, transform 160ms ease;
}

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

.send-button:active:not(:disabled) {
  transform: scale(0.97);
}

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

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

.composer-hint {
  margin: 8px 3px 0;
  color: var(--text-muted);
  font-size: 0.72rem;
  text-align: center;
}

.composer-hint code {
  padding: 1px 4px;
  color: #475467;
  background: #eef2f6;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.7rem;
}

.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;
}

button:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.28);
  outline-offset: 2px;
}

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

@keyframes typing {
  0%,
  60%,
  100% {
    opacity: 0.42;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-4px);
  }
}

@media (max-width: 640px) {
  .chat-app {
    width: 100%;
    height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .app-header {
    min-height: 72px;
    padding: 13px 15px;
  }

  .brand-mark {
    width: 40px;
    height: 40px;
  }

  .new-chat-button {
    width: 40px;
    padding: 9px;
    justify-content: center;
  }

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

  .logout-button {
    width: 40px;
    padding: 9px;
    justify-content: center;
  }

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

  .login-page {
    padding: 16px;
  }

  .login-card {
    padding: 28px 22px;
  }

  .conversation {
    padding: 24px 15px;
  }

  .message {
    max-width: 84%;
    font-size: 0.92rem;
  }

  .composer-area {
    padding: 12px 12px max(12px, env(safe-area-inset-bottom));
  }

  .composer-hint {
    line-height: 1.4;
  }
}

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