:root {
  color-scheme: light;
  --bg: #eef0f2;
  --surface: #ffffff;
  --surface-soft: #f4f5f6;
  --surface-muted: #eceff1;
  --line: #d8dde1;
  --line-strong: #bec6cc;
  --text: #111820;
  --muted: #66727e;
  --brand: #b42318;
  --brand-dark: #8f1c14;
  --charcoal: #3e4852;
  --charcoal-dark: #2f3740;
  --success: #287a5d;
  --danger-soft: #fff4f2;
  --focus-ring: rgba(180, 35, 24, 0.16);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  background: var(--bg);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei UI", "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  width: calc(100% - 24px);
  height: calc(100vh - 24px);
  min-height: calc(100vh - 24px);
  margin: 12px auto;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(17, 24, 32, 0.04);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 86px;
  padding: 16px 28px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 14px;
}

.brand-logo {
  width: 52px;
  height: 52px;
  flex: 0 0 52px;
  border-radius: 8px;
}

.brand-copy {
  display: flex;
  min-width: 0;
  align-items: baseline;
  gap: 10px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  overflow: hidden;
  font-size: 22px;
  font-weight: 750;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

h2 {
  font-size: 16px;
  font-weight: 750;
  line-height: 1.35;
}

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

.actions,
.compact-actions,
.panel-head,
.panel-head > div {
  display: flex;
  align-items: center;
}

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

.compact-actions {
  gap: 8px;
}

.button {
  display: inline-flex;
  height: 40px;
  min-width: 96px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  font-weight: 700;
  line-height: 1;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.button input {
  display: none;
}

.button:focus-visible,
.sheet-select:focus-visible,
.range-select:focus-visible,
.mode-button:focus-visible,
.field input:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 1px;
}

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

.button-icon {
  width: 18px;
  font-size: 20px;
  font-weight: 500;
  line-height: 1;
}

.secondary {
  border-color: var(--line-strong);
  background: var(--surface);
}

.secondary:hover {
  border-color: #959fa7;
  background: var(--surface-soft);
}

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

.primary:hover {
  border-color: var(--charcoal-dark);
  background: var(--charcoal-dark);
}

.success {
  border-color: var(--brand);
  background: var(--brand);
  color: #ffffff;
}

.success:hover {
  border-color: var(--brand-dark);
  background: var(--brand-dark);
}

.danger {
  border-color: #efc2bd;
  background: var(--danger-soft);
  color: var(--brand);
}

.danger:hover {
  border-color: #d98c84;
  background: #ffe9e6;
}

.small-button {
  height: 34px;
  min-width: 76px;
  padding: 0 12px;
  border-radius: 6px;
  font-size: 13px;
}

.status-row {
  display: grid;
  grid-template-columns: minmax(280px, auto) minmax(240px, 1fr);
  align-items: center;
  gap: 28px;
  min-height: 66px;
  padding: 12px 28px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
}

.status-copy {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 16px;
}

.status-label {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 13px;
}

#statusText {
  overflow: hidden;
  font-size: 14px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

progress {
  width: 100%;
  height: 7px;
  overflow: hidden;
  border: 0;
  border-radius: 4px;
  background: #dfe3e6;
  accent-color: var(--success);
}

progress::-webkit-progress-bar {
  border-radius: 4px;
  background: #dfe3e6;
}

progress::-webkit-progress-value {
  border-radius: 4px;
  background: var(--success);
}

progress::-moz-progress-bar {
  border-radius: 4px;
  background: var(--success);
}

.panel {
  background: var(--surface);
}

.panel-head {
  min-height: 58px;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
}

.panel-head > div {
  min-width: 0;
  gap: 10px;
}

.section-index {
  display: inline-flex;
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--brand);
  color: #ffffff;
  font-size: 11px;
  font-weight: 750;
}

.source-panel {
  border-bottom: 1px solid var(--line);
}

.source-panel .panel-head {
  min-height: 54px;
  padding: 10px 28px 8px;
}

.source-controls {
  display: grid;
  grid-template-columns: minmax(360px, 1fr) minmax(420px, 1.25fr);
  gap: 12px;
  padding: 0 28px 16px;
}

.control-panel .source-controls {
  grid-template-columns: 1fr;
  gap: 12px;
  padding: 0 20px 16px;
}

.control-panel .control-head {
  align-items: flex-start;
}

.control-panel .file-picker-row {
  align-items: flex-start;
  flex-wrap: wrap;
}

.control-panel .file-badge {
  width: 100%;
  flex-basis: 100%;
  padding-left: 62px;
}

.control-panel .sheet-picker-row {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  align-items: center;
  row-gap: 10px;
}

.control-panel .date-selection,
.control-panel .date-range-controls,
.control-panel .sheet-picker-row > .subtle {
  grid-column: 2;
  width: 100%;
  min-width: 0;
}

.control-panel .sheet-select,
.control-panel .range-select {
  width: 100%;
  min-width: 0;
}

.control-panel .date-range-controls {
  grid-column: 1 / -1;
  flex-wrap: nowrap;
}

.control-panel .range-select {
  min-width: 0;
  flex: 1 1 0;
}

.control-panel .sheet-picker-row > .subtle {
  overflow: visible;
  white-space: normal;
}

.control-panel .editor-head {
  position: static;
  border-top: 0;
}

.control-divider {
  height: 1px;
  margin: 0 20px;
  background: var(--line);
}

.file-picker-row,
.sheet-picker-row {
  display: flex;
  min-width: 0;
  min-height: 48px;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 6px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.sheet-picker-row {
  flex-wrap: wrap;
}

.control-label {
  width: 52px;
  flex: 0 0 52px;
  color: #3f4a54;
  font-size: 13px;
  font-weight: 700;
}

.compact-upload {
  height: 34px;
  min-width: 88px;
  padding: 0 12px;
  border-radius: 6px;
  background: var(--surface);
  font-size: 13px;
}

.file-badge {
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sheet-select {
  width: 180px;
  min-width: 140px;
  height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 0 32px 0 10px;
}

.sheet-select:disabled,
.range-select:disabled {
  border-color: var(--line);
  background: #e9ecef;
  color: #929ba3;
}

.date-mode {
  display: inline-flex;
  flex: 0 0 auto;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
}

.mode-button {
  height: 32px;
  padding: 0 10px;
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.mode-button:last-child {
  border-right: 0;
}

.mode-button.active {
  background: var(--brand);
  color: #ffffff;
}

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

.date-selection,
.date-range-controls {
  display: inline-flex;
  min-width: 0;
  align-items: center;
  gap: 8px;
}

.range-select {
  width: 142px;
  height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 0 28px 0 9px;
}

.range-separator {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
}

.sheet-picker-row .subtle {
  min-width: 120px;
  flex: 1 1 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 2.15fr) minmax(340px, 0.85fr);
  min-height: 0;
  height: auto;
}

.table-panel {
  min-width: 0;
  border-right: 1px solid var(--line);
}

.table-panel,
.editor-panel {
  display: flex;
  min-height: 0;
  flex-direction: column;
}

.row-count {
  display: inline-flex;
  height: 30px;
  min-width: 54px;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  border-left: 3px solid var(--brand);
  background: var(--surface-soft);
  color: #4f5b65;
  font-size: 13px;
  font-weight: 700;
}

.table-wrap {
  min-height: 0;
  flex: 1 1 auto;
  overflow: auto;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
}

th,
td {
  min-width: 88px;
  max-width: 240px;
  padding: 10px 12px;
  border: 0;
  border-right: 1px solid #edf0f2;
  border-bottom: 1px solid #e4e7e9;
  vertical-align: middle;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}

th {
  position: sticky;
  top: 0;
  z-index: 2;
  height: 46px;
  background: var(--surface-muted);
  color: #35414b;
  font-size: 13px;
  font-weight: 750;
  text-align: center;
}

td {
  min-height: 48px;
  background: var(--surface);
  color: #27323b;
  font-size: 13px;
}

th:first-child,
td:first-child {
  min-width: 64px;
}

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

tr.selected td {
  background: #fff0ee;
}

tr.selected td:first-child {
  box-shadow: inset 3px 0 0 var(--brand);
}

td[contenteditable="true"] {
  cursor: text;
}

td[contenteditable="true"]:focus {
  position: relative;
  outline: 2px solid var(--brand);
  outline-offset: -2px;
}

.editor-panel {
  overflow: auto;
  background: #fbfbfc;
}

.editor-head {
  position: sticky;
  top: 0;
  z-index: 3;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.editor-note {
  padding: 12px 20px 0;
  color: var(--muted);
  font-size: 12px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  padding: 16px 20px 24px;
}

.field {
  display: grid;
  min-width: 0;
  gap: 6px;
}

.field label {
  overflow: hidden;
  color: #53606b;
  font-size: 12px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.field input {
  width: 100%;
  height: 38px;
  min-width: 0;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 0 10px;
}

.field input:focus {
  border-color: var(--brand);
  outline: none;
  box-shadow: 0 0 0 3px var(--focus-ring);
}

@media (max-width: 900px) {
  .app-shell {
    height: auto;
  }

  .brand-copy {
    display: block;
  }

  .brand-copy .subtle {
    display: block;
    margin-top: 3px;
  }

  .source-controls {
    grid-template-columns: 1fr;
  }

  .workspace {
    height: auto;
    grid-template-columns: 1fr;
  }

  .table-panel {
    min-height: 520px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .editor-panel {
    max-height: none;
  }

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

@media (max-width: 760px) {
  .app-shell {
    width: 100%;
    height: auto;
    min-height: 100vh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    padding: 14px 16px;
  }

  .brand-logo {
    width: 46px;
    height: 46px;
    flex-basis: 46px;
  }

  h1 {
    font-size: 19px;
    white-space: normal;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .button {
    min-width: 0;
    padding: 0 10px;
  }

  .button-icon {
    display: none;
  }

  .status-row {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 12px 16px;
  }

  .source-panel .panel-head {
    align-items: flex-start;
    padding: 12px 16px 8px;
  }

  .source-panel .panel-head > .subtle {
    display: none;
  }

  .source-controls {
    padding: 0 16px 14px;
  }

  .file-picker-row,
  .sheet-picker-row {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .sheet-select {
    width: calc(100% - 64px);
    flex: 1 1 160px;
  }

  .date-selection,
  .date-range-controls {
    flex: 1 1 calc(100% - 62px);
  }

  .date-selection .sheet-select {
    width: 100%;
  }

  .date-range-controls {
    flex-wrap: wrap;
  }

  .range-select {
    min-width: 120px;
    flex: 1 1 120px;
  }

  .sheet-picker-row .subtle {
    width: 100%;
    padding-left: 62px;
  }

  .panel-head {
    align-items: flex-start;
    min-height: 62px;
    padding: 12px 14px;
  }

  .table-panel {
    min-height: 470px;
  }

  .compact-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

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

  .editor-note {
    padding-left: 14px;
  }
}

@media (max-width: 460px) {
  .actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

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