/* 布局与组件（主题无关，全部颜色/字体走变量，见 themes.css） */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--c-bg);
  color: var(--c-fg);
  font-family: var(--font-ui);
  transition: background 0.4s ease, color 0.4s ease;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
}

.brand {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  height: 60px;
  width: auto;
  max-width: min(384px, 46vw);
  object-fit: contain;
  display: block;
}

.brand-sub {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--c-accent);
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

#theme-bar {
  display: flex;
  gap: 6px;
}

.theme-chip {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.theme-chip[data-theme='neon'] {
  background: linear-gradient(135deg, #0e1230, #e94f75 60%, #35e0ff);
}
.theme-chip[data-theme='aurora'] {
  background: linear-gradient(135deg, #16123a, #6d5df6 50%, #38e0c8);
}
.theme-chip[data-theme='washi'] {
  background: linear-gradient(135deg, #f5eee1, #c41e3a 60%, #ffb84d);
}
.theme-chip[data-theme='mono'] {
  background: linear-gradient(135deg, #fff, #111);
}

.theme-chip:hover {
  transform: scale(1.15);
}
.theme-chip.active {
  border-color: var(--c-accent);
  box-shadow: 0 0 8px var(--c-accent);
}

.icon-btn {
  font-size: 1.1rem;
  padding: 4px 10px;
  border-radius: var(--radius, 10px);
  background: var(--c-surface);
  transition: background 0.2s;
}

.icon-btn:hover {
  background: var(--c-surface2, var(--c-surface));
}

.icon-btn.hidden {
  display: none;
}

/* ---------- 主布局 ---------- */
#layout {
  display: flex;
  gap: clamp(16px, 4vw, 48px);
  align-items: flex-start;
  justify-content: center;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  max-width: 1100px;
  margin: 0 auto;
}

#board-wrap {
  flex: 0 0 auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: 1px;
  width: min(94vw, 62dvh, 540px);
  aspect-ratio: 1;
  background: var(--c-line-bold);
  border-radius: calc(var(--radius, 10px) + 4px);
  overflow: hidden;
  box-shadow: var(--board-shadow, none);
}

.cell {
  position: relative;
  background: var(--c-cell);
  padding: 0;
  display: grid;
  place-items: center;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.15s ease;
}

.cell .val {
  font-family: var(--font-num);
  font-size: clamp(1.15rem, 5.2vmin, 1.9rem);
  font-weight: 600;
  line-height: 1;
}

.cell.given .val {
  color: var(--c-given);
  font-weight: 700;
}

.cell.user .val {
  color: var(--c-user);
}

.cell.wrong .val {
  color: var(--c-danger);
}

.cell.conflict::after {
  content: '';
  position: absolute;
  inset: 2px;
  border: 1.5px solid var(--c-danger);
  border-radius: 6px;
  pointer-events: none;
}

.cell .notes {
  position: absolute;
  inset: 8%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  font-family: var(--font-num);
  font-size: clamp(0.45rem, 1.6vmin, 0.62rem);
  color: var(--c-muted);
  pointer-events: none;
}

.cell.peer {
  background: var(--c-peer);
}

.cell.selected {
  background: var(--c-selected);
}

.cell.same {
  background: var(--c-same);
}

/* 3x3 宫分隔：第 3/6 列右缘、第 3/6 行下缘 */
.cell:nth-child(9n + 3),
.cell:nth-child(9n + 6) {
  box-shadow: inset -2px 0 0 var(--c-line-bold);
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  box-shadow: inset 0 -2px 0 var(--c-line-bold);
}

.cell:nth-child(9n + 3):nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(9n + 3):nth-child(n + 46):nth-child(-n + 54),
.cell:nth-child(9n + 6):nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(9n + 6):nth-child(n + 46):nth-child(-n + 54) {
  box-shadow: inset -2px 0 0 var(--c-line-bold), inset 0 -2px 0 var(--c-line-bold);
}

/* ---------- 侧栏 ---------- */
#side {
  flex: 1 1 300px;
  max-width: 420px;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-top: 4px;
}

.seg {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.diff-btn {
  flex: 1 1 auto;
  padding: 9px 14px;
  border-radius: var(--radius, 10px);
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  font-weight: 600;
  transition: all 0.2s ease;
}

.diff-btn:hover {
  border-color: var(--c-accent);
}

.diff-btn.active {
  background: var(--c-accent);
  border-color: var(--c-accent);
  color: var(--c-on-accent, #fff);
}

#status-row {
  display: flex;
  align-items: center;
  gap: 16px;
  font-size: 1.05rem;
  padding: 2px 4px;
}

#timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  letter-spacing: 0.05em;
}

#mistakes {
  color: var(--c-muted);
  font-size: 0.95rem;
}

#status-row .icon-btn {
  margin-left: auto;
}

.pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 8px;
}

.pad-num {
  position: relative;
  aspect-ratio: 0.78;
  border-radius: var(--radius, 10px);
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  display: grid;
  place-items: center;
  transition: all 0.15s ease;
}

.pad-num .d {
  font-family: var(--font-num);
  font-size: clamp(1.1rem, 3.2vmin, 1.5rem);
  font-weight: 700;
  color: var(--c-user);
}

.pad-num .count {
  position: absolute;
  right: 5px;
  bottom: 3px;
  font-size: 0.6rem;
  color: var(--c-muted);
}

.pad-num:hover {
  border-color: var(--c-accent);
  transform: translateY(-2px);
}

.pad-num.depleted {
  opacity: 0.35;
  pointer-events: none;
}

.pad-num.active-note {
  border-color: var(--c-accent2);
}

.action-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tool-btn {
  flex: 1 1 auto;
  padding: 10px 8px;
  border-radius: var(--radius, 10px);
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  font-size: 0.88rem;
  white-space: nowrap;
  transition: all 0.2s ease;
}

.tool-btn:hover {
  border-color: var(--c-accent);
}

.tool-btn.active {
  background: var(--c-accent2);
  border-color: var(--c-accent2);
  color: var(--c-on-accent, #fff);
}

.hint-text {
  color: var(--c-muted);
  font-size: 0.78rem;
  line-height: 1.7;
  margin: 4px 0 0;
}

/* ---------- 覆盖层 ---------- */
#overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg, rgba(0, 0, 0, 0.55));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}

#overlay[hidden] {
  display: none;
}

#overlay-card {
  width: min(92vw, 460px);
  max-height: 88dvh;
  overflow: auto;
  background: var(--c-card, var(--c-surface));
  border: 1px solid var(--c-line);
  border-radius: calc(var(--radius, 10px) + 8px);
  padding: 26px 26px 22px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
  animation: card-in 0.25s ease;
}

#overlay-card h2 {
  margin: 0 0 16px;
  font-size: 1.35rem;
}

#overlay-card h3 {
  margin: 22px 0 10px;
  font-size: 0.95rem;
  color: var(--c-muted);
  font-weight: 600;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.level-btn {
  padding: 12px 4px;
  border-radius: var(--radius, 10px);
  border: 1px solid var(--c-line);
  background: var(--c-surface);
  font-weight: 700;
  transition: all 0.2s ease;
}

.level-btn.active {
  background: var(--c-accent);
  border-color: var(--c-accent);
  color: var(--c-on-accent, #fff);
}

.btn {
  display: block;
  width: 100%;
  padding: 13px;
  border-radius: var(--radius, 10px);
  border: 1px solid var(--c-line);
  background: var(--c-surface);
  font-weight: 600;
  margin-top: 10px;
  transition: all 0.2s ease;
}

.btn:hover {
  border-color: var(--c-accent);
}

.btn.primary {
  background: linear-gradient(135deg, var(--c-accent), var(--c-accent2));
  border: none;
  color: var(--c-on-accent, #fff);
  box-shadow: var(--btn-shadow, none);
}

.btn.subtle {
  background: none;
  color: var(--c-muted);
  font-size: 0.85rem;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.theme-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius, 10px);
  border: 1px solid var(--c-line);
  background: var(--c-surface);
  transition: all 0.2s ease;
}

.theme-btn.active {
  border-color: var(--c-accent);
  box-shadow: 0 0 0 1px var(--c-accent);
}

.swatch {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  flex: none;
}

.sw-neon {
  background: linear-gradient(135deg, #0e1230, #e94f75 60%, #35e0ff);
}
.sw-aurora {
  background: linear-gradient(135deg, #16123a, #6d5df6 50%, #38e0c8);
}
.sw-washi {
  background: linear-gradient(135deg, #f5eee1, #c41e3a 60%, #ffb84d);
}
.sw-mono {
  background: linear-gradient(135deg, #fff, #111);
}

.stats-block table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.stats-block th,
.stats-block td {
  padding: 7px 6px;
  text-align: left;
  border-bottom: 1px solid var(--c-line);
}

.stats-block th {
  color: var(--c-muted);
  font-weight: 600;
  font-size: 0.8rem;
}

.muted {
  color: var(--c-muted);
}

.pause-time {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 0 0 12px;
}

.win-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 8px;
}

.win-stats div {
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  border-radius: var(--radius, 10px);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.win-stats span {
  font-size: 0.75rem;
  color: var(--c-muted);
}

.win-stats b {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.confetti-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

/* ---------- 响应式 ---------- */
@media (max-width: 780px) {
  #layout {
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 4px 10px calc(12px + env(safe-area-inset-bottom));
  }

  .board {
    width: min(96vw, 46dvh);
  }

  #side {
    width: 100%;
    max-width: 540px;
    min-width: 0;
    gap: 10px;
  }

  .hint-text {
    display: none;
  }

  .brand-logo {
    height: 40px;
  }

  .brand-sub {
    font-size: 0.85rem;
  }

  .pad-num {
    aspect-ratio: 0.9;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .board {
    width: min(60vw, 82dvh);
  }

  #layout {
    gap: 20px;
  }
}
