:root {
  --bg: #faf7f2;
  --ink: #1d1b19;
  --muted: #8a837a;
  --line: #e8e2d8;
  --accent: #a8834f;
  --accent-soft: #f1e7d6;
  --danger: #b4483c;
  --gap: 2px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); }
body { min-height: 100vh; overscroll-behavior-y: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
button:disabled { opacity: .3; cursor: default; }

/* ---------- top bar ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  padding: calc(var(--safe-t) + 8px) 12px 8px;
  border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.top-row + .top-row { margin-top: 8px; }
.account { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; min-width: 0; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--ink); color: var(--bg);
  display: grid; place-items: center; font-size: 14px; font-family: Georgia, serif; }
.demo-badge { font-size: 11px; font-weight: 500; color: var(--accent); background: var(--accent-soft);
  padding: 2px 7px; border-radius: 10px; }
.top-actions { display: flex; gap: 2px; }
.icon-btn { width: 38px; height: 38px; border-radius: 19px; font-size: 20px; line-height: 1; }
.icon-btn:active:not(:disabled) { background: var(--line); }
.segmented { display: inline-flex; background: #efe9df; border-radius: 10px; padding: 2px; }
.segmented button { padding: 6px 14px; border-radius: 8px; font-size: 14px; color: var(--muted); }
.segmented button.active { background: #fff; color: var(--ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.queue-bar { font-size: 13px; color: var(--muted); padding: 6px 8px; border-radius: 8px; text-align: right; white-space: nowrap; }
.queue-bar b { color: var(--ink); }
.queue-bar:active { background: var(--line); }
.status-line { margin-top: 6px; font-size: 12px; color: var(--muted); }
.status-line button { color: var(--accent); font-weight: 600; font-size: 12px; }

/* ---------- grid ---------- */
main { max-width: 560px; margin: 0 auto; padding-bottom: calc(110px + var(--safe-b)); }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--gap); position: relative; }
.cell {
  position: relative; aspect-ratio: 3 / 4; overflow: hidden; background: #e9e3d9;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: pan-y;
}
.cell img { position: absolute; display: block; pointer-events: none; -webkit-user-drag: none; max-width: none; }
.cell.draft::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.85);
}
.badge-num {
  position: absolute; top: 6px; left: 6px; z-index: 2; min-width: 22px; height: 22px; padding: 0 6px;
  border-radius: 11px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  display: grid; place-items: center; box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.badge-draft {
  position: absolute; bottom: 6px; left: 6px; z-index: 2; font-size: 10px; font-weight: 600;
  background: rgba(255,255,255,.88); color: var(--ink); padding: 2px 6px; border-radius: 8px;
}
.type-icon { position: absolute; top: 6px; right: 6px; z-index: 2; width: 18px; height: 18px;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.45)); }
.pin-icon { position: absolute; top: 5px; left: 6px; z-index: 2; font-size: 14px; filter: drop-shadow(0 1px 2px rgba(0,0,0,.4)); }
.cell.placeholder { background: var(--accent-soft); }
.cell.placeholder > * { visibility: hidden; }
.cell.flash { animation: flash 1.2s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 0 transparent; } }
.cell.missing { display: grid; place-items: center; color: var(--muted); font-size: 12px; text-align: center; padding: 8px; }

.skeleton { background: linear-gradient(100deg, #e9e3d9 30%, #f4efe7 50%, #e9e3d9 70%); background-size: 300% 100%;
  animation: shimmer 1.3s linear infinite; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }

.drag-float {
  position: fixed; z-index: 50; pointer-events: none; aspect-ratio: auto;
  transform: scale(1.08); box-shadow: 0 12px 30px rgba(0,0,0,.28); border-radius: 4px;
  transition: transform .15s ease;
}
body.dragging .cell.draft:not(.placeholder) { animation: wiggle .35s ease-in-out infinite alternate; }
@keyframes wiggle { from { transform: rotate(-.6deg); } to { transform: rotate(.6deg); } }
body.dragging { overflow: hidden; }

/* crop mode */
body.cropping .cell:not(.crop-target) { filter: brightness(.55); }
body.cropping .cell.crop-target { outline: 2px solid #fff; outline-offset: -2px; z-index: 3; touch-action: none; cursor: grab; }
body.cropping .fab, body.cropping .top { pointer-events: none; }
body.cropping .top { opacity: .5; }
.crop-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: #fff;
  border-top: 1px solid var(--line); padding: 12px 16px calc(12px + var(--safe-b));
  box-shadow: 0 -6px 20px rgba(0,0,0,.08);
}
.crop-hint { font-size: 13px; color: var(--muted); text-align: center; margin-bottom: 10px; }
.crop-actions { display: flex; gap: 10px; }
.crop-actions .btn { flex: 1; }

/* footer */
.feed-footer { padding: 18px 16px; text-align: center; font-size: 13px; color: var(--muted); }
.feed-footer .btn { min-width: 200px; }

/* buttons */
.btn { padding: 12px 16px; border-radius: 12px; font-size: 15px; font-weight: 600; }
.btn.primary { background: var(--ink); color: #fff; }
.btn.secondary { background: #efe9df; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { color: var(--danger); }
.btn.block { display: block; width: 100%; }

.fab {
  position: fixed; right: 18px; bottom: calc(22px + var(--safe-b)); z-index: 30;
  width: 60px; height: 60px; border-radius: 30px; background: var(--ink); color: #fff;
  font-size: 30px; line-height: 1; box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.fab:active { transform: scale(.95); }
body.dragging .fab { opacity: 0; pointer-events: none; }

/* sheets */
.sheet-layer { position: fixed; inset: 0; z-index: 60; background: rgba(20,18,16,.38); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; background: var(--bg);
  border-radius: 18px 18px 0 0; padding: 10px 16px calc(16px + var(--safe-b));
  animation: up .22s ease;
}
.sheet.full { height: 92vh; display: flex; flex-direction: column; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 36px; height: 5px; border-radius: 3px; background: #d8d0c4; margin: 0 auto 10px; }
.sheet h2 { font-size: 18px; margin: 4px 0 12px; }
.sheet h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 16px 0 8px; text-transform: uppercase; letter-spacing: .04em; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-close { font-size: 15px; color: var(--accent); font-weight: 600; padding: 6px 4px; }
.choice-list { display: grid; gap: 8px; }
.choice { display: flex; align-items: center; gap: 14px; padding: 14px; background: #fff; border-radius: 14px; text-align: left; border: 1px solid var(--line); }
.choice .ic { font-size: 24px; width: 32px; text-align: center; }
.choice small { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.choice.danger { color: var(--danger); }

.slides { display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 6px; }
.slide { flex: 0 0 82%; scroll-snap-align: center; background: #efe9df; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 5; display: grid; place-items: center; }
.slide img { width: 100%; height: 100%; object-fit: contain; }
.slide-count { text-align: center; font-size: 12px; color: var(--muted); margin-top: 4px; }

.strip { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 8px; }
.strip-item { flex: 0 0 84px; display: flex; flex-direction: column; gap: 4px; }
.strip-item .thumb { position: relative; width: 84px; height: 112px; border-radius: 8px; overflow: hidden; background: #eee; }
.strip-item .thumb img { width: 100%; height: 100%; object-fit: cover; }
.strip-item .thumb .n { position: absolute; top: 4px; left: 4px; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; border-radius: 8px; padding: 1px 6px; }
.strip-item .ctrl { display: flex; justify-content: space-between; }
.strip-item .ctrl button { flex: 1; padding: 4px 0; font-size: 14px; background: #efe9df; border-radius: 6px; }
.strip-item .ctrl button + button { margin-left: 4px; }

.row-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pos-row { display: flex; align-items: center; justify-content: space-between; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.pos-row .pos { font-weight: 600; }
.pos-row .arrows button { width: 40px; height: 36px; background: #efe9df; border-radius: 8px; font-size: 16px; }
.pos-row .arrows button + button { margin-left: 6px; }
.sheet .stack { display: grid; gap: 8px; }

.queue-list { display: grid; gap: 8px; }
.queue-item { display: flex; align-items: center; gap: 12px; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 8px; }
.queue-item .qn { width: 26px; height: 26px; border-radius: 13px; background: var(--accent); color: #fff; font-weight: 700; font-size: 13px; display: grid; place-items: center; flex: none; }
.queue-item .qthumb { width: 48px; height: 64px; border-radius: 6px; overflow: hidden; flex: none; background: #eee; }
.queue-item .qthumb img { width: 100%; height: 100%; object-fit: cover; }
.queue-item .qtext { flex: 1; min-width: 0; font-size: 14px; }
.queue-item .qtext small { display: block; color: var(--muted); font-size: 12px; }
.queue-item .arrows button { width: 34px; height: 34px; background: #efe9df; border-radius: 8px; }
.note { font-size: 13px; color: var(--muted); line-height: 1.45; }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%); z-index: 70;
  background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 12px; font-size: 14px;
  display: flex; gap: 14px; align-items: center; max-width: calc(100% - 32px); box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.toast button { color: #f0d8ae; font-weight: 700; }
