/* ===================================================================
   Reset & base
   =================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface-hover: #222;
  --border: #2a2a2a;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --user-bubble: #6366f1;
  --assistant-bubble: #1e1e1e;
  --danger: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  position: fixed;
  width: 100%;
  -webkit-text-size-adjust: 100%;
}

.screen {
  display: flex;
  height: 100%;
  height: 100dvh;
}

.screen[hidden] {
  display: none;
}

/* ===================================================================
   Login screen
   =================================================================== */
#login-screen {
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.login-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  margin-bottom: 0.75rem;
}

.login-card h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.5rem;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

#login-form input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 1rem;
  font-size: 0.95rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

#login-form input:focus {
  border-color: var(--accent);
}

#login-form button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.7rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

#login-form button:hover {
  background: var(--accent-hover);
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 0.75rem;
}

/* ===================================================================
   Chat screen layout
   =================================================================== */
#chat-screen {
  flex-direction: column;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}

/* Header */
#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: calc(0.75rem + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.header-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.header-title {
  font-weight: 700;
  font-size: 1.05rem;
}

#logout-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.3rem;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}

#logout-btn:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

/* Messages area */
#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Message bubbles */
.message {
  max-width: 85%;
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.935rem;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

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

/* Assistant message row: avatar + bubble */
.assistant-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  align-self: flex-start;
  max-width: 85%;
}

.assistant-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 2px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  flex: 1;
  min-width: 0;
}

/* Markdown inside messages */
.message p { margin-bottom: 0.5em; }
.message p:last-child { margin-bottom: 0; }
.message pre {
  background: rgba(0,0,0,0.3);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  overflow-x: auto;
  margin: 0.5em 0;
  font-size: 0.85rem;
}
.message code {
  font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
}
.message :not(pre) > code {
  background: rgba(255,255,255,0.08);
  padding: 0.15em 0.35em;
  border-radius: 4px;
}
.message ul, .message ol {
  padding-left: 1.3em;
  margin: 0.4em 0;
}
.message img {
  max-width: 100%;
  border-radius: 8px;
  margin: 0.4em 0;
}
.message a {
  color: var(--accent-hover);
}

/* Typing indicator row */
.typing-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  align-self: flex-start;
}

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 0.7rem 1rem;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing 1.2s ease-in-out infinite;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }

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

/* Attachment preview */
#attachment-preview {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  overflow-x: auto;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.attachment-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.attachment-chip img {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  object-fit: cover;
}

.attachment-chip .remove-attachment {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.15rem;
}

.attachment-chip .remove-attachment:hover {
  color: var(--danger);
}

/* User message attachment thumbnails */
.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
}

.message-attachments img {
  max-width: 200px;
  max-height: 150px;
  border-radius: 8px;
  object-fit: cover;
}

.message-attachments .file-chip {
  background: rgba(255,255,255,0.1);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
  color: rgba(255,255,255,0.7);
}

/* Input bar */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}

#message-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.9rem;
  font-size: 16px; /* Prevents iOS zoom on focus */
  color: var(--text);
  resize: none;
  outline: none;
  max-height: 150px;
  font-family: inherit;
  line-height: 1.45;
  transition: border-color 0.15s;
}

#message-input:focus {
  border-color: var(--accent);
}

#attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.45rem;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

#attach-btn:hover {
  color: var(--accent);
  background: rgba(99, 102, 241, 0.1);
}

#send-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  border-radius: var(--radius-sm);
  padding: 0.55rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}

#send-btn:hover {
  background: var(--accent-hover);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Error message in chat */
.message.error-msg {
  align-self: center;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: var(--danger);
  font-size: 0.85rem;
  text-align: center;
}

/* Scrollbar */
#messages::-webkit-scrollbar {
  width: 6px;
}
#messages::-webkit-scrollbar-track {
  background: transparent;
}
#messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ===================================================================
   Responsive / Mobile
   =================================================================== */
@media (max-width: 600px) {
  #chat-screen {
    max-width: 100%;
  }
  .message {
    max-width: 92%;
  }
  .assistant-row {
    max-width: 92%;
  }
  .login-card {
    margin: 0 1rem;
  }
  #attach-btn {
    padding: 0.6rem;
    min-width: 44px;
    min-height: 44px;
  }
  #send-btn {
    padding: 0.6rem;
    min-width: 44px;
    min-height: 44px;
  }
  #logout-btn {
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
  }
  #login-form input {
    padding: 0.85rem 1rem;
    font-size: 16px;
  }
  #login-form button {
    padding: 0.85rem;
    font-size: 1rem;
  }
}
