:root {
  color-scheme: light;
  --bg: #f7f7f4;
  --panel: #ffffff;
  --line: #d8d8d2;
  --text: #20231f;
  --muted: #6b6f68;
  --accent: #28666e;
  --accent-dark: #17484e;
  --soft: #eef3f1;
  --danger: #8b2f2f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
textarea {
  font: inherit;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 6px;
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
}

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

button:disabled {
  cursor: wait;
  opacity: 0.6;
}

button[hidden] {
  display: none;
}

[hidden] {
  display: none !important;
}

.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: var(--bg);
}

.login-panel {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.login-logo {
  width: 74px;
  height: 74px;
  object-fit: contain;
}

.login-panel h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
}

.login-panel label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

.login-panel input {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text);
  font: inherit;
}

.login-panel button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

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

.login-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.button-icon {
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

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

.brand-logo {
  display: block;
  width: 54px;
  min-width: 54px;
  height: 54px;
  flex: 0 0 54px;
  object-fit: contain;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.toolbar-toggle {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfbf9;
}

.toolbar-toggle button {
  min-height: 30px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  padding: 0 10px;
}

.toolbar-toggle button.active {
  background: var(--accent);
  color: #fff;
}

.toolbar-toggle button:hover {
  border-color: transparent;
  color: var(--accent-dark);
}

.toolbar-toggle button.active:hover {
  color: #fff;
}

.workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  min-height: 0;
}

.scope-summary {
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: #fbfbf9;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.messages {
  flex: 1;
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message {
  max-width: 920px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 12px 14px;
  line-height: 1.5;
}

.message.user {
  align-self: flex-end;
  background: #f1f6f5;
  border-color: #c7d9d6;
}

.message-original-query {
  color: var(--text);
}

.message-rewritten-query {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.message-rewritten-label {
  color: var(--accent-dark);
  font-weight: 600;
}

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

.rerun-button {
  display: inline-grid;
  place-items: center;
  width: 28px;
  min-height: 28px;
  height: 28px;
  padding: 0;
  border-radius: 50%;
  background: var(--panel);
  color: var(--accent-dark);
}

.message.assistant {
  align-self: flex-start;
}

.message-body {
  overflow-wrap: anywhere;
}

.message.user .message-body,
.message.working .message-body,
.message.system .message-body {
  white-space: pre-wrap;
}

.markdown-body > :first-child {
  margin-top: 0;
}

.markdown-body > :last-child {
  margin-bottom: 0;
}

.markdown-body p {
  margin: 0 0 10px;
}

.markdown-body ul,
.markdown-body ol {
  margin: 0 0 10px;
  padding-left: 22px;
}

.markdown-body li {
  margin: 3px 0;
}

.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 12px 0 6px;
  font-size: 15px;
  line-height: 1.35;
}

.markdown-body code {
  border: 1px solid #d9dedb;
  border-radius: 4px;
  padding: 1px 4px;
  background: #f3f6f5;
  color: var(--accent-dark);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}

.markdown-body pre {
  max-width: 100%;
  margin: 0 0 10px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 12px;
  background: #f3f6f5;
}

.markdown-body pre code {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  white-space: pre;
}

.markdown-body blockquote {
  margin: 0 0 10px;
  padding: 6px 10px;
  border-left: 3px solid var(--accent);
  background: #f6f8f7;
  color: var(--muted);
}

.markdown-body a {
  color: var(--accent-dark);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.message.system {
  align-self: center;
  max-width: 720px;
  color: var(--muted);
  background: transparent;
}

.acronym {
  display: inline-flex;
  color: var(--accent-dark);
  font-weight: 700;
  cursor: help;
  outline: none;
}

.message.working {
  border-style: dashed;
  color: var(--accent-dark);
}

.message-meta {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
}

.composer {
  position: sticky;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.composer textarea {
  width: 100%;
  resize: vertical;
  min-height: 48px;
  max-height: 180px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}

.composer button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  white-space: nowrap;
}

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

.muted {
  color: var(--muted);
}

.citation {
  display: inline-block;
  color: var(--accent-dark);
  font-weight: 700;
  cursor: help;
}

.floating-tooltip {
  position: fixed;
  inset: auto;
  margin: 0;
  z-index: 9999;
  width: min(420px, calc(100vw - 20px));
  max-height: min(260px, calc(100vh - 20px));
  overflow: auto;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  box-shadow: 0 12px 28px rgb(0 0 0 / 0.18);
  color: var(--text);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.4;
  text-align: left;
  white-space: pre-wrap;
  pointer-events: none;
}

dialog {
  width: min(860px, calc(100vw - 32px));
  max-height: min(820px, calc(100vh - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  overflow: auto;
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.28);
}

#scope-dialog {
  width: min(946px, calc(100vw - 32px));
  height: min(66.666vh, calc(100vh - 32px));
  max-height: min(66.666vh, calc(100vh - 32px));
}

#scope-dialog form {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

#scope-dialog .scope-section:last-of-type {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#scope-dialog .scope-list {
  flex: 1;
  min-height: 0;
  max-height: none;
}

#scope-dialog .dialog-header {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel);
}

#scope-dialog .scope-current-section {
  position: sticky;
  top: 57px;
  z-index: 1;
  background: var(--panel);
}

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

.dialog-header h2 {
  margin: 0;
  font-size: 18px;
}

.dialog-title {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 9px;
}

.dialog-logo {
  display: block;
  width: 32px;
  min-width: 32px;
  height: 32px;
  object-fit: contain;
}

.candidate-list {
  padding: 14px;
}

.scope-section {
  padding: 14px;
}

.scope-section + .scope-section {
  border-top: 1px solid var(--line);
}

.scope-section h3 {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

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

.scope-list {
  max-height: 360px;
  overflow: auto;
}

.scope-current-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.scope-filter {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text);
}

.scope-filter:focus {
  border-color: var(--accent);
  outline: 2px solid rgb(40 102 110 / 0.18);
}

.scope-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.scope-row.in-scope {
  background: #f1f6f5;
}

.scope-row:first-child {
  padding-top: 0;
}

.scope-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.scope-label {
  min-width: 0;
  overflow-wrap: anywhere;
}

.scope-current-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfbf9;
}

.scope-document-label {
  font-weight: 600;
}

.scope-document-title {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.scope-concepts {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 7px;
}

.scope-concept {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 7px;
  background: #fbfbf9;
  color: var(--accent-dark);
  font-size: 12px;
  line-height: 1.4;
}

.scope-concept:hover,
.scope-concept:focus {
  border-color: var(--accent);
  background: var(--soft);
}

.scope-add-button {
  min-width: 78px;
}

.scope-remove-button {
  min-width: 78px;
  color: var(--danger);
}

.scope-icon-button {
  display: inline-grid;
  place-items: center;
  width: 28px;
  min-height: 28px;
  height: 28px;
  padding: 0;
  color: var(--danger);
}

.scope-filter-clear {
  width: 38px;
  min-height: 38px;
  height: 38px;
}

.help-content {
  display: grid;
  gap: 0;
  padding: 4px 14px 14px;
}

.help-intro {
  margin: 10px 0 2px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfbf9;
  color: var(--text);
  line-height: 1.45;
}

.help-section {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

.help-section:last-child {
  border-bottom: 0;
}

.help-section h3 {
  margin: 0 0 5px;
  font-size: 15px;
}

.help-section p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
}

.candidate-button {
  width: 100%;
  justify-content: flex-start;
  text-align: left;
  margin-bottom: 8px;
}

@media (max-width: 900px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .brand-block {
    align-items: flex-start;
  }

  .toolbar {
    justify-content: flex-start;
  }

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