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

:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --primary: #4f6ef7;
  --primary-hover: #3b56d9;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --border: #e5e7eb;
  --user-bubble-bg: #4f6ef7;
  --user-bubble-text: #fff;
  --assistant-bubble-bg: #ffffff;
  --assistant-bubble-text: #1a1a2e;
  --code-bg: #1e1e2e;
  --code-text: #cdd6f4;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  --radius: 16px;
  --radius-sm: 10px;
  --max-width: 860px;
  --header-h: 60px;
  --input-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans SC', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #1e1e2e;
    --primary: #6c8aff;
    --primary-hover: #5370e0;
    --text: #e4e4e7;
    --text-secondary: #a1a1aa;
    --border: #2d2d3d;
    --user-bubble-bg: #6c8aff;
    --user-bubble-text: #fff;
    --assistant-bubble-bg: #1e1e2e;
    --assistant-bubble-text: #e4e4e7;
    --code-bg: #11111b;
    --code-text: #cdd6f4;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
  }
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  line-height: 1.6;
}

/* ===== Header ===== */
.chat-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0 16px;
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.chat-header .logo {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}

.chat-header h1 {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* ===== Messages Container ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-messages::before {
  content: '';
  flex: 1 0 40px;
}

/* ===== Message Bubbles ===== */
.message {
  display: flex;
  gap: 10px;
  max-width: 100%;
  animation: fadeIn 0.25s ease;
}

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

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

.message .avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  margin-top: 2px;
}

.message.user .avatar {
  order: 1;
  background: var(--primary);
  color: #fff;
}

.message.assistant .avatar {
  background: #10b981;
  color: #fff;
}

.message .bubble {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  line-height: 1.65;
  word-wrap: break-word;
  white-space: pre-wrap;
}

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

.message.assistant .bubble {
  background: var(--assistant-bubble-bg);
  color: var(--assistant-bubble-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* ===== Markdown Content in Messages ===== */
.message .bubble p {
  margin-bottom: 8px;
}

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

.message .bubble code {
  background: rgba(0, 0, 0, 0.08);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.85em;
}

@media (prefers-color-scheme: dark) {
  .message .bubble code {
    background: rgba(255, 255, 255, 0.1);
  }
}

.message .bubble pre {
  background: var(--code-bg);
  color: var(--code-text);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin: 10px 0;
  overflow-x: auto;
  font-size: 0.82rem;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}

.message .bubble pre code {
  background: transparent;
  padding: 0;
  font-size: inherit;
  color: inherit;
}

.message .bubble ul,
.message .bubble ol {
  padding-left: 20px;
  margin: 6px 0;
}

.message .bubble li {
  margin-bottom: 4px;
}

.message .bubble h1,
.message .bubble h2,
.message .bubble h3,
.message .bubble h4 {
  margin: 12px 0 6px;
  font-weight: 600;
  line-height: 1.3;
}

.message .bubble h1 { font-size: 1.2rem; }
.message .bubble h2 { font-size: 1.1rem; }
.message .bubble h3 { font-size: 1.05rem; }

.message .bubble blockquote {
  border-left: 3px solid var(--primary);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--text-secondary);
}

.message .bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 0.88rem;
}

.message .bubble th,
.message .bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.message .bubble th {
  background: rgba(79, 110, 247, 0.08);
  font-weight: 600;
}

.message .bubble a {
  color: var(--primary);
  text-decoration: underline;
  word-break: break-all;
}

.message .bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

/* ===== Typing Indicator ===== */
.typing .bubble {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 16px 20px;
}

.typing .dot {
  width: 8px;
  height: 8px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: pulse 1.4s infinite;
}

.typing .dot:nth-child(2) { animation-delay: 0.2s; }
.typing .dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  flex: 1;
}

.empty-state .icon {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  background: linear-gradient(135deg, var(--primary), #10b981);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #fff;
  margin-bottom: 16px;
  box-shadow: 0 8px 24px rgba(79, 110, 247, 0.25);
}

.empty-state h2 {
  font-size: 1.15rem;
  color: var(--text);
  margin-bottom: 6px;
  font-weight: 600;
}

.empty-state p {
  font-size: 0.9rem;
  max-width: 300px;
}

/* ===== Input Area ===== */
.chat-input-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.chat-input-wrapper {
  max-width: var(--max-width);
  margin: 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px;
  transition: border-color 0.2s;
}

.chat-input-wrapper:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 110, 247, 0.15);
}

.chat-input-wrapper textarea {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 12px;
  font-family: var(--font);
  font-size: 0.95rem;
  color: var(--text);
  resize: none;
  outline: none;
  min-height: 24px;
  max-height: 120px;
  line-height: 1.5;
}

.chat-input-wrapper textarea::placeholder {
  color: var(--text-secondary);
}

.chat-input-wrapper .send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s;
  margin-bottom: 2px;
}

.chat-input-wrapper .send-btn:hover {
  background: var(--primary-hover);
}

.chat-input-wrapper .send-btn:active {
  transform: scale(0.92);
}

.chat-input-wrapper .send-btn:disabled {
  background: var(--border);
  color: var(--text-secondary);
  cursor: not-allowed;
  transform: none;
}

.send-btn svg {
  width: 20px;
  height: 20px;
}

/* ===== Config Panel ===== */
.config-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.config-bar .status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
}

.config-bar .status-dot.offline {
  background: #ef4444;
}

.config-bar button {
  background: none;
  border: 1px solid var(--border);
  padding: 2px 10px;
  border-radius: 6px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.config-bar button:hover {
  border-color: var(--primary);
  color: var(--primary);
}

/* ===== API Key Modal ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 20px;
  backdrop-filter: blur(4px);
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
  animation: modalIn 0.25s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.modal h2 {
  font-size: 1.1rem;
  margin-bottom: 4px;
}

.modal p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.modal label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text-secondary);
}

.modal input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

.modal input:focus {
  border-color: var(--primary);
}

.modal .modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  justify-content: flex-end;
}

.modal .modal-actions button {
  padding: 8px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 0.88rem;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg);
  color: var(--text);
}

.modal .modal-actions button.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.modal .modal-actions button.primary:hover {
  background: var(--primary-hover);
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .chat-header h1 {
    font-size: 0.95rem;
  }

  .message .bubble {
    max-width: 90%;
    font-size: 0.9rem;
    padding: 10px 14px;
  }

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

  .chat-input-area {
    padding: 8px 10px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .empty-state .icon {
    width: 52px;
    height: 52px;
    font-size: 22px;
  }

  .empty-state h2 {
    font-size: 1rem;
  }
}

@media (max-width: 380px) {
  .message .bubble {
    max-width: 95%;
    font-size: 0.85rem;
    padding: 8px 12px;
  }

  .message.user .avatar,
  .message.assistant .avatar {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }
}

/* ===== Scrollbar ===== */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

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

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

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* ===== Copy button for code blocks ===== */
.message .bubble pre {
  position: relative;
}

.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--code-text);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.72rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}

.message .bubble pre:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Blinking cursor for streaming */
.cursor-blink {
  animation: blink 1s step-end infinite;
}

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

/* Error message */
.message.error .bubble {
  background: #fef2f2;
  border-color: #fecaca;
  color: #991b1b;
}

@media (prefers-color-scheme: dark) {
  .message.error .bubble {
    background: #1f1414;
    border-color: #5c1a1a;
    color: #fca5a5;
  }
}
