:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --panel: #ffffff;
  --panel-soft: #f3f4f6;
  --ink: #1b2421;
  --muted: #667078;
  --line: #dfe3e8;
  --accent: #ff5a00;
  --accent-dark: #d84b00;
  --warn: #a65f00;
  --bad: #a73636;
  --blue: #286a9b;
  --shadow: 0 16px 40px rgba(32, 48, 42, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", "Segoe UI", Arial, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button,
.file-button {
  min-height: 38px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 0 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

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

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

button.danger-action {
  border-color: #e5caca;
  color: var(--bad);
}

button.danger-action:hover {
  background: #fff3f3;
}

input,
select {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 6px 9px;
}

input:disabled {
  background: #edf1ef;
  color: #87928e;
}

.app-shell {
  width: min(1600px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar,
.settings-panel,
.input-panel,
.preview-panel,
.results-panel,
.stack-panel,
.single-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 22px 24px;
}

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

.brand-logo {
  width: 172px;
  height: auto;
  display: block;
  flex: 0 0 auto;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.2;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
  line-height: 1.3;
  letter-spacing: 0;
}

.topbar p {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.6;
}

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.file-button input {
  display: none;
}

.app-tabs {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

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

.tab-button:hover {
  background: #fff7f0;
  color: var(--accent-dark);
}

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

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.settings-panel {
  margin-top: 14px;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(5, minmax(112px, 1.1fr)) repeat(3, minmax(88px, 0.75fr)) minmax(112px, 1fr);
  gap: 10px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.settings-panel .setting-compact {
  min-width: 88px;
}

.workspace-grid {
  margin-top: 14px;
  display: grid;
  grid-template-columns: minmax(780px, 1fr) minmax(280px, 320px);
  gap: 14px;
}

.input-panel,
.preview-panel,
.results-panel,
.stack-panel,
.single-panel {
  min-width: 0;
  padding: 16px;
}

.results-panel,
.stack-panel,
.single-panel {
  margin-top: 14px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 920px;
}

.entry-table {
  min-width: 1080px;
  table-layout: fixed;
}

.single-entry-table {
  min-width: 1160px;
  table-layout: fixed;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 9px;
  text-align: left;
  vertical-align: middle;
  font-size: 13px;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #fff1e8;
  color: #41504b;
  font-weight: 700;
}

.entry-table th[colspan] {
  text-align: center;
}

.single-entry-table th[colspan] {
  text-align: center;
}

tr:last-child td {
  border-bottom: 0;
}

.selected-row td {
  background: #fff7f0;
}

.selected-row input {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(255, 90, 0, 0.12);
}

.tail-result-row td {
  background: #fffafa;
}

.split-value {
  color: #d90000;
  font-weight: 700;
}

td input {
  min-width: 0;
}

.model-input {
  min-width: 0;
}

.entry-model-col {
  width: 104px;
}

.entry-qty-col {
  width: 86px;
}

.entry-small-col {
  width: 70px;
}

.entry-dim-col {
  width: 68px;
}

.entry-upper-col {
  width: 76px;
}

.entry-pallet-col {
  width: 76px;
}

.entry-pallet-mode-col {
  width: 150px;
}

.entry-limit-col {
  width: 82px;
}

.entry-action-col {
  width: 44px;
}

.icon-button {
  width: 34px;
  min-height: 34px;
  padding: 0;
  background: #fff;
  color: var(--bad);
  border-color: #e5caca;
}

.icon-button:hover {
  background: #fff3f3;
}

.hint {
  color: var(--muted);
  font-size: 13px;
  margin-top: 10px;
  line-height: 1.5;
}

.compact-hint {
  margin-top: 6px;
}

.auto-pallet-rule {
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
}

.single-height-warning {
  color: var(--accent);
  font-weight: 700;
}

.single-settings-panel {
  display: grid;
  grid-template-columns: minmax(300px, 1.3fr) repeat(5, minmax(120px, 1fr)) auto;
  align-items: end;
  gap: 12px;
}

.single-results {
  display: grid;
  gap: 14px;
}

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

.single-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px;
  background: #fbfdfc;
  border-bottom: 1px solid var(--line);
}

.single-result-title {
  font-weight: 700;
}

.single-result-meta {
  color: var(--muted);
  font-size: 13px;
}

.single-result-table {
  min-width: 1180px;
}

.single-result-table th {
  background: #eef5f2;
}

.single-detail-row td {
  background: #fbfdfc;
  color: var(--muted);
}

.single-detail-row td:first-child {
  color: var(--accent-dark);
  font-weight: 700;
  padding-left: 20px;
}

.single-error {
  padding: 12px;
  color: var(--bad);
  background: #fff7f7;
}

.result-status {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  border: 1px solid #cfe4d9;
  background: #effaf4;
  color: #246348;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 700;
}

.result-status.warn {
  border-color: #f1d1a8;
  background: #fff7eb;
  color: var(--warn);
}

#palletCanvas {
  width: 100%;
  aspect-ratio: 1.2;
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #f8fbfa 0%, #edf3f1 100%);
}

.preview-choice {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 13px;
}

.preview-choice select {
  width: 100%;
  min-height: 36px;
}

.preview-meta {
  margin-top: 10px;
  min-height: 44px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.status-ok {
  color: var(--accent-dark);
  font-weight: 700;
}

.status-warn {
  color: var(--warn);
  font-weight: 700;
}

.status-bad {
  color: var(--bad);
  font-weight: 700;
}

.summary-chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  color: var(--muted);
  background: #fbfdfc;
  font-size: 13px;
}

.stack-groups {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 10px;
}

.stack-card {
  border: 1px solid var(--line);
  border-left: 5px solid var(--accent);
  border-radius: 8px;
  background: #fff;
  padding: 12px;
}

.stack-card.single {
  border-left-color: var(--warn);
}

.stack-title {
  font-weight: 700;
  margin-bottom: 6px;
}

.stack-detail {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.stack-fill {
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: #e7eeeb;
  overflow: hidden;
  margin-top: 10px;
}

.stack-fill span {
  display: block;
  height: 100%;
  background: var(--accent);
}

.app-credit {
  margin-top: 14px;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.preview-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 28px;
}

.preview-modal.open {
  display: flex;
}

.preview-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 24, 28, 0.58);
}

.preview-modal-content {
  position: relative;
  width: min(1100px, calc(100vw - 56px));
  max-height: calc(100vh - 56px);
  overflow: auto;
  background: #fff;
  border-radius: 8px;
  border: 1px solid var(--line);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.25);
  padding: 16px;
}

.preview-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

#largePalletCanvas {
  width: 100%;
  aspect-ratio: 1.46;
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #f8fbfa 0%, #edf3f1 100%);
}

@media (max-width: 1180px) {
  .settings-panel {
    grid-template-columns: repeat(4, minmax(140px, 1fr));
  }

  .single-settings-panel {
    grid-template-columns: repeat(3, minmax(150px, 1fr));
  }

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

@media (max-width: 720px) {
  .app-shell {
    width: min(100vw - 20px, 1600px);
    padding-top: 10px;
  }

  .topbar,
  .top-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .brand-title {
    align-items: flex-start;
    flex-direction: column;
  }

  .brand-logo {
    width: 148px;
  }

  .settings-panel {
    grid-template-columns: 1fr 1fr;
  }

  .app-tabs,
  .panel-head,
  .panel-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .single-settings-panel {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 23px;
  }

  .app-credit {
    text-align: center;
  }
}
