:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --border: #e5e5e5;
  --text: #1a1a1a;
  --text-secondary: #6b6b6b;
  --accent: #0d6efd;
  --accent-light: #e8f0fe;
  --user-bg: #0d6efd;
  --user-text: #ffffff;
  --tool-bg: #f0f4f9;
  --negative: #16a34a;
  --positive: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --sidebar-bg: #202123;
  --sidebar-text: #ececf1;
  --sidebar-hover: #2a2b32;
  --sidebar-active: #343541;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); }

/* ═══════ Auth Overlay ═══════ */
#auth-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.auth-card {
  width: 380px; padding: 40px 36px; background: var(--surface);
  border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.2);
  text-align: center;
}
.auth-logo { font-size: 48px; margin-bottom: 8px; }
.auth-card h2 { font-size: 22px; margin-bottom: 4px; }
.auth-subtitle { font-size: 13px; color: var(--text-secondary); margin-bottom: 28px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-card input {
  padding: 11px 14px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 14.5px; outline: none; font-family: inherit;
}
.auth-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-light); }
.auth-card button[type="submit"] {
  padding: 11px; background: var(--accent); color: #fff;
  border: none; border-radius: 10px; font-size: 15px; font-weight: 500;
  cursor: pointer; transition: background .15s;
}
.auth-card button[type="submit"]:hover { background: #0b5ed7; }
.auth-toggle { margin-top: 16px; font-size: 13px; color: var(--text-secondary); }
.auth-toggle a { color: var(--accent); text-decoration: none; font-weight: 500; }
.guest-btn {
  width: 100%; padding: 10px; margin-top: 8px;
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 10px;
  font-size: 14px; cursor: pointer; transition: all .15s;
}
.guest-btn:hover { background: var(--accent-light); }
.auth-error { color: var(--positive); font-size: 13px; min-height: 18px; margin-top: 8px; }
.auth-card .lang-btn { position: absolute; top: 16px; right: 16px; }
.auth-card { position: relative; }

/* ═══════ App Layout ═══════ */
#app {
  display: flex; height: 100vh;
}

/* ═══════ Sidebar ═══════ */
#sidebar {
  width: 260px; flex-shrink: 0;
  display: flex; flex-direction: column;
  background: var(--sidebar-bg); color: var(--sidebar-text);
  transition: width .2s, padding .2s;
  overflow: hidden;
}
#sidebar.collapsed { width: 0; padding: 0; }

.sidebar-header { padding: 12px; }
#new-chat-btn {
  width: 100%; padding: 10px 14px;
  display: flex; align-items: center; gap: 8px;
  border: 1px solid #565869; border-radius: 8px;
  background: transparent; color: var(--sidebar-text);
  font-size: 14px; cursor: pointer; transition: background .15s;
}
#new-chat-btn:hover { background: var(--sidebar-hover); }

#conv-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; margin-bottom: 2px;
  border-radius: 8px; cursor: pointer; font-size: 13px;
  transition: background .1s;
}
.conv-item:hover { background: var(--sidebar-hover); }
.conv-item.active { background: var(--sidebar-active); }
.conv-title {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-delete {
  visibility: hidden; background: none; border: none;
  color: #999; cursor: pointer; font-size: 16px; padding: 0 4px;
}
.conv-item:hover .conv-delete { visibility: visible; }
.conv-delete:hover { color: #fff; }

.sidebar-footer {
  padding: 12px; border-top: 1px solid #444;
  font-size: 13px; display: flex; justify-content: space-between; align-items: center;
}
#logout-btn {
  background: none; border: 1px solid #565869; border-radius: 6px;
  color: var(--sidebar-text); padding: 4px 12px; font-size: 12px; cursor: pointer;
}
#logout-btn:hover { background: var(--sidebar-hover); }

#conv-list::-webkit-scrollbar { width: 4px; }
#conv-list::-webkit-scrollbar-thumb { background: #555; border-radius: 2px; }

/* ═══════ Chat Main ═══════ */
#chat-main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0;
}

header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface); flex-shrink: 0;
}
#sidebar-toggle {
  background: none; border: none; font-size: 20px; cursor: pointer;
  color: var(--text-secondary); padding: 4px;
}
header .logo { font-size: 24px; }
header h1 { font-size: 17px; font-weight: 600; }
header p { font-size: 12px; color: var(--text-secondary); }

/* Language toggle button */
.lang-btn {
  padding: 5px 12px; font-size: 13px; font-weight: 500;
  background: var(--accent-light); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 6px;
  cursor: pointer; transition: all .15s; white-space: nowrap;
}
.lang-btn:hover { background: var(--accent); color: #fff; }

/* ═══════ Messages ═══════ */
#messages {
  flex: 1; overflow-y: auto;
  padding: 20px 24px;
  display: flex; flex-direction: column; gap: 16px;
  max-width: 820px; width: 100%; margin: 0 auto;
}

.msg { max-width: 90%; line-height: 1.65; font-size: 14.5px; }
.msg.user {
  align-self: flex-end;
  background: var(--user-bg); color: var(--user-text);
  padding: 10px 16px; border-radius: var(--radius) var(--radius) 4px var(--radius);
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bg);
  padding: 12px 16px; border-radius: var(--radius) var(--radius) var(--radius) 4px;
}
.msg.assistant p { margin-bottom: 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant ul, .msg.assistant ol { padding-left: 20px; margin-bottom: 8px; }
.msg.assistant table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.msg.assistant th { background: var(--accent-light); font-weight: 600; }
.msg.assistant strong { font-weight: 600; }
.msg.assistant code { background: #f1f1f1; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.msg.assistant a { color: var(--accent); text-decoration: none; }
.msg.assistant a:hover { text-decoration: underline; }
.msg.assistant img { max-width: 100%; border-radius: 8px; margin: 8px 0; border: 1px solid var(--border); }

/* ═══════ Tool indicator ═══════ */
.tool-indicator {
  align-self: flex-start;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  background: var(--tool-bg); border-radius: 8px;
  font-size: 13px; color: var(--text-secondary);
}
.tool-indicator .spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .6s linear infinite;
}
.tool-indicator.done .spinner { display: none; }
.tool-indicator.done::before { content: '\2713'; color: var(--negative); font-weight: bold; margin-right: -4px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ═══════ Quick actions ═══════ */
#quick-actions {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 0 24px 12px;
  max-width: 820px; width: 100%; margin: 0 auto;
}
.quick-btn {
  padding: 6px 14px; font-size: 13px;
  background: var(--accent-light); color: var(--accent);
  border: 1px solid transparent; border-radius: 20px;
  cursor: pointer; transition: all .15s;
}
.quick-btn:hover { background: var(--accent); color: #fff; }

/* ═══════ Input area ═══════ */
#input-area {
  display: flex; gap: 10px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  background: var(--surface); flex-shrink: 0;
  max-width: 820px; width: 100%; margin: 0 auto;
}
#input-area textarea {
  flex: 1; resize: none;
  padding: 10px 14px; font-size: 14.5px;
  border: 1px solid var(--border); border-radius: 10px;
  outline: none; font-family: inherit;
  min-height: 44px; max-height: 120px;
}
#input-area textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-light); }
#input-area button {
  padding: 0 20px; height: 44px;
  background: var(--accent); color: #fff;
  border: none; border-radius: 10px;
  font-size: 14.5px; font-weight: 500; cursor: pointer;
  transition: background .15s;
}
#input-area button:hover { background: #0b5ed7; }
#input-area button:disabled { opacity: .5; cursor: not-allowed; }

/* ═══════ Welcome ═══════ */
.welcome {
  text-align: center; padding: 60px 20px;
  color: var(--text-secondary);
}
.welcome h2 { font-size: 20px; color: var(--text); margin-bottom: 8px; }
.welcome p { font-size: 14px; }

/* ═══════ Scrollbar ═══════ */
#messages::-webkit-scrollbar { width: 6px; }
#messages::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

/* ═══════ Table overflow fix (for long rows in chat) ═══════ */
.msg.assistant table { display: block; overflow-x: auto; white-space: nowrap; }
.msg.assistant table td, .msg.assistant table th { white-space: nowrap; }

/* ═══════ Backdrop for mobile sidebar ═══════ */
#sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 49;
}
#sidebar-backdrop.show { display: block; }

/* ═══════ Mobile (≤768px) ═══════ */
@media (max-width: 768px) {
  /* Sidebar → drawer mode, closed by default on mobile */
  #sidebar {
    position: fixed; left: 0; top: 0;
    height: 100vh; z-index: 50;
    width: 280px; max-width: 80vw;
    transition: transform .25s ease;
    transform: translateX(-100%);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar.collapsed { transform: translateX(-100%); width: 280px; }

  /* Header compact */
  header { padding: 10px 12px; gap: 8px; }
  header h1 { font-size: 16px; }
  header p { display: none; }    /* hide subtitle on mobile */
  header .logo { font-size: 20px; }

  /* Remove horizontal padding duplication */
  #messages {
    padding: 14px 12px;
    max-width: 100%;
  }
  .msg { max-width: 92%; font-size: 14px; }
  .msg.user { padding: 8px 12px; }
  .msg.assistant { padding: 10px 12px; }

  /* Quick actions: horizontal scroll instead of wrap */
  #quick-actions {
    padding: 0 12px 10px;
    flex-wrap: nowrap; overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #quick-actions::-webkit-scrollbar { display: none; }
  .quick-btn { flex-shrink: 0; font-size: 12.5px; padding: 6px 12px; }

  /* Input area */
  #input-area {
    padding: 10px 12px;
    max-width: 100%;
    gap: 8px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
  #input-area textarea { font-size: 16px; /* prevent iOS zoom */ }
  #input-area button { padding: 0 16px; font-size: 14px; }

  /* Auth card fills screen better */
  .auth-card {
    width: calc(100vw - 32px); max-width: 400px;
    padding: 32px 24px;
  }

  /* Language button smaller */
  .lang-btn { padding: 4px 10px; font-size: 12px; }

  /* Welcome smaller */
  .welcome { padding: 40px 16px; }
  .welcome h2 { font-size: 18px; }

  /* Chart images full-width */
  .msg.assistant img { max-width: 100%; }

  /* Allow tables to wrap cell content on very small screens */
  .msg.assistant table { font-size: 12px; }
  .msg.assistant table td, .msg.assistant table th {
    padding: 5px 8px;
    white-space: normal;    /* override desktop nowrap */
  }
}

/* ═══════ Very small screens (≤380px) ═══════ */
@media (max-width: 380px) {
  header .logo { display: none; }
  header h1 { font-size: 15px; }
  .auth-card { padding: 28px 20px; }
}
