:root {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --bg-hover: #212530;
  --border: #2a2f3d;
  --text: #eceef4;
  --text-muted: #9aa3b2;
  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --user-bubble: #232834;
  --danger: #ef4444;
  --sidebar-w: 272px;
  --radius: 14px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg-elev: #f7f7f8;
  --bg-hover: #eceef2;
  --border: #e2e5ec;
  --text: #111827;
  --text-muted: #6b7280;
  --user-bubble: #f1f3f6;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.hidden { display: none !important; }

/* App Layout */
.app-shell {
  display: flex;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: margin-left 0.22s ease, transform 0.22s ease;
  z-index: 40;
}

.sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 8px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
  font-weight: 650;
  font-size: 15px;
}

.brand-logo { width: 26px; height: 26px; }

.sidebar-actions { padding: 8px 12px; display: flex; flex-direction: column; gap: 8px; }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  min-height: 42px;
}
.new-chat-btn:hover { background: var(--bg-hover); }

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.search-box input {
  width: 100%;
  background: transparent;
  border: 0;
  color: var(--text);
  padding: 9px 0;
  font-size: 13px;
  outline: none;
}

.chat-history-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text);
  min-height: 40px;
}
.chat-item:hover, .chat-item.active { background: var(--bg-hover); }
.chat-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.chat-item-actions { display: flex; gap: 4px; opacity: 0.75; }
.chat-item-actions button {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}
.chat-item-actions button:hover { color: var(--text); background: var(--bg); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.guest-badge {
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(16, 163, 127, 0.12);
  color: var(--accent);
  text-align: center;
}

.footer-controls { display: flex; gap: 6px; }
.footer-btn {
  flex: 1;
  text-align: center;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 12.5px;
  text-decoration: none;
  cursor: pointer;
}
.footer-btn:hover { color: var(--text); background: var(--bg-hover); }

.user-card { display: flex; align-items: center; gap: 10px; padding-top: 4px; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 600; font-size: 14px; flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-plan { font-size: 11.5px; color: var(--text-muted); text-transform: capitalize; }
.auth-pill {
  padding: 6px 10px; border-radius: 8px; background: var(--accent); color: #fff;
  text-decoration: none; font-size: 12px; font-weight: 600; border: 0; cursor: pointer;
}

/* Main Area */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100dvh;
}

.topbar {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.model-pill { font-size: 13px; font-weight: 600; color: var(--text-muted); }

.icon-btn {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  width: 38px;
  height: 38px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.announcement-banner {
  background: rgba(16, 163, 127, 0.14);
  color: var(--text);
  padding: 8px 16px;
  font-size: 13px;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding: 20px 16px;
}

.welcome-screen {
  max-width: 680px;
  margin: 8vh auto 0;
  text-align: center;
}
.welcome-icon { color: var(--text); margin-bottom: 12px; }
.welcome-screen h1 { font-size: clamp(22px, 4vw, 30px); margin: 0 0 10px; font-weight: 650; }
.welcome-sub { color: var(--text-muted); font-size: 14.5px; margin: 0 0 26px; }

.prompt-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
}
.prompt-chip {
  text-align: left;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
  line-height: 1.4;
}
.prompt-chip:hover { background: var(--bg-hover); }

.messages-container {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.msg-row { display: flex; flex-direction: column; gap: 6px; }
.msg-row.user { align-items: flex-end; }
.msg-row.assistant { align-items: flex-start; }

.msg-bubble {
  max-width: min(88%, 680px);
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.6;
  word-wrap: break-word;
}
.msg-row.user .msg-bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg-row.assistant .msg-bubble { background: transparent; padding-left: 0; max-width: 100%; }

.msg-attachment {
  font-size: 12px;
  color: var(--accent);
  margin-bottom: 6px;
  display: inline-block;
}

.msg-actions { display: flex; gap: 6px; opacity: 0.75; }
.msg-actions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 11.5px;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.msg-actions button:hover { color: var(--text); background: var(--bg-hover); }

/* Markdown & Code Blocks */
.msg-bubble pre {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  margin: 10px 0;
}
.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #161b24;
  font-size: 12px;
  color: #9aa3b2;
  border-bottom: 1px solid var(--border);
}
.copy-code-btn {
  background: transparent; border: 0; color: #eceef4; cursor: pointer; font-size: 12px;
}
.msg-bubble pre code { display: block; padding: 12px; font-family: ui-monospace, monospace; font-size: 13px; color: #e5e7eb; }
.msg-bubble code { font-family: ui-monospace, monospace; background: var(--bg-elev); padding: 2px 5px; border-radius: 5px; font-size: 0.9em; }

/* Composer */
.composer-wrap {
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
  max-width: 812px;
  width: 100%;
  margin: 0 auto;
  flex-shrink: 0;
}

.attachment-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  margin-bottom: 8px;
  border-radius: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  font-size: 13px;
}

.upload-progress-wrap { height: 4px; background: var(--border); border-radius: 99px; overflow: hidden; margin-bottom: 6px; }
.upload-progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width 0.15s ease; }

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 8px 10px;
}
.composer-box textarea {
  flex: 1;
  background: transparent;
  border: 0;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.45;
  resize: none;
  max-height: 180px;
  padding: 7px 4px;
  outline: none;
}

.send-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 0;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn.stop-btn { background: var(--danger); }
.composer-hint { text-align: center; font-size: 11.5px; color: var(--text-muted); margin: 6px 0 0; }

/* Auth & Admin */
.auth-body { display: grid; place-items: center; min-height: 100dvh; padding: 20px; }
.auth-container { width: 100%; max-width: 420px; }
.auth-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 24px;
}
.auth-brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 18px; margin-bottom: 18px; }
.auth-sub { color: var(--text-muted); font-size: 14px; margin: 6px 0 18px; }
.oauth-stack { display: flex; flex-direction: column; gap: 10px; }
.oauth-btn {
  width: 100%; padding: 11px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-weight: 500; cursor: pointer; min-height: 44px;
}
.oauth-btn:hover { background: var(--bg-hover); }
.divider { display: flex; align-items: center; gap: 10px; margin: 16px 0; color: var(--text-muted); font-size: 12px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 13px; color: var(--text-muted); }
.field input, .field select, .field textarea {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 14px;
}
.btn-primary {
  padding: 11px 16px; border-radius: 10px; border: 0; background: var(--accent);
  color: #fff; font-weight: 600; cursor: pointer; text-decoration: none; text-align: center; min-height: 42px;
}
.btn-secondary {
  padding: 11px 16px; border-radius: 10px; border: 1px solid var(--border); background: transparent;
  color: var(--text); font-weight: 500; cursor: pointer; min-height: 42px;
}
.full-width { width: 100%; }
.auth-links { display: flex; justify-content: space-between; margin-top: 14px; }
.link-btn { background: transparent; border: 0; color: var(--accent); font-size: 13px; cursor: pointer; text-decoration: none; padding: 0; }
.auth-footer-nav { margin-top: 20px; text-align: center; }
.auth-alert { padding: 10px 12px; border-radius: 10px; font-size: 13px; margin-bottom: 14px; }
.auth-alert.error { background: rgba(239, 68, 68, 0.15); color: #fca5a5; }
.auth-alert.success { background: rgba(16, 163, 127, 0.15); color: #6ee7b7; }
.verify-actions { display: flex; flex-direction: column; gap: 10px; }

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.65);
  display: grid; place-items: center; padding: 16px; z-index: 60;
}
.modal-card {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 16px;
  padding: 24px; max-width: 400px; width: 100%;
}
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }

/* Admin Panel */
.admin-shell { max-width: 1160px; margin: 0 auto; padding: 20px 16px; }
.admin-header { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 20px; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.stat-label { font-size: 12.5px; color: var(--text-muted); }
.stat-val { font-size: 24px; font-weight: 700; margin-top: 4px; }
.admin-tabs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.tab-btn { padding: 8px 14px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); cursor: pointer; }
.tab-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.admin-panel-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px; padding: 16px; overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.data-table th, .data-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); }

.mobile-only { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 768px) {
  .mobile-only { display: grid; }
  .sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    transform: translateX(-100%);
    margin-left: 0 !important;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 35;
  }
}
