/* ========== Reset & Base ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', 'Helvetica Neue', sans-serif;
  background: #f5f5f5;
  color: #1a1a1a;
  -webkit-font-smoothing: antialiased;
}

/* ========== Login Page ========== */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: #f0f2f5;
}
.login-container {
  background: #fff; border-radius: 16px; padding: 44px 36px;
  width: 400px; max-width: 95vw;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06), 0 4px 16px rgba(0,0,0,0.04);
}
.login-header { text-align: center; margin-bottom: 32px; }
.login-header h1 { font-size: 22px; font-weight: 600; color: #1a1a1a; margin-bottom: 6px; }
.login-header p { font-size: 13px; color: #999; }
.login-form .form-group { margin-bottom: 18px; }
.login-form label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: #555; }
.login-form input {
  width: 100%; padding: 10px 14px; border: 1px solid #e0e0e0; border-radius: 10px;
  font-size: 14px; transition: border-color 0.15s, box-shadow 0.15s; background: #fafafa;
}
.login-form input:focus { outline: none; border-color: #4f6bf7; box-shadow: 0 0 0 3px rgba(79,107,247,0.1); background: #fff; }
.form-error { color: #e53e3e; font-size: 12px; min-height: 18px; margin-bottom: 6px; }
.btn-primary {
  background: #1a1a1a; color: #fff; border: none; padding: 11px 20px;
  border-radius: 10px; font-size: 15px; font-weight: 500; cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:hover { background: #333; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-block { width: 100%; }
.login-hint { text-align: center; font-size: 12px; color: #bbb; margin-top: 14px; }

/* ========== Chat Layout ========== */
.chat-body { height: 100vh; overflow: hidden; }
.chat-app { display: flex; height: 100vh; }

/* ========== Sidebar (Light) ========== */
.chat-sidebar {
  width: 260px; background: #f9fafb; border-right: 1px solid #eee;
  display: flex; flex-direction: column; flex-shrink: 0;
}
.sidebar-header { padding: 14px; }
.btn-new-chat {
  width: 100%; padding: 10px; background: #fff; color: #333;
  border: 1px solid #e5e5e5; border-radius: 10px; cursor: pointer;
  font-size: 14px; font-weight: 500;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  transition: background 0.15s, border-color 0.15s;
}
.btn-new-chat:hover { background: #f0f0f0; border-color: #d0d0d0; }
.btn-new-chat svg { flex-shrink: 0; }

.sidebar-conversations { flex: 1; overflow-y: auto; padding: 8px 10px; }
.sidebar-section-title { font-size: 11px; font-weight: 600; color: #aaa; text-transform: uppercase; letter-spacing: 0.5px; padding: 8px 6px 6px; }
.conversation-list { display: flex; flex-direction: column; gap: 2px; }
.conversation-item {
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  font-size: 13px; color: #555; transition: background 0.1s;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
.conversation-item .session-info { flex: 1; min-width: 0; overflow: hidden; }
.conversation-item:hover { background: #f0f0f0; }
.conversation-item:hover .btn-delete-session { opacity: 1; }
.conversation-item.active { background: #efefef; color: #1a1a1a; font-weight: 500; }
.btn-delete-session {
  opacity: 0; font-size: 14px; background: none; border: none;
  color: #ccc; cursor: pointer; padding: 0 4px; line-height: 1;
  transition: opacity 0.15s, color 0.15s; flex-shrink: 0;
}
.btn-delete-session:hover { color: #e53e3e; }

.sidebar-footer {
  padding: 10px 14px; border-top: 1px solid #eee;
  display: flex; align-items: center; justify-content: space-between;
}
.sidebar-user { display: flex; align-items: center; gap: 8px; min-width: 0; }
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%; background: #4f6bf7;
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600; flex-shrink: 0;
}
.user-info { display: flex; flex-direction: column; min-width: 0; }
.user-name { font-size: 13px; font-weight: 500; color: #333; }
.user-tenant { font-size: 11px; color: #999; }
.btn-logout {
  width: 30px; height: 30px; border-radius: 6px; border: none; background: transparent;
  color: #999; cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all 0.15s; flex-shrink: 0;
}
.btn-logout:hover { background: #fee2e2; color: #e53e3e; }

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

/* Messages Area */
.chat-messages { flex: 1; overflow-y: auto; }

/* Welcome */
.welcome-message {
  text-align: center; max-width: 620px; margin: 100px auto 0; padding: 0 20px;
}
.welcome-logo {
  width: 52px; height: 52px;
  border-radius: 14px; display: block; margin: 0 auto 20px;
  object-fit: contain;
}
.welcome-message h2 { font-size: 22px; font-weight: 600; margin-bottom: 6px; color: #1a1a1a; }
.welcome-sub { font-size: 14px; color: #999; margin-bottom: 28px; }

.example-queries { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.example-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; background: #fff; border: 1px solid #e5e5e5;
  border-radius: 20px; cursor: pointer; font-size: 13px; color: #555;
  transition: all 0.15s; font-family: inherit;
}
.example-chip:hover { background: #f5f5f5; border-color: #d0d0d0; color: #1a1a1a; }
.example-chip svg { flex-shrink: 0; color: #999; }

/* ========== Message Bubbles ========== */
.message { padding: 16px 20px; animation: fadeIn 0.25s; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.message-user {
  background: #fff; border-bottom: 1px solid #f5f5f5;
}
.message-user .bubble {
  max-width: 720px; margin: 0 auto;
  font-size: 15px; line-height: 1.6; color: #1a1a1a;
}

.message-assistant {
  background: #fafafa; border-bottom: 1px solid #f0f0f0;
}
.message-assistant .bubble {
  max-width: 720px; margin: 0 auto; font-size: 14px;
}

/* Badges */
.bubble .badge { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; padding: 3px 10px; border-radius: 10px; margin-bottom: 10px; font-weight: 500; }
.badge-cache { background: #e6f7ed; color: #1a7d4c; }
.badge-new { background: #fef3c7; color: #92400e; }
.badge-refine { background: #ede9fe; color: #5b21b6; }
.badge-quick { background: #dbeafe; color: #1e40af; }

/* User message */
.message-user .bubble-inner {
  display: flex; align-items: flex-start; gap: 10px;
}
.message-user .user-msg-avatar {
  width: 28px; height: 28px; border-radius: 50%; background: #4f6bf7;
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600; flex-shrink: 0;
}

/* ========== SQL Block ========== */
.sql-block { margin: 12px 0; }
.sql-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.sql-label { font-size: 11px; color: #aaa; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.btn-copy {
  font-size: 11px; padding: 3px 10px; background: #fff; border: 1px solid #e5e5e5;
  border-radius: 5px; cursor: pointer; color: #888; transition: all 0.15s;
}
.btn-copy:hover { background: #f0f0f0; border-color: #ccc; color: #555; }
.sql-code {
  background: #f9fafb; border: 1px solid #eee; color: #374151;
  padding: 12px 14px; border-radius: 8px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Menlo', monospace;
  font-size: 13px; line-height: 1.6; overflow-x: auto; white-space: pre-wrap; word-break: break-all;
}

/* ========== Explanation ========== */
.explanation { font-size: 13px; color: #888; margin: 8px 0; line-height: 1.5; }

/* ========== Results ========== */
.results-section { margin-top: 14px; }
.results-info { font-size: 12px; color: #aaa; margin-bottom: 8px; }
.results-info strong { color: #555; font-weight: 600; }
.results-table-wrapper {
  overflow-x: auto; max-height: 380px; overflow-y: auto;
  border: 1px solid #eee; border-radius: 10px;
}
.results-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.results-table th {
  background: #fafafa; padding: 8px 12px; text-align: left;
  font-weight: 600; color: #666; border-bottom: 1px solid #eee;
  position: sticky; top: 0; white-space: nowrap; font-size: 12px;
}
.results-table td {
  padding: 8px 12px; border-bottom: 1px solid #f5f5f5;
  white-space: nowrap; max-width: 280px; overflow: hidden; text-overflow: ellipsis;
  color: #444;
}
.results-table tr:last-child td { border-bottom: none; }
.results-table tr:hover td { background: #fafbfd; }

/* ========== Actions ========== */
.message-actions { margin-top: 12px; display: flex; gap: 8px; }
.btn-action {
  font-size: 12px; padding: 6px 14px; border-radius: 7px; cursor: pointer;
  border: 1px solid #e5e5e5; background: #fff; color: #666;
  transition: all 0.15s; font-weight: 500;
}
.btn-action:hover { background: #f5f5f5; border-color: #ccc; }
.export-btn {
  background: #1a1a1a; color: #fff; border-color: #1a1a1a;
}
.export-btn:hover { background: #333; border-color: #333; }

/* ========== Loading ========== */
.message-loading { background: #fafafa; }
.message-loading .bubble { display: flex; align-items: center; gap: 10px; }
.typing-indicator { display: flex; gap: 4px; }
.typing-indicator span { width: 6px; height: 6px; background: #ccc; border-radius: 50%; animation: typing 1.4s infinite; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: 0.3; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ========== Error ========== */
.message-error { background: #fffbfb; }
.message-error .bubble { color: #991b1b; }
.error-text { font-size: 14px; font-weight: 500; }
.error-hint { font-size: 12px; color: #ef4444; margin-top: 4px; }

/* ========== Input Area ========== */
.chat-input-area { padding: 14px 20px 18px; background: #fff; border-top: 1px solid #f0f0f0; }
.chat-input-wrapper {
  display: flex; align-items: flex-end; gap: 8px;
  background: #f5f5f5; border: 1px solid transparent;
  border-radius: 16px; padding: 6px 6px 6px 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.chat-input-wrapper:focus-within {
  border-color: #ddd; background: #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.04);
}
.chat-input-wrapper textarea {
  flex: 1; border: none; background: transparent; padding: 8px 0;
  font-size: 14px; resize: none; outline: none; font-family: inherit;
  line-height: 1.5; max-height: 120px; color: #1a1a1a;
}
.chat-input-wrapper textarea::placeholder { color: #bbb; }
.btn-send {
  width: 34px; height: 34px; background: #1a1a1a; color: #fff;
  border: none; border-radius: 10px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s; flex-shrink: 0;
}
.btn-send:hover { background: #333; }
.btn-send:disabled { opacity: 0.3; cursor: not-allowed; }
.btn-send svg { width: 16px; height: 16px; }

.input-hint { text-align: center; font-size: 11px; color: #ccc; margin-top: 8px; }

/* ========== No Results ========== */
.no-results { text-align: center; padding: 24px; color: #bbb; font-size: 13px; }

/* ========== Scrollbar ========== */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #e0e0e0; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #ccc; }

/* ========== SQL Edit ========== */
.sql-edit {
  width: 100%; background: #f9fafb; border: 1px solid #e5e5e5;
  border-radius: 8px; padding: 12px 14px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px; line-height: 1.6; resize: vertical;
  color: #374151; min-height: 80px; box-sizing: border-box;
}
.sql-edit:focus { outline: none; border-color: #4f6bf7; box-shadow: 0 0 0 3px rgba(79,107,247,0.1); }
.sql-actions { display: flex; gap: 8px; margin-top: 8px; }
.btn-execute-sql {
  padding: 6px 16px; background: #4f6bf7; color: #fff;
  border: none; border-radius: 7px; cursor: pointer;
  font-size: 13px; font-weight: 500; transition: background 0.15s;
}
.btn-execute-sql:hover { background: #3d5ad4; }
.btn-regenerate-sql {
  padding: 6px 16px; background: #fff; color: #666;
  border: 1px solid #e5e5e5; border-radius: 7px; cursor: pointer;
  font-size: 13px; transition: background 0.15s;
}
.btn-regenerate-sql:hover { background: #f5f5f5; }

/* ========== Results Panel (table + chart stacked vertically) ========== */
.results-panel { margin-top: 8px; }
.results-panel .results-table-wrapper { max-height: 400px; margin-bottom: 12px; }
.results-panel .chart-container { width: 100%; height: 400px; border: 1px solid #eee; border-radius: 10px; }

/* ========== Stat Cards ========== */
.stat-cards { display: flex; gap: 16px; flex-wrap: wrap; align-items: stretch; height: 100%; }
.stat-card { flex: 1; min-width: 100px; text-align: center; padding: 24px 16px; background: #fafbfd; border: 1px solid #eee; border-radius: 10px; display: flex; flex-direction: column; justify-content: center; }
.stat-value { font-size: 32px; font-weight: 700; color: #1a1a1a; line-height: 1.2; word-break: break-all; }
.stat-label { font-size: 12px; color: #999; margin-top: 6px; }

/* ========== Pagination ========== */
.pagination { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; padding: 8px 0; }
.pagination-info { font-size: 12px; color: #999; }
.pagination-btns { display: flex; align-items: center; gap: 4px; }
.page-btn {
  padding: 4px 10px; border: 1px solid #e5e5e5; background: #fff;
  border-radius: 5px; font-size: 12px; color: #666; cursor: pointer; transition: background 0.15s;
}
.page-btn:hover { background: #f5f5f5; }
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.page-indicator { font-size: 12px; color: #888; padding: 0 8px; }

/* ========== History ========== */
.conversation-time { display: block; font-size: 10px; color: #bbb; margin-top: 2px; }

/* ========== Sidebar Toggle ========== */
.btn-sidebar-toggle {
  width: 32px; height: 32px; background: transparent; border: none;
  border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  color: #999; flex-shrink: 0; transition: all 0.15s;
}
.btn-sidebar-toggle:hover { background: #e8e8e8; color: #555; }

.chat-sidebar {
  transition: width 0.25s ease, min-width 0.25s ease, border 0.25s ease;
}
.chat-sidebar.collapsed { width: 0; min-width: 0; overflow: hidden; border-right: none; }

/* Sidebar overlay styles moved to @media (max-width: 1024px) below */

/* ========== Sidebar Tabs ========== */
.sidebar-tabs { display: flex; padding: 0 10px; gap: 2px; }
.sidebar-tab {
  flex: 1; padding: 7px 0; border: none; background: transparent;
  font-size: 12px; color: #999; cursor: pointer; border-radius: 6px 6px 0 0;
  transition: all 0.15s; font-weight: 500;
}
.sidebar-tab:hover { color: #555; }
.sidebar-tab.active { color: #1a1a1a; background: #fff; }

/* ========== Template Panel ========== */
.sidebar-templates { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.template-search { padding: 8px 10px; }
.template-search-input {
  width: 100%; padding: 7px 10px; border: 1px solid #e5e5e5; border-radius: 8px;
  font-size: 12px; background: #fff; outline: none; font-family: inherit;
  transition: border-color 0.15s;
}
.template-search-input:focus { border-color: #4f6bf7; }
.template-categories { padding: 4px 10px; display: flex; gap: 4px; flex-wrap: wrap; }
.template-cat-chip {
  padding: 3px 8px; border: 1px solid #eee; border-radius: 10px; font-size: 10px;
  cursor: pointer; color: #888; background: #fff; transition: all 0.15s;
}
.template-cat-chip:hover { border-color: #ccc; color: #555; }
.template-cat-chip.active { background: #4f6bf7; color: #fff; border-color: #4f6bf7; }
.template-list { flex: 1; overflow-y: auto; padding: 4px 10px 8px; }
.template-item {
  padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
  color: #555; transition: background 0.1s; line-height: 1.4;
}
.template-item:hover { background: #f0f0f0; }
.template-item-name { font-weight: 500; color: #333; margin-bottom: 2px; }
.template-item-sql {
  font-family: 'SF Mono', 'Fira Code', monospace; font-size: 10px;
  color: #999; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.template-item-badge {
  display: inline-block; font-size: 10px; padding: 1px 5px; border-radius: 4px;
  background: #ede9fe; color: #5b21b6;
}
.template-item-meta {
  display: flex; align-items: center; justify-content: space-between; margin-top: 3px;
}
.template-item-error {
  border-left: 3px solid #e53e3e;
  background: #fffbfb;
}
.btn-toggle-error {
  font-size: 10px; padding: 2px 6px; border: 1px solid #e5e5e5; border-radius: 4px;
  background: #fff; color: #888; cursor: pointer; transition: all 0.15s;
  white-space: nowrap;
}
.btn-toggle-error:hover { border-color: #ccc; color: #555; }
.btn-toggle-error.is-error {
  background: #fef2f2; border-color: #fecaca; color: #dc2626;
}
.btn-toggle-error.is-error:hover { background: #fee2e2; }

/* ========== Template Match Badge ========== */
.badge-template { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; padding: 3px 10px; border-radius: 10px; margin-bottom: 10px; font-weight: 500; background: #fce7f3; color: #9d174d; }

/* ========== Saved SQL Panel ========== */
.sidebar-saved { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.saved-search { padding: 8px 10px; }
.saved-search-input {
  width: 100%; padding: 7px 10px; border: 1px solid #e5e5e5; border-radius: 8px;
  font-size: 12px; background: #fff; outline: none; font-family: inherit;
  transition: border-color 0.15s;
}
.saved-search-input:focus { border-color: #4f6bf7; }
.saved-list { flex: 1; overflow-y: auto; padding: 4px 10px 8px; }
.saved-empty { text-align: center; padding: 32px 12px; color: #bbb; font-size: 13px; }
.saved-empty small { display: block; margin-top: 4px; font-size: 11px; color: #ccc; }
.saved-item {
  padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 12px;
  color: #555; transition: background 0.1s; line-height: 1.4; margin-bottom: 4px;
  border: 1px solid #f0f0f0;
}
.saved-item:hover { background: #f0f0f0; border-color: #e0e0e0; }
.saved-item-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.saved-item-name { font-weight: 600; color: #1a1a1a; font-size: 13px; }
.btn-delete-saved {
  font-size: 14px; background: none; border: none;
  color: #ccc; cursor: pointer; padding: 0 2px; line-height: 1;
  transition: color 0.15s; flex-shrink: 0;
}
.btn-delete-saved:hover { color: #e53e3e; }
.saved-item-sql {
  font-family: 'SF Mono', 'Fira Code', monospace; font-size: 10px;
  color: #999; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  margin-bottom: 6px;
}
.saved-item-meta { display: flex; align-items: center; justify-content: space-between; }
.saved-item-badge {
  display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 4px;
  background: #e6f7ed; color: #1a7d4c;
}
.saved-item-use { font-size: 10px; color: #bbb; }

/* ========== Save to My SQL Button ========== */
.btn-save-mysql {
  font-size: 11px; padding: 4px 12px; background: #f0f9ff; border: 1px solid #bae6fd;
  border-radius: 5px; cursor: pointer; color: #0369a1; transition: all 0.15s;
}
.btn-save-mysql:hover { background: #e0f2fe; border-color: #7dd3fc; }

/* ========== History Search ========== */
.history-search { padding: 8px 10px; }
.history-search-input {
  width: 100%; padding: 7px 10px; border: 1px solid #e5e5e5; border-radius: 8px;
  font-size: 12px; background: #fff; outline: none; font-family: inherit;
  transition: border-color 0.15s;
}
.history-search-input:focus { border-color: #4f6bf7; }
.history-results { overflow-y: auto; padding: 4px 10px 8px; }
.history-empty { text-align: center; padding: 20px; color: #bbb; font-size: 12px; }
.history-item {
  padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
  color: #555; transition: background 0.1s; margin-bottom: 2px;
}
.history-item:hover { background: #f0f0f0; }
.history-item-text { font-weight: 500; color: #333; margin-bottom: 2px; line-height: 1.4; }
.history-item-meta { display: flex; justify-content: space-between; font-size: 10px; color: #aaa; }

/* ========== Sidebar Overlay ========== */
.sidebar-overlay {
  display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.3); z-index: 99;
}
.sidebar-overlay.show { display: block; }

/* ========== Responsive ========== */
/* Tablet: auto-collapse sidebar, show as overlay */
@media (max-width: 1024px) {
  .chat-sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 100;
    width: 280px; box-shadow: 2px 0 12px rgba(0,0,0,0.12);
  }
  .chat-sidebar.collapsed {
    width: 0; box-shadow: none;
  }
  .chat-sidebar:not(.collapsed) ~ .sidebar-overlay { display: block; }
  .results-panel .chart-container { height: 300px; }
}

/* Mobile: tighter layout */
@media (max-width: 768px) {
  .chat-sidebar { width: 260px; }
  .chat-sidebar.collapsed { width: 0; }
  .message-user .bubble, .message-assistant .bubble { max-width: 100%; padding: 0 8px; }
  .results-panel .chart-container { height: 250px; }
  .results-table-wrapper { max-height: 250px; }
  .stat-value { font-size: 24px; }
  .chat-input-area { padding: 10px 12px 14px; }
  .example-queries { gap: 4px; }
  .example-chip { padding: 6px 10px; font-size: 11px; }
  .welcome-message { margin-top: 40px; }
  .welcome-message h2 { font-size: 18px; }
}
