:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-strong: #eef2f7;
  --text: #17202a;
  --muted: #687386;
  --line: #d6dce6;
  --accent: #0f766e;
  --accent-hover: #0b5f59;
  --danger: #b42318;
  --success: #177245;
  --shadow: 0 12px 32px rgba(35, 47, 63, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

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

.app-shell {
  min-height: 100vh;
  padding: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: 0 auto 16px;
  max-width: 1240px;
}

.topbar h1 {
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0;
}

.topbar p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.workspace,
.action-bar,
.status-line,
.summary-grid,
.result-tabs,
.table-wrap {
  max-width: 1240px;
  margin: 0 auto 14px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 0.9fr) minmax(520px, 1.5fr);
  gap: 14px;
}

.drop-zone {
  min-height: 126px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px;
  background: var(--panel);
  border: 1px dashed #9aa8ba;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.drop-zone.is-dragging {
  border-color: var(--accent);
  background: #eefaf7;
}

.drop-icon {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--accent);
  font-size: 28px;
  font-weight: 700;
}

.drop-zone strong,
.drop-zone span {
  display: block;
}

.drop-zone strong {
  margin-bottom: 6px;
  font-size: 16px;
}

.drop-zone span {
  color: var(--muted);
  font-size: 13px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(130px, 1fr));
  gap: 12px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.controls label {
  display: grid;
  gap: 6px;
}

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

select,
input[type="number"] {
  width: 100%;
  min-height: 36px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

.checkbox-row {
  display: flex !important;
  align-items: center;
  gap: 8px !important;
  min-height: 36px;
  margin-top: 18px;
}

.checkbox-row input {
  width: 16px;
  height: 16px;
}

.checkbox-row span {
  color: var(--text) !important;
  font-size: 13px !important;
}

.primary-button,
.secondary-button {
  min-height: 38px;
  padding: 0 16px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 650;
}

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

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

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

.secondary-button:hover {
  background: var(--panel-strong);
}

.action-bar {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.action-bar .primary-button {
  min-width: 180px;
}

.formula-dialog {
  width: min(920px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(24, 32, 44, 0.2);
}

.formula-dialog::backdrop {
  background: rgba(20, 28, 38, 0.36);
}

.dialog-header {
  margin-bottom: 12px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.formula-editor-wrap {
  display: grid;
  gap: 8px;
}

.formula-editor-wrap span {
  color: var(--muted);
  font-size: 12px;
}

#formulaEditor {
  width: 100%;
  min-height: 420px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  color: #17202a;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  line-height: 1.5;
  resize: vertical;
}

.formula-error {
  margin: 10px 0 0;
  min-height: 20px;
  color: var(--danger);
  font-size: 13px;
}

.formula-error:empty {
  display: none;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
}

.status-line {
  min-height: 36px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  border-left: 3px solid #9aa8ba;
  background: var(--panel);
  color: var(--muted);
}

.status-line span[data-kind="error"] {
  color: var(--danger);
}

.status-line span[data-kind="success"] {
  color: var(--success);
}

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

.result-tabs {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.tab-row,
.save-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px;
}

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

.save-row {
  background: #fafbfc;
}

.tab-button,
.save-button {
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
}

.tab-button.is-active {
  border-color: var(--accent);
  background: #eaf7f5;
  color: var(--accent);
  font-weight: 700;
}

.save-button:hover,
.tab-button:hover {
  background: var(--panel-strong);
}

.tab-button.is-active:hover {
  background: #e0f2ef;
}

.metric {
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
}

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

.metric span {
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

.metric strong {
  font-size: 22px;
  line-height: 1.1;
}

.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 320px);
  min-height: 260px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-strong);
  color: #253044;
  font-weight: 700;
}

td {
  color: #2d3748;
}

@media (max-width: 920px) {
  .topbar,
  .workspace {
    display: block;
  }

  .topbar .primary-button,
  .drop-zone {
    margin-top: 12px;
  }

  .controls {
    margin-top: 12px;
    grid-template-columns: 1fr;
  }

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

  .action-bar {
    flex-direction: column;
    justify-content: stretch;
  }

  .action-bar .secondary-button,
  .action-bar .primary-button {
    width: 100%;
  }

  .table-wrap {
    max-height: none;
  }
}
