@import url("https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=DM+Sans:wght@300;400;500&display=swap");

/* ─── Tokens ──────────────────────────────────────────────────── */
:root {
  --font-serif: "Instrument Serif", serif;
  --font-sans: "DM Sans", sans-serif;
  --transition: 0.2s ease;
  --sidebar-w: 260px;
}

[data-theme="dark"] {
  --bg: #0f0f0f;
  --sidebar-bg: #0a0a0a;
  --surface: #161616;
  --surface-2: #1e1e1e;
  --surface-3: #252525;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.12);
  --text: #e8e8e6;
  --text-muted: rgba(232, 232, 230, 0.38);
  --accent: #c9a96e;
  --accent-dim: rgba(201, 169, 110, 0.12);
  --accent-hover: rgba(201, 169, 110, 0.2);
  --error: #ff7070;
  --error-bg: rgba(255, 90, 90, 0.08);
  --error-border: rgba(255, 90, 90, 0.25);
  --success: #6ec98a;
  --success-bg: rgba(110, 201, 138, 0.08);
  --success-border: rgba(110, 201, 138, 0.25);
  --scroll-thumb: rgba(255, 255, 255, 0.09);
}

[data-theme="light"] {
  --bg: #faf9f7;
  --sidebar-bg: #f0ede8;
  --surface: #ffffff;
  --surface-2: #f4f3f0;
  --surface-3: #ece9e4;
  --border: rgba(0, 0, 0, 0.07);
  --border-strong: rgba(0, 0, 0, 0.12);
  --text: #1a1a18;
  --text-muted: rgba(26, 26, 24, 0.4);
  --accent: #9b7a3e;
  --accent-dim: rgba(155, 122, 62, 0.1);
  --accent-hover: rgba(155, 122, 62, 0.18);
  --error: #c03030;
  --error-bg: rgba(200, 50, 50, 0.06);
  --error-border: rgba(200, 50, 50, 0.2);
  --success: #2d8a4e;
  --success-bg: rgba(45, 138, 78, 0.07);
  --success-border: rgba(45, 138, 78, 0.2);
  --scroll-thumb: rgba(0, 0, 0, 0.12);
}

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

html {
  font-family: var(--font-sans);
  font-size: 15px;
  font-weight: 400;
  background: var(--bg);
  color: var(--text);
  transition:
    background var(--transition),
    color var(--transition);
}

a {
  color: inherit;
  text-decoration: none;
}
button {
  font-family: var(--font-sans);
}

/* ─── Scrollbars ──────────────────────────────────────────────── */
::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border-radius: 99px;
}

/* ─── Markdown styling ────────────────────────────────────────── */
.markdown-body {
  font-family: inherit;
  line-height: 1.6;
  font-size: inherit;
  color: inherit;
}
.markdown-body p {
  margin-bottom: 12px;
}
.markdown-body p:last-child {
  margin-bottom: 0;
}
.markdown-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.markdown-body a:hover {
  color: var(--text);
}
.markdown-body strong {
  font-weight: 500;
  color: var(--text);
}
.markdown-body em {
  font-style: italic;
}
.markdown-body ul,
.markdown-body ol {
  margin-bottom: 12px;
  padding-left: 24px;
}
.markdown-body ul:last-child,
.markdown-body ol:last-child {
  margin-bottom: 0;
}
.markdown-body li {
  margin-bottom: 4px;
}
.markdown-body code {
  font-family: monospace;
  background: var(--surface-3);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 13px;
  color: var(--accent);
}
.markdown-body pre {
  background: var(--surface-3);
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin-bottom: 12px;
}
.markdown-body pre:last-child {
  margin-bottom: 0;
}
.markdown-body pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  color: inherit;
  font-size: 12.5px;
}
.markdown-body blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-muted);
  margin-bottom: 12px;
  font-style: italic;
}
.markdown-body blockquote:last-child {
  margin-bottom: 0;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin-top: 16px;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--text);
}
.markdown-body h1:first-child,
.markdown-body h2:first-child,
.markdown-body h3:first-child {
  margin-top: 0;
}

/* ─── App Layout ──────────────────────────────────────────────── */
.app-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  transition: background var(--transition);
}

/* ─── Sidebar ─────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition:
    background var(--transition),
    transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 100;
}

/* Mobile sidebar overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.sidebar-overlay.active {
  display: block;
  opacity: 1;
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
}

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

.logo {
  font-family: var(--font-serif);
  font-size: 20px;
  font-style: italic;
  color: var(--accent);
  letter-spacing: -0.02em;
  user-select: none;
}

.new-conv-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 11px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--text-muted);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}
.new-conv-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--accent-dim);
}
.new-conv-btn svg {
  flex-shrink: 0;
}

/* ─── Conversations list ──────────────────────────────────────── */
.conv-section {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 8px;
}

.conv-group-label {
  padding: 10px 8px 4px;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  user-select: none;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.14s ease;
}
.conv-item:hover {
  background: var(--surface-2);
}
.conv-item.active {
  background: var(--accent-dim);
}
.conv-item.active .conv-title {
  color: var(--accent);
}

.conv-title {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
  transition: color 0.14s;
}

.conv-actions {
  display: none;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.conv-item:hover .conv-actions {
  display: flex;
}
.conv-item.active .conv-actions {
  display: flex;
}

.conv-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.14s ease;
}
.conv-action-btn:hover {
  background: var(--surface-3);
  color: var(--text);
}
.conv-action-btn.delete:hover {
  background: var(--error-bg);
  color: var(--error);
}

/* ─── Sidebar nav (bottom) ────────────────────────────────────── */
.sidebar-nav {
  border-top: 1px solid var(--border);
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}

.sidebar-nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 9px;
  font-size: 13.5px;
  font-weight: 400;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.14s ease;
  text-decoration: none;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
}
.sidebar-nav-link:hover {
  color: var(--text);
  background: var(--surface-2);
}
.sidebar-nav-link.active {
  color: var(--accent);
  background: var(--accent-dim);
}
.sidebar-nav-link svg {
  flex-shrink: 0;
  opacity: 0.7;
}
.sidebar-nav-link:hover svg,
.sidebar-nav-link.active svg {
  opacity: 1;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px 4px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-dim);
  border: 1px solid rgba(201, 169, 110, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 12px;
  color: var(--accent);
  flex-shrink: 0;
}

.user-info {
  min-width: 0;
  flex: 1;
}
.user-name {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-role {
  font-size: 11px;
  color: var(--text-muted);
}

/* ─── Main content wrapper ────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  transition: background var(--transition);
}

/* Mobile header (hamburger) */
.mobile-header {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.mobile-header-title {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 20px;
  color: var(--accent);
}
.hamburger-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
}
@media (max-width: 768px) {
  .mobile-header {
    display: flex;
  }
}

/* ─── Page header (indexation, admins, settings) ──────────────── */
.page-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 52px 24px 80px;
}

.page-inner {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 36px;
}

.page-heading h1 {
  font-family: var(--font-serif);
  font-size: 30px;
  font-style: italic;
  font-weight: 400;
  letter-spacing: -0.03em;
  line-height: 1.2;
}
.page-heading p {
  margin-top: 8px;
  font-size: 14px;
  color: var(--text-muted);
}

/* ─── Card ────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: hidden;
  transition:
    background var(--transition),
    border-color var(--transition);
}
.card-body {
  padding: 26px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.card-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 2px;
}

/* ─── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 10px 18px;
  border-radius: 10px;
  border: none;
  font-family: var(--font-sans);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn-primary {
  background: var(--accent);
  color: #0f0f0f;
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-1px);
}
.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-muted);
}
.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--accent);
  background: var(--accent-dim);
}

.btn-danger {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error);
}
.btn-danger:hover:not(:disabled) {
  filter: brightness(1.1);
}

.btn-sm {
  padding: 7px 13px;
  font-size: 12.5px;
  border-radius: 8px;
}

/* ─── Form inputs ─────────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.form-label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-muted);
}
.form-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  outline: none;
  transition:
    border-color 0.15s,
    box-shadow 0.15s,
    background var(--transition);
}
.form-input:focus {
  border-color: rgba(201, 169, 110, 0.5);
  box-shadow: 0 0 0 3px rgba(201, 169, 110, 0.07);
}
.form-input::placeholder {
  color: var(--text-muted);
}
.form-input[readonly] {
  opacity: 0.6;
  cursor: not-allowed;
}

.form-hint {
  font-size: 12px;
  color: var(--text-muted);
}
.form-error {
  font-size: 12.5px;
  color: var(--error);
}

/* ─── Select ──────────────────────────────────────────────────── */
.form-select {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  outline: none;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  transition:
    border-color 0.15s,
    background var(--transition);
}
.form-select:focus {
  border-color: rgba(201, 169, 110, 0.5);
}

/* ─── Spinner ─────────────────────────────────────────────────── */
.spinner {
  width: 15px;
  height: 15px;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-top-color: #0f0f0f;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}
.spinner-light {
  border-color: rgba(232, 232, 230, 0.2);
  border-top-color: var(--text-muted);
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ─── Toast ───────────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 9999;
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 13.5px;
  font-weight: 500;
  pointer-events: auto;
  animation: toastIn 0.22s ease-out;
  max-width: 300px;
  border: 1px solid;
}
@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(14px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
.toast-success {
  background: var(--success-bg);
  border-color: var(--success-border);
  color: var(--success);
}
.toast-error {
  background: var(--error-bg);
  border-color: var(--error-border);
  color: var(--error);
}
.toast svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ─── Modal backdrop ──────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
  animation: fadeIn 0.18s ease;
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 20px;
  width: 100%;
  max-width: 440px;
  animation: modalIn 0.2s ease-out;
  overflow: hidden;
}
@keyframes modalIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(8px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 22px 24px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-title {
  font-size: 16px;
  font-weight: 500;
}
.modal-close {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  border: none;
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.14s ease;
}
.modal-close:hover {
  background: var(--error-bg);
  color: var(--error);
}
.modal-body {
  padding: 22px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0 24px 20px;
}

/* ─── Badge / role pill ───────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 500;
}
.badge-full {
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid rgba(201, 169, 110, 0.25);
}
.badge-chat {
  background: var(--surface-3);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* ─── Table ───────────────────────────────────────────────────── */
.data-table {
  width: 100%;
  border-collapse: collapse;
}
.data-table th {
  text-align: left;
  padding: 10px 16px;
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.data-table td {
  padding: 13px 16px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  vertical-align: middle;
}
.data-table tr:last-child td {
  border-bottom: none;
}
.data-table tr:hover td {
  background: var(--surface-2);
  transition: background 0.12s;
}

/* ─── Alert bar ───────────────────────────────────────────────── */
.alert {
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  border: 1px solid;
  display: flex;
  align-items: center;
  gap: 10px;
}
.alert-error {
  background: var(--error-bg);
  border-color: var(--error-border);
  color: var(--error);
}
.alert-success {
  background: var(--success-bg);
  border-color: var(--success-border);
  color: var(--success);
}
