:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --bg2: var(--tg-theme-secondary-bg-color, #0e1621);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #8a99a8);
  --accent: var(--tg-theme-button-color, #5288c1);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #ec3942);
  --radius: 14px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
.hidden { display: none !important; }

#app { max-width: 560px; margin: 0 auto; padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); }
.screen { display: flex; flex-direction: column; gap: 14px; }

/* --- выбор файла --- */
.pick { min-height: 80vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; }
.pick-icon { width: 84px; height: 84px; border-radius: 24px; display: grid; place-items: center; background: var(--bg2); color: var(--accent); }
h1, h2 { margin: 6px 0 0; font-size: 20px; font-weight: 600; }
.hint { color: var(--hint); margin: 0; font-size: 14px; }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }

/* --- кнопки --- */
.btn { display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; padding: 12px 18px; font-weight: 600; cursor: pointer; user-select: none; }
.btn.big { width: 100%; padding: 14px; font-size: 16px; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.secondary { background: var(--bg2); color: var(--accent); }
.btn.ghost { color: var(--accent); }
.btn:disabled { opacity: .5; }
.pick .btn.big { max-width: 320px; margin-top: 10px; }

/* --- переключатели --- */
.seg { display: flex; background: var(--bg2); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; padding: 9px 0; border-radius: 9px; font-weight: 600; color: var(--hint); transition: background .15s, color .15s; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.seg.small button { padding: 7px 0; font-size: 14px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chips button { padding: 7px 14px; border-radius: 999px; background: var(--bg2); color: var(--text); font-size: 14px; border: 1.5px solid transparent; }
.chips button.on { border-color: var(--accent); color: var(--accent); }
.chips button:disabled { opacity: .35; }
.chips.emoji button { font-size: 22px; padding: 4px 8px; line-height: 1.2; }
.field .label { font-size: 13px; color: var(--hint); margin-bottom: 6px; }

/* --- сцена с рамкой --- */
#stage { position: relative; background: var(--bg2); border-radius: var(--radius); min-height: 220px; display: grid; place-items: center; overflow: hidden; padding: 10px; }
#frame { position: relative; touch-action: none; user-select: none;
  /* шахматка — видно прозрачность */
  background: repeating-conic-gradient(#8883 0 25%, #0000 0 50%) 0 0 / 16px 16px; }
#frame video, #frame img { display: block; width: 100%; height: 100%; pointer-events: none; }
#crop { position: absolute; outline: 2px solid #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, .55); cursor: move; touch-action: none; }
#crop.fit { box-shadow: none; outline: 2px dashed #fff8; cursor: default; }
#crop.fit .h, #crop.fit .grid { display: none; }
#crop.round { border-radius: 0; }
#frame { overflow: hidden; }
.grid { position: absolute; inset: 0; pointer-events: none;
  background:
    linear-gradient(to right, transparent calc(33.33% - .5px), #fff6 0 calc(33.33% + .5px), transparent 0 calc(66.66% - .5px), #fff6 0 calc(66.66% + .5px), transparent 0),
    linear-gradient(to bottom, transparent calc(33.33% - .5px), #fff6 0 calc(33.33% + .5px), transparent 0 calc(66.66% - .5px), #fff6 0 calc(66.66% + .5px), transparent 0); }
.h { position: absolute; width: 28px; height: 28px; touch-action: none; }
.h::after { content: ""; position: absolute; width: 16px; height: 16px; border: 4px solid #fff; }
.h[data-h="nw"] { left: -12px; top: -12px; cursor: nwse-resize; }
.h[data-h="ne"] { right: -12px; top: -12px; cursor: nesw-resize; }
.h[data-h="sw"] { left: -12px; bottom: -12px; cursor: nesw-resize; }
.h[data-h="se"] { right: -12px; bottom: -12px; cursor: nwse-resize; }
.h[data-h="nw"]::after { left: 8px; top: 8px; border-right: 0; border-bottom: 0; }
.h[data-h="ne"]::after { right: 8px; top: 8px; border-left: 0; border-bottom: 0; }
.h[data-h="sw"]::after { left: 8px; bottom: 8px; border-right: 0; border-top: 0; }
.h[data-h="se"]::after { right: 8px; bottom: 8px; border-left: 0; border-top: 0; }

.stage-msg { position: absolute; inset: 0; display: flex; gap: 10px; align-items: center; justify-content: center; background: color-mix(in srgb, var(--bg2) 85%, transparent); color: var(--hint); text-align: center; padding: 20px; }
.spinner { width: 18px; height: 18px; border: 2.5px solid var(--hint); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- таймлайн --- */
#video-controls { display: flex; flex-direction: column; gap: 10px; }
#timeline { position: relative; height: 56px; border-radius: 10px; background: var(--bg2); overflow: hidden; touch-action: none; user-select: none; }
#thumbs { position: absolute; inset: 0; display: flex; }
#thumbs img { flex: 1; min-width: 0; height: 100%; object-fit: cover; }
.shade { position: absolute; top: 0; bottom: 0; background: rgba(0, 0, 0, .55); pointer-events: none; }
#shade-l { left: 0; }
#shade-r { right: 0; }
#win { position: absolute; top: 0; bottom: 0; border: 3px solid #ffd43b; border-left-width: 14px; border-right-width: 14px; border-radius: 8px; cursor: grab; touch-action: none; }
.th { position: absolute; top: 0; bottom: 0; width: 26px; touch-action: none; cursor: ew-resize; }
.th[data-t="l"] { left: -20px; }
.th[data-t="r"] { right: -20px; }
.th::after { content: ""; position: absolute; top: 50%; left: 50%; width: 3px; height: 18px; margin: -9px 0 0 -1.5px; border-radius: 2px; background: #0008; }
#playhead { position: absolute; top: -2px; bottom: -2px; width: 3px; margin-left: -1.5px; background: #fff; border-radius: 2px; box-shadow: 0 0 4px #0009; pointer-events: none; }
#timeline.frame-mode #win, #timeline.frame-mode .shade { display: none; }
#timeline.frame-mode #playhead { width: 4px; top: 0; bottom: 0; box-shadow: 0 0 0 2px #ffd43b; }
.time-row { display: flex; align-items: center; gap: 10px; color: var(--hint); font-size: 14px; font-variant-numeric: tabular-nums; }
.icon-btn { width: 34px; height: 34px; border-radius: 50%; background: var(--bg2); color: var(--text); font-size: 12px; flex: none; }

.status { margin: 0; min-height: 1.4em; color: var(--hint); text-align: center; font-size: 14px; }
.status.err { color: var(--danger); }

/* --- результат --- */
.done { min-height: 80vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; }
.result-box { width: 220px; height: 220px; display: grid; place-items: center; border-radius: 20px;
  background: repeating-conic-gradient(#8882 0 25%, #0000 0 50%) 0 0 / 16px 16px, var(--bg2); }
.result-box video, .result-box img { max-width: 200px; max-height: 200px; }
.done .btn.big { max-width: 320px; }
