* { box-sizing: border-box; margin: 0; padding: 0; }

/* Twemoji が挿入する <img class="emoji"> をテキストと同サイズで表示 */
img.emoji {
  height: 1em; width: 1em;
  margin: 0 .05em 0 .1em;
  vertical-align: -0.1em;
  display: inline-block;
}
/* パレット/履歴のアイテム内ではマージン無しで中央寄せ */
.palette-item img.emoji,
.dock-mode-btn img.emoji {
  margin: 0;
  vertical-align: middle;
}

html, body {
  height: 100%;
  overflow: hidden;          /* 全画面 UI */
  overscroll-behavior: contain;
}

body {
  font-family: 'Segoe UI', sans-serif;
  background: #f4f4f6;
  color: #333;
}

/* ───────── 全画面アプリレイアウト ───────── */
.app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;            /* iOS Safari アドレスバー考慮 */
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: #ffffff;
  cursor: crosshair;
  display: block;
  /* タッチでブラウザのスクロール/ピンチが暴発するのを抑止 (JS で全部捌く) */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
/* UI 要素全般: テキスト選択を無効化 (canvas 上のドラッグで誤選択しないため) */
.top-bar, .bottom-dock, .play-controls, .touch-controls, .prop-panel,
.ctx-menu, .result-overlay, .palette-modal { user-select: none; -webkit-user-select: none; }

/* ───────── 上部ツールバー ───────── */
.top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 46px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid rgba(0,0,0,0.06);
  box-shadow: 0 2px 10px rgba(0,0,0,0.04);
  z-index: 100;
  transform: translateZ(0);
}

.top-bar .link {
  color: #4a90e2;
  text-decoration: none;
  font-size: 0.88rem;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
}
.top-bar .link:hover { background: rgba(74,144,226,0.08); }
.top-bar .muted { color: #888; font-size: 0.85rem; }
.top-bar .push-right { margin-left: auto; }
.top-bar .user-info { color: #555; font-size: 0.85rem; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-bar .logout-form { margin: 0; }
.top-bar .logout-btn {
  background: transparent; border: 1px solid #d0d0d8;
  color: #555; padding: 5px 12px; border-radius: 6px;
  cursor: pointer; font-size: 0.8rem;
}
.top-bar .logout-btn:hover { background: #f0f0f4; color: #222; }

#work-title {
  flex: 1;
  min-width: 140px;
  font-size: 0.95rem;
  font-weight: bold;
  padding: 6px 10px;
  border: 1px solid #d0d0d8;
  border-radius: 6px;
  background: rgba(255,255,255,0.85);
  color: #333;
  outline: none;
}
#work-title:focus { border-color: #4a90e2; background: #fff; }

.top-bar button {
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: bold;
  padding: 6px 12px;
  border-radius: 6px;
  border: 2px solid #4a90e2;
  background: #4a90e2;
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
.top-bar button:hover { background: #5fa3e8; }
.top-bar button.sub { background: rgba(255,255,255,0.7); color: #555; border-color: #d0d0d8; }
.top-bar button.sub:hover { background: #f0f0f4; color: #222; }
.top-bar button.danger { color: #c8232c; border-color: #f0c0c0; background: rgba(255,255,255,0.7); }
.top-bar button.danger:hover { background: #fdecec; color: #a01a22; }

#save-status {
  font-size: 0.85rem;
  color: #1dd1a1;
  min-width: 90px;
}
#save-status.error { color: #c8232c; }

/* JS が overflow を検知した時だけ btn-play を ▶ アイコンだけに圧縮 */
.top-bar.compact .btn-play .btn-label { display: none; }
.top-bar.compact .btn-play { padding: 5px 10px; font-size: 1rem; line-height: 1; }

/* ───────── プレイ中: プレイヤー絵文字ピッカー ───────── */
.player-emoji-picker {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.pep-box {
  background: #fff;
  border-radius: 12px;
  padding: 28px 28px 22px;
  width: min(90vw, 480px);
  position: relative;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.pep-title { font-size: 1.05rem; margin: 0 0 16px; padding-right: 40px; color: #333; text-align: center; }
.pep-close {
  position: absolute; top: 10px; right: 10px;
  width: 32px; height: 32px;
  background: rgba(0,0,0,0.06); border: none; border-radius: 50%;
  font-size: 1.1rem; cursor: pointer; color: #555;
}
.pep-close:hover { background: rgba(0,0,0,0.12); }
.pep-form { margin: 0 0 10px; }
.pep-input {
  width: 100%;
  font-size: 1.4rem;
  padding: 14px 16px;
  border: 2px solid #d0d0d8;
  border-radius: 8px;
  outline: none;
  box-sizing: border-box;
  text-align: center;
}
.pep-input:focus { border-color: #4a90e2; }
.pep-hint { font-size: 0.78rem; color: #999; text-align: center; }

/* ───────── Twemoji 画像の attribution (CC-BY 4.0 必須) ───────── */
.credits {
  font-size: 0.72rem;
  color: #888;
  text-align: center;
  padding: 14px 12px;
  line-height: 1.5;
}
.credits a { color: #4a90e2; text-decoration: none; }
.credits a:hover { text-decoration: underline; }
/* キャンバス画面: 左から1行表示で、画面幅に応じて自然改行。
   通常時はフッタードックの「下」(画面最下部) に置く。
   実際の credits 高さは JS で --credits-h に書き出し、ドックや
   プレイ用 D-pad の位置をそれに合わせて押し上げる。 */
:root { --credits-h: 18px; }

body.mode-server .credits,
body.mode-local  .credits,
body.mode-tool   .credits,
body.mode-play   .credits {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: calc(4px + env(safe-area-inset-bottom, 0px));
  padding: 4px 10px;
  font-size: 0.62rem;
  line-height: 1.6;
  background: rgba(255,255,255,0.85);
  border-radius: 6px;
  pointer-events: auto;
  z-index: 60;
  color: #555;
  text-align: left;
  word-break: keep-all;
  overflow-wrap: anywhere;
  max-height: 60vh;
  overflow-y: auto;
}
.credits .credit-item { white-space: normal; }
.credits .credit-sep  { color: #aaa; margin: 0 4px; }

/* gallery / my_works (リストページ): 作品が少ない時でも credits を
   ブラウザ画面下端に貼り付ける。body 全体を最低 100vh にしておき、
   credits を絶対位置で下端に貼る (flex を使わないので .list-wrap の
   レイアウトはそのまま) */
html:has(body.page-list),
body.page-list {
  height: auto;
  overflow: auto;
}
body.page-list {
  min-height: 100vh;
  position: relative;
  padding-bottom: 56px;
}
body.page-list .credits {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  background: transparent;
  padding: 16px 12px;
  font-size: 0.72rem;
}
/* テストプレイ中もライセンス表記は維持 (Twemoji 使用中なので法的に必要) */

/* ───────── 下部ドック (3 つの島が浮かぶ配置) ───────── */
.bottom-dock {
  position: fixed;
  left: 0; right: 0;
  /* ライセンス表記 (.credits) の上に乗る。
     credits の実高 (--credits-h) は JS で書き換えられ、
     折り返しが増えても自動でドックが押し上がる。 */
  bottom: calc(var(--credits-h, 18px) + 14px + env(safe-area-inset-bottom, 0px));
  background: transparent;
  z-index: 100;
  transform: translateZ(0);
  contain: layout style;
  pointer-events: none;
}
.bottom-dock > * { pointer-events: auto; }

.dock-row {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 12px 16px;
  box-sizing: border-box;
}
/* 開閉メニューは dock-row と同じ左右余白で、フッター全幅に乗る角丸モーダル */
.bottom-dock > .dock-expanded { margin: 0 16px; }
.bottom-dock.open > .dock-expanded { margin-bottom: 8px; }
.dock-island {
  /* 白寄りの淡いグレーのフッター色 */
  background: rgba(234,236,240,0.94);
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.10);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  height: 72px;
  /* overflow: visible で .picking の scale が島の外まで膨らめる。
     内部のスクロールが必要な要素 (#history / .terrain-palette) は
     個別に overflow-x: auto を持っている。 */
  overflow: visible;
}
/* モーダル島 (dock-expanded) は開閉で高さが変わるので height 固定は外す */
.dock-expanded.dock-island { height: auto; overflow: hidden; }
.dock-island-right  { flex: 0 0 auto; gap: 6px; }
.dock-island-center { width: 100%; gap: 6px; }

/* 中央島スタック: 上に付箋タブ + 下に中央島 (左の島は廃止、その分の幅も取る) */
.dock-island-stack {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
  /* dock-row は align-items: flex-end なので、stack 自体も底揃え */
  align-self: stretch;
  justify-content: flex-end;
}

/* 付箋タブ群: 中央島の上端に左寄せで貼り付き */
.dock-mode-switch {
  display: flex;
  gap: 2px;
  align-self: flex-start;
  margin-left: 8px;
  margin-bottom: -1px;     /* 中央島の上辺と 1px 重ねて境界線消す */
  position: relative;
  z-index: 2;
}
.dock-mode-btn {
  background: rgba(196,200,210,0.94);   /* 非選択: 中央島より少し濃いグレー */
  color: #4a4a55;
  border: 1px solid rgba(0,0,0,0.12);
  border-bottom: none;
  padding: 6px 14px 7px;
  font-size: 1rem;
  cursor: pointer;
  border-radius: 10px 10px 0 0;
  line-height: 1;
}
.dock-mode-btn:hover { background: rgba(216,219,226,0.95); }
.dock-mode-btn.active {
  /* 中央島と同じ色 → 境界線が消えて一体化 */
  background: rgba(234,236,240,0.94);
  color: #2a5a8a;
}

/* 中央の島 + その上に乗るモーダル島 */
.dock-center-stack {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
  gap: 8px;
  justify-content: flex-end;
}
/* 絵文字パレット 開閉トグル: 青ボタン + 白アイコン (フッター右端) */
.dock-toggle-icon {
  margin-left: auto;
  background: #4a90e2;
  border: none;
  cursor: pointer;
  color: #fff;
  width: 42px; height: 42px;
  padding: 0;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  transition: background 0.15s, transform 0.15s;
}
.dock-toggle-icon:hover { background: #5fa3e8; transform: scale(1.05); }
/* Unicode 文字 (🎨 / ✕) を 1.5rem で描画。ペン/消しゴム SVG (視覚 16-17px) と
   同等以上の視覚サイズになり、ブラウザ依存も少ない */
.dock-toggle-icon { font-size: 1.5rem; line-height: 1; }
.dock-toggle-icon svg { width: 22px; height: 22px; fill: currentColor; display: block; }

.dock-form {
  display: flex;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

#emoji-input {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  padding: 8px 12px;
  border: 1px solid #d0d0d8;
  border-radius: 6px;
  background: rgba(255,255,255,0.9);
  outline: none;
}
#emoji-input:focus { border-color: #4a90e2; background: #fff; }

.dock-form button,
#dock-toggle {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: bold;
  padding: 8px 14px;
  border-radius: 6px;
  border: 2px solid #4a90e2;
  background: #4a90e2;
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
.dock-form button:hover, #dock-toggle:hover { background: #5fa3e8; }
/* メニュー下段の検索フォーム (透明メニューに溶け込むデザイン) */
.dock-form-small { padding: 0; gap: 4px; }
.dock-form-small #emoji-input {
  font-size: 0.9rem;
  padding: 6px 12px;
  background: rgba(220, 235, 255, 0.65);
  border: 1px solid rgba(74, 144, 226, 0.35);
  border-radius: 999px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  color: #1f3a5f;
}
.dock-form-small #emoji-input::placeholder { color: rgba(74, 110, 160, 0.6); }
.dock-form-small #emoji-input:focus {
  background: rgba(220, 235, 255, 0.9);
  border-color: rgba(74, 144, 226, 0.85);
}
/* 展開メニュー下段: 検索フォーム + 右下ページャーの横並び */
.palette-bottom-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 4px;
}
.palette-bottom-row #spawn-form { flex: 1; min-width: 0; padding: 0; }
.palette-bottom-row #palette-categories { padding: 0; }
.dock-form button.sub,
#dock-toggle.sub {
  background: rgba(255,255,255,0.7);
  color: #555;
  border-color: #d0d0d8;
}
.dock-form button.sub:hover,
#dock-toggle.sub:hover { background: #f0f0f4; color: #222; }

/* ドック展開: 中央島の「上」に乗る角丸モーダル島 */
.dock-expanded {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.28s cubic-bezier(0.4, 0, 0.2, 1),
              padding    0.28s cubic-bezier(0.4, 0, 0.2, 1),
              opacity    0.18s ease,
              box-shadow 0.18s ease;
  will-change: max-height;
  /* メニューは半透明グレー + ぼかし (キャンバスがうっすら透けて見える) */
  background: rgba(214,217,224,0.45) !important;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0);
  padding: 0 12px;
  opacity: 0;
  display: block !important;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.bottom-dock.open .dock-expanded {
  max-height: 50vh;
  padding: 12px 14px;
  overflow-y: auto;
  opacity: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,0.10);
}
.dock-expanded::-webkit-scrollbar { display: none; }

/* ───────── パレット (履歴 + 検索結果) ───────── */
#history {
  display: flex;
  gap: 4px;
  /* 横スクロールではなく overflow:hidden で隠し、矢印 (#history-prev/next) で
     ページ送りする。これで D&D の touchmove が scroll に取られない */
  overflow: hidden;
  flex: 1 1 auto;
  min-width: 0;
  padding: 0;
  height: 56px;
  align-items: center;
}
/* 履歴ページ送りボタン */
.history-arrow {
  flex: 0 0 auto;
  width: 28px; height: 40px;
  background: rgba(255,255,255,0.65);
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 8px;
  cursor: pointer;
  color: #555;
  font-size: 1.2rem;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.history-arrow:hover { background: #fff; color: #222; }
.history-arrow:disabled { opacity: 0.3; cursor: default; }
/* 横スクロールバーは細めの控えめなデザインで (履歴/カラーパレット共通) */
#history,
.terrain-palette {
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,0.28) transparent;
}
#history::-webkit-scrollbar,
.terrain-palette::-webkit-scrollbar { height: 6px; }
#history::-webkit-scrollbar-track,
.terrain-palette::-webkit-scrollbar-track { background: transparent; }
#history::-webkit-scrollbar-thumb,
.terrain-palette::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.28);
  border-radius: 3px;
}
#history::-webkit-scrollbar-thumb:hover,
.terrain-palette::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.45); }
#history.empty .empty-msg {
  font-size: 0.75rem;
  color: #aaa;
  white-space: nowrap;
  padding: 6px 4px;
}

#palette {
  /* 1 ページ = 固定行数 × 自動列数 (列数はウィンドウ幅で変動) */
  display: grid;
  grid-template-columns: repeat(auto-fill, 48px);
  grid-auto-rows: 48px;
  gap: 4px;
  justify-content: center;
}
.palette-item {
  width: 48px; height: 48px;
  min-width: 0;
  box-sizing: border-box;
  padding: 0;
}

/* カテゴリページャー: < N/M > (右下に配置) */
.palette-categories {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 4px 0 0;
}
.cat-pager-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  padding: 2px 10px;
  border-radius: 6px;
  color: #555;
}
.cat-pager-btn:hover { background: rgba(0,0,0,0.10); color: #222; }
.cat-pager-label {
  font-size: 0.85rem;
  color: #555;
  min-width: 3.5em;
  text-align: center;
  user-select: none;
}

.palette-item {
  background: transparent;
  color: inherit;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0;
  font-size: 1.4rem;
  cursor: grab;
  font-weight: normal;
  /* flex コンテナ (#history) でコンテナが狭くなっても縮まずスクロール対象に */
  flex-shrink: 0;
  width: 48px; height: 48px;
  /* 絵文字をボックスの中心に置いて hover 背景とのズレを防ぐ */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-sizing: border-box;
  /* スクロール時の paint を最小化: 各 item を独立したレイヤーに */
  contain: layout style paint;
}
.palette-item:active { cursor: grabbing; }
/* 親 (#history) の横スクロールに touch が取られないよう、ボタン自体は
   touch-action: none。これがないと履歴からの D&D で touchmove が
   ブラウザ scroll に消費されハンドラに届かない */
.palette-item { touch-action: none; }
/* タッチで押下中 (D&D 待機) のアニメーション: 浮き上がるエフェクト */
.palette-item.picking {
  transform: scale(1.15);
  background: rgba(74,144,226,0.15);
  border-color: rgba(74,144,226,0.5);
  box-shadow: 0 4px 14px rgba(0,0,0,0.18);
  transition: transform 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
  z-index: 10;
}
.palette-item:hover {
  background: rgba(74,144,226,0.10);
  border-color: rgba(160,192,224,0.6);
}

.palette-empty {
  font-size: 0.85rem;
  color: #aaa;
  padding: 12px 4px;
}
.palette-empty.hidden { display: none; }

/* ───────── 右クリックメニュー ───────── */
.ctx-menu {
  position: fixed;
  background: #fff;
  border: 1px solid #c8c8d0;
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  padding: 4px 0;
  z-index: 200;
  min-width: 140px;
}
.ctx-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 8px 16px;
  cursor: pointer;
  color: #333;
  font-size: 0.9rem;
  font-weight: normal;
}
.ctx-menu button:hover { background: #eef4fb; color: #2a5a8a; }
.ctx-menu button[data-action="delete"]:hover { background: #fdecec; color: #c8232c; }
.ctx-menu.hidden { display: none; }
/* モバイル: 画面下部の角丸アクションシート */
.ctx-menu.sheet {
  left: 8px !important;
  right: 8px !important;
  top: auto !important;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  min-width: 0;
  border-radius: 14px;
  padding: 6px 0;
  background: rgba(245,246,250,0.96);
  box-shadow: 0 -8px 30px rgba(0,0,0,0.18);
  animation: ctx-sheet-in 0.2s cubic-bezier(0.4,0,0.2,1);
}
.ctx-menu.sheet button { padding: 14px 18px; font-size: 1rem; }
@keyframes ctx-sheet-in {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);     opacity: 1; }
}
.ctx-divider {
  height: 1px;
  background: #e0e0e8;
  margin: 4px 0;
}

/* ───────── 小さい画面用 (モバイル対応) ───────── */
@media (max-width: 640px) {
  /* フッター 3 島: 余白を詰めて狭幅でも 3 つ並ぶように */
  .dock-row { gap: 6px; padding: 8px 8px; }
  .dock-island { padding: 6px 8px; height: 64px; }
  .dock-island-center { gap: 4px; }
  .dock-mode-btn { padding: 5px 8px; font-size: 0.95rem; }
  .dock-island-right { gap: 4px; }
  .btn-terrain, .dock-toggle-icon { width: 38px; height: 38px; }
  .dock-toggle-icon { font-size: 1.3rem; }
  .terrain-alpha-slider { width: 70px; }
  /* メニューはより低めに (余裕を残す) */
  .bottom-dock.open > .dock-expanded { max-height: 60vh; padding: 10px 10px; }
  /* 全幅マージンを 8px に詰める */
  .bottom-dock > .dock-expanded { margin: 0 8px; }

  /* top-bar: 折り返しさせず横 1 行を維持。入りきらない時は中央タイトルを縮め、
     btn-play は ▶ アイコンだけにする (テキスト "テストプレイ" を隠す) */
  .top-bar {
    flex-wrap: nowrap;
    height: 46px;
    padding: 6px 8px;
    gap: 6px;
  }
  .top-bar .author-tag { font-size: 0.75rem; }
  .top-bar .work-title-static {
    min-width: 0;
    max-width: 30vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex-shrink: 1;
  }
  .top-bar button, .top-bar .btn-edit, .top-bar .logout-btn {
    font-size: 0.8rem; padding: 5px 10px;
    flex-shrink: 0;
  }
  /* save-status は狭幅で隠す (実装は維持) */
  #save-status { display: none; }
  /* publish-modal を 90vw に収める */
  #publish-modal .result-box { max-width: 90vw; box-sizing: border-box; padding: 22px 22px; }
  .publish-field input { min-width: 0; width: 100%; }

  /* iOS Safari は input の font-size が 16px 未満だと自動ズームしてしまう */
  #work-title, #emoji-input,
  input[type=text], input[type=email], input[type=password], input[type=search] {
    font-size: 16px;
  }

  /* ───── ギャラリー / マイページ ───── */
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
  }
  .card-title { font-size: 0.85rem; padding: 6px 10px 2px; }
  .card-author { font-size: 0.72rem; padding: 0 10px 2px; }
  .card-meta   { font-size: 0.68rem; padding: 0 10px 8px; }
  .edit-icon { width: 28px; height: 28px; top: 6px; right: 6px; font-size: 0.9rem; }

  /* user-bar: 折り返し許可 + コンパクト化 */
  .user-bar { gap: 8px; padding: 4px 0 10px; }
  .user-bar-nav { flex-wrap: wrap; gap: 2px; }
  .user-bar .nav-tab { padding: 5px 10px; font-size: 0.82rem; }
  .user-bar-right { gap: 6px; }
  .user-bar .user-name { display: none; }
}

/* タッチデバイス: blur の描画負荷を下げる */
@media (pointer: coarse), (hover: none) {
  .dock-expanded {
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }
  .dock-form-small #emoji-input {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

/* ───────── ギャラリー / マイページ共通 ───────── */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}
.work-card-wrap { position: relative; }
.work-card {
  background: #fff; border: 1px solid #e0e0e8; border-radius: 8px;
  overflow: hidden; text-decoration: none; color: inherit;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
  display: block;
}
.work-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 22px rgba(0,0,0,0.08);
  border-color: #a0c0e0;
}
.work-card img, .work-card .thumb-empty {
  display: block; width: 100%; aspect-ratio: 8/5; object-fit: cover;
  background: #f6f6fa;
}
.thumb-empty {
  display: flex; align-items: center; justify-content: center;
  color: #bbb; font-size: 0.85rem;
}
.card-title {
  font-weight: bold; font-size: 0.95rem;
  padding: 8px 12px 2px; color: #333;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-author {
  font-size: 0.8rem; color: #666;
  padding: 0 12px 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-meta {
  font-size: 0.75rem; color: #999;
  padding: 0 12px 10px;
}
.edit-icon {
  position: absolute;
  top: 8px; right: 8px;
  background: rgba(255,255,255,0.92);
  color: #4a90e2;
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.0rem;
  text-decoration: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  transition: background 0.15s, transform 0.15s;
  z-index: 2;
}
.edit-icon:hover {
  background: #4a90e2;
  color: #fff;
  transform: scale(1.08);
}

/* ───────── user-bar (上部ナビゲーション) ───────── */
.user-bar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 6px 0 14px;
  border-bottom: 1px solid #eee; margin-bottom: 14px;
  flex-wrap: wrap;
}
.user-bar-nav { display: flex; gap: 4px; }
.user-bar .nav-tab {
  color: #666; text-decoration: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}
.user-bar .nav-tab:hover { background: #eef4fc; color: #4a90e2; }
.user-bar .nav-tab.active {
  background: #4a90e2; color: #fff; font-weight: bold;
}
.user-bar-right {
  display: flex; align-items: center; gap: 10px;
  color: #888; font-size: 0.85rem;
}
.user-bar form { margin: 0; }
.user-bar .user-name { color: #555; }
.user-bar .logout-btn {
  background: transparent; border: 1px solid #d0d0d8;
  color: #555; padding: 5px 12px; border-radius: 6px;
  cursor: pointer; font-size: 0.8rem;
}
.user-bar .logout-btn:hover { background: #f0f0f4; color: #222; }
.user-bar .auth-link {
  color: #4a90e2; text-decoration: none; padding: 5px 10px;
  border-radius: 6px;
}
.user-bar .auth-link:hover { background: #eef4fc; }

/* ───────── プレイ / 編集モード切替 ───────── */
.btn-play {
  background: #4caf50 !important; border-color: #4caf50 !important;
  color: #fff !important; font-weight: bold;
}
.btn-play:hover { background: #5fc864 !important; border-color: #5fc864 !important; }

.play-controls {
  position: fixed;
  top: 10px; right: 10px;
  display: flex; gap: 8px;
  z-index: 110;
}
.play-controls button {
  background: rgba(0,0,0,0.7); color: #fff;
  border: none; padding: 8px 14px; border-radius: 6px;
  font-size: 0.85rem; font-weight: bold; cursor: pointer;
}
.play-controls button:hover { background: rgba(0,0,0,0.85); }
.play-controls.hidden { display: none; }

/* プレイ中の左上「👤 プレイヤー」ボタン (絵文字変更モーダルを開く) */
.play-player-btn {
  position: fixed;
  top: 10px; left: 10px;
  background: rgba(0,0,0,0.7); color: #fff;
  border: none; padding: 8px 14px; border-radius: 6px;
  font-size: 0.85rem; font-weight: bold; cursor: pointer;
  z-index: 110;
  display: none;
}
body.mode-play .play-player-btn { display: inline-flex; align-items: center; gap: 4px; }
.play-player-btn:hover { background: rgba(0,0,0,0.85); }

/* 編集モード内のテストプレイ中は top-bar と dock を非表示にして集中環境に。
   本番プレイ専用画面 (body.page-play) ではヘッダを残してギャラリーに戻れるようにする。 */
body.mode-play:not(.page-play) .top-bar { display: none; }
body.mode-play:not(.page-play) .bottom-dock { display: none; }
body.mode-play .play-controls { display: flex; }
body.mode-play .touch-controls { display: flex; }

/* ───────── 編集コントロール (モバイル + 編集中のみ表示) ───────── */
.edit-controls {
  position: fixed;
  right: 12px;
  bottom: calc(170px + env(safe-area-inset-bottom, 0px));
  display: none;
  flex-direction: column;
  gap: 8px;
  z-index: 90;
  pointer-events: none;
}
.edit-controls > * { pointer-events: auto; }
.edit-controls button {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: rgba(255,255,255,0.92);
  color: #2a5a8a;
  border: 1px solid #d0d0d8;
  font-size: 1.2rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: 0 3px 10px rgba(0,0,0,0.18);
  padding: 0;
}
.edit-controls button:active { transform: scale(0.94); background: #f0f4fa; }
.edit-controls button:disabled { opacity: 0.4; cursor: default; }
@media (pointer: coarse), (hover: none) {
  .edit-controls { display: flex; }
}
body.mode-play .edit-controls,
body.pasting .edit-controls { display: none; }

/* ───────── タッチコントロール (プレイ中のみ) ───────── */
.touch-controls {
  position: fixed;
  /* credits の上に置く。改行で credits が高くなった分だけ矢印も自動で上に逃げる */
  bottom: calc(var(--credits-h, 18px) + 16px + env(safe-area-inset-bottom, 0px));
  left: 0; right: 0;
  display: none;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  z-index: 110;
  pointer-events: none;
}
.touch-controls.hidden { display: none; }
.touch-controls > * { pointer-events: auto; }

.touch-dpad { display: flex; gap: 12px; }
.touch-action-group { display: flex; gap: 12px; align-items: center; }
.touch-dpad button,
.touch-sneak,
.touch-jump {
  width: 72px; height: 72px;
  background: rgba(255,255,255,0.85);
  color: #2a5a8a;
  border: 2px solid rgba(255,255,255,0.9);
  border-radius: 50%;
  font-size: 1.8rem; font-weight: bold;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: 0 4px 14px rgba(0,0,0,0.18);
  transition: transform 0.05s ease, background 0.1s ease;
}
.touch-dpad button.pressed, .touch-dpad button:active,
.touch-sneak.pressed, .touch-sneak:active,
.touch-jump.pressed, .touch-jump:active {
  background: #4a90e2;
  color: #fff;
  transform: scale(0.94);
}
.touch-sneak {
  background: rgba(255,193,7,0.85);
  color: #5d4400;
}
.touch-sneak.pressed, .touch-sneak:active {
  background: #f57f17;
  color: #fff;
  transform: scale(0.94);
}
.touch-jump {
  background: rgba(76,175,80,0.85);
  color: #fff;
  width: 88px; height: 88px;
  font-size: 2rem;
  border-color: rgba(255,255,255,0.9);
}
.touch-jump.pressed, .touch-jump:active {
  background: #2e7d32;
  color: #fff;
  transform: scale(0.94);
}

/* ───────── プロパティパネル (選択 item / プレイヤーの当たり判定調整) ───────── */
.prop-panel {
  position: fixed;
  top: 60px; right: 12px;
  width: 240px;
  background: rgba(255,255,255,0.96);
  border: 1px solid #d0d0d8;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.12);
  padding: 10px 12px;
  z-index: 105;
  font-size: 0.85rem;
}
.prop-panel.hidden { display: none; }
.prop-panel h4 {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: #444;
  display: flex; align-items: center; justify-content: space-between;
}
.prop-panel .prop-count { color: #999; font-size: 0.75rem; font-weight: normal; }
.prop-panel label {
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 8px;
  font-size: 0.8rem;
}
.prop-panel .prop-axis { width: 20px; color: #555; }
.prop-panel input[type=range] { flex: 1; }
.prop-panel .prop-val {
  width: 38px; text-align: right; color: #333;
  font-variant-numeric: tabular-nums;
}
.prop-panel .prop-reset {
  width: 100%;
  background: transparent;
  border: 1px solid #d0d0d8;
  color: #555;
  padding: 5px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.75rem;
  margin-top: 4px;
}
.prop-panel .prop-reset:hover { background: #f0f0f4; color: #222; }

@media (max-width: 600px) {
  .prop-panel { top: auto; bottom: 100px; right: 8px; width: 200px; padding: 8px 10px; }
}

/* プレイ中・閲覧モードでは隠す */
body.mode-play .prop-panel { display: none; }
body.mode-view .prop-panel { display: none; }

/* ───────── プレイ結果オーバーレイ ───────── */
.result-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.result-overlay.hidden { display: none; }
.result-box {
  background: #fff;
  border-radius: 12px;
  padding: 32px 40px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
  min-width: 260px;
  animation: result-pop 0.25s ease-out;
}
@keyframes result-pop {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
.result-title {
  font-size: 2rem; font-weight: bold; margin-bottom: 20px;
}
.result-overlay.cleared .result-title { color: #d4a000; }
.result-overlay.over    .result-title { color: #c8232c; }
.result-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.result-btn {
  background: #f0f0f4; border: 1px solid #c0c0c8; color: #333;
  padding: 10px 20px; border-radius: 6px;
  font-weight: bold; cursor: pointer; font-size: 0.95rem;
}
.result-btn:hover { background: #e0e0e8; }
.result-btn.primary { background: #4caf50; border-color: #4caf50; color: #fff; }
.result-btn.primary:hover { background: #5fc864; }

/* アイテム編集モーダル (タップ時の中央モーダル) */
.item-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 220;
}
.item-modal.hidden { display: none; }
.item-modal-box {
  background: #fff;
  border-radius: 14px;
  width: min(420px, 92vw);
  max-height: 85vh;
  padding: 18px 20px;
  position: relative;
  box-shadow: 0 18px 50px rgba(0,0,0,0.35);
  overflow-y: auto;
}
.item-modal-close {
  position: absolute; top: 8px; right: 8px;
  width: 34px; height: 34px;
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 6px; cursor: pointer; font-size: 1rem;
}
.item-modal-close:hover { background: #f0f0f4; }
.item-modal-title { font-size: 1rem; margin: 0 30px 12px 0; color: #333; }
.item-modal-section { margin-bottom: 16px; }
.item-modal-section h4 { font-size: 0.85rem; color: #555; margin-bottom: 8px; }
.item-modal-section .muted { color: #999; font-weight: normal; font-size: 0.8rem; }
.item-modal-section label {
  display: grid; grid-template-columns: 28px 1fr 36px;
  align-items: center; gap: 8px; margin-bottom: 6px;
}
.im-axis { font-size: 0.8rem; color: #555; }
.im-val  { font-size: 0.78rem; color: #555; text-align: right; font-variant-numeric: tabular-nums; }
.im-secondary-btn {
  width: 100%; margin-top: 6px;
  padding: 6px 10px; background: #f0f0f4; color: #555;
  border: 1px solid #d0d0d8; border-radius: 6px; cursor: pointer;
  font-size: 0.85rem;
}
.im-secondary-btn:hover { background: #e0e0e8; }
.im-action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.im-action-grid button {
  padding: 10px 8px;
  background: #f5f7fa; color: #2a3a55;
  border: 1px solid #c8d2e0; border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
}
.im-action-grid button:hover { background: #e8eef6; }
.im-action-grid button.im-danger {
  background: #fff; color: #c8232c; border-color: #f0c0c5;
}
.im-action-grid button.im-danger:hover { background: #fdecec; }

/* 投稿モーダル */
.publish-form { display: flex; flex-direction: column; gap: 16px; }
.publish-field { display: flex; flex-direction: column; gap: 6px; text-align: left; font-size: 0.9rem; }
.publish-field input {
  padding: 8px 10px; border: 1px solid #c0c0c8; border-radius: 6px;
  font-size: 1rem; min-width: 240px;
}
.publish-field input:focus { outline: none; border-color: #4a90e2; }
.top-bar .btn-primary {
  background: #4a90e2; color: #fff; border: 1px solid #4a90e2;
  padding: 6px 14px; border-radius: 6px; font-weight: bold; cursor: pointer;
}
.top-bar .btn-primary:hover { background: #5fa3e8; }

/* dock-mode-switch / dock-mode-btn のスタイルは付箋タブ化に伴い
   上部の .dock-island-stack ブロックで定義 (この場所からは削除) */
/* モード時の出し分け (id セレクタの specificity に勝つよう id でも当てる) */
body.dock-mode-emoji   .dock-terrain-only,
body.dock-mode-emoji   #terrain-palette { display: none !important; }
body.dock-mode-terrain .dock-emoji-section { display: none !important; }

/* dock-compact の flex 子要素が圧縮されないように共通で flex-shrink:0 */
.dock-mode-switch, .btn-terrain, .dock-toggle-icon, .terrain-alpha-wrap { flex-shrink: 0; }

/* アイコン丸ボタン (青 + 白アイコン) — ペン/消しゴム / パレットトグルで共通 */
.icon-btn-primary {
  background: #4a90e2;
  border: none;
  width: 42px; height: 42px;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  transition: background 0.15s, transform 0.15s;
}
.icon-btn-primary:hover { background: #5fa3e8; transform: scale(1.05); }
.icon-btn-primary svg { width: 22px; height: 22px; fill: currentColor; display: block; }
/* 旧クラス名 (btn-terrain) も互換のため同じスタイルにマップ */
.btn-terrain { /* alias */
  background: #4a90e2; border: none; width: 42px; height: 42px;
  padding: 0; border-radius: 50%; cursor: pointer; color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  transition: background 0.15s, transform 0.15s;
}
.btn-terrain:hover { background: #5fa3e8; transform: scale(1.05); }
.btn-terrain svg { width: 22px; height: 22px; fill: currentColor; display: block; }
.dock-terrain-section {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  flex: 1 1 auto;     /* dock-compact の残り幅を全部取る */
  min-width: 0;
}
.dock-terrain-section .push-right { margin-left: auto; }
/* 地形ペン用カラーパレット (12色)
   絵文字パレットの中心距離 (48px item + 4px gap = 52px) を踏襲するため、
   swatch 24px + gap 28px = 24+28 = 52px の中心ピッチに合わせる。 */
/* terrain-palette はどこに置かれていても (dock-terrain-section の中でも
   中央島の中でも) 同じピッチ・同じ高さで描画。
   gap 28 + swatch 24 = 中心距離 52px (#palette の 48+4 と一致)。
   先頭 swatch (24px) の中心を絵文字 (48px) の中心と揃えるため左 12px の
   オフセット。*/
#terrain-palette.terrain-palette {
  display: flex !important;
  gap: 28px !important;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  padding: 0 0 0 12px;
  height: 56px;
  box-sizing: border-box;
}
.terrain-swatch {
  flex: 0 0 auto;
  /* border-box にして border 含めて厳密に 24px。これがないと content-box で
     border 1px が外に出て 26x26 となり、中心ピッチが絵文字 (52px) より広く
     ズレてしまう (累積で「フッターに入る個数」がズレる原因)。 */
  box-sizing: border-box;
  width: 24px; height: 24px; padding: 0;
  border: 1px solid rgba(0,0,0,0.2);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.25);
}
/* 不透明度スライダー: track は左 (有効範囲) = 薄い青 / 右 (未使用) = グレー、
   つまみは青。JS で --pct を更新して塗り分ける。 */
.terrain-alpha-wrap { display: inline-flex; align-items: center; }
.terrain-alpha-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 90px;
  cursor: pointer;
  background: transparent;
  --pct: 100%;
}
.terrain-alpha-slider::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(to right,
    #cce4ff 0,
    #cce4ff var(--pct),
    rgba(0,0,0,0.18) var(--pct),
    rgba(0,0,0,0.18) 100%);
}
.terrain-alpha-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px; height: 14px;
  background: #4a90e2;
  border: 2px solid #fff;
  border-radius: 50%;
  margin-top: -6px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
}
.terrain-alpha-slider::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(to right,
    #cce4ff 0,
    #cce4ff var(--pct),
    rgba(0,0,0,0.18) var(--pct),
    rgba(0,0,0,0.18) 100%);
}
.terrain-alpha-slider::-moz-range-thumb {
  width: 14px; height: 14px;
  background: #4a90e2;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
}

/* 地形/背景 切替ボタン (テキストピル型、状態で配色反転) */
.btn-terrain-text {
  width: auto;
  min-width: 56px;
  height: 36px;
  padding: 0 14px;
  border-radius: 18px;
  font-size: 0.85rem;
  font-weight: bold;
}
.btn-terrain-text.layer-fg {           /* 地形モード = 白ボタン + 青テキスト */
  background: #fff;
  color: #4a90e2;
  border: 2px solid #4a90e2;
  box-sizing: border-box;
}
.btn-terrain-text.layer-fg:hover { background: #f0f6ff; transform: scale(1.04); }
.btn-terrain-text.layer-bg {           /* 背景モード = 青ボタン + 白テキスト */
  background: #4a90e2;
  color: #fff;
  border: none;
}
.btn-terrain-text.layer-bg:hover { background: #5fa3e8; transform: scale(1.04); }
.terrain-swatch:hover { transform: scale(1.08); }
.terrain-swatch.active {
  outline: 2px solid #4a90e2;
  outline-offset: 2px;
}
body.terrain-tool #game { cursor: cell !important; }

/* ペースト待機中: 他の UI は触れないように。
   bottom-dock は子要素まで含めて完全にブロックし、見た目は子要素のみ
   半透明化することで dock-expanded の backdrop-filter blur を保持する。 */
body.pasting .top-bar,
body.pasting .bottom-dock,
body.pasting .bottom-dock *,
body.pasting .prop-panel,
body.pasting #publish-modal,
body.pasting #context-menu {
  pointer-events: none !important;
}
body.pasting .top-bar,
body.pasting .dock-compact,
body.pasting .dock-expanded > *,
body.pasting .prop-panel,
body.pasting #publish-modal,
body.pasting #context-menu {
  opacity: 0.5;
}
body.pasting #game { cursor: crosshair !important; }
/* Ctrl 押下中はカーソルをパンに統一 (ペースト/地形/通常を全部上書き) */
body.ctrl-pan #game { cursor: grab !important; }

/* ───────── トースト通知 ───────── */
.toast {
  position: fixed;
  top: 60px; left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: rgba(0,0,0,0.82); color: #fff;
  padding: 10px 18px; border-radius: 6px;
  font-size: 0.9rem; font-weight: bold;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 300;
  pointer-events: none;
  white-space: nowrap;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ───────── 閲覧モード (view) ───────── */
.top-bar .work-title-static {
  font-weight: bold;
  color: #333;
  font-size: 1rem;
  padding: 0 6px;
}
.top-bar .author-tag {
  font-size: 0.85rem;
  color: #888;
}
.top-bar .btn-edit {
  background: #4a90e2; color: #fff;
  text-decoration: none;
  padding: 7px 16px;
  border-radius: 6px;
  font-weight: bold;
  font-size: 0.9rem;
}
.top-bar .btn-edit:hover { background: #5fa3e8; }
/* view モードでは canvas が画面いっぱい (ドック非表示) */
body.mode-view canvas { height: 100vh; }
