:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-soft: #f9fafb;
  --surface-softer: #e5e7eb;
  --border-subtle: #e5e7eb;
  --accent: #111827;
  --accent-soft: #111827;
  --accent-contrast: #ffffff;
  --text-main: #111827;
  --text-subtle: #6b7280;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --shadow-soft: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shell-frame: 0px;
  --shell-max: 1140px;
  --chat-max: 1040px;
  --composer-max: 860px;
  --vvh: 100dvh;
  --transition-fast: 150ms ease-out;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text-main);
  background: var(--bg);
}

body {
  min-height: var(--vvh);
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding: var(--shell-frame);
  overflow-x: hidden;
  overflow-y: auto;
}

#app-shell {
  width: 100%;
  height: calc(var(--vvh) - (var(--shell-frame) * 2));
  min-height: calc(var(--vvh) - (var(--shell-frame) * 2));
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--surface);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-mark {
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-contrast);
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-name {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-transform: uppercase;
}

.brand-subtitle {
  display: none;
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-left: auto;
  min-width: 0;
}

.user-pill {
  font-size: 12px;
  color: var(--text-main);
  background: var(--surface-soft);
  border-radius: 999px;
  padding: 6px 10px;
  border: 1px solid var(--border-subtle);
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: none;
}

.btn {
  border: none;
  border-radius: 999px;
  min-height: 40px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    transform 80ms ease-out,
    box-shadow var(--transition-fast),
    border-color var(--transition-fast);
  outline: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 6px 12px rgba(15, 23, 42, 0.25);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(15, 23, 42, 0.35);
}

.btn-ghost {
  background: var(--surface);
  color: var(--accent);
  border-radius: 999px;
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--border-subtle);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-soft);
}

.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.btn-icon:hover:not(:disabled) {
  background: var(--surface-soft);
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

main.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface-soft);
  color: var(--text-main);
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  overflow: hidden;
  min-height: 0;
}

.view {
  display: none;
  min-height: 0;
  height: 100%;
}

.view.active {
  display: flex;
  flex: 1;
}

/* AUTH VIEW */
#auth-view {
  justify-content: flex-start;
  align-items: stretch;
  overflow-y: auto;
}

.auth-card {
  width: 100%;
  max-width: 100%;
  background: var(--surface);
  border-radius: 14px;
  border: 1px solid var(--border-subtle);
  padding: 14px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-header {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.auth-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-main);
}

.auth-subtitle {
  font-size: 13px;
  color: var(--text-subtle);
}

.auth-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 6px;
}

.field-label {
  font-size: 12px;
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.field-help {
  font-size: 11px;
  color: var(--text-subtle);
  line-height: 1.3;
}

.field-input-wrapper {
  position: relative;
}

.field-input {
  width: 100%;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  padding: 10px 12px;
  font-size: 16px;
  background: var(--surface-soft);
  color: var(--text-main);
  outline: none;
  transition:
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    background var(--transition-fast),
    transform 80ms ease-out;
}

.field-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(15, 23, 42, 0.2);
  background: var(--surface);
  transform: translateY(-0.5px);
}

.field-error {
  font-size: 11px;
  color: #b91c1c;
  min-height: 14px;
}

.auth-footer {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  margin-top: 8px;
}

.auth-footer .btn {
  width: 100%;
}

.auth-hint {
  font-size: 11px;
  color: var(--text-subtle);
  line-height: 1.35;
}

.auth-step {
  display: none;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid var(--border-subtle);
  background: var(--surface);
  animation: auth-step-in 220ms ease-out;
}

.auth-step.active {
  display: block;
}

@keyframes auth-step-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.auth-stepper {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-soft);
}

.auth-stepper-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-main);
}

.auth-stepper-sub {
  font-size: 11px;
  color: var(--text-subtle);
}

.auth-stepper .btn {
  width: 100%;
  justify-content: center;
}

.auth-inline-banner {
  border-radius: 14px;
  border: 1px solid var(--border-subtle);
  padding: 10px 12px;
  background: var(--surface-soft);
  font-size: 12px;
  color: var(--text-main);
}

.auth-inline-banner.success {
  border-color: #4ade80;
  background: var(--success-soft);
}

.auth-inline-banner.info {
  border-color: #bae6fd;
  background: #e0f2fe;
}

/* LISTINGS VIEW */
#listings-view {
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.section-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  margin-bottom: 2px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
}

.section-subtitle {
  font-size: 12px;
  color: var(--text-subtle);
}

.listings-empty {
  padding-top: 18px;
  flex: 1;
}

.listings-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  padding-right: 2px;
  overflow-y: auto;
  min-height: 0;
  align-content: start;
  align-items: start;
}

.listings-cta {
  position: sticky;
  bottom: 0;
  z-index: 6;
  padding-top: 8px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(249, 250, 251, 0) 0%, rgba(249, 250, 251, 1) 22px);
}

.listings-cta .btn {
  width: 100%;
}

.listing-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  transition:
    transform 120ms ease-out,
    box-shadow var(--transition-fast),
    border-color var(--transition-fast),
    background 160ms ease-out;
}

.listing-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.12);
  border-color: var(--accent);
}

.listing-head {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 10px;
}

.listing-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-main);
  white-space: normal;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.listing-meta {
  font-size: 12px;
  color: var(--text-subtle);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.listing-detail {
  font-size: 12px;
  color: var(--text-subtle);
  margin-top: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.listing-create-card {
  display: none;
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 14px;
  text-align: center;
  border: 1px solid var(--border-subtle);
  background: var(--surface);
  color: var(--text-main);
  box-shadow: none;
}

.listing-create-card:hover {
  border-color: #bcc5d6;
  background: var(--surface);
}

.create-listing-icon {
  width: 72px;
  height: 72px;
  border-radius: 999px;
  background: #e8ecf8;
  color: #4e63df;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  line-height: 1;
}

.create-listing-label {
  font-size: 18px;
  font-weight: 500;
}

.meatball-btn {
  border: none;
  background: rgba(255, 255, 255, 0.55);
  color: #4c5677;
  font-size: 18px;
  padding: 0;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast), transform 80ms ease-out;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.meatball-btn:hover {
  background: rgba(255, 255, 255, 0.88);
  color: var(--accent);
  transform: translateY(-1px);
}

.menu-dropdown {
  position: absolute;
  top: 8px;
  right: 8px;
  min-width: 140px;
  background: var(--surface);
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  box-shadow: 0 16px 32px rgba(15, 23, 42, 0.15);
  padding: 4px;
  z-index: 20;
  animation: fade-in-scale 120ms ease-out;
}

.menu-item {
  border: none;
  width: 100%;
  background: transparent;
  color: var(--text-main);
  font-size: 12px;
  padding: 6px 8px;
  border-radius: 6px;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
}

.menu-item:hover {
  background: var(--surface-soft);
}

.menu-item.danger {
  color: var(--danger);
}

/* MODALS */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 10px;
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease-out;
}

.backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  width: 100%;
  max-width: 520px;
  max-height: calc(var(--vvh) - 20px);
  background: var(--surface);
  border-radius: 14px 14px 10px 10px;
  padding: 14px 12px 12px;
  border: 1px solid var(--border-subtle);
  box-shadow: 0 18px 40px rgba(15, 23, 42, 0.15);
  transform: translateY(12px) scale(0.98);
  opacity: 0;
  overflow-y: auto;
  transition: transform 150ms ease-out, opacity 150ms ease-out;
}

.backdrop.active .modal {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
}

.modal-subtitle {
  font-size: 12px;
  color: var(--text-subtle);
  margin-bottom: 10px;
}

#listing-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 2px 10px;
}

#listing-form .field {
  margin-bottom: 0;
}

.modal-actions {
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
}

.modal-actions .btn {
  width: 100%;
}

/* CHAT VIEW */
#chat-view {
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  height: 100%;
}

.chat-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  margin-bottom: 0;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-header-left .btn-icon {
  width: 38px;
  height: 38px;
  font-size: 18px;
  flex: 0 0 auto;
}

.chat-listing-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.chat-listing-title {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.2;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-listing-sub {
  font-size: 13px;
  line-height: 1.25;
  color: var(--text-subtle);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  align-self: start;
  justify-self: end;
}

#chat-listing-badge {
  display: none;
}

.chat-header-right .btn {
  min-height: 38px;
  padding: 7px 16px;
  font-size: 15px;
}

.pill-secondary {
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-soft);
  border: 1px solid var(--border-subtle);
  font-size: 11px;
  color: var(--text-subtle);
}

.chat-layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  overflow: hidden;
}

.chat-panel {
  flex: 1;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  width: 100%;
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 8px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  min-height: 0;
}

.chat-empty {
  font-size: 13px;
  color: var(--text-subtle);
  text-align: center;
  padding: 40px 10px;
}

.chat-message-row {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 10px;
  animation: fade-in-up 160ms ease-out;
  will-change: transform, opacity;
}

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

.chat-message-row.assistant {
  align-items: stretch;
}

.message-bubble {
  max-width: 94%;
  border-radius: 16px;
  padding: 9px 11px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message-bubble.user {
  background: var(--accent);
  color: var(--accent-contrast);
}

.message-bubble.assistant {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 0;
  max-width: 100%;
  color: var(--text-main);
  white-space: normal;
}

.message-bubble.assistant .msg-text {
  white-space: normal;
}

.message-bubble.assistant .msg-text p,
.message-bubble.assistant .msg-text ul,
.message-bubble.assistant .msg-text ol,
.message-bubble.assistant .msg-text blockquote,
.message-bubble.assistant .msg-text .md-table-wrap {
  margin: 0 0 10px;
}

.message-bubble.assistant .msg-text p:last-child,
.message-bubble.assistant .msg-text ul:last-child,
.message-bubble.assistant .msg-text ol:last-child,
.message-bubble.assistant .msg-text blockquote:last-child,
.message-bubble.assistant .msg-text .md-table-wrap:last-child {
  margin-bottom: 0;
}

.message-bubble.assistant .msg-text ul,
.message-bubble.assistant .msg-text ol {
  padding-left: 18px;
}

.message-bubble.assistant .msg-text li + li {
  margin-top: 4px;
}

.message-bubble.assistant .msg-text h1,
.message-bubble.assistant .msg-text h2,
.message-bubble.assistant .msg-text h3,
.message-bubble.assistant .msg-text h4 {
  margin: 0 0 8px;
  line-height: 1.35;
  font-weight: 650;
}

.message-bubble.assistant .msg-text h1 {
  font-size: 1.15em;
}

.message-bubble.assistant .msg-text h2 {
  font-size: 1.08em;
}

.message-bubble.assistant .msg-text h3,
.message-bubble.assistant .msg-text h4 {
  font-size: 1em;
}

.message-bubble.assistant .msg-text code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
  padding: 1px 4px;
  border-radius: 5px;
  background: var(--surface-soft);
  border: 1px solid var(--border-subtle);
}

.message-bubble.assistant .msg-text blockquote {
  padding: 8px 10px;
  border-left: 3px solid var(--border-subtle);
  background: var(--surface-soft);
  border-radius: 8px;
  color: var(--text-subtle);
}

.message-bubble.assistant .msg-text .md-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  background: var(--surface);
}

.message-bubble.assistant .msg-text .md-table {
  width: 100%;
  min-width: 360px;
  border-collapse: collapse;
}

.message-bubble.assistant .msg-text .md-table th,
.message-bubble.assistant .msg-text .md-table td {
  padding: 7px 8px;
  font-size: 0.95em;
  line-height: 1.35;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border-subtle);
  border-right: 1px solid var(--border-subtle);
}

.message-bubble.assistant .msg-text .md-table th:last-child,
.message-bubble.assistant .msg-text .md-table td:last-child {
  border-right: none;
}

.message-bubble.assistant .msg-text .md-table tr:last-child td {
  border-bottom: none;
}

.message-bubble.assistant .msg-text .md-table th {
  font-weight: 650;
  background: var(--surface-soft);
}

.message-meta {
  font-size: 11px;
  color: var(--text-subtle);
}

.typing-indicator {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 4px 2px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: rgba(17, 24, 39, 0.35);
  animation: typing-bounce 900ms ease-in-out infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 140ms;
}

.typing-dot:nth-child(3) {
  animation-delay: 280ms;
}

@keyframes typing-bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* Citation chips */
.cite {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 18px;
  min-width: 18px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 11px;
  line-height: 1;
  border: 1px solid var(--border-subtle);
  background: var(--surface-soft);
  color: var(--accent);
  cursor: default;
  user-select: none;
  vertical-align: baseline;
  margin-left: 2px;
  transition: transform 80ms ease-out, background var(--transition-fast), border-color var(--transition-fast);
}

.cite:hover {
  background: var(--surface-soft);
  border-color: var(--border-subtle);
  transform: none;
}

@media (hover: hover) {
  .cite:hover {
    background: var(--surface);
    border-color: var(--accent);
    transform: translateY(-0.5px);
  }
}

.cite:focus-visible {
  outline: 2px solid rgba(17, 24, 39, 0.35);
  outline-offset: 1px;
}

.cite.active {
  background: var(--surface);
  border-color: var(--accent);
}

/* Citation popover */
.cite-popover {
  position: fixed;
  z-index: 80;
  width: min(440px, calc(100vw - 16px));
  max-height: min(72vh, 500px);
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.18);
  padding: 10px 10px 9px;
  animation: fade-in-scale 120ms ease-out;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
}

.cite-popover .title {
  font-size: 12px;
  font-weight: 650;
  color: var(--text-main);
  margin-bottom: 2px;
  white-space: normal;
  overflow-wrap: anywhere;
  line-height: 1.35;
}

.cite-popover .meta {
  font-size: 11px;
  color: var(--text-subtle);
  margin-bottom: 7px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  overflow-wrap: anywhere;
}

.cite-popover .claim-line {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 8px;
  padding: 7px 8px;
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  background: var(--surface-soft);
}

.cite-popover .claim-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-subtle);
}

.cite-popover .claim-text {
  font-size: 11px;
  line-height: 1.4;
  color: var(--text-main);
  overflow-wrap: anywhere;
}

.cite-popover .snippet-stack {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.cite-popover .snippet {
  font-size: 12px;
  color: var(--text-main);
  line-height: 1.45;
  background: var(--surface-soft);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 8px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.cite-popover .snippet-focus {
  border-color: rgba(15, 23, 42, 0.24);
}

.cite-popover .cite-highlight {
  background: #fef3c7;
  color: inherit;
  padding: 0 1px;
  border-radius: 2px;
}

.cite-popover .snippet-context {
  font-size: 11px;
  color: var(--text-subtle);
  line-height: 1.4;
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 7px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.cite-popover .snippet-context-group {
  display: flex;
  flex-direction: column;
}

.cite-popover .snippet-context-toggle {
  width: 100%;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.cite-popover .snippet-context-toggle strong {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-main);
}

.cite-popover .snippet-context-toggle::after {
  content: "▸";
  font-size: 12px;
  color: var(--text-subtle);
  transform: rotate(0deg);
  transition: transform var(--transition-fast);
}

.cite-popover .snippet-context-toggle[aria-expanded="true"] {
  border-color: rgba(15, 23, 42, 0.24);
}

.cite-popover .snippet-context-toggle[aria-expanded="true"]::after {
  transform: rotate(90deg);
}

.cite-popover .snippet-context-toggle:disabled {
  cursor: default;
  opacity: 0.75;
}

.cite-popover .snippet-context-toggle:disabled::after {
  opacity: 0.45;
}

.cite-popover .snippet-context-content {
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  transform: translateY(-2px);
  transition:
    max-height 200ms ease-out,
    opacity 160ms ease-out,
    transform 160ms ease-out;
}

.cite-popover .snippet-context-content.is-open {
  opacity: 1;
  transform: translateY(0);
}

.cite-popover .snippet-context-content .snippet-context {
  margin-top: 6px;
}

.cite-popover .popover-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.cite-open-doc-btn {
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
}

.cite-open-doc-btn:hover:not(:disabled) {
  background: var(--surface-soft);
}

.cite-open-doc-btn:disabled {
  color: var(--text-subtle);
  cursor: default;
}

.chat-input-row {
  width: min(var(--composer-max), 100%);
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: static;
  background: transparent;
  border-top: none;
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
}

.chat-input-wrapper {
  position: relative;
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border-subtle);
  border-radius: 28px;
  background: var(--surface);
  box-shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
  min-height: 62px;
  padding: 10px 62px 10px 14px;
  overflow: hidden;
}

.chat-input-wrapper:focus-within {
  border-color: rgba(17, 24, 39, 0.28);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.12);
}

.chat-input-wrapper::after {
  content: "";
  position: absolute;
  left: 14px;
  right: 58px;
  bottom: 8px;
  height: 28px;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, var(--surface) 80%);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.chat-input-wrapper.is-overflowing::after {
  opacity: 1;
}

.chat-textarea {
  width: 100%;
  min-height: 40px;
  max-height: 210px;
  resize: none;
  border: none;
  border-radius: 0;
  padding: 8px 0;
  font-size: 16px;
  font-family: inherit;
  line-height: 1.35;
  letter-spacing: normal;
  background: transparent;
  color: var(--text-main);
  overflow-y: hidden;
  scrollbar-width: none;
  outline: none;
  -webkit-overflow-scrolling: touch;
  transition: color var(--transition-fast);
}

.chat-textarea::-webkit-scrollbar {
  width: 6px;
}

.chat-textarea::placeholder {
  font-size: 0.82em;
  line-height: 1.35;
}

.chat-textarea:focus {
  background: transparent;
}

.composer-send-btn {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-contrast);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.3);
  transition: transform 80ms ease-out, box-shadow var(--transition-fast), opacity var(--transition-fast);
}

.composer-send-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.composer-send-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 16px rgba(15, 23, 42, 0.35);
}

.composer-send-btn.is-loading {
  opacity: 0.65;
}

.composer-send-btn:disabled {
  cursor: default;
  transform: none;
}

.chat-input-hint {
  font-size: 11px;
  color: var(--text-subtle);
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 3px;
  line-height: 1.35;
  text-align: center;
}

#chat-status:empty {
  display: none;
}

/* SOURCES DRAWER */
.sources-drawer {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: 100%;
  background: var(--surface);
  box-shadow: -16px 0 40px rgba(15, 23, 42, 0.12);
  transform: translateX(100%);
  transition: transform 180ms ease-out;
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.sources-drawer.active {
  transform: translateX(0);
}

.sources-header {
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sources-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-main);
}

.sources-subtitle {
  font-size: 11px;
  color: var(--text-subtle);
}

.sources-documents-label {
  margin-top: 6px;
}

.sources-body {
  flex: 1;
  padding: 10px 12px max(12px, env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.upload-zone {
  border-radius: 16px;
  border: 1px dashed #d1d5db;
  padding: 12px;
  text-align: center;
  background: var(--surface-soft);
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast), transform 80ms ease-out;
}

.upload-zone.dragover {
  border-style: solid;
  border-color: var(--accent);
  background: var(--surface);
  transform: translateY(-1px);
}

.upload-title {
  font-size: 13px;
  color: var(--text-main);
}

.upload-subtitle {
  font-size: 11px;
  color: var(--text-subtle);
  margin-top: 4px;
}

.sources-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sources-empty-state {
  padding: 16px 4px;
  font-size: 12px;
}

.source-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  background: var(--surface-soft);
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  padding: 6px 8px;
  font-size: 12px;
}

.source-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.source-name {
  color: var(--text-main);
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.source-meta {
  color: var(--text-subtle);
  font-size: 11px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.source-status-badge {
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  border: 1px solid;
  flex-shrink: 0;
}

.status-pending {
  color: #d97706;
  border-color: #fbbf24;
  background: #fef3c7;
}

.status-processing {
  color: #ea580c;
  border-color: #fdba74;
  background: #ffedd5;
}

.status-ready {
  color: var(--success);
  border-color: #4ade80;
  background: var(--success-soft);
}

.status-error {
  color: #b91c1c;
  border-color: #fecaca;
  background: #fee2e2;
}

.source-status-badge.uploading {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.source-status-spinner {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  border: 2px solid rgba(148, 163, 184, 0.6);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}

/* TOASTS */
.toast-host {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 60;
}

.toast {
  width: 100%;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border-subtle);
  padding: 8px 10px;
  display: flex;
  gap: 8px;
  animation: fade-in-slide-right 160ms ease-out;
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.1);
  font-size: 12px;
  color: var(--text-main);
}

.toast-success {
  border-color: #4ade80;
}

.toast-error {
  border-color: #fecaca;
}

.toast-info {
  border-color: #bae6fd;
}

.toast-icon {
  font-size: 14px;
  margin-top: 1px;
}

.toast-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toast-title {
  font-weight: 600;
  font-size: 12px;
}

.toast-message {
  font-size: 11px;
  color: var(--text-subtle);
}

.toast-close {
  border: none;
  background: transparent;
  color: var(--text-subtle);
  font-size: 14px;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

@keyframes fade-in-up {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fade-in-scale {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes fade-in-slide-right {
  from {
    opacity: 0;
    transform: translateX(8px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

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

@media (min-width: 640px) {
  :root {
    --shell-frame: 10px;
  }

  body {
    overflow: hidden;
  }

  #app-shell {
    max-height: calc(var(--vvh) - (var(--shell-frame) * 2));
    border-radius: 20px;
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-soft);
  }

  header.app-header {
    align-items: center;
    padding: 12px 14px;
  }

  .brand-mark {
    width: 26px;
    height: 26px;
    font-size: 15px;
  }

  .user-pill {
    display: inline-flex;
    align-items: center;
  }

  .btn,
  .btn-ghost {
    min-height: 34px;
  }

  .btn-icon {
    width: 32px;
    height: 32px;
  }

  main.app-main {
    padding: 10px;
  }

  #auth-view {
    align-items: center;
    justify-content: center;
  }

  .auth-card {
    max-width: 560px;
    padding: 18px 16px 14px;
  }

  .field-label {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
  }

  .field-help {
    text-align: right;
  }

  .field-input {
    font-size: 13px;
    padding: 9px 12px;
  }

  .auth-step {
    padding: 12px;
  }

  .auth-stepper {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .auth-stepper .btn {
    width: auto;
  }

  .auth-footer {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .auth-footer .btn {
    width: auto;
  }

  .section-header {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .listings-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: 190px;
    align-content: start;
  }

  .listings-cta {
    position: static;
    padding-top: 0;
    padding-bottom: 0;
    background: transparent;
    display: flex;
    justify-content: flex-end;
  }

  .listings-cta .btn {
    width: auto;
  }

  .listing-card {
    height: 100%;
    padding: 14px;
  }

  .listing-title {
    font-size: 16px;
    -webkit-line-clamp: 2;
  }

  .backdrop {
    align-items: center;
    padding: 12px;
  }

  .modal {
    border-radius: 18px;
    max-height: calc(var(--vvh) - 24px);
    padding: 16px 16px 14px;
  }

  #listing-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #listing-form .field:nth-of-type(1),
  #listing-form .modal-actions {
    grid-column: 1 / -1;
  }

  .modal-actions {
    flex-direction: row;
    justify-content: flex-end;
  }

  .modal-actions .btn {
    width: auto;
  }

  .chat-header {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
  }

  .chat-header-right {
    justify-self: end;
  }

  #chat-listing-badge {
    display: inline-flex;
  }

  .chat-input-row {
    padding: 10px;
  }

  .chat-textarea {
    font-size: 15px;
  }

  .chat-textarea::placeholder {
    font-size: 0.82em;
    line-height: 1.35;
  }

  .chat-input-hint {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  .message-bubble {
    max-width: 90%;
  }

  .sources-drawer {
    width: min(420px, 100%);
    border-left: 1px solid var(--border-subtle);
  }

  .source-row {
    align-items: center;
  }

  .toast-host {
    left: auto;
    right: 12px;
    width: min(320px, calc(100vw - 24px));
  }

  .cite-popover {
    width: min(460px, calc(100vw - 24px));
  }
}

@media (min-width: 900px) {
  :root {
    --shell-frame: 12px;
  }

  #app-shell {
    max-width: var(--shell-max);
    border-radius: 24px;
  }

  header.app-header {
    gap: 12px;
    padding: 14px 18px;
  }

  .brand-mark {
    width: 28px;
    height: 28px;
    font-size: 16px;
  }

  .brand-name {
    font-size: 14px;
  }

  .brand-subtitle {
    display: block;
    font-size: 11px;
    color: var(--text-subtle);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  main.app-main {
    padding: 12px;
  }

  #auth-view {
    overflow: hidden;
  }

  .auth-card {
    max-width: 580px;
    padding: 22px 20px 18px;
    box-shadow: var(--shadow-soft);
  }

  .auth-body {
    gap: 12px;
  }

  #listings-view {
    gap: 12px;
  }

  .listings-cta {
    display: none;
  }

  .listings-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    grid-auto-rows: 240px;
    align-content: start;
  }

  .listing-create-card {
    display: flex;
  }

  .listing-card {
    padding: 16px;
  }

  .listing-title {
    font-size: 24px;
    line-height: 1.25;
    -webkit-line-clamp: 2;
  }

  .listing-meta,
  .listing-detail {
    font-size: 15px;
  }

  #chat-view {
    gap: 10px;
  }

  .chat-layout {
    gap: 10px;
  }

  .chat-panel {
    width: min(var(--chat-max), 100%);
    margin-inline: auto;
  }

  .chat-messages {
    padding: 12px;
  }

  .message-bubble {
    max-width: 82%;
  }

  .chat-listing-title {
    font-size: 14px;
  }

  .sources-body {
    padding-bottom: 12px;
  }
}

@media (min-width: 1120px) {
  main.app-main {
    padding: 14px;
  }

  .listings-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
