:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --line: #dfe4ec;
  --line-strong: #cdd4e0;
  --ink: #1c2333;
  --muted: #697386;
  --subtle: #8a93a6;
  --accent: #2f6fed;
  --accent-strong: #2456d8;
  --accent-soft: #e8efff;
  --green: #1c9c62;
  --amber: #b7791f;
  --shadow: 0 18px 40px -24px rgba(24, 39, 75, .35);
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background:
    radial-gradient(1100px 600px at 12% -10%, #ffffff 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #e6ecff 0%, transparent 52%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

/* ---------------- Header ---------------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  background: rgba(255, 255, 255, .84);
  border-bottom: 1px solid rgba(210, 217, 230, .85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, #4d8bff, #2456d8);
  box-shadow: 0 8px 18px -8px rgba(47, 111, 237, .7);
}

.brand-mark svg { width: 22px; height: 22px; }

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.15;
  letter-spacing: .01em;
}

.brand-text p {
  margin: 3px 0 0;
  font-size: 11.5px;
  color: var(--muted);
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #f2f5fa;
  border: 1px solid #e2e7f0;
  color: #556074;
  font-size: 11.5px;
  font-weight: 500;
  white-space: nowrap;
}

.chip-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 3px rgba(28, 156, 98, .12);
}

.chip-ghost {
  background: transparent;
  border-color: #e1e5ee;
  color: #7a8397;
}

/* ---------------- Layout ---------------- */
.app-main {
  max-width: 1560px;
  margin: 0 auto;
  padding: 22px 28px 8px;
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr) 212px;
  gap: 20px;
  align-items: start;
}

/* ---------------- Preview ---------------- */
.preview-pane {
  position: sticky;
  top: 88px;
  min-width: 0;
}

.preview-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.preview-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px;
}

.preview-title {
  font-size: 12px;
  font-weight: 700;
  color: #556074;
  letter-spacing: .08em;
}

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #eaf7f0;
  border: 1px solid #d3eddf;
  color: var(--green);
  font-size: 10.5px;
  font-weight: 700;
}

.live-pill span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
}

.preview-frame {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: min(calc(100vh - 190px), 860px);
  min-height: 420px;
  padding: 10px;
  overflow: hidden;
  border-radius: 12px;
  background: linear-gradient(180deg, #e9edf4, #e1e6ef);
}

#previewCanvas {
  display: block;
  height: 100%;
  width: auto;
  max-width: 100%;
  border-radius: 5px;
  background: #fff;
  box-shadow:
    0 10px 26px -12px rgba(21, 32, 56, .35),
    0 0 0 1px rgba(30, 43, 70, .06);
  opacity: 0;
  transition: opacity .28s ease;
}

.preview-frame.is-ready #previewCanvas { opacity: 1; }

.preview-loader {
  position: absolute;
  inset: 10px;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border-radius: 10px;
  background: linear-gradient(180deg, #e9edf4, #e1e6ef);
  color: var(--muted);
  font-size: 12px;
  transition: opacity .25s ease;
}

.preview-frame.is-ready .preview-loader {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity .25s ease, visibility 0s linear .25s;
}

.spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid #cfd7e6;
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.preview-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 4px 2px;
  color: var(--subtle);
  font-size: 11px;
}

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent-strong);
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
}

.link-btn:hover { text-decoration: underline; }

/* ---------------- Form ---------------- */
.form-pane {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.group {
  margin: 0;
  padding: 18px 20px 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px -26px rgba(24, 39, 75, .35);
}

.group legend {
  padding: 0 8px;
  margin-left: 4px;
  color: #4d586c;
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: .04em;
}

.fields {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 14px 22px;
}

.fields + .fields { margin-top: 14px; }

.field {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.field-label {
  color: #66718a;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 11.5px;
  letter-spacing: .01em;
  white-space: nowrap;
}

.field input,
.field select {
  height: 38px;
  padding: 0 11px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: #fbfcfe;
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}

.field input:hover,
.field select:hover {
  border-color: #b9c2d3;
  background: #fff;
}

.field input:focus,
.field select:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, .14);
}

.field input[readonly] {
  background: #f3f6fb;
  color: #42506a;
  cursor: default;
}

.field input:disabled,
.field select:disabled {
  background: #f2f4f8;
  color: #98a1b3;
  cursor: not-allowed;
}

.w-80 { width: 80px; }
.w-90 { width: 80px; }
.w-100 { width: 100px; }
.w-120 { width: 120px; }
.w-140 { width: 120px; }
.w-190 { width: 170px; }

.group-main { padding-bottom: 22px; }

.stock-panel {
  margin-top: 18px;
  padding: 14px 16px 16px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: linear-gradient(180deg, #fbfcff, #f8faff);
}

.stock-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 12px;
  border-bottom: 1px dashed #e1e6ef;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #4f5b70;
  font-size: 12.5px;
  cursor: pointer;
  user-select: none;
}

.check input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.check-strong span {
  color: #39465e;
  font-weight: 700;
}

.stock-badge {
  padding: 3px 8px;
  border: 1px solid #e1e6ef;
  border-radius: 999px;
  background: #fff;
  color: #7b8599;
  font-size: 10.5px;
  white-space: nowrap;
}

.stock-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px 14px;
  margin-top: 14px;
  transition: opacity .2s ease, filter .2s ease;
}

.stock-grid.is-disabled {
  opacity: .42;
  filter: grayscale(.2);
  pointer-events: none;
}

.field-stack {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}

.field-stack input { width: 100%; }

.cash-fields {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px dashed #e1e6ef;
}

/* ---------------- Action pane ---------------- */
.action-pane {
  position: sticky;
  top: 88px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

.action-card {
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px -26px rgba(24, 39, 75, .35);
}

.check-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 14px;
  border-bottom: 1px dashed #e1e6ef;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 14px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: #fff;
  color: #39465e;
  font-size: 12.8px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, box-shadow .15s ease, transform .06s ease;
}

.btn svg { width: 16px; height: 16px; flex: 0 0 auto; }

.btn:hover {
  background: #f7f9fd;
  border-color: #b9c2d3;
}

.btn:active { transform: translateY(1px); }

.btn-primary {
  border-color: #2a63e6;
  background: linear-gradient(180deg, #3f7bf3, #2a63e6);
  color: #fff;
  box-shadow: 0 10px 20px -12px rgba(47, 111, 237, .85);
}

.btn-primary:hover {
  border-color: #2f6aeb;
  background: linear-gradient(180deg, #4d86f6, #2f6aeb);
}

.btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, .25);
}

.action-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 12px 2px 0;
  color: #7b8599;
  font-size: 11px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
}

.action-status.busy .status-dot {
  background: var(--amber);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

.help-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, .72);
}

.help-card summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  color: #556074;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
}

.help-card summary::-webkit-details-marker { display: none; }

.help-card summary::after {
  content: "▾";
  color: var(--subtle);
  font-size: 10px;
  transition: transform .2s ease;
}

.help-card[open] summary::after { transform: rotate(180deg); }

.help-body {
  padding: 0 14px 14px;
  color: #6d778c;
  font-size: 11.5px;
  line-height: 1.75;
}

.help-body ul {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding-left: 16px;
}

.help-body code {
  padding: 1px 4px;
  border-radius: 4px;
  background: #f1f4fa;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 10.5px;
}

.reset-btn {
  margin-top: 10px;
  color: #7b8599;
}

.reset-btn:hover { color: var(--accent-strong); }

/* ---------------- Footer / toast / viewer ---------------- */
.app-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 1560px;
  margin: 0 auto;
  padding: 16px 28px 32px;
  color: var(--subtle);
  font-size: 11px;
}

.footer-sep { opacity: .6; }

.build-tag {
  color: #9aa3b5;
  font-variant-numeric: tabular-nums;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  z-index: 80;
  max-width: min(92vw, 560px);
  padding: 10px 16px;
  border-radius: 999px;
  background: #1f2937;
  color: #fff;
  font-size: 12.5px;
  text-align: center;
  box-shadow: 0 18px 40px -18px rgba(0, 0, 0, .6);
  opacity: 0;
  transform: translate(-50%, 16px);
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.warn { background: #8a5a12; }

.viewer {
  width: min(94vw, 760px);
  max-height: 92vh;
  padding: 0;
  border: none;
  border-radius: 16px;
  background: #0f172a;
  color: #fff;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .7);
}

.viewer::backdrop {
  background: rgba(10, 16, 30, .6);
  backdrop-filter: blur(3px);
}

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, .08);
  color: #cbd5e1;
  font-size: 12px;
}

.viewer-close {
  padding: 0 4px;
  border: none;
  background: transparent;
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.viewer-scroll {
  display: flex;
  justify-content: center;
  max-height: calc(92vh - 48px);
  padding: 14px;
  overflow: auto;
}

.viewer-scroll img {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

/* ---------------- Responsive ---------------- */
@media (max-width: 1180px) {
  .app-main {
    grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  }

  .action-pane {
    position: static;
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    align-items: start;
  }

  .action-buttons {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .btn { flex: 1 1 140px; width: auto; }
}

@media (max-width: 1024px) {
  .app-main { grid-template-columns: 1fr; }

  .preview-pane { position: static; }

  .preview-frame { height: min(62vh, 680px); }

  .action-pane { grid-template-columns: 1fr; }

  .stock-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 680px) {
  .app-header { padding: 12px 16px; }
  .brand-text p { display: none; }
  .header-meta .chip-ghost { display: none; }
  .app-main { padding: 14px 12px 0; gap: 14px; }
  .preview-frame { height: 54vh; min-height: 380px; }
  .group { padding: 14px; }
  .fields { gap: 10px 12px; }
  .field { flex: 1 1 100%; }
  .field input, .field select { flex: 1 1 auto; width: auto; }
  .w-80, .w-90, .w-100, .w-120, .w-140, .w-190 { width: auto; }
  .stock-grid { grid-template-columns: 1fr; }
  .action-buttons { flex-direction: column; }
  .app-footer { padding: 14px 16px 24px; }
}
