:root {
  --bg: #eef1f8;
  --panel: #ffffff;
  --text: #1a1d2b;
  --muted: #5b6275;
  --line: #d5dae6;
  --accent: #e8a800;
  --accent-text: #1a1d2b;
  --danger: #d81b4a;
  --good: #159a3c;
  --stage-bg: #e2e6f0;
  --pad: #dfe4ef;
  --pad-knob: #9aa3b8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #07080f;
    --panel: #151a2e;
    --text: #eef1fa;
    --muted: #98a0b8;
    --line: #2a3150;
    --accent: #ffcf33;
    --accent-text: #1a1400;
    --danger: #ff4f7b;
    --good: #7dff9a;
    --stage-bg: #0d1020;
    --pad: #1b2140;
    --pad-knob: #4a5480;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input { font: inherit; color: inherit; }

[hidden] { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---- ボタン ---- */

.btn {
  min-height: 48px;
  padding: 0 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.small { min-height: 44px; padding: 0 14px; font-size: 15px; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; }

.icon-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 10px;
  background: transparent;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---- タイトル ---- */

#screen-title { align-items: center; justify-content: center; }

.title-box {
  position: relative;
  width: min(100%, 420px);
  padding: 16px;
  text-align: center;
}

#title-canvas {
  display: block;
  margin: 0 auto 8px;
  border-radius: 12px;
}

.logo {
  margin: 8px 0 0;
  font-size: 52px;
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 1;
}
.logo span { color: var(--accent); margin-left: 0.15em; }

.tagline { margin: 8px 0 24px; color: var(--muted); }

.menu { display: grid; gap: 12px; }
.ghost-btn { background: transparent; }

/* ---- ヘッダー ---- */

.bar, .hud {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  min-height: 52px;
}
.bar h2 { margin: 0; font-size: 20px; flex: 1; }
.star-total { color: var(--accent); font-weight: 700; padding-right: 8px; }

.scroll { flex: 1; overflow-y: auto; padding: 8px 16px 24px; -webkit-overflow-scrolling: touch; }

/* ---- ステージ選択 ---- */

.stage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 10px;
}

.stage-btn {
  min-height: 92px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: manipulation;
}
.stage-btn .num { font-size: 24px; font-weight: 900; }
.stage-btn .nm { font-size: 12px; color: var(--muted); }
.stage-btn .stars { color: var(--accent); font-size: 14px; letter-spacing: 1px; }
.stage-btn .best { font-size: 11px; color: var(--muted); }
.stage-btn:disabled { opacity: 0.35; }

.sub-head { margin: 28px 0 8px; font-size: 16px; }
.hint { color: var(--muted); font-size: 14px; margin: 4px 0; min-height: 1.2em; }

.custom-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.custom-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 6px 6px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
}
.custom-list .cname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.custom-list .ok { color: var(--good); font-size: 12px; }

.toggle { display: flex; align-items: center; gap: 10px; margin-top: 24px; min-height: 44px; }
.toggle input { width: 22px; height: 22px; }

/* ---- プレイ画面 ---- */

.hud { font-variant-numeric: tabular-nums; }
.hud-name { flex: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hud-stat { display: flex; align-items: center; gap: 4px; font-weight: 700; min-width: 52px; justify-content: flex-end; }
.gold-icon {
  width: 14px;
  height: 10px;
  background: var(--accent);
  clip-path: polygon(20% 0, 80% 0, 100% 100%, 0 100%);
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--stage-bg);
  overflow: hidden;
}
.stage canvas { display: block; touch-action: none; }

.banner {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 16px;
  border-radius: 14px;
  background: var(--panel);
  border: 2px solid var(--danger);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  white-space: nowrap;
  font-size: 14px;
}
.banner strong { color: var(--danger); }

/* チュートリアルの吹き出し。上はやられたときの帯が出るので、下に置く。 */
.tut {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px 10px 14px;
  border-radius: 14px;
  background: var(--panel);
  border: 2px solid var(--accent);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}
.tut p { flex: 1; margin: 0; font-size: 15px; line-height: 1.45; font-weight: 700; }

/* 次に触ってほしい操作部品を、光る輪で示す。 */
.tut-focus { animation: tut-pulse 1.1s ease-in-out infinite; }
@keyframes tut-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 80%, transparent); }
  50% { box-shadow: 0 0 0 10px color-mix(in srgb, var(--accent) 0%, transparent); }
}

.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 16px;
  gap: 12px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.pad {
  width: 156px;
  height: 156px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--pad);
  position: relative;
  touch-action: none;
}
.pad-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background:
    linear-gradient(var(--line), var(--line)) center / 2px 70% no-repeat,
    linear-gradient(var(--line), var(--line)) center / 70% 2px no-repeat;
}
.pad-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: var(--pad-knob);
  pointer-events: none;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 10px;
}
.act {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: none;
  background: var(--pad);
  font-size: 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
  touch-action: none;
}
.act small { font-size: 12px; margin-top: 2px; }
.act.dig { background: var(--accent); color: var(--accent-text); }
.act.rewind { grid-column: 1 / 3; justify-self: center; width: 64px; height: 48px; border-radius: 24px; font-size: 20px; }
.act.pressed { transform: scale(0.92); filter: brightness(1.2); }

/* 横向きのスマホでは、操作部を画面の左右に分ける */
@media (orientation: landscape) and (max-height: 540px) {
  #screen-game {
    display: grid;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto 1fr;
    grid-template-areas: 'hud hud hud' 'pad stage act';
  }
  .hud { grid-area: hud; min-height: 40px; }
  .stage { grid-area: stage; }
  .controls { display: contents; }
  .pad { grid-area: pad; align-self: center; margin: 0 12px; width: 140px; height: 140px; }
  .actions { grid-area: act; align-self: center; margin: 0 12px; }
}

/* ---- エディタ ---- */

.name-input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  font-size: 16px;
}

.editor-stage { flex: 0 1 auto; padding: 4px 0; }

.palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 8px 12px;
}
.pal-btn {
  min-height: 52px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  touch-action: manipulation;
}
.pal-btn canvas { width: 22px; height: 22px; }
.pal-btn.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--panel)); }

.ed-actions { display: flex; gap: 8px; padding: 0 12px; flex-wrap: wrap; }
.ed-actions .btn { flex: 1; }
#ed-msg { padding: 4px 12px; }

/* ---- ダイアログ ---- */

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  padding: 16px;
  z-index: 10;
}
.modal-box {
  width: min(100%, 380px);
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--panel);
  border-radius: 18px;
  padding: 20px;
  text-align: center;
}
.modal-box h2 { margin: 0 0 8px; }
.modal-buttons { display: grid; gap: 10px; margin-top: 16px; }
.big-stars { font-size: 40px; color: var(--accent); letter-spacing: 6px; }
.big-stars .off { color: var(--line); }
.result-line { margin: 6px 0; color: var(--muted); }
.result-line b { color: var(--text); }
.howto { text-align: left; padding-left: 20px; margin: 0; line-height: 1.6; font-size: 15px; }
.howto li { margin-bottom: 6px; }
.howto-tut { width: 100%; margin-top: 12px; }
