*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #f5f0e6;
  --surface: #fffdf8;
  --text: #2c2416;
  --muted: #6b5d4a;
  --stick: #8b5a2b;
  --stick-tip: #c0392b;
  --primary: #2d6a4f;
  --primary-text: #fff;
  --disabled: #aaa;
  --clear: #40916c;
  --revealed: #e07a2f;
  --error: #c0392b;
  --border: #d4c4a8;
  --shadow: rgba(44, 36, 22, 0.12);
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* hidden 属性を CSS display より優先（BUG-001/BUG-002 対策） */
[hidden] {
  display: none !important;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 16px 32px;
}

.app-header {
  text-align: center;
  margin-bottom: 16px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.5rem;
}

.subtitle {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.view h2 {
  font-size: 1.2rem;
  margin: 0 0 8px;
}

.list-hint {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 12px;
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}

.puzzle-card {
  aspect-ratio: 1;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 2px;
  box-shadow: 0 2px 4px var(--shadow);
  transition: transform 0.1s, border-color 0.15s;
  touch-action: manipulation;
  user-select: none;
}

.puzzle-card:active {
  transform: scale(0.96);
}

.puzzle-card.unplayed {
  border-color: var(--border);
}

.puzzle-card.cleared {
  border-color: var(--clear);
  background: #eef7f0;
  color: var(--clear);
}

.puzzle-card.revealed {
  border-color: var(--revealed);
  background: #fff5eb;
  color: var(--revealed);
}

.puzzle-card.revealed-cleared {
  border-color: var(--clear);
  background: linear-gradient(135deg, #eef7f0 60%, #fff5eb 100%);
  color: var(--text);
}

.puzzle-card .card-num {
  font-size: 1.1rem;
  font-weight: 700;
}

.puzzle-card .card-badges {
  display: flex;
  gap: 3px;
  margin-top: 2px;
}

.card-badge {
  font-size: 0.55rem;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 4px;
  line-height: 1.2;
}

.badge-cleared {
  background: var(--clear);
  color: #fff;
}

.badge-revealed {
  background: var(--revealed);
  color: #fff;
}

.puzzle-card .card-type {
  font-size: 0.55rem;
  font-weight: 500;
  color: var(--muted);
}

.play-header {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.puzzle-badge, .type-badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e8dfd0;
  color: var(--muted);
}

.prompt {
  font-size: 1rem;
  line-height: 1.5;
  margin: 0 0 12px;
}

.legend {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 12px;
  overflow-x: auto;
}

.legend-title {
  font-size: 0.7rem;
  color: var(--muted);
  display: block;
  margin-bottom: 4px;
}

.legend-digits {
  display: flex;
  gap: 6px;
  min-width: max-content;
}

.legend-digit {
  flex-shrink: 0;
}

.equation-area {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  margin-bottom: 8px;
  overflow-x: auto;
  touch-action: none;
}

.equation-svg {
  display: block;
  width: 100%;
  min-height: 120px;
}

.remove-zone,
.stock-zone {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 8px;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
  min-height: 48px;
}

.remove-zone.drag-over,
.stock-zone.drag-over {
  border-color: var(--revealed);
  background: #fff5eb;
}

.stock-svg {
  width: 60px;
  height: 40px;
  margin: 4px auto 0;
}

.ops-remaining {
  text-align: center;
  font-size: 0.9rem;
  font-weight: 600;
  margin: 8px 0;
  color: var(--muted);
}

.result-message {
  text-align: center;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 10px;
  border-radius: 8px;
  margin: 8px 0;
}

.result-message.correct {
  background: #d8f3dc;
  color: var(--clear);
}

.result-message.incorrect {
  background: #fde8e8;
  color: var(--error);
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.btn {
  flex: 1;
  min-width: 90px;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  background: #ece6da;
  color: var(--disabled);
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

.btn-primary:disabled {
  background: #9ab8a8;
  border-color: #9ab8a8;
}

.btn-hint:not(:disabled) {
  border-color: #f4d03f;
}

.btn-answer:not(:disabled) {
  border-color: var(--revealed);
}

.error-view {
  text-align: center;
  padding: 24px 0;
}

.error-message {
  color: var(--error);
  margin: 16px 0 24px;
  line-height: 1.6;
}

.modal-overlay:not([hidden]) {
  display: flex;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  max-width: 360px;
  width: 100%;
  box-shadow: 0 8px 24px var(--shadow);
}

.modal h3 {
  margin: 0 0 12px;
}

.modal-body {
  margin: 0 0 16px;
  line-height: 1.6;
}

.modal-body p {
  margin: 0 0 12px;
}

.answer-variant {
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border, #ddd);
}

.answer-variant:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.answer-variant-title {
  font-size: 1rem;
  margin: 0 0 0.75rem;
  color: var(--accent, #c45a11);
}

.answer-display {
  font-size: 0.92rem;
}

.answer-row {
  display: flex;
  gap: 8px;
  align-items: baseline;
  margin: 0 0 8px;
}

.answer-label {
  font-weight: 700;
  color: var(--muted);
  min-width: 4.5em;
  flex-shrink: 0;
}

.answer-value {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 1.05rem;
}

.answer-eq {
  color: var(--primary);
  font-weight: 700;
}

.answer-compare {
  background: #f8f4ec;
  border-radius: 8px;
  padding: 10px;
  margin: 8px 0 12px;
}

.answer-steps-title {
  margin: 0 0 6px;
}

.answer-steps {
  margin: 0;
  padding-left: 1.4em;
}

.answer-steps li {
  margin-bottom: 6px;
}

.answer-fallback {
  white-space: pre-wrap;
}

/* SVG stick styles */
.stick-line {
  stroke: var(--stick);
  stroke-width: 6;
  stroke-linecap: round;
  cursor: grab;
}

.stick-line.dragging {
  cursor: grabbing;
  opacity: 0.75;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.25));
}

.stick-hit {
  fill: transparent;
  cursor: grab;
}

.stick-hit.dragging {
  cursor: grabbing;
}

.stick-tip {
  fill: var(--stick-tip);
}

.slot-guide {
  fill: none;
  stroke: rgba(139, 90, 43, 0.08);
  stroke-width: 1;
}

@media (max-width: 360px) {
  .puzzle-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .btn {
    min-width: 80px;
    font-size: 0.82rem;
  }
}
