:root {
  color-scheme: dark;
  --ink: #e8edf4;
  --muted: #aab5c3;
  --faint: #758195;
  --line: #10141b;
  --panel: #242a34;
  --panel-2: #303744;
  --field: #171b22;
  --stage: #15191f;
  --accent: #f4b740;
  --accent-2: #21b7c4;
  --danger: #ef4444;
  --ok: #22c55e;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body.drag-over::after {
  content: "拖入图片即可导入";
  position: fixed;
  z-index: 1000;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 3px dashed var(--accent);
  background: rgba(244, 183, 64, 0.08);
  color: var(--accent);
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 4px;
  pointer-events: none;
  backdrop-filter: blur(2px);
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: var(--stage);
  color: var(--ink);
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
    monospace;
}

button,
textarea,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: #171b22;
}

.topbar {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 14px;
  min-height: 44px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  background: #2b3240;
  z-index: 20;
}

.window-dots {
  display: flex;
  flex: 0 0 auto;
  gap: 6px;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.dot.red {
  background: #ef4444;
}

.dot.amber {
  background: #f59e0b;
}

.dot.green {
  background: #22c55e;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand h1 {
  margin: 0;
  color: #fff;
  font-size: 14px;
  letter-spacing: 0;
  white-space: nowrap;
}

.brand span {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.icon-btn,
.action-btn,
.tool-btn,
.layer {
  min-height: 36px;
  border: 1px solid var(--line);
  background: #202632;
  color: #d4dbe6;
  transition:
    background 0.14s ease,
    color 0.14s ease,
    border-color 0.14s ease;
}

.icon-btn {
  width: 36px;
  padding: 0;
  font-size: 11px;
  font-weight: 800;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 12px;
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
}

.action-btn.full {
  width: 100%;
}

.link-btn {
  margin-top: 6px;
}

.action-btn.primary,
.tool-btn.active,
.action-btn:active,
.action-btn.flash {
  border-color: #d69222;
  background: #3b4250;
  color: var(--accent);
}

.action-btn.flash {
  transition:
    background 0.14s ease,
    border-color 0.14s ease,
    color 0.14s ease;
}

.action-btn.secondary:hover,
.icon-btn:hover,
.tool-btn:hover,
.layer:hover {
  background: #2a313f;
}

.workspace {
  display: grid;
  flex: 1 1 0;
  min-height: 0;
  grid-template-columns: 220px minmax(0, 1fr) 300px;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background: #1f2530;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-2) transparent;
}

.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--panel-2);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: #3d4555;
}

.sidebar.left {
  border-right: 1px solid var(--line);
}

.sidebar.right {
  border-left: 1px solid var(--line);
}

.panel {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
  min-width: 0;
  overflow: hidden; /* 兜底防溢出 */
}

.panel-title {
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
  padding: 7px 10px;
  color: #d5dce7;
  font-size: 12px;
  font-weight: 800;
}

/* 子元素改用 panel 自己的 padding 而非外边距，避免 width:100% 加 margin 后溢出 */
.panel > :not(.panel-title):not(.panel-tabs) {
  margin: 10px;
  max-width: calc(100% - 20px);
  box-sizing: border-box;
}

.panel-tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}

.panel-tab {
  flex: 1;
  min-height: 30px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.panel-tab:hover {
  color: #d5dce7;
}

.panel-tab.active {
  background: var(--panel);
  color: var(--accent);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.palette-pane {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.palette-pane.hidden {
  display: none;
}

.sv-picker {
  width: 100%;
  aspect-ratio: 10 / 7;
  border: 1px solid var(--line);
  cursor: crosshair;
  touch-action: none;
}

.hue-picker {
  position: relative;
  height: 16px;
  border: 1px solid var(--line);
  background: linear-gradient(
    to right,
    hsl(0, 100%, 50%),
    hsl(60, 100%, 50%),
    hsl(120, 100%, 50%),
    hsl(180, 100%, 50%),
    hsl(240, 100%, 50%),
    hsl(300, 100%, 50%),
    hsl(360, 100%, 50%)
  );
  cursor: pointer;
  touch-action: none;
}

.hue-thumb {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 6px;
  margin-left: -3px;
  border: 2px solid #fff;
  background: transparent;
  box-shadow: 0 0 0 1px #000;
  pointer-events: none;
}

.color-inputs {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.color-input-row {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
}

.color-input-row > span {
  width: 28px;
  font-weight: 700;
}

.color-input-row input {
  flex: 1;
  min-width: 0;
  height: 26px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  outline: 0;
  padding: 0 6px;
  font-family: inherit;
  font-size: 11px;
}

.color-input-row input:focus {
  border-color: var(--accent);
}

.rgb-inputs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
}

.rgb-inputs label {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 11px;
}

.rgb-inputs label > span {
  font-weight: 700;
}

.rgb-inputs input {
  width: 100%;
  min-width: 0;
  height: 26px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  outline: 0;
  padding: 0 4px;
  font-family: inherit;
  font-size: 11px;
}

.rgb-inputs input:focus {
  border-color: var(--accent);
}

.preset-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.preset-btn {
  display: block;
  aspect-ratio: 11 / 20;
  min-width: 0;
  border: 1px solid var(--line);
  background: #15191f;
  padding: 3px;
  cursor: pointer;
}

.preset-btn:hover {
  border-color: var(--accent);
}

.preset-thumb {
  display: grid;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  grid-template-rows: repeat(20, minmax(0, 1fr));
  width: 100%;
  height: 100%;
  background: #0b0f17;
  image-rendering: pixelated;
}

.preset-thumb span {
  width: 100%;
  height: 100%;
}

.tool-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.tool-group.primary {
  grid-template-columns: 1fr;
}

.tool-divider {
  height: 1px;
  margin: 2px 0;
  background: var(--line);
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
  padding: 0 8px;
  font-size: 12px;
}

.tool-btn .mark {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  color: var(--accent-2);
}

.tool-btn .mark svg {
  display: block;
  width: 14px;
  height: 14px;
}

.tool-btn.primary-tool {
  min-height: 48px;
  border-color: rgba(244, 183, 64, 0.35);
  background: #2f3645;
  color: #f0e5cb;
  font-size: 13px;
  font-weight: 800;
}

.tool-btn.primary-tool .mark {
  width: 24px;
  height: 24px;
  color: var(--accent);
}

.tool-btn.primary-tool .mark svg {
  width: 16px;
  height: 16px;
}

.tool-btn.primary-tool:hover {
  background: #3a4150;
}

.tool-btn.primary-tool.active {
  border-color: var(--accent);
  background: #3f4756;
  color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.swatch {
  aspect-ratio: 1;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--swatch);
}

.swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px #d69222;
}

.current-color,
.device-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  border: 1px solid var(--line);
  background: var(--field);
  padding: 7px;
  color: #cbd5e1;
  font-size: 12px;
}

#colorChip {
  width: 20px;
  height: 20px;
  border: 1px solid var(--line);
  background: var(--active-color);
}

.layer-list {
  display: grid;
  gap: 5px;
}

.layer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px;
  color: var(--muted);
  font-size: 12px;
  text-align: left;
}

.layer.active {
  border-color: #d69222;
  background: var(--panel-2);
  color: var(--accent);
}

.canvas-column {
  display: flex;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  background: #171b22;
  overflow: hidden;
}

.canvas-toolbar {
  flex: 0 0 auto;
}

.canvas-toolbar,
.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 37px;
  border-bottom: 1px solid var(--line);
  background: #202632;
  padding: 0 12px;
  color: var(--muted);
  font-size: 12px;
}

.canvas-toolbar > div,
.statusbar {
  flex-wrap: wrap;
}

.canvas-toolbar > div {
  display: flex;
  align-items: center;
  gap: 13px;
}

.statusbar {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  border-bottom: 0;
  font-size: 11px;
  z-index: 15;
}

.canvas-stage {
  display: flex;
  flex: 1 1 0;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow: auto;
  padding: 22px;
  background:
    linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    radial-gradient(circle at center, #252b36 0, #171b22 53%, #11151b 100%);
  background-size:
    28px 28px,
    28px 28px,
    auto;
}

.canvas-stage::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.canvas-stage::-webkit-scrollbar-track {
  background: transparent;
}

.canvas-stage::-webkit-scrollbar-thumb {
  background: var(--panel-2);
  border-radius: 4px;
}

.pixel-window {
  width: min(82vw, calc(360px * var(--zoom-scale, 1)));
  border: 1px solid var(--line);
  background: #242a34;
  box-shadow:
    0 0 0 1px #4b5563,
    0 18px 42px rgba(0, 0, 0, 0.35);
  transition: width 0.12s ease;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.zoom-btn {
  width: 22px;
  height: 22px;
  border: 1px solid var(--line);
  background: #202632;
  color: #d4dbe6;
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  padding: 0;
}

.zoom-btn:hover:not(:disabled) {
  background: #2a313f;
  border-color: var(--accent);
  color: var(--accent);
}

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

#zoomDisplay {
  min-width: 56px;
  text-align: center;
  color: var(--ink);
}

.pixel-window-title,
.pixel-window-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 30px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
  padding: 0 10px;
  color: #cbd5e1;
  font-size: 11px;
}

.pixel-window-status {
  border-top: 1px solid var(--line);
  border-bottom: 0;
  color: var(--muted);
}

.grid-wrap {
  padding: 16px;
  background: #151922;
}

.grid-inner {
  position: relative;
  width: 100%;
}

.selection-overlay {
  position: absolute;
  z-index: 5;
  border: 2px dashed var(--accent);
  background: rgba(244, 183, 64, 0.12);
  pointer-events: none;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}

.pixel-grid {
  display: grid;
  aspect-ratio: 11 / 20;
  width: 100%;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  gap: 1px;
  border: 1px solid #0b0f17;
  background: #0b0f17;
  touch-action: none;
  user-select: none;
}

.pixel {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  border: 1px solid #111827;
  background: var(--pixel-bg);
}

.pixel.selected::after {
  position: absolute;
  inset: -2px;
  border: 2px solid var(--accent);
  content: "";
  pointer-events: none;
}

.transparent {
  background:
    linear-gradient(135deg, #2a3140 25%, transparent 25%) 0 0 / 8px 8px,
    linear-gradient(135deg, transparent 75%, #2a3140 75%) 0 0 / 8px 8px,
    linear-gradient(135deg, transparent 25%, #202635 25%, #202635 50%, transparent 50%, transparent 75%, #202635 75%) 0 0 / 8px 8px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes pulse-success {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 6px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -2px;
}

.action-btn:disabled {
  opacity: 0.7;
  cursor: progress;
}

#aiStatus.loading {
  color: var(--accent);
}

#aiStatus.success {
  color: var(--ok);
  animation: pulse-success 0.6s ease;
}

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

.key-dialog,
.history-dialog {
  width: min(92vw, 380px);
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 0;
}

.history-dialog {
  width: min(92vw, 580px);
}

.history-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-close {
  width: 24px;
  height: 24px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.dialog-close:hover {
  color: var(--accent);
}

.history-list {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
}

@media (max-width: 640px) {
  .history-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

.history-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid var(--line);
  background: #15191f;
  padding: 5px;
  cursor: pointer;
  transition: border-color 0.14s ease;
}

.history-item:hover {
  border-color: var(--accent);
}

.history-thumb {
  display: grid;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  grid-template-rows: repeat(20, minmax(0, 1fr));
  aspect-ratio: 11 / 20;
  width: 100%;
  background: #0b0f17;
  image-rendering: pixelated;
}

.history-thumb span {
  width: 100%;
  height: 100%;
}

.history-time {
  color: var(--muted);
  font-size: 10px;
  text-align: center;
  font-weight: 700;
}

.history-empty {
  grid-column: 1 / -1;
  padding: 24px 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.key-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
}

.dialog-title {
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
}

.dialog-hint {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.55;
}

.dialog-hint a {
  color: var(--accent-2);
  text-decoration: none;
}

.dialog-hint a:hover {
  text-decoration: underline;
}

.key-dialog input {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  outline: 0;
  padding: 0 10px;
  font-family: inherit;
  font-size: 12px;
}

.key-dialog input:focus {
  border-color: var(--accent);
}

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

.dialog-actions .action-btn {
  min-width: 64px;
}

textarea {
  width: 100%;
  height: 112px;
  resize: none;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  outline: 0;
  padding: 10px;
  font-size: 12px;
  line-height: 1.5;
}

textarea:focus {
  border-color: var(--accent);
}

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

.preview-box {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  background: var(--field);
}

.mini-preview {
  display: grid;
  width: min(72%, 132px);
  aspect-ratio: 11 / 20;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  overflow: hidden;
  image-rendering: pixelated;
}

.mini-cell {
  background: var(--mini-bg);
}

.brightness-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 11px;
}

.brightness-label {
  flex: 0 0 auto;
  font-weight: 700;
}

.brightness-row input[type="range"] {
  flex: 1;
  min-width: 0;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--line);
  border-radius: 2px;
  outline: 0;
}

.brightness-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.brightness-row input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.brightness-value {
  flex: 0 0 auto;
  min-width: 36px;
  color: var(--accent);
  font-weight: 800;
  text-align: right;
}

.protocol-details {
  margin-top: 8px;
}

.protocol-details > summary {
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  user-select: none;
  padding: 2px 0;
}

.protocol-details > summary::-webkit-details-marker {
  display: none;
}

.protocol-details > summary::before {
  content: "▶";
  display: inline-block;
  margin-right: 6px;
  font-size: 9px;
  transition: transform 0.15s ease;
  transform: translateY(-1px);
}

.protocol-details[open] > summary::before {
  transform: rotate(90deg) translateX(-1px);
}

.protocol-details > summary:hover {
  color: var(--accent);
}

.protocol-details > p {
  margin-top: 4px;
}

.muted,
.faint {
  margin-top: 8px;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.55;
}

.faint {
  margin-top: 4px;
  color: var(--faint);
}

.props {
  display: grid;
  gap: 8px;
  color: #cbd5e1;
  font-size: 12px;
}

.props div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.props strong {
  color: #eef2f7;
}

.mobile-tools {
  display: none;
}

/* ============== 中等屏 (≤1020px) · 三栏压缩 ============== */
@media (max-width: 1020px) {
  .brand span,
  .saveBtn {
    display: none;
  }

  .workspace {
    grid-template-columns: 170px minmax(0, 1fr) 230px;
  }

  .sidebar {
    padding: 8px;
    gap: 8px;
  }

  .tool-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tool-group.primary {
    grid-template-columns: 1fr;
  }

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

  .mobile-tools {
    display: none;
  }
}

/* ============== 窄屏 (≤760px) · 单列堆叠 + 顶/底固定 ============== */
@media (max-width: 760px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 1fr;
    overflow-y: auto;
  }

  /* 单列时取消 sidebar 独立滚动，整个 workspace 滚动 */
  .sidebar {
    overflow: visible;
    min-height: 0;
  }

  .canvas-column {
    overflow: visible;
    order: 1;
  }

  .sidebar.left {
    order: 2;
    border-top: 1px solid var(--line);
    border-right: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }

  .sidebar.right {
    order: 3;
    border-top: 1px solid var(--line);
    border-left: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }

  /* 画布区在单列模式下不再 flex 1，让它有自然高度 + 内部滚动 */
  .canvas-stage {
    flex: 0 0 auto;
    min-height: 360px;
    overflow: visible;
  }

  .canvas-toolbar {
    position: sticky;
    top: 0;
    z-index: 12;
    background: #202632;
  }

  /* 时间轴与状态栏粘底，不被压掉 */
  .timeline-panel {
    position: sticky;
    bottom: 38px; /* 给状态栏留位置 */
    z-index: 35;
    box-shadow: 0 -10px 24px rgba(0, 0, 0, 0.55);
  }

  .statusbar {
    position: sticky;
    bottom: 0;
    z-index: 36;
    background: #202632;
  }

  .tool-group {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

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

@media (max-width: 640px) {
  .topbar {
    gap: 8px;
    padding: 0 8px;
  }

  .window-dots,
  #exportTopBtn {
    display: none;
  }

  .brand h1 {
    font-size: 13px;
  }

  .canvas-stage {
    min-height: 320px;
    padding: 12px;
  }

  .pixel-window {
    width: min(92vw, 320px);
  }

  .sidebar.left,
  .sidebar.right {
    grid-template-columns: 1fr;
  }

  .canvas-toolbar {
    padding: 6px 10px;
    min-height: 32px;
  }

  .statusbar {
    padding: 4px 10px;
    min-height: 28px;
    font-size: 10px;
  }

  .tool-btn {
    min-height: 40px;
  }
}

/* ============== 时间轴 · 逐帧动画 ============== */
.timeline-panel {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  gap: 0;
  border-top: 1px solid var(--line);
  background: #1c2230;
  z-index: 15;
}

.timeline-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 28px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
  padding: 0 12px;
}

.timeline-title {
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1px;
}

.timeline-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.timeline-meta .dot-divider {
  color: var(--faint);
}

#frameFps {
  color: var(--accent-2);
}

.timeline-body {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
}

.timeline-actions,
.timeline-controls {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 5px;
}

.tl-btn {
  min-height: 30px;
  padding: 0 10px;
  font-size: 11px;
  font-weight: 700;
}

.tl-btn.icon {
  width: 30px;
  padding: 0;
}

.timeline-strip-wrap {
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  border: 1px solid var(--line);
  background: #0e1219;
  padding: 6px;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-2) #0e1219;
}

.timeline-strip-wrap::-webkit-scrollbar {
  height: 6px;
}

.timeline-strip-wrap::-webkit-scrollbar-track {
  background: #0e1219;
}

.timeline-strip-wrap::-webkit-scrollbar-thumb {
  background: var(--panel-2);
  border-radius: 3px;
}

.timeline-strip {
  display: flex;
  align-items: stretch;
  gap: 6px;
  min-height: 64px;
}

.tl-frame {
  position: relative;
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--line);
  background: #15191f;
  padding: 3px 3px 2px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
  transition:
    border-color 0.14s ease,
    background 0.14s ease,
    color 0.14s ease;
}

.tl-frame:hover {
  border-color: var(--accent-2);
  color: #d5dce7;
}

.tl-frame.current {
  border-color: var(--accent);
  background: #2a2620;
  color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.tl-frame.playing {
  border-color: var(--accent-2);
  background: #1b2630;
  color: var(--accent-2);
  box-shadow: 0 0 0 1px var(--accent-2);
}

.tl-frame-thumb {
  display: grid;
  grid-template-columns: repeat(11, minmax(0, 1fr));
  grid-template-rows: repeat(20, minmax(0, 1fr));
  width: 28px;
  aspect-ratio: 11 / 20;
  background: #0b0f17;
  image-rendering: pixelated;
  pointer-events: none;
}

.tl-frame-thumb span {
  width: 100%;
  height: 100%;
}

.tl-frame-num {
  display: block;
  pointer-events: none;
}

.delay-input {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.delay-input input {
  width: 60px;
  height: 26px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  outline: 0;
  padding: 0 4px;
  font-family: inherit;
  font-size: 11px;
  text-align: right;
}

.delay-input input:focus {
  border-color: var(--accent);
}

.delay-input .unit {
  color: var(--faint);
}

.loop-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
}

.loop-toggle input {
  accent-color: var(--accent);
  cursor: pointer;
}

.loop-toggle:hover {
  color: var(--accent);
}

#playBtn.playing {
  background: #3b4250;
  border-color: var(--accent-2);
  color: var(--accent-2);
}

/* ============== 内置动效库按钮组 ============== */
.timeline-presets {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  border-bottom: 1px solid var(--line);
  background: #1a1f29;
  padding: 6px 12px;
}

.tl-presets-label {
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1px;
  margin-right: 4px;
}

.anim-preset-btn {
  min-height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line);
  background: #202632;
  color: #d4dbe6;
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  transition:
    background 0.14s ease,
    border-color 0.14s ease,
    color 0.14s ease;
}

.anim-preset-btn:hover {
  background: #2a313f;
  border-color: var(--accent-2);
  color: var(--accent-2);
}

.anim-preset-btn:active {
  background: #3b4250;
  border-color: var(--accent);
  color: var(--accent);
}

/* ============== 响应式优化 ============== */
@media (max-width: 1020px) {
  /* 顶栏改为可换行 */
  .topbar {
    flex-wrap: wrap;
    min-height: 0;
    padding: 6px 10px;
    gap: 8px;
  }

  /* 时间轴垂直堆叠 */
  .timeline-body {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .timeline-actions,
  .timeline-controls {
    flex-wrap: wrap;
    justify-content: center;
    gap: 5px;
  }

  .timeline-strip-wrap {
    order: 0;
    flex-basis: auto;
    margin-top: 0;
    width: 100%;
  }
}

@media (max-width: 640px) {
  /* 顶栏按钮换行 + 紧凑 */
  .top-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 4px;
    margin-left: 0;
  }

  .top-actions .action-btn {
    padding: 0 8px;
    font-size: 11px;
    min-height: 32px;
  }

  .icon-btn {
    width: 32px;
    min-height: 32px;
  }

  /* 时间轴按钮更紧凑 */
  .tl-btn {
    padding: 0 7px;
    font-size: 10px;
    min-height: 28px;
  }

  .tl-btn.icon {
    width: 28px;
  }

  .anim-preset-btn {
    padding: 0 7px;
    font-size: 10px;
    min-height: 26px;
  }

  /* 时间轴头部允许换行 */
  .timeline-head {
    flex-wrap: wrap;
    min-height: 0;
    padding: 4px 10px;
    gap: 6px;
  }

  /* 延时输入更窄 */
  .delay-input input {
    width: 50px;
  }

  /* 移除底部固定的移动工具栏占的空间（已经有左侧栏了） */
  .workspace {
    padding-bottom: 0;
  }

  .mobile-tools {
    position: static;
  }
}

/* 超窄屏：动效按钮只显示 emoji */
@media (max-width: 480px) {
  .anim-preset-btn {
    font-size: 0;
    padding: 0;
    width: 32px;
    min-height: 32px;
  }

  .anim-preset-btn::first-letter {
    font-size: 16px;
  }

  .tl-presets-label {
    width: 100%;
    text-align: left;
    margin-right: 0;
    margin-bottom: 2px;
  }
}
