:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --ink: #2b2b33;
  --muted: #7a7a85;
  --cell: #fbfaf6;
  --cell-line: #d9d4c7;
  --wall: #5b5866;
  --domino: #2f6fdb;
  --domino-start: #e0662f;
  --flag-hit: #3fae5a;
  --accent: #2f6fdb;
  --danger: #c8443a;
  --step: 120ms;
  --board-size: min(92vw, 480px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: 8px 4vw 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.hidden { display: none !important; }

.top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.top h1 { font-size: 1.25rem; margin: 6px 0; letter-spacing: 0.04em; }
.top-buttons { display: flex; gap: 6px; }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--cell-line);
  background: var(--panel);
  font-size: 1.05rem;
  cursor: pointer;
}

.tabs { width: 100%; display: flex; gap: 6px; margin: 4px 0 8px; }
.tab {
  flex: 1;
  padding: 9px 6px;
  border: 1px solid var(--cell-line);
  border-radius: 10px;
  background: var(--panel);
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--ink);
}
.tab.active { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: bold; }

.bar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  font-size: 0.9rem;
}
.bar select { font-size: 0.9rem; padding: 4px; margin-left: 4px; }

.info {
  display: flex;
  gap: 14px;
  margin: 6px 0;
  font-size: 0.95rem;
}
#remain.shake { animation: shake 0.4s; color: var(--danger); }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

.board-wrap { width: var(--board-size); }
.board {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  gap: 3px;
  padding: 3px;
  background: var(--cell-line);
  border-radius: 10px;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.board.busy { cursor: progress; }

.cell {
  position: relative;
  border: none;
  border-radius: 5px;
  background: var(--cell);
  padding: 0;
  margin: 0;
  overflow: hidden;
  cursor: default;
  font-size: calc(var(--board-size) / var(--n, 6) * 0.42);
  line-height: 1;
  color: var(--ink);
}
.cell.empty { cursor: pointer; }
.cell.empty:hover { background: #f0ece0; }
.cell.wall {
  background: var(--wall);
  background-image: repeating-linear-gradient(45deg, rgba(255,255,255,0.08) 0 4px, transparent 4px 8px);
}
.cell.flag { background: #fff6e6; }
.cell.turn { background: #e8f0ff; }
.cell.split { background: #f3e8ff; }
.cell.start { background: #ffece2; }

.sym {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--step) ease-out, opacity var(--step);
}
.cell.turn .sym { color: var(--accent); font-weight: bold; }
.cell.split .sym { color: #8a4fd0; }

.cell.flag.hit { background: #dff3e3; }
.cell.flag.hit .sym { transform: rotate(80deg) scale(0.85); opacity: 0.75; }

.cell.pulse { animation: pulse 0.35s ease-out; }
@keyframes pulse {
  0% { box-shadow: inset 0 0 0 0 rgba(47,111,219,0.7); }
  50% { box-shadow: inset 0 0 0 5px rgba(47,111,219,0.5); }
  100% { box-shadow: inset 0 0 0 0 rgba(47,111,219,0); }
}

/* ドミノ：--rot で向き（0=上）。立っている板を上から見た形、倒れると前方へ伸びる */
.domino {
  position: absolute;
  inset: 0;
  transform: rotate(var(--rot, 0deg));
  pointer-events: none;
}
.domino .plate {
  position: absolute;
  left: 18%;
  width: 64%;
  top: 40%;
  height: 18%;
  background: var(--domino);
  border-radius: 3px;
  box-shadow: 0 2px 0 rgba(0,0,0,0.25);
  transition: top var(--step) ease-in, height var(--step) ease-in, background var(--step);
}
.domino .plate::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -55%;
  transform: translateX(-50%);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 7px solid currentColor;
  color: var(--domino);
  opacity: 0.8;
}
.cell.start .domino .plate { background: var(--domino-start); }
.cell.start .domino .plate::after { color: var(--domino-start); }

.cell.fallen .domino .plate {
  top: 6%;
  height: 52%;
  box-shadow: none;
  opacity: 0.85;
}
.cell.fallen .domino .plate::after { opacity: 0; }

.domino.ghost .plate {
  background: transparent;
  border: 2px dashed var(--flag-hit);
  box-shadow: none;
}
.domino.ghost .plate::after { color: var(--flag-hit); }

.cell.hint { animation: hintblink 0.5s ease-in-out 4; }
@keyframes hintblink {
  0%, 100% { box-shadow: inset 0 0 0 0 rgba(63,174,90,0); }
  50% { box-shadow: inset 0 0 0 4px rgba(63,174,90,0.9); }
}

.msg { min-height: 1.4em; margin: 8px 0 4px; font-weight: bold; text-align: center; }
.msg.ok { color: var(--flag-hit); }
.msg.ng { color: var(--danger); }

.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--cell-line);
  background: var(--panel);
  color: var(--ink);
  font-size: 1rem;
  cursor: pointer;
  min-height: 44px;
}
.btn.small { padding: 5px 10px; font-size: 0.9rem; min-height: 32px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: bold; }
.btn.danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.attempts { margin: 8px 0 0; font-size: 0.95rem; }
.keys { margin: 6px 0 0; font-size: 0.75rem; color: var(--muted); text-align: center; }
.seed { margin: 6px 0 0; font-size: 0.7rem; color: var(--muted); word-break: break-all; }

/* モーダル */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 30, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
.modal-box {
  background: var(--panel);
  border-radius: 14px;
  padding: 18px 18px 14px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.modal-box h2 { margin: 0 0 10px; font-size: 1.2rem; }
.modal-box p { line-height: 1.6; font-size: 0.92rem; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; flex-wrap: wrap; }

.legend { list-style: none; padding: 0; margin: 8px 0; }
.legend li { display: flex; align-items: center; gap: 10px; margin: 6px 0; font-size: 0.9rem; }
.lg.cell {
  flex: 0 0 auto;
  display: inline-block;
  width: 36px;
  height: 36px;
  font-size: 18px;
  border: 1px solid var(--cell-line);
}

.stars { font-size: 2rem; text-align: center; margin: 4px 0; letter-spacing: 0.1em; }
.share-preview {
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.9rem;
  white-space: pre-wrap;
  font-family: inherit;
}
.share-fallback { width: 100%; margin-top: 8px; font-size: 0.85rem; }

.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; margin-bottom: 10px; }
.stats-grid .num { font-size: 1.4rem; font-weight: bold; }
.stats-grid .lbl { font-size: 0.72rem; color: var(--muted); }
.stats-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.stats-table th, .stats-table td { border-bottom: 1px solid var(--cell-line); padding: 5px; text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  background: rgba(30, 30, 40, 0.9);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 20;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 400px) {
  .top h1 { font-size: 1.05rem; }
  .btn { padding: 9px 12px; font-size: 0.95rem; }
  .info { gap: 10px; font-size: 0.88rem; }
}

@media (prefers-reduced-motion: reduce) {
  :root { --step: 30ms; }
  .cell.pulse, .cell.hint, #remain.shake { animation-duration: 0.01s; }
  .toast { transition: none; }
}
