:root {
  --primary: #6c5ce7;
  --light-bg: #f8f9fa;
  --cell-bg: #fff;
  --border: #dfe6e9;
  --text: #2d3436;
  --error: #e74c3c;
  --user: #0984e3;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, sans-serif;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--text);
  padding: 10px;
}

.container {
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.12);
  padding: 20px 28px;
  width: 100%;
  max-width: 880px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

header {
  text-align: center;
}

h1 {
  font-size: 2.2rem;
  color: var(--primary);
  margin-bottom: 12px;
}

.info {
  display: flex;
  justify-content: center;
  gap: 32px;
  font-size: 1.05rem;
  font-weight: 500;
}

.level {
  display: flex;
  background: #dfe6e9;
  border-radius: 999px;
  padding: 4px;
}

.level-btn {
  padding: 6px 16px;
  border: none;
  background: transparent;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
}

.level-btn.active {
  background: var(--primary);
  color: white;
}

.main {
  display: flex;
  flex-direction: row;
  gap: 28px;
  flex-grow: 1;
}

.board {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 540px;
  background: var(--primary);
  border: 3px solid var(--primary);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

.cell {
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 500;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s;
}

.cell:nth-child(3n)   { border-right: 3px solid var(--primary); }
.cell:nth-child(9n)   { border-right: none; }
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) { border-bottom: 3px solid var(--primary); }
.cell:nth-child(n+73) { border-bottom: none; }

.cell.selected { background: #e0deff !important; }
.cell.highlight { background: #f1f2f6; }
.cell.error { background: #ffebee !important; animation: shake 0.4s; }
.cell.system { color: #2d3436; font-weight: bold; pointer-events: none; }
.cell.user { color: var(--user); }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 170px;
  padding-top: 40px;
}

.sidebar button {
  padding: 14px 24px;
  font-size: 1.05rem;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

#new-game {
  background: var(--primary);
  color: white;
}

#undo, #clear {
  background: #f0f2f5;
  color: var(--text);
}

.sidebar button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* 數字鍵盤 - 強制一行 + 允許稍微滾動 */
.numpad {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;              /* 禁止換行，強制一行 */
  justify-content: center;
  gap: 12px;                      /* 間距稍小一點 */
  max-width: 100%;                /* 占滿可用寬度 */
  overflow-x: auto;               /* 如果太寬，允許水平滾動（手機上比較有用） */
  padding: 8px 0;
  scrollbar-width: thin;          /* Firefox 細捲軸 */
  -ms-overflow-style: none;       /* IE/Edge 隱藏捲軸 */
}

.numpad button {
  width: 70px;
  height: 70px;
  font-size: 1.7rem;
  font-weight: 600;
  border: none;
  background: white;
  border-radius: 14px;
  color: var(--primary);
  box-shadow: 0 4px 10px rgba(0,0,0,0.08);
  cursor: pointer;
}

.numpad button:active {
  transform: scale(0.94);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: white;
  padding: 40px 50px;
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}

.modal-content h2 {
  color: var(--primary);
  margin-bottom: 20px;
}

#play-again {
  margin-top: 20px;
  padding: 14px 40px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 50px;
  font-size: 1.1rem;
  font-weight: bold;
  cursor: pointer;
}

@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

@media (max-width: 780px) {
  .main {
    flex-direction: column;
  }
  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    min-width: auto;
    padding-top: 0;
    gap: 10px;
  }
  .sidebar button {
    flex: 1 1 45%;
    min-width: 120px;
  }
  .grid {
    max-width: 420px;
  }
  .numpad button {
    width: 60px;
    height: 60px;
    font-size: 1.5rem;
  }
}