:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #d8dee6;
  --text: #1f2933;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --ok: #16803c;
  --warn: #b45309;
  --bad: #b42318;
}

* {
  box-sizing: border-box;
}

html {
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
  max-width: 100%;
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

button,
.download-link {
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 14px;
  text-decoration: none;
}

button:hover,
.download-link:hover {
  background: var(--accent-dark);
}

button.secondary {
  background: #fff;
  color: var(--text);
  border-color: var(--line);
}

.secondary-link {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 14px;
  text-decoration: none;
}

button.danger {
  background: #fff;
  border-color: #f1b8b4;
  color: var(--bad);
}

button.secondary:hover,
.secondary-link:hover,
.icon-button:hover {
  background: #eef2f7;
}

button.danger:hover {
  background: #fff1f0;
}

button:disabled,
.download-link.disabled {
  opacity: 0.5;
  pointer-events: none;
}

input[type="file"],
input[type="search"],
input[type="password"] {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  min-height: 36px;
  padding: 7px 10px;
}

.topbar {
  align-items: center;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 68px;
  padding: 10px 24px;
}

.topbar > div {
  min-width: 0;
}

.topbar-actions,
.topbar-actions form {
  flex: 0 0 auto;
}

.topbar-actions {
  align-items: center;
  display: flex;
  gap: 8px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  font-weight: 650;
}

h2 {
  font-size: 16px;
  font-weight: 650;
}

.topbar p {
  color: var(--muted);
  margin-top: 4px;
}

.layout {
  max-width: 100vw;
  overflow-x: hidden;
  padding: 18px 24px 28px;
  width: 100%;
}

.upload-band,
.progress-section,
.results-section,
.batch-list {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  max-width: 100%;
  min-width: 0;
}

.upload-band {
  margin-bottom: 16px;
  padding: 14px;
}

.upload-form {
  align-items: center;
  display: flex;
  gap: 12px;
}

.upload-form input[type="file"] {
  flex: 1;
  min-width: 260px;
}

.inline-message {
  color: var(--muted);
  min-width: 180px;
}

.split {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 16px;
  max-width: 100%;
  min-width: 0;
  width: 100%;
}

.batch-list {
  align-self: start;
  max-height: calc(100vh - 124px);
  overflow: hidden;
}

.section-head {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 48px;
  min-width: 0;
  padding: 10px 14px;
  gap: 12px;
}

.section-head h2 {
  flex: 0 0 auto;
}

.result-title {
  align-items: center;
  display: flex;
  flex: 0 1 auto;
  flex-wrap: wrap;
  gap: 12px;
  min-width: 0;
}

.view-tabs {
  background: #eef2f7;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: inline-flex;
  padding: 2px;
}

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

.tab-button:hover {
  background: #fff;
}

.tab-button.active {
  background: #fff;
  border-color: var(--line);
  color: var(--text);
}

.download-link {
  flex: 0 0 auto;
}

.actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
  justify-content: flex-end;
}

.icon-button {
  background: #fff;
  border-color: var(--line);
  color: var(--text);
  min-height: 32px;
  padding: 0 10px;
}

.batch-list-body {
  max-height: calc(100vh - 173px);
  overflow: auto;
  padding: 8px;
}

.batch-item {
  background: #fff;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  margin-bottom: 6px;
  padding: 9px 10px;
}

.batch-item.active {
  border-color: var(--accent);
  background: #eef4ff;
}

.batch-item strong {
  display: block;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.batch-item span {
  color: var(--muted);
  display: block;
  font-size: 12px;
  margin-top: 3px;
}

.work-area {
  display: grid;
  gap: 16px;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}

.progress-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(100px, 1fr));
  gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}

.progress-grid > div {
  background: #fff;
  padding: 12px 14px;
}

.metric-label {
  color: var(--muted);
  display: block;
  font-size: 12px;
}

.progress-grid strong {
  display: block;
  font-size: 20px;
  margin-top: 4px;
}

.progress-track {
  background: #e8edf5;
  height: 8px;
}

.progress-bar {
  background: var(--accent);
  height: 8px;
  width: 0;
}

.table-wrap {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  width: 100%;
}

.table-wrap.compact {
  max-height: 260px;
}

table {
  border-collapse: collapse;
  min-width: 980px;
  table-layout: auto;
  width: 100%;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}

th {
  background: #f8fafc;
  color: #475569;
  font-size: 12px;
  font-weight: 650;
  position: sticky;
  top: 0;
  z-index: 1;
}

td {
  background: #fff;
}

.results-table td:last-child,
.results-table th:last-child {
  min-width: 180px;
  white-space: normal;
}

.result-view {
  display: none;
}

.result-view.active {
  display: block;
}

.timeline-body {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.timeline-empty {
  color: var(--muted);
  padding: 18px 2px;
}

.patient-timeline {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.patient-timeline summary {
  align-items: center;
  background: #f8fafc;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  list-style: none;
  min-height: 42px;
  padding: 10px 14px;
}

.patient-timeline summary::-webkit-details-marker {
  display: none;
}

.patient-timeline summary strong {
  font-size: 15px;
}

.patient-timeline summary span {
  color: var(--muted);
  font-size: 12px;
}

.drug-history-list {
  display: grid;
  gap: 12px;
  padding: 12px 14px 14px;
}

.drug-history-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.drug-history-head {
  align-items: center;
  background: #fbfdff;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-height: 42px;
  padding: 10px 12px;
}

.drug-history-head > div {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.drug-history-head strong {
  font-size: 15px;
}

.drug-history-head > span {
  color: var(--muted);
  flex: 0 0 auto;
  font-size: 12px;
}

.drug-record-list {
  display: grid;
}

.drug-record {
  display: grid;
  grid-template-columns: minmax(170px, 0.34fr) minmax(0, 1fr);
  gap: 14px;
  padding: 11px 12px;
}

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

.drug-record-date {
  color: #334155;
  display: flex;
  flex-wrap: wrap;
  gap: 5px 8px;
}

.drug-record-date span {
  background: #f1f5f9;
  border-radius: 999px;
  padding: 3px 8px;
}

.drug-record-body {
  min-width: 0;
}

.drug-tag {
  background: #e8f1ff;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  color: #1d4ed8;
  font-size: 12px;
  padding: 3px 8px;
}

.timeline-meta {
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 7px;
}

.timeline-reason {
  color: #334155;
  margin-top: 7px;
}

.lane-body {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.lane-patient {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.lane-patient summary {
  align-items: center;
  background: #f8fafc;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  list-style: none;
  min-height: 42px;
  padding: 10px 14px;
}

.lane-patient summary::-webkit-details-marker {
  display: none;
}

.lane-patient summary strong {
  font-size: 15px;
}

.lane-patient summary span {
  color: var(--muted);
  font-size: 12px;
}

.lane-note {
  background: #fffdf5;
  border-top: 1px solid #fdecc8;
  color: #7c4a03;
  font-size: 12px;
  padding: 8px 14px;
}

.lane-axis,
.lane-row {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  min-width: 0;
  width: 100%;
}

.lane-scroll {
  overflow-x: hidden;
  width: 100%;
}

.lane-axis {
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  padding: 10px 14px 4px;
}

.lane-axis-label {
  padding-right: 12px;
}

.lane-axis-track {
  border-bottom: 1px solid var(--line);
  height: 24px;
  position: relative;
}

.lane-axis-track span {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  white-space: nowrap;
}

.lane-grid {
  padding: 0 14px 14px;
}

.lane-row {
  border-bottom: 1px solid var(--line);
}

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

.lane-label {
  align-content: center;
  border-right: 1px solid var(--line);
  display: grid;
  gap: 4px;
  min-width: 0;
  padding: 9px 12px 9px 0;
}

.lane-label strong {
  align-items: center;
  background: #f1f5f9;
  border-radius: 999px;
  display: inline-flex;
  height: 24px;
  justify-content: center;
  width: 32px;
}

.lane-label strong,
.lane-label span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lane-label span {
  color: var(--muted);
  font-size: 12px;
}

.lane-canvas {
  background:
    linear-gradient(to right, rgba(216, 222, 230, 0.55) 1px, transparent 1px) 0 0 / 20% 100%,
    #fff;
  height: 42px;
  min-width: 0;
  overflow: hidden;
  position: relative;
}

.lane-bar,
.lane-point {
  position: absolute;
}

.lane-bar {
  align-items: center;
  border-radius: 999px;
  display: flex;
  height: 20px;
  max-width: 100%;
  min-width: 38px;
  overflow: hidden;
  padding: 0 8px;
  top: 11px;
  white-space: nowrap;
}

.lane-bar span,
.lane-point span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lane-bar.range {
  background: var(--lane-bg);
  border: 1px solid var(--lane-color);
  color: var(--lane-color);
}

.lane-bar.open {
  background: var(--lane-bg);
  border: 1px dashed var(--lane-color);
  color: var(--lane-color);
}

.lane-point {
  align-items: center;
  display: flex;
  gap: 6px;
  height: 20px;
  top: 11px;
  transform: translateX(-7px);
  white-space: nowrap;
}

.lane-point::before {
  background: var(--lane-color);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--lane-color);
  content: "";
  flex: 0 0 auto;
  height: 10px;
  width: 10px;
}

.lane-point span {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: #334155;
  max-width: 220px;
  padding: 2px 7px;
}

.lane-undated {
  border-top: 1px solid var(--line);
  display: grid;
  gap: 8px;
  padding: 12px 14px 14px;
}

.lane-undated > strong {
  color: var(--muted);
  font-size: 13px;
}

.lane-undated-row {
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(120px, 0.22fr) minmax(0, 1fr);
}

.lane-undated-row > span {
  color: #334155;
  font-weight: 650;
}

.lane-undated-row > div {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.lane-undated-row em {
  background: #f1f5f9;
  border-radius: 999px;
  color: var(--muted);
  font-style: normal;
  padding: 3px 8px;
}

.filters {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
  min-width: 0;
}

.filters input {
  flex: 0 1 150px;
  min-width: 110px;
  width: 150px;
}

.status {
  border-radius: 999px;
  display: inline-flex;
  font-size: 12px;
  line-height: 1;
  padding: 5px 8px;
}

.status.queued,
.status.running {
  background: #e0f2fe;
  color: #075985;
}

.status.success {
  background: #dcfce7;
  color: var(--ok);
}

.status.partial {
  background: #fef3c7;
  color: var(--warn);
}

.status.failed {
  background: #fee2e2;
  color: var(--bad);
}

.form-error {
  color: var(--bad);
  min-height: 20px;
}

.login-page {
  align-items: center;
  display: flex;
  min-height: 100vh;
  justify-content: center;
}

.login-shell {
  width: min(380px, calc(100vw - 32px));
}

.login-panel {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 12px;
  padding: 22px;
}

.login-panel h1 {
  margin-bottom: 6px;
}

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

@media (max-width: 960px) {
  .split {
    grid-template-columns: 1fr;
  }

  .batch-list,
  .batch-list-body {
    max-height: none;
  }

  .progress-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .section-head {
    align-items: stretch;
    flex-direction: column;
  }

  .filters {
    justify-content: flex-start;
    overflow-x: auto;
  }

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

  .drug-record {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .lane-axis,
  .lane-row {
    grid-template-columns: 76px minmax(520px, 1fr);
    min-width: 610px;
  }

  .lane-scroll {
    overflow-x: auto;
  }

  .lane-undated-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .topbar,
  .layout {
    padding-left: 12px;
    padding-right: 12px;
  }

  .upload-form {
    align-items: stretch;
    flex-direction: column;
  }

  .upload-form input[type="file"] {
    min-width: 0;
  }

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