:root {
  --primary: #7C3AED;
  --primary-hover: #6D28D9;
  --on-primary: #FFFFFF;
  --secondary: #8B5CF6;
  --accent: #0891B2;
  --accent-hover: #0E7490;
  --bg: #FAFAF9;
  --surface: #FFFFFF;
  --fg: #1C1917;
  --fg-muted: #6B6560;
  --muted: #F4F1F8;
  --border: rgba(28, 25, 23, 0.10);
  --destructive: #DC2626;
  --ring: #7C3AED;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(28, 25, 23, 0.08), 0 4px 16px rgba(124, 58, 237, 0.06);
  --font-display: "Fredoka", "Nunito", system-ui, sans-serif;
  --font-body: "Nunito", system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; }

/* ── 顶栏 ─────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 18px;
  color: var(--primary);
}
.brand-icon { width: 30px; height: 30px; border-radius: 50%; box-shadow: 0 0 0 2px var(--primary); }
.tabs { display: flex; gap: 4px; background: var(--muted); border-radius: 999px; padding: 4px; }
.tab {
  border: none;
  background: transparent;
  padding: 7px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  color: var(--fg-muted);
  transition: all 0.2s;
}
.tab:hover { color: var(--fg); }
.tab.active { background: var(--primary); color: var(--on-primary); }

/* ── 页面 ─────────────────────────── */
.page { display: none; flex: 1; min-height: 0; overflow: auto; }
.page.active { display: block; }
#page-editor.active { display: flex; flex-direction: column; }

/* ── 空状态 ───────────────────────── */
.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.drop-zone {
  width: min(560px, 100%);
  border: 2px dashed rgba(124, 58, 237, 0.35);
  border-radius: 20px;
  background: var(--surface);
  padding: 64px 40px;
  text-align: center;
  color: var(--fg-muted);
  transition: all 0.2s;
}
.drop-zone.dragover { border-color: var(--primary); background: var(--muted); }
.drop-zone svg { color: var(--primary); margin-bottom: 16px; }
.drop-zone h2 { font-family: var(--font-display); font-size: 26px; color: var(--fg); margin-bottom: 10px; }
.drop-zone p { font-size: 14px; line-height: 1.7; margin-bottom: 24px; }

/* ── 按钮 ─────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 700;
  transition: all 0.2s;
  text-decoration: none;
}
.btn:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-hover); }
.btn-ghost { background: var(--muted); color: var(--fg); }
.btn-ghost:hover { background: #EAE4F2; }
.btn-ghost.danger { color: var(--destructive); }
.btn-block { width: 100%; margin-top: 8px; }
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--muted);
  color: var(--fg);
  transition: all 0.2s;
}
.icon-btn:hover { background: #EAE4F2; color: var(--primary); }

/* ── 工作区布局 ───────────────────── */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 1fr 280px;
  gap: 0;
  min-height: 0;
}
.panel {
  background: var(--surface);
  padding: 16px;
  overflow-y: auto;
}
.panel-left { border-right: 1px solid var(--border); }
.panel-right { border-left: 1px solid var(--border); }
.panel-title {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  margin-bottom: 10px;
}

/* 槽位列表 */
.slot-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.slot-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: var(--muted);
  border: 2px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 14px;
  font-weight: 600;
}
.slot-item:hover { border-color: rgba(124, 58, 237, 0.3); }
.slot-item.active { border-color: var(--primary); background: #EFE9FA; }
.slot-item .dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.slot-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot-item .del {
  border: none; background: transparent; color: var(--fg-muted);
  width: 22px; height: 22px; border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
}
.slot-item .del:hover { color: var(--destructive); background: rgba(220, 38, 38, 0.1); }

.avatar-test img {
  display: block;
  width: 72px; height: 72px;
  border-radius: 50%;
  object-fit: cover;
  margin: 0 auto 8px;
  border: 2px solid var(--border);
}
.gif-info { font-size: 13px; color: var(--fg-muted); line-height: 1.8; }

/* ── 中央画布 ─────────────────────── */
.center-col { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 20px;
  background:
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%),
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%);
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px;
  background-color: #F5F3F0;
}
#editor-canvas {
  max-width: 100%;
  max-height: 100%;
  background: transparent;
  box-shadow: var(--shadow);
  touch-action: none;
}

/* 播放条 */
.playbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.frame-label { font-size: 13px; font-weight: 700; color: var(--fg-muted); min-width: 80px; }
.playbar-right { margin-left: auto; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; color: var(--fg-muted); cursor: pointer; }
.check input { accent-color: var(--primary); }

/* 时间轴 */
.timeline {
  display: flex;
  gap: 6px;
  padding: 10px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
  min-height: 86px;
}
.tl-frame {
  position: relative;
  flex-shrink: 0;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  background: #fff;
  padding: 0;
  transition: border-color 0.15s;
}
.tl-frame:hover { border-color: var(--secondary); }
.tl-frame.active { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.25); }
.tl-frame canvas { width: 100%; height: 100%; display: block; }
.tl-frame .idx {
  position: absolute; left: 3px; top: 2px;
  font-size: 10px; font-weight: 700;
  color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}
.tl-frame .kf-mark {
  position: absolute; right: 3px; bottom: 3px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  border: 1.5px solid #fff;
  display: none;
}
.tl-frame.has-kf .kf-mark { display: block; }

/* ── 属性面板 ─────────────────────── */
.prop-empty { font-size: 13px; color: var(--fg-muted); padding: 12px 0; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; font-weight: 700; color: var(--fg-muted); margin-bottom: 5px; }
.field input[type="text"], .field input[type="number"] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
  color: var(--fg);
}
.field input:focus { outline: 2px solid var(--ring); outline-offset: -1px; border-color: transparent; }
.field input[type="range"] { width: 100%; accent-color: var(--primary); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg { display: flex; background: var(--muted); border-radius: var(--radius-sm); padding: 3px; gap: 3px; }
.seg button {
  flex: 1;
  border: none;
  background: transparent;
  padding: 6px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--fg-muted);
  transition: all 0.15s;
}
.seg button.active { background: var(--primary); color: #fff; }
.divider { height: 1px; background: var(--border); margin: 16px 0; }
.kf-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.kf-btns .btn { padding: 8px 6px; font-size: 12px; }
.hint { font-size: 12px; color: var(--fg-muted); line-height: 1.6; margin-top: 8px; }

/* ── 模板库 ───────────────────────── */
#page-library { padding: 32px; }
.lib-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; flex-wrap: wrap; gap: 12px; }
.lib-head h1 { font-family: var(--font-display); font-size: 28px; }
.lib-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.lib-seg { width: 140px; }
.api-input {
  width: 220px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: var(--surface);
  color: var(--fg);
}
.api-input:focus { outline: 2px solid var(--ring); outline-offset: -1px; }
.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}
.lib-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s;
}
.lib-card:hover { transform: translateY(-3px); box-shadow: 0 6px 24px rgba(124, 58, 237, 0.15); }
.lib-card .thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  background:
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%),
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
  background-color: #F5F3F0;
  display: block;
}
.lib-card .meta { padding: 12px 14px; }
.lib-card .meta .name { font-weight: 700; font-size: 15px; margin-bottom: 2px; }
.lib-card .meta .sub { font-size: 12px; color: var(--fg-muted); }
.lib-card .card-btns { display: flex; gap: 6px; padding: 0 14px 14px; }
.lib-card .card-btns .btn { flex: 1; padding: 7px 4px; font-size: 12px; }
.lib-empty { text-align: center; color: var(--fg-muted); padding: 80px 0; font-size: 15px; }

/* ── 弹窗 ─────────────────────────── */
.modal-mask {
  position: fixed; inset: 0;
  background: rgba(28, 25, 23, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  width: min(440px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 0;
}
.modal-head h2 { font-family: var(--font-display); font-size: 20px; }
.modal-body { padding: 16px 20px 20px; }
.use-preview {
  display: flex; align-items: center; justify-content: center;
  background:
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%),
    linear-gradient(45deg, #EDEAE6 25%, transparent 25%, transparent 75%, #EDEAE6 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
  background-color: #F5F3F0;
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 14px;
}
.use-preview img { max-width: 100%; max-height: 260px; }
.use-slot-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px;
  background: var(--muted);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
}
.use-slot-row img {
  width: 48px; height: 48px;
  border-radius: 50%;
  object-fit: cover;
  background: #fff;
  border: 1px solid var(--border);
}
.use-slot-row .info { flex: 1; font-size: 14px; font-weight: 700; }
.use-slot-row .btn { padding: 7px 12px; font-size: 12px; }

/* ── API 调用弹窗 ───────────────────────── */
.api-row { margin-bottom: 14px; }
.api-row-label { font-size: 13px; font-weight: 700; margin-bottom: 6px; }
.api-row-line { display: flex; gap: 6px; align-items: stretch; }
.api-code {
  flex: 1;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 12px;
  background: var(--muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  word-break: break-all;
  white-space: pre-wrap;
}
.api-row-line .btn { padding: 6px 12px; font-size: 12px; flex-shrink: 0; align-self: center; }

/* ── 响应式 ───────────────────────── */
@media (max-width: 1024px) {
  .workspace { grid-template-columns: 200px 1fr 240px; }
}

/* 移动端：画布置顶（固定高度），播放条/时间轴紧随，面板纵向堆叠，整页滚动 */
@media (max-width: 768px) {
  body { overflow: auto; }
  .topbar { padding: 0 12px; height: 52px; gap: 8px; }
  .brand { font-size: 15px; gap: 6px; }
  .brand-icon { width: 24px; height: 24px; }
  .tab { padding: 6px 12px; font-size: 13px; white-space: nowrap; }

  #page-editor.active { display: block; overflow-y: auto; }
  .workspace {
    display: flex;
    flex-direction: column;
    min-height: auto;
  }
  .center-col { order: 0; }
  .panel-left { order: 1; border-right: none; border-top: 1px solid var(--border); }
  .panel-right { order: 2; border-left: none; border-top: 1px solid var(--border); }
  .panel { overflow: visible; padding: 14px; }

  .canvas-wrap {
    flex: none;
    height: 42vh;
    min-height: 240px;
    padding: 12px;
  }
  .playbar { padding: 8px 12px; position: sticky; top: 0; z-index: 5; }
  .timeline { min-height: 72px; padding: 8px 12px; }
  .tl-frame { width: 48px; height: 48px; }

  .kf-btns .btn { padding: 10px 6px; }
  .btn { padding: 11px 20px; }
  .icon-btn { width: 38px; height: 38px; }

  .empty-state { padding: 20px; }
  .drop-zone { padding: 40px 20px; }
  .drop-zone h2 { font-size: 20px; }

  #page-library { padding: 16px; }
  .lib-head { margin-bottom: 16px; }
  .lib-head h1 { font-size: 22px; }
  .lib-actions { width: 100%; }
  .api-input { flex: 1; min-width: 140px; }
  .lib-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
  .lib-card .card-btns { flex-wrap: wrap; }
  .lib-card .card-btns .btn { min-width: calc(50% - 3px); }

  .modal { max-height: 85vh; }
}

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