:root {
  --bg: #111111;
  --bg-elev: #1a1a1a;
  --bg-elev-2: #222222;
  --border: #303030;
  --text: #e7e9ee;
  --text-dim: #9aa3b2;
  --text-faint: #6b7280;
  --accent: #d41a1a;
  --accent-soft: rgba(212, 26, 26, 0.14);
  --accent-2: #ff4040;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --radius: 12px;
  --radius-sm: 8px;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
}

.hidden {
  display: none !important;
}

button {
  font-family: inherit;
  cursor: pointer;
}

input,
textarea,
select {
  font-family: inherit;
  font-size: 14px;
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-weight: 600;
  transition: filter 0.15s ease, transform 0.05s ease;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 13px;
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--text-faint);
}

/* Brand logo */
.brand-logo {
  height: 36px;
  width: auto;
  flex-shrink: 0;
  display: block;
}

.brand-logo.small {
  height: 28px;
}

.sidebar-brand-name {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
  color: var(--text);
}

.brand-logo.empty-state-logo {
  height: 56px;
  margin: 0 auto 8px;
  display: block;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand h1 {
  font-size: 18px;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand.small h1 {
  font-size: 16px;
}

.auth-card .brand-sub {
  margin: 6px 0 12px;
  font-size: 13px;
  color: var(--text-dim);
}

/* AUTH */
.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(
      1200px 600px at 50% -10%,
      rgba(212, 26, 26, 0.12),
      transparent 60%
    ),
    var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.auth-card .brand {
  margin-bottom: 18px;
}

.auth-intro {
  font-size: 13px;
  color: var(--text-dim);
  margin: 0 0 18px;
  min-height: 18px;
}

#auth-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#auth-form label {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 8px;
}

#auth-form button {
  margin-top: 18px;
}

.form-error {
  color: var(--err);
  font-size: 13px;
  margin: 10px 0 0;
  min-height: 16px;
}

/* APP SHELL */
.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100%;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.btn-new {
  margin-top: 14px;
  width: 100%;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid rgba(212, 26, 26, 0.35);
  border-radius: var(--radius-sm);
  padding: 9px;
  font-weight: 600;
}

.btn-new:hover {
  background: rgba(212, 26, 26, 0.2);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  min-height: 0;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13.5px;
}

.conv-item:hover {
  background: var(--bg-elev-2);
  color: var(--text);
}

.conv-item.active {
  background: var(--accent-soft);
  color: var(--text);
}

.conv-item .conv-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-del {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 15px;
  padding: 0 4px;
}

.conv-item:hover .conv-del {
  opacity: 1;
}

.conv-del:hover {
  color: var(--err);
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 12px;
}

.nav-link {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
}

.nav-link:hover {
  background: var(--bg-elev-2);
  color: var(--text);
}

.nav-link.active {
  color: var(--accent);
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.user-email {
  font-size: 12.5px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* MAIN / CHAT */
.main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 0 40px;
  min-height: 0;
}

.empty-state {
  max-width: 520px;
  margin: 8vh auto 0;
  text-align: center;
  color: var(--text-dim);
  padding: 0 24px;
}

.empty-state h2 {
  color: var(--text);
  margin: 18px 0 8px;
  font-size: 22px;
}

.msg {
  max-width: 760px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  gap: 14px;
}

.msg .avatar {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
}

.msg.user .avatar {
  background: var(--bg-elev-2);
  color: var(--text-dim);
}

.msg.assistant .avatar {
  background: linear-gradient(160deg, var(--accent-2), var(--accent) 70%);
  color: #fff;
}

.msg .body {
  flex: 1;
  min-width: 0;
}

.msg .role {
  font-size: 12px;
  color: var(--text-faint);
  margin-bottom: 4px;
}

.msg .content {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg .content p {
  margin: 0 0 0.6em;
}
.msg .content p:last-child {
  margin-bottom: 0;
}
.msg .content ul,
.msg .content ol {
  margin: 0.4em 0 0.6em 1.4em;
  padding: 0;
}
.msg .content li {
  margin-bottom: 0.2em;
}
.msg .content strong {
  font-weight: 650;
  color: var(--text);
}
.msg .content em {
  font-style: italic;
  color: var(--text-dim);
}
.msg .content code {
  font-family: "JetBrains Mono", "Fira Code", monospace;
  font-size: 0.88em;
  background: var(--bg-elev-2);
  border-radius: 4px;
  padding: 1px 5px;
}

.msg .content.notfound {
  border-left: 3px solid var(--warn);
  padding-left: 12px;
  color: var(--text-dim);
}

.msg .content.error {
  border-left: 3px solid var(--err);
  padding-left: 12px;
  color: var(--err);
}

.cursor-blink::after {
  content: "▋";
  color: var(--accent);
  animation: blink 1s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.sources {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 12px;
}

.chip {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 11px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.12s ease;
}

.chip:hover {
  border-color: var(--accent);
  color: var(--text);
}

.chip .dot {
  color: var(--accent);
  margin: 0 5px;
}

/* COMPOSER */
.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 14px 0 18px;
}

.composer-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px;
}

.scope-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--text-dim);
}

.scope-row select {
  padding: 5px 8px;
}

.chat-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

#chat-input {
  flex: 1;
  resize: none;
  max-height: 200px;
  line-height: 1.5;
}

/* ADMIN */
.admin-scroll {
  overflow-y: auto;
  padding: 28px;
  max-width: 880px;
  margin: 0 auto;
  width: 100%;
}

.admin-section {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 22px;
}

.admin-section h2 {
  margin: 0 0 16px;
  font-size: 17px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-head h2 {
  margin: 0;
}

.upload-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.upload-form .field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.upload-form .field label {
  font-size: 12px;
  color: var(--text-dim);
}

.upload-form button {
  grid-column: 1 / -1;
  justify-self: start;
}

.form-note {
  grid-column: 1 / -1;
  font-size: 13px;
  color: var(--text-dim);
  margin: 4px 0 0;
}

.docs-table,
.users-table {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.doc-row,
.user-row-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.doc-meta {
  min-width: 0;
}

.doc-meta .doc-title {
  font-weight: 600;
  font-size: 14px;
}

.doc-meta .doc-sub {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.badge {
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  text-transform: capitalize;
}

.badge.pending {
  background: rgba(156, 163, 178, 0.15);
  color: var(--text-dim);
}

.badge.processing,
.badge.pending {
  background: rgba(251, 191, 36, 0.15);
  color: var(--warn);
  animation: badge-pulse 1.6s ease-in-out infinite;
}

@keyframes badge-pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.45; }
}

.badge.ready {
  background: rgba(52, 211, 153, 0.15);
  color: var(--ok);
}

.badge.error {
  background: rgba(248, 113, 113, 0.15);
  color: var(--err);
}

.doc-actions {
  display: flex;
  gap: 6px;
}

.user-form {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.user-form input {
  flex: 1;
  min-width: 160px;
}

.user-row-item {
  grid-template-columns: 1fr auto;
}

.role-tag {
  font-size: 12px;
  color: var(--text-dim);
}

.role-tag.admin {
  color: var(--accent-2);
  font-weight: 600;
}

.doc-error {
  grid-column: 1 / -1;
  font-size: 12px;
  color: var(--err);
  margin-top: 2px;
}

/* SOURCE PANEL */
.source-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 420px;
  max-width: 90vw;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.4);
}

.source-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.source-panel-head h3 {
  margin: 0;
  font-size: 15px;
}

.source-body {
  padding: 20px;
  overflow-y: auto;
}

.source-body .src-meta {
  font-size: 13px;
  color: var(--accent-2);
  margin-bottom: 12px;
}

.source-body .src-text {
  white-space: pre-wrap;
  font-size: 13.5px;
  color: var(--text-dim);
  line-height: 1.6;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 40;
}

/* Scrollbars */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 6px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-faint);
}

/* Mobile top bar */
.mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  align-items: center;
  padding: 0 12px;
  z-index: 50;
  gap: 12px;
}

.btn-hamburger {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  padding: 6px 8px;
  line-height: 1;
  flex-shrink: 0;
}

.topbar-logo {
  height: 24px;
  width: auto;
  flex: 1;
  object-fit: contain;
  object-position: left;
}

.btn-topbar-new {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid rgba(212, 26, 26, 0.35);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Sidebar drawer overlay (mobile only) */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 100;
}

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
  }

  .mobile-topbar {
    display: flex;
  }

  .main {
    padding-top: 56px;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 280px;
    z-index: 200;
    transform: translateX(-280px);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay {
    display: block;
  }

  .brand-logo {
    max-width: 100%;
    height: auto;
  }

  .upload-form {
    grid-template-columns: 1fr;
  }
}
