:root {
  --bg: #000;
  --card: #121212;
  --text: #fff;
  --text-secondary: #8e8e93;
  --accent: #1da1f2;
  --border: #262626;
  --user-bubble: #1da1f2;
  --bot-bubble: #1e1e1e;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  height: 100%;
  /* Fallback for browsers without dvh support */
  height: -webkit-fill-available;
}

body {
  height: 100%;
  min-height: 100vh;
  min-height: 100dvh; /* accounts for mobile browser UI (address bar) */
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
}

.chat-container {
  width: 100%;
  max-width: 700px;
  height: min(95vh, 900px);
  height: min(95dvh, 900px);
  background: var(--card);
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  overflow: hidden; /* keeps children clipped to the rounded corners */
}

.chat-header {
  flex-shrink: 0;
  height: 60px;
  padding: 0 20px;
  padding-top: env(safe-area-inset-top, 0);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  position: relative;
  z-index: 2;
}

.status-dot {
  width: 10px;
  height: 10px;
  background: #34c759;
  border-radius: 50%;
  flex-shrink: 0;
}

.chat-header h2 {
  font-size: 17px;
  font-weight: 600;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 24px 20px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  min-height: 0; /* required so flex children can actually scroll instead of overflowing the container */
}

.message {
  max-width: 80%;
  padding: 12px 18px;
  border-radius: 18px;
  font-size: 16px;
  line-height: 1.5;
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.user-message {
  align-self: flex-end;
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.bot-message {
  align-self: flex-start;
  background: var(--bot-bubble);
  color: var(--text);
  border-bottom-left-radius: 6px;
}

.system-message {
  align-self: center;
  background: var(--border);
  color: var(--text-secondary);
  font-size: 15px;
  max-width: 85%;
  text-align: center;
  border-radius: 16px;
}

.chat-footer {
  flex-shrink: 0;
  padding: 14px 20px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0));
  padding-left: calc(20px + env(safe-area-inset-left, 0));
  padding-right: calc(20px + env(safe-area-inset-right, 0));
  border-top: 1px solid var(--border);
  background: var(--card);
  position: relative;
  z-index: 2;
}

#chatForm {
  display: flex;
  gap: 12px;
  align-items: center;
}

#userInput {
  flex: 1;
  padding: 12px 20px;
  border: 1px solid var(--border);
  border-radius: 24px;
  outline: none;
  font-size: 16px; /* keeps this at 16px+ on every breakpoint to stop iOS Safari auto-zoom on focus */
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
  height: 48px;
  min-width: 0; /* allows the input to actually shrink inside the flex row on narrow screens */
  resize: none;
  -webkit-appearance: none;
  appearance: none;
}

#userInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(29,161,242,0.15);
}

#sendBtn {
  background: var(--accent);
  color: #fff;
  border: none;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background-color 0.2s, transform 0.1s;
  flex-shrink: 0;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation; /* removes the ~300ms tap delay on mobile browsers */
}

/* Only apply hover on devices that actually support hovering (mouse/trackpad),
   so the accent color doesn't get "stuck" after a tap on touchscreens */
@media (hover: hover) and (pointer: fine) {
  #sendBtn:hover { background: #1a8cd8; }
}
#sendBtn:active { transform: scale(0.95); }
#sendBtn:focus-visible,
#userInput:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Large / wide desktop screens: let the card breathe a bit more */
@media (min-width: 1000px) {
  .chat-container {
    max-width: 760px;
  }
}

/* Tablets and small laptops */
@media (max-width: 900px) {
  .chat-container {
    max-width: 90vw;
  }
}

/* Mobile */
@media (max-width: 600px) {
  body {
    align-items: stretch;
  }

  .chat-container {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-width: 100vw;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
    border: none;
    box-shadow: none;
  }

  .chat-header {
    height: 56px;
    padding: 0 16px;
    padding-top: env(safe-area-inset-top, 0);
  }

  .chat-messages {
    padding: 18px 16px;
    gap: 12px;
  }

  .message {
    max-width: 85%;
    font-size: 15px;
    padding: 10px 16px;
  }

  .system-message {
    max-width: 90%;
    font-size: 14px;
  }

  .chat-footer {
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
    padding-left: calc(16px + env(safe-area-inset-left, 0));
    padding-right: calc(16px + env(safe-area-inset-right, 0));
  }

  #userInput {
    font-size: 16px; /* unchanged from base - never shrink below 16px on mobile */
    padding: 10px 16px;
    height: 46px;
  }

  #sendBtn {
    width: 46px;
    height: 46px;
  }

  #chatForm {
    gap: 10px;
  }
}

@media (max-width: 380px) {
  .message { max-width: 88%; font-size: 14px; }
  .chat-header { padding-left: 14px; padding-right: 14px; }
  .chat-footer { padding-left: 14px; padding-right: 14px; }
  .chat-messages { padding-left: 14px; padding-right: 14px; }
}

/* Short / landscape phones: reclaim vertical space so the input never gets
   pushed off-screen by the keyboard */
@media (max-height: 480px) and (orientation: landscape) {
  .chat-header {
    height: 44px;
  }

  .chat-messages {
    padding-top: 12px;
    padding-bottom: 12px;
    gap: 8px;
  }

  .chat-footer {
    padding-top: 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0));
  }

  #userInput,
  #sendBtn {
    height: 40px;
  }
}

/* Respect users who've asked for reduced motion */
@media (prefers-reduced-motion: reduce) {
  .chat-messages {
    scroll-behavior: auto;
  }
  #userInput,
  #sendBtn {
    transition: none;
  }
}
