:root {
  color-scheme: dark;
  --bg: #0b1017;
  --panel: #151d27;
  --panel-2: #101720;
  --line: #283646;
  --text: #edf4ff;
  --muted: #96a6ba;
  --teal: #5eead4;
  --green: #bef264;
  --blue: #60a5fa;
  --amber: #f59e0b;
  --danger: #fb7185;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(94, 234, 212, 0.10), transparent 260px),
    linear-gradient(90deg, rgba(96, 165, 250, 0.06), transparent 42%),
    var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #202a36;
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled),
button.active {
  border-color: var(--teal);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button.primary {
  border-color: transparent;
  background: var(--green);
  color: #13220c;
  font-weight: 700;
}

input,
select {
  width: 100%;
  height: 38px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
}

input[type="file"] {
  height: auto;
  min-height: 46px;
  padding: 12px;
  border-style: dashed;
  border-color: #3b8791;
  background: rgba(94, 234, 212, 0.08);
}

input[type="range"] {
  padding: 0;
  accent-color: var(--teal);
}

progress {
  width: 100%;
  height: 10px;
  accent-color: var(--teal);
}

.topbar {
  width: min(1280px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 18px 0 14px;
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
}

.topbar h1 {
  margin: 0;
  font-size: 34px;
  letter-spacing: 0;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.tabs {
  display: inline-flex;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(16, 23, 32, 0.84);
  gap: 4px;
}

.tab {
  min-height: 34px;
  white-space: nowrap;
  border-radius: 6px;
  background: transparent;
}

.tab.active {
  background: #223042;
}

main {
  width: min(1280px, calc(100vw - 32px));
  margin: 0 auto;
  padding-bottom: 34px;
}

.tool {
  display: none;
}

.tool.active {
  display: block;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(420px, 1fr) minmax(340px, 430px);
  gap: 16px;
  align-items: start;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(21, 29, 39, 0.94);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
}

.viewer {
  padding: 14px;
}

.video-stage {
  position: relative;
  width: 100%;
  min-height: 360px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid var(--line);
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.05) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.05) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255, 255, 255, 0.05) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255, 255, 255, 0.05) 75%),
    #0d131b;
  background-size: 28px 28px;
  background-position: 0 0, 0 14px, 14px -14px, -14px 0;
}

.video-stage.empty {
  aspect-ratio: 16 / 9;
}

.video-stage:not(.empty) {
  min-height: 0;
}

.video-stage video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: fill;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 15px;
}

.crop-layer {
  position: absolute;
  inset: 0;
  cursor: crosshair;
}

.crop-box {
  position: absolute;
  min-width: 20px;
  min-height: 20px;
  border: 2px solid var(--teal);
  background: rgba(94, 234, 212, 0.08);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.46);
  cursor: move;
  outline: none;
}

.crop-box:focus {
  border-color: var(--amber);
}

.handle {
  position: absolute;
  width: 12px;
  height: 12px;
  border: 2px solid #06131a;
  border-radius: 999px;
  background: var(--teal);
}

.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.e { right: -7px; top: 50%; transform: translateY(-50%); cursor: ew-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; }

.viewer-meta {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}

.controls {
  display: grid;
  gap: 14px;
  padding: 16px;
}

.field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.field b {
  color: var(--text);
}

.wide {
  grid-column: 1 / -1;
}

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

.crop-controls {
  display: grid;
  gap: 10px;
}

.crop-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.action-row button {
  flex: 1 1 160px;
}

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

.summary div {
  min-height: 52px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.035);
}

.summary span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.summary b {
  display: block;
  margin-top: 3px;
  color: var(--text);
  font-size: 14px;
}

.status {
  min-height: 22px;
  color: var(--muted);
  font-size: 13px;
}

.status.warn {
  color: var(--amber);
}

.status.error {
  color: var(--danger);
}

.sprite-workspace {
  grid-template-areas:
    "preview controls"
    "sheet controls";
}

.sprite-viewer {
  grid-area: preview;
  min-height: 560px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.045) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.045) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255, 255, 255, 0.045) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255, 255, 255, 0.045) 75%),
    #0f151d;
  background-size: 28px 28px;
  background-position: 0 0, 0 14px, 14px -14px, -14px 0;
}

.sprite-viewer canvas {
  width: min(480px, 78vw);
  height: min(480px, 78vw);
}

.sheet-panel {
  grid-area: sheet;
  max-height: 420px;
  overflow: auto;
  padding: 12px;
  background: #0b1118;
}

.sheet-panel img {
  display: block;
  max-width: none;
  border-radius: 6px;
}

#spriteTool .controls {
  grid-area: controls;
}

@media (max-width: 960px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .tabs {
    width: 100%;
  }

  .tab {
    flex: 1 1 0;
  }

  .workspace,
  .sprite-workspace {
    grid-template-columns: 1fr;
    grid-template-areas:
      "preview"
      "controls"
      "sheet";
  }

  .video-stage,
  .sprite-viewer {
    min-height: 380px;
  }
}

@media (max-width: 560px) {
  .topbar,
  main {
    width: min(100vw - 20px, 1280px);
  }

  .topbar h1 {
    font-size: 25px;
  }

  .control-grid,
  .crop-grid,
  .summary {
    grid-template-columns: 1fr;
  }
}
