/* ===== VARIABLES ===== */
:root {
  --sidebar-bg: #0f0f17;
  --sidebar-hover: #1e1e2e;
  --sidebar-active: #2a2a3e;
  --main-bg: #ffffff;
  --border: #2a2a3e;
  --text-primary: #e8e8f0;
  --text-secondary: #888899;
  --text-dark: #1a1a2e;
  --accent: #6c63ff;
  --accent-hover: #5a52e0;
  --danger: #e05a5a;
  --danger-hover: #c44;
  --success: #4caf50;
  --user-msg-bg: #f0f0ff;
  --assistant-msg-bg: #f9f9f9;
  --input-bg: #1e1e2e;
  --input-border: #3a3a5e;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
  --radius: 8px;
  --radius-lg: 12px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

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

html,
body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  background: var(--main-bg);
  color: var(--text-dark);
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.flex-1 {
  flex: 1;
}

/* ===== AUTH PAGE ===== */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #0f0f17 0%, #1a1a2e 100%);
}

.auth-card {
  background: #1e1e2e;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.auth-header {
  text-align: center;
  margin-bottom: 1.5rem;
}

.auth-header h1 {
  font-size: 1.8rem;
  color: var(--text-primary);
  margin-bottom: 0.4rem;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ===== TABS ===== */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.2rem;
}

.tab {
  flex: 1;
  padding: 0.55rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.9rem;
  transition: color 0.15s, border-color 0.15s;
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.tab-panel.hidden {
  display: none;
}

/* ===== INPUTS ===== */
.input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}

.input:focus {
  border-color: var(--accent);
}

.input::placeholder {
  color: var(--text-secondary);
}

.input-sm {
  padding: 0.35rem 0.55rem;
  font-size: 0.82rem;
}

.select {
  width: 100%;
  padding: 0.5rem 0.65rem;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
}

.textarea {
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.82rem;
  font-family: var(--font);
  outline: none;
  resize: vertical;
  min-height: 80px;
  line-height: 1.5;
  transition: border-color 0.15s;
}

.textarea:focus {
  border-color: var(--accent);
}

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

.save-status {
  font-size: 0.72rem;
  color: var(--text-secondary);
  transition: opacity 0.3s;
}

.save-status.saved {
  color: var(--success);
}

/* ===== INSTRUCTIONS MODAL ===== */
.instr-modal-content {
  width: 70vw;
  min-width: 600px;
  max-width: 1000px;
  max-height: 90vh;
  background: #1e1e2e;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.instr-hint {
  color: var(--text-secondary);
  font-size: 0.8rem;
  padding: 0.75rem 1.25rem 0;
  margin: 0;
}

.instr-textarea {
  flex: 1;
  width: 100%;
  min-height: 55vh;
  padding: 0.85rem 1.25rem;
  background: var(--input-bg);
  border: none;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-family: var(--font);
  line-height: 1.6;
  resize: none;
  outline: none;
  margin-top: 0.5rem;
}

.instr-textarea::placeholder {
  color: var(--text-secondary);
}

.instr-modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.85rem 1.25rem;
  border-top: 1px solid var(--border);
}

/* ===== TOV ===== */
.tov-status {
  min-height: 0;
}

.btn-primary {
  padding: 0.55rem 1.1rem;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  padding: 0.45rem 0.9rem;
  background: #2a2a3e;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: #333350;
}

.btn-ghost {
  padding: 0.3rem 0.6rem;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: var(--radius);
  transition: color 0.15s, background 0.15s;
}

.btn-ghost:hover {
  color: var(--text-primary);
  background: var(--sidebar-hover);
}

.btn-danger {
  padding: 0.4rem 0.75rem;
  background: transparent;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}

.btn-full {
  width: 100%;
}

.btn-xs {
  padding: 0.28rem 0.6rem;
  font-size: 0.78rem;
}

.error-msg {
  color: #f87171;
  font-size: 0.82rem;
  padding: 0.3rem 0;
}

/* ===== APP LAYOUT ===== */
.app-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: 260px;
  min-width: 220px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-block {
  padding: 0.75rem 0.85rem;
}

.sidebar-block-grow {
  flex: 1;
  overflow-y: auto;
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.user-email {
  color: var(--text-primary);
  font-size: 0.82rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.section-label {
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.row {
  display: flex;
  align-items: center;
}

.gap-xs {
  gap: 0.4rem;
}

.mt-xs {
  margin-top: 0.4rem;
}

.mt-sm {
  margin-top: 0.6rem;
}

/* ===== SESSIONS ===== */
.session-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.session-item {
  display: flex;
  align-items: center;
  border-radius: var(--radius);
  overflow: hidden;
  transition: background 0.12s;
}

.session-item:hover {
  background: var(--sidebar-hover);
}

.session-item.active {
  background: var(--sidebar-active);
}

.session-btn {
  flex: 1;
  background: none;
  border: none;
  padding: 0.5rem 0.6rem;
  color: var(--text-primary);
  font-size: 0.83rem;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.session-actions {
  display: flex;
  gap: 2px;
  padding: 0 4px;
  opacity: 0;
  transition: opacity 0.12s;
}

.session-item:hover .session-actions,
.session-item.active .session-actions {
  opacity: 1;
}

.session-icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 3px 5px;
  border-radius: 4px;
  font-size: 0.8rem;
  line-height: 1;
  transition: color 0.12s, background 0.12s;
}

.session-icon-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.08);
}

.session-icon-btn.del:hover {
  color: var(--danger);
}

/* ===== DOCUMENTS ===== */
.file-label {
  display: block;
  cursor: pointer;
}

.doc-list {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.doc-item {
  background: var(--sidebar-hover);
  border-radius: var(--radius);
  padding: 0.45rem 0.6rem;
}

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

.doc-name {
  color: var(--text-primary);
  font-size: 0.8rem;
  font-weight: 500;
  word-break: break-word;
}

.doc-meta {
  color: var(--text-secondary);
  font-size: 0.72rem;
  margin-top: 2px;
}

.doc-sections {
  margin-top: 0.4rem;
}

.doc-sections-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 0.25rem;
}

.doc-sections-label {
  color: var(--text-secondary);
  font-size: 0.72rem;
  flex: 1;
}

.section-chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.section-chip {
  background: #2a2a3e;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  font-size: 0.72rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  user-select: none;
}

.section-chip.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}


.tov-active {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(76, 175, 80, 0.12);
  border: 1px solid rgba(76, 175, 80, 0.3);
  border-radius: var(--radius);
  padding: 0.4rem 0.6rem;
  font-size: 0.8rem;
  color: var(--success);
}

.tov-empty {
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-style: italic;
}

/* ===== MAIN AREA ===== */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--main-bg);
  overflow: hidden;
  position: relative;
}

/* ===== EMPTY STATE ===== */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #aaa;
  padding: 2rem;
}

.empty-state h1 {
  font-size: 2.2rem;
  color: #333;
  margin-bottom: 0.5rem;
}

.empty-sub {
  font-size: 1rem;
}

/* ===== MESSAGES ===== */
.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1.5rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.chat-header {
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid #eee;
  font-size: 1rem;
  font-weight: 600;
  color: #333;
}

.msg {
  display: flex;
  gap: 0.75rem;
  max-width: 820px;
  width: 100%;
}

.msg.user {
  flex-direction: row-reverse;
  align-self: flex-end;
}

.msg.assistant {
  align-self: flex-start;
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  flex-shrink: 0;
  font-weight: 600;
}

.msg.user .msg-avatar {
  background: var(--accent);
  color: #fff;
}

.msg.assistant .msg-avatar {
  background: #eee;
  color: #555;
}

.msg-bubble {
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-lg);
  font-size: 0.9rem;
  line-height: 1.6;
  max-width: calc(100% - 45px);
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: var(--user-msg-bg);
  border: 1px solid #dde;
  border-bottom-right-radius: 4px;
}

.msg.assistant .msg-bubble {
  background: var(--assistant-msg-bg);
  border: 1px solid #eee;
  border-bottom-left-radius: 4px;
  white-space: pre-wrap;
}

/* Markdown in assistant messages */
.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3 {
  margin: 0.5rem 0 0.3rem;
}

.msg-bubble p {
  margin: 0.25rem 0;
}

.msg-bubble code {
  background: #f0f0f8;
  border-radius: 4px;
  padding: 1px 5px;
  font-family: 'Consolas', monospace;
  font-size: 0.85em;
}

.msg-bubble pre {
  background: #f5f5ff;
  border: 1px solid #dde;
  border-radius: 6px;
  padding: 0.75rem;
  overflow-x: auto;
  margin: 0.4rem 0;
}

.msg-bubble pre code {
  background: none;
  padding: 0;
}

.msg-bubble ul,
.msg-bubble ol {
  padding-left: 1.4rem;
  margin: 0.3rem 0;
}

/* ===== BOTTOM BAR ===== */
.bottom-bar {
  padding: 0.75rem 1.5rem 1rem;
  border-top: 1px solid #eee;
  background: var(--main-bg);
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

/* ===== TOOL PILLS ===== */
.tool-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.tool-pill {
  padding: 0.28rem 0.75rem;
  border: 1px solid #cce;
  border-radius: 20px;
  background: none;
  color: #556;
  font-size: 0.78rem;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.tool-pill:hover {
  background: #f0f0ff;
  border-color: var(--accent);
  color: var(--accent);
}

.tool-pill.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ===== TOV BUTTON ===== */
.btn-tov {
  padding: 0.5rem 1rem;
  background: linear-gradient(135deg, #6c63ff, #a78bfa);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn-tov:hover {
  opacity: 0.9;
}

.btn-tov:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ===== CHAT INPUT ===== */
.input-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.chat-textarea {
  flex: 1;
  padding: 0.6rem 0.85rem;
  background: #f5f5f8;
  border: 1px solid #dde;
  border-radius: var(--radius-lg);
  color: #1a1a2e;
  font-size: 0.9rem;
  font-family: var(--font);
  resize: none;
  outline: none;
  max-height: 180px;
  line-height: 1.5;
  transition: border-color 0.15s;
}

.chat-textarea:focus {
  border-color: var(--accent);
}

.chat-textarea::placeholder {
  color: #aaa;
}

.btn-send {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

.btn-send:hover {
  background: var(--accent-hover);
}

.btn-send:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-card {
  background: #1e1e2e;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.modal-title {
  color: var(--text-primary);
  font-size: 1rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 0.6rem 1.2rem;
  border-radius: 20px;
  font-size: 0.85rem;
  z-index: 2000;
  box-shadow: var(--shadow);
  animation: fade-in 0.2s ease;
}

@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* ===== MODE TABS ===== */
.mode-tabs {
  display: flex;
  padding: 0.4rem 0.6rem;
  gap: 4px;
}

.mode-tab {
  flex: 1;
  padding: 0.35rem 0.5rem;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.78rem;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  text-align: center;
}

.mode-tab:hover {
  background: var(--sidebar-hover);
  color: var(--text-primary);
}

.mode-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ===== STOP BUTTON ===== */
.btn-stop {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--danger);
  border: none;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

.btn-stop:hover {
  background: var(--danger-hover);
}

/* ===== SPINNER ===== */
.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 4px;
}

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

/* ===== TYPING CURSOR ===== */
.typing-cursor::after {
  content: '▋';
  animation: blink 0.9s step-start infinite;
}

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

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar {
  width: 5px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #333;
  border-radius: 4px;
}

/* ===== LOADING STATE ===== */
.loading-dots::after {
  content: '...';
  animation: dots 1.2s steps(4, end) infinite;
}

@keyframes dots {

  0%,
  20% {
    content: '';
  }

  40% {
    content: '.';
  }

  60% {
    content: '..';
  }

  80%,
  100% {
    content: '...';
  }
}