*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', sans-serif;
  background: #1a1a2e;
  color: #eee;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  user-select: none;
}

h1 {
  font-size: 1.4rem;
  margin-bottom: 8px;
  color: #e0aaff;
  letter-spacing: 2px;
}

.controls-bar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.controls-bar label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
}

.controls-bar input[type=number] {
  width: 80px;
  background: #2d2d4e;
  border: 1px solid #555;
  color: #eee;
  padding: 3px 6px;
  border-radius: 4px;
}

.controls-bar input[type=checkbox] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

button {
  background: #3d3d6b;
  border: 1px solid #6060aa;
  color: #eee;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: background 0.15s;
  white-space: nowrap;
}

button:hover:not(:disabled) { background: #5050a0; }
button:disabled { opacity: 0.4; cursor: not-allowed; }

button.primary {
  background: #7b2d8b;
  border-color: #b060d0;
}
button.primary:hover:not(:disabled) { background: #9b3dab; }

button.danger {
  background: #8b2d2d;
  border-color: #d06060;
}

.game-area {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

/* ─── FIELD ─────────────────────────────────── */
.field-wrap {
  position: relative;
}

#field {
  display: grid;
  grid-template-columns: repeat(6, 38px);
  grid-template-rows: repeat(13, 38px);
  gap: 3px;
  background: #0d0d1f;
  padding: 6px;
  border: 2px solid #3d3d6b;
  border-radius: 6px;
  outline: none;
}

.cell {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  transition: background 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 19px;
  color: rgba(255,255,255,0.92);
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  font-family: 'Segoe UI', monospace;
}

.cell-empty { background: #2a2a50; border: 1px solid #3a3a70; }
/* R=赤, Y=黄, G=緑, B=青, #=おじゃま */
.cell-R { background: radial-gradient(circle at 35% 35%, #ff6b6b, #c0392b); box-shadow: 0 2px 4px rgba(0,0,0,.4); }
.cell-Y { background: radial-gradient(circle at 35% 35%, #ffd93d, #e6a817); color: rgba(0,0,0,0.8); text-shadow: none; box-shadow: 0 2px 4px rgba(0,0,0,.4); }
.cell-G { background: radial-gradient(circle at 35% 35%, #6bcb77, #27ae60); box-shadow: 0 2px 4px rgba(0,0,0,.4); }
.cell-B { background: radial-gradient(circle at 35% 35%, #74b9ff, #2980b9); box-shadow: 0 2px 4px rgba(0,0,0,.4); }
.cell-hash { background: radial-gradient(circle at 35% 35%, #bdc3c7, #7f8c8d); box-shadow: 0 2px 4px rgba(0,0,0,.4); }

.cell-ghost-R { background: rgba(192,57,43,0.25); border: 2px dashed rgba(255,107,107,0.7); color: rgba(255,150,150,0.9); }
.cell-ghost-Y { background: rgba(230,168,23,0.25); border: 2px dashed rgba(255,217,61,0.7); color: rgba(255,220,80,0.9); }
.cell-ghost-G { background: rgba(39,174,96,0.25); border: 2px dashed rgba(107,203,119,0.7); color: rgba(120,220,130,0.9); }
.cell-ghost-B { background: rgba(41,128,185,0.25); border: 2px dashed rgba(116,185,255,0.7); color: rgba(130,190,255,0.9); }

.cell-pop { animation: pop 0.3s ease-out forwards; }
@keyframes pop {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.5; }
  100% { transform: scale(0); opacity: 0; }
}

/* AI highlight on field */
.cell-ai-best { box-shadow: 0 0 10px 3px gold; }

/* ─── SIDEBAR ─────────────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 160px;
}

.panel {
  background: #16213e;
  border: 1px solid #3d3d6b;
  border-radius: 8px;
  padding: 10px;
}

.panel h3 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #aaa;
  margin-bottom: 8px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  color: #ccc;
}

.setting-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.setting-control input[type=range] {
  flex: 1;
  accent-color: #4cc9f0;
}

.setting-control span {
  min-width: 2.5rem;
  text-align: right;
  color: #4cc9f0;
  font-weight: bold;
}

details.panel > summary {
  list-style: none;
}
details.panel > summary::-webkit-details-marker {
  display: none;
}

/* Current piece preview */
.piece-display {
  display: grid;
  grid-template-columns: repeat(2, 34px);
  grid-template-rows: repeat(2, 34px);
  gap: 3px;
  margin-bottom: 6px;
}

.piece-cell {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #1e1e3a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 16px;
  color: rgba(255,255,255,0.92);
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}

.rotation-label {
  font-size: 0.75rem;
  color: #aaa;
  text-align: center;
}

/* Next queue */
.next-item {
  display: flex;
  gap: 4px;
  margin-bottom: 5px;
  align-items: center;
}

.next-item span {
  font-size: 0.7rem;
  color: #888;
  width: 14px;
}

.mini-cell {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #1e1e3a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 12px;
  color: rgba(255,255,255,0.92);
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}

.mini-cell.R { background: radial-gradient(circle at 35% 35%, #ff6b6b, #c0392b); }
.mini-cell.Y { background: radial-gradient(circle at 35% 35%, #ffd93d, #e6a817); color: rgba(0,0,0,0.75); text-shadow: none; }
.mini-cell.G { background: radial-gradient(circle at 35% 35%, #6bcb77, #27ae60); }
.mini-cell.B { background: radial-gradient(circle at 35% 35%, #74b9ff, #2980b9); }

/* Controls */
.move-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, auto);
  gap: 5px;
  margin-top: 6px;
}

.move-grid button { padding: 5px 2px; font-size: 0.75rem; }
.move-grid .drop-btn { grid-column: 2; }

.rotate-row {
  display: flex;
  gap: 5px;
  margin-bottom: 5px;
}

.rotate-row button { flex: 1; font-size: 0.75rem; padding: 5px; }

.undo-row {
  display: flex;
  gap: 5px;
}
.undo-row button { flex: 1; font-size: 0.75rem; padding: 5px; }

/* Stats */
.stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
}

.stats .val { color: #e0aaff; font-weight: bold; font-size: 1.1rem; }

/* ─── AI OVERLAY ─────────────────────────────── */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 100;
  justify-content: center;
  align-items: center;
}

.overlay.active { display: flex; }

.modal {
  background: #16213e;
  border: 2px solid #6060aa;
  border-radius: 12px;
  padding: 20px;
  max-width: 420px;
  width: 90%;
}

.modal h3 {
  margin-bottom: 12px;
  font-size: 1rem;
  color: #e0aaff;
}

.modal p { margin-bottom: 12px; font-size: 0.9rem; color: #ccc; }

.modal-buttons {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 14px;
}

/* AI candidates list */
.candidate-list { list-style: none; }

.candidate-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  margin-bottom: 5px;
  background: #1e1e3a;
  font-size: 0.85rem;
}

.candidate-list li.best { background: #2a3a1a; border: 1px solid #6bcb77; }

.rank-badge {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #3d3d6b;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: bold;
  flex-shrink: 0;
}

.rank-badge.gold { background: #b8860b; color: #ffe; }

.cand-detail { flex: 1; }
.cand-score { color: #aaa; font-size: 0.75rem; }

/* AI status indicator */
.ai-status {
  font-size: 0.7rem;
  color: #888;
  margin-top: 4px;
}

.ai-status.querying { color: #ffd93d; }
.ai-status.ready { color: #6bcb77; }
.ai-status.error { color: #ff6b6b; }

/* Chain notification */
.chain-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.85);
  color: gold;
  font-size: 2rem;
  font-weight: bold;
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid gold;
  pointer-events: none;
  display: none;
  z-index: 10;
  white-space: nowrap;
}

.chain-banner.show { display: block; }
