:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f0f6);
  --text: var(--tg-theme-text-color, #16151c);
  --hint: var(--tg-theme-hint-color, #8b8a96);
  --line: color-mix(in srgb, var(--text) 10%, transparent);
  --accent: #5b3df5;
  --accent-soft: color-mix(in srgb, #5b3df5 14%, var(--bg));
  --tape: #ffd23f;
  --danger: #e5484d;
  --ok: #1f9d63;
  --display: "Unbounded", "Manrope", system-ui, sans-serif;
  --ui: "Manrope", system-ui, -apple-system, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font: 500 15px/1.45 var(--ui); }
body { overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
input, textarea { font: inherit; color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
img { -webkit-user-drag: none; }
[hidden] { display: none !important; }

.screen { min-height: 100vh; padding: 0 16px calc(96px + var(--safe-b)); }
.topbar { display: flex; align-items: center; gap: 10px; padding: 16px 0 12px; position: sticky; top: 0; background: var(--bg); z-index: 5; }
.topbar h1 { font: 700 20px/1.15 var(--display); margin: 0; flex: 1; letter-spacing: -0.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .sub { color: var(--hint); font-size: 13px; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: var(--bg2); flex: none; }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- home ---------- */
.brand { padding: 22px 0 6px; }
.brand h1 { font: 900 30px/1 var(--display); margin: 0; letter-spacing: -0.02em; }
.brand p { margin: 8px 0 0; color: var(--hint); max-width: 30em; }

.make { margin: 18px 0 8px; display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 50px; padding: 0 18px; border-radius: 14px; font-weight: 800; font-size: 15px; background: var(--bg2); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .45; }
.btn svg { width: 20px; height: 20px; }

.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 26px 0 12px; }
.section-title h2 { font: 700 16px/1 var(--display); margin: 0; }
.section-title span { color: var(--hint); font-size: 13px; }

.packs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.pack-card { text-align: left; background: var(--bg2); border-radius: 18px; padding: 12px; display: flex; flex-direction: column; gap: 10px; position: relative; }
.pack-card .cover { aspect-ratio: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 6px; }
.pack-card .cover.empty { display: grid; place-items: center; grid-template-columns: 1fr; border: 2px dashed var(--line); border-radius: 12px; color: var(--hint); font-size: 13px; text-align: center; padding: 10px; }
.cut { width: 100%; aspect-ratio: 1; object-fit: contain;
  filter: drop-shadow(1.6px 0 0 #fff) drop-shadow(-1.6px 0 0 #fff) drop-shadow(0 1.6px 0 #fff) drop-shadow(0 -1.6px 0 #fff) drop-shadow(0 3px 4px rgba(0,0,0,.2)); }
.cover .cut:nth-child(1) { transform: rotate(-6deg); }
.cover .cut:nth-child(2) { transform: rotate(5deg) translateY(3px); }
.cover .cut:nth-child(3) { transform: rotate(4deg); }
.cover .cut:nth-child(4) { transform: rotate(-3deg) translateY(-2px); }
.pack-card .t { font-weight: 800; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pack-card .m { color: var(--hint); font-size: 12.5px; display: flex; gap: 6px; align-items: center; }
.badge { display: inline-block; padding: 2px 7px; border-radius: 999px; font-size: 11.5px; font-weight: 800; background: var(--accent-soft); color: var(--accent); }
.tape { position: absolute; top: -7px; right: 14px; background: var(--tape); color: #3a2c00; font-size: 11px; font-weight: 800; padding: 3px 8px; border-radius: 3px; transform: rotate(3deg); box-shadow: 0 2px 4px rgba(0,0,0,.12); }
.new-card { border: 2px dashed var(--line); background: transparent; align-items: center; justify-content: center; min-height: 200px; color: var(--hint); font-weight: 700; }
.new-card svg { width: 30px; height: 30px; }

.empty-state { text-align: center; padding: 40px 16px; color: var(--hint); }
.empty-state b { display: block; color: var(--text); font: 700 17px/1.3 var(--display); margin-bottom: 8px; }

/* ---------- pack ---------- */
.pack-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: -4px 0 14px; color: var(--hint); font-size: 13px; }
.progress-line { height: 4px; border-radius: 4px; background: var(--bg2); overflow: hidden; flex: 1; min-width: 80px; }
.progress-line i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.pack-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grid.emoji { grid-template-columns: repeat(6, 1fr); gap: 6px; }
.cell { position: relative; aspect-ratio: 1; border-radius: 14px; background: var(--bg2); display: grid; place-items: center; overflow: hidden; touch-action: manipulation; }
.cell img { width: 86%; height: 86%; object-fit: contain; pointer-events: none; }
.cell .e { position: absolute; left: 5px; bottom: 3px; font-size: 13px; line-height: 1; }
.cell .f { position: absolute; right: 5px; top: 5px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.cell.sel { box-shadow: inset 0 0 0 3px var(--accent); background: var(--accent-soft); }
.cell .check { position: absolute; right: 6px; bottom: 6px; width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--hint); background: var(--bg); }
.cell.sel .check { background: var(--accent); border-color: var(--accent); }
.cell.sel .check::after { content: ""; position: absolute; left: 5px; top: 2px; width: 5px; height: 9px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }
.sortable-ghost { opacity: .3; }
.sortable-chosen { transform: scale(1.06); box-shadow: 0 8px 20px rgba(0,0,0,.2); }
.hint-line { color: var(--hint); font-size: 12.5px; margin: 14px 2px 0; }

.bottom-bar { position: fixed; left: 0; right: 0; bottom: 0; padding: 10px 16px calc(10px + var(--safe-b)); background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(12px); border-top: 1px solid var(--line); display: flex; gap: 10px; z-index: 20; }
.bottom-bar .btn { flex: 1; }

/* ---------- sheets ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(10, 8, 20, .45); z-index: 50; animation: fade .18s ease; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; background: var(--bg); border-radius: 22px 22px 0 0; padding: 8px 16px calc(16px + var(--safe-b)); max-height: 88vh; overflow: auto; animation: rise .22s cubic-bezier(.2,.8,.2,1); }
.sheet .grab { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 12px; }
.sheet h3 { font: 700 18px/1.2 var(--display); margin: 4px 0 14px; }
.sheet p { margin: 0 0 12px; color: var(--hint); }
@keyframes rise { from { transform: translateY(40%); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .backdrop { animation: none; } }

.list { display: flex; flex-direction: column; gap: 2px; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 14px; text-align: left; width: 100%; }
.row:active { background: var(--bg2); }
.row .ri { width: 44px; height: 44px; border-radius: 12px; background: var(--bg2); display: grid; place-items: center; flex: none; overflow: hidden; }
.row .ri img { width: 80%; height: 80%; object-fit: contain; }
.row .rt { flex: 1; min-width: 0; }
.row .rt b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .rt small { color: var(--hint); }
.row.on { background: var(--accent-soft); }
.row.danger { color: var(--danger); }

.field { width: 100%; height: 48px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--bg2); padding: 0 14px; outline: none; }
.field:focus { border-color: var(--accent); }
textarea.field { height: 90px; padding: 12px 14px; resize: none; }
.label { font-size: 13px; color: var(--hint); margin: 14px 2px 6px; display: block; }
.seg { display: flex; background: var(--bg2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; height: 38px; border-radius: 10px; font-weight: 700; font-size: 14px; color: var(--hint); }
.seg button.on { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.quick { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.quick button { width: 40px; height: 40px; border-radius: 10px; background: var(--bg2); font-size: 21px; }
.stack { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }

.preview-big { display: grid; place-items: center; height: 220px; margin: 0 0 12px; border-radius: 18px; background: var(--bg2); }
.preview-big img, .preview-big video, .preview-big .lottie { max-width: 190px; max-height: 190px; width: 190px; height: 190px; object-fit: contain; }

.bar { height: 8px; border-radius: 6px; background: var(--bg2); overflow: hidden; margin: 10px 0; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }

/* ---------- editor ---------- */
.editor { position: fixed; inset: 0; background: var(--bg); display: flex; flex-direction: column; z-index: 30; }
.ed-top { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.ed-top .seg { flex: 1; max-width: 260px; margin: 0 auto; }
.mat-wrap { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; min-height: 0; padding: 4px 12px; position: relative; }
.mat { position: relative; width: min(100%, 50vh); aspect-ratio: 1; border-radius: 18px; overflow: hidden;
  background-color: #e9e8ee;
  background-image: linear-gradient(45deg, #d6d5de 25%, transparent 25%), linear-gradient(-45deg, #d6d5de 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #d6d5de 75%), linear-gradient(-45deg, transparent 75%, #d6d5de 75%);
  background-size: 22px 22px; background-position: 0 0, 0 11px, 11px -11px, -11px 0;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.06); }
.mat canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.mat.round { border-radius: 50%; }
.chat-prev { display: flex; align-items: center; gap: 10px; background: var(--bg2); border-radius: 14px; padding: 6px 12px; pointer-events: none; min-height: 40px; }
.chat-prev canvas { display: block; }
.chat-prev span { font-size: 13px; color: var(--hint); }
.busy { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.35); color: #fff; font-weight: 800; z-index: 3; border-radius: 18px; }
.spinner { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; margin: 0 auto 10px; }
@keyframes spin { to { transform: rotate(360deg); } }

.tools { display: flex; gap: 4px; overflow-x: auto; padding: 8px 12px 4px; scrollbar-width: none; }
.tools::-webkit-scrollbar { display: none; }
.tool { flex: none; display: flex; flex-direction: column; align-items: center; gap: 4px; width: 64px; padding: 8px 0; border-radius: 12px; font-size: 11.5px; font-weight: 700; color: var(--hint); }
.tool svg { width: 24px; height: 24px; }
.tool.on { color: var(--accent); background: var(--accent-soft); }
.panel { padding: 6px 16px 4px; min-height: 64px; display: flex; flex-direction: column; gap: 8px; justify-content: center; }
.prow { display: flex; align-items: center; gap: 10px; }
.prow > span { font-size: 13px; color: var(--hint); width: 70px; flex: none; }
.prow input[type=range] { flex: 1; accent-color: var(--accent); height: 28px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 36px; padding: 0 14px; border-radius: 999px; background: var(--bg2); font-weight: 700; font-size: 13.5px; white-space: nowrap; }
.chip.on { background: var(--accent); color: #fff; }
.swatches { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 2px; }
.sw { flex: none; width: 30px; height: 30px; border-radius: 50%; box-shadow: inset 0 0 0 1.5px rgba(0,0,0,.15); }
.sw.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.sw input { opacity: 0; width: 100%; height: 100%; }
.ed-bottom { display: flex; gap: 10px; padding: 8px 16px calc(12px + var(--safe-b)); }
.ed-bottom .btn { flex: 1; }
.ed-bottom .icon-btn { height: 50px; width: 50px; }
.ed-bottom .icon-btn:disabled { opacity: .35; }

/* video */
.vframe { position: relative; width: min(100%, 52vh); aspect-ratio: 1; border-radius: 18px; overflow: hidden; background-color: #e9e8ee;
  background-image: linear-gradient(45deg, #d6d5de 25%, transparent 25%), linear-gradient(-45deg, #d6d5de 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #d6d5de 75%), linear-gradient(-45deg, transparent 75%, #d6d5de 75%);
  background-size: 22px 22px; background-position: 0 0, 0 11px, 11px -11px, -11px 0; touch-action: none; }
.vframe video, .vframe img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
.vframe.cover video, .vframe.cover img { object-fit: cover; }
.vframe.round { border-radius: 50%; }
.vframe .pan-hint { white-space: nowrap; position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); font-size: 12px; font-weight: 700; color: #fff; background: rgba(0,0,0,.45); padding: 4px 10px; border-radius: 999px; pointer-events: none; }
.timeline { position: relative; height: 44px; border-radius: 10px; background: var(--bg2); margin: 4px 0; touch-action: none; overflow: hidden; }
.timeline .win { position: absolute; top: 0; bottom: 0; border: 3px solid var(--accent); border-radius: 10px; background: color-mix(in srgb, var(--accent) 16%, transparent); }
.timeline .win::before, .timeline .win::after { content: ""; position: absolute; top: 50%; width: 4px; height: 18px; margin-top: -9px; border-radius: 2px; background: var(--accent); }
.timeline .win::before { left: 3px; } .timeline .win::after { right: 3px; }
.timeline .play { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text); opacity: .7; }
.tl-info { display: flex; justify-content: space-between; color: var(--hint); font-size: 12.5px; }

#toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%) translateY(20px); background: #1d1b29; color: #fff; padding: 11px 16px; border-radius: 14px; font-weight: 700; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; z-index: 100; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.err { background: var(--danger); }

.loader { display: grid; place-items: center; height: 60vh; }
.loader .spinner { border-color: var(--line); border-top-color: var(--accent); }
.skeleton { background: linear-gradient(90deg, var(--bg2), color-mix(in srgb, var(--bg2) 60%, var(--bg)), var(--bg2)); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }
