/* ── Tokens ── */
:root {
  --navy-deep:    #0D1B2A;
  --navy-mid:     #1A3A5C;
  --navy-light:   #254E7A;
  --parchment:    #F4EFE4;
  --parchment-lt: #FAF8F5;
  --gold:         #C9973A;
  --gold-light:   #F0C860;
  --gold-dim:     #7A5C1E;
  --text:         #1A1A2E;
  --text-mid:     #3D4F62;
  --text-muted:   #6B7A8D;
  --border:       #D6CEC2;
  --border-light: #EAE5DC;
  --red:          #C0392B;
  --green:        #1E7A4A;
  --white:        #FFFFFF;
  --shadow-sm:    0 1px 3px rgba(0,0,0,.08);
  --shadow-md:    0 4px 16px rgba(0,0,0,.12);
  --radius:       6px;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--parchment-lt);
  line-height: 1.5;
}
button { cursor: pointer; border: none; background: none; font: inherit; }
input, select, textarea { font: inherit; }
a { color: inherit; }
.hidden { display: none !important; }

/* ── Auth ── */
#auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--navy-deep);
  background-image:
    radial-gradient(ellipse 60% 50% at 50% 0%, rgba(201,151,58,.18) 0%, transparent 70%);
}

.auth-panel {
  background: var(--white);
  border-radius: 12px;
  padding: 48px 44px;
  width: 400px;
  box-shadow: 0 24px 80px rgba(0,0,0,.35);
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.auth-logo-mark {
  font-size: 22px;
  color: var(--gold);
  line-height: 1;
}
.auth-logo-text {
  font-family: 'IBM Plex Serif', serif;
  font-size: 20px;
  font-weight: 600;
  color: var(--navy-deep);
}
.auth-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 28px;
}

.auth-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border-light);
  margin-bottom: 24px;
}
.auth-tab {
  flex: 1;
  padding: 8px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .15s, border-color .15s;
}
.auth-tab.active {
  color: var(--navy-deep);
  border-bottom-color: var(--gold);
}

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-mid);
  margin-bottom: 6px;
  letter-spacing: .02em;
  text-transform: uppercase;
}
.field input {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--parchment-lt);
  color: var(--text);
  transition: border-color .15s;
}
.field input:focus {
  outline: none;
  border-color: var(--navy-mid);
}

.auth-error {
  font-size: 13px;
  color: var(--red);
  background: #FEF2F2;
  border: 1px solid #FECACA;
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 14px;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  transition: background .15s, opacity .15s;
}
.btn-primary {
  background: var(--navy-mid);
  color: var(--white);
}
.btn-primary:hover { background: var(--navy-light); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost {
  color: var(--text-muted);
  padding: 6px 10px;
  border-radius: var(--radius);
}
.btn-ghost:hover { background: var(--border-light); color: var(--text); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; }
.btn-danger { background: #FEF2F2; color: var(--red); }
.btn-danger:hover { background: #FECACA; }

/* ── App Shell ── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 0 24px;
  height: 52px;
  background: var(--navy-deep);
  border-bottom: 1px solid rgba(255,255,255,.08);
  flex-shrink: 0;
  z-index: 10;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: 32px;
}
.brand-mark { font-size: 18px; color: var(--gold); }
.brand-name {
  font-family: 'IBM Plex Serif', serif;
  font-size: 15px;
  font-weight: 600;
  color: var(--white);
  white-space: nowrap;
}

.topbar-nav {
  display: flex;
  gap: 2px;
  flex: 1;
}
.nav-btn {
  padding: 6px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  color: rgba(255,255,255,.55);
  transition: color .15s, background .15s;
}
.nav-btn:hover { color: rgba(255,255,255,.85); background: rgba(255,255,255,.06); }
.nav-btn.active { color: var(--white); background: rgba(255,255,255,.1); }

.topbar-user {
  display: flex;
  align-items: center;
  gap: 12px;
  color: rgba(255,255,255,.5);
  font-size: 12px;
}
.topbar-user .btn-ghost { color: rgba(255,255,255,.4); font-size: 12px; }
.topbar-user .btn-ghost:hover { background: rgba(255,255,255,.08); color: rgba(255,255,255,.7); }

/* ── Views ── */
.view {
  flex: 1;
  overflow: auto;
  padding: 28px 32px;
}

.view-header {
  margin-bottom: 24px;
}
.view-title {
  font-family: 'IBM Plex Serif', serif;
  font-size: 24px;
  font-weight: 600;
  color: var(--navy-deep);
  margin-bottom: 4px;
}
.view-subtitle {
  color: var(--text-muted);
  font-size: 13px;
}
.section-title {
  font-family: 'IBM Plex Serif', serif;
  font-size: 16px;
  font-weight: 600;
  color: var(--navy-deep);
  margin: 28px 0 14px;
}

/* ── Filter bar ── */
.filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 16px;
}

.search-input {
  flex: 1;
  min-width: 220px;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--text);
}
.search-input:focus { outline: none; border-color: var(--navy-mid); }

.select-sm {
  padding: 7px 10px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--text);
  font-size: 13px;
}
.select-sm:focus { outline: none; border-color: var(--navy-mid); }

/* ── Doc stats ── */
.doc-stats {
  display: flex;
  gap: 20px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.stat-chip {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--white);
  border: 1px solid var(--border-light);
  border-radius: 20px;
  padding: 4px 12px;
}
.stat-chip strong { color: var(--navy-mid); }

/* ── Table ── */
.table-wrap {
  background: var(--white);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.doc-table {
  width: 100%;
  border-collapse: collapse;
}
.doc-table th {
  background: var(--parchment);
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: .05em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.doc-table td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border-light);
  font-size: 13px;
  color: var(--text);
  vertical-align: middle;
}
.doc-table tr:last-child td { border-bottom: none; }
.doc-table tr:hover td { background: var(--parchment-lt); }
#doc-thead-sort th[data-col]:hover { color: var(--navy); }
#doc-thead-sort th.sort-active { color: var(--navy-mid); }
.sort-icon { font-style: normal; }
#doc-thead-filter th {
  padding: 4px 6px;
  background: var(--parchment-lt);
}
.col-filter {
  width: 100%; padding: 4px 6px; font-size: 11px;
  border: 1px solid var(--border); border-radius: 4px;
  background: var(--white); color: var(--text);
  outline: none;
}
.col-filter:focus { border-color: var(--navy-mid); }
.col-filter-select { cursor: pointer; }

.doc-name-cell {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}
.badge-type {
  background: #EEF2FF;
  color: #3730A3;
}
.badge-tax {
  background: #F0FDF4;
  color: #166534;
}
.badge-embedded {
  background: #F0FDF4;
  color: var(--green);
}
.badge-pending {
  background: #FFF7ED;
  color: #9A3412;
}

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

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 0;
  font-size: 13px;
  color: var(--text-muted);
}
.page-btn {
  padding: 5px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--white);
  font-size: 13px;
  color: var(--text);
}
.page-btn:hover:not(:disabled) { border-color: var(--navy-mid); color: var(--navy-mid); }
.page-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── Chat layout ── */
#view-chat {
  padding: 0;
  overflow: hidden;
}

.chat-layout {
  display: grid;
  grid-template-columns: 220px 1fr 1fr;
  height: 100%;
}

/* Session sidebar */
.session-sidebar {
  background: var(--navy-deep);
  border-right: 1px solid rgba(255,255,255,.07);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 8px;
  overflow: hidden;
}

.new-chat-btn {
  flex-shrink: 0;
  justify-content: center;
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.8);
  border: 1px solid rgba(255,255,255,.12);
  font-size: 12px;
}
.new-chat-btn:hover { background: rgba(255,255,255,.14); }

.session-list {
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.session-item {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 12px;
  color: rgba(255,255,255,.5);
  cursor: pointer;
  transition: background .12s, color .12s;
}
.session-item:hover { background: rgba(255,255,255,.07); color: rgba(255,255,255,.8); }
.session-item.active { background: rgba(255,255,255,.1); color: var(--white); }
.session-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-delete-btn {
  flex-shrink: 0; opacity: 0; background: none; border: none;
  color: rgba(255,255,255,.5); font-size: 16px; line-height: 1;
  cursor: pointer; padding: 0 2px; border-radius: 3px;
  transition: opacity .12s, color .12s;
}
.session-item:hover .session-delete-btn { opacity: 1; }
.session-delete-btn:hover { color: var(--red) !important; }

/* Chat panel */
.chat-panel {
  display: flex;
  flex-direction: column;
  background: var(--parchment-lt);
  overflow: hidden;
}

.chat-filters {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-light);
  background: var(--white);
  flex-shrink: 0;
}

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

.messages-empty {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
}
.empty-icon { font-size: 32px; color: var(--border); margin-bottom: 12px; }
.empty-hint { font-size: 12px; margin-top: 6px; }

.message { display: flex; flex-direction: column; gap: 4px; max-width: 100%; }

.message-user { align-items: flex-end; }
.message-assistant { align-items: flex-start; }

.message-bubble {
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.65;
  max-width: 85%;
}
.message-user .message-bubble {
  background: var(--navy-mid);
  color: var(--white);
  border-bottom-right-radius: 3px;
}
.message-assistant .message-bubble {
  background: var(--white);
  color: var(--text);
  border: 1px solid var(--border-light);
  border-bottom-left-radius: 3px;
  box-shadow: var(--shadow-sm);
}

.message-bubble sup {
  color: var(--gold);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
}
.message-bubble sup:hover { text-decoration: underline; }

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  padding: 0 4px;
}

.streaming-cursor::after {
  content: '▋';
  animation: blink .7s step-end infinite;
  color: var(--gold);
}
@keyframes blink { 0%,100% { opacity: 1 } 50% { opacity: 0 } }

/* Chat input bar */
.chat-input-bar {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 14px 16px;
  border-top: 1px solid var(--border-light);
  background: var(--white);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  background: var(--parchment-lt);
  resize: none;
  max-height: 140px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color .15s;
}
#chat-input:focus { outline: none; border-color: var(--navy-mid); }

.send-btn { border-radius: 20px; padding: 10px 20px; flex-shrink: 0; }

/* Provider selector */
.provider-select {
  flex-shrink: 0;
  padding: 6px 8px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  background: var(--parchment-lt);
  font-size: 12px;
  font-weight: 600;
  color: var(--navy);
  cursor: pointer;
  outline: none;
  max-width: 110px;
  transition: border-color .15s;
}
.provider-select:focus { border-color: var(--navy-mid); }

/* Source panel */
.source-panel {
  background: var(--white);
  border-left: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.source-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border-light);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: .06em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.source-list {
  overflow-y: auto;
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.source-empty {
  color: var(--text-muted);
  font-size: 12px;
  padding: 16px;
  text-align: center;
}

.source-card {
  border: 1.5px solid var(--border-light);
  border-radius: var(--radius);
  padding: 10px 12px;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
  font-size: 12px;
}
.source-card:hover { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(201,151,58,.1); }
.source-card.active { border-color: var(--gold); background: #FFFBF0; }

.source-card-index {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  font-weight: 500;
  color: var(--gold);
  margin-bottom: 4px;
}
.source-card-name {
  font-weight: 500;
  color: var(--text);
  margin-bottom: 2px;
  line-height: 1.4;
}
.source-card-meta {
  color: var(--text-muted);
  font-size: 11px;
}
.source-card-excerpt {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--border-light);
  color: var(--text-mid);
  font-size: 11px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.pdf-viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.pdf-viewer iframe {
  flex: 1;
  border: none;
  width: 100%;
  height: 100%;
}

/* ── Settings ── */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 20px;
}

.provider-card {
  background: var(--white);
  border: 1.5px solid var(--border-light);
  border-radius: 10px;
  padding: 24px;
  box-shadow: var(--shadow-sm);
}
.provider-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}
.provider-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  flex-shrink: 0;
}
.provider-icon-mistral { background: #FF7000; color: #fff; font-size: 16px; font-weight: 700; }
.provider-name { font-weight: 600; font-size: 15px; }
.provider-status {
  margin-left: auto;
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 20px;
}
.provider-status.configured { background: #F0FDF4; color: var(--green); }
.provider-status.not-configured { background: var(--parchment); color: var(--text-muted); }

.provider-form { display: flex; flex-direction: column; gap: 12px; }
.provider-form .field { margin-bottom: 0; }

.key-input-wrap {
  display: flex;
  gap: 8px;
  align-items: center;
}
.key-input-wrap input { flex: 1; }

.provider-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

/* ── Admin ── */
.admin-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 8px;
}

.stat-card {
  background: var(--white);
  border: 1px solid var(--border-light);
  border-radius: 10px;
  padding: 20px 24px;
  box-shadow: var(--shadow-sm);
}
.stat-card-value {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 28px;
  font-weight: 500;
  color: var(--navy-mid);
  line-height: 1;
  margin-bottom: 6px;
}
.stat-card-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: .05em;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--navy-deep);
  color: var(--white);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  box-shadow: var(--shadow-md);
  z-index: 999;
  animation: slide-up .2s ease;
}
.toast.error { background: var(--red); }
@keyframes slide-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ── Markdown inside assistant bubbles ── */
.message-assistant .message-bubble { max-width: 90%; }

.message-bubble p { margin: 0 0 .75em; }
.message-bubble p:last-child { margin-bottom: 0; }

.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
  font-family: 'IBM Plex Serif', serif;
  font-weight: 600;
  color: var(--navy-deep);
  margin: 1.1em 0 .4em;
  line-height: 1.3;
}
.message-bubble h1 { font-size: 1.25em; }
.message-bubble h2 { font-size: 1.1em; }
.message-bubble h3 { font-size: 1em; }
.message-bubble h4 { font-size: .95em; color: var(--text-mid); }

.message-bubble ul,
.message-bubble ol {
  padding-left: 1.5em;
  margin: .5em 0 .75em;
}
.message-bubble li { margin-bottom: .25em; line-height: 1.6; }
.message-bubble li + li { margin-top: .1em; }

.message-bubble blockquote {
  border-left: 3px solid var(--gold);
  margin: .75em 0;
  padding: .4em .9em;
  background: #FFFBF0;
  color: var(--text-mid);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-style: italic;
}
.message-bubble blockquote p { margin: 0; }

.message-bubble code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: .85em;
  background: var(--parchment);
  border: 1px solid var(--border-light);
  border-radius: 3px;
  padding: .15em .4em;
  color: var(--navy-mid);
}

.message-bubble pre {
  background: var(--navy-deep);
  border-radius: var(--radius);
  padding: 14px 16px;
  overflow-x: auto;
  margin: .75em 0;
}
.message-bubble pre code {
  background: none;
  border: none;
  padding: 0;
  color: #E2D9C8;
  font-size: .82em;
  line-height: 1.6;
}

.message-bubble table {
  border-collapse: collapse;
  width: 100%;
  font-size: .9em;
  margin: .75em 0;
}
.message-bubble th {
  background: var(--parchment);
  padding: 7px 12px;
  text-align: left;
  font-weight: 600;
  font-size: .8em;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.message-bubble td {
  padding: 7px 12px;
  border: 1px solid var(--border-light);
  vertical-align: top;
}
.message-bubble tr:nth-child(even) td { background: var(--parchment-lt); }

.message-bubble hr {
  border: none;
  border-top: 1px solid var(--border-light);
  margin: 1em 0;
}

.message-bubble a {
  color: var(--navy-mid);
  text-decoration: underline;
  text-decoration-color: var(--gold);
  text-underline-offset: 2px;
}
.message-bubble a:hover { color: var(--gold-dim); }

.message-bubble strong { font-weight: 600; color: var(--text); }
.message-bubble em { font-style: italic; }
.message-bubble del { text-decoration: line-through; color: var(--text-muted); }

/* Citation superscripts */
.message-bubble sup.cite-ref {
  font-family: 'IBM Plex Mono', monospace;
  font-size: .72em;
  color: var(--gold);
  cursor: pointer;
  font-weight: 500;
  padding: 0 1px;
  vertical-align: super;
  line-height: 0;
}
.message-bubble sup.cite-ref:hover {
  text-decoration: underline;
  color: var(--gold-dim);
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

/* ── Loading skeleton ── */
.skeleton {
  background: linear-gradient(90deg, var(--border-light) 25%, var(--parchment) 50%, var(--border-light) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: 4px;
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
