:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --line: #dbe3ef;
  --line-strong: #c8d4e3;
  --text: #172033;
  --muted: #607089;
  --blue: #2563eb;
  --blue-soft: #eaf1ff;
  --green: #14804a;
  --green-soft: #e9f8f0;
  --amber: #b7791f;
  --amber-soft: #fff4dc;
  --red: #d43f3a;
  --red-soft: #fff0ef;
  --purple: #6d5bd0;
  --shadow: 0 18px 45px rgba(21, 38, 66, 0.1);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light;
}

body {
  margin: 0;
  color: var(--text);
  background: linear-gradient(180deg, #f8fbff 0%, var(--bg) 44%, #edf3f9 100%);
  overflow-x: hidden;
}

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

button {
  cursor: pointer;
}

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

.app-shell {
  width: min(1760px, calc(100vw - 16px));
  min-height: calc(100vh - 28px);
  margin: 8px auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.topbar,
.panel,
.sheet-bar,
.table-shell,
.footer-bar,
.report-shell {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  animation: surfaceIn 0.22s ease-out both;
}

@keyframes surfaceIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.topbar {
  padding: 14px 16px;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) auto;
  gap: 14px;
  align-items: center;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.brand-title {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0;
}

.brand-subtitle {
  color: var(--muted);
  font-size: 13px;
}

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

.btn,
.icon-btn,
.chip,
.tab-btn {
  min-height: 32px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  position: relative;
  transition: border-color 0.16s, background-color 0.16s, color 0.16s, transform 0.16s, box-shadow 0.16s;
}

.btn {
  padding: 0 12px;
  font-weight: 650;
}

.btn:hover,
.icon-btn:hover,
.chip:hover,
.tab-btn:hover {
  border-color: #9eb2cc;
  background: #f7faff;
  box-shadow: 0 8px 18px rgba(15, 31, 56, 0.08);
  transform: translateY(-1px);
}

.btn:active,
.icon-btn:active,
.chip:active,
.tab-btn:active {
  transform: translateY(1px);
}

.btn:focus-visible,
.icon-btn:focus-visible,
.chip:focus-visible,
.tab-btn:focus-visible,
.sheet-tab:focus-visible,
.back-top:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.22);
  outline-offset: 2px;
}

.btn.is-loading {
  opacity: 0.9;
}

.btn.is-loading::before {
  content: "";
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 999px;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.btn.primary {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

.btn.success {
  border-color: var(--green);
  background: var(--green);
  color: #fff;
}

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

.btn.ghost {
  background: var(--surface-soft);
}

.icon-btn {
  width: 32px;
  padding: 0;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  gap: 3px;
}

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

.segmented .tab-btn.active {
  background: #fff;
  color: var(--blue);
  box-shadow: 0 1px 6px rgba(30, 64, 175, 0.12);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(140px, 1fr));
  gap: 10px;
}

.stat {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  min-height: 76px;
  transition: transform 0.18s, border-color 0.18s, box-shadow 0.18s;
}

.stat:hover {
  border-color: #b8c7db;
  box-shadow: 0 12px 28px rgba(21, 38, 66, 0.09);
  transform: translateY(-1px);
}

.stat-label {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 7px;
}

.stat-value {
  font-size: 23px;
  font-weight: 750;
  letter-spacing: 0;
  white-space: nowrap;
}

.stat small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
}

.positive {
  color: var(--green);
}

.negative {
  color: var(--red);
}

.panel {
  padding: 12px;
}

.filter-grid {
  display: grid;
  grid-template-columns: 2fr repeat(7, minmax(116px, 1fr)) auto;
  gap: 8px;
  align-items: end;
}

.simple-filter-grid {
  display: grid;
  grid-template-columns: minmax(280px, 1.8fr) minmax(130px, 0.7fr) minmax(150px, 0.7fr) minmax(112px, 0.55fr) minmax(150px, 0.8fr) auto auto;
  gap: 8px;
  align-items: end;
}

.advanced-filter-panel {
  display: grid;
  grid-template-columns: repeat(7, minmax(120px, 1fr)) auto;
  gap: 8px;
  align-items: end;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  animation: panelReveal 0.2s ease-out both;
}

@keyframes panelReveal {
  from {
    opacity: 0;
    transform: translateY(-5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.field input,
.field select {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  color: var(--text);
  background: #fff;
  padding: 0 9px;
  outline: none;
  transition: border-color 0.16s, box-shadow 0.16s, background-color 0.16s;
}

.field input:focus,
.field select:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.quick-row {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 10px;
  align-items: center;
}

.filter-summary {
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
}

.chip {
  min-height: 30px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 13px;
}

.chip.active {
  background: var(--blue-soft);
  border-color: #98b6ff;
  color: #1d4ed8;
  font-weight: 700;
}

.sheet-bar {
  padding: 8px 10px;
  display: flex;
  gap: 8px;
  align-items: center;
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: #94a3b8 #edf2f7;
}

.sheet-tab {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 12px;
  background: #fff;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  transition: background-color 0.16s, border-color 0.16s, color 0.16s, transform 0.16s, box-shadow 0.16s;
}

.sheet-tab:hover {
  box-shadow: 0 8px 18px rgba(15, 31, 56, 0.08);
  transform: translateY(-1px);
}

.sheet-tab.active {
  color: var(--blue);
  border-color: #abc1f7;
  background: var(--blue-soft);
  font-weight: 700;
}

.sheet-tab input {
  width: 140px;
  border: 0;
  border-bottom: 1px solid var(--line-strong);
  outline: none;
  background: transparent;
  color: inherit;
}

.table-shell {
  overflow: hidden;
  min-height: 420px;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
}

.table-scroll {
  overflow: auto;
  max-height: calc(100vh - 374px);
  min-height: 410px;
  scrollbar-width: thin;
  scrollbar-color: #8f99a8 #edf2f7;
  scrollbar-gutter: stable;
}

.table-scroll::-webkit-scrollbar,
.sheet-bar::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.table-scroll::-webkit-scrollbar-track,
.sheet-bar::-webkit-scrollbar-track {
  background: #edf2f7;
  border-radius: 999px;
}

.table-scroll::-webkit-scrollbar-thumb,
.sheet-bar::-webkit-scrollbar-thumb {
  background: #8f99a8;
  border: 2px solid #edf2f7;
  border-radius: 999px;
}

.table-scroll::-webkit-scrollbar-corner {
  background: #edf2f7;
}

table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}

th,
td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  text-align: center;
  white-space: nowrap;
  background: #fff;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 4;
  background: #eef4fb;
  color: #26364f;
}

thead tr.group-row th {
  top: 0;
  height: 34px;
  font-weight: 750;
  color: #41516b;
  background: #e9f0f8;
}

thead tr.column-row th {
  top: 34px;
  height: 42px;
  background: #f8fafc;
}

th:first-child,
td:first-child {
  border-left: 1px solid var(--line);
}

.sticky-left {
  position: sticky;
  left: 0;
  z-index: 5;
  box-shadow: 4px 0 10px rgba(20, 34, 56, 0.06);
}

thead .sticky-left {
  z-index: 7;
}

.sticky-right {
  position: sticky;
  right: 0;
  z-index: 5;
  box-shadow: -4px 0 10px rgba(20, 34, 56, 0.06);
}

thead .sticky-right {
  z-index: 7;
}

tbody tr:hover td {
  background: #f7fbff;
}

tbody tr {
  transition: background-color 0.16s;
}

tbody tr:has(.cell-input:focus) td {
  background: #eef6ff;
}

tbody tr.row-highlight td {
  animation: rowFlash 1.1s ease-out both;
}

@keyframes rowFlash {
  0% {
    background: #ecfdf3;
  }
  60% {
    background: #f2fbf6;
  }
  100% {
    background: #fff;
  }
}

tbody tr.loss-row td {
  background: #fff8f7;
}

.cell-input {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: none;
  background: transparent;
  color: #27364d;
  padding: 0 8px;
  text-align: center;
}

.cell-input.align-left {
  text-align: left;
}

.cell-input.align-right {
  text-align: right;
}

.cell-input:focus {
  background: #fff;
  box-shadow: none;
}

.editable-cell {
  height: 54px;
  padding: 0;
}

.editable-cell:focus-within {
  background: #fff;
  box-shadow: inset 0 0 0 2px rgba(37, 99, 235, 0.5);
}

.cell-editor {
  width: 100%;
  height: 54px;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 0 8px;
}

.computed-cell {
  height: 54px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  font-weight: 750;
}

.image-cell {
  width: 72px;
  height: 62px;
  padding: 5px;
}

.image-box {
  width: 60px;
  height: 52px;
  border: 1px dashed #b7c4d6;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #f8fafc;
  color: var(--muted);
  font-size: 12px;
  margin: 0 auto;
}

.image-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.row-actions {
  min-width: 104px;
  padding: 0 5px;
  display: flex;
  gap: 4px;
  align-items: center;
  justify-content: center;
}

.row-actions .icon-btn {
  width: 28px;
  min-height: 28px;
}

.empty-state {
  padding: 34px;
  color: var(--muted);
  text-align: center;
}

.footer-bar {
  padding: 8px 12px;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.footer-left,
.footer-right {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.footer-right .btn {
  min-height: 34px;
  padding: 0 12px;
}

.status-text {
  color: var(--muted);
  font-size: 13px;
}

.report-shell {
  padding: 14px;
}

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

.report-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.report-title {
  margin: 0;
  padding: 10px 12px;
  font-size: 15px;
  border-bottom: 1px solid var(--line);
  background: #f8fafc;
}

.report-table {
  width: 100%;
  min-width: 0;
  border-collapse: collapse;
}

.report-table th,
.report-table td {
  height: 36px;
  padding: 0 10px;
  text-align: right;
}

.report-table th:first-child,
.report-table td:first-child {
  text-align: left;
}

.toast {
  position: fixed;
  right: 18px;
  top: 18px;
  z-index: 50;
  max-width: 380px;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  box-shadow: var(--shadow);
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 9px;
  animation: toastIn 0.22s ease-out both;
}

.toast-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: var(--blue);
  box-shadow: 0 0 0 5px rgba(37, 99, 235, 0.1);
  flex: none;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateY(-8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.toast.warn {
  border-color: #ffd28a;
  background: var(--amber-soft);
  color: #7a4b00;
}

.toast.warn .toast-dot {
  background: var(--amber);
  box-shadow: 0 0 0 5px rgba(183, 121, 31, 0.12);
}

.toast.error {
  border-color: #f5b4b2;
  background: var(--red-soft);
  color: #8f1f1b;
}

.toast.error .toast-dot {
  background: var(--red);
  box-shadow: 0 0 0 5px rgba(212, 63, 58, 0.12);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(10, 20, 35, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  animation: fadeIn 0.18s ease-out both;
}

.modal img {
  max-width: 92vw;
  max-height: 88vh;
  border-radius: 8px;
  background: #fff;
  animation: zoomIn 0.2s ease-out both;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes zoomIn {
  from {
    opacity: 0;
    transform: scale(0.97);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.hidden-file {
  display: none;
}

.notice {
  border: 1px solid #ffe0a6;
  background: #fff9ed;
  color: #725010;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}

.back-top {
  position: fixed;
  right: 22px;
  bottom: 82px;
  z-index: 120;
  width: 42px;
  height: 42px;
  border: 1px solid #9eb2cc;
  border-radius: 999px;
  background: #ffffff;
  color: var(--blue);
  box-shadow: 0 12px 28px rgba(21, 38, 66, 0.16);
  font-size: 20px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: opacity 0.18s, transform 0.18s, background-color 0.16s, color 0.16s;
}

.back-top.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.back-top:hover {
  background: var(--blue);
  color: #fff;
}

@media (max-width: 1280px) {
  .stat-grid {
    grid-template-columns: repeat(3, minmax(140px, 1fr));
  }

  .filter-grid {
    grid-template-columns: repeat(4, minmax(140px, 1fr));
  }

  .simple-filter-grid,
  .advanced-filter-panel {
    grid-template-columns: repeat(4, minmax(140px, 1fr));
  }
}

@media print {
  body {
    min-width: 0;
    background: #fff;
  }

  .toolbar,
  .filter-grid,
  .quick-row,
  .sheet-bar,
  .footer-bar,
  .row-actions {
    display: none;
  }

  .app-shell {
    width: 100%;
    margin: 0;
  }

  .table-scroll {
    max-height: none;
    overflow: visible;
  }
}
