:root {
  color-scheme: dark;

  --bg-0: #060c18;
  --bg-1: #0b1830;
  --panel: #102140;
  --panel-2: #0d1c38;
  --sidebar: #08132a;
  --border: #1e3357;
  --border-soft: #16284a;

  --text: #eef2f9;
  --text-muted: #9fb1cd;
  --text-faint: #6f84a8;

  --accent: #4f8ef7;
  --accent-strong: #7ab2ff;
  --accent-soft: rgba(79, 142, 247, 0.14);

  --highlight-bg: #11264a;
  --highlight-border: #2f5da3;

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;

  --shadow: 0 10px 30px -10px rgba(0, 10, 30, 0.6);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: linear-gradient(180deg, var(--bg-1) 0%, var(--bg-0) 100%);
  overflow: hidden;
}

/* ---------- App layout ---------- */

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    display: none;
  }
}

/* ---------- Sidebar ---------- */

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border-soft);
  padding: 1.5rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0 0.3rem;
}

.brand-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--accent-soft);
  color: var(--accent-strong);
  box-shadow: inset 0 0 0 1px rgba(79, 142, 247, 0.25);
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  color: #04101f;
  border: none;
  border-radius: var(--radius-md);
  padding: 0.7rem 1rem;
  font-size: 0.95rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 20px -6px rgba(79, 142, 247, 0.55);
  transition: transform 0.1s ease, box-shadow 0.15s ease;
}

.new-chat-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 24px -6px rgba(79, 142, 247, 0.65);
}

.db-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.65rem 1rem;
  font-size: 0.92rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease;
}

.db-btn:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.history-section {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  min-height: 0;
  flex: 1;
  overflow: hidden;
}

/* ---------- User bar (bottom of sidebar) ---------- */

.user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem;
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.user-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.user-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-faint);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: 0.3rem 0.55rem;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease;
  white-space: nowrap;
}

.logout-btn:hover {
  border-color: #ff9c9c;
  color: #ff9c9c;
}

.history-label {
  margin: 0.4rem 0.3rem 0;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  overflow-y: auto;
}

.history-empty {
  margin: 0.3rem;
  color: var(--text-faint);
  font-size: 0.88rem;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.9rem;
  border: 1px solid transparent;
  transition: background 0.12s ease, color 0.12s ease;
}

.history-item:hover {
  background: var(--panel-2);
  color: var(--text);
}

.history-item.active {
  background: var(--highlight-bg);
  border-color: var(--highlight-border);
  color: var(--text);
}

.history-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.history-delete {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0.1rem 0.3rem;
  border-radius: var(--radius-sm);
  opacity: 0;
  transition: opacity 0.12s ease, color 0.12s ease, background 0.12s ease;
}

.history-item:hover .history-delete {
  opacity: 1;
}

.history-delete:hover {
  color: #ff9c9c;
  background: rgba(255, 107, 107, 0.12);
}

/* ---------- Chat main ---------- */

.chat-main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-width: 0;
}

.chat-header {
  padding: 1.25rem 2rem 1rem;
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.model-selector {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.model-selector label {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.model-selector select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.7rem;
  font-size: 0.88rem;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.12s ease;
}

.model-selector select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.model-selector select:hover {
  border-color: var(--accent);
}

.chat-header h2 {
  margin: 0 0 0.2rem;
  font-size: 1.2rem;
  font-weight: 800;
}

.tagline {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.92rem;
}

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.empty-state {
  max-width: 60ch;
  margin: 2.5rem auto;
  text-align: center;
  color: var(--text-muted);
}

.empty-state h3 {
  margin: 0 0 0.5rem;
  color: var(--text);
  font-size: 1.2rem;
}

.empty-state p {
  margin: 0;
}

/* ---------- Messages ---------- */

.message {
  display: flex;
  flex-direction: column;
  max-width: 75ch;
}

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

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

.message-bubble {
  border-radius: var(--radius-lg);
  padding: 0.85rem 1.1rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 1rem;
  line-height: 1.6;
  box-shadow: var(--shadow);
}

.message.user .message-bubble {
  background: var(--highlight-bg);
  border: 1px solid var(--highlight-border);
}

.message.assistant .message-bubble {
  background: var(--panel);
  border: 1px solid var(--border-soft);
}

.message.pending .message-bubble {
  color: var(--text-muted);
}

/* ---------- Privacy details (expandable) ---------- */

.privacy-toggle {
  margin-top: 0.4rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: none;
  border: none;
  color: var(--text-faint);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.2rem 0.1rem;
  transition: color 0.12s ease;
}

.privacy-toggle:hover {
  color: var(--accent-strong);
}

.privacy-toggle .chevron {
  display: inline-block;
  transition: transform 0.15s ease;
  font-size: 0.7rem;
}

.privacy-toggle.open .chevron {
  transform: rotate(90deg);
}

.privacy-details {
  display: none;
  margin-top: 0.6rem;
  width: 100%;
  flex-direction: column;
  gap: 0.75rem;
}

.privacy-details.open {
  display: flex;
}

.privacy-card {
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  padding: 0.9rem 1rem;
  text-align: left;
  width: 100%;
}

.privacy-card h4 {
  margin: 0 0 0.6rem;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.content-box {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg-0);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 0.8rem 0.9rem;
  max-height: 240px;
  overflow: auto;
}

/* ---------- Entities list ---------- */

.entities-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.entity-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin: 0;
}

.entity-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.6rem 0.8rem;
  background: var(--bg-0);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}

.entity-info {
  flex: 1;
  min-width: 0;
}

.entity-label {
  font-weight: 600;
  font-size: 0.9rem;
}

.entity-value {
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
}

.badge.TOKENIZE { background: rgba(79, 142, 247, 0.16); color: var(--accent-strong); }
.badge.REDACT { background: rgba(255, 107, 107, 0.16); color: #ff9c9c; }
.badge.HASH { background: rgba(255, 193, 104, 0.16); color: #ffcf8f; }
.badge.PARTIAL_MASK { background: rgba(199, 155, 255, 0.16); color: #d6bbff; }
.badge.ALLOW { background: rgba(123, 224, 168, 0.16); color: #9be8c0; }

/* ---------- Input bar ---------- */

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  padding: 1rem 2rem;
  border-top: 1px solid var(--border-soft);
}

textarea {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.8rem 1rem;
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea::placeholder {
  color: var(--text-faint);
}

button#send {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  color: #04101f;
  border: none;
  border-radius: var(--radius-md);
  padding: 0.85rem 1.6rem;
  font-size: 1rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 20px -6px rgba(79, 142, 247, 0.55);
  transition: transform 0.1s ease, box-shadow 0.15s ease, opacity 0.15s ease;
  flex-shrink: 0;
}

button#send:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 10px 24px -6px rgba(79, 142, 247, 0.65);
}

button#send:active:not(:disabled) {
  transform: translateY(0);
}

button#send:disabled {
  opacity: 0.65;
  cursor: default;
  box-shadow: none;
}

.btn-spinner {
  display: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(4, 16, 31, 0.35);
  border-top-color: #04101f;
  animation: spin 0.7s linear infinite;
}

button#send.loading .btn-spinner {
  display: inline-block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#status {
  margin: 0 2rem 0.9rem;
  color: var(--text-muted);
  font-size: 0.88rem;
  min-height: 1.2em;
}

#status.error {
  color: #ff9c9c;
}

/* ---------- Privacy database modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 16, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  width: min(560px, 100%);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.5rem;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.modal-header h2 {
  margin: 0 0 0.35rem;
  font-size: 1.15rem;
  font-weight: 800;
}

.modal-close {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: var(--radius-sm);
}

.modal-close:hover {
  color: var(--text);
  background: var(--panel-2);
}

.db-add-form {
  display: flex;
  gap: 0.5rem;
}

.db-add-form input {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.6rem 0.8rem;
  font-size: 0.95rem;
  font-family: inherit;
}

.db-add-form select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.6rem 0.8rem;
  font-size: 0.95rem;
  font-family: inherit;
  max-width: 160px;
}

.db-add-form input:focus,
.db-add-form select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.db-add-form button {
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  color: #04101f;
  border: none;
  border-radius: var(--radius-md);
  padding: 0.6rem 1.2rem;
  font-size: 0.95rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
}

.db-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
}

.badge.dictionary-source {
  background: rgba(123, 224, 168, 0.16);
  color: #9be8c0;
}

.entity-add-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: 0.3rem 0.6rem;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.12s ease, color 0.12s ease;
}

.entity-add-btn:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.entity-add-btn:disabled {
  opacity: 0.6;
  cursor: default;
}
