/* ===== リセット & ベース ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #00e5ff;
  --primary-dark: #00b8d4;
  --secondary: #ff9100;
  --success: #00e676;
  --danger: #ff1744;
  --bg: #0a0e17;
  --surface: rgba(10,20,35,0.92);
  --text: #c8d6e5;
  --text-muted: #5f7d95;
  --border: rgba(0,229,255,0.15);
  --radius: 2px;
  --shadow: 0 0 20px rgba(0,229,255,0.06), inset 0 0 1px rgba(0,229,255,0.08);
  --given-color: #00e5ff;
  --solved-color: #00e676;
  --glow-cyan: 0 0 8px rgba(0,229,255,0.4);
  --glow-amber: 0 0 8px rgba(255,145,0,0.4);
  --font-mono: 'Consolas', 'SF Mono', 'Fira Code', 'Courier New', monospace;
}

html { font-size: 16px; }

body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

/* ブループリント格子背景 */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    linear-gradient(rgba(0,229,255,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,229,255,0.03) 1px, transparent 1px),
    linear-gradient(rgba(0,229,255,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,229,255,0.06) 1px, transparent 1px);
  background-size:
    20px 20px,
    20px 20px,
    100px 100px,
    100px 100px;
  pointer-events: none;
  z-index: 0;
}

/* 薄いスキャンライン */
body::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: linear-gradient(90deg, transparent, rgba(0,229,255,0.5), transparent);
  box-shadow: 0 0 20px rgba(0,229,255,0.3), 0 0 60px rgba(0,229,255,0.1);
  animation: scanline 4s linear infinite;
  pointer-events: none;
  z-index: 1000;
}

@keyframes scanline {
  0%   { top: -4px; opacity: 0; }
  5%   { opacity: 1; }
  95%  { opacity: 1; }
  100% { top: 100vh; opacity: 0; }
}

/* ===== レイアウト ===== */
#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
  position: relative;
  min-height: 100vh;
  z-index: 1;
}

/* ===== ヘッダー ===== */
header {
  text-align: center;
  padding: 24px 0 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: 0.15em;
  text-transform: uppercase;
  text-shadow: 0 0 20px rgba(0,229,255,0.5), 0 0 40px rgba(0,229,255,0.2);
  animation: bootUp 1.2s ease-out;
}

@keyframes bootUp {
  0%   { opacity: 0; letter-spacing: 0.6em; filter: blur(8px); }
  40%  { opacity: 1; filter: blur(0); }
  55%  { opacity: 0.3; }
  65%  { opacity: 1; }
  75%  { opacity: 0.6; }
  85%  { opacity: 1; }
  100% { letter-spacing: 0.15em; }
}

.version {
  color: var(--secondary);
  margin-top: 4px;
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  animation: fadeSlideUp 0.8s 0.6s ease-out both;
}

.subtitle {
  color: var(--text-muted);
  margin-top: 4px;
  font-size: 0.8rem;
  letter-spacing: 0.05em;
  animation: fadeSlideUp 0.8s 0.9s ease-out both;
}

@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== ステップインジケーター ===== */
#step-indicator {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 20px;
  position: relative;
}

#step-indicator::before {
  content: '';
  position: absolute;
  top: 26px;
  left: 10%;
  right: 10%;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--primary), transparent);
  opacity: 0.3;
  z-index: 0;
}

#step-indicator::after {
  content: '';
  position: absolute;
  top: 25px;
  left: 10%;
  width: 6px;
  height: 3px;
  background: var(--primary);
  border-radius: 1px;
  box-shadow: 0 0 8px rgba(0,229,255,0.8);
  animation: dataFlow 3s linear infinite;
  z-index: 0;
}

@keyframes dataFlow {
  0%   { left: 10%; opacity: 0; }
  10%  { opacity: 1; }
  90%  { opacity: 1; }
  100% { left: 90%; opacity: 0; }
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.68rem;
  color: var(--text-muted);
  gap: 6px;
  z-index: 1;
  transition: color 0.3s;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.step span {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 0;
  background: rgba(0,229,255,0.05);
  border: 1px solid rgba(0,229,255,0.2);
  font-weight: 700;
  font-size: 0.85rem;
  transition: background 0.3s, color 0.3s, border-color 0.3s, box-shadow 0.3s;
  clip-path: polygon(15% 0%, 85% 0%, 100% 15%, 100% 85%, 85% 100%, 15% 100%, 0% 85%, 0% 15%);
}

.step.active {
  color: var(--primary);
}

.step.active span {
  background: rgba(0,229,255,0.15);
  border-color: var(--primary);
  color: var(--primary);
  box-shadow: var(--glow-cyan);
  animation: activePulse 2s ease-in-out infinite;
}

@keyframes activePulse {
  0%, 100% { box-shadow: 0 0 8px rgba(0,229,255,0.4); }
  50%      { box-shadow: 0 0 18px rgba(0,229,255,0.7), 0 0 30px rgba(0,229,255,0.2); }
}

.step.done span {
  background: rgba(0,230,118,0.15);
  border-color: var(--success);
  color: var(--success);
  box-shadow: 0 0 8px rgba(0,230,118,0.4);
}

/* ===== カード ===== */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  position: relative;
  animation: cardReveal 0.6s ease-out;
}

@keyframes cardReveal {
  0%   { opacity: 0; transform: translateY(20px); clip-path: inset(0 100% 0 0); }
  40%  { clip-path: inset(0 0 0 0); }
  100% { opacity: 1; transform: translateY(0); }
}

/* 角のブラケット装飾 */
.card::before,
.card::after {
  content: '';
  position: absolute;
  width: 12px;
  height: 12px;
  border-color: var(--primary);
  border-style: solid;
  opacity: 0.5;
}
.card::before {
  top: -1px;
  left: -1px;
  border-width: 2px 0 0 2px;
}
.card::after {
  bottom: -1px;
  right: -1px;
  border-width: 0 2px 2px 0;
}

.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--primary);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

/* ===== ボタン ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: 1px solid;
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  position: relative;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }

.btn svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.btn-primary {
  background: rgba(0,229,255,0.1);
  border-color: var(--primary);
  color: var(--primary);
  box-shadow: var(--glow-cyan);
  overflow: hidden;
}
.btn-primary::after {
  content: '';
  position: absolute;
  top: 0; left: -100%;
  width: 100%; height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0,229,255,0.15), transparent);
  transition: none;
}
.btn-primary:hover {
  background: rgba(0,229,255,0.2);
  box-shadow: 0 0 16px rgba(0,229,255,0.5);
}
.btn-primary:hover::after {
  animation: btnScan 0.6s ease-out;
}

@keyframes btnScan {
  from { left: -100%; }
  to   { left: 100%; }
}

.btn-secondary {
  background: rgba(255,145,0,0.1);
  border-color: var(--secondary);
  color: var(--secondary);
  box-shadow: var(--glow-amber);
}
.btn-secondary:hover {
  background: rgba(255,145,0,0.2);
  box-shadow: 0 0 16px rgba(255,145,0,0.5);
}

.btn-success {
  background: rgba(0,230,118,0.1);
  border-color: var(--success);
  color: var(--success);
  box-shadow: 0 0 8px rgba(0,230,118,0.4);
  overflow: hidden;
}
.btn-success::after {
  content: '';
  position: absolute;
  top: 0; left: -100%;
  width: 100%; height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0,230,118,0.15), transparent);
  transition: none;
}
.btn-success:hover {
  background: rgba(0,230,118,0.2);
  box-shadow: 0 0 16px rgba(0,230,118,0.5);
}
.btn-success:hover::after {
  animation: btnScanGreen 0.6s ease-out;
}

@keyframes btnScanGreen {
  from { left: -100%; }
  to   { left: 100%; }
}

.btn-ghost {
  background: transparent;
  border: 1px solid rgba(0,229,255,0.2);
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: rgba(0,229,255,0.05);
  border-color: var(--primary);
  color: var(--primary);
}

.input-buttons {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 20px;
}

.input-buttons .btn { flex: 1; min-width: 160px; justify-content: center; }

.btn-row {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 20px;
}

/* ===== Canvas / プレビュー ===== */
#camera-preview-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

#camera-video {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #000;
  box-shadow: var(--glow-cyan);
}

.camera-controls {
  display: flex;
  gap: 12px;
  justify-content: center;
}

#preview-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

canvas {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}

.canvas-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.canvas-wrap {
  flex: 1;
  min-width: 140px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.canvas-wrap label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* ===== 数独グリッド ===== */
.sudoku-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 1px;
  background: rgba(0,229,255,0.08);
  border: 2px solid rgba(0,229,255,0.3);
  border-radius: 0;
  overflow: hidden;
  width: 100%;
  max-width: 400px;
  margin: 0 auto 16px;
  aspect-ratio: 1;
  box-shadow: 0 0 30px rgba(0,229,255,0.08), inset 0 0 30px rgba(0,229,255,0.03);
  animation: gridBorderPulse 3s ease-in-out infinite;
}

@keyframes gridBorderPulse {
  0%, 100% { border-color: rgba(0,229,255,0.3); box-shadow: 0 0 30px rgba(0,229,255,0.08), inset 0 0 30px rgba(0,229,255,0.03); }
  50%      { border-color: rgba(0,229,255,0.55); box-shadow: 0 0 40px rgba(0,229,255,0.15), inset 0 0 40px rgba(0,229,255,0.06); }
}

.sudoku-cell {
  background: rgba(10,14,23,0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(12px, 3.5vw, 22px);
  font-weight: 700;
  font-family: var(--font-mono);
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s;
  -webkit-user-select: none;
  user-select: none;
  position: relative;
  aspect-ratio: 1;
  color: var(--text);
}

/* 3x3ブロック境界線 (縦) */
.sudoku-cell[data-col="2"],
.sudoku-cell[data-col="5"] {
  border-right: 2px solid rgba(0,229,255,0.25);
}

/* 3x3ブロック境界線 (横) */
.sudoku-cell[data-row="2"],
.sudoku-cell[data-row="5"] {
  border-bottom: 2px solid rgba(0,229,255,0.25);
}

.sudoku-cell.given {
  color: var(--given-color);
  background: rgba(0,229,255,0.06);
  text-shadow: 0 0 6px rgba(0,229,255,0.5);
}
.sudoku-cell.solved {
  color: var(--solved-color);
  text-shadow: 0 0 6px rgba(0,230,118,0.5);
  animation: cellReveal 0.4s ease-out both;
}

@keyframes cellReveal {
  0%   { opacity: 0; transform: scale(0.3); text-shadow: 0 0 20px rgba(0,230,118,1); }
  60%  { transform: scale(1.1); }
  100% { opacity: 1; transform: scale(1); text-shadow: 0 0 6px rgba(0,230,118,0.5); }
}
.sudoku-cell.selected {
  background: rgba(0,229,255,0.12);
  box-shadow: inset 0 0 12px rgba(0,229,255,0.15);
}
.sudoku-cell:hover:not(.given) {
  background: rgba(0,229,255,0.08);
}
.sudoku-cell.empty { color: var(--text-muted); }

/* 数独セル内の入力 (手動編集時のinput) */
.sudoku-cell input {
  width: 100%;
  height: 100%;
  border: none;
  background: transparent;
  text-align: center;
  font-size: inherit;
  font-weight: 700;
  font-family: var(--font-mono);
  color: inherit;
  outline: none;
  cursor: pointer;
}

/* ===== 解答結果 ===== */
#solve-result {
  text-align: center;
  padding: 10px;
  margin-bottom: 12px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#solve-result.success {
  background: rgba(0,230,118,0.08);
  border: 1px solid rgba(0,230,118,0.3);
  color: var(--success);
  box-shadow: 0 0 12px rgba(0,230,118,0.15);
}

#solve-result.error {
  background: rgba(255,23,68,0.08);
  border: 1px solid rgba(255,23,68,0.3);
  color: var(--danger);
  box-shadow: 0 0 12px rgba(255,23,68,0.15);
}

/* ===== エラーメッセージ ===== */
.error-msg {
  background: rgba(255,145,0,0.08);
  border: 1px solid rgba(255,145,0,0.3);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 0.82rem;
  color: var(--secondary);
  margin-bottom: 12px;
}

#manual-hint {
  margin-top: 8px;
  font-weight: 600;
}

/* ===== ローディング ===== */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10,14,23,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  z-index: 999;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
}

.spinner {
  width: 52px;
  height: 52px;
  border: 3px solid rgba(0,229,255,0.15);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite, spinGlow 2s ease-in-out infinite;
  box-shadow: var(--glow-cyan);
}

@keyframes spinGlow {
  0%, 100% { box-shadow: 0 0 8px rgba(0,229,255,0.4); }
  50%      { box-shadow: 0 0 25px rgba(0,229,255,0.7), 0 0 50px rgba(0,229,255,0.2); }
}

#loading-msg {
  color: var(--primary);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  animation: blink 1.5s step-end infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes blink {
  50% { opacity: 0.3; }
}

/* ===== セクション表示制御 ===== */
.step-section { display: none; }
.step-section.active { display: block; animation: sectionIn 0.5s ease-out; }

@keyframes sectionIn {
  from { opacity: 0; transform: translateX(30px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ===== ユーティリティ ===== */
.hidden { display: none !important; }
#input-gallery { display: none; }

/* ===== 解法解説画面 ===== */
#explain-progress {
  text-align: center;
  margin-bottom: 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

#explain-step-label {
  color: var(--primary);
  font-weight: bold;
}

.explain-reason {
  margin: 14px 0 8px;
  padding: 12px 16px;
  background: rgba(0, 229, 255, 0.06);
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  font-size: 0.88rem;
  line-height: 1.6;
  min-height: 48px;
  color: var(--text);
}

.explain-reason .reason-technique {
  display: inline-block;
  padding: 2px 8px;
  margin-right: 6px;
  font-size: 0.75rem;
  font-weight: bold;
  border-radius: 3px;
  vertical-align: middle;
}

.explain-reason .reason-technique.naked-single {
  background: rgba(0, 230, 118, 0.15);
  color: var(--success);
  border: 1px solid rgba(0, 230, 118, 0.3);
}

.explain-reason .reason-technique.hidden-single {
  background: rgba(255, 145, 0, 0.15);
  color: var(--secondary);
  border: 1px solid rgba(255, 145, 0, 0.3);
}

.explain-reason .reason-technique.backtrack {
  background: rgba(255, 23, 68, 0.15);
  color: var(--danger);
  border: 1px solid rgba(255, 23, 68, 0.3);
}

.sudoku-cell.explain-current {
  background: rgba(0, 229, 255, 0.25) !important;
  box-shadow: inset 0 0 12px rgba(0, 229, 255, 0.4), 0 0 8px rgba(0, 229, 255, 0.3);
  animation: cellPulse 0.8s ease-in-out infinite alternate;
  z-index: 1;
}

.sudoku-cell.explain-filled {
  color: var(--success) !important;
}

@keyframes cellPulse {
  from { box-shadow: inset 0 0 12px rgba(0, 229, 255, 0.4), 0 0 8px rgba(0, 229, 255, 0.3); }
  to   { box-shadow: inset 0 0 18px rgba(0, 229, 255, 0.6), 0 0 14px rgba(0, 229, 255, 0.5); }
}

.btn-row.explain-nav {
  justify-content: center;
  gap: 10px;
  margin-bottom: 4px;
}

.btn-row.explain-nav .btn {
  min-width: 80px;
}

/* ===== レスポンシブ (スマホ最適化) ===== */
@media (max-width: 480px) {
  .card { padding: 16px; }
  .btn { font-size: 0.78rem; padding: 10px 14px; }
  header h1 { font-size: 1.3rem; }
  .canvas-row { flex-direction: column; }
}
