:root {
  --bg-color: #0d1117;
  --card-bg: #161b22;
  --border-color: #30363d;
  --text-main: #ffffff;
  --text-muted: #8b949e;
  --accent-color: #2f81f7;
  --assistant-bg: #21262d;
  --assistant-text: #c9d1d9;
  --hover-bg: #21262d;
  --hover-border: #8b949e;
}

body.light-mode {
  --bg-color: #f6f8fa;
  --card-bg: #ffffff;
  --border-color: #d0d7de;
  --text-main: #1b1f24;
  --text-muted: #57606a;
  --accent-color: #2f81f7;
  --assistant-bg: #eef1f4;
  --assistant-text: #24292f;
  --hover-bg: #eef1f4;
  --hover-border: #8b949e;
}

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

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-main);
  min-height: 100vh;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ---------- Auth screen ---------- */

.auth-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 2.5rem 2rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.auth-logo {
  height: 44px;
  width: auto;
  object-fit: contain;
  margin-bottom: 0.5rem;
}

.auth-card h2 {
  font-size: 1.4rem;
}

.auth-card p {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

#google-login-btn {
  margin-top: 0.75rem;
  padding: 12px 24px;
  background-color: var(--accent-color);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
}

#google-login-btn:hover {
  background-color: #58a6ff;
}

.login-error {
  color: #f85149;
  font-size: 0.85rem;
}

/* ---------- Chat screen ---------- */

.chat-container {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 24px 20px;
  gap: 16px;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.header-branding {
  display: flex;
  align-items: center;
  gap: 14px;
}

.header-logo {
  height: 34px;
  width: auto;
  object-fit: contain;
}

.brand-divider {
  width: 1px;
  height: 24px;
  background-color: var(--border-color);
}

.chat-header h1 {
  font-size: 1.4rem;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.chat-header h1 span {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--text-muted);
}

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

.logout-btn {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.logout-btn:hover {
  background: var(--hover-bg);
  border-color: var(--hover-border);
}

.chat-box {
  flex: 1;
  overflow-y: auto;
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  padding: 10px 14px;
  border-radius: 6px;
  max-width: 85%;
  word-wrap: break-word;
  font-size: 0.95rem;
  line-height: 1.4;
}

.message.user {
  background-color: var(--accent-color);
  color: #ffffff;
  align-self: flex-end;
  margin-left: auto;
}

.message.assistant {
  background-color: var(--assistant-bg);
  color: var(--assistant-text);
  align-self: flex-start;
  border: 1px solid var(--border-color);
  max-width: 95%;
}

.message.system {
  background-color: transparent;
  color: var(--text-muted);
  align-self: center;
  font-size: 0.85rem;
  border: 1px dashed var(--border-color);
}

/* ---------- Markdown rendering inside assistant messages ---------- */

.message.assistant h1,
.message.assistant h2,
.message.assistant h3,
.message.assistant h4 {
  margin: 0.6em 0 0.35em;
  font-size: 1rem;
  font-weight: 700;
  color: var(--assistant-text);
}

.message.assistant h1:first-child,
.message.assistant h2:first-child,
.message.assistant h3:first-child,
.message.assistant h4:first-child {
  margin-top: 0;
}

.message.assistant p {
  margin: 0.4em 0;
}

.message.assistant ul,
.message.assistant ol {
  margin: 0.3em 0 0.5em 1.3em;
}

.message.assistant li {
  margin-bottom: 0.25em;
}

.message.assistant strong {
  color: var(--assistant-text);
  font-weight: 700;
}

.message.assistant hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 0.75em 0;
}

.message.assistant code {
  background: rgba(127, 127, 127, 0.18);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.85em;
}

.message.assistant a {
  color: var(--accent-color);
}

.message.assistant table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.85rem;
}

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

.message.assistant th {
  background: rgba(127, 127, 127, 0.15);
  font-weight: 700;
  color: var(--assistant-text);
}

.message.assistant tr:nth-child(even) td {
  background: rgba(127, 127, 127, 0.06);
}

.chat-input-area {
  display: flex;
  gap: 10px;
}

#user-input {
  flex: 1;
  padding: 12px 16px;
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-main);
  font-size: 1rem;
}

#user-input:focus {
  outline: none;
  border-color: #58a6ff;
}

#send-btn {
  padding: 12px 24px;
  background-color: var(--accent-color);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}

#send-btn:hover {
  background-color: #58a6ff;
}

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

/* Bouncing loading indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 16px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background-color: var(--text-muted);
  border-radius: 50%;
  display: inline-block;
  animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1.0); }
}

/* ---------- Admin panel ---------- */

.pending-item {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 0.75rem;
}

.pending-item h4 {
  font-size: 0.95rem;
  margin-bottom: 0.35rem;
}

.pending-item p {
  color: var(--text-muted);
  font-size: 0.85rem;
  white-space: pre-wrap;
}

/* ---------- Mobile responsive header ---------- */

@media (max-width: 600px) {
  .chat-container {
    padding: 16px 12px;
  }

  .chat-header {
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .header-branding h1 {
    font-size: 1.1rem;
  }

  .header-actions {
    width: 100%;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 6px;
  }

  #user-email-display {
    order: -1;
    width: 100%;
    text-align: right;
    margin-right: 0 !important;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .logout-btn {
    font-size: 0.78rem;
    padding: 5px 10px;
  }
}
/* ---- Mobile layout (added v18) ---- */
@media (max-width: 700px) {
  html, body { overflow-x: hidden; }
  .chat-header { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .header-branding { width: 100%; gap: 10px; }
  .header-logo { height: 28px; width: auto; }
  .brand-divider { display: none; }
  .chat-header h1 { font-size: 1.25rem; }
  .header-actions { width: 100%; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
  .header-actions > :not(button) { flex: 1 1 100%; font-size: 0.8rem; overflow-wrap: anywhere; }
  .logout-btn { flex: 1 1 0; padding: 8px 6px; font-size: 0.85rem; }
  .chat-box { overflow-x: hidden; padding: 10px; }
  .message { max-width: 94%; overflow-wrap: anywhere; }
  .message.assistant { max-width: 100%; }
  .message.assistant table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .message.assistant img, .chart-wrap { max-width: 100%; }
  .chat-input-area { padding: 8px; gap: 6px; }
  #user-input { min-width: 0; flex: 1; font-size: 16px; }
  #send-btn { flex-shrink: 0; padding: 10px 14px; }
  #admin-view { padding: 1rem !important; }
  #status-panel table { display: block; overflow-x: auto; }
}

/* ---- Mobile: keep the input bar on screen (v19) ---- */
@media (max-width: 700px) {
  html, body { height: 100%; }
  .chat-container { height: 100dvh !important; max-height: 100dvh !important; min-height: 0 !important; margin: 0 !important; border-radius: 0 !important; display: flex !important; flex-direction: column !important; }
  #chat-view { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
  .chat-box { flex: 1 1 auto !important; min-height: 0 !important; overflow-y: auto !important; }
  .chat-input-area { flex: 0 0 auto; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
}

/* Desktop and laptop: use more of the screen. Phones are unaffected (rules below 700px stay as they are). */
@media (min-width: 1000px) {
  .chat-container { max-width: min(1600px, 96vw); }
  .message.assistant { max-width: 100%; }
  .message.user { max-width: 70%; }
  .message.assistant table { width: 100%; }
}
