/* テーマは app.js が <html data-theme="light|dark"> を立てて切り替える（localStorage 保存）。
   紙・線の色は draw.js の THEMES。ここは UI（body の余白＝レターボックス色・パネル等）。 */
:root[data-theme="light"] { color-scheme: light; --letterbox: #f2f2f2;
  --toast-bg: rgba(28,28,28,0.92); --toast-fg: #fff;      --toast-bd: rgba(255,255,255,0.22);
  --dlg-bg: #ffffff; --dlg-fg: #222222; }
:root[data-theme="dark"]  { color-scheme: dark;  --letterbox: #0c0c0c;
  --toast-bg: rgba(238,236,230,0.96); --toast-fg: #181818; --toast-bd: rgba(0,0,0,0.30);
  --dlg-bg: #1b1b1b; --dlg-fg: #ececec; }
:root[data-theme="sepia"] { color-scheme: light; --letterbox: #e6dcc4;   /* セピア（紙より少し濃い縁色） */
  --toast-bg: rgba(74,53,36,0.95); --toast-fg: #f4ecd8;   --toast-bd: rgba(244,236,216,0.30);
  --dlg-bg: #f7efdc; --dlg-fg: #3a2b1d; }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", sans-serif;
  background: var(--letterbox, #f2f2f2);
  -webkit-tap-highlight-color: transparent;
}

/* 全画面 canvas */
#board {
  position: fixed; inset: 0;
  width: 100vw; height: 100vh;
  width: 100dvw; height: 100dvh;
  display: block;
  /* 長押しの iOS コールアウト・選択・スクロールを抑制（タップ/長押しジェスチャー用） */
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}

/* ハンバーガー（overlay トグル） */
#menuBtn {
  position: fixed; top: 12px; right: 12px; z-index: 20;
  width: 42px; height: 42px; border: none; border-radius: 10px;
  background: rgba(255,255,255,0.85); color: #222;
  font-size: 20px; line-height: 1; cursor: pointer;
  box-shadow: 0 1px 6px rgba(0,0,0,0.15);
  backdrop-filter: blur(4px);
  transition: opacity 0.45s ease;
}
/* スクリーンセーバー風の自動フェード: 無操作でハンバーガーとマウスカーソルを隠す（app.js が body.idle を立てる） */
body.idle { cursor: none; }
body.idle #menuBtn,
body.idle #pauseFab { opacity: 0; pointer-events: none; }   /* FAB もハンバーガーと同じく消す */
@media (prefers-reduced-motion: reduce) { #menuBtn, #pauseFab { transition: opacity 0.15s ease; } }

/* 操作モードの一時停止/再開ボタン（画面下中央の overlay） */
#pauseFab {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  z-index: 20; width: 60px; height: 60px; border: none; border-radius: 50%;
  background: rgba(255,255,255,0.9); color: #222;
  font-size: 24px; line-height: 1; cursor: pointer;
  box-shadow: 0 2px 12px rgba(0,0,0,0.2); backdrop-filter: blur(4px);
  display: none;
  transition: opacity 0.45s ease;   /* 無操作フェード用（body.idle）。ハンバーガーと揃える */
}
/* FAB はタッチ端末（主ポインタが指）のときだけ出す。PC はショートカットキーがあるので出さない */
@media (pointer: coarse) { #pauseFab.show { display: block; } }
:root[data-theme="dark"] #pauseFab { background: rgba(40,40,40,0.9); color: #eee; }

/* トースト通知（画面下中央。FAB より上に出して重ならないように） */
#toast {
  position: fixed; left: 50%; bottom: 84px; z-index: 30;
  transform: translate(-50%, 8px); pointer-events: none;
  padding: 9px 16px; border-radius: 999px;
  /* テーマ連動の地・文字色＋どのテーマでも輪郭が出る縁（初期 data-theme 未設定時のフォールバックつき） */
  background: var(--toast-bg, rgba(20,20,20,0.92)); color: var(--toast-fg, #fff);
  border: 1px solid var(--toast-bd, rgba(255,255,255,0.22));
  font-size: 13px; white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0,0,0,0.28);
  opacity: 0; transition: opacity 0.2s ease, transform 0.2s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
/* ボタン付きのときだけクリックを受け付ける（通常のトーストは下の canvas 操作を邪魔しない） */
#toast.has-action { pointer-events: auto; display: inline-flex; align-items: center; gap: 10px; }
#toast .toast-btn {
  flex: none; padding: 3px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid currentColor; background: transparent; color: inherit;
  font-size: 12px; line-height: 1.5; font-family: inherit;
}
#toast .toast-btn:active { background: rgba(128,128,128,0.3); }
@media (prefers-reduced-motion: reduce) {
  #toast { transition: opacity 0.15s ease; transform: translate(-50%, 0); }
}

/* コントロールパネル（overlay。canvas のサイズを変えない） */
#panel {
  position: fixed; top: 0; right: 0; z-index: 15;
  width: min(320px, 86vw); height: 100%;
  padding: 20px; padding-top: 64px;
  background: rgba(255,255,255,0.62);
  backdrop-filter: blur(14px);
  box-shadow: -2px 0 16px rgba(0,0,0,0.12);
  transform: translateX(0); transition: transform 0.2s ease;
  overflow-y: auto;
  color: #222;
}
#panel.hidden { transform: translateX(100%); }
:root[data-theme="dark"] #menuBtn { background: rgba(40,40,40,0.85); color: #eee; }
:root[data-theme="dark"] #panel { background: rgba(18,18,18,0.66); color: #eee; }

/* テーマ切替（パネル左上。ライト/ダーク/セピアの3択セグメント。各チップは紙色＋線色の見本） */
/* テーマ切替と ? は1つの行にまとめて中心を揃える（高さが違うので top 揃えだとズレる） */
#panelTools {
  position: absolute; top: 14px; left: 16px; z-index: 1;
  display: flex; align-items: center; gap: 10px;
}
#theme {
  display: flex; gap: 2px; padding: 2px; border-radius: 9px;
  border: 1px solid rgba(128,128,128,0.35);
}
#theme button {
  display: grid; place-items: center; width: 30px; height: 28px; padding: 0; cursor: pointer;
  border: 0; border-radius: 7px; background: transparent;
}
#theme button.on { background: rgba(128,128,128,0.22); }
#theme .th-chip { width: 15px; height: 15px; border-radius: 4px; border: 2px solid; }
#theme button.on .th-chip { box-shadow: 0 0 0 2px rgba(128,128,128,0.35); }

#panel .speed-row.hidden { display: none; }   /* 非アクティブモードの速度スライダーを隠す（#panel label に勝つ詳細度） */
#panel #soundOpts.hidden { display: none; }   /* 音モード OFF のときは音の選択肢を隠す */
#panel .speed-row b { font-variant-numeric: tabular-nums; opacity: 0.9; }
#panel .speed-row input[type="range"] { display: block; width: 100%; margin: 4px 0 0; }
/* 段階の目盛り線（--n 本を等間隔で。左右に thumb 半径ぶんの余白を取り、つまみ位置に近づける） */
#panel .speed-row .ticks {
  display: block; height: 6px; margin: 1px 8px 0;
  background-image: repeating-linear-gradient(to right,
    currentColor 0 1px, transparent 1px calc((100% - 1px) / (var(--n) - 1)));
  opacity: 0.35;
}
#panel h1 { margin: 0; font-size: 22px; letter-spacing: 0.02em; }
#panel h1 a { color: inherit; text-decoration: none; }   /* トップ / へのリンク。色も装飾も変えない */
#panel .tag { margin: 4px 0 18px; font-size: 13px; opacity: 0.65; }
#panel label { display: block; margin: 14px 0 4px; font-size: 13px; opacity: 0.85; }
#panel input[type="range"], #panel select, #panel input[type="text"] { width: 100%; }
#panel input[type="text"] { font-family: ui-monospace, monospace; font-size: 11px; padding: 6px; }

.modes { display: flex; gap: 8px; margin-bottom: 10px; }
.modes button {
  flex: 1; height: 40px; cursor: pointer; font-size: 15px;
  border: 1px solid rgba(128,128,128,0.4); border-radius: 10px;
  background: transparent; color: inherit;
}
.modes button.on { background: #3a7bd5; color: #fff; border-color: #3a7bd5; }
.hint { margin: 0 0 12px; font-size: 12px; opacity: 0.7; min-height: 1em; }

.controls { display: flex; gap: 8px; margin-bottom: 8px; }
.controls button {
  flex: 1; height: 44px; font-size: 20px; cursor: pointer;
  border: 1px solid rgba(128,128,128,0.4); border-radius: 10px;
  background: transparent; color: inherit;
}
.controls button:active { background: rgba(128,128,128,0.15); }
.controls button.hidden { display: none; }
/* 「新しい絵」だけ文字ラベル。4文字が収まるよう小さめ＆折り返さない */
#new { font-size: 14px; white-space: nowrap; }

#copy {
  margin-top: 8px; width: 100%; height: 40px; cursor: pointer;
  border: 1px solid rgba(128,128,128,0.4); border-radius: 10px;
  background: transparent; color: inherit; font-size: 14px;
}
#copy:active { background: rgba(128,128,128,0.15); }   /* 押したフィードバック（他のボタンと揃える） */

/* パレット選択（色見本のラジオ）。#panel label（display:block）に詳細度で勝つよう #panel 配下で指定 */
#panel .palettes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 0 4px; }
#panel .pal { display: flex; flex-direction: column; align-items: center; gap: 4px; margin: 0; padding: 5px 4px;
  cursor: pointer; border: 1px solid rgba(128,128,128,0.3); border-radius: 8px; opacity: 1; }
#panel .pal input { position: absolute; width: 0; height: 0; opacity: 0; pointer-events: none; }
#panel .pal:has(input:checked) { border-color: #3a7bd5; box-shadow: 0 0 0 1px #3a7bd5; }
#panel .pal-sw { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; width: 100%; aspect-ratio: 2 / 1; }
#panel .pal-sw i { display: block; border-radius: 1px; }
#panel .pal-sw.pal-none, #panel .line-sw.pal-none { border: 1px dashed rgba(128,128,128,0.6); border-radius: 3px; }   /* 中身なし・点線枠だけ */
#panel .pal-name { font-size: 11px; opacity: 0.8; line-height: 1.1; text-align: center; }
/* 線の太さ見本: 中央に太さぶんの横線（高さは JS がインラインで指定） */
#panel .line-sw { display: flex; align-items: center; justify-content: center; width: 100%; aspect-ratio: 2 / 1; }
#panel .line-sw i { display: block; width: 72%; background: currentColor; border-radius: 2px; }

/* アカウント（M4）。投稿・いいね用。シェアだけならログイン不要 */
#account {
  margin-top: 20px; padding-top: 12px;
  border-top: 1px dashed rgba(128,128,128,0.4);
}
#account h2 { margin: 0 0 6px; font-size: 13px; opacity: 0.6; font-weight: 600; }
#account .btn, #account button {
  display: block; width: 100%; height: 40px; margin-top: 8px;
  border: 1px solid rgba(128,128,128,0.4); border-radius: 10px;
  background: transparent; color: inherit; font-size: 14px; cursor: pointer;
  line-height: 38px; text-align: center; text-decoration: none; box-sizing: border-box;
}
#account .btn:active, #account button:active { background: rgba(128,128,128,0.15); }
#account button[disabled] { opacity: 0.5; cursor: default; }
/* hidden 属性を尊重する（上の display:block が [hidden] を上書きしてしまうため）*/
#account [hidden] { display: none !important; }

#dev {
  margin-top: 20px; padding-top: 12px;
  border-top: 1px dashed rgba(128,128,128,0.4);
}
#dev.hidden { display: none; }
/* #panel label（display:block）に詳細度で勝つよう #panel 配下で指定する */
/* 関連する操作をまとめる角丸ボックス（今は音まわり）。中の先頭要素の余白は殺して詰める */
#panel .box {
  margin-top: 16px; padding: 10px 12px 12px;
  border: 1px solid rgba(128,128,128,0.3); border-radius: 10px;
  background: rgba(128,128,128,0.06);
}
#panel .box > :first-child { margin-top: 0; }
#panel .box #soundOpts > label:first-child { margin-top: 10px; }   /* チェックと最初の選択の間 */
#panel .loop { display: flex; align-items: center; gap: 6px; margin-top: 10px; font-size: 13px; }
#panel .loop.hidden { display: none; }
#panel .loop input { width: auto; margin: 0; }
#dev h2 { margin: 0 0 6px; font-size: 13px; opacity: 0.6; font-weight: 600; }
#dev label b { font-variant-numeric: tabular-nums; opacity: 0.9; }
.dev-more { margin-top: 4px; }
.dev-more > summary { cursor: pointer; font-size: 12px; opacity: 0.6; user-select: none; padding: 4px 0; }
.dev-more[open] > summary { opacity: 0.8; }
.status { margin-top: 18px; font-family: ui-monospace, monospace; font-size: 12px; opacity: 0.7; }
/* 作者表記（パネル最下部）。控えめに置くが、リンクは押せると分かる程度に見せる */
/* 上に破線を引いて「ここから先はクレジット」と分かるようにする（#dev / #account の区切りと同じ引き方） */
.credit {
  display: flex; align-items: center; gap: 7px;
  margin-top: 20px; padding-top: 12px; margin-bottom: 4px;
  border-top: 1px dashed rgba(128,128,128,0.4);
  font-size: 11px;
}
/* 文字だけ薄くする（アイコンは薄めない）。opacity を .credit ごと掛けると顔が沈むうえ、
   子で打ち消せない。文字は素の「( )」まで含めて span でくるんで一括で薄くする */
.credit > span { opacity: 0.55; }
.credit img { flex: none; width: 20px; height: 20px; border-radius: 50%; object-fit: cover; }
.credit a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

/* 説明を開く ? ボタン（テーマ切替の右隣）。起動時には何も出さず、押したときだけ開く */
#helpBtn {
  flex: none; width: 28px; height: 28px; padding: 0; border-radius: 50%; cursor: pointer;
  border: 1px solid rgba(128,128,128,0.35); background: transparent; color: inherit;
  font-size: 14px; line-height: 1; font-family: inherit;
}
#helpBtn:active { background: rgba(128,128,128,0.2); }
/* 言語切替（? の隣）。押すともう一方の言語に切り替わる。ラベルは「切り替わる先」を出す */
#langBtn {
  flex: none; height: 28px; padding: 0 9px; border-radius: 999px; cursor: pointer;
  border: 1px solid rgba(128,128,128,0.35); background: transparent; color: inherit;
  font-size: 11px; line-height: 1; font-family: inherit; letter-spacing: 0.04em;
}
#langBtn:active { background: rgba(128,128,128,0.2); }

/* 説明モーダル。<dialog> なので Esc 閉じ・フォーカス管理はブラウザ任せ */
#help {
  width: min(680px, 92vw); max-height: 86vh; padding: 26px 28px 32px;
  border: 1px solid rgba(128,128,128,0.35); border-radius: 14px;
  background: var(--dlg-bg, #fff); color: var(--dlg-fg, #222);
  font-size: 14px; line-height: 1.75; overflow-y: auto;
}
#help::backdrop { background: rgba(0,0,0,0.45); }
#help h2 { font-size: 15px; margin: 22px 0 6px; letter-spacing: 0.02em; }
#help h2:first-of-type { margin-top: 4px; }
#help p, #help ul, #help dl { margin: 0 0 4px; }
#help ul { padding-left: 1.2em; }
#help li { margin: 2px 0; }
#help dl { display: grid; grid-template-columns: max-content 1fr; gap: 2px 14px; margin: 4px 0; }
#help dt { font-weight: 600; white-space: nowrap; }
#help dd { margin: 0; }
#help .note { font-size: 13px; opacity: 0.75; margin-top: 8px; }
#help .help-close {
  position: sticky; top: 0; float: right; margin: -8px -8px 0 0;
  width: 32px; height: 32px; border-radius: 50%; cursor: pointer;
  border: 1px solid rgba(128,128,128,0.35); background: transparent; color: inherit;
  font-size: 18px; line-height: 1;
}
/* スマホは画面いっぱいに出す（狭い画面で枠と余白に幅を取られると読みにくいため）。
   ノッチ・ホームバーを避けるため safe-area ぶんの余白を足す */
@media (max-width: 600px) {
  #help {
    /* dialog の UA 既定（max-width/height: calc(100% - 6px - 2em)・margin:auto）を上書きして
       画面全体に貼る。inset:0 まで指定しないと中央寄せの小箱のままになる */
    position: fixed; inset: 0;
    width: 100%; max-width: none; height: 100%; max-height: none;
    margin: 0; border: 0; border-radius: 0;
    padding: calc(20px + env(safe-area-inset-top)) calc(18px + env(safe-area-inset-right))
             calc(28px + env(safe-area-inset-bottom)) calc(18px + env(safe-area-inset-left));
  }
  #help .help-close { margin: -4px -4px 0 0; }
}
