:root {
  --bg: #0f141b;
  --panel: #171f2b;
  --panel-2: #1f2a3a;
  --line: #2b3a4f;
  --text: #e8eef6;
  --muted: #93a3b8;
  --accent: #3a8dff;
  --accent-strong: #1f6fff;
  --radius: 14px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP",
    "Segoe UI", sans-serif;
  background: radial-gradient(1200px 600px at 50% -10%, #1b2740 0%, var(--bg) 60%);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
  padding: 22px 28px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}
.brand-mark {
  font-size: 34px;
  line-height: 1;
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.4));
}
.brand h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.02em;
}
.brand p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--muted);
}

/* ---------- Layout ---------- */
.app-main {
  flex: 1;
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 8px 24px 32px;
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 24px;
  align-items: start;
}
@media (max-width: 860px) {
  .app-main {
    grid-template-columns: 1fr;
  }
}

/* ---------- Stage ---------- */
.stage-area {
  width: 100%;
}
.stage-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
#camera {
  display: none;
}
#stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* プレビューは鏡像（自撮り体験）。撮影データ自体は正像。 */
  transform: scaleX(-1);
}

/* ガイド枠 */
.guide-frame {
  position: absolute;
  pointer-events: none;
  border: 2px dashed rgba(255, 255, 255, 0.75);
  border-radius: 6px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.28);
}
.guide-corner {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 3px solid var(--accent);
}
.guide-corner.tl {
  top: -2px;
  left: -2px;
  border-right: none;
  border-bottom: none;
}
.guide-corner.tr {
  top: -2px;
  right: -2px;
  border-left: none;
  border-bottom: none;
}
.guide-corner.bl {
  bottom: -2px;
  left: -2px;
  border-right: none;
  border-top: none;
}
.guide-corner.br {
  bottom: -2px;
  right: -2px;
  border-left: none;
  border-top: none;
}
.guide-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px dashed rgba(255, 255, 255, 0.55);
}
.guide-line i {
  position: absolute;
  left: 6px;
  top: -9px;
  font-size: 10px;
  font-style: normal;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(0, 0, 0, 0.35);
  padding: 1px 5px;
  border-radius: 4px;
}
.guide-top {
  top: 14%;
}
.guide-chin {
  top: 78%;
}
.guide-vline {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 0;
  border-left: 1px dashed rgba(255, 255, 255, 0.6);
}

/* カウントダウン */
.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 120px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 6px 30px rgba(0, 0, 0, 0.6);
  opacity: 0;
  transform: scale(0.5);
  pointer-events: none;
}
.countdown.show {
  animation: pop 0.8s ease-out;
}
@keyframes pop {
  0% {
    opacity: 0;
    transform: scale(1.6);
  }
  25% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.85);
  }
}

/* フラッシュ */
.flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}
.flash.fire {
  animation: flash 0.32s ease-out;
}
@keyframes flash {
  0% {
    opacity: 0;
  }
  15% {
    opacity: 0.92;
  }
  100% {
    opacity: 0;
  }
}

/* ステータス */
.stage-status {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  justify-content: center;
  background: rgba(6, 10, 16, 0.78);
  color: var(--text);
  text-align: center;
  padding: 24px;
  font-size: 14px;
}
.stage-status[hidden] {
  display: none;
}
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Control panel ---------- */
.control-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.control-group {
  margin-bottom: 22px;
}
.control-group h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-transform: uppercase;
}

/* 背景スウォッチ */
.swatches {
  display: flex;
  gap: 12px;
}
.swatch {
  flex: 1;
  aspect-ratio: 1;
  border-radius: 10px;
  border: 2px solid transparent;
  cursor: pointer;
  position: relative;
  transition: transform 0.12s ease, border-color 0.12s ease;
  outline: none;
}
.swatch:hover {
  transform: translateY(-2px);
}
.swatch.active {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--accent);
}
.swatch span {
  position: absolute;
  bottom: -20px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
}

/* サイズリスト */
.size-list {
  display: grid;
  gap: 8px;
}
.size-option {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  text-align: left;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.size-option:hover {
  border-color: var(--accent);
}
.size-option.active {
  border-color: var(--accent);
  background: rgba(58, 141, 255, 0.16);
}
.size-option .dim {
  font-size: 12px;
  color: var(--muted);
}

/* 撮影ボタン */
.shutter-btn {
  width: 100%;
  margin-top: 6px;
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.12s ease, transform 0.06s ease;
}
.shutter-btn:hover:not(:disabled) {
  filter: brightness(1.08);
}
.shutter-btn:active:not(:disabled) {
  transform: scale(0.98);
}
.shutter-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.shutter-ring {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 3px solid #fff;
  background: rgba(255, 255, 255, 0.25);
}
.hint {
  margin: 12px 2px 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

/* ---------- Result modal ---------- */
.result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 14, 0.82);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 50;
}
.result-overlay[hidden] {
  display: none;
}
.result-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 24px;
  width: min(420px, 100%);
  box-shadow: var(--shadow);
  text-align: center;
}
.result-card h2 {
  margin: 0 0 16px;
  font-size: 18px;
}
.result-preview {
  display: flex;
  justify-content: center;
  padding: 16px;
  background: repeating-conic-gradient(#cfd6df 0 25%, #e8edf3 0 50%) 50% / 22px 22px;
  border-radius: 12px;
}
#resultCanvas {
  max-height: 320px;
  max-width: 100%;
  border-radius: 4px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}
.result-meta {
  margin: 14px 0 18px;
  font-size: 13px;
  color: var(--muted);
}
.result-actions {
  display: flex;
  gap: 12px;
}
.btn {
  flex: 1;
  padding: 13px;
  border-radius: 11px;
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.12s ease, transform 0.06s ease;
}
.btn:active {
  transform: scale(0.98);
}
.btn-primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  color: #fff;
}
.btn-primary:hover {
  filter: brightness(1.08);
}
.btn-secondary {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
}
.btn-secondary:hover {
  border-color: var(--accent);
}

/* ---------- Footer ---------- */
.app-footer {
  text-align: center;
  padding: 18px;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- コンビニ印刷セクション ---------- */
/* .btn を anchor でも使えるように */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
/* 結果カードが縦に伸びても収まるように */
.result-card {
  max-height: 90vh;
  overflow-y: auto;
}
.konbini {
  margin-top: 22px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
  text-align: left;
}
.konbini h3 {
  margin: 0 0 8px;
  font-size: 15px;
}
.konbini-desc {
  margin: 0 0 14px;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--muted);
}
.konbini-desc strong {
  color: var(--accent);
  font-size: 15px;
}
.chain-select {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
}
.chain {
  flex: 1;
  padding: 11px 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.chain:hover {
  border-color: var(--accent);
}
.chain.active {
  border-color: var(--accent);
  background: rgba(58, 141, 255, 0.16);
}
.konbini-actions {
  display: flex;
  gap: 12px;
}
.konbini-steps {
  margin: 16px 0 0;
  padding-left: 1.3em;
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--text);
}
.konbini-steps li {
  margin-bottom: 5px;
}
.konbini-note {
  margin: 12px 0 0;
  font-size: 11px;
  line-height: 1.5;
  color: var(--muted);
}

/* 撮影画面のコンビニ印刷ボタン＆案内モーダル */
.konbini-cta {
  width: 100%;
  margin-top: 14px;
}
.info-card {
  text-align: left;
}
.info-card h2 {
  text-align: center;
}
.info-lead {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
  margin: 0 0 14px;
}
.info-lead strong {
  color: var(--accent);
}
.info-steps {
  margin: 0 0 20px;
  padding-left: 1.3em;
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--muted);
}
.info-steps li {
  margin-bottom: 5px;
}
.info-actions {
  display: flex;
}
.info-actions .btn {
  flex: 1;
}

/* ---------- 処理ログ（背景アンビエント） ---------- */
/* 極小フォント・低コントラスト。意識には残りにくいが処理内容は読める粒度。 */
.proc-log {
  position: fixed;
  left: 0;
  bottom: 48px; /* フッターの上、画面の左下に固定 */
  z-index: 0; /* コンテンツ(1)より背面 → 不透明なカメラ枠/パネルが覆い、映像と重ならない */
  width: min(42vw, 460px);
  height: min(46vh, 560px);
  display: flex;
  flex-direction: column;
  justify-content: flex-end; /* 下揃え＝ログは最下部から積み上がる（開始位置が下） */
  margin: 0;
  padding: 14px 18px;
  pointer-events: none;
  user-select: none;
  overflow: hidden;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
  font-size: 8px;
  line-height: 1.6;
  letter-spacing: 0.04em;
  color: rgba(140, 200, 235, 0.22); /* 淡いシアン（やや濃いめ） */
  white-space: pre;
  /* 下が濃く、上へフェード */
  -webkit-mask-image: linear-gradient(to top, #000 60%, transparent 100%);
  mask-image: linear-gradient(to top, #000 60%, transparent 100%);
}
.proc-line {
  break-inside: avoid;
  margin: 0;
}
/* コンテンツは背景ログより前面に */
.app-header,
.app-main,
.app-footer {
  position: relative;
  z-index: 1;
}
@media (prefers-reduced-motion: reduce) {
  .proc-log {
    display: none;
  }
}

/* ---------- 撮影結果：画面右側のサイドパネル（ポップアップ／中央モーダルではない・背景ログが見え続ける） ---------- */
.result-inline {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(384px, 88vw);
  z-index: 40; /* コンテンツより前面・案内モーダル(50)より背面 */
  margin: 0;
  padding: 0;
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: -16px 0 48px rgba(0, 0, 0, 0.45);
}
.result-inline[hidden] {
  display: none;
}
.result-inline:not([hidden]) {
  animation: slideInRight 0.22s ease-out;
}
@keyframes slideInRight {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}
.result-inline .result-card {
  width: 100%;
  max-width: none;
  min-height: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
  box-shadow: none;
  max-height: none;
  overflow: visible;
}
@media (prefers-reduced-motion: reduce) {
  .result-inline:not([hidden]) {
    animation: none;
  }
}
/* 常時表示パネル分の余白を右に確保（広い画面ではメインUIが隠れない） */
@media (min-width: 700px) {
  body {
    padding-right: 384px;
  }
}
/* 撮影前の空状態 */
.result-empty {
  display: none;
}
.result-card.is-empty #resultCanvas {
  display: none;
}
.result-card.is-empty .result-preview {
  background: rgba(255, 255, 255, 0.03);
}
.result-card.is-empty .result-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 220px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  text-align: center;
}
.result-empty-icon {
  font-size: 40px;
  opacity: 0.5;
}
.result-card.is-empty .result-meta,
.result-card.is-empty .result-actions,
.result-card.is-empty .konbini {
  display: none;
}

/* ---------- 狭い画面（スマホ等）: 撮影結果は固定パネルではなくポップアップ ---------- */
@keyframes fadeInModal {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@media (max-width: 699px) {
  /* 撮影前（空状態）はパネルごと非表示にして画面を占有しない */
  .result-inline:has(.result-card.is-empty) {
    display: none;
  }
  /* 撮影後は中央ポップアップ（背景オーバーレイ） */
  .result-inline {
    position: fixed;
    inset: 0;
    width: auto;
    height: auto;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 18px;
    overflow-y: auto;
    background: rgba(4, 8, 14, 0.82);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    border-left: none;
    box-shadow: none;
  }
  .result-inline:not([hidden]) {
    animation: fadeInModal 0.18s ease-out;
  }
  .result-inline .result-card {
    width: min(440px, 100%);
    max-width: 440px;
    min-height: 0;
    max-height: 88vh;
    margin: auto;
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 18px;
    box-shadow: var(--shadow);
  }
}

/* ---------- スマホ用ボタン最適化（タップしやすく） ---------- */
@media (max-width: 699px) {
  /* コントロールパネル */
  .swatches {
    gap: 14px;
  }
  .size-list {
    gap: 12px;
  }
  .size-option {
    align-items: center;
    min-height: 56px;
    padding: 14px 16px;
    font-size: 15px;
  }
  .size-option .dim {
    font-size: 13px;
  }
  .shutter-btn {
    padding: 18px;
    font-size: 18px;
    border-radius: 14px;
  }
  .shutter-ring {
    width: 22px;
    height: 22px;
  }
  .konbini-cta {
    min-height: 54px;
    padding: 16px;
    font-size: 15px;
  }

  /* 結果ポップアップ内: ボタンは全幅・縦並び・大きめ */
  .result-actions {
    flex-direction: column;
    gap: 12px;
  }
  .result-actions .btn {
    flex: 0 0 auto;
    width: 100%;
    min-height: 54px;
    padding: 16px;
    font-size: 16px;
  }
  .chain-select {
    flex-direction: column;
    gap: 12px;
  }
  .chain {
    flex: 0 0 auto;
    width: 100%;
    min-height: 52px;
    padding: 15px 16px;
    font-size: 15px;
  }
  .konbini-actions {
    flex-direction: column;
    gap: 12px;
  }
  .konbini-actions .btn {
    flex: 0 0 auto;
    width: 100%;
    min-height: 54px;
    padding: 16px;
    font-size: 16px;
  }
  .konbini h3 {
    font-size: 16px;
  }
  .konbini-desc,
  .konbini-steps {
    font-size: 13px;
  }
}

/* ---------- スマホ用 大胆レイアウト: 縦長カメラ＋直下シャッター＋1画面完結（スクロールなし） ---------- */
@media (max-width: 699px) {
  /* 画面ぴったり・スクロールなし */
  body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }
  .app-header {
    flex: 0 0 auto;
    padding: 8px 14px;
  }
  .app-header h1 {
    font-size: 16px;
  }
  .app-header .brand p {
    display: none;
  }
  .brand-mark {
    font-size: 24px;
  }
  .app-footer {
    display: none;
  }

  /* メインを縦フレックスにして残り高さを埋める */
  .app-main {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 12px;
    width: 100%;
  }
  /* コントロールパネルの箱を解除し、子要素を直接並べ替え可能に */
  .control-panel {
    display: contents;
  }

  /* 並び順: カメラ(1) → 撮影ボタン(2) → 背景色/サイズ(3) → コンビニ(4) */
  .stage-area {
    order: 1;
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  /* カメラは残り高さいっぱいの縦長。比率はJSが実映像に合わせる */
  .stage-wrap {
    height: 100%;
    width: auto;
    max-width: 100%;
    aspect-ratio: 3 / 4;
  }
  .shutter-btn {
    order: 2;
    flex: 0 0 auto;
    padding: 15px;
    font-size: 17px;
  }
  .control-group {
    order: 3;
    flex: 0 0 auto;
    margin-bottom: 0;
  }
  .control-group h2 {
    margin: 0 0 6px;
    font-size: 11px;
  }
  .hint {
    display: none;
  }
  .konbini-cta {
    order: 4;
    flex: 0 0 auto;
    margin-top: 0;
    min-height: 0;
    padding: 12px;
    font-size: 14px;
  }

  /* 背景色: 小さめ・ラベル省略 */
  .swatches {
    gap: 8px;
  }
  .swatch {
    aspect-ratio: auto;
    height: 38px;
    border-radius: 8px;
  }
  .swatch span {
    display: none;
  }

  /* サイズ: 2列グリッドで省スペース */
  .size-list {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .size-option {
    flex-direction: column;
    align-items: flex-start;
    gap: 1px;
    min-height: 0;
    padding: 9px 12px;
    font-size: 13px;
  }
  .size-option .dim {
    font-size: 11px;
  }
}
