:root {
  --ink: #172033;
  --muted: #667085;
  --line: #d9e0e8;
  --line-strong: #c8d1dc;
  --surface: #ffffff;
  --surface-soft: #f5f7fa;
  --canvas: #eef2f6;
  --navy: #17324d;
  --navy-hover: #10283f;
  --teal: #197a68;
  --teal-soft: #e8f4f0;
  --red: #b42318;
  --red-soft: #fce8e6;
  --orange: #a15c00;
  --orange-soft: #fff1dc;
  --yellow-soft: #fff7d6;
  --green: #157347;
  --green-soft: #e7f4ec;
  --grey-soft: #eef1f4;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px rgba(16, 24, 40, 0.05);
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
  font-family: "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
}

.app-container {
  width: min(100% - 40px, 1560px);
  margin-inline: auto;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 1030;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
}

.header-inner {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}

.brand-block,
.header-actions {
  display: flex;
  align-items: center;
}

.brand-block {
  gap: 12px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 6px;
  background: var(--navy);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
}

.brand-block h1 {
  margin: 0;
  font-size: 1.18rem;
  font-weight: 700;
  line-height: 1.3;
}

.brand-block p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.header-actions {
  gap: 12px;
}

.status-pill {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #98a2b3;
}

.status-pill.status-working {
  border-color: #b8cde2;
  background: #edf5fc;
  color: #175b8f;
}

.status-pill.status-working .status-dot {
  background: #2e90fa;
}

.status-pill.status-complete {
  border-color: #b8dccd;
  background: var(--teal-soft);
  color: var(--teal);
}

.status-pill.status-complete .status-dot {
  background: var(--teal);
}

.status-pill.status-error {
  border-color: #efc3be;
  background: var(--red-soft);
  color: var(--red);
}

.status-pill.status-error .status-dot {
  background: var(--red);
}

.btn {
  border-radius: 6px;
  font-weight: 650;
}

.export-button {
  min-height: 40px;
  padding-inline: 18px;
  border: 1px solid var(--teal);
  background: var(--teal);
  color: #fff;
}

.export-button:hover,
.export-button:focus-visible {
  border-color: #126554;
  background: #126554;
  color: #fff;
}

.export-button:disabled {
  border-color: #cbd5df;
  background: #dbe2e9;
  color: #7b8794;
}

.app-main {
  padding-block: 28px 48px;
}

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

.section-heading,
.anomaly-toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
}

.section-kicker {
  margin: 0 0 4px;
  color: var(--teal);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.section-heading h2,
.anomaly-toolbar h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 750;
}

.format-note {
  color: var(--muted);
  font-size: 0.8rem;
}

.upload-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr)) 180px;
  align-items: end;
  gap: 12px;
  margin-top: 16px;
}

.upload-source {
  min-width: 0;
}

.upload-source-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
}

.upload-source-title {
  color: var(--navy);
  font-size: 0.86rem;
  font-weight: 800;
}

.upload-source-hint {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.72rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-picker {
  min-width: 0;
  min-height: 50px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
  margin: 0;
  padding: 7px 12px 7px 7px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.file-picker:hover,
.file-picker.is-dragging {
  border-color: var(--teal);
  background: #fbfefd;
  box-shadow: 0 0 0 3px rgba(25, 122, 104, 0.11);
}

.file-action {
  align-self: stretch;
  display: inline-flex;
  align-items: center;
  padding-inline: 14px;
  border-radius: 4px;
  background: var(--surface-soft);
  color: var(--navy);
  font-size: 0.88rem;
  font-weight: 700;
  white-space: nowrap;
}

.file-name {
  min-width: 0;
  overflow: hidden;
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-name.is-empty {
  color: var(--muted);
  font-weight: 500;
}

.file-hint {
  color: var(--muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.process-button {
  min-height: 50px;
  border-color: var(--navy);
  background: var(--navy);
  color: #fff;
}

.process-button:hover,
.process-button:focus-visible {
  border-color: var(--navy-hover);
  background: var(--navy-hover);
  color: #fff;
}

.process-button:disabled {
  border-color: #8090a0;
  background: #8090a0;
}

.app-message {
  margin-top: 14px;
  padding: 11px 13px;
  border: 1px solid;
  border-radius: 6px;
  font-size: 0.88rem;
  font-weight: 600;
}

.message-info {
  border-color: #bdd7ec;
  background: #eef7fd;
  color: #175b8f;
}

.message-success {
  border-color: #b8dccd;
  background: var(--green-soft);
  color: var(--green);
}

.message-danger {
  border-color: #efc3be;
  background: var(--red-soft);
  color: var(--red);
}

.report-shell {
  margin-top: 22px;
}

.report-tabs-wrap {
  border-bottom: 1px solid var(--line-strong);
}

.report-tabs {
  gap: 6px;
}

.report-tabs .nav-link {
  min-height: 46px;
  padding: 10px 14px;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
}

.report-tabs .nav-link:hover {
  color: var(--navy);
}

.report-tabs .nav-link.active {
  border-bottom-color: var(--teal);
  background: transparent;
  color: var(--ink);
}

.tab-count {
  min-width: 22px;
  display: inline-grid;
  place-items: center;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--red-soft);
  color: var(--red);
  font-size: 0.72rem;
}

.tab-content {
  padding-top: 20px;
}

.empty-state {
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.52);
  color: var(--muted);
  text-align: left;
}

.empty-state-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  color: var(--teal);
  font-size: 0.82rem;
  font-weight: 800;
}

.empty-state h2 {
  margin: 0 0 4px;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 750;
}

.empty-state p {
  margin: 0;
  font-size: 0.86rem;
}

.result-context {
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 0.84rem;
}

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

.metric-card {
  grid-column: span 3;
  min-height: 112px;
  padding: 16px 17px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.03);
}

.metric-card:first-child {
  grid-column: span 6;
}

.metric-label {
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

.metric-value {
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 1.55rem;
  font-weight: 780;
  line-height: 1.2;
}

.metric-card:first-child .metric-value {
  font-size: 1.3rem;
}

.metric-card.metric-positive {
  border-top: 3px solid var(--green);
}

.metric-card.metric-warning {
  border-top: 3px solid #d18400;
}

.metric-card.metric-danger {
  border-top: 3px solid var(--red);
}

.metric-card.metric-neutral {
  border-top: 3px solid #98a2b3;
}

.metric-card.metric-accent {
  border-top: 3px solid var(--teal);
}

.metric-value.text-danger {
  color: var(--red) !important;
}

.anomaly-toolbar {
  align-items: center;
  margin-bottom: 12px;
}

.filter-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}

.filter-button {
  min-height: 34px;
  padding: 5px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: #475467;
  font-size: 0.78rem;
  font-weight: 700;
}

.filter-button:hover,
.filter-button.active {
  border-color: var(--navy);
  background: var(--navy);
  color: #fff;
}

.filter-count {
  margin-left: 5px;
  opacity: 0.74;
}

.table-wrap {
  max-height: calc(100vh - 250px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.anomaly-table {
  min-width: 1540px;
  color: var(--ink);
  font-size: 0.82rem;
}

.anomaly-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 11px 10px;
  border-bottom: 1px solid var(--line-strong);
  background: #f3f6f9;
  color: #475467;
  font-size: 0.75rem;
  font-weight: 800;
  white-space: nowrap;
}

.anomaly-table td {
  padding: 10px;
  border-bottom-color: rgba(152, 162, 179, 0.35);
  vertical-align: top;
}

.anomaly-table tbody tr {
  box-shadow: inset 4px 0 transparent;
}

.anomaly-table tbody tr:hover td {
  filter: brightness(0.985);
}

.anomaly-table td:nth-child(1) {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.anomaly-table td:nth-child(3),
.anomaly-table td:nth-child(4) {
  min-width: 150px;
}

.anomaly-table td:nth-child(11),
.anomaly-table td:nth-child(12) {
  min-width: 260px;
  max-width: 360px;
  white-space: normal;
}

.reason-input {
  display: block;
  width: 100%;
  min-height: 64px;
  padding: 7px 8px;
  border: 1px solid #b8c5d1;
  border-radius: 4px;
  background: rgba(255, 255, 255, .78);
  color: var(--ink);
  font: inherit;
  line-height: 1.45;
  resize: vertical;
}

.reason-input:focus {
  border-color: var(--teal);
  outline: 2px solid rgba(15, 118, 110, .18);
  background: #fff;
}

.amount-negative {
  color: var(--red);
  font-weight: 750;
}

.issue-badge {
  display: inline-flex;
  align-items: center;
  min-height: 25px;
  padding: 3px 7px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 800;
  white-space: nowrap;
}

.issue-red {
  background: #f7c9c5;
  color: #8f1d14;
}

.issue-orange {
  background: #f5d6ae;
  color: #854a00;
}

.issue-yellow {
  background: #f7e9a9;
  color: #6f5700;
}

.issue-grey {
  background: #dfe4e9;
  color: #475467;
}

.issue-green {
  background: #cce7d7;
  color: #11643d;
}

.row-grey {
  box-shadow: inset 4px 0 #98a2b3 !important;
}

.row-grey td {
  background: var(--grey-soft) !important;
}

.row-yellow {
  box-shadow: inset 4px 0 #d2a900 !important;
}

.row-yellow td {
  background: var(--yellow-soft) !important;
}

.row-green {
  box-shadow: inset 4px 0 var(--green) !important;
}

.row-green td {
  background: var(--green-soft) !important;
}

.row-red {
  box-shadow: inset 4px 0 var(--red) !important;
}

.row-red td {
  background: var(--red-soft) !important;
}

.row-orange {
  box-shadow: inset 4px 0 #d27a00 !important;
}

.row-orange td {
  background: var(--orange-soft) !important;
}

.empty-table-row td {
  height: 140px;
  background: #fff !important;
  color: var(--muted);
  text-align: center;
  vertical-align: middle;
}

@media (max-width: 1100px) {
  .metric-card,
  .metric-card:first-child {
    grid-column: span 4;
  }

  .anomaly-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .filter-group {
    justify-content: flex-start;
  }
}

@media (max-width: 760px) {
  .app-container {
    width: min(100% - 24px, 1560px);
  }

  .header-inner {
    min-height: 68px;
  }

  .brand-mark,
  .status-pill {
    display: none;
  }

  .brand-block h1 {
    font-size: 1rem;
  }

  .brand-block p {
    font-size: 0.76rem;
  }

  .export-button {
    min-height: 38px;
    padding-inline: 12px;
    font-size: 0.82rem;
  }

  .app-main {
    padding-block: 16px 32px;
  }

  .upload-panel {
    padding: 16px;
  }

  .upload-form {
    grid-template-columns: 1fr;
  }

  .file-picker {
    grid-template-columns: auto minmax(0, 1fr);
  }

  .file-hint {
    display: none;
  }

  .metric-card,
  .metric-card:first-child {
    grid-column: span 6;
  }

  .metric-card {
    min-height: 102px;
  }

  .metric-value,
  .metric-card:first-child .metric-value {
    font-size: 1.18rem;
  }

  .empty-state {
    min-height: 150px;
    padding: 20px;
  }

  .empty-state-mark {
    display: none;
  }

  .report-tabs .nav-link {
    padding-inline: 10px;
    font-size: 0.86rem;
  }
}

@media (max-width: 480px) {
  .format-note {
    display: none;
  }

  .metric-card,
  .metric-card:first-child {
    grid-column: span 12;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition: none !important;
  }
}

.sop-layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.sop-sidebar {
  position: sticky;
  top: 96px;
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.sop-sidebar-heading h2,
.sop-intro h2 {
  margin: 0;
  color: var(--ink);
  font-size: 1.12rem;
  font-weight: 750;
}

.sop-search-label {
  display: block;
  margin-top: 18px;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 700;
}

.sop-search {
  min-height: 40px;
  border-color: #c8d1dc;
  border-radius: 6px;
  font-size: .84rem;
}

.sop-search:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(25, 122, 104, .12);
}

.sop-result-count {
  min-height: 18px;
  margin: 8px 0 10px;
  color: var(--muted);
  font-size: .75rem;
}

.sop-quick-nav {
  display: grid;
  gap: 3px;
}

.sop-nav-link {
  display: grid;
  gap: 2px;
  width: 100%;
  padding: 8px 9px;
  border: 0;
  border-left: 2px solid transparent;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font-size: .8rem;
  font-weight: 700;
}

.sop-nav-link:hover {
  border-left-color: var(--teal);
  background: var(--teal-soft);
  color: var(--teal);
}

.sop-nav-link span,
.sop-section-kicker {
  color: var(--teal);
  font-size: .66rem;
  font-weight: 800;
  letter-spacing: .08em;
}

.sop-content-column {
  min-width: 0;
}

.sop-intro {
  margin-bottom: 14px;
  padding: 18px 20px;
  border: 1px solid #cfe5df;
  border-radius: 8px;
  background: var(--teal-soft);
}

.sop-intro p:last-child {
  max-width: 780px;
  margin: 8px 0 0;
  color: #46635e;
  font-size: .86rem;
  line-height: 1.7;
}

.sop-body {
  display: grid;
  gap: 10px;
}

.sop-section {
  scroll-margin-top: 100px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.sop-section summary {
  display: grid;
  grid-template-columns: auto minmax(150px, auto) minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 58px;
  padding: 11px 16px;
  cursor: pointer;
  list-style: none;
}

.sop-section summary::-webkit-details-marker {
  display: none;
}

.sop-section summary::after {
  content: "+";
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 1px solid #c8d1dc;
  border-radius: 50%;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
}

.sop-section[open] summary::after {
  content: "−";
}

.sop-section summary:hover {
  background: #fafcfb;
}

.sop-section-title {
  color: var(--ink);
  font-size: .98rem;
  font-weight: 750;
}

.sop-section-summary {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: .8rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sop-section-body {
  padding: 0 20px 16px;
  border-top: 1px solid #edf0f3;
}

.sop-block {
  padding-top: 15px;
}

.sop-block + .sop-block {
  border-top: 1px solid #edf0f3;
}

.sop-block h3 {
  margin: 0 0 8px;
  color: var(--navy);
  font-size: .88rem;
  font-weight: 750;
}

.sop-block ul {
  margin: 0;
  padding-left: 1.2rem;
  color: #344054;
  font-size: .84rem;
  line-height: 1.75;
}

.sop-block li + li {
  margin-top: 3px;
}

.sop-formula {
  display: inline-block;
  max-width: 100%;
  margin-top: 10px;
  padding: 8px 10px;
  overflow-x: auto;
  border: 1px solid #d6e4ec;
  border-radius: 5px;
  background: #f5f9fb;
}

.sop-formula code {
  color: #175b8f;
  font-family: Consolas, "Courier New", monospace;
  font-size: .8rem;
  white-space: nowrap;
}

.sop-note {
  margin: 11px 0 0;
  padding: 9px 11px;
  border-left: 3px solid #d18400;
  background: var(--orange-soft);
  color: #7a4b00;
  font-size: .8rem;
  line-height: 1.65;
}

.sop-no-results {
  display: grid;
  gap: 5px;
  padding: 28px 20px;
  border: 1px dashed #c8d1dc;
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: .84rem;
}

.sop-no-results strong {
  color: var(--ink);
}

@media (max-width: 860px) {
  .upload-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .process-button {
    grid-column: 1 / -1;
  }

  .sop-layout {
    grid-template-columns: 1fr;
  }

  .sop-sidebar {
    position: static;
  }

  .sop-quick-nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 8px;
  }
}

@media (max-width: 560px) {
  .upload-form {
    grid-template-columns: 1fr;
  }

  .process-button {
    grid-column: auto;
  }

  .upload-source-heading {
    align-items: flex-start;
    flex-direction: column;
    gap: 2px;
  }

  .upload-source-hint {
    white-space: normal;
  }

  .sop-intro,
  .sop-section-body {
    padding-inline: 14px;
  }

  .sop-quick-nav {
    grid-template-columns: 1fr;
  }

  .sop-section summary {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 4px 8px;
  }

  .sop-section-kicker {
    grid-column: 1;
  }

  .sop-section-title {
    grid-column: 1;
  }

  .sop-section-summary {
    grid-column: 1;
    white-space: normal;
  }

  .sop-section summary::after {
    grid-column: 2;
    grid-row: 1 / span 3;
  }
}
