:root {
  /* 色とフォントは naoblock.jp（assets/main.css）に合わせる */
  --bg: #f6f6f6;
  --fg: #151515;
  --muted: #5d5d5d;
  --line: #ddd;
  --accent: #da0534;
  --panel: #fff;
  --board-bg: #ecebe6;
  --font-display: "Londrina Solid", "Noto Sans JP", system-ui, sans-serif;
  --font-body: "Noto Sans JP", system-ui, -apple-system, "Hiragino Kaku Gothic ProN", "Yu Gothic", Meiryo, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }

button, .button {
  font: inherit;
  font-size: 0.95rem;
  padding: 8px 14px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}
button.primary, .button.primary {
  font-family: var(--font-display);
  letter-spacing: 0.04em;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: bold;
}
button.primary.stop { background: #555; border-color: #555; }
button:disabled { opacity: 0.4; cursor: default; }

/* ---- はじめの画面 ---- */

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: 24px 16px;
}
.start-header { display: block; width: 100%; height: auto; margin: 0 0 16px; border-radius: 14px; background: #13235a; }
h1 { margin: 0 0 4px; font-size: 1.7rem; font-weight: 900; }
h1 small { font-family: var(--font-display); font-size: 1.15rem; color: var(--muted); font-weight: 900; letter-spacing: 0.02em; }
/* スタート画面のアプリ名。naoblock.jp のロゴと同じ Londrina Solid */
.app-title { font-family: var(--font-display); font-size: 2.6rem; line-height: 1.1; letter-spacing: 0.02em; }
.lead { font-weight: 700; }
.lead { color: var(--muted); }
/* Londrina Solid は字幅が細く小文字も低いので、日本語と混ざる行では英字だけ少し大きくして揃える（naoblock.jp と同じ） */
.en { font-size: 1.15em; line-height: 1; letter-spacing: 0.02em; }
.start-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin: 20px 0; }
.start-form input { font: inherit; width: 7em; padding: 8px; border: 1px solid var(--line); border-radius: 8px; }
.category-tabs { display: flex; gap: 4px; margin: 20px 0 0; border-bottom: 2px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.category-tab {
  flex: 1 0 auto; min-height: 44px; padding: 8px 12px; margin-bottom: -2px;
  border: 1px solid transparent; border-bottom: 2px solid transparent; border-radius: 10px 10px 0 0;
  background: transparent; color: var(--muted); font-family: var(--font-display); font-size: 1.05rem; font-weight: 900; letter-spacing: 0.02em; white-space: nowrap;
}
.category-tab[aria-selected="true"] { color: var(--accent); border-color: var(--line); border-bottom-color: var(--bg); background: var(--bg); }
.category { margin: 16px 0 0; }
.category .start-form { margin: 0; }
.soon { display: inline-block; margin: 0; padding: 6px 12px; border: 1px dashed var(--line); border-radius: 10px; color: var(--muted); font-size: 0.9rem; }
.soon-note { margin: 6px 0 0; color: var(--muted); font-size: 0.85rem; }
.picture-list { display: flex; flex-wrap: wrap; gap: 12px; }
.picture-card {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: 112px; padding: 10px 8px; border: 3px solid var(--fg); border-radius: 0; box-shadow: 4px 4px 0 var(--fg);
  background: #fff; color: inherit; text-decoration: none; font-weight: 900;
}
.picture-card img { width: 88px; height: 88px; object-fit: contain; image-rendering: pixelated; }
.picture-card:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--fg); }

.legal { color: var(--muted); font-size: 0.75rem; margin-top: 32px; }
.legal p { margin: 2px 0; }
.version { opacity: 0.8; }
.version.stale { color: #c62828; font-weight: bold; opacity: 1; }

/* ---- 遊ぶ画面 ---- */

#view-play {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.hud-top, .controller {
  flex: none;
  background: var(--panel);
  padding: 6px 12px;
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
}
.hud-top { border-bottom: 1px solid var(--line); padding-top: max(6px, env(safe-area-inset-top)); }

.hud-main { display: flex; align-items: center; gap: 10px; }
.hud-info { flex: 1; min-width: 0; }
.hud-row { display: flex; gap: 8px; align-items: baseline; font-size: 0.85rem; white-space: nowrap; min-width: 0; }
.mode-badge { font-size: 0.7rem; padding: 0 6px; border-radius: 999px; background: var(--line); }
.mode-badge.ta { background: #c62828; color: #fff; }
.mode-badge:empty { display: none; }
.progress-row { display: flex; align-items: center; gap: 8px; }
.progress-row .progress { flex: 1; }
#progress-text { flex: none; font-size: 0.75rem; font-variant-numeric: tabular-nums; }
.brand { color: var(--accent); font-family: var(--font-display); font-size: 1.1em; font-weight: 900; letter-spacing: 0.02em; text-decoration: none; }
.step-row { display: flex; align-items: baseline; gap: 8px; margin: 2px 0 4px; min-width: 0; }
.step-label { font-weight: bold; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.timer { margin-left: auto; flex: none; font-weight: bold; font-variant-numeric: tabular-nums; color: var(--fg); }
.timer.low { color: #c62828; }
.lives { flex: none; color: var(--accent); letter-spacing: 0.05em; font-size: 0.95rem; }
/* タイムアタック：ステージののこり時間（細いバー） */
.limit { height: 4px; margin-top: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
#limit-bar { height: 100%; width: 100%; background: var(--fg); }
.limit.low #limit-bar { background: #c62828; }
.gameover-title { margin: 0 0 6px; font-family: var(--font-display); font-size: 1.6rem; font-weight: 900; color: var(--accent); text-align: center; }
.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }

/* 次のブロックの見本（右上） */
.next-box {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 96px;
  height: 58px;
  overflow: hidden;
  padding: 3px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
}
.next-title { color: var(--muted); font-size: 0.7rem; line-height: 1.2; }
#next-canvas { display: block; }

.board-area {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
}

.board-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
  overflow: auto;
  display: flex;
  background: var(--board-bg);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#board {
  margin: auto;
  flex: none;
  display: block;
  border-radius: 4px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.25);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  /* 速く続けてタップしても、ブラウザのダブルタップ拡大に取られないように */
  touch-action: none;
}

/* お手本の小窓（盤面の右上に固定。盤面と一緒にスクロールしない） */
.mini {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 5;
  padding: 3px;
  min-height: 0;
  border-radius: 6px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  line-height: 0;
}
.mini canvas, #sample-canvas { image-rendering: pixelated; display: block; }

.sample-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; height: 44px; }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; min-height: 40px; }
.segmented button[aria-selected="true"],
.segmented button[aria-pressed="true"] { background: var(--accent); color: #fff; font-weight: bold; }
.close { margin-left: auto; width: 44px; padding: 0; font-size: 1.4rem; }
#sample-canvas { margin: 0 auto; border-radius: 4px; }
#sample-note { margin: 8px 0 0; font-size: 0.8rem; text-align: center; min-height: 3.4em; width: 0; min-width: 100%; }

.help-list { padding-left: 1.2em; margin: 8px 0; }
.help-list li { margin: 6px 0; }

/* コントローラー（画面の下に固定） */
.controller {
  border-top: 1px solid var(--line);
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "pad tools actions" "legal legal legal";
  align-items: center;
  gap: 4px 10px;
  user-select: none;
  -webkit-user-select: none;
}
.controller button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.dpad {
  grid-area: pad;
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 40px);
  gap: 2px;
}
.dpad .pad { min-height: 0; padding: 0; font-size: 1.1rem; border-radius: 8px; }
.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 3; }
.tools { grid-area: tools; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.tools .small { width: 40px; min-height: 36px; padding: 0; font-size: 1rem; }
.actions { grid-area: actions; display: flex; flex-direction: column; gap: 6px; }
.rotates { display: flex; gap: 6px; }
/* 回転と「はめる」は子どもの指でも押しやすい大きさに（1 個 64px 以上、はめるがいちばん大きい） */
.rotates button { width: 64px; min-height: 64px; padding: 0; font-size: 1.9rem; border-radius: 14px; }
.actions .place { min-height: 76px; font-size: 1.35rem; border-radius: 16px; letter-spacing: 0.08em; }
.controller button.pressed { filter: brightness(0.9); transform: translateY(1px); }
.controller .legal { grid-area: legal; margin: 2px 0 0; font-size: 0.6rem; line-height: 1.35; }

/* 幅の狭いスマホ（360px 前後）：十字キーと間をほんの少し詰めて、大きいボタンを収める */
@media (max-width: 370px) {
  .controller { gap: 4px 6px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .dpad { grid-template-columns: repeat(3, 44px); }
}

/* スマホ横向き：盤面を左、情報・小窓・コントローラーを右に */
@media (max-height: 500px) and (orientation: landscape) {
  #view-play { display: grid; grid-template-columns: 1fr 344px; grid-template-rows: auto 1fr; }
  .hud-top { grid-column: 2; grid-row: 1; border-bottom: 0; border-left: 1px solid var(--line); }
  .hud-row { white-space: nowrap; font-size: 0.75rem; gap: 6px; }
  .step-label { font-size: 0.9rem; }
  .next-box { width: 88px; height: 58px; }
  .controller { grid-column: 2; grid-row: 2; border-top: 0; border-left: 1px solid var(--line); align-content: end; gap: 2px 8px; }
  .board-area { grid-column: 1; grid-row: 1 / 3; }
  .dpad { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 34px); }
  .tools .small { width: 36px; min-height: 32px; }
  .controller .legal { font-size: 0.55rem; }
  /* 小窓は盤面に重ならないよう、右の欄の見出しの下に置く（top は main.js で合わせる） */
  .mini { position: fixed; right: max(12px, env(safe-area-inset-right)); }
}

.toast {
  white-space: nowrap;
  position: fixed;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  background: rgba(30, 30, 30, 0.88);
  color: #fff;
  font-weight: bold;
  font-size: 1.3rem;
  padding: 12px 24px;
  border-radius: 14px;
  pointer-events: none;
  z-index: 10;
}
.toast.pop { animation: pop 1.1s ease-out forwards; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.05); }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}
.card {
  background: var(--panel);
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 360px;
  max-height: 100%;
  overflow-y: auto; /* 小さい画面では、カードの中をスクロールしてボタンまで行ける */
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}
.card h2 { margin: 0 0 8px; }
.card .buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.card .buttons > * { flex: 1; }

/* 大きく表示するカードだけ広くする（ほかのカードは 360px のまま） */
.card.sample-card { width: 100%; max-width: 520px; }

/* ---- 完成画面 ---- */

.finish h1 { font-size: 1.6rem; margin-bottom: 0; }
.finish-stats { font-size: 1.05rem; margin: 4px 0 12px; }
#finish-canvas {
  display: block;
  width: 100%;
  max-width: 560px;
  height: auto;
  margin: 0 auto;
  border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
.finish-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0 4px; }
.finish-actions > * { flex: 1; min-width: 140px; }
.note { color: var(--muted); font-size: 0.85rem; margin: 4px 0; }

.real { margin: 20px 0; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
.real summary { cursor: pointer; padding: 12px 14px; font-weight: bold; }
.real > div { padding: 0 14px 14px; }
.real h3 { margin: 16px 0 6px; font-size: 1rem; border-bottom: 2px solid var(--line); padding-bottom: 4px; }

table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: left; }
th.num, td.num { text-align: right; white-space: nowrap; }
tfoot td { font-weight: bold; border-bottom: 0; }

.swatch {
  display: inline-block;
  width: 1em;
  height: 1em;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.3);
  vertical-align: -0.15em;
  margin-right: 6px;
}

.warn {
  background: #fff4e5;
  color: #8a4b00;
  border-radius: 6px;
  padding: 8px 12px;
  margin: 8px 0;
}

.print-action { margin-top: 16px; }
.print-action button { width: 100%; }
.print-root { display: none; }

/* モードをえらぶ */
.mode-card h2 { margin: 0 0 12px; }
.guide-row { display: flex; align-items: center; gap: 12px; }
.guide-label { font-weight: bold; }
.guide-row .segmented button { min-width: 72px; }
.guide-note { margin: 6px 0 12px; font-size: 0.85rem; }
.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  margin: 8px 0;
  border-radius: 14px;
}
.mode-btn strong { font-size: 1.2rem; }
.mode-btn span { font-size: 0.85rem; opacity: 0.9; }
.mode-best { font-weight: bold; }
.tools .hint { font-size: 1.1rem; }
.dpad .pad:disabled { opacity: 0.3; }

/* 完成画面：結果とランキング */
.finish-result { margin: 16px 0 4px; }
.finish-result h3 { margin: 14px 0 6px; font-size: 1rem; border-bottom: 2px solid var(--line); padding-bottom: 4px; }
.result-table td { font-variant-numeric: tabular-nums; }
.ranking { list-style: none; padding: 0; margin: 0; }
.ranking li { display: flex; gap: 12px; align-items: baseline; padding: 6px 8px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.ranking li.me { background: #fff4e5; font-weight: bold; border-radius: 6px; }
.ranking .rank-no { width: 2.5em; }
.ranking .rank-time { flex: 1; }
.ranking .rank-date { color: var(--muted); font-size: 0.85rem; }
