:root {
  color-scheme: light dark;
  --bg: light-dark(#f4f7fb, #0d1420);
  --surface: light-dark(#ffffff, #151e2d);
  --surface-alt: light-dark(#eef3f9, #1b2738);
  --text: light-dark(#172033, #edf3fb);
  --muted: light-dark(#5b6678, #a8b4c6);
  --border: light-dark(#d7dfeb, #2b3a50);
  --primary: light-dark(#0969da, #66aaff);
  --primary-hover: light-dark(#0757ba, #8bc1ff);
  --primary-soft: light-dark(#dbeafe, #19395d);
  --danger: light-dark(#c62828, #ff7b72);
  --danger-hover: light-dark(#a51f1f, #ff9a94);
  --success: light-dark(#197a3a, #67d68b);
  --warning: light-dark(#8a5a00, #ffd166);
  --warning-bg: light-dark(#fff4ce, #3b3013);
  --shadow: light-dark(0 8px 24px rgba(23, 32, 51, 0.08), 0 10px 30px rgba(0, 0, 0, 0.25));
  --radius: 14px;
  --radius-small: 9px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

:root[data-theme="dark"] {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

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

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

button,
a {
  -webkit-tap-highlight-color: transparent;
}

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

[hidden] {
  display: none !important;
}

.app-container {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 64px;
}

.header,
.section,
.footer {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.header {
  padding: 30px;
  text-align: center;
}

.header h1 {
  margin: 4px 0 8px;
  font-size: clamp(2.2rem, 7vw, 4rem);
  letter-spacing: -0.04em;
}

.eyebrow {
  margin: 0;
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.subtitle {
  max-width: 760px;
  margin: 0 auto 22px;
  color: var(--muted);
  font-size: 1.05rem;
}

.section {
  margin-top: 22px;
  padding: 24px;
}

.section > h2,
.section-heading h2 {
  margin: 0;
  font-size: 1.35rem;
}

.section-description,
.field-help,
.form-message {
  color: var(--muted);
  font-size: 0.9rem;
}

.section-description {
  margin: 6px 0 0;
}

.header-tools {
  display: grid;
  justify-items: center;
  gap: 10px;
}

.language-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.language-control select {
  width: auto;
  min-width: 120px;
  padding: 7px 10px;
}

.access-control {
  display: flex;
  justify-content: center;
  gap: 10px;
  max-width: 620px;
  margin: 0 auto;
}

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

input[type="file"] {
  padding: 9px;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent);
  outline-offset: 2px;
}

textarea {
  min-height: 96px;
  resize: vertical;
}

label {
  display: grid;
  gap: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 700;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: var(--radius-small);
  padding: 9px 15px;
  cursor: pointer;
  font-weight: 750;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  transition: background 140ms ease, border-color 140ms ease, transform 140ms ease;
}

.button:hover:not(:disabled) {
  transform: translateY(-1px);
}

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

.button.primary {
  background: var(--primary);
  color: #ffffff;
}

.button.primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

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

.button.danger {
  border-color: color-mix(in srgb, var(--danger) 40%, var(--border));
  background: transparent;
  color: var(--danger);
}

.button.small {
  min-height: 34px;
  padding: 7px 10px;
  font-size: 0.8rem;
}

.button.full {
  width: 100%;
  margin-top: 10px;
}

.text-link {
  color: var(--primary);
  font-weight: 700;
  text-underline-offset: 3px;
}

.notice {
  margin: 22px auto 0;
  max-width: 900px;
  border: 1px solid color-mix(in srgb, var(--warning) 55%, var(--border));
  border-radius: var(--radius);
  background: var(--warning-bg);
  padding: 18px 20px;
  text-align: center;
}

.notice strong {
  color: var(--warning);
  font-size: 1.08rem;
}

.notice p {
  margin: 7px auto 0;
  max-width: 78ch;
}

.steps {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin: 18px 0 0;
  padding: 0;
  list-style: none;
  counter-reset: steps;
}

.steps li {
  position: relative;
  min-height: 112px;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface-alt);
  padding: 18px 18px 18px 54px;
  counter-increment: steps;
}

.steps li::before {
  position: absolute;
  top: 17px;
  left: 16px;
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 50%;
  background: var(--primary);
  color: #ffffff;
  content: counter(steps);
  font-weight: 800;
}

.steps strong,
.steps span {
  display: block;
}

.steps span {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.86rem;
}

.section-heading,
.program-actions,
.export-buttons,
.dialog-header,
.dialog-actions,
.batch-controls,
.filters,
.card-header,
.card-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-heading {
  justify-content: space-between;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.programs-grid,
.items-grid,
.finding-list {
  display: grid;
  gap: 16px;
}

.programs-grid,
.finding-list {
  grid-template-columns: repeat(auto-fit, minmax(270px, 1fr));
}

.program-card,
.item-card,
.finding-card,
.crossref-pair,
.service-row {
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface-alt);
}

.program-card {
  padding: 18px;
}

.program-card h3 {
  margin: 5px 0 8px;
  font-size: 1.05rem;
}

.program-card p {
  margin: 5px 0;
}

.program-actions,
.export-buttons,
.dialog-actions,
.batch-controls {
  flex-wrap: wrap;
}

details > summary {
  cursor: pointer;
  font-size: 1.12rem;
  font-weight: 800;
}

details[open] > summary {
  margin-bottom: 18px;
}

.services-list {
  display: grid;
  gap: 8px;
}

.service-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 16px;
  padding: 11px 13px;
}

.service-row > span:first-child {
  margin-right: auto;
}

.service-status {
  margin-left: auto;
  white-space: nowrap;
  font-size: 0.82rem;
  font-weight: 800;
}

.service-row > .text-link {
  margin-left: 14px;
  white-space: nowrap;
}

.service-status.available {
  color: var(--success);
}

.service-status.unavailable {
  color: var(--muted);
}

.manual-form {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 760px;
  margin-top: 14px;
}

.manual-form input {
  flex: 1;
}

.manual-form .button {
  flex: 0 0 auto;
}

.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--border);
}

.tab {
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 750;
}

.tab.active {
  border-bottom-color: var(--primary);
  color: var(--primary);
}

.stack-form {
  display: grid;
  gap: 16px;
}

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

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

.count {
  display: inline-grid;
  min-width: 28px;
  height: 28px;
  place-items: center;
  margin-left: 6px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.82rem;
  vertical-align: 2px;
}

.filters {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.filters input {
  min-width: 280px;
}

.filters select {
  min-width: 170px;
}

.items-grid {
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
}

.item-card {
  overflow: hidden;
}

.card-header {
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  padding: 11px;
}

.tag {
  display: inline-flex;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 0.72rem;
  font-weight: 800;
}

.program-tag {
  background: var(--primary-soft);
  color: var(--primary);
}

.type-tag {
  background: var(--surface);
  color: var(--muted);
}

.card-status {
  width: auto;
  min-width: 130px;
  margin-left: auto;
  padding: 6px 8px;
  font-size: 0.8rem;
}

.card-image {
  position: relative;
  display: grid;
  min-height: 230px;
  place-items: center;
  overflow: hidden;
  background: var(--surface);
}

.card-image img {
  display: block;
  width: 100%;
  height: 260px;
  object-fit: contain;
}

.image-placeholder {
  color: var(--muted);
  font-size: 0.9rem;
}

.card-body {
  padding: 14px;
}

.card-ref {
  color: var(--primary);
  font-weight: 850;
}

.card-description,
.card-source {
  margin: 6px 0;
  color: var(--muted);
  font-size: 0.88rem;
  white-space: pre-wrap;
}

.card-analysis {
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  padding: 11px;
  font-size: 0.8rem;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 5px;
}

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

.card-actions {
  flex-wrap: wrap;
  border-top: 1px solid var(--border);
  padding: 11px;
}

.empty-state {
  margin: 18px 0 0;
  border: 1px dashed var(--border);
  border-radius: var(--radius-small);
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.check-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
}

.check-control input {
  width: 18px;
  height: 18px;
}

.progress {
  position: relative;
  height: 9px;
  overflow: hidden;
  margin-top: 18px;
  border-radius: 999px;
  background: var(--surface-alt);
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 180ms ease;
}

.progress-fill[data-percent="5"] { width: 5%; }
.progress-fill[data-percent="10"] { width: 10%; }
.progress-fill[data-percent="15"] { width: 15%; }
.progress-fill[data-percent="20"] { width: 20%; }
.progress-fill[data-percent="25"] { width: 25%; }
.progress-fill[data-percent="30"] { width: 30%; }
.progress-fill[data-percent="35"] { width: 35%; }
.progress-fill[data-percent="40"] { width: 40%; }
.progress-fill[data-percent="45"] { width: 45%; }
.progress-fill[data-percent="50"] { width: 50%; }
.progress-fill[data-percent="55"] { width: 55%; }
.progress-fill[data-percent="60"] { width: 60%; }
.progress-fill[data-percent="65"] { width: 65%; }
.progress-fill[data-percent="70"] { width: 70%; }
.progress-fill[data-percent="75"] { width: 75%; }
.progress-fill[data-percent="80"] { width: 80%; }
.progress-fill[data-percent="85"] { width: 85%; }
.progress-fill[data-percent="90"] { width: 90%; }
.progress-fill[data-percent="95"] { width: 95%; }
.progress-fill[data-percent="100"] { width: 100%; }

#progressText {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: right;
}

.crossref-results {
  display: grid;
  gap: 9px;
}

.crossref-pair {
  padding: 12px;
}

.crossref-pair p {
  margin: 4px 0 0;
}

.footer {
  margin-top: 22px;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.footer p {
  margin: 5px 0;
}

.dialog {
  width: min(1000px, calc(100% - 28px));
  max-height: min(88vh, 900px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.32);
  padding: 22px;
}

.dialog::backdrop {
  background: rgba(3, 8, 18, 0.72);
  backdrop-filter: blur(3px);
}

.dialog-header {
  position: sticky;
  top: -22px;
  z-index: 2;
  justify-content: space-between;
  margin: -22px -22px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 16px 22px;
}

.dialog-header h2 {
  margin: 0;
}

.dialog-content {
  display: grid;
  gap: 18px;
}

.result-summary,
.result-section {
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface-alt);
  padding: 15px;
}

.result-summary p,
.result-section p {
  margin: 5px 0;
}

.result-section h3 {
  margin: 0 0 10px;
}

.finding-card {
  overflow: hidden;
}

.finding-card img {
  display: block;
  width: 100%;
  height: 180px;
  object-fit: contain;
  background: var(--surface);
}

.finding-content {
  display: grid;
  gap: 5px;
  padding: 13px;
}

.finding-content p {
  margin: 0;
  font-size: 0.86rem;
}

.source-list {
  margin: 0;
  padding-left: 20px;
}

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

.crop-dialog {
  width: min(960px, calc(100% - 28px));
}

.crop-canvas-large {
  display: block;
  width: 100%;
  height: auto;
  max-height: 65vh;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface-alt);
  cursor: crosshair;
  touch-action: none;
}

.toast-region {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 20;
  display: grid;
  width: min(360px, calc(100% - 36px));
  gap: 8px;
}

.toast {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 12px 14px;
}

.toast-close {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
  padding: 0;
}

.toast.success {
  border-color: var(--success);
}

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

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

@media (max-width: 820px) {
  .steps,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .filters,
  .filters input,
  .filters select {
    width: 100%;
  }

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

@media (max-width: 520px) {
  .app-container {
    width: min(100% - 18px, 1180px);
    padding-top: 9px;
  }

  .header,
  .section {
    padding: 17px;
  }

  .access-control,
  .form-actions,
  .export-buttons,
  .batch-controls,
  .manual-form,
  .card-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .access-control .button,
  .form-actions .button,
  .export-buttons .button,
  .manual-form .button,
  .card-actions .button {
    width: 100%;
  }

  .card-header {
    align-items: stretch;
  }

  .card-status {
    width: 100%;
    margin-left: 0;
  }

  .dialog {
    padding: 14px;
  }

  .dialog-header {
    top: -14px;
    margin: -14px -14px 14px;
    padding: 12px 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
