:root {
  --bg: #16171b;
  --panel: #202228;
  --panel2: #2a2d35;
  --line: #363a44;
  --text: #e9eaee;
  --muted: #9097a5;
  --accent: #ff6a3d;
  --accent2: #ffd2c2;
  --radius: 12px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
}

button, .btn, select, input { font: inherit; color: inherit; touch-action: manipulation; }
svg { width: 22px; height: 22px; display: block; }

/* ---------- top bar ---------- */
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(8px + var(--safe-t)) calc(12px + var(--safe-r)) 8px calc(12px + var(--safe-l));
  background: var(--panel); border-bottom: 1px solid var(--line);
  min-height: 56px; flex-wrap: wrap;
}
.group { display: flex; align-items: center; gap: 8px; min-width: 0; }
.sep { width: 1px; height: 26px; background: var(--line); margin: 0 4px; }
.muted { color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 14px; }
#projName { max-width: 28vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 40px; padding: 0 16px; border-radius: 10px;
  background: var(--panel2); border: 1px solid var(--line); cursor: pointer;
  text-decoration: none; white-space: nowrap; font-weight: 500;
}
.btn:active { filter: brightness(1.2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1a0d07; font-weight: 600; }
.btn.danger { color: #ff8a8a; }
.btn.big { height: 52px; padding: 0 28px; font-size: 17px; }
.btn[disabled], .icon[disabled] { opacity: .35; pointer-events: none; }

.icon {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid transparent;
  background: transparent; display: grid; place-items: center; cursor: pointer; padding: 0;
}
.icon:active { background: var(--panel2); }
.icon.big { width: 46px; height: 46px; background: var(--panel2); border-color: var(--line); }
.icon.toggle.active { background: rgba(255,106,61,.18); color: var(--accent); border-color: rgba(255,106,61,.45); }

.field { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.field select {
  height: 40px; padding: 0 10px; border-radius: 10px; background: var(--panel2);
  border: 1px solid var(--line); color: var(--text); font-weight: 600;
}
.field.slider input { width: 120px; }
.field .ico { width: 20px; height: 20px; }

input[type=range] { accent-color: var(--accent); height: 32px; }

/* ---------- main / viewport ---------- */
#main { position: relative; min-height: 0; overflow: hidden; }
#viewport {
  position: absolute; inset: 0; overflow: hidden; touch-action: none;
  background:
    radial-gradient(circle at 50% 40%, #23252c 0%, #15161a 70%);
}
#stage {
  position: absolute; left: 0; top: 0; transform-origin: 0 0;
  box-shadow: 0 12px 50px rgba(0,0,0,.55); display: none;
}
#stage canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
#paperCanvas { background: #fff; }
#onionCanvas { opacity: .35; }
#brushCursor {
  position: absolute; left: 0; top: 0; pointer-events: none; border-radius: 50%;
  border: 1px solid rgba(0,0,0,.7); box-shadow: 0 0 0 1px rgba(255,255,255,.8);
  display: none; transform: translate(-50%, -50%);
}

#empty[hidden] { display: none; }
#empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; }
.emptyCard { text-align: center; max-width: 460px; }
.emptyCard h1 { font-size: 40px; margin: 0 0 8px; letter-spacing: -.02em; }
.emptyCard p { color: var(--muted); margin: 0 0 22px; font-size: 17px; }
.emptyCard .hint { margin-top: 22px; font-size: 14px; line-height: 1.7; }

#loading {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 14px;
  align-items: center; justify-content: center; background: rgba(10,10,12,.55); z-index: 5;
}
#loading[hidden] { display: none; }
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- tool panel ---------- */
#tools {
  position: absolute; left: calc(12px + var(--safe-l)); top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 8px; border-radius: 16px; background: rgba(32,34,40,.92);
  border: 1px solid var(--line); box-shadow: 0 8px 30px rgba(0,0,0,.4);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  max-height: calc(100% - 24px); overflow-y: auto; z-index: 3;
  scrollbar-width: none;
}
.tool {
  width: 46px; height: 46px; border-radius: 12px; background: transparent;
  border: 1px solid transparent; display: grid; place-items: center; cursor: pointer; padding: 0;
  font-weight: 700; font-size: 16px;
}
.tool.small { width: 40px; height: 40px; color: var(--muted); }
.tool.active { background: var(--accent); color: #1a0d07; }
.tool.small.active { background: rgba(255,106,61,.18); color: var(--accent); border-color: rgba(255,106,61,.45); }
.divider { width: 30px; height: 1px; background: var(--line); margin: 3px 0; }
#swatches { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.swatch {
  width: 20px; height: 20px; border-radius: 50%; border: 2px solid rgba(255,255,255,.15);
  cursor: pointer; padding: 0; position: relative;
}
.swatch.active { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatch.custom {
  width: 30px; height: 30px; overflow: hidden; display: block;
  background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red);
}
.swatch.custom input { opacity: 0; position: absolute; inset: 0; width: 100%; height: 100%; cursor: pointer; }
.sizeBox { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.sizeBox input {
  writing-mode: vertical-lr; direction: rtl; width: 32px; height: 130px;
}
#sizeLabel { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- timeline ---------- */
#timeline {
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 8px calc(12px + var(--safe-r)) calc(8px + var(--safe-b)) calc(12px + var(--safe-l));
  display: flex; flex-direction: column; gap: 4px;
}
#timeline .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
#frameLabel { margin-left: 8px; min-width: 120px; }
#scrub { width: 100%; margin: 0; }
#strip {
  overflow-x: auto; overflow-y: hidden; height: 30px; border-radius: 8px;
  background: #18191d; border: 1px solid var(--line); touch-action: pan-x;
  scrollbar-width: none;
}
#strip::-webkit-scrollbar { display: none; }
#stripInner { display: flex; height: 100%; }
.cell {
  flex: 0 0 16px; height: 100%; border-right: 1px solid #262830; position: relative; cursor: pointer;
}
.cell.sec { border-right-color: #4a4f5c; }
.cell.ink::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; margin: -3px 0 0 -3px;
  border-radius: 50%; background: var(--accent);
}
.cell.cur { background: rgba(255,106,61,.35); }

/* ---------- dialogs ---------- */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: 18px; padding: 22px; width: min(520px, calc(100vw - 32px));
  box-shadow: 0 20px 70px rgba(0,0,0,.6); max-height: calc(100dvh - 40px); overflow: auto;
}
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h2 { margin: 0 0 14px; font-size: 20px; }
fieldset { border: 0; padding: 0; margin: 0 0 16px; }
legend { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; margin-bottom: 8px; padding: 0; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg label { position: relative; }
.seg input[type=radio] { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 14px;
  border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); cursor: pointer;
}
.seg input:checked + span { border-color: var(--accent); background: rgba(255,106,61,.16); color: #fff; }
.seg input:disabled + span { opacity: .35; }
.seg input[type=color] { width: 26px; height: 26px; border: 0; padding: 0; background: none; }
.inline { display: flex; align-items: center; gap: 12px; }
.inline input { flex: 1; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
progress { width: 100%; height: 10px; accent-color: var(--accent); }
#resultPreview, #resultImg { width: 100%; max-height: 45dvh; border-radius: 10px; background: #000; margin-top: 8px; object-fit: contain; }
#resultImg { background: repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 20px 20px; }
.menuList { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
#fpsCustom {
  width: 100%; height: 46px; font-size: 20px; padding: 0 12px; border-radius: 10px;
  background: var(--panel2); border: 1px solid var(--line); -webkit-user-select: text; user-select: text;
}

#toast {
  position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%);
  background: rgba(20,20,24,.95); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px;
  z-index: 50; pointer-events: none; font-size: 14px; max-width: 90vw; text-align: center;
}

#video { position: fixed; left: 0; top: 0; width: 2px; height: 2px; opacity: 0; pointer-events: none; }

/* narrow / portrait */
@media (max-width: 760px) {
  #projName { display: none; }
  .field.slider input { width: 80px; }
  #frameLabel { min-width: 0; }
}

.sel { height: 40px; padding: 0 10px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); color: var(--text); font-weight: 600; }
.cell.novideo { background: repeating-linear-gradient(45deg, #1d1e23 0 4px, #24262c 4px 8px); }
.cell.novideo.cur { background: rgba(255,106,61,.35); }
#topbar .icon b { font-size: 16px; }
@media (max-width: 1000px) { #projName { display: none; } }
