* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #f6f1e7;
  --ink: #3a3326;
  --sub: #8a7f6a;
  --card: #fffdf8;
  --accent: #e0703a;
  --line: #d9cdb6;
  --w: 5;
  --h: 5;
  --cell: min(calc((min(100vw, 480px) - 36px) / var(--w)), calc((100vh - 350px) / var(--h)), 60px);
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
}

.screen {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-height: 100vh;
  padding: 8px 12px 12px;
  gap: 8px;
}

/* ボタン */
.btn {
  font: inherit;
  font-size: 15px;
  padding: 10px 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 2px 0 var(--line);
  touch-action: manipulation;
}
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: none; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: #b9542a; color: #fff; box-shadow: 0 2px 0 #b9542a; }
.btn.big { font-size: 19px; padding: 14px; }
.btn.icon { padding: 6px 12px; font-size: 18px; }

/* タイトル */
#screen-title { justify-content: center; text-align: center; gap: 18px; }
.logo { margin: 0; line-height: 1.1; }
.logo-a { display: block; font-size: 22px; color: var(--sub); letter-spacing: 0.3em; }
.logo-b {
  display: inline-block; font-size: 52px; color: var(--accent);
  text-shadow: 3px 3px 0 #f2c230, 6px 6px 0 #3d7be0;
  transform: rotate(-3deg);
}
.tagline { margin: 0; color: var(--sub); }
.menu { display: flex; flex-direction: column; gap: 12px; padding: 0 24px; }
.menu-row { display: flex; gap: 12px; }
.menu-row .btn { flex: 1; }
.check { color: #fff; background: #3bb273; border-radius: 50%; padding: 0 6px; margin-left: 4px; }
.title-stats { display: flex; justify-content: center; gap: 24px; color: var(--sub); }
.stat-label { font-size: 12px; margin-right: 6px; }
.stat-value { font-size: 22px; color: var(--ink); font-weight: bold; }

/* 上部バー */
.bar { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.bar h2 { flex: 1; margin: 0; font-size: 17px; text-align: center; }
.bar-spacer { width: 48px; }
.moves { min-width: 64px; text-align: right; font-size: 14px; color: var(--sub); }
.moves b { color: var(--ink); font-size: 17px; }

/* ステージ選択 */
.stage-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding-bottom: 16px;
}
.stage-btn {
  aspect-ratio: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 4px;
  font-size: 18px; font-weight: bold;
}
.stage-btn .s-stars { font-size: 11px; color: #e0a800; letter-spacing: -1px; min-height: 1em; }
.stage-btn.locked { background: #eee6d6; color: var(--sub); }

/* 注文 */
.orders { display: flex; align-items: center; gap: 8px; background: var(--card); border-radius: 14px; padding: 8px 10px; border: 2px solid var(--line); }
.orders-label, .tray-label { font-size: 12px; color: var(--sub); writing-mode: vertical-rl; letter-spacing: 0.2em; }
.order-cards { flex: 1; display: flex; align-items: center; gap: 8px; min-height: 60px; }
.order-card {
  display: flex; align-items: center; justify-content: center;
  border-radius: 10px; color: #fff; font-weight: bold;
  border: 2px solid rgba(0, 0, 0, 0.25);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4);
}
.order-card.first { width: 58px; height: 58px; font-size: 26px; animation: slideIn 220ms ease-out; }
.order-card.next { width: 36px; height: 36px; font-size: 16px; opacity: 0.85; }
.order-card.done { width: 58px; height: 58px; background: #ddd; color: #888; font-size: 14px; text-shadow: none; }
.orders-rest { font-size: 13px; color: var(--sub); white-space: nowrap; }
.orders-rest b { font-size: 18px; color: var(--ink); }

/* 盤面 */
.board-wrap { display: flex; justify-content: center; padding: 4px 0; }
.board {
  position: relative;
  width: calc(var(--cell) * var(--w));
  height: calc(var(--cell) * var(--h));
  background-color: #e9dfcb;
  background-image:
    linear-gradient(to right, rgba(0, 0, 0, 0.07) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0, 0, 0, 0.07) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  border-radius: 8px;
  box-shadow: inset 0 0 0 2px var(--line);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.paper {
  position: absolute;
  inset: 0;
  pointer-events: none;
  filter: drop-shadow(0 2px 1.5px rgba(0, 0, 0, 0.35));
  transform-origin: 50% 50%;
}
.paper.removed { display: none; }
.pcell {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  pointer-events: auto;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: rgba(255, 255, 255, 0.85);
  font-size: calc(var(--cell) * 0.32);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
  border: 0 solid rgba(0, 0, 0, 0.35);
}
.pcell.bt { border-top-width: 2px; }
.pcell.bb { border-bottom-width: 2px; }
.pcell.bl { border-left-width: 2px; }
.pcell.br { border-right-width: 2px; }
.pcell .sym.main { font-size: calc(var(--cell) * 0.46); color: #fff; }

.paper.peeling { animation: peel 250ms ease-in forwards; pointer-events: none; }
.paper.peeling .pcell { pointer-events: none; }
.paper.shake { animation: shake 300ms; }
.paper.hint { animation: hintPulse 0.5s ease-in-out 3; }

@keyframes peel {
  0% { transform: none; opacity: 1; }
  40% { transform: translateY(-6px) rotate(-4deg) scale(1.04); opacity: 1; }
  100% { transform: translateY(-40px) rotate(-12deg) scale(1.1); opacity: 0; }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}
@keyframes hintPulse {
  0%, 100% { filter: drop-shadow(0 2px 1.5px rgba(0, 0, 0, 0.35)); }
  50% { filter: drop-shadow(0 0 6px #fff) drop-shadow(0 0 10px #ffd400); }
}
@keyframes slideIn {
  from { transform: translateX(24px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* トレイ */
.tray-area { display: flex; align-items: center; gap: 8px; justify-content: center; }
.tray { display: flex; gap: 10px; padding: 6px; border-radius: 12px; }
.tray.warn { animation: warn 0.8s ease-in-out infinite; }
.slot {
  width: 48px; height: 48px;
  border: 2px dashed var(--line);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
}
.slot.filled { border-style: solid; border-color: rgba(0, 0, 0, 0.25); color: #fff; font-size: 22px; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4); animation: drop 200ms ease-out; }
.slot.fly { animation: flyUp 220ms ease-in forwards; }
@keyframes warn {
  0%, 100% { box-shadow: 0 0 0 2px rgba(232, 80, 91, 0.2); }
  50% { box-shadow: 0 0 0 3px #e8505b; background: rgba(232, 80, 91, 0.12); }
}
@keyframes drop {
  from { transform: translateY(-16px) scale(0.8); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes flyUp {
  to { transform: translateY(-120px) scale(0.6); opacity: 0; }
}

.message { min-height: 1.4em; text-align: center; font-size: 13px; color: #b9542a; }

.controls { display: flex; gap: 8px; }
.controls .btn { flex: 1; padding: 10px 4px; }

/* モーダル */
.modal {
  position: fixed; inset: 0;
  background: rgba(40, 32, 20, 0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal-box {
  width: 100%; max-width: 380px; max-height: 90vh; overflow-y: auto;
  background: var(--card); border-radius: 16px; padding: 18px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  animation: pop 180ms ease-out;
}
.modal-box h3 { margin: 0 0 10px; text-align: center; font-size: 20px; }
.modal-buttons { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.result-stars { text-align: center; font-size: 40px; letter-spacing: 6px; color: #e0a800; }
.result-stars .off { color: #ddd; }
.result-detail { text-align: center; color: var(--sub); white-space: pre-line; margin: 8px 0 0; }
.help-list { padding-left: 20px; margin: 0; line-height: 1.6; font-size: 14px; }
.help-list li { margin-bottom: 6px; }
.help-note { font-size: 13px; color: var(--sub); }
.toggle { display: flex; align-items: center; gap: 10px; padding: 10px 4px; font-size: 16px; }
.toggle input { width: 22px; height: 22px; }
@keyframes pop {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: none; opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; }
}
