:root {
  color-scheme: light dark;
  --bg: #FAFAFA;
  --surface: #FFFFFF;
  --border: #E4E4E7;
  --text: #18181B;
  --text-muted: #6B7280;
  --accent: #4F46E5;
  --accent-weak: #EEF2FF;
  --on-accent: #FFFFFF;
  --code-bg: #F4F4F5;
  --danger: #991B1B;
  --danger-border: #FECACA;
  --danger-bg: #FEF2F2;
  --overlay: rgba(24, 24, 27, 0.28);
  --radius: 10px;
  --shadow-popover: 0 1px 2px rgba(0, 0, 0, 0.04);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  background: var(--bg);
}

html[data-theme="light"] { color-scheme: light; }

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111113;
  --surface: #18181B;
  --border: #27272A;
  --text: #FAFAFA;
  --text-muted: #A1A1AA;
  --accent: #818CF8;
  --accent-weak: rgba(99, 102, 241, 0.16);
  --on-accent: #18181B;
  --code-bg: #27272A;
  --danger: #FCA5A5;
  --danger-border: rgba(248, 113, 113, 0.4);
  --danger-bg: rgba(127, 29, 29, 0.24);
  --overlay: rgba(0, 0, 0, 0.56);
  --shadow-popover: 0 1px 2px rgba(0, 0, 0, 0.28);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #111113;
    --surface: #18181B;
    --border: #27272A;
    --text: #FAFAFA;
    --text-muted: #A1A1AA;
    --accent: #818CF8;
    --accent-weak: rgba(99, 102, 241, 0.16);
    --on-accent: #18181B;
    --code-bg: #27272A;
    --danger: #FCA5A5;
    --danger-border: rgba(248, 113, 113, 0.4);
    --danger-bg: rgba(127, 29, 29, 0.24);
    --overlay: rgba(0, 0, 0, 0.56);
    --shadow-popover: 0 1px 2px rgba(0, 0, 0, 0.28);
  }
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

button,
input,
textarea,
select { font: inherit; }

button,
a,
input,
textarea,
select,
summary {
  transition: color 150ms ease, background-color 150ms ease, border-color 150ms ease, box-shadow 150ms ease, opacity 150ms ease;
}

button { cursor: pointer; }

a { color: var(--accent); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

input,
textarea,
select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  background: var(--surface);
}

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

input:hover,
textarea:hover,
select:hover { border-color: var(--text-muted); }

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  outline: 0;
  box-shadow: 0 0 0 2px var(--accent);
}

textarea { resize: vertical; }

button:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

[hidden] { display: none !important; }

.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;
}

/* App shell */

.app-main {
  min-width: 0;
  min-height: 100vh;
  margin-left: 260px;
}

.conversation-sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 20;
  display: flex;
  width: 260px;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

.sidebar-branding { padding: 24px 20px 20px; }

.sidebar-branding .brand,
.login-branding .brand {
  display: block;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  line-height: 1.25;
  text-decoration: none;
}

.sidebar-branding > span,
.login-branding > span {
  display: block;
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 12px;
}

.sidebar-content {
  min-height: 0;
  padding: 0 12px 20px;
  overflow-y: auto;
}

.new-conversation {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--on-accent);
  background: var(--accent);
  font-weight: 600;
  text-align: left;
}

.new-conversation:hover { opacity: 0.9; }

.sidebar-section { margin-top: 24px; }

.sidebar-section h2 {
  margin: 0 8px 8px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.sidebar-section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.sidebar-section-heading h2 { margin-bottom: 0; }

.sidebar-secondary {
  padding: 4px 8px;
  border: 0;
  border-radius: var(--radius);
  color: var(--text-muted);
  background: transparent;
  font-size: 13px;
}

.sidebar-secondary:hover {
  color: var(--accent);
  background: var(--accent-weak);
}

.conversation-list,
.case-list {
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.conversation-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  border-left: 2px solid transparent;
  border-radius: var(--radius);
}

.conversation-item:hover,
.conversation-item.is-active,
.case-list li:hover,
.case-list li.is-active,
.conversation-list > li:has(> a):hover {
  background: var(--accent-weak);
}

.conversation-item.is-active { border-left-color: var(--accent); }

.conversation-open {
  min-width: 0;
  padding: 8px 10px;
  overflow: hidden;
  border: 0;
  border-radius: var(--radius);
  color: var(--text);
  background: transparent;
  font-weight: 400;
  text-align: left;
}

.conversation-item.is-active .conversation-title { color: var(--accent); }

.conversation-title,
.conversation-date {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-title { font-size: 14px; }

.conversation-date {
  display: none;
}

.conversation-delete {
  width: 28px;
  height: 28px;
  margin-right: 4px;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  color: var(--text-muted);
  background: transparent;
  font-size: 18px;
  line-height: 1;
  opacity: 0;
}

.conversation-item:hover .conversation-delete,
.conversation-delete:focus-visible { opacity: 1; }

.conversation-delete:hover {
  color: var(--danger);
  background: var(--danger-bg);
}

.case-list a,
.conversation-list > li > a {
  display: block;
  padding: 8px 10px;
  overflow: hidden;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.case-list li.is-active a { color: var(--accent); }

.conversation-list-empty {
  margin: 8px 10px;
  color: var(--text-muted);
  font-size: 13px;
}

.tool-links { display: grid; gap: 2px; }

.tool-links a {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  text-decoration: none;
}

.tool-links a:hover,
.tool-links a[aria-current="page"] {
  color: var(--accent);
  background: var(--accent-weak);
}

.tool-icon {
  width: 16px;
  color: var(--text-muted);
  text-align: center;
}

.sidebar-footer {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.theme-toggle {
  display: flex;
  width: 100%;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  background: transparent;
  font-size: 12px;
  text-align: left;
}

.theme-toggle:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-weak);
}

.theme-icon {
  width: 16px;
  color: var(--accent);
  font-size: 14px;
  line-height: 1;
  text-align: center;
}

.user-row {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
}

.user-name {
  overflow: hidden;
  font-size: 13px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-row form { margin: 0; }

.logout-button {
  padding: 3px 0;
  border: 0;
  color: var(--text-muted);
  background: transparent;
  font-size: 12px;
}

.logout-button:hover { color: var(--accent); }

.sidebar-footer p {
  margin: 10px 0 0;
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.4;
}

.sidebar-toggle,
.sidebar-backdrop { display: none; }

/* Chat */

.chat-shell {
  display: flex;
  height: 100vh;
  height: 100dvh;
  min-height: 520px;
  flex-direction: column;
  overflow: hidden;
}

.messages {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 28px;
  overflow-y: auto;
  padding: 48px max(24px, calc((100% - 720px) / 2)) 24px;
  scroll-behavior: smooth;
}

.empty-state {
  display: grid;
  width: 100%;
  margin: auto;
  gap: 24px;
}

.empty-state h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
}

.starter-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.starter-prompt {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  background: var(--surface);
  text-align: left;
}

.starter-prompt:hover {
  border-color: var(--accent);
  background: var(--accent-weak);
}

.starter-title,
.starter-description { display: block; }

.starter-title {
  color: var(--accent);
  font-weight: 600;
}

.starter-description {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 13px;
  white-space: nowrap;
}

.message { width: 100%; }

.message-user {
  width: fit-content;
  max-width: 85%;
  align-self: flex-end;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--accent-weak);
}

.message-assistant {
  align-self: stretch;
  color: var(--text);
  background: transparent;
}

.message-label {
  display: flex;
  gap: 7px;
  align-items: center;
  margin-bottom: 8px;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}

.message-user .message-label { display: none; }

.message-body { line-height: 1.65; }

.message-body > :first-child { margin-top: 0; }
.message-body > :last-child { margin-bottom: 0; }

.message-body h1,
.message-body h2,
.message-body h3 {
  margin: 24px 0 8px;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.4;
}

.message-body h3 { font-size: 15px; }

.message-body p,
.message-body ul,
.message-body ol { margin: 0 0 12px; }

.message-body pre {
  max-width: 100%;
  padding: 12px;
  overflow-x: auto;
  border-radius: var(--radius);
  background: var(--code-bg);
}

.message-body code {
  padding: 2px 4px;
  border-radius: var(--radius);
  background: var(--code-bg);
  font-size: 13px;
}

.message-body pre code { padding: 0; background: transparent; }

.message-body table,
.prose table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}

.message-body th,
.message-body td,
.prose th,
.prose td {
  padding: 7px 9px;
  border: 1px solid var(--border);
  background: transparent;
  text-align: left;
  vertical-align: top;
}

.message-body th,
.prose th { font-weight: 600; }

.stream-pulse {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: stream-pulse 1.2s ease-in-out infinite;
}

@keyframes stream-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1); }
}

.message-notice {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  background: var(--surface);
}

.notice-error,
.error {
  border-color: var(--danger-border);
  color: var(--danger);
  background: var(--danger-bg);
}

.composer-dock {
  width: min(100% - 48px, 720px);
  margin: 0 auto;
  padding: 8px 0 24px;
  background: var(--bg);
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.composer textarea {
  max-height: 144px;
  min-height: 40px;
  padding: 9px 8px;
  overflow-y: auto;
  border: 0;
  border-radius: 0;
  background: transparent;
  line-height: 20px;
  resize: none;
}

.composer textarea:hover,
.composer textarea:focus {
  border: 0;
  outline: 0;
  box-shadow: none;
}

.composer button {
  display: grid;
  width: 36px;
  height: 36px;
  padding: 0;
  place-items: center;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--on-accent);
  background: var(--accent);
  font-size: 18px;
  font-weight: 600;
}

.composer button:hover { opacity: 0.9; }

.case-selector {
  display: flex;
  gap: 6px;
  align-items: center;
  width: max-content;
  max-width: 100%;
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 13px;
}

.case-selector label { font-weight: 400; }

.case-selector select {
  width: auto;
  max-width: 260px;
  padding: 3px 24px 3px 4px;
  border: 0;
  color: var(--text-muted);
  background-color: transparent;
  font-size: 13px;
}

/* Tool and case pages */

.page-content {
  width: min(100% - 48px, 720px);
  margin: 0 auto;
  padding: 56px 0 80px;
}

.page-header { margin-bottom: 32px; }

.page-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.35;
}

.page-header p {
  margin: 6px 0 0;
  color: var(--text-muted);
  font-size: 13px;
}

.tool-form,
.case-editor form,
.login-panel form {
  display: grid;
  gap: 12px;
}

.tool-form { gap: 0; }

.tool-form > label,
.case-editor form > label,
.login-panel label {
  font-size: 13px;
  font-weight: 600;
}

.tool-form > label { margin-top: 24px; }

fieldset {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 24px 0;
  border: 0;
  border-bottom: 1px solid var(--border);
}

legend {
  padding: 0;
  font-size: 16px;
  font-weight: 600;
}

fieldset label { font-size: 13px; font-weight: 600; }

.choice-row,
.choice-stack {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 20px;
}

.choice-stack { flex-direction: column; }

.choice-row label,
.choice-stack label,
.checkbox-label {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-weight: 400;
}

.choice-row input,
.choice-stack input,
.checkbox-label input {
  width: auto;
  margin-top: 3px;
  accent-color: var(--accent);
}

.tool-form > button,
.form-actions button,
.login-panel button[type="submit"] {
  width: fit-content;
  margin-top: 24px;
  padding: 9px 14px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--on-accent);
  background: var(--accent);
  font-weight: 600;
}

.tool-form > button:hover,
.form-actions button:hover,
.login-panel button[type="submit"]:hover { opacity: 0.9; }

.optional,
.field-help {
  color: var(--text-muted);
  font-weight: 400;
}

.field-help {
  margin: -6px 0 2px;
  font-size: 13px;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.form-actions button { margin-top: 8px; }

.form-actions button.danger {
  border-color: var(--danger);
  color: var(--danger);
  background: transparent;
}

.form-actions button.danger:hover { background: var(--danger-bg); }

.save-status {
  min-height: 21px;
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.case-editor > h2 {
  margin: 48px 0 8px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
  font-size: 16px;
  font-weight: 600;
}

.case-editor > h2 + p { color: var(--text-muted); }

.linked-conversations { padding-left: 20px; }
.linked-conversations a { color: var(--accent); }

.claim-browser #claim-search { margin-bottom: 16px; }

.jump-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 0;
}

.jump-label {
  margin-right: 4px;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}

.jump-links a,
.claim-direct-links a {
  display: inline-flex;
  min-width: 32px;
  min-height: 32px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  background: var(--surface);
  font-size: 13px;
  text-decoration: none;
}

.jump-links a:hover,
.claim-direct-links a:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-weak);
}

.claim-index { margin: 12px 0 32px; color: var(--text-muted); font-size: 13px; }
.claim-index summary { width: fit-content; cursor: pointer; }
.claim-index summary:hover { color: var(--accent); }

.claim-direct-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.claim-direct-links a {
  min-width: 40px;
  min-height: 28px;
}

.prose { line-height: 1.65; }

.prose h1,
.prose h2,
.prose h3 {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.4;
  scroll-margin-top: 24px;
}

.prose h1 { margin: 40px 0 12px; }
.prose h2 { margin: 40px 0 12px; }
.prose h3 { margin: 0 0 12px; font-size: 15px; }

.claim-entry {
  margin-top: 28px;
  padding-top: 28px;
  border-top: 1px solid var(--border);
}

.claim-entry[hidden] { display: none; }

/* Login */

.login-main {
  display: grid;
  min-height: 100vh;
  padding: 32px 20px;
  place-items: center;
}

.login-wrap { width: min(100%, 400px); }

.login-panel {
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.login-branding {
  margin-bottom: 32px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.login-panel h1 {
  margin: 0 0 24px;
  font-size: 20px;
  font-weight: 600;
}

.login-panel button[type="submit"] { width: 100%; }

.error {
  margin: 0 0 16px;
  padding: 10px 12px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius);
}

.login-legal {
  margin: 12px 8px 0;
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.5;
  text-align: center;
}

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

@media (max-width: 899px) {
  .app-main { margin-left: 0; padding-top: 52px; }

  .conversation-sidebar {
    width: min(300px, calc(100vw - 48px));
    transform: translateX(-100%);
    transition: transform 150ms ease;
  }

  body.sidebar-open { overflow: hidden; }
  body.sidebar-open .conversation-sidebar { transform: translateX(0); }

  .sidebar-toggle {
    position: fixed;
    top: 10px;
    left: 12px;
    z-index: 30;
    display: grid;
    width: 36px;
    height: 36px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-popover);
    place-content: center;
    gap: 4px;
  }

  .sidebar-toggle span {
    display: block;
    width: 16px;
    height: 1px;
    background: var(--text);
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: block;
    background: var(--overlay);
  }

  .chat-shell {
    height: calc(100vh - 52px);
    height: calc(100dvh - 52px);
    min-height: 460px;
  }

  .messages { padding-top: 24px; }
  .page-content { padding-top: 32px; }
}

@media (max-width: 600px) {
  .starter-grid { grid-template-columns: 1fr; }
  .starter-description { white-space: normal; }
  .messages { padding-inline: 16px; }
  .composer-dock { width: calc(100% - 32px); padding-bottom: 16px; }
  .page-content { width: calc(100% - 32px); }
  .message-user { max-width: 92%; }
  .login-panel { padding: 24px; }
}
