:root {
  color-scheme: light;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --app-bg: #f4f4f4;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --surface-muted: #e9edf1;
  --surface-hover: #f4f6f7;
  --ink: #1d1d1f;
  --ink-soft: #3f3f46;
  --heading: #243042;
  --muted: #5f6672;
  --muted-quiet: #6f7785;
  --border: #cbd4dd;
  --border-soft: #dde4ea;
  --focus: #0f91a3;
  --focus-strong: #0f91a3;
  --focus-ring: rgba(34, 199, 220, .42);
  --focus-fill: rgba(15, 145, 163, .08);
  --link: #1556b7;
  --send-bg: #fff7e8;
  --send-border: #dccba8;
  --send-bg-hover: var(--surface-hover);
  --send-fg: var(--ink);
  --user-surface: #eef5ff;
  --user-border: #bfd4ff;
  --code-surface: #eef2f7;
  --success: #118544;
  --success-fill: rgba(17, 133, 68, .12);
  --danger: #b42318;
  --danger-fill: #fff2f0;
  --danger-border: #f2b8b2;
  --warning: #91620e;
  --warning-fill: #fff8e8;
  --warning-border: #ecd49b;
  --espresso: #704823;
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 16px;
  --shadow-control: 0 1px 2px rgba(15, 23, 42, .08);
  --shadow-popover: 0 14px 28px rgba(15, 23, 42, .14);
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --z-popover: 30;
  font-family: var(--font-sans);
  background: var(--app-bg);
  color: var(--ink);
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  background: var(--app-bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

main {
  box-sizing: border-box;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(100%, 1120px);
  height: 100dvh;
  margin: 0 auto;
  padding: 14px 20px 18px;
}

a {
  color: var(--link);
  text-underline-offset: 3px;
}

a:hover {
  color: var(--focus-strong);
}

h1 {
  margin: 0;
  color: var(--ink);
  font-size: 29px;
  font-weight: 760;
  letter-spacing: 0;
  line-height: 1.16;
  text-wrap: balance;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 650;
}

select {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 32px 7px 10px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}

textarea {
  box-sizing: border-box;
  width: 100%;
  min-height: 48px;
  resize: none;
  border: 0;
  border-radius: 0;
  padding: 14px 146px 14px 16px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  line-height: 1.48;
  outline: none;
  overflow-y: hidden;
}

textarea::placeholder {
  color: var(--muted);
}

textarea:focus {
  box-shadow: none;
}

button {
  flex: 0 0 auto;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 14px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
  box-shadow: var(--shadow-control);
  transition:
    transform .12s var(--ease-out),
    border-color .18s var(--ease-out),
    background-color .18s var(--ease-out),
    box-shadow .18s var(--ease-out),
    color .18s var(--ease-out);
}

button:hover {
  border-color: #c7ced6;
  background: var(--surface-hover);
}

button:active {
  transform: translateY(1px);
}

button:focus-visible,
textarea:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.composer-input-shell textarea:focus-visible {
  outline: none;
}

button:disabled {
  border-color: var(--border-soft);
  background: var(--surface-muted);
  color: #8a93a0;
  box-shadow: none;
  cursor: not-allowed;
}

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--focus-strong);
}

input[type="date"] {
  box-sizing: border-box;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}

input[type="date"]:focus {
  border-color: var(--focus);
  outline: none;
  box-shadow: 0 0 0 3px var(--focus-fill);
}

.visually-hidden,
.composer-label {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
  padding: 0;
}

.chat-shell.is-empty,
.chat-shell.is-read-only {
  grid-template-rows: auto minmax(0, 1fr);
}

.chat-shell.is-read-only .composer {
  display: none;
}

header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 66px;
  gap: 18px;
  border-bottom: 1px solid var(--border);
}

.header-controls {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-width: 0;
}

.language-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  line-height: 1.25;
  white-space: nowrap;
}

.derek-avatar {
  position: absolute;
  top: 65%;
  left: 50%;
  z-index: 1;
  box-sizing: border-box;
  width: 64px;
  height: 64px;
  border: 1px solid rgba(29, 29, 31, .12);
  border-radius: 50%;
  background: #c7c3bf;
  object-fit: cover;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 4px rgba(15, 23, 42, .12);
  pointer-events: none;
}

.status {
  color: var(--muted);
  font-size: 14px;
}

.readiness {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  border-radius: 999px;
  padding: 2px 12px;
  color: var(--ink-soft);
  line-height: 1.25;
}

.readiness::before {
  content: "";
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px var(--success-fill);
}

.readiness.error::before {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(180, 35, 24, .12);
}

.error {
  color: var(--danger);
}

.chat-log {
  display: grid;
  align-content: start;
  gap: 16px;
  min-height: 0;
  overflow-y: auto;
  padding: 22px 4px;
  scroll-behavior: smooth;
  scrollbar-gutter: stable;
}

.chat-shell.is-empty .chat-log {
  display: none;
}

.message {
  display: grid;
  gap: 12px;
  max-width: 900px;
  border: 1px solid #c8d1dc;
  border-radius: 12px;
  padding: 18px;
  background: var(--surface);
  color: var(--ink);
}

.message.user {
  justify-self: end;
  width: min(760px, 88%);
  border-color: #a9c2f5;
  background: var(--user-surface);
}

.message.assistant {
  justify-self: start;
  width: min(900px, 94%);
}

.message.failed {
  border-color: var(--danger-border);
  background: var(--danger-fill);
}

.message-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted-quiet);
  font-size: 13px;
  font-weight: 650;
}

.message-heading-actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.message-body {
  color: var(--ink);
  white-space: pre-wrap;
  line-height: 1.58;
  overflow-wrap: anywhere;
}

.share-action {
  min-height: 30px;
  border-color: var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 10px;
  color: var(--ink);
  font-size: 13px;
  box-shadow: none;
}

.markdown-answer {
  display: grid;
  gap: 12px;
  overflow-x: auto;
  white-space: normal;
  line-height: 1.6;
}

.markdown-answer > * {
  margin: 0;
}

.markdown-answer h3,
.markdown-answer h4,
.markdown-answer h5 {
  color: var(--heading);
  line-height: 1.28;
  text-wrap: balance;
}

.markdown-answer h3 {
  font-size: 19px;
}

.markdown-answer h4,
.markdown-answer h5 {
  font-size: 16px;
}

.markdown-answer ul,
.markdown-answer ol {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 24px;
}

.markdown-answer li {
  padding-left: 2px;
}

.markdown-answer blockquote {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  background: var(--surface-soft);
  color: var(--muted);
}

.markdown-answer code {
  border-radius: var(--radius-xs);
  padding: 1px 4px;
  background: var(--code-surface);
  color: #253044;
  font-family: var(--font-mono);
  font-size: .92em;
}

.markdown-answer pre {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  background: #f8fafc;
}

.markdown-answer pre code {
  display: block;
  padding: 0;
  background: transparent;
}

.markdown-answer hr {
  width: 100%;
  border: 0;
  border-top: 1px solid var(--border);
}

.markdown-answer .markdown-table-scroll {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.markdown-answer table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.markdown-answer th,
.markdown-answer td {
  border: 1px solid var(--border);
  padding: 9px 11px;
  text-align: left;
  vertical-align: top;
}

.markdown-answer th {
  background: var(--surface-muted);
  color: #334155;
  font-weight: 650;
}

.answer-callout {
  border: 1px solid var(--warning-border);
  border-radius: var(--radius-md);
  padding: 11px 13px;
  background: var(--warning-fill);
  color: var(--ink);
}

.answer-callout.warning {
  border-color: var(--danger-border);
  background: var(--danger-fill);
}

.answer-step {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr);
  gap: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-soft);
}

.answer-step:first-child {
  padding-top: 0;
  border-top: 0;
}

.answer-step-number {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--heading);
  color: #ffffff;
  font-size: 13px;
  font-weight: 650;
}

.answer-step-content {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.citation-reference {
  margin-left: 2px;
  font-size: .72em;
  line-height: 1;
}

.citation-reference a {
  border: 1px solid #c5d4fb;
  border-radius: 999px;
  padding: 0 5px;
  background: var(--user-surface);
  color: var(--link);
  text-decoration: none;
}

.citation-reference a:hover {
  border-color: #9bb8f0;
  background: #e6efff;
}

.source-anchor {
  position: relative;
  top: -8px;
}

.phase-status {
  display: none;
  align-items: center;
  gap: 8px;
  min-height: 20px;
  color: var(--muted);
  font-size: 14px;
}

.phase-status.is-active {
  display: inline-flex;
}

.coffee-bean {
  position: relative;
  width: 11px;
  height: 15px;
  border-radius: 50%;
  background: var(--espresso);
  transform: rotate(22deg);
  animation: bean-pulse .9s ease-in-out infinite;
}

.coffee-bean::after {
  content: "";
  position: absolute;
  top: 2px;
  bottom: 2px;
  left: 5px;
  width: 1px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .55);
}

@keyframes bean-pulse {
  0% {
    transform: rotate(22deg) scale(.94);
    opacity: .72;
  }

  50% {
    transform: rotate(22deg) scale(1.04);
    opacity: 1;
  }

  100% {
    transform: rotate(22deg) scale(.94);
    opacity: .72;
  }
}

.resource-list {
  display: grid;
  gap: 10px;
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}

.resource-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 11px 12px;
  background: var(--surface-soft);
}

.resource-meta {
  margin-top: 6px;
  color: var(--muted-quiet);
  font-size: 13px;
}

.resource-item a {
  color: var(--ink);
}

.resource-item a:hover {
  color: var(--focus-strong);
}

.composer {
  border-top: 1px solid var(--border-soft);
  padding-top: 14px;
  background: var(--app-bg);
}

.chat-shell.is-empty .composer {
  grid-row: 2;
  align-self: center;
  justify-self: center;
  box-sizing: border-box;
  width: min(800px, 100%);
  border-top: 0;
  padding-top: 0;
  padding-bottom: 11vh;
}

.chat-shell.is-empty .composer-input-shell {
  border-color: #bcc8d4;
  box-shadow: 0 10px 14px rgba(15, 23, 42, .1);
}

.chat-shell.is-empty .composer-filters {
  top: calc(100% + 12px);
  bottom: auto;
}

.chat-shell.is-empty .composer-filters--beside-toggle {
  top: 50%;
  right: calc(100% + 10px);
  bottom: auto;
  max-height: min(calc(100dvh - 24px), 520px);
  transform: translateY(-50%);
}

.chat-shell.is-empty .composer-status {
  min-height: 0;
}

.composer-row {
  display: grid;
  grid-template-columns: 1fr;
  align-items: end;
  gap: 10px;
  position: relative;
}

.composer-question {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
}

.composer-input-shell {
  position: relative;
  box-sizing: border-box;
  min-width: 0;
  border: 1px solid #c6d0da;
  border-radius: 16px;
  background: var(--surface);
  transition:
    border-color .18s var(--ease-out),
    box-shadow .18s var(--ease-out),
    background-color .18s var(--ease-out);
}

.composer-input-shell:focus-within {
  border-color: var(--focus);
  box-shadow: 0 0 0 4px var(--focus-fill);
}

.chat-shell.is-empty .composer-input-shell:has(textarea:focus-visible),
.composer-input-shell:has(textarea:focus-visible) {
  border-color: var(--focus);
  box-shadow:
    0 0 0 1px var(--focus),
    0 0 0 5px var(--focus-ring);
}

.composer-actions {
  position: absolute;
  right: 8px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.composer-status {
  min-height: 20px;
  margin-top: 8px;
}

.composer-disclaimer {
  display: none;
  margin-top: 10px;
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
  text-wrap: pretty;
}

.chat-shell.is-empty .composer-disclaimer {
  display: block;
}

.chat-shell.is-empty .composer:has(.filter-toggle[aria-expanded="true"]) .composer-disclaimer {
  visibility: hidden;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  min-width: 40px;
  height: 40px;
  min-height: 40px;
  border-radius: 50%;
  padding: 0;
}

.icon-button svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
  pointer-events: none;
}

.send-button:not(:disabled) {
  border-color: var(--send-border);
  background: var(--send-bg);
  color: var(--send-fg);
}

.send-button:hover:not(:disabled) {
  border-color: #c7ced6;
  background: var(--send-bg-hover);
  color: var(--ink);
}

.dictation-button.is-listening {
  border-color: var(--danger);
  background: var(--danger-fill);
  color: var(--danger);
}

.filter-menu {
  position: relative;
  display: flex;
  justify-content: flex-end;
}

.filter-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  min-width: 40px;
  padding: 0;
}

.filter-toggle[aria-expanded="true"] {
  border-color: var(--focus);
  background: var(--focus-fill);
  box-shadow: 0 0 0 3px var(--focus-fill);
}

.filter-toggle--ring {
  position: relative;
  border-radius: 12px;
  border-color: #bfd3d8;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(15, 23, 42, .06);
}

.filter-toggle--ring::before {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid transparent;
  border-radius: 8px;
  pointer-events: none;
  transition:
    border-color .18s var(--ease-out),
    box-shadow .18s var(--ease-out);
}

.filter-toggle--ring .vertical-dots {
  gap: 4px;
}

.filter-toggle--ring .vertical-dots span {
  width: 3px;
  height: 3px;
}

.filter-toggle--ring[aria-expanded="true"] {
  border-color: #4aaeba;
  background: var(--focus-fill);
  color: var(--focus-strong);
  box-shadow: none;
}

.filter-toggle--ring[aria-expanded="true"]::before {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px var(--focus-fill);
}

.vertical-dots {
  display: grid;
  justify-items: center;
  gap: 3px;
}

.vertical-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
}

.composer-filters {
  display: grid;
  gap: 10px;
  align-self: stretch;
  min-width: 0;
  margin: 0;
  position: absolute;
  bottom: calc(100% + 10px);
  right: 0;
  z-index: var(--z-popover);
  box-sizing: border-box;
  width: 280px;
  max-height: min(70vh, 520px);
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-popover);
}

.composer-filters[hidden],
.date-filter[hidden] {
  display: none;
}

.composer-filters legend {
  padding: 0 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.filter-label {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 26px;
  margin: 0;
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
}

.date-filter {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.composer-filters .language-control {
  display: grid;
  gap: 6px;
  color: var(--muted);
  white-space: normal;
}

.source-list {
  display: grid;
  gap: 7px;
  margin: -2px 0 2px 27px;
}

.source-group-label {
  margin: -2px 0 -2px 27px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.source-empty {
  color: var(--muted-quiet);
  font-size: 13px;
}

.composer-filters--sheet {
  width: 292px;
  gap: 7px;
  border-color: var(--border);
  border-radius: 12px;
  padding: 11px;
  box-shadow: 0 8px 8px rgba(15, 23, 42, .1);
}

.composer-filters--sheet > legend {
  float: left;
  width: 100%;
  margin: 0;
  border-bottom: 1px solid var(--border-soft);
  padding: 0 0 7px;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.2;
}

.composer-filters--sheet > legend + .filter-label {
  clear: both;
  margin-top: 2px;
}

.composer-filters--sheet .filter-label {
  min-height: 25px;
  gap: 8px;
  font-size: 14px;
}

.composer-filters--sheet .source-group-label {
  margin: 4px 0 -2px;
  border-top: 1px solid var(--border-soft);
  padding-top: 7px;
  font-size: 12px;
}

.composer-filters--sheet .source-list {
  gap: 4px;
  margin: 0 0 1px 24px;
}

.composer-filters--sheet .language-control {
  margin-top: 3px;
  border-top: 1px solid var(--border-soft);
  padding-top: 8px;
}

@media (max-width: 720px) {
  main {
    padding: 14px 12px;
  }

  header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 6px 12px;
    min-height: 52px;
    padding-bottom: 14px;
  }

  h1 {
    grid-column: 1;
    margin: 0;
    font-size: 22px;
  }

  select {
    max-width: 100%;
  }

  .derek-avatar {
    position: static;
    grid-column: 2;
    grid-row: 1 / span 2;
    width: 64px;
    height: 64px;
    transform: none;
  }

  .header-controls {
    grid-column: 1;
    grid-row: 2;
    display: grid;
    justify-content: start;
    gap: 6px;
  }

  .readiness {
    max-width: 100%;
    padding-inline: 9px;
    font-size: 13px;
  }

  .language-control {
    font-size: 12px;
  }

  .chat-log {
    gap: 12px;
    padding: 16px 0;
  }

  .message,
  .message.user,
  .message.assistant {
    width: auto;
    max-width: none;
    border-radius: var(--radius-md);
    padding: 14px;
  }

  .message-heading {
    align-items: start;
    flex-wrap: wrap;
  }

  .composer-row,
  .composer-question {
    grid-template-columns: 1fr;
  }

  .composer-input-shell {
    border-radius: var(--radius-lg);
  }

  textarea {
    min-height: 50px;
    padding: 14px 158px 14px 14px;
  }

  .composer-actions {
    right: 6px;
    bottom: 6px;
  }

  .icon-button,
  .filter-toggle,
  .filter-menu .filter-toggle {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
  }

  .composer-filters {
    width: min(320px, calc(100vw - 24px));
    max-height: min(68vh, 520px);
  }

  .share-action {
    width: auto;
    min-height: 34px;
  }
}

@media (max-width: 380px) {
  textarea {
    padding-right: 14px;
    padding-bottom: 58px;
  }

  .composer-actions {
    left: 8px;
    right: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }
}
