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

:root {
  --slate: #334155;
  --slate-dark: #1e293b;
  --blue: #5b8fb9;
  --blue-hover: #4a7fa9;
  --bg: #f0f0f0;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --radius: 8px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: Georgia, "Times New Roman", serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
header {
  background: var(--slate);
  color: #fff;
  text-align: center;
  padding: 2.5rem 1rem 2rem;
}

header h1 {
  font-size: 2.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-bottom: 0.25rem;
}

header h1 .bot {
  font-weight: 400;
  letter-spacing: 0.08em;
}

header .tagline {
  font-style: italic;
  font-size: 1.05rem;
  opacity: 0.85;
}

/* Main */
main {
  flex: 1;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1rem;
}

/* Search Section */
.search-section {
  margin-bottom: 1.5rem;
}

.search-bar {
  display: flex;
  gap: 0;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.search-bar input {
  flex: 1;
  padding: 0.9rem 1.2rem;
  font-size: 1.05rem;
  font-family: inherit;
  border: 2px solid var(--border);
  border-right: none;
  border-radius: var(--radius) 0 0 var(--radius);
  outline: none;
  transition: border-color 0.2s;
}

.search-bar input:focus {
  border-color: var(--blue);
}

.search-bar button {
  padding: 0.9rem 1.8rem;
  font-size: 1.05rem;
  font-family: inherit;
  font-weight: 600;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 0 var(--radius) var(--radius) 0;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

.search-bar button:hover {
  background: var(--blue-hover);
}

.search-bar button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.search-options {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.search-options label {
  font-weight: 600;
}

.search-options select {
  padding: 0.3rem 0.5rem;
  font-family: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  cursor: pointer;
}

.quote {
  text-align: center;
  font-style: italic;
  color: var(--text-muted);
  margin-top: 1.2rem;
  font-size: 0.95rem;
}

/* History */
.history-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
  padding: 0.5rem 0;
  overflow-x: auto;
  font-size: 0.85rem;
}

.history-label {
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}

.history-items {
  display: flex;
  gap: 0.4rem;
}

.history-chip {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.25rem 0.75rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
  font-family: inherit;
  font-size: 0.85rem;
}

.history-chip:hover {
  background: var(--border);
}

/* Result */
.result-section {
  margin-bottom: 1.5rem;
}

.result-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}

.result-content {
  font-size: 1rem;
  line-height: 1.7;
  white-space: pre-wrap;
  word-wrap: break-word;
  min-height: 3rem;
}

.result-content h1,
.result-content h2,
.result-content h3 {
  margin-top: 1rem;
  margin-bottom: 0.4rem;
}

.result-content p {
  margin-bottom: 0.6rem;
}

.result-content ul,
.result-content ol {
  margin: 0.4rem 0 0.6rem 1.5rem;
}

.result-content code {
  background: #f1f5f9;
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  font-size: 0.9em;
}

.result-content pre {
  background: #f1f5f9;
  padding: 0.8rem;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.result-content strong {
  font-weight: 700;
}

.result-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.btn-action {
  padding: 0.4rem 0.8rem;
  font-family: inherit;
  font-size: 0.85rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-action:hover {
  background: var(--border);
}

.btn-action.active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

/* Buttons */
.btn-small {
  padding: 0.3rem 0.7rem;
  font-family: inherit;
  font-size: 0.85rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-small:hover {
  background: var(--border);
}

.btn-primary {
  padding: 0.5rem 1.2rem;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}

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

/* Knowledge Base Toggle */
.kb-toggle {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--slate);
  color: #fff;
  border: none;
  border-radius: 50px;
  padding: 0.7rem 1.2rem;
  font-size: 1rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  z-index: 100;
  transition: background 0.2s;
}

.kb-toggle:hover {
  background: var(--slate-dark);
}

/* Sidebar */
.sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 380px;
  max-width: 90vw;
  height: 100vh;
  background: var(--card-bg);
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.15);
  z-index: 200;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}

.sidebar[hidden] {
  display: none;
}

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

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.2rem 1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--card-bg);
  z-index: 1;
}

.sidebar-header h2 {
  font-size: 1.2rem;
}

.sidebar-actions {
  display: flex;
  gap: 0.4rem;
}

.kb-topics {
  padding: 1rem;
}

.kb-topic-group {
  margin-bottom: 1.5rem;
}

.kb-topic-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--blue);
  margin-bottom: 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.kb-topic-title .topic-export {
  font-size: 0.8rem;
  color: var(--text-muted);
  cursor: pointer;
  border: none;
  background: none;
  font-family: inherit;
}

.kb-topic-title .topic-export:hover {
  color: var(--blue);
}

.kb-entry {
  background: var(--bg);
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

.kb-entry-clickable {
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s, border-color 0.15s;
  border: 2px solid transparent;
}

.kb-entry-clickable:hover {
  background: #e2e8f0;
  border-color: var(--blue);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.kb-entry-clickable:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}

.kb-entry-query {
  font-weight: 600;
  margin-bottom: 0.25rem;
  color: var(--slate);
}

.kb-entry-preview {
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.4;
  max-height: 3.2em;
  overflow: hidden;
}

.kb-entry-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.4rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.kb-entry-delete {
  color: #ef4444;
  cursor: pointer;
  border: none;
  background: none;
  font-size: 0.8rem;
  font-family: inherit;
}

.kb-entry-delete:hover {
  text-decoration: underline;
}

.kb-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 1rem;
  font-style: italic;
}

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 150;
}

.overlay[hidden] {
  display: none;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.modal[hidden] {
  display: none;
}

#authModal {
  background: rgba(0, 0, 0, 0.6);
}

.modal-content {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 350px;
  max-width: 90vw;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
}

.modal-content h3 {
  margin-bottom: 0.75rem;
}

.modal-content input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  font-family: inherit;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: 4px;
  outline: none;
  margin-bottom: 1rem;
}

.modal-content input:focus {
  border-color: var(--blue);
}

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

/* Loading animation */
@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.loading-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--blue);
  margin: 0 2px;
  animation: pulse 1.2s infinite;
}

.loading-dot:nth-child(2) { animation-delay: 0.2s; }
.loading-dot:nth-child(3) { animation-delay: 0.4s; }

/* Footer */
footer {
  background: var(--slate);
  color: #fff;
  text-align: center;
  padding: 1rem;
  font-size: 0.85rem;
  opacity: 0.9;
}

/* Responsive */
@media (max-width: 600px) {
  header h1 {
    font-size: 2rem;
  }

  main {
    padding: 1rem 0.75rem;
  }

  .search-bar input {
    font-size: 0.95rem;
    padding: 0.75rem 1rem;
  }

  .search-bar button {
    padding: 0.75rem 1.2rem;
    font-size: 0.95rem;
  }

  .search-options {
    flex-wrap: wrap;
  }

  .sidebar {
    width: 100vw;
    max-width: 100vw;
  }

  .result-card {
    padding: 1rem;
  }
}
