:root {
  --bg: #f4efe6;
  --fg: #2b2723;
  --muted: #7a7066;
  --accent: #1f7a78;
  --accent-fg: #ffffff;
  --line: rgba(43, 39, 35, 0.14);
  --surface: #fffaf2;
  --wak-bg: #fffaf2;
  --wak-fg: var(--fg);
  --wak-muted: var(--muted);
  --wak-line: var(--line);
  --wak-accent: var(--accent);
  --wak-accent-fg: var(--accent-fg);
}

* { box-sizing: border-box; }

/* Safari 26 の上下バーの色は html の背景から取られる。body と同じ色にしておく */
html { background-color: var(--bg); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background-color: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* 画面の端に置くものはセーフエリアの分だけ内側に。高さは 100vh ではなく 100dvh。
   縦長: 帯 → 絵 → 回数 → 操作。横長: 左に絵、右に帯と操作 */
.app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "bar" "pic" "count" "panel";
  align-content: start;
  gap: 10px;
  max-width: 560px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  touch-action: manipulation;   /* ダブルタップ拡大を止める */
  user-select: none;
  -webkit-user-select: none;
}

.bar { grid-area: bar; display: flex; align-items: center; gap: 10px; min-height: 44px; }
.bar__title { margin: 0 auto 0 0; font-size: 19px; letter-spacing: 0.08em; }
.icon-btn {
  width: 44px; height: 44px; flex: none;
  border: 1px solid var(--line); background: var(--surface); border-radius: 50%;
  font-size: 18px; font-weight: 700; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; display: block; margin: auto; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="false"] { color: var(--muted); }

/* 絵: 幅いっぱいの正方形（最大 480px）。とても低い画面では、操作が見えるよう高さに合わせて小さくする */
.pic {
  grid-area: pic;
  position: relative;
  justify-self: center;
  width: min(100%, 480px, calc(100dvh - 250px));
  min-width: min(100%, 240px);
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg);
  box-shadow: 0 0 0 1px var(--line), 0 6px 18px rgba(60, 45, 30, 0.12);
  cursor: pointer;
  -webkit-touch-callout: none;
}
.pic canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; image-rendering: pixelated; image-rendering: crisp-edges; }
.pic__tag, .pic__back { position: absolute; margin: 0; pointer-events: none; }
.pic__tag {
  top: 8px; left: 8px; padding: 3px 10px; border-radius: 999px;
  background: rgba(43, 39, 35, 0.72); color: #fff; font-size: 13px;
}
.pic__back {
  inset: 0; margin: auto; width: max-content; height: max-content;
  padding: 10px 22px; border-radius: 999px;
  background: var(--accent); color: var(--accent-fg);
  font-size: 26px; font-weight: 800; letter-spacing: 0.06em;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
  animation: pop 0.3s ease-out;
}
@keyframes pop { from { transform: scale(0.6); opacity: 0; } }

.count { grid-area: count; display: flex; flex-direction: column; gap: 6px; }
.count__text { margin: 0; font-size: 15px; color: var(--muted); font-variant-numeric: tabular-nums; }
.count__text b { color: var(--fg); font-size: 20px; }
.bar-track { position: relative; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); }
.bar-mid { position: absolute; top: 0; bottom: 0; left: calc(50% - 1px); width: 2px; background: var(--fg); opacity: 0.55; }

.panel { grid-area: panel; display: flex; flex-direction: column; gap: 10px; min-width: 0; }

.btn {
  min-height: 48px; padding: 0 10px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  font-size: 15px; cursor: pointer; white-space: nowrap;
}
.btn:active { background: #efe6d6; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn[aria-pressed="true"] { border-color: var(--accent); background: rgba(31, 122, 120, 0.12); color: #145755; font-weight: 700; }
.btn--primary, .btn--play { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 700; }
.btn--play:active { background: #2a908d; }

.controls { display: grid; grid-template-columns: 1fr 1fr 1.25fr 1fr; gap: 6px; }
.controls .btn { padding: 0 4px; font-size: 14px; }
.controls .btn--play { font-size: 22px; min-height: 52px; }
.row { display: grid; grid-template-columns: 1fr 3fr; gap: 6px; }
.speed { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.row .btn { padding: 0 4px; font-size: 14px; }

.label { margin: 8px 0 0; font-size: 13px; font-weight: 600; color: var(--muted); }

/* おすすめの大きさ: 横に並べ、はみ出したら横にスクロール */
.presets { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.presets::-webkit-scrollbar { display: none; }
.presets .btn { flex: none; min-width: 56px; font-variant-numeric: tabular-nums; }

.size { display: grid; grid-template-columns: 56px 1fr 56px; gap: 8px; align-items: center; }
.btn--sq { font-size: 22px; padding: 0; }
.size__n { margin: 0; text-align: center; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.slider { width: 100%; height: 44px; margin: 0; accent-color: var(--accent); cursor: pointer; }

.note { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.5; }
.note--err { color: #b3261e; font-weight: 600; }
#periodNote { font-size: 14px; color: var(--fg); }

.pics { display: flex; flex-wrap: wrap; gap: 6px; }

.foot { margin-top: 8px; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.foot__actions { display: flex; gap: 8px; }
.pill {
  min-height: 44px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 0 16px; font-size: 14px; cursor: pointer;
}
.credit { margin: 0; font-size: 13px; color: var(--muted); }
.credit a { color: inherit; display: inline-flex; align-items: center; min-height: 44px; padding: 0 4px; }

/* 横長の画面: 絵を高さいっぱいにして左に置き、右の列だけスクロールしていく */
@media (orientation: landscape) and (min-width: 560px) {
  .app {
    max-width: 1280px;
    grid-template-columns: auto minmax(280px, 440px);
    grid-template-rows: auto auto 1fr;
    grid-template-areas: "pic bar" "pic count" "pic panel";
    justify-content: center;
    column-gap: 20px;
  }
  .pic {
    align-self: start;
    position: sticky;
    top: max(12px, env(safe-area-inset-top));
    width: min(calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 60vw, 640px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .pic__back { animation: none; }
}

/* 遊び方の重ね画面。隠すときは display: none（[hidden]） */
.help {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(43, 39, 35, 0.5);
  overflow-y: auto;
}
.help__box {
  width: min(100%, 420px);
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  padding: 20px; line-height: 1.6;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}
.help__box h2 { margin: 0 0 8px; font-size: 18px; }
.help__box ol { margin: 0 0 12px; padding-left: 1.3em; font-size: 15px; }
.help__box li + li { margin-top: 6px; }
.help__note { margin: 0 0 16px; font-size: 13px; color: var(--muted); }
.help__box .btn { width: 100%; }
