:root {
  --bg: #fbf3df;
  --panel: #fffaf0;
  --ink: #2b2620;
  --muted: #8a7f6f;
  --line: #e6d8bb;
  --accent: #f4b13d;
  --accent-ink: #4a3104;
  --on: #2b2620;
  --danger: #c8412f;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1c18;
    --panel: #2a2621;
    --ink: #f3ead8;
    --muted: #a89d8a;
    --line: #3d372f;
    --on: #f3ead8;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.screen {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
}
.bar {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 52px;
}
.logo { font-size: 22px; margin: 0 auto 0 4px; letter-spacing: -0.5px; }
.logo-q { color: var(--accent); -webkit-text-stroke: 0.6px var(--accent-ink); }
.icon {
  width: 44px; height: 44px;
  border: 0; background: none; border-radius: 50%;
  display: grid; place-items: center; cursor: pointer;
}
.icon:active { background: var(--line); }
.icon svg, .tool svg { width: 24px; height: 24px; }
.framepos { margin: 0 auto; font-variant-numeric: tabular-nums; font-size: 17px; }
.who { margin: 0 auto 0 4px; display: flex; flex-direction: column; min-width: 0; }
.who b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who span { color: var(--muted); font-size: 13px; }

/* library */
.tabs { display: flex; gap: 4px; margin-bottom: 10px; overflow-x: auto; }
.tabs button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px;
  padding: 7px 14px; white-space: nowrap; cursor: pointer;
}
.tabs button.on { background: var(--on); color: var(--bg); border-color: var(--on); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px; cursor: pointer; text-align: left; display: flex; flex-direction: column; gap: 4px;
}
.card img, .card canvas {
  width: 100%; aspect-ratio: 4 / 3; image-rendering: pixelated; border-radius: 6px;
  background: #fff; display: block; border: 1px solid var(--line);
}
.card .meta { font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.card .meta b { color: var(--ink); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card.new {
  align-items: center; justify-content: center; min-height: 140px;
  border: 2px dashed var(--accent); color: var(--accent-ink); background: color-mix(in srgb, var(--accent) 18%, var(--panel));
  font-weight: 600; gap: 8px;
}
@media (prefers-color-scheme: dark) { .card.new { color: var(--accent); } }
.card.new svg { width: 36px; height: 36px; }
.msg { padding: 12px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line); margin-bottom: 10px; color: var(--muted); }
.wide { margin: 12px 0; padding: 12px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--panel); cursor: pointer; }

/* editor / player */
.stage {
  background: #d9ccb0; border-radius: var(--radius); padding: 6px; touch-action: none;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.08);
}
@media (prefers-color-scheme: dark) { .stage { background: #3a342c; } }
.stage canvas {
  display: block; width: 100%; aspect-ratio: 4 / 3; image-rendering: pixelated;
  touch-action: none; border-radius: 4px; background: #fff; cursor: crosshair;
}
input[type=range] { width: 100%; margin: 10px 0 4px; accent-color: var(--accent); }
.tools { display: flex; flex-direction: column; gap: 6px; padding-bottom: 10px; }
.row { display: flex; gap: 6px; align-items: center; }
.row.center { justify-content: center; }
.gap { flex: 1; }
.tool, .layer {
  min-width: 44px; height: 44px; padding: 0 8px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.tool:disabled { opacity: .35; cursor: default; }
.tool.on, .layer.on { background: var(--accent); border-color: var(--accent-ink); color: var(--accent-ink); }
.tool.big { width: 64px; height: 52px; }
.tool.txt { font-size: 14px; font-weight: 600; }
.tool .dot { background: currentColor; border-radius: 50%; display: block; }
.tool canvas { width: 20px; height: 20px; image-rendering: pixelated; border: 1px solid var(--muted); border-radius: 3px; }
.layer { font-weight: 700; min-width: 54px; }
.layer .sw { width: 16px; height: 16px; border-radius: 50%; border: 2px solid rgba(0,0,0,.35); }
.paper { width: 22px; height: 16px; border-radius: 3px; border: 2px solid var(--muted); display: block; }

/* dialogs */
dialog {
  border: 0; border-radius: 16px; padding: 18px; width: min(92vw, 420px);
  background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(30, 20, 5, .45); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
dialog label { display: block; margin-bottom: 10px; font-size: 14px; color: var(--muted); }
dialog input:not([type=checkbox]):not([type=range]) {
  display: block; width: 100%; margin-top: 4px; padding: 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit;
}
dialog .hint { font-size: 13px; color: var(--muted); margin: 6px 0; }
dialog code { font-size: 12px; background: var(--bg); padding: 1px 4px; border-radius: 4px; }
.buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.buttons button, .menu-item {
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); cursor: pointer;
}
.buttons .primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent-ink); font-weight: 600; }
.menu-item { display: block; width: 100%; text-align: left; margin-bottom: 6px; }
.menu-item.danger { color: var(--danger); }
.menu-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 14px; }
.menu-row input[type=range] { margin: 0; flex: 1; }
.menu-row output { min-width: 70px; text-align: right; font-variant-numeric: tabular-nums; }
.link-box { word-break: break-all; font-size: 13px; background: var(--bg); padding: 8px; border-radius: 8px; margin-bottom: 10px; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--on); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  max-width: 90vw; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 10;
}
