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

:root {
  --bg: #f5f4f0;
  --surface: #edecea;
  --card: #f9f8f5;
  --border: #dddbd6;
  --text: #1a1a1a;
  --text-muted: #6b6a66;
  --accent: #2a2a2a;
  --user-bubble: #1a1a1a;
  --user-bubble-text: #f5f4f0;
  --assistant-card: #ffffff;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

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

/* ── Auth pages ── */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.auth-box h1 {
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.auth-box .subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.75rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.form-group input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus {
  border-color: #999;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 7px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
  margin-top: 0.5rem;
}

.btn-primary:hover { background: #333; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover { background: var(--surface); }

.btn-danger {
  background: transparent;
  color: #dc2626;
  border: 1px solid #fca5a5;
  font-size: 0.75rem;
  padding: 0.2rem 0.55rem;
}

.btn-danger:hover { background: #fee2e2; border-color: #dc2626; }

.auth-toggle {
  margin-top: 1.25rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.auth-toggle a {
  color: var(--text);
  text-decoration: underline;
  cursor: pointer;
}

.auth-disclaimer {
  margin-top: 1.25rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.5;
}

.error-msg {
  background: #fee2e2;
  color: #991b1b;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  margin-bottom: 1rem;
  display: none;
}

.error-msg.visible { display: block; }

/* ── Chat layout ── */
.chat-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.sidebar-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text-muted);
  padding: 0.3rem 0.5rem;
  border-radius: 5px;
  line-height: 1;
  font-family: inherit;
}

.sidebar-toggle:hover { background: var(--surface); color: var(--text); }

.chat-header .brand {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.03em;
}

.chat-header .header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.header-link {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.3rem 0.6rem;
  border-radius: 5px;
}

.header-link:hover { background: var(--surface); color: var(--text); }

/* ── Chat body (sidebar + main) ── */
.chat-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

.sidebar {
  width: 210px;
  flex-shrink: 0;
  display: none;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--bg);
  overflow: hidden;
}

.sidebar.open { display: flex; }

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.session-item {
  padding: 0.45rem 0.65rem;
  font-size: 0.82rem;
  cursor: pointer;
  border-radius: 5px;
  color: var(--text-muted);
  transition: background 0.1s, color 0.1s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item:hover { background: var(--surface); color: var(--text); }

.session-item.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 0;
}

.messages-inner {
  max-width: 680px;
  margin: 0 auto;
  padding: 0 1.25rem;
}

/* ── Date separator ── */
.date-separator {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.5rem 0 1rem;
}

.date-separator::before,
.date-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.date-separator span {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  letter-spacing: 0.04em;
}

/* ── Message bubbles ── */
.message {
  display: flex;
  margin-bottom: 0.75rem;
}

.message.user {
  justify-content: flex-end;
}

.message.assistant {
  justify-content: flex-start;
}

.bubble {
  max-width: 78%;
  padding: 0.65rem 0.9rem;
  border-radius: 10px;
  font-size: 0.9rem;
  line-height: 1.65;
  word-break: break-word;
}

.message.user .bubble {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 3px;
}

.message.assistant .bubble {
  background: var(--assistant-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
  box-shadow: var(--shadow);
}

.bubble p { margin-bottom: 0.5em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.65rem 0.8rem;
  overflow-x: auto;
  font-size: 0.82rem;
  margin: 0.5em 0;
}
.bubble code {
  font-family: 'SFMono-Regular', Consolas, monospace;
  font-size: 0.85em;
  background: var(--surface);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}
.bubble pre code {
  background: transparent;
  padding: 0;
}
.bubble ul, .bubble ol {
  padding-left: 1.4em;
  margin: 0.4em 0;
}
.bubble li { margin-bottom: 0.15em; }
.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; }
.bubble h1, .bubble h2, .bubble h3 {
  font-size: 1em;
  font-weight: 600;
  margin: 0.6em 0 0.2em;
}

.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: currentColor;
  margin-left: 1px;
  vertical-align: text-bottom;
  animation: blink 0.85s steps(1) infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ── Input bar ── */
.input-bar {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 0.75rem 1.25rem 1rem;
}

.input-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
}

#messageInput {
  flex: 1;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 160px;
  overflow-y: auto;
  line-height: 1.55;
  transition: border-color 0.15s;
}

#messageInput:focus { border-color: #aaa; }
#messageInput::placeholder { color: var(--text-muted); }

#sendBtn {
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
  transition: background 0.15s, opacity 0.15s;
  flex-shrink: 0;
}

#sendBtn:hover { background: #333; }
#sendBtn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ── Admin panel ── */
.admin-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.admin-header .brand {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
}

.admin-header .admin-badge {
  font-size: 0.7rem;
  background: var(--accent);
  color: #fff;
  border-radius: 4px;
  padding: 0.15rem 0.45rem;
  margin-left: 0.5rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.admin-body {
  display: flex;
  flex: 1;
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
  padding: 1.5rem;
  gap: 1.5rem;
}

.user-list-panel {
  width: 320px;
  flex-shrink: 0;
}

.user-list-panel h2 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.user-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.5rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.1s;
}

.user-card:hover { border-color: #bbb; }
.user-card.active { border-color: var(--accent); background: #fff; }

.user-card .user-email {
  font-size: 0.875rem;
  font-weight: 500;
  word-break: break-all;
}

.user-card .user-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.pill {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 500;
  letter-spacing: 0.03em;
}

.pill-green { background: #dcfce7; color: #15803d; }
.pill-gray { background: var(--surface); color: var(--text-muted); }
.pill-blue { background: #dbeafe; color: #1d4ed8; }

.editor-panel {
  flex: 1;
  min-width: 0;
}

.editor-panel h2 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.editor-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
}

.editor-box .editor-title {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.editor-box .editor-subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

#promptTextarea {
  width: 100%;
  min-height: 420px;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  font-family: inherit;
  line-height: 1.65;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

#promptTextarea:focus { border-color: #aaa; }

.editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
}

.word-count {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.usage-section {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.usage-section h3 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.usage-grid {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.usage-stat {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 0.6rem 1rem;
  font-size: 0.825rem;
}

.usage-stat .stat-num {
  font-size: 1.4rem;
  font-weight: 600;
  line-height: 1.2;
}

.usage-stat .stat-label {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.save-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  transition: opacity 0.3s;
}

.save-status.success { color: #15803d; }
.save-status.error { color: #dc2626; }

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

@media (max-width: 700px) {
  .admin-body { flex-direction: column; }
  .user-list-panel { width: 100%; }
}

@media (max-width: 600px) {
  .sidebar {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    z-index: 50;
    box-shadow: 2px 0 12px rgba(0,0,0,0.1);
  }
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1.5rem;
}

.modal-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.12);
}

.modal-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1.25rem;
}

.modal-footer {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.success-msg {
  background: #dcfce7;
  color: #15803d;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  margin-bottom: 1rem;
  display: none;
}

.success-msg.visible { display: block; }
