:root {
  --bg: #0f1218;
  --panel: #171b24;
  --panel2: #1e2430;
  --border: #2a3344;
  --text: #e8edf7;
  --muted: #8b97ad;
  --accent: #5b8cff;
  --accent2: #3dd68c;
  --danger: #ff6b7a;
  --radius: 10px;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  --controls-w: min(42vw, 340px);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html {
  height: 100%;
  height: 100dvh;
}
body {
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  min-height: 100dvh;
  height: 100%;
  height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--controls-w);
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  min-height: 0;
}

/* ---- Left: white stage ---- */
.stage-wrap {
  min-width: 0;
  min-height: 0;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}
.stage-viewport {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}
.stage-stack {
  position: relative;
  width: 100%;
  height: 100%;
  background: #ffffff;
  overflow: hidden;
  touch-action: none;
  transform-origin: center center;
}
.stage-stack canvas {
  display: block;
  width: 100%;
  height: 100%;
}
#stage { background: transparent; }
#stage-overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background: transparent;
}

/* ---- Right: controls ---- */
.controls {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow: hidden;
}

.tool-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: repeat(5, minmax(52px, auto));
  gap: 6px;
  padding: 8px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
}

.tool-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.15;
  padding: 6px 4px;
  cursor: pointer;
  user-select: none;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 52px;
  transition: background 0.12s, border-color 0.12s, transform 0.05s;
}
.tool-btn .sub {
  font-size: 0.65rem;
  font-weight: 500;
  color: var(--muted);
}
.tool-btn:hover { border-color: var(--accent); }
.tool-btn:active { transform: scale(0.97); }
.tool-btn.mode.active {
  background: rgba(91, 140, 255, 0.22);
  border-color: var(--accent);
  color: #cfe0ff;
  box-shadow: inset 0 0 0 1px rgba(91, 140, 255, 0.35);
}
.tool-btn.danger { border-color: #5a3038; color: var(--danger); }
.tool-btn.play {
  font-size: 1.45rem;
  border-color: #2a4a38;
  color: var(--accent2);
  font-weight: 700;
}
.tool-btn.play.active {
  background: #1e3a2c;
  border-color: var(--accent2);
}
.tool-btn.busy { opacity: 0.7; pointer-events: none; }
label.tool-btn { cursor: pointer; margin: 0; }

.zoom-cell {
  display: flex;
  flex-direction: row;
  gap: 4px;
  padding: 4px;
  align-items: stretch;
  justify-content: stretch;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 52px;
}
.zoom-btn {
  flex: 1;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 6px;
  font-size: 1.35rem;
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
  min-width: 0;
}
.zoom-btn:active { background: rgba(91, 140, 255, 0.25); }

.bottom-panes {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 36px 1fr;
  gap: 0;
  overflow: hidden;
}

.layers-pane,
.frames-pane {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 6px;
  background: var(--panel);
}
.layers-pane { border-right: 1px solid var(--border); }
.frames-pane { border-left: 1px solid var(--border); }

.pane-head {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
  flex-shrink: 0;
  line-height: 1.2;
}

.new-layer-btn {
  appearance: none;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  transform: rotate(180deg);
  border: none;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  cursor: pointer;
  padding: 8px 4px;
  line-height: 1.1;
}
.new-layer-btn:hover { background: rgba(91, 140, 255, 0.18); color: #cfe0ff; }
.new-layer-btn:active { transform: rotate(180deg) scale(0.98); }

.layer-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
  min-height: 0;
}
.layer-list:empty { display: none; }
.empty-hint {
  color: var(--muted);
  font-size: 0.72rem;
  margin: 0;
  line-height: 1.35;
}
.hidden { display: none !important; }

.layer-item {
  display: flex;
  flex-direction: row;
  gap: 4px;
  align-items: center;
  padding: 6px;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.layer-item.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
  background: #24304a;
}
.layer-item .eye {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.layer-item .eye.off { opacity: 0.35; }
.layer-item .meta {
  min-width: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
}
.layer-item .name {
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layer-item .sub {
  font-size: 0.62rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layer-item .actions { display: flex; flex-shrink: 0; }
.layer-item .actions .btn {
  padding: 2px 7px;
  font-size: 0.72rem;
  border-radius: 6px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
}
.btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
.btn.danger { border-color: #5a3038; color: var(--danger); }
.btn.tiny {
  padding: 4px 8px;
  font-size: 0.85rem;
  min-width: 28px;
  font-weight: 700;
}
.btn.icon { padding: 6px 10px; line-height: 1; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.frame-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  margin-bottom: 4px;
  flex-shrink: 0;
}
.frame-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.68rem;
  color: var(--muted);
  margin-bottom: 4px;
  flex-shrink: 0;
}
.fps-label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 0.68rem;
  color: var(--muted);
  margin-left: auto;
}
.fps-label input {
  width: 2.8em;
  padding: 2px 3px;
  border-radius: 5px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.25);
  color: inherit;
  font: inherit;
}

.frame-strip {
  display: flex;
  flex-direction: column;
  gap: 5px;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1 1 auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}
.frame-cell {
  flex: 0 0 auto;
  width: 100%;
  height: 52px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px;
  user-select: none;
}
.frame-cell.current {
  border-color: var(--accent2);
  box-shadow: 0 0 0 1px var(--accent2);
  background: #1a2e24;
}
.frame-cell .frame-num {
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1;
}
.frame-cell .frame-thumb {
  font-size: 0.6rem;
  color: var(--muted);
}

/* Modal / toast / file input */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal-card {
  width: min(920px, 100%);
  max-height: 95vh;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.photos-card { max-width: min(420px, 94vw); }
.photos-preview-wrap {
  margin: 0 8px 8px;
  background: #111;
  border-radius: 12px;
  overflow: hidden;
  min-height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.photos-preview {
  display: block;
  max-width: 100%;
  max-height: min(42vh, 320px);
  width: auto;
  height: auto;
  margin: 0 auto;
}
.photos-preview.hidden { display: none !important; }
.photos-hint {
  margin: 8px 8px 4px;
  font-size: 0.88rem;
  line-height: 1.35;
  color: var(--muted);
}
.photos-foot .btn.primary {
  flex: 1;
  min-height: 48px;
  font-size: 1rem;
}
.export-toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  z-index: 80;
  background: rgba(15, 18, 24, 0.92);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 0.95rem;
  font-weight: 600;
  pointer-events: none;
}
.export-toast.hidden { display: none; }

.file-input-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

/* Portrait phones: stage on top, panel below */
@media (max-width: 720px) and (orientation: portrait) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) minmax(280px, 46vh);
  }
  .controls {
    border-left: none;
    border-top: 1px solid var(--border);
  }
  .tool-grid {
    grid-template-rows: repeat(5, minmax(44px, auto));
    gap: 4px;
    padding: 6px;
  }
  .tool-btn { min-height: 44px; font-size: 0.72rem; }
  .bottom-panes { grid-template-columns: 1fr 30px 1fr; }
  .new-layer-btn { font-size: 0.65rem; padding: 6px 2px; }
}

/* Narrow landscape: keep left stage large */
@media (orientation: landscape) and (max-height: 480px) {
  :root { --controls-w: min(48vw, 300px); }
  .tool-grid {
    grid-template-rows: repeat(5, minmax(36px, auto));
    gap: 3px;
    padding: 4px;
  }
  .tool-btn { min-height: 36px; font-size: 0.65rem; padding: 3px 2px; }
  .zoom-cell { min-height: 36px; }
  .pane-head { font-size: 0.6rem; }
}

.tool-btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}
.spacer-cell {
  visibility: hidden;
  pointer-events: none;
  border: none;
  background: transparent;
}

.color-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  padding: 4px 2px;
}
.color-cell input[type="color"] {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 36px;
  height: 28px;
  padding: 0;
  background: transparent;
  cursor: pointer;
}
.color-cell input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 2px;
}
.color-cell input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 4px;
}
