:root {
  --bg: #f3f4f6;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #db2777;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --seg-bg: #f3f4f6;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: #334155;
    --primary: #ec4899;
    --danger: #f87171;
    --seg-bg: #0f172a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

button, input, select { font: inherit; color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--primary);
  color: var(--primary-text);
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
}
.topbar h1 { margin: 0; font-size: 18px; }
.top-btn {
  height: 28px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, .7);
  border-radius: 14px;
  background: transparent;
  color: var(--primary-text);
  font-size: 13px;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 12px calc(env(safe-area-inset-bottom) + 24px);
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}
.card h2 { font-size: 16px; margin: 0 0 8px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.count { font-size: 14px; font-weight: 700; color: var(--primary); }

details.card summary { display: flex; align-items: center; gap: 6px; cursor: pointer; list-style: none; }
details.card summary::-webkit-details-marker { display: none; }
details.card summary::before { content: "▸"; color: var(--muted); }
details.card[open] summary::before { content: "▾"; }
details.card summary h2 { margin: 0; }
details.card[open] summary { margin-bottom: 8px; }
details.card summary .muted { margin-left: auto; }
details.card[open] summary .muted { display: none; }
.spot-count { margin: 0 0 8px; text-align: right; }

.hint { margin: 0 0 10px; font-size: 13px; line-height: 1.6; color: var(--muted); }
.muted { color: var(--muted); font-weight: normal; font-size: 12px; }
.error { margin: 8px 0 0; font-size: 14px; color: var(--danger); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; margin-top: 10px; }
.btn:disabled { opacity: .5; cursor: default; }
.actions { display: flex; gap: 8px; margin-top: 10px; }
.actions .btn { flex: 1; }
.actions .btn.primary { flex: 2; }

/* 写真の選択 */
.pick-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pick {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 3 / 4;
  border: 2px dashed var(--line);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  background: var(--seg-bg);
}
.pick img { width: 100%; height: 100%; object-fit: cover; }
.pick-label {
  position: absolute;
  left: 8px;
  top: 8px;
  padding: 2px 10px;
  border-radius: 10px;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}
.pick:not(.filled) .pick-label { position: static; background: none; color: var(--muted); font-size: 15px; }
.pick:not(.filled) .pick-label::after { content: "\A タップして選ぶ"; white-space: pre; font-weight: normal; font-size: 12px; }
.pick:not(.filled) .pick-label { text-align: center; }
.pick.filled { border-style: solid; }

/* 切り替えボタン */
.seg { display: flex; padding: 3px; margin-bottom: 10px; border-radius: 10px; background: var(--seg-bg); border: 1px solid var(--line); }
.seg button {
  flex: 1;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }

/* 四隅の指定・結果の表示 */
.corner-wrap, .view-wrap { position: relative; }
#cornerCanvas, #viewCanvas {
  display: block;
  width: 100%;
  border-radius: 8px;
  background: #000;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.loupe {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 120px;
  height: 120px;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .5);
  pointer-events: none;
}
.loupe.right { left: auto; right: 8px; }

.spot-tools { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.spot-tools label { flex: 1; display: flex; align-items: center; gap: 8px; font-size: 14px; white-space: nowrap; }
.spot-tools input { flex: 1; min-width: 0; }
.spot-tools .btn { min-height: 38px; padding: 0 12px; font-size: 14px; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; font-size: 14px; }
.row input[type="range"] { flex: 1; max-width: 60%; }
.row select { min-height: 36px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
.row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--primary); }

/* 作成した画像 */
.out { margin: 16px 0 0; }
.out figcaption { font-size: 14px; font-weight: 700; margin-bottom: 6px; }
.out-img { display: block; width: 100%; border-radius: 8px; border: 1px solid var(--line); }
.muted-line { margin: 6px 0 0; font-size: 12px; color: var(--muted); text-align: right; }

/* カード内の折りたたみ */
details.sub { margin-top: 12px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; }
details.sub summary { font-size: 14px; font-weight: 600; cursor: pointer; }
details.sub[open] summary { margin-bottom: 4px; }

/* 処理中の表示 */
.busy {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .45);
}
.busy-box { padding: 20px 28px; border-radius: 12px; background: var(--card); text-align: center; }
.busy-box p { margin: 10px 0 0; font-size: 14px; }
.spinner {
  width: 32px;
  height: 32px;
  margin: 0 auto;
  border: 3px solid var(--line);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
