*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* header */

header {
  width: 100%;
  background: #E4002B;
  color: #fff;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 2px 6px #00000033;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: .3px;
}

header p {
  font-size: .78rem;
  opacity: .85;
  margin-top: 1px;
}

/* chat container */

.chat-wrapper {
  width: 100%;
  max-width: 760px;
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px 16px 0;
  min-height: 0;
}

#messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}

/* bubbles */

.message {
  display: flex;
  flex-direction: column;
  max-width: 78%;
}

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

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

.bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user .bubble {
  background: #E4002B;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.bot .bubble {
  background: #fff;
  color: #1a1a1a;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px #0000001a;
}

.message.bot .bubble.loading {
  color: #888;
  font-style: italic;
}

.sources {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #e0e0e0;
  font-size: 0.85rem;
  color: #666;
}

.sources strong {
  color: #1a1a1a;
  display: block;
  margin-bottom: 6px;
}

.sources ul {
  list-style: none;
  padding-left: 0;
}

.sources li {
  margin-bottom: 6px;
}

.sources a {
  color: #E4002B;
  text-decoration: none;
}

.sources a:hover {
  text-decoration: underline;
}

.timestamp {
  font-size: .68rem;
  color: #999;
  margin-top: 4px;
  padding: 0 4px;
}

/* welcome message */

.welcome {
  text-align: center;
  color: #888;
  font-size: .9rem;
  margin: auto;
  padding: 32px 0;
}

.welcome strong {
  color: #E4002B;
}

/* input box */

.input-area {
  width: 100%;
  max-width: 760px;
  padding: 12px 16px 20px;
  background: hsl(0, 0%, 96%);
}

.input-row {
  display: flex;
  gap: 8px;
  background: #fff;
  border: 1.5px solid #ddd;
  border-radius: 28px;
  padding: 6px 6px 6px 16px;
  transition: border-color .2s;
}

.input-row:focus-within {
  border-color: #E4002B;
}

#user-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 0.95rem;
  background: transparent;
  resize: none;
  max-height: 120px;
  line-height: 1.5;
  padding: 4px 0;
}

#send {
  background: #E4002B;
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .2s, transform .1s;
}

#send:hover {
  background: #c0001f;
}

#send:active {
  transform: scale(.93);
}

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

#send svg {
  width: 18px;
  height: 18px;
  fill: #fff;
}

/* ── Header nav ─────────────────────────────────────────────────────── */

.header-nav {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.nav-link {
  color: #fff;
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 18px;
  border: 1.5px solid rgba(255,255,255,0.4);
  transition: background .2s, border-color .2s;
}

.nav-link:hover,
.nav-link.active {
  background: rgba(255,255,255,0.2);
  border-color: #fff;
}

/* ── Dashboard layout ───────────────────────────────────────────────── */

.dashboard {
  width: 100%;
  max-width: 960px;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ── Knowledge base controls ─────────────────────────────────────────── */

.kb-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.kb-btn {
  padding: 6px 14px;
  border: 1.5px solid #ddd;
  border-radius: 18px;
  background: #fff;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all .2s;
}

.kb-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.kb-reload:hover:not(:disabled) {
  border-color: #2e7d32;
  color: #2e7d32;
}

.kb-clear:hover:not(:disabled) {
  border-color: #c62828;
  color: #c62828;
}

.kb-scrape:hover:not(:disabled) {
  border-color: #1565c0;
  color: #1565c0;
}

.kb-pdf:hover:not(:disabled) {
  border-color: #6a1b9a;
  color: #6a1b9a;
}

/* ── Scrape / PDF modal ──────────────────────────────────────────────── */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex;
}

.modal-box {
  background: #fff;
  border-radius: 14px;
  padding: 32px 36px;
  width: 480px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 20px 60px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: #111;
  margin: 0;
}

.modal-icon {
  font-size: 1.4rem;
  font-weight: 700;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.modal-icon.icon-done {
  background: #e8f5e9;
  color: #2e7d32;
}

.modal-icon.icon-error {
  background: #ffebee;
  color: #c62828;
}

.modal-subtitle {
  font-size: 0.82rem;
  color: #666;
  margin: 0;
}

.modal-progress-track {
  height: 8px;
  background: #eee;
  border-radius: 99px;
  overflow: hidden;
}

.modal-progress-fill {
  height: 100%;
  width: 0%;
  background: #1565c0;
  border-radius: 99px;
  transition: width 1.8s ease;
}

.modal-progress-fill.progress-done {
  background: #2e7d32;
}

.modal-progress-fill.progress-error {
  background: #c62828;
}

.modal-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: #888;
}

.modal-message {
  font-size: 0.85rem;
  color: #444;
  margin: 0;
  min-height: 1.2em;
}

.modal-close-btn {
  align-self: flex-end;
  padding: 8px 22px;
  border: none;
  border-radius: 20px;
  background: #E4002B;
  color: #fff;
  font-size: 0.85rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
}

.modal-close-btn:hover {
  background: #b0001f;
}

.kb-status {
  font-size: 0.8rem;
  color: #666;
}

.kb-status.kb-success {
  color: #2e7d32;
}

.kb-status.kb-error {
  color: #c62828;
}

/* ── Filter bar ─────────────────────────────────────────────────────── */

.filter-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.filter-label {
  font-size: 0.85rem;
  color: #666;
  font-weight: 500;
}

.filter-btn {
  padding: 6px 14px;
  border: 1.5px solid #ddd;
  border-radius: 18px;
  background: #fff;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all .2s;
}

.filter-btn:hover {
  border-color: #E4002B;
  color: #E4002B;
}

.filter-btn.active {
  background: #E4002B;
  color: #fff;
  border-color: #E4002B;
}

/* ── Metric cards ───────────────────────────────────────────────────── */

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.metric-card {
  background: #fff;
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  text-align: center;
}

.metric-value {
  font-size: 2rem;
  font-weight: 700;
  color: #E4002B;
  line-height: 1.2;
}

.metric-unit {
  font-size: 0.9rem;
  font-weight: 400;
  color: #888;
}

.metric-label {
  font-size: 0.82rem;
  color: #666;
  margin-top: 4px;
}

.metric-danger {
  color: #c0392b;
}

/* ── Dashboard cards (chart, table) ─────────────────────────────────── */

.dashboard-card {
  background: #fff;
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

/* ── Response time chart ────────────────────────────────────────────── */

.chart-container {
  height: 180px;
  display: flex;
  align-items: flex-end;
  position: relative;
  overflow: hidden;
}

.chart-y-axis {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
  padding-right: 8px;
  font-size: 0.7rem;
  color: #999;
  min-width: 48px;
  text-align: right;
}

.chart-bars {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 100%;
  min-width: 0;
  overflow: hidden;
  border-bottom: 1px solid #eee;
}

.chart-bar {
  border-radius: 3px 3px 0 0;
  min-height: 2px;
  flex-shrink: 1;
  transition: opacity .2s;
}

.chart-bar:hover {
  opacity: 0.75;
}

.chart-loading,
.chart-empty {
  width: 100%;
  text-align: center;
  color: #999;
  font-size: 0.85rem;
  align-self: center;
}

/* ── Logs table ─────────────────────────────────────────────────────── */

.table-wrapper {
  overflow-x: auto;
}

.logs-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.logs-table th {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 2px solid #eee;
  color: #666;
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.logs-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
  color: #333;
}

.logs-table tr:hover {
  background: #fafafa;
}

.row-error {
  background: #fff5f5;
}

.row-error:hover {
  background: #ffebeb !important;
}

.cell-query {
  max-width: 320px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
}

.status-ok {
  background: #e8f5e9;
  color: #2e7d32;
}

.status-fail {
  background: #ffebee;
  color: #c62828;
}

.table-loading,
.table-empty {
  text-align: center;
  color: #999;
  padding: 24px 12px;
  font-style: italic;
}

/* ── Pagination ─────────────────────────────────────────────────────── */

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding-top: 16px;
}

.page-btn {
  padding: 6px 14px;
  border: 1.5px solid #ddd;
  border-radius: 18px;
  background: #fff;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all .2s;
}

.page-btn:hover {
  border-color: #E4002B;
  color: #E4002B;
}

.page-info {
  font-size: 0.8rem;
  color: #666;
}

/* ── Nav user badge ────────────────────────────────────────────────── */

.nav-user {
  color: #fff;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 6px 14px;
  opacity: 0.85;
}

/* ── Auth pages ────────────────────────────────────────────────────── */

.auth-wrapper {
  width: 100%;
  max-width: 420px;
  padding: 40px 16px;
  margin: 0 auto;
}

.auth-card {
  background: #fff;
  border-radius: 18px;
  padding: 32px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.auth-title {
  font-size: 1.4rem;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 4px;
}

.auth-subtitle {
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 24px;
}

.auth-error {
  background: #ffebee;
  color: #c62828;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.85rem;
  margin-bottom: 16px;
}

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

.auth-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #444;
  margin-top: 8px;
}

.auth-input {
  padding: 10px 14px;
  border: 1.5px solid #ddd;
  border-radius: 10px;
  font-size: 0.95rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

.auth-input:focus {
  border-color: #E4002B;
}

.auth-btn {
  margin-top: 16px;
  padding: 12px;
  background: #E4002B;
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.auth-btn:hover {
  background: #c0001f;
}

.auth-footer {
  text-align: center;
  font-size: 0.85rem;
  color: #666;
  margin-top: 20px;
}

.auth-footer a {
  color: #E4002B;
  text-decoration: none;
  font-weight: 600;
}

.auth-footer a:hover {
  text-decoration: underline;
}

/* ── Chat layout with sidebar ──────────────────────────────────────── */

.chat-layout {
  flex: 1;
  display: flex;
  width: 100%;
  min-height: 0;
  position: relative;
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}

.chat-sidebar {
  width: 260px;
  background: #fff;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  height: 100%;
}

.sidebar-toggle {
  display: none;
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 20;
  background: #fff;
  border: 1.5px solid #ddd;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 1.1rem;
  cursor: pointer;
  color: #444;
}

@media (max-width: 768px) {
  .chat-sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s;
    box-shadow: 2px 0 8px rgba(0,0,0,0.1);
  }
  .chat-sidebar.open {
    transform: translateX(0);
  }
  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #eee;
}

.sidebar-header h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: #333;
}

.sidebar-new-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1.5px solid #ddd;
  background: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #444;
  transition: all 0.2s;
}

.sidebar-new-btn:hover {
  border-color: #E4002B;
  color: #E4002B;
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.sidebar-empty {
  font-size: 0.82rem;
  color: #999;
  text-align: center;
  padding: 24px 8px;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
}

.sidebar-item:hover {
  background: #f5f5f5;
}

.sidebar-item.active {
  background: #fce4e8;
}

.sidebar-item-title {
  flex: 1;
  font-size: 0.82rem;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-item-delete {
  background: none;
  border: none;
  font-size: 1.1rem;
  color: #bbb;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}

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

.sidebar-item-delete:hover {
  color: #c62828;
}

/* ── History page ──────────────────────────────────────────────────── */

.history-wrapper {
  width: 100%;
  max-width: 760px;
  padding: 24px 16px;
  margin: 0 auto;
}

.history-title {
  font-size: 1.3rem;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 20px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-card {
  background: #fff;
  border-radius: 14px;
  padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.history-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.history-card-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: #1a1a1a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.history-card-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.history-resume-btn {
  padding: 5px 14px;
  border: 1.5px solid #E4002B;
  border-radius: 14px;
  background: #fff;
  color: #E4002B;
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.2s;
}

.history-resume-btn:hover {
  background: #E4002B;
  color: #fff;
}

.history-delete-btn {
  padding: 5px 14px;
  border: 1.5px solid #ddd;
  border-radius: 14px;
  background: #fff;
  color: #666;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s;
}

.history-delete-btn:hover {
  border-color: #c62828;
  color: #c62828;
}

.history-card-date {
  display: block;
  font-size: 0.78rem;
  color: #999;
  margin-top: 4px;
}

.history-expand-btn {
  margin-top: 10px;
  padding: 5px 12px;
  border: 1px solid #ddd;
  border-radius: 10px;
  background: #fafafa;
  color: #666;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s;
}

.history-expand-btn:hover {
  border-color: #E4002B;
  color: #E4002B;
}

.history-card-messages {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #eee;
}

.history-msg {
  margin-bottom: 12px;
  font-size: 0.85rem;
  line-height: 1.5;
}

.history-msg-query {
  color: #333;
  margin-bottom: 4px;
}

.history-msg-answer {
  color: #555;
}

.history-loading {
  font-size: 0.82rem;
  color: #999;
  font-style: italic;
}

.history-empty {
  text-align: center;
  color: #888;
  font-size: 0.9rem;
  padding: 40px 16px;
}

.history-empty a {
  color: #E4002B;
  text-decoration: none;
  font-weight: 600;
}

.history-empty a:hover {
  text-decoration: underline;
}