:root {
  --bg: #f4f2f0;
  --panel-bg: #ffffff;
  --border: #ddd6d0;
  --text: #2b2b2b;
  --accent: #ff3399;
  --accent-dark: #d6207f;
  --toggle-on: #2f6fed;
  --toggle-on-dark: #1d54c4;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b1a;
    --panel-bg: #262422;
    --border: #3a3735;
    --text: #ece8e4;
    --toggle-on: #5b8dfa;
    --toggle-on-dark: #3f6fe0;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

:root {
  --footer-height: 4.75rem;
  /* ヘッダーは fixed ではないが、キャンバスの最大高さを決めるのに要る。 */
  --header-height: 3.25rem;
}

body {
  margin: 0;
  font-family: "Hiragino Sans", "Yu Gothic", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  /* フッターは fixed なので、その分の高さ（＋ホームインジケータ等の safe area）を
     確保してコンテンツが隠れないようにする。 */
  padding-bottom: calc(var(--footer-height) + env(safe-area-inset-bottom) + 0.5rem);
}

/* 入力デバイスによる文言の出し分け。マウス等（hover 可能）とタッチとで
   「クリック」「タップ」や、ドラッグ＆ドロップの案内を切り替える。 */
.coarse-only { display: none; }

@media (hover: none) and (pointer: coarse) {
  .fine-only { display: none; }
  /* revert で要素本来の display（p なら block、span なら inline）に戻す。 */
  .coarse-only { display: revert; }
}

/* ヘッダーは position: fixed にしない。1画面で完結するツールなので、編集中は
   スクロールで画面外へ送れるほうが、キャンバスに使える高さが増えて都合が良い。 */
.app-header {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.8rem 1.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: var(--header-height);
}

.app-header .logo {
  width: 1.75rem;
  height: 1.75rem;
  display: block;
}

.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.app-main {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  padding: 1.5rem 1.5rem 2rem;
  max-width: 1200px;
  margin: 0 auto;
  align-items: flex-start;
}

.canvas-area {
  flex: 1 1 500px;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.7rem;
}

/* 画像選択前だけ出す説明文（intro / about）。SEO・インデックス対策として
   初期HTMLに置き、画像を読み込んだら hidden で消す（編集画面を狭めないため）。
   後からJSで挿入する形にはしない — クローラが見るのは初期状態のため。
   1行の長さが伸びすぎないよう max-width で folded に保つ。 */
/* .canvas-area は align-items: center なので、放っておくと中央に寄って
   ドロップゾーンと左端が揃わない。 */
.intro {
  align-self: flex-start;
  max-width: 46rem;
  margin: 0;
  line-height: 1.9;
  font-size: 0.95rem;
}

/* 外側は .app-main と同じ幅・余白にして左端を揃え、行長だけ内側で詰める。 */
.about {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem 2.5rem;
  line-height: 1.9;
  font-size: 0.95rem;
}

.about > * {
  max-width: 46rem;
}

.about h2 {
  font-size: 1.05rem;
  margin: 2rem 0 0.6rem;
}

.about p {
  margin: 0 0 1rem;
}

.about ol {
  margin: 0 0 1rem;
  padding-left: 1.4rem;
}

.about li {
  margin-bottom: 0.6rem;
}

.drop-zone {
  width: 100%;
  min-height: 300px;
  border: 2px dashed var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.8rem;
  background: var(--panel-bg);
  text-align: center;
  padding: 1rem;
}

.drop-zone.dragover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--panel-bg));
}

.file-btn {
  background: var(--accent);
  color: white;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  /* 404ページではリンクとして使うため、下線を消しておく。 */
  text-decoration: none;
}

/* hover 系はタッチ端末では「タップ後に押しっぱなしの見た目」が残るため、
   ポインタでホバーできる環境に限定する。宣言順（＝優先順位）を変えないよう、
   まとめずに元の位置のままメディアクエリで囲っている。 */
@media (hover: hover) {
  .file-btn:hover {
    background: var(--accent-dark);
  }
}

.canvas-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.5rem;
  overflow: auto;
}

#canvas {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - var(--header-height) - var(--footer-height) - 7rem);
  /* dvh はモバイルのアドレスバー伸縮を除いた実際の表示領域を指す。 */
  max-height: calc(100dvh - var(--header-height) - var(--footer-height) - 7rem);
  width: auto;
  height: auto;
  border-radius: 6px;
  cursor: crosshair;
  /* 縦スワイプによるページスクロールは残しつつ（キャンバスの下にコントロール
     パネルが縦積みされるため）、ダブルタップズームは抑止する。 */
  touch-action: pan-y;
  /* iOS の長押しで画像メニューやテキスト選択が出るのを防ぐ。 */
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

#canvas.erase-mode {
  cursor: not-allowed;
}

.hint {
  font-size: 0.85rem;
  color: #918a83;
  margin: 0;
}

.controls {
  flex: 0 0 260px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
}

.checkbox-group {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.control-group label {
  display: flex;
  justify-content: space-between;
}

input[type="range"] {
  width: 100%;
}

input[type="color"] {
  width: 100%;
  height: 2.2rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  background: none;
}

.color-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.color-row input[type="color"] {
  flex: 1 1 auto;
  min-width: 0;
}

.color-row .btn {
  flex: 0 0 auto;
  white-space: nowrap;
}

.section-title {
  font-size: 0.95rem;
  margin: 0;
}

hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.2rem 0;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--text);
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
}

@media (hover: hover) {
  .btn:hover {
    background: var(--bg);
  }
}

.btn.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

@media (hover: hover) {
  .btn.primary:hover {
    background: var(--accent-dark);
  }
}

.toggle-btn.active {
  background: var(--toggle-on);
  color: white;
  border-color: var(--toggle-on);
}

@media (hover: hover) {
  .toggle-btn.active:hover {
    background: var(--toggle-on-dark);
  }
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.app-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  /* safe area の分だけ実際の高さが伸びるので、固定値ではなく下限として指定する。 */
  min-height: var(--footer-height);
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.5rem 1rem calc(0.5rem + env(safe-area-inset-bottom));
  background: var(--panel-bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.08);

  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(4rem, 1fr);
  gap: 0.4rem;
  overflow-x: auto;
}

.app-footer .icon-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  padding: 0.4rem 0.2rem;
  min-width: 0;
}

.app-footer .icon-btn .icon {
  display: flex;
}

.app-footer .icon-btn .icon svg {
  width: 22px;
  height: 22px;
}

.app-footer .icon-btn .label {
  font-size: 0.68rem;
  font-weight: 600;
  white-space: nowrap;
}

/* 成長中の水玉をキャンセルするボタン。タッチ端末には右クリックが無いため、
   成長中だけ画面に出す明示的な取り消し手段として用意している。フッターの
   すぐ上に固定表示し、ページのどこまでスクロールしていても押せるようにする。 */
.cancel-growing {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--footer-height) + env(safe-area-inset-bottom) + 0.75rem);
  z-index: 110;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 44px;
  padding: 0.5rem 1rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.cancel-growing .icon {
  display: flex;
}

.cancel-growing .icon svg {
  width: 18px;
  height: 18px;
}

/* ---- 狭い画面（スマホ・タブレット縦） ---- */
/* 900px は「キャンバス(500px)＋コントロールパネル(260px)＋余白」が横に並ぶ
   下限のあたり。これを下回ったら縦積みに切り替える。 */
@media (max-width: 900px) {
  :root {
    --header-height: 2.75rem;
  }

  .app-header {
    padding: 0.6rem 0.75rem 0;
    gap: 0.5rem;
  }

  .app-header .logo {
    width: 1.5rem;
    height: 1.5rem;
  }

  .app-header h1 {
    font-size: 1.05rem;
  }

  .app-main {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem 0.75rem 1.5rem;
  }

  .about {
    padding: 0 0.75rem 2rem;
  }

  /* 横並び用の flex-basis を打ち消して、確実に1カラムで縦積みにする。 */
  .canvas-area,
  .controls {
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
  }

  /* コントロールパネルが下に続いていることが分かるよう、キャンバスの
     最大高さを desktop より抑えて画面を占有しきらないようにする。 */
  #canvas {
    max-height: calc(100vh - var(--header-height) - var(--footer-height) - 10rem);
    max-height: calc(100dvh - var(--header-height) - var(--footer-height) - 10rem);
  }

  /* 6ボタンを横スクロールさせず必ず1画面に収める。最小幅の下限を外して
     均等に縮め、そのぶん余白を詰める。 */
  .app-footer {
    grid-auto-columns: minmax(0, 1fr);
    gap: 0.25rem;
    padding: 0.4rem 0.5rem calc(0.4rem + env(safe-area-inset-bottom));
    overflow-x: hidden;
  }

  /* 指で押せるサイズ（44px）を確保する。 */
  .app-footer .icon-btn {
    min-height: 44px;
    padding: 0.3rem 0.1rem;
  }

  .app-footer .icon-btn .label {
    font-size: 0.62rem;
  }

  /* スライダーのつまみも指で扱えるよう、行の高さを広げる。 */
  .controls input[type="range"] {
    height: 2rem;
  }

  .color-row {
    flex-wrap: wrap;
  }

  .color-row .btn {
    min-height: 44px;
  }
}

/* ---- タッチ端末（画面幅によらず） ---- */
/* 上書き対象のベース宣言より後ろに置く必要があるため、文言出し分けの
   メディアクエリとは別に、ファイル末尾にまとめている。 */
@media (hover: none) and (pointer: coarse) {
  /* 破線の枠はドロップ先を示すもので、ドラッグ＆ドロップができない環境では
     意味を持たないため実線にする。 */
  .drop-zone {
    border-style: solid;
    min-height: 220px;
  }

  /* 指で押せるサイズ（44px）を確保する。 */
  .file-btn {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
  }
}
