:root {
  --primary-color: #2c5282;
  --primary-light: #ebf4ff;
  --error-color: #e53e3e;
  --bg-color: #f0f2f7;
  --cell-bg: #ffffff;
  --border-color: #90a4c9;
  --text-color: #1a365d;
  --button-hover: #2b6cb0;
  --input-border: #90a4c9;
  --shadow-color: rgba(44, 82, 130, 0.08);
  --grid-border: #90a4c9;
  --container-bg: #ffffff;
  --input-color: #c41e3a;
  --highlight-color: #ebf4ff;
  --card-bg: #ffffff;
  --card-shadow: rgba(17, 12, 46, 0.08);
  --card-hover-shadow: rgba(17, 12, 46, 0.12);
  --note-border: #e6e8f0;
  --note-bg: #f8f9fc;
  --note-text: #64748b;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  min-height: 100vh;
  padding: 10px;
  display: flex;
  justify-content: center;
  overflow-x: hidden;
}

.container {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 15px;
  background: var(--container-bg);
  border-radius: 8px;
  box-shadow: 0 4px 6px var(--shadow-color);
  padding: 15px;
}

h1 {
  text-align: center;
  font-size: 24px;
  color: var(--text-color);
  margin-bottom: 10px;
}

.top-controls, .bottom-controls {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 15px;
}

.difficulty-buttons {
  margin: 20px 0;
  text-align: center;
}

.difficulty-buttons button {
  padding: 8px 20px;
  margin: 0 10px;
  font-size: 16px;
  border: 2px solid #4CAF50;
  border-radius: 5px;
  background-color: white;
  color: #4CAF50;
  cursor: pointer;
  transition: all 0.3s ease;
}

.difficulty-buttons button:hover {
  background-color: #4CAF50;
  color: white;
}

.difficulty-buttons button.active {
  background-color: #4CAF50;
  color: white;
}

#d {
  width: min(100%, 90vmin);
  padding-bottom: min(100%, 90vmin);
  position: relative;
  margin: 0 auto 15px;
  background: var(--cell-bg);
  border-radius: 4px;
  box-sizing: content-box;
}

#d > ul {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  list-style: none;
  border: 2px solid var(--primary-color);
  background: var(--cell-bg);
  touch-action: manipulation;
  -webkit-touch-callout: none;
  user-select: none;
}

#d > ul > li {
  position: relative;
  border-right: 1px solid var(--grid-border);
  border-bottom: 1px solid var(--grid-border);
  background: var(--cell-bg);
}

/* 3x3格子的边框 */
#d > ul > li:nth-child(3n) {
  border-right: 2px solid var(--primary-color);
}

#d > ul > li:nth-child(9n) {
  border-right: none;
}

/* 为最后一列添加右边框 */
#d > ul > li:nth-child(9n) {
  border-right: 2px solid var(--primary-color);
}

/* 加粗3x3格子的边框 */
#d > ul > li:nth-child(n+19):nth-child(-n+27),
#d > ul > li:nth-child(n+46):nth-child(-n+54) {
  border-bottom: 2px solid var(--primary-color);
}

/* 为第一行添加上边框 */
#d > ul > li:nth-child(-n+9) {
  border-top: 2px solid var(--primary-color);
}

/* 为最后一行添加下边框 */
#d > ul > li:nth-child(n+73):nth-child(-n+81) {
  border-bottom: 2px solid var(--primary-color);
}

/* 为第一列添加左边框 */
#d > ul > li:nth-child(9n-8) {
  border-left: 2px solid var(--primary-color);
}

#d > ul > li > input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  font-size: 18px;
  background: transparent;
  color: var(--input-color);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
  user-select: none;
  outline: none;
}

#d > ul > li > input:focus {
  background: var(--primary-light);
}

input.solution-shown {
  background-color: var(--highlight-color);
  color: var(--primary-color);
  font-weight: bold;
  cursor: not-allowed;
  opacity: 0.7;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

#d > ul > li > span {
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  font-size: 18px;
  padding: 0;
  margin: 0;
  background: transparent;
  color: var(--text-color);
  font-weight: 600;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media screen and (max-width: 500px) {
  #d > ul > li > input,
  #d > ul > li > span {
    font-size: 16px;
  }
}

@media screen and (max-width: 360px) {
  #d > ul > li > input,
  #d > ul > li > span {
    font-size: 14px;
  }
}

button, select {
  padding: 8px 16px;
  font-size: 14px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--cell-bg);
  color: var(--text-color);
  cursor: pointer;
  min-width: 80px;
  touch-action: manipulation;
  transition: all 0.2s ease;
}

button {
  background: var(--primary-color);
  color: white;
  border: none;
}

button:hover {
  background: var(--button-hover);
}

select:hover {
  border-color: var(--primary-color);
}

#difficulty {
  padding: 8px 15px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background-color: white;
  cursor: pointer;
  font-weight: bold;
}

#difficulty option {
  font-weight: bold;
}

#newGameBtn {
  padding: 8px 15px;
  font-size: 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

#newGameBtn:hover {
  background-color: #45a049;
}

#timer {
  display: inline-block;
  padding: 8px 15px;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-color);
  background-color: var(--cell-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  margin-left: 10px;
  font-family: "SF Mono", "Consolas", monospace;
}

@media screen and (max-width: 480px) {
  #timer {
    font-size: 16px;
    padding: 6px 12px;
    margin-left: 8px;
  }
  
  .top-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    align-items: center;
  }

  #difficulty, #newGameBtn {
    flex: 0 1 auto;
  }
}

.nav-links {
  text-align: center;
  margin-bottom: 2px;
}

.nav-links a {
  color: var(--text-color);
  text-decoration: none;
  padding: 0 10px;
  font-size: 14px;
}

.nav-links a:hover {
  color: var(--primary-color);
}

.copyright {
  margin-top: 2px;
  text-align: center;
  font-size: 14px;
  color: var(--text-color);
  clear: both;
  padding: 10px;
}

.copyright a {
  color: var(--text-color);
  text-decoration: none;
}

.copyright a:hover {
  color: var(--primary-color);
}

.hint {
  animation: hint-pulse 1s ease-in-out;
}

.shake {
  animation: shake 0.5s ease-in-out;
  color: var(--error-color) !important;
}

@keyframes hint-pulse {
  0% {
    background-color: var(--primary-light);
    transform: scale(1);
  }
  50% {
    background-color: var(--primary-color);
    color: white;
    transform: scale(1.1);
  }
  100% {
    background-color: var(--primary-light);
    transform: scale(1);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.highlighted {
  background-color: var(--highlight-color);
}

@media screen and (max-width: 500px) {
  body {
    padding: 5px;
  }
  
  .container {
    gap: 10px;
    padding: 10px;
  }
  
  h1 {
    font-size: 20px;
    margin-bottom: 8px;
  }

  .top-controls {
    margin-bottom: 10px;
  }

  #d {
    width: min(100%, 95vmin);
    padding-bottom: min(100%, 95vmin);
    margin-bottom: 10px;
  }
  
  button, select {
    padding: 6px 12px;
    font-size: 12px;
    min-width: 60px;
  }

  .bottom-controls {
    margin-top: 10px;
  }

  .copyright {
    margin-top: 10px;
    padding: 5px;
  }
}

@media screen and (max-width: 360px) {
  body {
    padding: 3px;
  }

  button, select {
    padding: 4px 8px;
    font-size: 12px;
    min-width: 50px;
  }
}

/* 移除number input的上下箭头 */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

input {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border-radius: 0;
}

/* 移动端音频控制器样式 */
.audio-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  transition: opacity 0.3s ease;
  justify-content: center;
}

.audio-controls.desktop-only {
  width: 100%;
  max-width: 320px;
  margin: 10px auto;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(44, 82, 130, 0.12),
              0 2px 4px rgba(44, 82, 130, 0.08);
  justify-content: space-evenly;
  border: 1px solid rgba(44, 82, 130, 0.15);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

@media (max-width: 1200px) {
  .audio-controls.desktop-only {
    left: auto;
    right: 20px;
    bottom: 100px;
  }
}

@media (max-width: 768px) {
  .desktop-only {
    display: none !important;
  }
  
  .mobile-only {
    display: flex !important;
  }

  .audio-controls.mobile-only {
    width: 100%;
    max-width: 320px;
    margin: 10px auto;
    padding: 14px 20px;
    background: rgba(255, 255, 255, 0.98);
    border-radius: 16px;
    box-shadow: 0 4px 16px rgba(44, 82, 130, 0.12),
                0 2px 4px rgba(44, 82, 130, 0.08);
    justify-content: space-evenly;
    border: 1px solid rgba(44, 82, 130, 0.15);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  .mobile-only .icon-button {
    width: 44px;
    height: 44px;
    background: #2C5282;
    color: white;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 6px rgba(44, 82, 130, 0.2),
                0 2px 4px rgba(44, 82, 130, 0.1),
                inset 0 2px 4px rgba(255, 255, 255, 0.1);
    position: relative;
    overflow: hidden;
  }

  .mobile-only .icon-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 8px rgba(44, 82, 130, 0.25),
                0 3px 6px rgba(44, 82, 130, 0.15),
                inset 0 2px 4px rgba(255, 255, 255, 0.15);
  }

  .mobile-only .icon-button:active {
    transform: scale(0.95) translateY(1px);
    background: #234067;
    box-shadow: 0 2px 4px rgba(44, 82, 130, 0.15),
                inset 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .mobile-only .icon-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      120deg,
      transparent,
      rgba(255, 255, 255, 0.2),
      transparent
    );
    transition: 0.5s;
  }

  .mobile-only .icon-button:hover::before {
    left: 100%;
  }

  .mobile-only .music-select {
    width: 110px;
    height: 44px;
    font-size: 14px;
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid rgba(44, 82, 130, 0.2);
    background: #f8fafc;
    color: #2C5282;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(44, 82, 130, 0.1);
    transition: all 0.3s ease;
  }

  .mobile-only .music-select:focus {
    border-color: #2C5282;
    box-shadow: 0 0 0 3px rgba(44, 82, 130, 0.1);
  }

  .mobile-only #volumeControlMobile {
    width: 85px;
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(to right, #2C5282, rgba(44, 82, 130, 0.7));
    -webkit-appearance: none;
    appearance: none;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .mobile-only #volumeControlMobile::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 8px;
    background: #ffffff;
    border: 2px solid #2C5282;
    box-shadow: 0 2px 4px rgba(44, 82, 130, 0.2);
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .mobile-only #volumeControlMobile::-webkit-slider-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 6px rgba(44, 82, 130, 0.25);
  }

  .mobile-only #volumeControlMobile::-webkit-slider-thumb:active {
    transform: scale(0.95);
    background: #f8fafc;
  }
}

@media (prefers-color-scheme: dark) {
  .audio-controls.mobile-only {
    background: rgba(26, 32, 44, 0.95);
    border-color: rgba(44, 82, 130, 0.3);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2),
                0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .mobile-only .icon-button {
    background: #2C5282;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2),
                0 2px 4px rgba(0, 0, 0, 0.1),
                inset 0 2px 4px rgba(255, 255, 255, 0.05);
  }

  .mobile-only .icon-button:hover {
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.25),
                0 3px 6px rgba(0, 0, 0, 0.15),
                inset 0 2px 4px rgba(255, 255, 255, 0.1);
  }

  .mobile-only .icon-button:active {
    background: #234067;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15),
                inset 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  .mobile-only .music-select {
    background: #2D3748;
    color: #E2E8F0;
    border-color: rgba(44, 82, 130, 0.3);
  }

  .mobile-only #volumeControlMobile {
    background: linear-gradient(to right, #2C5282, rgba(44, 82, 130, 0.5));
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  .mobile-only #volumeControlMobile::-webkit-slider-thumb {
    background: #2D3748;
    border-color: #2C5282;
  }
}

/* 触摸设备优化 */
@media (hover: hover) {
  .icon-button:hover {
    transform: translateY(-1px);
  }
}

/* 移动端音频控制器样式 */
.icon-button {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: var(--primary-color);
  color: white;
  cursor: pointer;
  transition: background-color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 16px;
}

.icon-button:hover {
  background: var(--button-hover);
}

.icon-button.playing {
  background: var(--button-hover);
}

.music-select {
  padding: 4px 8px;
  border-radius: 6px;
  border: 1px solid var(--note-border);
  background: white;
  color: var(--text-color);
  font-size: 13px;
  width: 90px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'%3E%3Cpath fill='%23666' d='M0 2l4 4 4-4z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
  cursor: pointer;
}

#volumeControlMobile,
#volumeControlDesktop {
  width: 80px;
  height: 6px;
  -webkit-appearance: none;
  background: #e5e7eb;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
  position: relative;
  margin: 0 8px;
}

#volumeControlMobile::-webkit-slider-thumb,
#volumeControlDesktop::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #2563eb;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.3);
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  z-index: 2;
}

#volumeControlMobile::-moz-range-thumb,
#volumeControlDesktop::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #2563eb;
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.3);
  cursor: pointer;
  transition: all 0.2s ease;
}

#volumeControlMobile::-webkit-slider-runnable-track,
#volumeControlDesktop::-webkit-slider-runnable-track {
  background: linear-gradient(to right, #2563eb var(--value, 0%), #e5e7eb var(--value, 0%));
  border-radius: 3px;
  height: 6px;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}

#volumeControlMobile::-moz-range-track,
#volumeControlDesktop::-moz-range-track {
  background: linear-gradient(to right, #2563eb var(--value, 0%), #e5e7eb var(--value, 0%));
  border-radius: 3px;
  height: 6px;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}

#volumeControlMobile::-webkit-slider-thumb:hover,
#volumeControlDesktop::-webkit-slider-thumb:hover {
  background: #1d4ed8;
  transform: scale(1.1);
  box-shadow: 0 3px 6px rgba(37, 99, 235, 0.4);
}

#volumeControlMobile::-moz-range-thumb:hover,
#volumeControlDesktop::-moz-range-thumb:hover {
  background: #1d4ed8;
  transform: scale(1.1);
  box-shadow: 0 3px 6px rgba(37, 99, 235, 0.4);
}

#volumeControlMobile:active::-webkit-slider-thumb,
#volumeControlDesktop:active::-webkit-slider-thumb {
  transform: scale(1.2);
  box-shadow: 0 4px 8px rgba(37, 99, 235, 0.5);
}

#volumeControlMobile:active::-moz-range-thumb,
#volumeControlDesktop:active::-moz-range-thumb {
  transform: scale(1.2);
  box-shadow: 0 4px 8px rgba(37, 99, 235, 0.5);
}

/* 添加焦点样式 */
#volumeControlMobile:focus,
#volumeControlDesktop:focus {
  outline: none;
}

#volumeControlMobile:focus::-webkit-slider-runnable-track,
#volumeControlDesktop:focus::-webkit-slider-runnable-track {
  background: linear-gradient(to right, #2563eb var(--value, 0%), #e5e7eb var(--value, 0%));
}

#volumeControlMobile:focus::-moz-range-track,
#volumeControlDesktop:focus::-moz-range-track {
  background: linear-gradient(to right, #1d4ed8 var(--value, 0%), #e5e7eb var(--value, 0%));
}

/* 响应式设计 */
.mobile-only {
  display: none !important;
}

@media (max-width: 1200px) {
  .audio-controls.desktop-only {
    right: 20px;
  }
}

@media (max-width: 768px) {
  .desktop-only {
    display: none !important;
  }
  
  .mobile-only {
    display: flex !important;
  }

  .audio-controls.mobile-only {
    width: 100%;
    margin: 8px auto 0;
    padding: 0;
    background: none;
    box-shadow: none;
  }

  .icon-button {
    width: 22px;
    height: 22px;
    font-size: 12px;
  }

  .music-select {
    width: 70px;
    font-size: 11px;
    padding: 2px 6px;
  }

  #volumeControlMobile,
  #volumeControlDesktop {
    width: 50px;
  }
}

@media (max-width: 480px) {
  .nav-links {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    padding: 8px 0;
  }

  .audio-controls.mobile-only {
    margin-top: 6px;
  }

  .music-select {
    width: 65px;
  }

  #volumeControlMobile,
  #volumeControlDesktop {
    width: 45px;
  }
}
