:root {
  --bg: #0b0b12;
  --surface: #16161f;
  --surface-2: #1e1e2a;
  --line: #2a2a38;
  --text: #f2f2f7;
  --muted: #9a9aa8;
  --primary: #5b8cff;
  --primary-press: #4a76e6;
  --danger: #ff5b6a;
  --ok: #43c47a;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  overscroll-behavior: none;                 /* suppress bounce (§10.2) */
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

#app { height: 100%; display: flex; flex-direction: column; }
.boot { display: grid; place-items: center; height: 100%; color: var(--muted); }

/* --- header / body / footer ------------------------------------------------ */
.app-header {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 12px) 8px calc(var(--safe-left) + 12px);
  background: var(--bg); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.app-title { font-size: 17px; font-weight: 600; margin: 0; flex: 1; text-align: center; }
.icon-btn { font-size: 26px; line-height: 1; width: 40px; height: 40px; border-radius: 10px; color: var(--text); }
.icon-btn:active { background: var(--surface-2); }
.icon-btn-spacer { width: 40px; height: 40px; display: inline-block; }
.app-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px calc(var(--safe-right) + 16px) 24px calc(var(--safe-left) + 16px);
  display: flex; flex-direction: column; gap: 16px;
}
.app-footer {
  padding: 12px calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 14px) calc(var(--safe-left) + 16px);
  border-top: 1px solid var(--line); background: var(--bg);
  display: flex; flex-direction: column; gap: 8px;
}

/* --- buttons --------------------------------------------------------------- */
.btn { border-radius: 12px; padding: 13px 16px; font-weight: 600; background: var(--surface-2); color: var(--text); }
.btn:active { transform: translateY(1px); }
.btn.full { width: 100%; }
.btn.sm { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active { background: var(--primary-press); }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.on { border-color: var(--primary); color: var(--primary); }
.btn:disabled { opacity: .45; pointer-events: none; }

.big-btn {
  width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px; display: flex; flex-direction: column; gap: 4px;
}
.big-btn.primary { background: linear-gradient(135deg, #26315a, var(--surface)); border-color: #34406e; }
.big-btn:active { background: var(--surface-2); }
.big-btn-label { font-size: 19px; font-weight: 700; }
.big-btn-sub { font-size: 13px; color: var(--muted); }

.home-actions { display: flex; flex-direction: column; gap: 12px; }
.mode-list { display: flex; flex-direction: column; gap: 12px; }
.lead { font-size: 15px; margin: 0; }
.note { font-size: 13px; color: var(--muted); margin: 0; line-height: 1.6; }
.section-title { font-size: 14px; color: var(--muted); margin: 8px 0 0; font-weight: 600; }

/* --- quota badge ----------------------------------------------------------- */
.quota-badge { font-size: 12px; font-weight: 600; padding: 6px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.quota-badge.exhausted { color: var(--danger); background: rgba(255,91,106,.12); }

/* --- history --------------------------------------------------------------- */
.history { display: flex; flex-direction: column; gap: 8px; }
.hist-item { display: flex; gap: 12px; align-items: center; padding: 8px; border-radius: 12px; background: var(--surface); text-align: left; }
.hist-item:active { background: var(--surface-2); }
.hist-thumb { width: 56px; height: 56px; border-radius: 10px; object-fit: cover; background: var(--surface-2); flex: none; }
.hist-thumb.placeholder { display: block; }
.hist-meta { min-width: 0; flex: 1; }
.hist-memo { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-sub { display: flex; gap: 8px; align-items: center; font-size: 12px; margin-top: 4px; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill-succeeded { background: rgba(67,196,122,.15); color: var(--ok); }
.pill-failed { background: rgba(255,91,106,.15); color: var(--danger); }
.pill-running, .pill-queued { background: rgba(91,140,255,.15); color: var(--primary); }
.pill-adopt { background: rgba(91,140,255,.2); color: var(--primary); }

/* --- fields ---------------------------------------------------------------- */
.stack { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; color: var(--muted); }
.field-hint { font-size: 12px; color: var(--muted); margin: 4px 0 0; line-height: 1.6; }
.text-input {
  width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; resize: vertical; color: var(--text);
}
.text-input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

/* --- single: image slot ---------------------------------------------------- */
.image-slot { display: flex; flex-direction: column; gap: 10px; }
.pick-btn {
  width: 100%; aspect-ratio: 1; border: 2px dashed var(--line); border-radius: var(--radius);
  display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center;
  color: var(--muted); background: var(--surface);
}
.pick-icon { font-size: 40px; }
.preview-img { width: 100%; aspect-ratio: 1; object-fit: contain; border-radius: var(--radius); background: var(--surface); }
.slot-row { display: flex; justify-content: space-between; align-items: center; }

/* --- multi: tray + slots --------------------------------------------------- */
.tray { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.tray-add { flex: none; width: 72px; height: 72px; border-radius: 12px; border: 1px dashed var(--line); color: var(--muted); font-size: 13px; background: var(--surface); }
.tray-thumb { flex: none; width: 72px; height: 72px; border-radius: 12px; overflow: hidden; border: 2px solid transparent; }
.tray-thumb.selected { border-color: var(--primary); }
.tray-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.slot {
  aspect-ratio: 1; border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
  position: relative; overflow: hidden; display: grid; place-items: center;
}
.slot.aux { border-style: dashed; }
.slot.filled { border-color: var(--primary); }
.slot img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.slot-plus { font-size: 26px; color: var(--muted); }
.slot-label { position: absolute; bottom: 0; left: 0; right: 0; font-size: 11px; padding: 3px; text-align: center; background: rgba(0,0,0,.55); }
.slot-aux-tag { margin-left: 4px; color: var(--muted); }
.warn { background: rgba(255,91,106,.1); border: 1px solid rgba(255,91,106,.3); border-radius: 12px; padding: 12px; font-size: 13px; display: flex; flex-direction: column; gap: 8px; }

/* --- overlays / spinner ---------------------------------------------------- */
.block-overlay, .modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 50;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}
.spinner-wrap { display: flex; flex-direction: column; gap: 12px; align-items: center; padding: 24px; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--primary); animation: spin 0.9s linear infinite; }
.spinner-label { color: var(--muted); font-size: 14px; }
@keyframes spin { to { transform: rotate(360deg); } }
.gen-status { text-align: center; font-size: 16px; }
.gen-memo { text-align: center; color: var(--muted); }

.fail-box { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.fail-msg { color: var(--danger); margin: 0 0 8px; }

/* --- modal ----------------------------------------------------------------- */
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 20px; width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 10px; }
.modal-title { margin: 0; font-size: 17px; }
.modal-msg { margin: 0; line-height: 1.6; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 6px; }

/* --- result / viewer ------------------------------------------------------- */
.viewer-wrap { position: relative; width: 100%; aspect-ratio: 1; background: var(--surface); border-radius: var(--radius); overflow: hidden; display: grid; place-items: center; }
.viewer { width: 100%; height: 100%; --poster-color: transparent; background: radial-gradient(circle at 50% 40%, #1b1b28, #0b0b12); }
.viewer-wrap .spinner-wrap { position: absolute; }
.viewer-fallback { padding: 20px; text-align: center; display: flex; flex-direction: column; gap: 10px; }
.result-meta { display: flex; flex-direction: column; gap: 8px; }
.adopt-row { display: flex; gap: 8px; }
.regen { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.regen summary { font-weight: 600; cursor: pointer; }
.regen .text-input { margin: 10px 0; }

/* --- toast / banners ------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 20px); transform: translate(-50%, 20px);
  background: #2a2a38; color: #fff; padding: 12px 18px; border-radius: 12px; max-width: 88%;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 90; font-size: 14px; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-error { background: #52222a; }
.toast-info { background: #2a2a38; }

.install-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-bottom) + 12px);
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px;
  display: flex; gap: 10px; align-items: flex-start; z-index: 80; box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.install-body strong { display: block; font-size: 15px; margin-bottom: 4px; }
.install-steps { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.6; }
.ios-share { display: inline-block; }
.install-close { font-size: 22px; color: var(--muted); width: 32px; height: 32px; flex: none; }

.update-banner {
  position: fixed; left: 12px; right: 12px; top: calc(var(--safe-top) + 8px);
  background: var(--primary); color: #fff; border-radius: 12px; padding: 10px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px; z-index: 95;
}
.update-banner .btn { background: rgba(255,255,255,.2); }

/* --- camera (§6.3) --------------------------------------------------------- */
.camera-screen { position: fixed; inset: 0; background: #000; z-index: 40; display: flex; flex-direction: column; }
.cam-stage { position: absolute; inset: 0; overflow: hidden; }
.cam-video { width: 100%; height: 100%; object-fit: cover; }
.cam-prev { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(78vw, 78vh); height: min(78vw, 78vh); object-fit: cover; opacity: .32; pointer-events: none; }
.cam-guide { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(78vw, 78vh); height: min(78vw, 78vh); border: 2px solid rgba(255,255,255,.9); border-radius: 8px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); pointer-events: none; }
.cam-top { position: absolute; top: 0; left: 0; right: 0; padding: calc(var(--safe-top) + 10px) 16px 10px; display: flex; flex-direction: column; gap: 10px; align-items: center; }
.cam-close { position: absolute; top: calc(var(--safe-top) + 8px); left: 12px; font-size: 26px; color: #fff; width: 40px; height: 40px; }
.cam-instr { color: #fff; font-size: 15px; font-weight: 600; text-align: center; text-shadow: 0 1px 3px rgba(0,0,0,.8); margin-top: 34px; }
.cam-dots { display: flex; gap: 8px; }
.cam-dot { width: 9px; height: 9px; border-radius: 50%; background: rgba(255,255,255,.35); }
.cam-dot.done { background: var(--ok); }
.cam-dot.current { background: #fff; transform: scale(1.3); }
.cam-dot.aux { outline: 1px dashed rgba(255,255,255,.5); outline-offset: 1px; }
.cam-bottom { position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 16px calc(var(--safe-bottom) + 14px); display: flex; flex-direction: column; gap: 12px; background: linear-gradient(transparent, rgba(0,0,0,.6)); }
.cam-thumbs { display: flex; gap: 8px; overflow-x: auto; }
.cam-thumb { flex: none; width: 52px; border-radius: 8px; overflow: hidden; border: 2px solid transparent; }
.cam-thumb.active { border-color: var(--primary); }
.cam-thumb img { width: 52px; height: 52px; object-fit: cover; display: block; }
.cam-thumb-label { display: block; font-size: 10px; text-align: center; color: #fff; background: rgba(0,0,0,.5); }
.cam-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; }
.cam-shutter { width: 68px; height: 68px; border-radius: 50%; background: #fff; border: 4px solid rgba(255,255,255,.4); justify-self: center; }
.cam-shutter:active { transform: scale(.94); }
.cam-skip { justify-self: start; color: #fff; padding: 10px 14px; border-radius: 10px; background: rgba(255,255,255,.15); }
.cam-done { justify-self: end; color: #fff; font-weight: 700; padding: 10px 16px; border-radius: 10px; background: var(--primary); }
.cam-done:disabled { opacity: .4; }
.cam-guidance { position: absolute; inset: 0; background: rgba(0,0,0,.8); display: grid; place-items: center; padding: 24px; z-index: 3; }
.cam-guidance-card { background: var(--surface); border-radius: 16px; padding: 22px; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.cam-guidance-card h2 { margin: 0; font-size: 18px; }
.cam-guidance-card p { margin: 0; color: var(--muted); line-height: 1.7; }
.cam-error { display: flex; flex-direction: column; gap: 12px; padding: 40px 24px; place-content: center; height: 100%; }

/* --- camera finish --------------------------------------------------------- */
.finish-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.finish-cell { margin: 0; }
.finish-cell img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; }
.finish-cell figcaption { font-size: 11px; color: var(--muted); text-align: center; margin-top: 3px; }
