:root {
  color-scheme: dark;
  --bg: #10131a;
  --panel: #171b24;
  --panel-2: #202633;
  --line: #31394a;
  --text: #eef2ff;
  --muted: #9ca8bf;
  --primary: #6ea8fe;
  --primary-strong: #4f8df7;
  --danger: #ff6b6b;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top left, #202943 0, var(--bg) 42rem);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }

button {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

button:hover:not(:disabled) { transform: translateY(-1px); border-color: var(--primary); }
button:disabled { cursor: not-allowed; opacity: 0.45; }
button.primary { background: linear-gradient(180deg, var(--primary), var(--primary-strong)); color: #06101f; border-color: transparent; font-weight: 700; }
button.danger { color: #ffd6d6; border-color: rgba(255, 107, 107, 0.5); }

input[type="number"], input[type="file"], input[type="color"], input[type="text"] {
  width: 100%;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 10px;
  background: #0f131b;
  color: var(--text);
}

input[type="color"] { height: 42px; padding: 4px; }
input[type="range"] { width: 100%; margin-top: 10px; }
input[type="checkbox"] { width: auto; margin: 0 8px 0 0; }

label {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.check-label {
  display: flex;
  align-items: center;
  min-height: 42px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0f131b;
}

.app-header { padding: 24px; border-bottom: 1px solid rgba(255, 255, 255, 0.06); }
.app-header h1 { margin: 0 0 8px; font-size: clamp(24px, 3vw, 36px); letter-spacing: -0.04em; }
.app-header p { margin: 0; color: var(--muted); }

.app-layout {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(420px, 1fr) minmax(320px, 430px);
  gap: 16px;
  padding: 16px;
}

.panel {
  min-width: 0;
  background: rgba(23, 27, 36, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-title {
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  font-weight: 800;
}

.panel-body { padding: 16px; }
.controls { display: flex; flex-direction: column; gap: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.button-row button { flex: 1 1 auto; }

.nudge-pad {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-areas:
    ". up ."
    "left reset right"
    ". down .";
  gap: 8px;
  width: min(100%, 280px);
  margin: 0 auto 10px;
}

.nudge-pad > button {
  min-width: 0;
  min-height: 44px;
  padding: 10px 8px;
  font-size: 16px;
  line-height: 1;
}

#nudgeUpButton { grid-area: up; }
#nudgeLeftButton { grid-area: left; }
#nudgeResetButton { grid-area: reset; font-size: 12px; line-height: 1.2; }
#nudgeRightButton { grid-area: right; }
#nudgeDownButton { grid-area: down; }

.nudge-pad .button-row { display: contents; }
.nudge-pad .button-row button { flex: none; }

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

.align-row button {
  min-width: 0;
  padding-left: 8px;
  padding-right: 8px;
}

.hint, .status { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.status { min-height: 20px; color: #b8ffcf; }
.center-panel { min-height: 660px; }

.stage-wrap {
  width: 100%;
  height: calc(100vh - 160px);
  min-height: 560px;
  overflow: auto;
  padding: 16px;
  background-image:
    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%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}

.source-canvas { image-rendering: pixelated; border: 1px solid var(--line); background: rgba(0, 0, 0, 0.18); }

.preview-box {
  display: grid;
  place-items: center;
  min-height: 140px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #0f131b;
  background-image:
    linear-gradient(45deg, rgba(255,255,255,0.06) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255,255,255,0.06) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255,255,255,0.06) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255,255,255,0.06) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

#previewCanvas { image-rendering: pixelated; max-width: 100%; }
.sub-title { margin-bottom: 8px; color: var(--muted); font-size: 13px; font-weight: 800; }

.frame-list, .output-list {
  display: grid;
  gap: 8px;
  max-height: 230px;
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(15, 19, 27, 0.86);
}

.fixed-grid-list {
  grid-auto-flow: row;
  align-items: stretch;
}

.frame-card, .output-card {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 44px;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  cursor: pointer;
  user-select: none;
}

.frame-card.selected, .output-card.selected { outline: 2px solid var(--primary); border-color: var(--primary); }
.frame-card.shifted, .output-card.shifted { border-color: #fbbf24; }
.frame-card canvas, .output-card canvas { image-rendering: pixelated; width: 80%; height: 80%; object-fit: contain; }
.frame-index { position: absolute; left: 4px; top: 3px; padding: 1px 4px; border-radius: 999px; background: rgba(0, 0, 0, 0.62); color: #fff; font-size: 10px; }
.drop-target { outline: 2px dashed var(--primary); }

@media (max-width: 1160px) {
  .app-layout { grid-template-columns: 1fr; }
  .stage-wrap { height: auto; }
}

@media (max-width: 520px) {
  .app-header { padding: 16px; }
  .app-layout { padding: 10px; gap: 10px; }
  .panel-body { padding: 12px; }
  .grid-2 { grid-template-columns: 1fr; }
  .button-row { gap: 6px; }
  .button-row button { flex: 1 1 calc(50% - 6px); }
  .frame-list, .output-list {
    gap: 4px;
    padding: 4px;
    max-height: none;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .frame-card, .output-card {
    min-height: 0;
    border-radius: 6px;
  }
  .frame-index {
    left: 2px;
    top: 2px;
    font-size: 8px;
    padding: 0 3px;
  }
  .nudge-pad {
    width: min(100%, 240px);
    gap: 6px;
  }
  .nudge-pad > button {
    min-height: 46px;
    padding: 8px 6px;
  }
  #nudgeResetButton { font-size: 11px; }
  .align-row { grid-template-columns: 1fr; gap: 6px; }
}
