/* ==========================================================================
   IT Policy Agent — "Institutional Clarity" Design System
   Swiss-influenced with warm authority
   ========================================================================== */

/* ---------- Custom Properties ---------- */
:root {
  /* Core */
  --color-ink: #1a1a2e;
  --color-paper: #f7f5f0;
  --color-surface: #ffffff;

  /* Accent */
  --color-accent: #c45d3e;
  --color-accent-soft: #f0d9cf;
  --color-accent-deep: #8b3a24;

  /* Semantic */
  --color-source: #2d6a4f;
  --color-source-soft: #d4edda;
  --color-error: #b23a48;
  --color-muted: #6b7280;

  /* Spacing scale (4px base) */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 40px;
  --space-2xl: 64px;

  /* Typography scale */
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --text-2xl: 2rem;
}

/* ---------- Reset & Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'DM Sans', sans-serif;
  font-size: var(--text-base);
  line-height: 1.6;
  color: var(--color-ink);
  background-color: var(--color-paper);
  min-height: 100vh;
}

/* Screen-reader only utility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ==========================================================================
   Layout — Two-column desktop, single-column mobile
   ========================================================================== */

.app-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 1fr;
  min-height: 100vh;
}

/* ---------- Sidebar (desktop) ---------- */
.sidebar {
  background-color: var(--color-surface);
  border-right: 1px solid var(--color-accent-soft);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  padding: var(--space-md) var(--space-md);
  border-bottom: 1px solid var(--color-accent-soft);
  display: flex;
  align-items: center;
  min-height: 56px;
  box-sizing: border-box;
}

.sidebar-title {
  font-family: 'DM Serif Display', serif;
  font-size: var(--text-xl);
  font-weight: 400;
  color: var(--color-ink);
}

.source-list {
  padding: var(--space-md);
  flex: 1;
  overflow-y: auto;
}

.source-empty {
  color: var(--color-muted);
  font-size: var(--text-sm);
  font-style: italic;
}

/* Source citation chips */
.source-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-xs) var(--space-sm);
  margin: var(--space-xs);
  background-color: var(--color-source-soft);
  color: var(--color-source);
  font-family: 'JetBrains Mono', monospace;
  font-size: var(--text-xs);
  font-weight: 500;
  border: 1px solid var(--color-source);
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.15s ease;
  animation: chip-in 0.25s ease-out both;
}

.source-chip:hover {
  background-color: var(--color-source);
  color: var(--color-surface);
}

.source-chip:focus-visible {
  outline: 2px solid var(--color-source);
  outline-offset: 2px;
}

@keyframes chip-in {
  from {
    opacity: 0;
    transform: translateX(-8px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

/* ==========================================================================
   Main Content Area
   ========================================================================== */

.main-content {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: var(--color-paper);
}

/* ---------- Top Bar ---------- */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-accent-soft);
  background-color: var(--color-surface);
  min-height: 56px;
  box-sizing: border-box;
}

.top-bar-title {
  font-family: 'DM Serif Display', serif;
  font-size: var(--text-xl);
  font-weight: 400;
  color: var(--color-ink);
}

/* ---------- Buttons ---------- */
.btn {
  font-family: 'DM Sans', sans-serif;
  font-size: var(--text-sm);
  font-weight: 500;
  border: none;
  border-radius: 4px;
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background-color: var(--color-accent);
  color: var(--color-surface);
}

.btn-primary:hover {
  background-color: var(--color-accent-deep);
}

.btn-primary:disabled {
  background-color: var(--color-muted);
  cursor: not-allowed;
}

.btn-secondary {
  background-color: transparent;
  color: var(--color-accent);
  border: 1px solid var(--color-accent);
}

.btn-secondary:hover {
  background-color: var(--color-accent-soft);
}

/* ==========================================================================
   Chat Messages
   ========================================================================== */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-lg) var(--space-xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
}

/* Welcome state */
.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex: 1;
  padding: var(--space-2xl) var(--space-lg);
}

.chat-welcome-heading {
  font-family: 'DM Serif Display', serif;
  font-size: var(--text-2xl);
  font-weight: 400;
  color: var(--color-ink);
  margin-bottom: var(--space-sm);
}

.chat-welcome-text {
  color: var(--color-muted);
  font-size: var(--text-lg);
  max-width: 480px;
}

/* Message containers */
.message {
  max-width: 720px;
  animation: message-in 0.3s ease-out both;
  display: flex;
  gap: var(--space-sm);
  align-items: flex-start;
}

/* Avatars */
.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
}

.message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.message-avatar-initials {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  background-color: var(--color-accent);
  color: var(--color-surface);
  font-family: 'DM Sans', sans-serif;
  font-size: var(--text-sm);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.message-bubble {
  flex: 1;
  min-width: 0;
}

.message-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-user .message-bubble {
  background-color: var(--color-accent);
  color: var(--color-surface);
  padding: var(--space-sm) var(--space-md);
  border-radius: 12px 12px 2px 12px;
  font-size: var(--text-base);
}

.message-agent {
  align-self: flex-start;
}

.message-agent .message-bubble {
  background-color: var(--color-surface);
  padding: var(--space-md) var(--space-lg);
  border-radius: 2px 12px 12px 12px;
  border: 1px solid var(--color-accent-soft);
  position: relative;
}

.message-agent .message-bubble .answer-text {
  font-family: 'DM Sans', sans-serif;
  font-size: var(--text-base);
  font-weight: 400;
  line-height: 1.7;
  color: var(--color-ink);
}

/* Markdown heading styles within agent responses */
.message-agent .answer-text h1,
.message-agent .answer-text h2,
.message-agent .answer-text h3 {
  font-family: 'DM Serif Display', serif;
  font-weight: 400;
  margin-top: var(--space-md);
  margin-bottom: var(--space-sm);
}

.message-agent .answer-text h1 { font-size: var(--text-xl); }
.message-agent .answer-text h2 { font-size: var(--text-lg); }
.message-agent .answer-text h3 { font-size: var(--text-base); font-weight: 500; }

/* Markdown paragraph spacing */
.message-agent .answer-text p {
  margin-bottom: var(--space-sm);
}

.message-agent .answer-text p:last-child {
  margin-bottom: 0;
}

/* Markdown lists */
.message-agent .answer-text ul,
.message-agent .answer-text ol {
  margin: var(--space-sm) 0;
  padding-left: var(--space-lg);
}

.message-agent .answer-text li {
  margin-bottom: var(--space-xs);
}

/* Markdown inline code */
.message-agent .answer-text code {
  font-family: 'JetBrains Mono', monospace;
  font-size: var(--text-sm);
  background-color: var(--color-paper);
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid var(--color-accent-soft);
}

/* Markdown code blocks */
.message-agent .answer-text pre {
  background-color: var(--color-ink);
  color: var(--color-paper);
  padding: var(--space-md);
  border-radius: 6px;
  overflow-x: auto;
  margin: var(--space-sm) 0;
}

.message-agent .answer-text pre code {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font-size: var(--text-sm);
}

/* Markdown bold and emphasis */
.message-agent .answer-text strong {
  font-weight: 600;
}

.message-agent .answer-text em {
  font-style: italic;
}

/* Markdown blockquotes */
.message-agent .answer-text blockquote {
  border-left: 3px solid var(--color-accent);
  padding-left: var(--space-md);
  margin: var(--space-sm) 0;
  color: var(--color-muted);
  font-style: italic;
}

/* Markdown tables */
.message-agent .answer-text table {
  border-collapse: collapse;
  width: 100%;
  margin: var(--space-sm) 0;
  font-size: var(--text-sm);
}

.message-agent .answer-text th,
.message-agent .answer-text td {
  border: 1px solid var(--color-accent-soft);
  padding: var(--space-xs) var(--space-sm);
  text-align: left;
}

.message-agent .answer-text th {
  background-color: var(--color-paper);
  font-weight: 600;
}

/* Staggered content reveal */
.message-agent .answer-text {
  animation: paragraph-reveal 0.35s ease-out both;
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes paragraph-reveal {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Source citations within a message */
.message-sources {
  margin-top: var(--space-md);
  padding-top: var(--space-sm);
  border-top: 1px solid var(--color-accent-soft);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

/* ==========================================================================
   "The Stamp" — High-confidence source verification seal
   ========================================================================== */

@keyframes stamp-in {
  0% {
    opacity: 0;
    transform: scale(1.3) rotate(-15deg);
  }
  60% {
    opacity: 1;
    transform: scale(0.95) rotate(2deg);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(0deg);
  }
}

.source-verified {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background-color: var(--color-source);
  color: var(--color-surface);
  font-size: var(--text-sm);
  margin-left: var(--space-sm);
  vertical-align: middle;
  animation: stamp-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* ==========================================================================
   Loading Indicator — Three pulsing dots
   ========================================================================== */

.loading-indicator {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-xl);
}

.loading-indicator[hidden] {
  display: none;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--color-accent);
  animation: dot-pulse 1.2s ease-in-out infinite;
}

.dot:nth-child(2) {
  animation-delay: 0.15s;
}

.dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes dot-pulse {
  0%, 80%, 100% {
    opacity: 0.3;
    transform: scale(0.8);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

/* ==========================================================================
   Query Form
   ========================================================================== */

.query-form {
  display: flex;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  border-top: 1px solid var(--color-accent-soft);
  background-color: var(--color-surface);
}

.query-input {
  flex: 1;
  font-family: 'DM Sans', sans-serif;
  font-size: var(--text-base);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-muted);
  border-radius: 4px;
  background-color: var(--color-surface);
  color: var(--color-ink);
  transition: border-color 0.15s ease;
}

.query-input::placeholder {
  color: var(--color-muted);
}

.query-input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px var(--color-accent-soft);
}

/* ==========================================================================
   Error Message
   ========================================================================== */

.error-message {
  padding: var(--space-sm) var(--space-lg);
  background-color: #fce8ea;
  color: var(--color-error);
  font-size: var(--text-sm);
  border-left: 3px solid var(--color-error);
}

.error-message[hidden] {
  display: none;
}

/* ==========================================================================
   Mobile Source Drawer
   ========================================================================== */

.drawer-toggle {
  display: none;
}

.source-drawer {
  display: none;
}

/* ==========================================================================
   Responsive — Mobile (< 768px)
   ========================================================================== */

@media (max-width: 767px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  /* Hide desktop sidebar on mobile */
  .sidebar {
    display: none;
  }

  /* Show drawer toggle */
  .drawer-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 80px;
    right: var(--space-md);
    z-index: 100;
    background-color: var(--color-source);
    color: var(--color-surface);
    font-family: 'DM Sans', sans-serif;
    font-size: var(--text-sm);
    font-weight: 500;
    padding: var(--space-sm) var(--space-md);
    border: none;
    border-radius: 20px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: background-color 0.15s ease;
  }

  .drawer-toggle[hidden] {
    display: none;
  }

  .drawer-toggle:hover {
    background-color: var(--color-accent-deep);
  }

  .drawer-toggle:focus-visible {
    outline: 2px solid var(--color-source);
    outline-offset: 2px;
  }

  /* Mobile source drawer — slides up from bottom */
  .source-drawer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 60vh;
    background-color: var(--color-surface);
    border-top: 2px solid var(--color-source);
    border-radius: 12px 12px 0 0;
    z-index: 200;
    overflow-y: auto;
    transform: translateY(100%);
    transition: transform 0.3s ease;
  }

  .source-drawer[hidden] {
    display: none;
  }

  .source-drawer.open {
    display: block;
    transform: translateY(0);
  }

  .drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-md);
    border-bottom: 1px solid var(--color-accent-soft);
    position: sticky;
    top: 0;
    background-color: var(--color-surface);
  }

  .drawer-close {
    background: none;
    border: none;
    font-size: var(--text-xl);
    color: var(--color-muted);
    cursor: pointer;
    padding: var(--space-xs);
    line-height: 1;
  }

  .drawer-close:hover {
    color: var(--color-ink);
  }

  .drawer-close:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }

  /* Tighten spacing on mobile */
  .chat-messages {
    padding: var(--space-md);
  }

  .query-form {
    padding: var(--space-sm) var(--space-md);
  }

  .top-bar {
    padding: var(--space-sm) var(--space-md);
  }

  .top-bar-title {
    font-size: var(--text-lg);
  }
}

/* ==========================================================================
   Reduced Motion — Accessibility
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .message {
    animation: fade-in 0.2s ease both;
  }

  .message-agent .answer-text {
    animation: fade-in 0.2s ease both;
  }

  .source-verified {
    animation: fade-in 0.2s ease both;
  }

  .source-chip {
    animation: fade-in 0.2s ease both;
  }

  .dot {
    animation: fade-in 0.2s ease both;
    opacity: 0.6;
  }

  .source-drawer {
    transition: none;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ==========================================================================
   Document Preview Slide-Over Panel
   ========================================================================== */

.doc-preview-overlay {
  position: fixed;
  inset: 0;
  background-color: rgba(26, 26, 46, 0.4);
  z-index: 300;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.doc-preview-overlay[hidden] {
  display: none;
}

.doc-preview-overlay.visible {
  opacity: 1;
}

.doc-preview {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(680px, 90vw);
  background-color: var(--color-surface);
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.15);
  z-index: 400;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.doc-preview[hidden] {
  display: none;
}

.doc-preview.open {
  transform: translateX(0);
}

.doc-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-accent-soft);
  background-color: var(--color-surface);
  flex-shrink: 0;
}

.doc-preview-title {
  font-family: 'JetBrains Mono', monospace;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-preview-close {
  background: none;
  border: none;
  font-size: var(--text-xl);
  color: var(--color-muted);
  cursor: pointer;
  padding: var(--space-xs) var(--space-sm);
  line-height: 1;
  border-radius: 4px;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.doc-preview-close:hover {
  color: var(--color-ink);
  background-color: var(--color-paper);
}

.doc-preview-close:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.doc-preview-frame {
  flex: 1;
  border: none;
  width: 100%;
}

/* Make source chips look clickable when they have URLs */
.source-chip[data-url] {
  cursor: pointer;
}

.source-chip[data-url]:hover {
  background-color: var(--color-source);
  color: var(--color-surface);
}

@media (prefers-reduced-motion: reduce) {
  .doc-preview {
    transition: none;
  }
  .doc-preview-overlay {
    transition: none;
  }
}
