/* ============================================================================ */
/* Variables                                                                    */
/* ============================================================================ */

:root {
  --bg-primary:    #f6f8fa;
  --bg-sidebar:    #f1f3f5;
  --bg-input:      #ffffff;
  --bg-user-msg:   #eef4ff;
  --bg-hover:      #e8efff;
  --bg-active:     #dbe4ff;
  --bg-modal:      #ffffff;
  --bg-btn:        #2d6cdf;
  --bg-btn-hover:  #1f4fb2;

  --text-primary:  #222222;
  --text-muted:    #555555;
  --text-btn:      #ffffff;

  --border:        #e0e4e8;
  --ring-color:    #2d6cdf;
  --ring-warn:     #a66b00;
  --ring-danger:   #b33434;

  --radius:        8px;
  --sidebar-width: 260px;
  --input-max:     860px;
}

/* ============================================================================ */
/* Reset                                                                         */
/* ============================================================================ */

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               Helvetica, Arial, sans-serif;
  font-size: 15px;
  background: var(--bg-primary);
  color: var(--text-primary);
}

.hidden { display: none !important; }

button { cursor: pointer; border: none; background: none; color: inherit; font: inherit; }
textarea, input { font: inherit; color: inherit; background: none; border: none; outline: none; resize: none; }
a { color: inherit; }
ul { list-style: none; }

/* ============================================================================ */
/* App shell — sidebar + main                                                   */
/* ============================================================================ */

#app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ============================================================================ */
/* Sidebar                                                                       */
/* ============================================================================ */

#sidebar {
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

#sidebar-header {
  padding: 12px 10px 10px;
  flex-shrink: 0;
  background: linear-gradient(160deg, #1e3a5f 0%, #2d6cdf 60%, #4f9cf9 100%);
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  color: #f1f5f9;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}
.new-chat-btn:hover { background: rgba(255,255,255,0.15); }
.new-chat-btn svg { flex-shrink: 0; }

/* Conversation list */

#conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px;
}

#conv-list::-webkit-scrollbar { width: 4px; }
#conv-list::-webkit-scrollbar-track { background: transparent; }
#conv-list::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }

.conv-group-label {
  padding: 8px 8px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
  gap: 6px;
}
.conv-item:hover  { background: var(--bg-hover); color: var(--text-primary); }
.conv-item.active {
  background: var(--bg-active);
  color: var(--text-primary);
  border-left: 3px solid var(--ring-color);
  padding-left: 7px;
}

.conv-item-title {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.conv-delete-btn {
  flex-shrink: 0;
  opacity: 0;
  padding: 2px 4px;
  border-radius: 4px;
  color: var(--text-muted);
  transition: opacity 0.1s, color 0.1s;
}
.conv-item:hover .conv-delete-btn { opacity: 1; }
.conv-delete-btn:hover { color: var(--ring-danger); }

/* Sidebar footer */

#sidebar-footer {
  flex-shrink: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#token-usage-widget {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

.token-ring { flex-shrink: 0; }
.token-ring-bg  { fill: none; stroke: #d0d5dd; stroke-width: 3; }
.token-ring-arc {
  fill: none;
  stroke: var(--ring-color);
  stroke-width: 3;
  stroke-linecap: round;
  transition: stroke-dasharray 0.4s ease, stroke 0.4s ease;
}

.sidebar-sign-out {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 13.5px;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.sidebar-sign-out:hover { background: var(--bg-hover); color: var(--text-primary); }

/* ============================================================================ */
/* Main chat area                                                                */
/* ============================================================================ */

#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ---- Empty state ---- */

#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  gap: 16px;
}

.empty-logo {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-primary);
}

.empty-tagline {
  font-size: 15px;
  color: var(--text-muted);
  text-align: center;
  max-width: 420px;
}

.starter-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  max-width: 540px;
  width: 100%;
  margin-top: 8px;
}

.starter-btn {
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-muted);
  font-size: 13.5px;
  text-align: left;
  line-height: 1.4;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.starter-btn:hover {
  background: var(--bg-active);
  color: var(--text-primary);
  border-color: #a8c0f0;
}

/* ---- Chat log ---- */

#chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
}

#chat-log::-webkit-scrollbar { width: 4px; }
#chat-log::-webkit-scrollbar-track { background: transparent; }
#chat-log::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }

/* ---- Message rows ---- */

.msg-row {
  display: flex;
  padding: 6px 24px;
  gap: 12px;
  width: 100%;
  max-width: 860px;
}

.msg-row--user {
  justify-content: flex-end;
}

.msg-row--assistant {
  justify-content: flex-start;
}

.msg-bubble {
  max-width: 800px;
  line-height: 1.65;
  font-size: 15px;
}

.msg-row--user .msg-bubble {
  background: var(--bg-user-msg);
  border-radius: 18px 18px 4px 18px;
  padding: 10px 16px;
  color: var(--text-primary);
}

.msg-row--assistant .msg-bubble {
  color: var(--text-primary);
  padding: 4px 0;
}

/* Thinking dots */
.thinking-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  margin: 0 2px;
  animation: dot-bounce 1.2s infinite ease-in-out;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

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

.cancel-query-btn {
  display: block;
  margin-top: 10px;
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 0;
  background: none;
  border: none;
  cursor: pointer;
}
.cancel-query-btn:hover { color: var(--ring-danger); }

/* Markdown rendering inside assistant bubbles */
.msg-markdown h1, .msg-markdown h2, .msg-markdown h3 {
  font-weight: 600;
  margin: 12px 0 4px;
  line-height: 1.3;
}
.msg-markdown h1 { font-size: 1.15rem; }
.msg-markdown h2 { font-size: 1.05rem; }
.msg-markdown h3 { font-size: 0.95rem; }
.msg-markdown p  { margin: 6px 0; }
.msg-markdown ul, .msg-markdown ol { padding-left: 20px; margin: 6px 0; }
.msg-markdown li { margin: 3px 0; }
.msg-markdown a  { color: var(--ring-color); text-decoration: underline; }
.msg-markdown a:hover { opacity: 0.8; }
.msg-markdown code {
  background: rgba(0,0,0,0.06);
  border-radius: 3px;
  padding: 1px 5px;
  font-family: monospace;
  font-size: 0.9em;
}
.msg-markdown pre {
  background: rgba(0,0,0,0.04);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-markdown pre code { background: none; padding: 0; }
.msg-markdown strong { font-weight: 600; }
.msg-markdown blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 6px 0;
}

/* Sources section */
.sources-section {
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-muted);
}

.sources-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 4px 0;
  color: var(--text-muted);
  font-size: 13px;
  transition: color 0.12s;
}
.sources-toggle:hover { color: var(--text-primary); }
.sources-toggle svg { transition: transform 0.2s; }
.sources-toggle.open svg { transform: rotate(90deg); }

.sources-list {
  display: none;
  flex-direction: column;
  gap: 4px;
  padding: 6px 0 2px 18px;
}
.sources-list.visible { display: flex; }

.source-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 12.5px;
  transition: color 0.12s;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.source-link:hover { color: var(--text-primary); text-decoration: underline; }

/* ---- Input bar ---- */

#input-bar {
  flex-shrink: 0;
  padding: 12px 24px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#input-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: var(--input-max);
  background: var(--bg-input);
  border: 1px solid #e0e0e0;
  border-radius: 28px;
  padding: 12px 12px 12px 20px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  transition: border-color 0.15s, box-shadow 0.15s;
}
#input-wrapper:focus-within {
  border-color: #c0c0c0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}

#chat-input {
  flex: 1;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.5;
  font-size: 15px;
  color: var(--text-primary);
}
#chat-input::placeholder { color: var(--text-muted); }

#btn-send {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-btn);
  color: var(--text-btn);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
}
#btn-send:hover    { background: var(--bg-btn-hover); }
#btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

.input-error {
  font-size: 13px;
  color: var(--ring-danger);
  max-width: var(--input-max);
  width: 100%;
  padding: 0 4px;
}

/* ============================================================================ */
/* Modals                                                                        */
/* ============================================================================ */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-content {
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 32px;
  width: 100%;
}

.modal-content--sm {
  max-width: 400px;
}

.modal-message {
  color: var(--text-muted);
  margin: 8px 0 20px;
  font-size: 14px;
  line-height: 1.5;
}

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

.modal-actions button {
  padding: 9px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  background: var(--bg-btn);
  color: var(--text-btn);
  transition: background 0.15s;
}
.modal-actions button:hover { background: var(--bg-btn-hover); }
.modal-actions .btn-secondary {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.modal-actions .btn-secondary:hover { background: var(--bg-active); }

/* Sign-in modal */

.modal-content--auth {
  display: flex;
  border-radius: 10px;
  overflow: hidden;
  width: 820px;
  max-width: 95vw;
  background: var(--bg-modal);
}

.auth-form-panel {
  flex: 1;
  padding: 56px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
}

.auth-form-panel h2 {
  font-size: 26px;
  font-weight: 700;
  margin: 0 0 4px;
  color: #1a1a1a;
}

.auth-form-desc {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.6;
  margin: 0 0 8px;
}

.auth-form-actions button {
  width: 100%;
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--bg-btn);
  color: var(--text-btn);
  font-size: 15px;
  font-weight: 600;
  transition: background 0.15s;
}
.auth-form-actions button:hover { background: var(--bg-btn-hover); }

.auth-preview-panel {
  flex: 1;
  background: linear-gradient(160deg, #1e3a5f 0%, #2d6cdf 60%, #4f9cf9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 56px 40px;
  color: white;
}

.auth-preview-inner {
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-preview-logo {
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: #fff;
  line-height: 1.15;
}

.auth-preview-tagline {
  font-size: 15px;
  opacity: 0.85;
  line-height: 1.5;
  margin: 0 0 6px;
}

.auth-preview-features {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-preview-features li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 14px;
  opacity: 0.9;
  line-height: 1.4;
}

.auth-preview-features svg {
  flex-shrink: 0;
  margin-top: 1px;
  color: #a8c8fd;
}

.auth-preview-clouds {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  padding-top: 8px;
}

.cloud-provider-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  opacity: 0.8;
}

.cloud-provider-icon svg {
  width: 56px;
  height: 56px;
}

.cloud-provider-icon span {
  font-size: 10px;
  font-weight: 700;
  color: rgba(255,255,255,0.85);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ============================================================================ */
/* Mobile responsive                                                             */
/* ============================================================================ */

#mobile-topbar {
  display: none;
}

#sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  color: var(--text-muted);
  flex-shrink: 0;
}
#sidebar-toggle:hover { background: var(--bg-hover); }

@media (max-width: 640px) {

  /* Hide sidebar by default on mobile — shown when toggled */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  /* Hamburger toggle visible on mobile */
  #sidebar-toggle {
    display: flex;
  }

  /* Mobile top bar above chat */
  #chat-main {
    position: relative;
  }

  #mobile-topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  #mobile-topbar-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
  }

  /* Allow empty state and chat log to scroll on mobile */
  #chat-main {
    overflow-y: auto;
  }

  #empty-state {
    overflow-y: auto;
    justify-content: flex-start;
    padding-top: 32px;
  }

  /* Tighter chat padding on mobile */
  .msg-row {
    padding: 4px 12px;
  }

  #input-bar {
    padding: 8px 12px 14px;
  }

  /* Override hardcoded font sizes in sidebar */
  .conv-item {
    font-size: 20px;
    padding: 12px 14px;
  }

  .conv-group-label {
    font-size: 16px;
  }

  .new-chat-btn {
    font-size: 20px;
    padding: 14px 16px;
  }

  .sidebar-sign-out {
    font-size: 20px;
  }

  #token-usage-widget {
    font-size: 18px;
  }

  /* 1.5x font sizes for readability on small screens */
  html, body {
    font-size: 22px;
  }

  .msg-bubble {
    font-size: 22px;
  }

  #chat-input {
    font-size: 22px;
  }

  .empty-logo {
    font-size: 39px;
  }

  .empty-tagline {
    font-size: 22px;
  }

  .starter-btn {
    font-size: 20px;
    padding: 14px 16px;
  }

  /* Stack login modal vertically — hide gradient panel */
  .modal-content--auth {
    flex-direction: column;
    width: 95vw;
  }

  .auth-preview-panel {
    display: none;
  }

  .auth-form-panel {
    padding: 40px 28px;
  }

  /* Starter grid single column on mobile */
  .starter-grid {
    grid-template-columns: 1fr;
    max-width: 100%;
  }

  /* Dim overlay when sidebar is open */
  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 49;
  }

  #sidebar-overlay.visible {
    display: block;
  }
}
