:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f2f5);
  --text: var(--tg-theme-text-color, #111317);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --accent: var(--tg-theme-button-color, #2f8cf0);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sel: #ffcc33;
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #17181c; --bg2: #22242a; --text: #eceef2; --hint: #8b919c;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }

main {
  display: flex; flex-direction: column; gap: 12px;
  max-width: 560px; margin: 0 auto;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
  min-height: 100%;
}

/* --- кадр и рамка --- */
.stage {
  display: flex; align-items: center; justify-content: center;
  background: #000; border-radius: 14px; overflow: hidden;
  height: min(52vh, 440px); min-height: 220px; position: relative;
}
.frame { position: relative; touch-action: none; }
.frame video { display: block; width: 100%; height: 100%; object-fit: fill; pointer-events: none; }
.crop {
  position: absolute; border: 2px solid #fff; cursor: move; touch-action: none;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .55);
}
.crop .grid {
  position: absolute; inset: 0; pointer-events: none; opacity: .5;
  background:
    linear-gradient(90deg, transparent calc(33.33% - .5px), #fff calc(33.33% - .5px), #fff calc(33.33% + .5px), transparent calc(33.33% + .5px),
                           transparent calc(66.66% - .5px), #fff calc(66.66% - .5px), #fff calc(66.66% + .5px), transparent calc(66.66% + .5px)),
    linear-gradient(0deg, transparent calc(33.33% - .5px), #fff calc(33.33% - .5px), #fff calc(33.33% + .5px), transparent calc(33.33% + .5px),
                          transparent calc(66.66% - .5px), #fff calc(66.66% - .5px), #fff calc(66.66% + .5px), transparent calc(66.66% + .5px));
}
.crop .h { position: absolute; width: 28px; height: 28px; touch-action: none; }
.crop .h::after { content: ""; position: absolute; background: #fff; border-radius: 2px; }
.h.nw { left: -14px; top: -14px; cursor: nwse-resize; }
.h.ne { right: -14px; top: -14px; cursor: nesw-resize; }
.h.sw { left: -14px; bottom: -14px; cursor: nesw-resize; }
.h.se { right: -14px; bottom: -14px; cursor: nwse-resize; }
.h.nw::after, .h.ne::after, .h.sw::after, .h.se::after { width: 12px; height: 12px; left: 8px; top: 8px; border-radius: 50%; }
.h.n, .h.s { left: calc(50% - 14px); cursor: ns-resize; }
.h.n { top: -14px; } .h.s { bottom: -14px; }
.h.w, .h.e { top: calc(50% - 14px); cursor: ew-resize; }
.h.w { left: -14px; } .h.e { right: -14px; }
.h.n::after, .h.s::after { width: 18px; height: 4px; left: 5px; top: 12px; }
.h.w::after, .h.e::after { width: 4px; height: 18px; left: 12px; top: 5px; }
.crop.locked .h.n, .crop.locked .h.s, .crop.locked .h.w, .crop.locked .h.e { display: none; }

/* --- чипы --- */
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; border: 0; border-radius: 999px; padding: 7px 12px;
  background: var(--bg2); font-size: 14px;
}
.chips button.on { background: var(--accent); color: var(--accent-text); }
.chips.small button { padding: 5px 10px; font-size: 13px; }

/* --- таймлайн --- */
.times { display: flex; justify-content: space-between; font-size: 13px; color: var(--hint); margin-bottom: 6px; font-variant-numeric: tabular-nums; }
.times b { color: var(--text); font-weight: 600; }
.timeline {
  position: relative; height: 52px; border-radius: 10px; overflow: hidden;
  background: var(--bg2); touch-action: none;
}
.strip { position: absolute; top: 0; bottom: 0; left: 14px; right: 14px; display: flex; }
.strip img { flex: 1 1 0; min-width: 0; height: 100%; object-fit: cover; }
.shade { position: absolute; top: 0; bottom: 0; background: rgba(0, 0, 0, .55); pointer-events: none; }
#shadeL { left: 0; } #shadeR { right: 0; }
.win {
  position: absolute; top: 0; bottom: 0; cursor: grab;
  border: 3px solid var(--sel); border-left-width: 14px; border-right-width: 14px; border-radius: 10px;
}
.grip { position: absolute; top: 0; bottom: 0; width: 30px; cursor: ew-resize; }
.grip::after { content: ""; position: absolute; top: 50%; width: 3px; height: 16px; margin-top: -8px; border-radius: 2px; background: rgba(0, 0, 0, .55); }
.grip.l { left: -22px; } .grip.l::after { left: 14px; }
.grip.r { right: -22px; } .grip.r::after { right: 14px; }
.playhead { position: absolute; top: 3px; bottom: 3px; width: 2px; background: #fff; box-shadow: 0 0 2px rgba(0,0,0,.6); pointer-events: none; }

/* --- низ --- */
.bottom { display: flex; gap: 12px; align-items: center; }
.preview-box {
  flex: none; width: 96px; height: 96px; border-radius: 12px; display: grid; place-items: center;
  background:
    conic-gradient(var(--bg2) 25%, transparent 0 50%, var(--bg2) 0 75%, transparent 0) 0 0 / 16px 16px;
}
#preview { display: block; max-width: 96px; max-height: 96px; border-radius: 8px; }
.opts { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.info { font-size: 13px; color: var(--hint); }
.info .warn { color: var(--danger); }

.fallback-btn {
  border: 0; border-radius: 12px; padding: 14px; font-weight: 600;
  background: var(--accent); color: var(--accent-text);
}

/* --- оверлей --- */
.overlay {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); z-index: 10;
}
.overlay[hidden] { display: none; }
.msg { text-align: center; max-width: 320px; }
.msg p { margin: 12px 0 0; }
.spinner {
  width: 36px; height: 36px; margin: 0 auto; border-radius: 50%;
  border: 3px solid var(--bg2); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
.spinner[hidden] { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.actions { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }
.actions[hidden] { display: none; }
.actions button { border: 0; border-radius: 10px; padding: 10px 16px; background: var(--bg2); }
.actions button.primary { background: var(--accent); color: var(--accent-text); }
