:root {
  color-scheme: dark;
  --bg: #111;
  --panel: #1b1b1b;
  --panel-2: #242424;
  --line: #343434;
  --text: #f6f6f6;
  --muted: #a9a9a9;
  --accent: #6ee7c8;
  --danger: #ff6d6d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
.button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
}

button,
.button {
  min-height: 42px;
  padding: 9px 12px;
  text-decoration: none;
  text-align: center;
}

button:active,
.button:active {
  transform: translateY(1px);
}

input,
select {
  min-height: 38px;
  padding: 8px;
  width: 100%;
}

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

.primary {
  background: var(--accent);
  color: #0c211b;
  border-color: transparent;
  font-weight: 700;
}

.danger {
  color: var(--danger);
}

.app {
  min-height: 100vh;
  max-width: 760px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: center;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: rgba(17, 17, 17, 0.92);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

h1,
h2 {
  margin: 0;
}

h1 {
  font-size: 17px;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
}

.tab.active {
  border-color: var(--accent);
}

.active-tool {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.screen {
  display: none;
  padding: 12px;
}

.screen.active {
  display: block;
}

.stage-shell {
  position: relative;
  width: 100%;
  margin: 0 auto;
  aspect-ratio: 9 / 16;
  max-height: 66vh;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #050505;
  touch-action: none;
}

#cameraVideo {
  display: none;
}

#stageCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.placement-controls {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

.delete-control {
  position: absolute;
  width: 34px;
  height: 34px;
  min-height: 34px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 2px solid rgba(255, 255, 255, 0.88);
  border-radius: 50%;
  background: rgba(17, 17, 17, 0.86);
  color: #fff;
  font-size: 21px;
  line-height: 1;
  font-weight: 800;
  pointer-events: auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.34);
  transform: translate(-50%, -50%);
}

.delete-control:active {
  transform: translate(-50%, -50%) scale(0.94);
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 20px;
  text-align: center;
  color: var(--muted);
  background: linear-gradient(160deg, #171717, #050505);
  z-index: 3;
}

.empty-state strong {
  color: var(--text);
  font-size: 20px;
}

.empty-state.hidden {
  display: none;
}

.toolbar,
.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.post-capture-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  padding: 10px;
  border: 1px solid rgba(110, 231, 200, 0.52);
  border-radius: 8px;
  background: rgba(110, 231, 200, 0.12);
  color: var(--text);
}

.post-capture-bar span {
  font-size: 13px;
  color: var(--accent);
}

.hidden {
  display: none !important;
}

.toolbar label,
.edit-panel label,
.asset-form label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

.scene-panel,
.edit-panel,
.asset-form,
.storage-card {
  margin-top: 12px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel-head,
.storage-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.storage-card div {
  display: grid;
  gap: 3px;
}

.storage-card span,
.hint,
.notice {
  color: var(--muted);
  font-size: 13px;
}

.scene-list,
.library-list,
.pick-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.scene-item,
.character-card,
.pick-item {
  display: grid;
  grid-template-columns: 54px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.scene-item.selected {
  border-color: var(--accent);
}

.thumb {
  width: 54px;
  height: 54px;
  object-fit: contain;
  border-radius: 6px;
  background: #0b0b0b;
}

.meta {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.meta strong,
.meta span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta span {
  color: var(--muted);
  font-size: 13px;
}

.edit-panel {
  display: grid;
  gap: 10px;
}

.edit-panel.disabled {
  opacity: 0.55;
  pointer-events: none;
}

.asset-form {
  display: grid;
  gap: 10px;
}

.file-picker input {
  padding: 10px;
}

.variants {
  grid-column: 1 / -1;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-top: 4px;
}

.variant-chip {
  flex: 0 0 auto;
  display: grid;
  gap: 4px;
  width: 76px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.variant-chip img {
  width: 76px;
  height: 76px;
}

dialog {
  width: min(92vw, 520px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.7);
}

.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.preview-dialog {
  width: min(94vw, 620px);
}

.cutout-dialog {
  width: min(96vw, 720px);
}

.preview-body {
  display: grid;
  gap: 10px;
}

.cutout-body {
  display: grid;
  gap: 10px;
}

.cutout-stage {
  width: 100%;
  height: min(58vh, 560px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: #242424;
  background-image:
    linear-gradient(45deg, #303030 25%, transparent 25%),
    linear-gradient(-45deg, #303030 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #303030 75%),
    linear-gradient(-45deg, transparent 75%, #303030 75%);
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  touch-action: none;
}

#cutoutCanvas {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  touch-action: none;
}

.cutout-tools {
  display: grid;
  gap: 10px;
}

.cutout-tools label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

#previewImage {
  width: 100%;
  max-height: 62vh;
  object-fit: contain;
  background: #050505;
  border-radius: 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 20;
  max-width: min(92vw, 420px);
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(246, 246, 246, 0.95);
  color: #111;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: 180ms ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (min-width: 680px) {
  .toolbar {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .button-row {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
