*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --header-from: #1a1a2e;
  --header-to: #16213e;
  --accent: #4f8ef7;
  --accent-hover: #3a7de0;
  --user-bg: #4f8ef7;
  --user-text: #ffffff;
  --bot-bg: #f1f5fb;
  --bot-text: #1e293b;
  --border: #e2e8f0;
  --muted: #94a3b8;
  --radius: 18px;
  --shadow: 0 4px 24px rgba(0,0,0,0.08);
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app {
  width: 720px;
  height: 90vh;
  max-height: 820px;
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Header */
.header {
  background: linear-gradient(135deg, var(--header-from), var(--header-to));
  padding: 18px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

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

.logo {
  font-size: 28px;
  line-height: 1;
}

.header-text h1 {
  font-size: 20px;
  font-weight: 700;
  color: #ffffff;
  letter-spacing: 0.3px;
}

.header-text .subtitle {
  font-size: 12px;
  color: rgba(255,255,255,0.55);
  letter-spacing: 0.5px;
}

.clear-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  background: rgba(255,255,255,0.1);
  color: rgba(255,255,255,0.8);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s;
}

.clear-btn:hover {
  background: rgba(255,255,255,0.2);
  color: #fff;
}

/* Chat area */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px 24px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chat::-webkit-scrollbar {
  width: 4px;
}

.chat::-webkit-scrollbar-track {
  background: transparent;
}

.chat::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* Empty state */
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 15px;
}

/* Messages */
.message {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.6;
  animation: fadeSlide 0.2s ease;
}

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

.message.user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.message.bot {
  align-self: flex-start;
  background: var(--bot-bg);
  color: var(--bot-text);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.message.bot p { margin: 0 0 6px; }
.message.bot p:last-child { margin-bottom: 0; }
.message.bot ul, .message.bot ol { padding-left: 18px; margin: 6px 0; }
.message.bot code {
  background: #e8edf5;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
}
.message.bot pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
}
.message.bot a {
  color: var(--accent);
  text-decoration: none;
}
.message.bot a:hover { text-decoration: underline; }
.message.bot img {
  max-width: 100%;
  border-radius: 10px;
  margin: 8px 0;
  display: block;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}

/* Loader */
.loader-dots {
  display: flex;
  gap: 5px;
  padding: 4px 0;
}

.loader-dots span {
  width: 7px;
  height: 7px;
  background: var(--muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

.loader-dots span:nth-child(2) { animation-delay: 0.2s; }
.loader-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%            { transform: translateY(-6px); opacity: 1; }
}

/* Input area */
.input-area {
  padding: 12px 24px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 14px;
  padding: 10px 10px 10px 16px;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
}

textarea {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font-size: 14.5px;
  font-family: inherit;
  color: var(--bot-text);
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
  scrollbar-width: thin;
  outline: none;
}

textarea::placeholder { color: var(--muted); }

.send-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s;
}

.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { transform: scale(0.94); }

.hint {
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}

kbd {
  background: var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  font-family: inherit;
}

/* Responsive */
@media (max-width: 760px) {
  html, body {
    height: 100%;
    overflow: hidden;
  }

  body {
    align-items: stretch;
  }

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

  .app {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 0;
    max-height: none;
    width: 100%;
    overflow: hidden;
  }

  .header {
    padding: 14px 16px;
  }

  /* Extra bottom padding so last message isn't hidden behind fixed input bar */
  .chat {
    padding: 16px 14px 110px;
    min-height: 0;
    overflow-x: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .chat::-webkit-scrollbar {
    display: none;
  }

  .message {
    max-width: 84%;
    font-size: 14px;
    padding: 10px 13px;
  }

  .message.user {
    margin-right: 2px;
  }

  .message.bot {
    margin-left: 2px;
  }

  .input-wrapper {
    align-items: center;
  }

  /* Breaks out of .app overflow:hidden via position:fixed.
     JS moves it above the keyboard using transform (no layout reflow = no scrollbar flash). */
  .input-area {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
    will-change: transform;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 10px 14px 40px;
    padding-bottom: max(40px, env(safe-area-inset-bottom));
    z-index: 100;
  }

  .hint {
    display: none;
  }
}
