:root {
  color-scheme: dark;
  --bg: #111316;
  --panel: #1a1d22;
  --panel-2: #20242b;
  --line: #323843;
  --text: #f2f4f7;
  --muted: #99a2af;
  --accent: #5fd3b3;
  --accent-2: #8fb8ff;
  --gold: #d59c35;
  --danger: #ff6f7d;
  --shadow: 0 18px 50px rgba(0, 0, 0, .34);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
}

body {
  font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: .45;
}

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.topbar {
  min-height: 76px;
  padding: 16px 24px;
  display: grid;
  grid-template-columns: minmax(210px, 1fr) auto auto;
  gap: 18px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: rgba(17, 19, 22, .92);
  backdrop-filter: blur(12px);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0;
}

.topbar p {
  margin-top: 5px;
  color: var(--muted);
  font-size: 13px;
}

.mode-switch,
.format-row,
.pill-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.mode-switch {
  padding: 4px;
  background: #0d0f12;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.mode-btn,
.pill-row button,
.custom-ratio button {
  min-height: 34px;
  padding: 0 14px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
}

.mode-btn.active,
.pill-row button.active,
.custom-ratio button {
  color: #09110e;
  background: var(--accent);
}

.upload-btn,
.primary-btn,
.ghost-btn,
.icon-btn {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 7px;
  white-space: nowrap;
}

.upload-btn {
  padding: 0 16px;
  background: var(--text);
  color: #111316;
  font-weight: 700;
  cursor: pointer;
}

.upload-btn input {
  display: none;
}

.workspace {
  min-height: 0;
  padding: 18px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: 18px;
}

.stage-panel,
.side-panel,
.bottom-controls {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.stage-panel {
  min-width: 0;
  border-radius: 8px;
  overflow: hidden;
}

.stage {
  position: relative;
  width: 100%;
  height: calc(100vh - 172px);
  min-height: 430px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px),
    linear-gradient(0deg, rgba(255,255,255,.035) 1px, transparent 1px),
    #14171b;
  background-size: 28px 28px;
}

.stage.long-mode {
  display: block;
  overflow: auto;
  padding: 54px 32px 40px;
  scroll-behavior: auto;
  cursor: grab;
  touch-action: none;
}

.stage.long-mode.panning {
  cursor: grabbing;
}

.stage.long-mode .image-wrap {
  display: block;
  width: max-content;
  max-width: none;
  max-height: none;
  margin: 0 auto;
}

.stage.long-mode #previewImage {
  max-width: none;
  max-height: none;
  cursor: grab;
  -webkit-user-drag: none;
}

.stage.long-mode.panning #previewImage {
  cursor: grabbing;
}

.long-ruler {
  position: sticky;
  top: -54px;
  left: 0;
  z-index: 30;
  width: 100%;
  height: 34px;
  margin-bottom: 16px;
  border-bottom: 1px solid #68e5ef;
  background:
    repeating-linear-gradient(90deg, transparent 0 9px, rgba(255,255,255,.34) 9px 10px),
    #252a30;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .38);
  color: #d9fbff;
  cursor: ns-resize;
}

.long-ruler::after {
  content: "";
  position: absolute;
  inset: 17px 0 0;
  background: repeating-linear-gradient(90deg, transparent 0 49px, rgba(255,255,255,.7) 49px 50px);
}

.long-ruler span {
  position: relative;
  z-index: 1;
  display: inline-flex;
  height: 24px;
  margin: 5px 8px;
  padding: 0 8px;
  align-items: center;
  border-radius: 4px;
  background: rgba(18, 21, 25, .88);
  font-size: 11px;
}

.empty-state {
  display: grid;
  gap: 8px;
  text-align: center;
  color: var(--muted);
}

.empty-state strong {
  color: var(--text);
  font-size: 18px;
}

.image-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%;
  max-height: 100%;
  user-select: none;
}

#previewImage {
  display: block;
  max-width: calc(100vw - 430px);
  max-height: calc(100vh - 220px);
  width: auto;
  height: auto;
  object-fit: contain;
}

.grid-overlay,
.grid-inset-overlay,
.saved-overlay,
.long-guide-overlay,
.crop-box {
  position: absolute;
  inset: 0;
}

.long-guide-overlay {
  pointer-events: none;
  overflow: visible;
}

.long-guide {
  position: absolute;
  left: calc(-100vw);
  width: 300vw;
  height: 1px;
  background: #59e4ee;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .18);
  pointer-events: auto;
  cursor: ns-resize;
}

.long-guide::before {
  content: attr(data-label);
  position: absolute;
  left: calc(100vw + 6px);
  top: -24px;
  min-height: 22px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  background: #59e4ee;
  color: #071113;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.grid-overlay {
  pointer-events: none;
  background-image: linear-gradient(to right, rgba(95, 211, 179, .88) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(95, 211, 179, .88) 1px, transparent 1px);
}

.saved-overlay {
  pointer-events: none;
}

.grid-inset-overlay {
  pointer-events: none;
}

.grid-inset-rect {
  position: absolute;
  border: 2px solid #ff4d5d;
  background: rgba(255, 77, 93, .035);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .1);
}

.saved-region {
  position: absolute;
  border: 2px dashed var(--gold);
  background: rgba(213, 156, 53, .08);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .18);
}

.saved-region strong,
.saved-region button {
  position: absolute;
  top: -2px;
  min-height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.saved-region strong {
  left: -2px;
  padding: 0 8px;
  background: var(--gold);
  color: #120f08;
  font-size: 12px;
}

.saved-region button {
  right: -2px;
  width: 24px;
  padding: 0;
  pointer-events: auto;
  background: var(--danger);
  color: #fff;
  border-radius: 0 0 0 5px;
}

.crop-box {
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .48), inset 0 0 0 1px rgba(0, 0, 0, .24);
  cursor: move;
  outline: 0;
}

.crop-box::before,
.crop-box::after {
  content: "";
  position: absolute;
  inset: 33.333% 0 auto 0;
  border-top: 1px solid rgba(255,255,255,.48);
}

.crop-box::after {
  inset: 66.666% 0 auto 0;
}

.crop-size-badge {
  position: absolute;
  right: -2px;
  top: -32px;
  min-height: 26px;
  padding: 0 9px;
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(95, 211, 179, .5);
  border-radius: 6px;
  background: rgba(13, 15, 18, .92);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  pointer-events: none;
}

.handle {
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid #111316;
  border-radius: 50%;
  background: var(--accent);
}

.handle-nw { left: -7px; top: -7px; cursor: nwse-resize; }
.handle-ne { right: -7px; top: -7px; cursor: nesw-resize; }
.handle-sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.handle-se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.handle-n { left: 50%; top: -7px; transform: translateX(-50%); cursor: ns-resize; }
.handle-s { left: 50%; bottom: -7px; transform: translateX(-50%); cursor: ns-resize; }
.handle-w { left: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.handle-e { right: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }

.side-panel {
  min-height: 0;
  border-radius: 8px;
  overflow: auto;
}

.panel-section {
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.section-head {
  min-height: 38px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

h2 {
  font-size: 15px;
  font-weight: 700;
}

.format-row {
  margin-top: 12px;
}

.format-row label {
  min-height: 34px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.quality-control {
  margin-top: 14px;
  display: grid;
  grid-template-columns: auto 1fr 42px;
  gap: 10px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.grid-buttons {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.grid-buttons button,
.primary-btn,
.ghost-btn,
.icon-btn {
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
}

.grid-buttons button {
  min-height: 42px;
  border-radius: 7px;
}

.grid-buttons button.active {
  border-color: var(--accent);
  color: var(--accent);
}

.inset-control {
  margin-top: 14px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 62px auto;
  gap: 10px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.inset-control input[type="number"] {
  min-height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #111316;
  color: var(--text);
}

.inset-control em {
  color: var(--muted);
  font-style: normal;
}

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

.hint.compact {
  margin: -4px 0 12px;
}

.primary-btn {
  width: 100%;
  margin: 12px 0;
  background: var(--accent);
  color: #07110d;
  border-color: transparent;
  font-weight: 700;
}

.secondary-btn {
  width: 100%;
  min-height: 36px;
  margin: -2px 0 12px;
  border: 1px solid rgba(143, 184, 255, .38);
  border-radius: 7px;
  background: rgba(143, 184, 255, .1);
  color: var(--accent-2);
}

.zoom-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.zoom-actions .secondary-btn {
  margin: 0;
}

.zoom-value {
  color: var(--accent);
  font-size: 13px;
}

#longControls strong {
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
}

.ghost-btn,
.icon-btn {
  padding: 0 12px;
}

.selection-list {
  display: grid;
  gap: 10px;
}

.selection-item {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  background: #15181d;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.selection-item.dragging-item {
  opacity: .55;
  border-color: var(--accent);
}

.selection-item img {
  width: 72px;
  height: 54px;
  object-fit: cover;
  border-radius: 5px;
  background: #0d0f12;
}

.selection-meta {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.selection-meta strong {
  font-size: 13px;
}

.name-input {
  min-width: 0;
  width: 100%;
  min-height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #0d0f12;
  color: var(--text);
}

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

.selection-actions {
  display: grid;
  gap: 6px;
}

.selection-actions .delete {
  color: var(--danger);
}

.bottom-controls {
  min-height: 72px;
  margin: 0 18px 18px;
  padding: 14px 260px 14px 18px;
  border-radius: 8px;
  display: flex;
  align-items: center;
}

.control-group {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.control-group > span {
  color: var(--muted);
  font-size: 13px;
}

.bottom-inset {
  min-width: 180px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.bottom-inset input {
  width: 128px;
}

.bottom-inset strong {
  min-width: 42px;
  color: #ff7a86;
  font-size: 13px;
}

.pill-row button {
  border: 1px solid var(--line);
  background: var(--panel-2);
}

.pill-row button.active {
  border-color: transparent;
}

.commerce-row button {
  border-color: rgba(213, 156, 53, .35);
}

.commerce-row button.active {
  background: var(--gold);
  color: #120f08;
}

.custom-ratio {
  display: flex;
  gap: 8px;
  align-items: center;
}

.custom-ratio input {
  width: 110px;
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #111316;
  color: var(--text);
}

.contact-link {
  position: fixed;
  right: 28px;
  bottom: 28px;
  z-index: 18;
  min-height: 42px;
  padding: 0 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(95, 211, 179, .46);
  border-radius: 7px;
  background: rgba(17, 19, 22, .92);
  color: var(--accent);
  box-shadow: 0 12px 36px rgba(0, 0, 0, .42);
  font-weight: 700;
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

@media (max-width: 980px) {
  .topbar,
  .workspace {
    grid-template-columns: 1fr;
  }

  .workspace {
    padding: 12px;
  }

  .stage {
    height: 56vh;
    min-height: 330px;
  }

  #previewImage {
    max-width: calc(100vw - 48px);
    max-height: calc(56vh - 42px);
  }

  .side-panel {
    max-height: none;
  }

  .bottom-controls {
    margin: 0 12px 12px;
    padding-right: 18px;
    align-items: stretch;
    flex-direction: column;
  }

  .contact-link {
    right: 16px;
    bottom: 16px;
    max-width: calc(100vw - 32px);
    white-space: normal;
  }
}
