:root {
  color-scheme: dark;
  --bg: #101214;
  --panel: #171a1f;
  --panel-2: #1f242b;
  --field: #0c0e12;
  --line: #343a44;
  --line-strong: #4b5563;
  --text: #e8edf2;
  --muted: #9aa5b1;
  --teal: #13a89e;
  --teal-2: #0f766e;
  --green: #16a34a;
  --amber: #f59e0b;
  --red: #dc2626;
  --blue: #2563eb;
}

* { box-sizing: border-box; }

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

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

button {
  cursor: pointer;
}

button:disabled {
  cursor: wait;
  opacity: 0.68;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: #15181d;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 14px;
}

.app-title {
  font-weight: 800;
  font-size: 17px;
}

.app-subtitle {
  color: var(--muted);
  font-weight: 600;
}

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

.tool-btn, .primary-btn, .mini-btn, .export-btn, .wide-btn {
  border: 1px solid var(--line-strong);
  color: var(--text);
  background: var(--panel-2);
  border-radius: 8px;
  min-height: 34px;
  padding: 0 12px;
  font-weight: 700;
  white-space: nowrap;
}

.tool-btn:hover, .primary-btn:hover, .mini-btn:hover {
  border-color: var(--teal);
}

.tool-btn.accent, .wide-btn {
  background: var(--blue);
  border-color: #3b82f6;
}

.hidden-file-input {
  display: none;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(360px, 31%) minmax(520px, 39%) minmax(360px, 30%);
  gap: 0;
}

.panel {
  min-height: 0;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 12px;
}

.right-panel {
  border-right: 0;
}

.panel-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
  background: #15191f;
}

.help-section {
  border-color: #2d4b63;
  background: #121b24;
}

.help-section details {
  margin: 0;
}

.help-section summary {
  cursor: pointer;
  color: #bae6fd;
  font-size: 15px;
  font-weight: 900;
  list-style-position: inside;
}

.help-content {
  margin-top: 10px;
  color: #dbeafe;
  line-height: 1.55;
}

.help-content ol {
  margin: 0;
  padding-left: 22px;
}

.help-content li {
  margin-bottom: 7px;
}

.help-content strong {
  color: #fef3c7;
}

.help-note {
  margin-top: 10px;
  padding: 8px 10px;
  border: 1px solid #334155;
  border-radius: 7px;
  color: #c4b5fd;
  background: #111827;
}

.section-heading {
  color: #7dd3fc;
  font-size: 15px;
  font-weight: 800;
  margin-bottom: 10px;
}

.ai-input {
  width: 100%;
  height: 250px;
  resize: vertical;
}

input, textarea, select {
  width: 100%;
  color: var(--text);
  background: var(--field);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 8px 9px;
  outline: none;
}

textarea {
  resize: vertical;
  line-height: 1.45;
}

input:focus, textarea:focus, select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 2px rgba(19, 168, 158, 0.16);
}

.key-row, .rate-row, .inline-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.key-row input {
  flex: 1;
}

.ai-config-grid {
  display: grid;
  grid-template-columns: 74px 1fr;
  gap: 8px 10px;
  align-items: center;
  margin-top: 10px;
}

.ai-config-grid label {
  color: var(--muted);
  white-space: nowrap;
}

.ai-save-btn {
  grid-column: 2;
}

.inline-row label, .rate-row span {
  width: 74px;
  color: var(--muted);
  white-space: nowrap;
}

.rate-row input {
  width: 110px;
}

.rate-row .primary-btn {
  flex: 1;
}

.segment {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  flex: 1;
}

.segment button {
  min-height: 38px;
  border-radius: 7px;
  border: 1px solid var(--line-strong);
  background: #252a32;
  color: var(--muted);
  font-weight: 800;
}

.segment button.active {
  background: var(--amber);
  color: #111827;
  border-color: #fbbf24;
}

.wide-btn {
  width: 100%;
  margin-top: 10px;
  min-height: 42px;
}

.wide-btn.loading,
.tool-btn.loading,
.primary-btn.loading {
  border-color: #38bdf8;
  background: #1d4ed8;
}

.ai-progress {
  margin-top: 10px;
}

.ai-progress[hidden] {
  display: none;
}

.ai-progress-track {
  height: 9px;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: #080b10;
}

.ai-progress-bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #22c55e, #38bdf8);
  transition: width .28s ease;
}

.ai-progress-text {
  margin-top: 6px;
  color: #bae6fd;
  font-size: 12px;
  line-height: 1.35;
}

.ai-progress.success .ai-progress-bar {
  background: linear-gradient(90deg, #16a34a, #22c55e);
}

.ai-progress.error .ai-progress-bar {
  background: linear-gradient(90deg, #dc2626, #f97316);
}

.ai-progress.error .ai-progress-text {
  color: #fecaca;
}

.file-import-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px;
  gap: 8px;
  align-items: center;
}

.form-grid {
  display: grid;
  grid-template-columns: 118px 1fr;
  gap: 8px 10px;
  align-items: center;
}

.form-grid.compact {
  grid-template-columns: 80px 1fr 80px 1fr 90px 1fr;
}

.form-grid label, .charges-grid label, .extras-grid label {
  color: var(--muted);
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  color: var(--muted);
}

.check-row input {
  width: 16px;
}

.logo-config {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  margin-top: 10px;
}

.logo-preview {
  width: 92px;
  height: 64px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: #080b10;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  color: var(--muted);
  font-weight: 800;
}

.logo-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.logo-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px;
  gap: 8px;
}

.template-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 10px;
  margin-top: 2px;
}

.template-options .check-row {
  margin-top: 8px;
}

.template-save-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.document-bar {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #15191f;
  margin-bottom: 12px;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.table-actions {
  display: flex;
  gap: 6px;
}

.mini-btn {
  min-height: 30px;
  padding: 0 9px;
}

.danger {
  background: #7f1d1d;
  border-color: #991b1b;
}

.table-wrap {
  height: clamp(380px, 44vh, 620px);
  min-height: 340px;
  max-height: 72vh;
  overflow: auto;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #090b0f;
}

.item-table {
  width: 100%;
  min-width: 1220px;
  border-collapse: collapse;
  table-layout: fixed;
}

.item-table th, .item-table td {
  border: 1px solid #2f3540;
  padding: 0;
  vertical-align: middle;
}

.item-table th {
  height: 54px;
  color: #bfdbfe;
  background: #20252d;
  font-weight: 800;
  text-align: center;
}

.item-table tbody tr {
  background: #0c0f14;
  height: 74px;
}

.item-table tbody tr.selected {
  outline: 2px solid var(--teal);
  outline-offset: -2px;
}

.item-table input, .item-table textarea {
  border: 0;
  border-radius: 0;
  min-height: 50px;
  background: transparent;
  box-shadow: none;
  padding: 10px 11px;
}

.item-table textarea {
  min-height: 74px;
  max-height: 220px;
  resize: vertical;
}

.item-table .amount-cell {
  padding: 10px;
  color: #fbbf24;
  font-weight: 800;
  text-align: right;
}

.item-table .image-check {
  display: block;
  padding: 10px 8px;
  color: #93c5fd;
  text-align: center;
}

.col-index {
  width: 44px;
}

.item-table th:nth-child(2) { width: 92px; }
.item-table th:nth-child(3), .item-table th:nth-child(4) { width: 180px; }
.item-table th:nth-child(5) { width: 230px; }
.item-table th:nth-child(6), .item-table th:nth-child(7), .item-table th:nth-child(8), .item-table th:nth-child(9) { width: 112px; }
.item-table th:nth-child(10) { width: 210px; }

.charges-grid, .extras-grid {
  display: grid;
  grid-template-columns: 82px 1fr 82px 1fr;
  gap: 8px 10px;
  align-items: center;
  margin-top: 12px;
}

.charges-grid textarea {
  grid-column: span 3;
}

.bottom-bar {
  position: sticky;
  bottom: -12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  padding: 12px 0 0;
  background: var(--panel);
}

.total-label {
  color: #fbbf24;
  font-size: 20px;
  font-weight: 900;
}

.export-actions {
  display: flex;
  gap: 8px;
}

.export-btn {
  min-height: 42px;
  font-size: 15px;
}

.export-btn.teal { background: var(--teal-2); border-color: var(--teal); }
.export-btn.green { background: var(--green); border-color: #22c55e; }
.export-btn.red { background: var(--red); border-color: #ef4444; }

.tabs {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 12px;
}

.tabs button {
  min-height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px 8px 0 0;
  background: #20252d;
  color: var(--muted);
  font-weight: 800;
}

.tabs button.active {
  background: var(--blue);
  color: #fff;
}

.tab-pane {
  display: none;
}

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

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

.image-box {
  aspect-ratio: 1.85 / 1;
  border: 2px dashed #596270;
  border-radius: 8px;
  background: #080b10;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-align: center;
  overflow: hidden;
  position: relative;
}

.image-box.active {
  border-color: #22d3ee;
}

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

.image-box input {
  display: none;
}

.image-menu-hint {
  position: absolute;
  right: 8px;
  bottom: 7px;
  color: #dbeafe;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.4);
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 11px;
  opacity: 0;
  transition: opacity .12s ease;
}

.image-box:hover .image-menu-hint {
  opacity: 1;
}

.image-actions {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.batch-image-add {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  gap: 6px;
}

.image-actions input,
.image-actions button {
  min-width: 0;
}

.image-box strong {
  color: #bfdbfe;
}

.image-box span {
  display: inline-block;
  max-width: 92%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

.hint-text {
  margin-top: 10px;
  color: var(--muted);
  line-height: 1.45;
}

#previewFrame {
  width: 100%;
  height: calc(100vh - 132px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: #111827;
  color: #f8fafc;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
  z-index: 10;
  max-width: min(520px, calc(100vw - 24px));
}

.toast.show {
  opacity: 1;
}

.context-menu {
  position: fixed;
  z-index: 20;
  display: none;
  min-width: 190px;
  padding: 6px;
  background: #111827;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.38);
}

.context-menu.show {
  display: block;
}

.context-menu button {
  width: 100%;
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #fee2e2;
  text-align: left;
  padding: 0 10px;
  font-weight: 700;
}

.context-menu button:hover {
  background: #7f1d1d;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(3, 7, 18, 0.72);
}

.modal-backdrop[hidden] {
  display: none;
}

.import-modal {
  width: min(1180px, 100%);
  max-height: min(840px, calc(100vh - 36px));
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #15191f;
  padding: 14px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55);
}

.import-modal-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.import-warnings {
  margin-top: 10px;
  color: #fde68a;
  line-height: 1.45;
}

.import-preview-wrap {
  max-height: 430px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 12px;
  background: #090b0f;
}

.import-preview-table {
  width: 100%;
  min-width: 980px;
  border-collapse: collapse;
}

.import-preview-table th,
.import-preview-table td {
  border: 1px solid #2f3540;
  padding: 8px;
  vertical-align: top;
}

.import-preview-table th {
  color: #bfdbfe;
  background: #20252d;
  text-align: center;
}

.import-thumb {
  width: 96px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #080b10;
  color: var(--muted);
}

.import-thumb img,
.candidate-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.candidate-title {
  color: #7dd3fc;
  font-weight: 800;
  margin-top: 12px;
  margin-bottom: 8px;
}

.candidate-image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 10px;
}

.candidate-image {
  min-height: 132px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #090b0f;
  padding: 8px;
  color: var(--muted);
}

.candidate-image img {
  width: 100%;
  height: 78px;
  margin-bottom: 6px;
}

.candidate-image div {
  color: #bfdbfe;
  font-weight: 800;
}

.candidate-image span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.import-modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 14px;
}

@media (max-width: 1280px) {
  .workspace {
    grid-template-columns: 360px minmax(500px, 1fr);
  }
  .right-panel {
    grid-column: 1 / -1;
    border-top: 1px solid var(--line);
  }
  #previewFrame {
    height: 680px;
  }
}

@media (max-width: 840px) {
  .topbar {
    height: auto;
    padding: 10px;
    align-items: flex-start;
    gap: 10px;
  }
  .brand, .top-actions {
    flex-wrap: wrap;
  }
  .workspace {
    display: block;
  }
  .panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .form-grid, .form-grid.compact, .charges-grid, .extras-grid {
    grid-template-columns: 1fr;
  }
  .file-import-row {
    grid-template-columns: 1fr;
  }
  .logo-config,
  .logo-actions,
  .template-options,
  .template-save-row {
    grid-template-columns: 1fr;
  }
  .ai-config-grid {
    grid-template-columns: 1fr;
  }
  .ai-save-btn {
    grid-column: auto;
  }
  .charges-grid textarea {
    grid-column: auto;
  }
  .bottom-bar, .export-actions {
    flex-direction: column;
    align-items: stretch;
  }
}
