:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-muted: #eef1f6;
  --border: #d8dee8;
  --border-light: #e8ecf2;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #1e4d7b;
  --accent-hover: #163a5f;
  --accent-soft: #e8f0f8;
  --error: #c53030;
  --success: #2f855a;
  --legal: #1e4d7b;
  --legal-soft: #e8f0f8;
  --legal-border: #9db8d4;
  --general: #9a3412;
  --general-soft: #fff7ed;
  --general-border: #fdba74;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
}

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

body {
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.hidden { display: none !important; }

.screen { min-height: 100vh; }

/* Login */
.login-card {
  max-width: 400px;
  margin: 18vh auto;
  padding: 2.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  text-align: center;
  box-shadow: var(--shadow);
}

.login-card h1 {
  font-size: 2rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: 0.25rem;
  color: var(--accent);
}

.subtitle {
  color: var(--muted);
  margin-bottom: 2rem;
  font-size: 0.95rem;
}

.login-card input {
  width: 100%;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.login-card button {
  width: 100%;
  padding: 0.75rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
}

.login-card button:hover { background: var(--accent-hover); }

.login-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.login-actions button {
  flex: 1;
  min-width: 8rem;
}

.login-actions button.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.login-actions button.secondary:hover {
  background: var(--bg);
}

.error { color: var(--error); margin-top: 0.75rem; font-size: 0.875rem; }

/* App layout */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand {
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--accent);
}

.controls {
  display: flex;
  align-items: center;
  gap: 1.25rem;
}

.controls label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

select {
  padding: 0.5rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.875rem;
  min-width: 200px;
}

select:focus {
  outline: none;
  border-color: var(--accent);
}

button {
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
}

button:hover { background: var(--accent-hover); }

button.secondary {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

button.secondary:hover {
  background: var(--surface-muted);
  border-color: #c5ced9;
}

button:disabled { opacity: 0.55; cursor: not-allowed; }

#chat-main {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 60px);
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 1rem;
}

.message {
  margin-bottom: 1.25rem;
  padding: 1rem 1.25rem;
  border-radius: 10px;
  line-height: 1.65;
  box-shadow: var(--shadow);
}

.message.user {
  background: var(--surface);
  border: 1px solid var(--border);
  margin-left: 2rem;
}

.message.assistant {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-left: 3px solid var(--accent);
  margin-right: 2rem;
}

.message .role {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.message-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.message-actions .copy-btn,
.message-actions .cite-export-btn,
.message-actions .retry-btn {
  padding: 0.25rem 0.6rem;
  font-size: 0.75rem;
}

.cite-export {
  position: relative;
}

.cite-export-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 0.35rem);
  z-index: 50;
  min-width: 15.5rem;
  max-height: min(50vh, 22rem);
  overflow-y: auto;
  padding: 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.14);
}

.cite-export-item {
  display: block;
  width: 100%;
  text-align: left;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 500;
  letter-spacing: normal;
  text-transform: none;
  padding: 0.45rem 0.55rem;
  border-radius: 6px;
  cursor: pointer;
}

.cite-export-item:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

button.stop {
  background: var(--error);
}

button.stop:hover {
  background: #9b2c2c;
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.6rem;
  background: var(--accent-soft);
  border: 1px solid #c5d9eb;
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--accent);
}

.attachment-chip button {
  padding: 0 0.35rem;
  min-width: 1.25rem;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
}

.attachment-chip button:hover {
  color: var(--text);
  background: transparent;
}

.chat-actions-left,
.chat-actions-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.chat-actions-right {
  margin-left: auto;
}

.message.assistant.legal { border-left-color: var(--legal); }

.message .meta {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border-light);
  font-size: 0.75rem;
  color: var(--muted);
}

#chat-form {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  background: var(--bg);
}

#app-screen.mode-legal #chat-form {
  border-top-color: var(--legal-border);
}

#app-screen.mode-general #chat-form {
  border-top-color: var(--general-border);
}

.mode-banner {
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 0.85rem;
  line-height: 1.4;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.mode-banner strong {
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.mode-banner-legal {
  background: var(--legal-soft);
  border-color: var(--legal-border);
  color: var(--legal);
}

.mode-banner-general {
  background: var(--general-soft);
  border-color: var(--general-border);
  color: var(--general);
}

.mode-control-legal select {
  border-color: var(--legal-border);
  background-color: var(--legal-soft);
  color: var(--legal);
  font-weight: 600;
}

.mode-control-general select {
  border-color: var(--general-border);
  background-color: var(--general-soft);
  color: var(--general);
  font-weight: 600;
}

.mode-control-legal select:focus {
  border-color: var(--legal);
  box-shadow: 0 0 0 3px var(--legal-soft);
}

.mode-control-general select:focus {
  border-color: var(--general);
  box-shadow: 0 0 0 3px var(--general-soft);
}

#send-btn.send-legal {
  background: var(--legal);
}

#send-btn.send-legal:hover {
  background: var(--accent-hover);
}

#send-btn.send-general {
  background: var(--general);
}

#send-btn.send-general:hover {
  background: #7c2d12;
}

#app-screen.mode-general #chat-input {
  border-color: var(--general-border);
}

#app-screen.mode-general #chat-input:focus {
  border-color: var(--general);
  box-shadow: 0 0 0 3px var(--general-soft);
}

#app-screen.mode-legal #chat-input:focus {
  border-color: var(--legal);
  box-shadow: 0 0 0 3px var(--legal-soft);
}

.app-disclaimer {
  margin-top: 0.85rem;
  padding: 0.55rem 0.15rem 0.25rem;
  font-size: 0.75rem;
  line-height: 1.45;
  color: var(--muted);
  border-top: 1px dashed var(--border-light);
}

#chat-input {
  width: 100%;
  padding: 0.875rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  resize: vertical;
  font-family: inherit;
}

#chat-input:focus {
  outline: none;
}

.chat-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.stream-toggle {
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-transform: none;
  letter-spacing: normal;
  font-weight: normal;
}

.sources-control {
  position: relative;
}

.sources-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.4rem 0.75rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
  width: auto;
  box-shadow: none;
}

.sources-btn:hover {
  background: var(--surface-muted);
  border-color: #c5cedb;
}

.sources-btn[aria-expanded="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.sources-btn-label {
  color: var(--muted);
  font-weight: 400;
}

.sources-btn-summary {
  color: var(--accent);
}

.sources-btn-chevron {
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1;
}

.sources-btn.is-empty .sources-btn-summary {
  color: var(--muted);
}

.sources-panel {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  left: 0;
  z-index: 40;
  width: min(26rem, calc(100vw - 2rem));
  max-height: min(70vh, 32rem);
  overflow-y: auto;
  padding: 0.85rem 0.9rem 0.95rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
}

.sources-panel-head {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-bottom: 0.65rem;
}

.sources-panel-lead {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0;
  line-height: 1.45;
}

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

.sources-mini-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg, #fff);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 0.28rem 0.55rem;
  border-radius: 6px;
  cursor: pointer;
}

.sources-mini-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.source-group {
  padding: 0.35rem 0 0.45rem;
}

.source-group + .source-group {
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
  padding-top: 0.55rem;
}

.source-group-title {
  margin: 0 0 0.25rem 0.15rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.source-option {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  padding: 0.5rem 0.55rem;
  border-radius: 8px;
  cursor: pointer;
  text-transform: none;
  letter-spacing: normal;
  font-weight: normal;
}

.source-option:hover {
  background: var(--accent-soft);
}

.source-option + .source-option {
  margin-top: 0.05rem;
}

.source-option input {
  margin-top: 0.2rem;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.source-option-body {
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
  min-width: 0;
}

.source-option-title {
  font-size: 0.88rem;
  color: var(--text);
  font-weight: 600;
}

.source-option-desc {
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.35;
}

.sources-control.is-disabled {
  opacity: 0.55;
  pointer-events: none;
}

.output-style-control {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
  text-transform: none;
  letter-spacing: normal;
}

.output-style-label {
  white-space: nowrap;
}

.output-style-control select {
  appearance: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 1.75rem 0.4rem 0.75rem;
  font-size: 0.85rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236b7280' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.65rem center;
  box-shadow: none;
}

.output-style-control select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.typing::after {
  content: "▋";
  animation: blink 1s step-end infinite;
  color: var(--accent);
}

@keyframes blink { 50% { opacity: 0; } }

.custom-model-note {
  font-size: 0.7rem;
  color: #b7791f;
  margin-top: 0.25rem;
}

/* Markdown in assistant messages */
.message .content.markdown {
  line-height: 1.7;
  color: var(--text);
}

.message .content.markdown :first-child { margin-top: 0; }
.message .content.markdown :last-child { margin-bottom: 0; }

.message .content.markdown p { margin: 0.75em 0; }

.message .content.markdown h1,
.message .content.markdown h2,
.message .content.markdown h3,
.message .content.markdown h4 {
  margin: 1.25em 0 0.5em;
  line-height: 1.3;
  color: var(--text);
}

.message .content.markdown h1 { font-size: 1.35rem; }
.message .content.markdown h2 { font-size: 1.15rem; }
.message .content.markdown h3 { font-size: 1.05rem; }

.message .content.markdown ul,
.message .content.markdown ol {
  margin: 0.75em 0;
  padding-left: 1.5em;
}

.message .content.markdown li { margin: 0.25em 0; }

.message .content.markdown code {
  background: var(--surface-muted);
  border: 1px solid var(--border-light);
  border-radius: 4px;
  padding: 0.15em 0.4em;
  font-size: 0.9em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.message .content.markdown pre {
  background: var(--surface-muted);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 1rem;
  overflow-x: auto;
  margin: 0.75em 0;
}

.message .content.markdown pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85em;
}

.message .content.markdown blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.75em 0;
  padding: 0.25em 0 0.25em 1em;
  color: var(--muted);
  background: var(--surface-muted);
  border-radius: 0 6px 6px 0;
}

.message .content.markdown a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message .content.markdown a:hover {
  color: var(--accent-hover);
}

.message .content.markdown table {
  border-collapse: collapse;
  margin: 0.75em 0;
  width: 100%;
  font-size: 0.9em;
}

.message .content.markdown th,
.message .content.markdown td {
  border: 1px solid var(--border);
  padding: 0.5em 0.75em;
  text-align: left;
}

.message .content.markdown th {
  background: var(--surface-muted);
  font-weight: 600;
}

.message .content.markdown hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.25em 0;
}

.message .content.status {
  color: var(--muted);
  font-style: normal;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.message.status-message {
  opacity: 0.95;
  border-style: dashed;
  background: var(--surface-muted);
  box-shadow: none;
}
