/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --brand: #1a56db;
  --brand-light: #e1effe;
  --brand-dark: #1e40af;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --success: #059669;
  --danger: #dc2626;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ===== Top bar ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  height: 56px;
  background: var(--brand);
  color: #fff;
}
.topbar-brand {
  font-weight: 700;
  font-size: 1.15rem;
  color: #fff;
  text-decoration: none;
}
.topbar-tagline {
  font-size: 0.85rem;
  opacity: 0.8;
}
.topbar-links { margin-left: auto; display: flex; gap: 8px; }
.topbar-links a {
  color: rgba(255,255,255,.8);
  text-decoration: none;
  padding: 6px 14px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  transition: background .15s;
}
.topbar-links a:hover, .topbar-links a.active {
  background: rgba(255,255,255,.15);
  color: #fff;
}

/* ===== Container ===== */
.container { max-width: 960px; margin: 0 auto; padding: 24px 16px; }

/* ===== Chat ===== */
.chat-wrapper {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 56px - 48px);
}
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.chat-header h1 { font-size: 1.25rem; }
.exchange-counter {
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--surface);
  padding: 4px 12px;
  border-radius: 20px;
  border: 1px solid var(--border);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.95rem;
  line-height: 1.6;
  white-space: pre-wrap;
}
.msg.user {
  align-self: flex-end;
  background: var(--brand);
  color: #fff;
  border-bottom-right-radius: 2px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 2px;
}
.msg .sources {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
}
.msg.user .sources { border-top-color: rgba(255,255,255,.25); color: rgba(255,255,255,.7); }

.msg.assistant ul, .msg.assistant ol { padding-left: 20px; margin: 4px 0; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant strong { font-weight: 600; }

.typing-indicator {
  align-self: flex-start;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Input area */
.chat-input-area {
  display: flex;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.chat-input-area textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  resize: none;
  font-family: inherit;
  line-height: 1.4;
  min-height: 44px;
  max-height: 120px;
}
.chat-input-area textarea:focus { outline: none; border-color: var(--brand); }
.chat-input-area button { white-space: nowrap; }

.limit-banner {
  text-align: center;
  padding: 12px;
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: var(--radius);
  font-size: 0.9rem;
  color: #92400e;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 18px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #d1d5db; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }
.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-outline:hover:not(:disabled) { background: var(--bg); }

/* ===== Admin ===== */
.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.admin-header h1 { font-size: 1.25rem; }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--border);
}
.tab-btn {
  padding: 8px 20px;
  border: none;
  background: none;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .15s;
}
.tab-btn.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* Cards & tables */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
th { font-weight: 600; font-size: 0.85rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; }
td { font-size: 0.9rem; }
tr:last-child td { border-bottom: none; }
.empty-row td { text-align: center; color: var(--text-muted); padding: 32px 12px; }

.tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--brand-light);
  color: var(--brand-dark);
  font-size: 0.75rem;
  border-radius: 4px;
  margin: 1px 2px;
}

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text);
}
.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  outline: none;
  border-color: var(--brand);
}
.form-group textarea { resize: vertical; min-height: 120px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Modal overlay */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal h2 { font-size: 1.1rem; margin-bottom: 16px; }

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .container { padding: 16px 8px; }
  .msg { max-width: 95%; }
  .topbar-tagline { display: none; }
}
