* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent: #6366f1;
  --accent-dark: #4338ca;
  --ink: #312e5e;
  --line: #c7cdf0;
  --line-bold: #6d74b8;
}

html {
  height: 100%;
}

html, body {
  font-family: "PingFang SC", "Microsoft YaHei", -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, #eef2ff 0%, #e0e7ff 45%, #ede9fe 100%);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* min-height fills the screen so content centers when it fits, but the body
     can grow taller and scroll when it doesn't (fixes top/bottom clipping on
     small mobile viewports). dvh accounts for mobile browser chrome. */
  min-height: 100vh;
  min-height: 100dvh;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

/* Center vertically when there's room; top-align (auto margins collapse to 0)
   and let the page scroll when the content is taller than the viewport. */
.screen {
  margin: auto 0;
}

/* Language picker — top-right corner, matches the playful button style.
   #langToggle is the pill container; each [data-lang] button jumps straight
   to that language, the active one filled with the accent color. */
#langToggle {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 60;
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 3px solid var(--accent);
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 4px 0 rgba(67, 56, 202, 0.25);
}

#langToggle button {
  font-family: inherit;
  font-size: clamp(11px, 2.6vw, 13px);
  font-weight: 800;
  padding: 5px 8px;
  border: none;
  border-radius: 11px;
  background: transparent;
  color: var(--accent-dark);
  cursor: pointer;
  white-space: nowrap;
}

#langToggle button.active { background: var(--accent); color: #fff; }
#langToggle button:active { transform: translateY(1px); }

/* Confetti overlay */
#fx {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}

.screen {
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.hidden { display: none !important; }

/* ---- Menu ---- */
h1 {
  font-size: clamp(32px, 8vw, 52px);
  text-shadow: 2px 2px 0 #fff, 4px 4px 0 rgba(67, 56, 202, 0.25);
}

.subtitle {
  font-size: clamp(16px, 4vw, 22px);
  color: var(--accent-dark);
  font-weight: 600;
}

.card {
  width: 100%;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 20px;
  padding: 16px;
  box-shadow: 0 6px 0 rgba(67, 56, 202, 0.15);
  text-align: center;
}

.card h2 {
  font-size: clamp(16px, 4.5vw, 20px);
  margin-bottom: 12px;
}

.segmented {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.segmented button {
  font-size: clamp(14px, 3.8vw, 17px);
  font-weight: 700;
  font-family: inherit;
  color: var(--ink);
  padding: 10px 14px;
  border: 3px solid transparent;
  border-radius: 14px;
  background: #fff;
  cursor: pointer;
}

.segmented button.selected {
  border-color: var(--accent);
  color: var(--accent-dark);
  background: #eef2ff;
}

.big-btn {
  font-size: clamp(18px, 5vw, 24px);
  font-weight: 800;
  font-family: inherit;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 18px;
  padding: 14px 42px;
  cursor: pointer;
  box-shadow: 0 6px 0 var(--accent-dark);
}

.big-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 var(--accent-dark); }

.big-btn.secondary {
  background: #fff;
  color: var(--accent-dark);
  border: 3px solid var(--accent);
  box-shadow: 0 6px 0 rgba(67, 56, 202, 0.25);
}

.big-btn:disabled { opacity: 0.6; cursor: default; }

/* ---- Game HUD ---- */
.hud {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.hud-item {
  font-size: clamp(14px, 3.8vw, 18px);
  font-weight: 800;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  padding: 6px 14px;
  box-shadow: 0 3px 0 rgba(67, 56, 202, 0.12);
  min-width: 72px;
  text-align: center;
}

/* ---- Board ---- */
#board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: min(100%, 440px);
  aspect-ratio: 1;
  background: #fff;
  border: 3px solid var(--line-bold);
  border-radius: 12px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell {
  position: relative;
  border: 0 solid var(--line);
  border-right-width: 1px;
  border-bottom-width: 1px;
  background: #fff;
  font-family: inherit;
  font-size: clamp(18px, 4.6vw, 26px);
  font-weight: 700;
  color: var(--accent-dark);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* thick 3×3 box separators */
.cell.bx { border-right-width: 2px; border-right-color: var(--line-bold); }
.cell.by { border-bottom-width: 2px; border-bottom-color: var(--line-bold); }
.cell:nth-child(9n) { border-right-width: 0; }
.cell:nth-child(n+73) { border-bottom-width: 0; }

.cell.given { color: var(--ink); background: #f4f6ff; }
.cell.peer { background: #e8ecfd; }
.cell.given.peer { background: #e2e7fb; }
.cell.same { background: #cdd6fa; }
.cell.sel { background: #b3c0f7; box-shadow: inset 0 0 0 2px var(--accent); }
.cell.wrong { color: #dc2626; background: #fee2e2; }
.cell.wrong.sel { box-shadow: inset 0 0 0 2px #dc2626; }
.cell.flash { animation: cellPop 0.18s ease; }

@keyframes cellPop {
  from { transform: scale(0.7); }
  to { transform: scale(1); }
}

/* pencil notes: a mini 3×3 grid inside the cell */
.cell .notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
}

.cell .notes span {
  font-size: clamp(8px, 2vw, 11px);
  font-weight: 600;
  color: #7c86c9;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- Number pad ---- */
#pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 5px;
  width: min(100%, 440px);
}

#pad button {
  font-family: inherit;
  font-size: clamp(17px, 4.6vw, 24px);
  font-weight: 800;
  color: var(--accent-dark);
  background: #fff;
  border: none;
  border-radius: 10px;
  padding: 6px 0 3px;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(67, 56, 202, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
}

#pad button:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(67, 56, 202, 0.2); }

#pad button .left {
  font-size: clamp(9px, 2.4vw, 12px);
  font-weight: 700;
  color: #8a93cf;
}

#pad button.exhausted { opacity: 0.35; }

#pad button.note-mode { background: #fef9c3; }

/* ---- Controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.ctrl-btn {
  font-size: clamp(13px, 3.4vw, 16px);
  font-weight: 700;
  font-family: inherit;
  color: var(--ink);
  padding: 9px 12px;
  border: none;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(67, 56, 202, 0.18);
}

.ctrl-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(67, 56, 202, 0.18); }

.ctrl-btn.active {
  background: #fef08a;
  box-shadow: 0 3px 0 rgba(202, 138, 4, 0.35);
}

/* ---- Win overlay ---- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 55;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(49, 46, 94, 0.45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  padding: 20px;
}

.overlay-card {
  background: #fff;
  border-radius: 22px;
  padding: 28px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  animation: cardPop 0.25s ease;
}

@keyframes cardPop {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.overlay-card h2 { font-size: clamp(22px, 6vw, 30px); }

.overlay-card p {
  font-size: clamp(15px, 4vw, 18px);
  font-weight: 600;
  color: var(--accent-dark);
}
