:root {
  --main-font:
    "UD デジタル 教科書体 N-R",
    "UD デジタル 教科書体 NK-R",
    "UD Digi Kyokasho N-R",
    "UD Digi Kyokasho NK-R",
    "BIZ UDPGothic",
    "BIZ UDゴシック",
    "Yu Gothic UI",
    "Yu Gothic",
    "Meiryo",
    sans-serif;
  --bg: #fdf7eb;
  --surface: #ffffff;
  --surface-2: #fff8ef;
  --border: #d8bf8a;
  --accent: #3f7d20;
  --accent-soft: #e7f3dc;
  --warning: #c95b1d;
  --text: #2f241c;
  --muted: #6e5d49;
  --shadow: 0 12px 30px rgba(70, 45, 15, 0.12);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--main-font);
  background: linear-gradient(180deg, var(--bg), #f7f1e3 100%);
  color: var(--text);
  line-height: 1.5;
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--warning);
  outline-offset: 2px;
}

.app-shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 14px 28px;
}

.top-bar {
  text-align: center;
  padding: 12px 8px 6px;
}

.top-bar h1 {
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  margin: 0 0 6px;
}

.top-bar p {
  margin: 0;
  font-size: clamp(1rem, 2.4vw, 1.15rem);
  color: var(--muted);
}

.screen {
  display: none;
}

.screen.active {
  display: block;
}

.card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.card h2 {
  font-size: clamp(1.35rem, 3vw, 1.8rem);
  margin: 0 0 12px;
}

.mode-buttons {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 16px;
}

.primary-btn,
.secondary-btn,
.choice-btn,
.part-btn {
  border: 2px solid var(--border);
  border-radius: 999px;
  padding: 14px 18px;
  font-size: clamp(1.1rem, 2.2vw, 1.35rem);
  min-height: 54px;
  font-weight: 600;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.primary-btn {
  background: var(--accent);
  color: white;
}

.secondary-btn,
.choice-btn,
.part-btn {
  background: var(--surface-2);
  color: var(--text);
}

.primary-btn:active,
.secondary-btn:active,
.choice-btn:active,
.part-btn:active {
  transform: scale(0.98);
}

.primary-btn:hover,
.secondary-btn:hover,
.choice-btn:hover,
.part-btn:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.primary-btn:disabled,
.secondary-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.wide-btn {
  width: 100%;
}

.selector-block {
  margin-top: 14px;
}

.selector-label,
.settings-group legend {
  font-weight: 700;
  margin-bottom: 10px;
  display: inline-block;
}

.segment-group,
.segmented-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.part-btn.active,
.choice-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.option-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 10px;
}

.option-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 1.05rem;
}

.option-chip input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.weekday-ruby {
  ruby-position: over;
  ruby-align: center;
  white-space: nowrap;
}

.weekday-ruby rt {
  font-size: 0.55em;
  line-height: 1;
}

.settings-card {
  display: grid;
  gap: 16px;
}

.settings-group {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 14px;
  margin: 0;
}

.settings-message {
  min-height: 28px;
  color: var(--warning);
  font-weight: 700;
}

.game-card {
  display: grid;
  gap: 16px;
}

.game-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.mode-badge {
  display: inline-block;
  margin: 4px 0 0;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.status-grid {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.status-box {
  min-width: 120px;
  background: var(--surface-2);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 10px 12px;
  text-align: center;
}

#weekday-reading {
  display: inline-block;
  min-width: 6em;
  padding-top: 0.2em;
}

.status-label {
  display: block;
  font-size: 0.95rem;
  color: var(--muted);
}

.game-body {
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  align-items: start;
}

.sample-area {
  background: var(--surface-2);
  border: 2px dashed var(--border);
  border-radius: 20px;
  padding: 14px;
  text-align: center;
}

.sample-area h3,
.pool-panel h3 {
  margin: 0 0 8px;
  font-size: 1.15rem;
}

.sample-area canvas {
  width: 100%;
  max-width: 300px;
  height: auto;
  display: block;
  margin: 0 auto;
}

.board-wrap {
  display: grid;
  gap: 12px;
}

.board-grid {
  display: grid;
  gap: 10px;
  width: min(100%, 520px);
  aspect-ratio: 1 / 1;
}

.board-grid.grid-1x2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(1, minmax(0, 1fr));
}

.board-grid.grid-2x2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

.board-grid.grid-2x3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

.board-grid.grid-3x3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
}

.board-cell {
  border: 3px solid var(--border);
  background: linear-gradient(145deg, #fffaf1, #f7ecd1);
  border-radius: 20px;
  min-height: 72px;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: 6px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.04);
}

.board-cell.drag-target {
  border-color: var(--warning);
  box-shadow: inset 0 0 0 3px rgba(201, 91, 29, 0.2);
}

.piece-button {
  width: 100%;
  height: 100%;
  border: none;
  background: transparent;
  padding: 0;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.piece-button canvas {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.piece-button.correct {
  border: 3px solid var(--accent);
  box-shadow: inset 0 0 0 2px rgba(63, 125, 32, 0.18);
}

.piece-button.dragging {
  opacity: 0.25;
}

.pool-panel {
  background: linear-gradient(180deg, #fffaf1, #f6ebd3);
  border: 3px dashed var(--border);
  border-radius: 24px;
  padding: 12px;
  min-height: 220px;
}

.piece-pool {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  min-height: 180px;
  align-content: start;
}

.bottom-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.result-summary {
  display: grid;
  gap: 10px;
  margin: 16px 0 20px;
  font-size: 1.15rem;
}

.result-item {
  background: var(--surface-2);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(38, 29, 18, 0.2);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  padding: 16px;
}

.overlay.hidden,
.drag-ghost.hidden {
  display: none;
}

.overlay-card,
.success-card {
  width: min(92vw, 360px);
  background: var(--surface);
  border: 3px solid var(--border);
  border-radius: 24px;
  padding: 24px;
  text-align: center;
  box-shadow: var(--shadow);
}

.success-mark {
  font-size: 5rem;
  color: var(--accent);
  line-height: 1;
  margin-bottom: 6px;
}

.success-text {
  font-size: 1.6rem;
  font-weight: 700;
}

.drag-ghost {
  position: fixed;
  z-index: 1100;
  pointer-events: none;
  width: 120px;
  height: 120px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.95);
  border: 3px solid var(--border);
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translate(-50%, -50%);
}

.drag-ghost canvas {
  width: 100%;
  height: 100%;
}

.confetti-piece {
  position: fixed;
  width: 10px;
  height: 18px;
  border-radius: 3px;
  z-index: 1050;
  animation: fall linear forwards;
  will-change: transform, opacity;
}

@keyframes fall {
  0% {
    transform: translate3d(0, -20px, 0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translate3d(var(--drift), 120vh, 0) rotate(720deg);
    opacity: 0;
  }
}

@media (max-width: 900px) {
  .game-body {
    grid-template-columns: 1fr;
  }

  .sample-area {
    order: 2;
  }

  .board-wrap {
    order: 1;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 10px 10px 20px;
  }

  .card {
    padding: 14px;
  }

  .mode-buttons {
    grid-template-columns: 1fr;
  }

  .option-grid {
    grid-template-columns: 1fr 1fr;
  }

  .bottom-actions {
    justify-content: stretch;
  }

  .bottom-actions > * {
    flex: 1 1 calc(50% - 10px);
  }

  .piece-pool {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .board-grid {
    width: 100%;
  }
}

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