:root {
  --ink: #0b0f14;
  --panel: #141b23;
  --panel-2: #1c2630;
  --line: #2a3644;
  --paper: #e8eef3;
  --haze: #8b9aab;
  --mint: #3ddb97;
  --mint-dim: #2a9e6e;
  --warn: #ff6b4a;
  --radius: 10px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --font: system-ui, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Consolas, monospace;
  --topbar-h: 52px;
  --tools-w: 64px;
  --props-w: 280px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font);
  font-size: 13px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

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

.app {
  height: 100%;
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr;
}

/* ── Topbar ─────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
  flex-shrink: 0;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--mint), #1fa3ff);
  box-shadow: 0 0 0 1px rgba(61, 219, 151, 0.3), 0 0 18px rgba(61, 219, 151, 0.25);
}

.brand-text {
  font-weight: 700;
  letter-spacing: -0.02em;
  font-size: 15px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.flex-spacer {
  flex: 1;
}

.sep {
  width: 1px;
  height: 18px;
  background: var(--line);
  margin: 0 4px;
}

.zoom-ctrl {
  display: flex;
  align-items: center;
  gap: 4px;
}

.zoom-label {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--haze);
  min-width: 48px;
  text-align: center;
}

/* ── Buttons ────────────────────────────────────── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--paper);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.08s;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: #243040;
  border-color: #3a4b5e;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:focus-visible {
  outline: 2px solid var(--mint);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--mint);
  border-color: transparent;
  color: #062616;
  font-weight: 600;
}

.btn.primary:hover:not(:disabled) {
  background: #4ae8a6;
  border-color: transparent;
}

.btn.primary:disabled {
  background: var(--mint-dim);
  opacity: 0.45;
}

.btn.icon {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn.sm {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 6px;
}

.btn.lg {
  padding: 12px 22px;
  font-size: 15px;
  border-radius: 12px;
}

.btn.danger {
  border-color: rgba(255, 107, 74, 0.45);
  color: #ffb4a1;
}

.btn.danger:hover:not(:disabled) {
  background: rgba(255, 107, 74, 0.12);
  border-color: var(--warn);
}

/* ── Main layout ────────────────────────────────── */
.main {
  display: grid;
  grid-template-columns: var(--tools-w) 1fr var(--props-w);
  min-height: 0;
}

/* ── Tools ──────────────────────────────────────── */
.tools {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 8px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}

.tool {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--haze);
  border-radius: 10px;
  padding: 8px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tool:hover {
  color: var(--paper);
  background: rgba(255, 255, 255, 0.04);
}

.tool:focus-visible {
  outline: 2px solid var(--mint);
  outline-offset: 1px;
}

.tool.active {
  color: var(--mint);
  background: rgba(61, 219, 151, 0.12);
  border-color: rgba(61, 219, 151, 0.35);
}

.tool-ico {
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
}

.tool.active .tool-ico {
  background: rgba(61, 219, 151, 0.18);
}

.tool.accent {
  color: #9fd0ff;
}

.tool.accent .tool-ico {
  background: rgba(31, 163, 255, 0.15);
  font-size: 12px;
  letter-spacing: -0.02em;
}

.tool.accent.active {
  color: #9fd0ff;
  background: rgba(31, 163, 255, 0.12);
  border-color: rgba(31, 163, 255, 0.35);
}

.tool-name {
  font-size: 11px;
  letter-spacing: 0.02em;
}

.ai-preview {
  margin: 8px 0 4px;
  border-radius: 10px;
  border: 1px solid var(--line);
  min-height: 0;
  overflow: hidden;
  background:
    linear-gradient(45deg, #1a222c 25%, transparent 25%),
    linear-gradient(-45deg, #1a222c 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1a222c 75%),
    linear-gradient(-45deg, transparent 75%, #1a222c 75%);
  background-size: 12px 12px;
  background-position: 0 0, 0 6px, 6px -6px, -6px 0;
  background-color: #121820;
}

.ai-preview:empty {
  display: none;
}

.ai-preview img {
  display: block;
  width: 100%;
  height: auto;
}

/* ── Stage ──────────────────────────────────────── */
.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(61, 219, 151, 0.06), transparent 55%),
    var(--ink);
  overflow: hidden;
}

.view {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
}

.view.hidden,
.hidden {
  display: none !important;
}

/* Empty state */
.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background-image:
    linear-gradient(rgba(42, 54, 68, 0.35) 1px, transparent 1px),
    linear-gradient(90deg, rgba(42, 54, 68, 0.35) 1px, transparent 1px);
  background-size: 32px 32px;
}

.empty-card {
  width: min(520px, 100%);
  padding: 36px 32px;
  border-radius: 18px;
  background: rgba(20, 27, 35, 0.92);
  border: 1px solid rgba(61, 219, 151, 0.28);
  box-shadow:
    0 0 0 1px rgba(61, 219, 151, 0.08),
    0 24px 60px rgba(0, 0, 0, 0.45),
    0 0 40px rgba(61, 219, 151, 0.08);
  text-align: center;
}

.empty-badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(61, 219, 151, 0.14);
  color: var(--mint);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  margin-bottom: 14px;
}

.empty-title {
  margin: 0 0 10px;
  font-size: 26px;
  letter-spacing: -0.03em;
  line-height: 1.2;
}

.empty-desc {
  margin: 0 0 22px;
  color: var(--haze);
  font-size: 13px;
  line-height: 1.7;
}

.empty-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}

.empty-hint {
  color: var(--haze);
  font-size: 12px;
}

.empty-features {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
  text-align: left;
  color: var(--haze);
  font-size: 12px;
}

.empty-features li {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line);
}

.empty-features strong {
  color: var(--mint);
  margin-right: 8px;
  font-weight: 600;
}

.stage.dragover::after {
  content: "松开导入图片";
  position: absolute;
  inset: 12px;
  border: 2px dashed var(--mint);
  border-radius: 16px;
  background: rgba(61, 219, 151, 0.08);
  display: grid;
  place-items: center;
  font-size: 18px;
  font-weight: 600;
  color: var(--mint);
  pointer-events: none;
  z-index: 30;
}

.status {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(20, 27, 35, 0.95);
  border: 1px solid var(--line);
  color: var(--paper);
  font-size: 12px;
  z-index: 25;
  box-shadow: var(--shadow);
  max-width: min(90%, 480px);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status.ok {
  border-color: rgba(61, 219, 151, 0.45);
  color: var(--mint);
}

.status.err {
  border-color: rgba(255, 107, 74, 0.5);
  color: #ffb4a1;
}

.status.busy {
  border-color: rgba(31, 163, 255, 0.45);
  color: #9fd0ff;
}

/* Crop overlay */
.crop-overlay {
  position: absolute;
  inset: 0;
  z-index: 15;
  background: rgba(0, 0, 0, 0.45);
}

.crop-rect {
  position: absolute;
  border: 1.5px solid var(--mint);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
  cursor: move;
}

.crop-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--mint);
  border-radius: 2px;
  z-index: 2;
}

.crop-handle.nw { left: -6px; top: -6px; cursor: nwse-resize; }
.crop-handle.n  { left: 50%; top: -6px; margin-left: -6px; cursor: ns-resize; }
.crop-handle.ne { right: -6px; top: -6px; cursor: nesw-resize; }
.crop-handle.e  { right: -6px; top: 50%; margin-top: -6px; cursor: ew-resize; }
.crop-handle.se { right: -6px; bottom: -6px; cursor: nwse-resize; }
.crop-handle.s  { left: 50%; bottom: -6px; margin-left: -6px; cursor: ns-resize; }
.crop-handle.sw { left: -6px; bottom: -6px; cursor: nesw-resize; }
.crop-handle.w  { left: -6px; top: 50%; margin-top: -6px; cursor: ew-resize; }

.crop-actions {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

/* ── Props panel ────────────────────────────────── */
.props {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}

.props-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.props-foot {
  border-top: 1px solid var(--line);
  padding: 10px 14px;
  color: var(--haze);
  font-size: 11px;
  font-family: var(--mono);
}

.panel-title {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--mint);
  letter-spacing: 0.04em;
  text-transform: none;
}

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

.panel-hint {
  margin: 0;
  color: var(--haze);
  font-size: 12px;
  line-height: 1.6;
}

.panel-hint kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--paper);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.field > span {
  color: var(--haze);
  font-size: 12px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field-row .field:last-child:nth-child(odd) {
  grid-column: 1 / -1;
}

.field input[type="text"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  background: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--mint);
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--mint);
}

.field input[type="color"] {
  width: 100%;
  height: 34px;
  padding: 2px;
  background: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.field.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.field.check input {
  accent-color: var(--mint);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.slider-row input[type="range"] {
  flex: 1;
}

.slider-val {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--haze);
  min-width: 36px;
  text-align: right;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn-row .btn {
  flex: 1;
  min-width: 70px;
}

/* ── Modal ──────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
}

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

.modal-head h2 {
  margin: 0;
  font-size: 16px;
  letter-spacing: -0.02em;
}

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

.ai-text-block {
  margin-top: 4px;
  padding: 10px;
  border-radius: 10px;
  border: 1px dashed rgba(61, 219, 151, 0.35);
  background: rgba(61, 219, 151, 0.06);
}

.export-preview {
  margin: 8px 0 4px;
  border-radius: 10px;
  border: 1px solid var(--line);
  min-height: 80px;
  display: grid;
  place-items: center;
  background:
    linear-gradient(45deg, #1a222c 25%, transparent 25%),
    linear-gradient(-45deg, #1a222c 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1a222c 75%),
    linear-gradient(-45deg, transparent 75%, #1a222c 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #121820;
  overflow: hidden;
}

.export-preview img,
.export-preview canvas {
  max-width: 100%;
  max-height: 160px;
  display: block;
}

/* Scrollbars */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-thumb {
  background: #2a3644;
  border-radius: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}

/* ── Responsive ─────────────────────────────────── */
@media (max-width: 900px) {
  :root {
    --tools-w: 56px;
    --props-w: 240px;
  }

  .brand-text {
    display: none;
  }

  .empty-title {
    font-size: 22px;
  }
}

@media (max-width: 720px) {
  .main {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto auto;
  }

  .tools {
    flex-direction: row;
    order: 2;
    border-right: none;
    border-top: 1px solid var(--line);
    padding: 6px;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .tool {
    min-width: 52px;
    padding: 6px 4px;
  }

  .props {
    order: 3;
    max-height: 34vh;
    border-left: none;
    border-top: 1px solid var(--line);
  }

  .stage {
    order: 1;
    min-height: 40vh;
  }

  .zoom-ctrl .btn:not(.icon) {
    display: none;
  }

  #btnPaste {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
