:root {
  --bg: #f7f4ed;
  --panel: #ffffff;
  --ink: #1c2730;
  --muted: #66717a;
  --line: #d8d0c3;
  --accent: #126a73;
  --accent-strong: #0a4f57;
  --gold: #a15f10;
  --warn-bg: #fff3d6;
  --warn-text: #7a4a08;
  --shadow: 0 16px 40px rgba(28, 39, 48, 0.08);
  --font-main: "Outfit", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(18, 106, 115, 0.1), transparent 330px),
    var(--bg);
  color: var(--ink);
  font-family: var(--font-main);
}

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

.search-shell {
  width: 100%;
  margin: 0;
  padding: 24px 20px 56px;
}

.search-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 24px;
  padding: 8px 0 24px;
}

.back-link {
  display: inline-flex;
  margin-bottom: 8px;
  color: var(--accent-strong);
  font-size: 0.92rem;
  font-weight: 700;
  text-decoration: none;
}

h1 {
  margin: 0;
  font-size: clamp(2.4rem, 5vw, 5.2rem);
  line-height: 0.95;
  letter-spacing: 0;
}

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

.pilot-badge,
.corpus-stats,
.chip,
.context-chip,
.attachment-chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.7);
  font-size: 0.82rem;
  font-weight: 500;
}

.pilot-badge {
  border-color: rgba(18, 106, 115, 0.35);
  color: var(--accent-strong);
}

.search-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.search-form label {
  display: block;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
  text-transform: uppercase;
}

.query-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

input[type="search"] {
  width: 100%;
  min-height: 56px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 16px;
  color: var(--ink);
  background: #fffdf9;
  font: inherit;
  font-size: 1.05rem;
  outline: none;
}

input[type="search"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(18, 106, 115, 0.14);
}

button,
.result-actions a {
  min-height: 40px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0 14px;
  color: #fff;
  background: var(--accent);
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

button:hover,
.result-actions a:hover {
  background: var(--accent-strong);
}

.scope-note {
  margin: 12px 0 0;
  max-width: 820px;
  color: var(--muted);
  line-height: 1.45;
}

.query-suggestions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
}

.suggestion-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
}

.suggestion-chip {
  min-height: 32px;
  border-color: var(--line);
  color: var(--accent-strong);
  background: #fff;
  font-size: 0.88rem;
}

.suggestion-chip:hover {
  color: #fff;
  border-color: var(--accent);
}

.results-summary {
  margin: 22px 0 12px;
  color: var(--muted);
  font-weight: 700;
}

.results-list {
  display: grid;
  gap: 16px;
}

.result-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: clip;
  background: rgba(255, 255, 255, 0.62);
  box-shadow: 0 8px 22px rgba(28, 39, 48, 0.05);
}

.group-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: #eef6f3;
  color: inherit;
  text-align: left;
}

.group-header:hover {
  background: #e2f0ec;
}

.group-title {
  color: var(--ink);
  font-size: 1.08rem;
  font-weight: 700;
  line-height: 1.25;
}

.group-meta {
  margin-left: auto;
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 600;
}

.group-toggle-indicator {
  flex: 0 0 auto;
  color: var(--accent-strong);
  font-size: 1rem;
  transition: transform 0.16s ease;
}

.result-group.collapsed .group-toggle-indicator {
  transform: rotate(-90deg);
}

.group-chunks {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(440px, 100%), 1fr));
  grid-auto-flow: dense;
  gap: 1px;
  background: var(--line);
}

.result-group.collapsed .group-chunks {
  display: none;
}

.result-item {
  border: 0;
  border-radius: 0;
  background: var(--panel);
  box-shadow: none;
}

.result-main {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 16px;
  padding: 18px;
}

.result-group:first-child .result-item:first-child .result-main {
  grid-template-columns: 56px minmax(0, 1fr);
  padding: 22px;
}

.result-rank {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: var(--accent-strong);
  background: #e7f2ef;
  font-weight: 700;
}

.result-meta,
.context-row,
.attachment-row,
.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip.quality-note {
  position: relative;
  border-color: rgba(161, 95, 16, 0.35);
  color: var(--warn-text);
  background: var(--warn-bg);
  cursor: help;
}

.chip.quality-note::after {
  position: absolute;
  left: 0;
  bottom: calc(100% + 8px);
  z-index: 20;
  width: min(360px, 80vw);
  padding: 10px 12px;
  border: 1px solid rgba(161, 95, 16, 0.35);
  border-radius: 6px;
  color: var(--ink);
  background: #fffaf0;
  box-shadow: 0 14px 32px rgba(28, 39, 48, 0.16);
  font-size: 0.84rem;
  font-weight: 500;
  line-height: 1.35;
  white-space: normal;
  content: attr(data-tooltip);
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition:
    opacity 0.12s ease,
    transform 0.12s ease;
}

.chip.quality-note:hover::after,
.chip.quality-note:focus::after {
  opacity: 1;
  transform: translateY(0);
}

.result-body h2 {
  margin: 10px 0 8px;
  font-size: 1.25rem;
  line-height: 1.2;
  letter-spacing: 0;
}

.result-body {
  min-width: 0;
}

.result-group:first-child .result-item:first-child .result-body h2 {
  font-size: clamp(1.45rem, 2vw, 2rem);
}

.heading-path {
  color: var(--accent-strong);
  font-size: 0.94rem;
  font-weight: 700;
  line-height: 1.35;
}

.snippet {
  margin: 12px 0;
  color: #2e3942;
  font-size: 1rem;
  line-height: 1.55;
}

mark {
  border-radius: 3px;
  padding: 0 2px;
  color: inherit;
  background: #ffe08a;
}

.attachment-row,
.prev-context-row,
.next-context-row {
  margin: 10px 0 14px;
}

.attachment-row,
.prev-context-row,
.next-context-row {
  align-items: flex-start;
}

.prev-context-row {
  margin: 12px 0 10px;
}

.next-context-row {
  margin: 10px 0 12px;
}

.context-chip,
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border-radius: 6px;
  color: var(--accent-strong);
  background: #fff;
  white-space: normal;
}

.attachment-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.1em;
  color: var(--muted);
  font-size: 1.05em;
  line-height: 1;
}

.context-chip:hover,
.attachment-chip:hover {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent-strong);
}

.attachment-chip:hover .attachment-icon {
  color: #fff;
}

.context-preview,
.attachment-panel {
  flex: 1 1 100%;
  min-width: 0;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  color: #2e3942;
  background: #fffdf9;
  font-size: 0.92rem;
  line-height: 1.45;
}

.context-preview p,
.attachment-panel p {
  margin: 6px 0 0;
}

.attachment-panel a {
  display: inline-flex;
  margin-top: 8px;
  font-weight: 700;
}

.link-source-chips {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 8px;
}

.link-source-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--accent-strong);
  background: #fff;
  transition: background 0.16s ease, color 0.16s ease, border-color 0.16s ease, transform 0.16s ease;
}

.link-source-chip svg {
  width: 16px;
  height: 16px;
}

.link-source-chip:hover {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent-strong);
  transform: translateY(-1px);
}

.link-source-chip.active {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent);
}

.attachment-panel figure {
  margin: 0;
}

.attachment-panel img {
  display: block;
  width: min(100%, 720px);
  max-height: 520px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.attachment-panel figcaption {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.table-preview {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

.table-preview table {
  width: max-content;
  min-width: 100%;
  max-width: none;
  border-collapse: collapse;
  font-size: 0.86rem;
}

.table-preview th,
.table-preview td {
  border: 1px solid var(--line);
  padding: 7px 9px;
  text-align: left;
  vertical-align: top;
  min-width: 140px;
  overflow-wrap: normal;
  white-space: nowrap;
}

.table-preview th {
  color: var(--ink);
  background: #eef6f3;
  font-weight: 700;
}

.result-actions a,
.result-actions button {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  color: var(--accent-strong);
  background: #fff;
}

.result-actions .link-source-chips {
  gap: 10px;
  margin-right: 4px;
}

.result-actions .link-source-chip {
  width: 44px;
  height: 44px;
  border-width: 1.5px;
  box-shadow: 0 1px 2px rgba(28, 39, 48, 0.08);
}

.result-actions .link-source-chip svg {
  width: 20px;
  height: 20px;
  stroke-width: 2.2;
}

.copy-cite-button {
  max-width: 100%;
  overflow-wrap: anywhere;
}

.attachment-panel .link-source-chips {
  margin-top: 8px;
}

.result-actions a:hover,
.result-actions button:hover {
  color: #fff;
}

@media (max-width: 720px) {
  .search-shell {
    padding: 16px 10px 40px;
  }

  .search-header,
  .query-row {
    display: grid;
    grid-template-columns: 1fr;
  }

  .status-group {
    justify-content: start;
  }

  .group-header {
    display: grid;
    grid-template-columns: 1fr;
  }

  .group-meta {
    flex: 1 1 auto;
  }

  .result-main {
    grid-template-columns: 1fr;
  }
}
