:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #0b1d55;
  background: #f9f7ff;
  --brand: #b58cff;
  --brand-strong: #8b5cf6;
  --brand-deep: #7047e8;
  --brand-soft: #f0e9ff;
  --brand-soft-2: #e4d6ff;
  --navy: #0a1e58;
  --navy-soft: #33436e;
  --muted: #78819a;
  --line: #d8d9e6;
  --line-strong: #47577d;
  --surface: rgba(255,255,255,.92);
  --danger: #ef4b57;
  --green: #25b86a;
  --amber: #f3a70a;
  --red: #ef4936;
  --shadow: 0 12px 35px rgba(61, 42, 124, .12);
  --shadow-soft: 0 7px 22px rgba(61, 42, 124, .10);
}

* { box-sizing: border-box; }
html, body, #app { min-height: 100%; margin: 0; }
body {
  background:
    radial-gradient(circle at 82% 12%, rgba(181,140,255,.17), transparent 28%),
    radial-gradient(circle at 12% 68%, rgba(181,140,255,.10), transparent 28%),
    var(--tg-theme-bg-color, #f9f7ff);
  overscroll-behavior: none;
}
button { font: inherit; -webkit-tap-highlight-color: transparent; }
button, [role="button"] { touch-action: manipulation; }

.app-shell {
  width: min(100%, 760px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
  color: var(--navy);
  user-select: none;
}

.topbar {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.topbar-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 800;
  color: #111;
  letter-spacing: -.02em;
}
.icon-button {
  width: 44px;
  height: 44px;
  padding: 9px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--navy-soft);
}
.icon-button:active { transform: scale(.96); background: rgba(181,140,255,.13); }
.icon-button svg, .tool-icon svg, .mini-icon svg, .pause-icon svg, .difficulty-icon svg, .stat-icon svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* MENU */
.menu-screen { padding-bottom: 28px; }
.menu-hero {
  position: relative;
  margin: 4px auto 14px;
  min-height: 230px;
  display: grid;
  place-items: center;
}
.menu-hero::before,
.menu-hero::after {
  content: "✦";
  position: absolute;
  color: rgba(181,140,255,.54);
  font-size: 25px;
}
.menu-hero::before { right: 3%; top: 18%; }
.menu-hero::after { left: 4%; bottom: 8%; font-size: 17px; }
.brand-lockup {
  width: min(100%, 650px);
  display: grid;
  grid-template-columns: minmax(112px, 34%) 1fr;
  align-items: center;
  gap: 16px;
  padding: 4px 8px;
}
.brand-icon {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 26%;
  box-shadow: 0 16px 34px rgba(82,52,159,.16);
}
.brand-copy { min-width: 0; }
.brand-title {
  margin: 0;
  color: var(--navy);
  font-weight: 900;
  font-size: clamp(33px, 9vw, 66px);
  line-height: .92;
  letter-spacing: -.055em;
}
.brand-title span { color: var(--brand-strong); }
.brand-subtitle {
  margin: 12px 0 0;
  font-size: clamp(14px, 3.7vw, 20px);
  color: var(--navy-soft);
  font-weight: 650;
}
.brand-subtitle b { color: var(--brand-strong); }

.score-card {
  display: grid;
  grid-template-columns: 74px 1fr 48px;
  align-items: center;
  gap: 16px;
  min-height: 126px;
  padding: 19px 22px;
  border: 1px solid rgba(181,140,255,.23);
  border-radius: 29px;
  background: linear-gradient(135deg, rgba(255,255,255,.96), rgba(247,243,255,.92));
  box-shadow: var(--shadow);
}
.score-emblem, .score-star {
  display: grid;
  place-items: center;
  color: var(--brand-deep);
  background: var(--brand-soft);
  border: 2px solid rgba(181,140,255,.25);
}
.score-emblem { width: 70px; height: 70px; border-radius: 50%; padding: 17px; }
.score-star { width: 48px; height: 48px; border-radius: 15px; padding: 12px; }
.score-label { color: var(--muted); font-size: 17px; font-weight: 600; }
.score-value { font-size: clamp(38px, 10vw, 62px); font-weight: 900; line-height: 1; letter-spacing: -.035em; margin-top: 3px; }

.menu-section-title {
  margin: 24px 4px 13px;
  font-size: clamp(22px, 5.8vw, 30px);
  font-weight: 900;
  letter-spacing: -.025em;
}
.difficulty-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0,1fr));
  gap: 13px;
}
.difficulty-card {
  position: relative;
  min-height: 108px;
  padding: 16px;
  border: 1px solid rgba(20,33,79,.07);
  border-radius: 24px;
  background: rgba(255,255,255,.88);
  box-shadow: var(--shadow-soft);
  color: var(--navy);
  display: grid;
  grid-template-columns: 54px 1fr;
  gap: 13px;
  align-items: center;
  text-align: left;
}
.difficulty-card:active { transform: translateY(1px) scale(.99); }
.difficulty-card.selected {
  border: 2px solid var(--brand-strong);
  background: linear-gradient(135deg, rgba(255,255,255,.98), rgba(240,233,255,.93));
  box-shadow: 0 10px 30px rgba(126,78,239,.20);
}
.difficulty-check {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 27px;
  height: 27px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--brand-deep);
  color: #fff;
  font-weight: 900;
  font-size: 15px;
}
.difficulty-icon {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  padding: 14px;
  display: grid;
  place-items: center;
}
.difficulty-icon.easy { color: var(--green); background: rgba(37,184,106,.12); }
.difficulty-icon.medium { color: var(--amber); background: rgba(243,167,10,.13); }
.difficulty-icon.hard { color: var(--red); background: rgba(239,73,54,.12); }
.difficulty-icon.extreme { color: var(--brand-deep); background: rgba(181,140,255,.18); }
.difficulty-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.difficulty-name {
  display: block;
  max-width: 100%;
  font-size: 18px;
  font-weight: 900;
  line-height: 1.08;
  letter-spacing: -.025em;
}
.difficulty-desc {
  display: block;
  max-width: 100%;
  color: var(--muted);
  font-size: 14px;
  margin-top: 6px;
  line-height: 1.18;
}

.primary-action {
  width: 100%;
  min-height: 72px;
  margin-top: 18px;
  border: 0;
  border-radius: 23px;
  color: #fff;
  background: linear-gradient(110deg, var(--brand-strong), var(--brand));
  box-shadow: 0 13px 30px rgba(126,78,239,.28);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: clamp(21px, 5.8vw, 29px);
  font-weight: 900;
}
.primary-action:active { transform: translateY(1px) scale(.995); }
.primary-action .mini-icon { width: 28px; height: 28px; }

.secondary-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 13px; margin-top: 14px; }
.secondary-action {
  min-height: 70px;
  border: 1px solid rgba(20,33,79,.07);
  border-radius: 22px;
  background: rgba(255,255,255,.88);
  color: var(--navy);
  box-shadow: var(--shadow-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-weight: 800;
}
.secondary-action:disabled { opacity: .42; }
.secondary-action .mini-icon { width: 36px; height: 36px; padding: 8px; border-radius: 50%; background: var(--brand-soft); color: var(--brand-deep); }
.menu-tip { margin: 18px 6px 0; display: flex; justify-content: center; align-items: center; gap: 9px; color: var(--muted); font-size: 13px; text-align: center; }
.menu-tip .mini-icon { width: 30px; height: 30px; padding: 7px; border-radius: 50%; background: var(--brand-soft); color: var(--brand-deep); flex: 0 0 auto; }

/* GAME */
.game-screen { display: flex; flex-direction: column; min-height: calc(100dvh - 20px); }
.game-header { padding: 5px 0 10px; }
.game-score { display: block; text-align: center; color: var(--brand-deep); font-size: clamp(26px, 7vw, 38px); font-weight: 900; }
.game-meta {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-top: 15px;
  color: var(--muted);
  font-size: clamp(14px, 3.7vw, 19px);
}
.game-meta > span:first-child { justify-self: start; }
.difficulty-link { border: 0; background: transparent; color: var(--muted); padding: 5px 8px; }
.timer-button { justify-self: end; display: inline-flex; align-items: center; gap: 7px; border: 0; background: transparent; color: var(--muted); padding: 0; }
.pause-icon { width: 34px; height: 34px; color: var(--brand-strong); }

.board-wrap { width: 100%; }
.sudoku-board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  overflow: hidden;
  border: 3px solid var(--line-strong);
  background: rgba(255,255,255,.92);
  box-shadow: 0 6px 22px rgba(23,38,87,.07);
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.84);
  color: var(--navy);
}
.cell.related { background: rgba(181,140,255,.08); }
.cell.same-number { background: rgba(181,140,255,.17); }
.cell.selected { background: rgba(181,140,255,.34); box-shadow: inset 0 0 0 3px var(--brand-deep); z-index: 2; }
.cell.editable { color: var(--brand-deep); }
.cell.conflict { color: var(--danger); background: #ffecef; }
.cell.hinted { color: #9c6feb; }
.cell-value { font-size: clamp(22px, 6.2vw, 44px); font-weight: 650; line-height: 1; }
.notes-grid { position: absolute; inset: 3px; display: grid; grid-template-columns: repeat(3,1fr); grid-template-rows: repeat(3,1fr); }
.notes-grid span { display: grid; place-items: center; font-size: clamp(7px, 2vw, 13px); line-height: 1; color: var(--brand-deep); font-weight: 650; }
.pause-overlay { position: absolute; inset: 0; z-index: 5; border: 0; background: rgba(250,248,255,.96); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; color: var(--brand-deep); font-weight: 900; font-size: 18px; }
.pause-overlay .pause-icon { width: 72px; height: 72px; }

.tools { display: grid; grid-template-columns: repeat(5,1fr); gap: 7px; margin: 20px 0 12px; }
.tool-button {
  min-width: 0;
  min-height: 105px;
  padding: 10px 3px 8px;
  border: 1px solid rgba(20,33,79,.06);
  border-radius: 20px;
  background: rgba(255,255,255,.90);
  box-shadow: var(--shadow-soft);
  color: var(--navy-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-size: clamp(9px, 2.6vw, 13px);
  line-height: 1.08;
  text-align: center;
}
.tool-button:disabled { opacity: .36; }
.tool-button.active { color: var(--brand-deep); background: linear-gradient(180deg, #fff, var(--brand-soft)); }
.tool-icon { position: relative; width: clamp(36px, 9vw, 52px); height: clamp(36px, 9vw, 52px); color: var(--brand-deep); }
.badge-wrap b { position: absolute; top: -7px; right: -9px; min-width: 25px; height: 23px; padding: 0 6px; border-radius: 12px; display: grid; place-items: center; background: var(--brand-deep); color: #fff; font-size: 12px; font-weight: 900; }
.tool-button.pencil .badge-wrap b { background: #a1a6b4; }
.tool-button.pencil.active .badge-wrap b { background: var(--brand-deep); }

.number-pad { display: grid; grid-template-columns: repeat(9,1fr); gap: clamp(3px, 1vw, 8px); margin-top: auto; }
.number-pad button { min-width: 0; height: clamp(68px, 17vw, 100px); border: 1px solid rgba(20,33,79,.05); border-radius: 17px; background: rgba(255,255,255,.94); box-shadow: var(--shadow-soft); color: var(--brand-deep); display: flex; flex-direction: column; align-items: center; justify-content: center; }
.number-pad button:active { transform: scale(.98); background: var(--brand-soft); }
.number-pad button:disabled { opacity: .36; }
.number-pad strong { font-size: clamp(25px, 6.8vw, 40px); font-weight: 750; line-height: 1; }
.number-pad small { margin-top: 6px; color: #555f78; font-size: clamp(10px, 2.8vw, 16px); }
.brand-note { text-align: center; color: #8d95a9; font-size: 12px; padding: 16px 0 2px; }

/* DIALOGS / STATS */
.modal-backdrop { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 18px; background: rgba(17,22,49,.44); backdrop-filter: blur(7px); }
.modal-card { width: min(100%, 420px); max-height: 86dvh; overflow: auto; padding: 27px; border-radius: 28px; background: linear-gradient(145deg,#fff,#f7f2ff); text-align: center; box-shadow: 0 24px 80px rgba(20,18,61,.28); }
.modal-card h2 { margin: 0; font-size: 27px; color: var(--navy); }
.modal-card p { margin: 9px 0 20px; color: var(--muted); line-height: 1.45; }
.modal-emoji { font-size: 54px; margin-bottom: 8px; }
.modal-primary, .modal-secondary { width: 100%; min-height: 51px; border-radius: 16px; padding: 12px 16px; font-weight: 900; }
.modal-primary { border: 0; background: linear-gradient(110deg,var(--brand-strong),var(--brand)); color: #fff; }
.modal-secondary { margin-top: 9px; border: 0; background: transparent; color: var(--brand-deep); }
.stats-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 18px 0 8px; }
.stat-card { padding: 16px 10px; border-radius: 18px; background: rgba(255,255,255,.86); border: 1px solid rgba(181,140,255,.15); box-shadow: var(--shadow-soft); }
.stat-card strong { display: block; font-size: 27px; color: var(--brand-deep); }
.stat-card span { display: block; margin-top: 5px; color: var(--muted); font-size: 13px; }
.stats-by-difficulty { margin-top: 15px; text-align: left; }
.stats-row { display: grid; grid-template-columns: 1fr auto; padding: 10px 2px; border-bottom: 1px solid rgba(20,33,79,.08); color: var(--navy-soft); }
.stats-row b { color: var(--navy); }

@media (max-width: 470px) {
  .app-shell { padding-left: 10px; padding-right: 10px; }
  .menu-hero { min-height: 195px; }
  .brand-lockup { gap: 10px; padding-inline: 2px; }
  .score-card { grid-template-columns: 58px 1fr 42px; gap: 10px; padding: 16px; border-radius: 24px; }
  .score-emblem { width: 58px; height: 58px; padding: 14px; }
  .score-star { width: 42px; height: 42px; padding: 10px; }
  .difficulty-card {
    min-height: 96px;
    padding: 12px 9px;
    grid-template-columns: 42px minmax(0, 1fr);
    gap: 8px;
    border-radius: 20px;
    overflow: hidden;
  }
  .difficulty-icon { width: 42px; height: 42px; padding: 10px; }
  .difficulty-name { font-size: clamp(13px, 3.7vw, 16px); line-height: 1.05; }
  .difficulty-desc { font-size: clamp(10.5px, 3vw, 12px); margin-top: 5px; line-height: 1.15; }
  .difficulty-card[data-difficulty="extreme"] .difficulty-name {
    font-size: clamp(12.5px, 3.45vw, 15px);
    letter-spacing: -.045em;
  }
  .difficulty-check { top: 8px; right: 8px; width: 25px; height: 25px; font-size: 14px; }
  .tool-button { min-height: 92px; border-radius: 17px; }
}

@media (max-width: 390px) {
  .difficulty-cards { gap: 10px; }
  .difficulty-card { padding: 11px 8px; grid-template-columns: 39px minmax(0, 1fr); gap: 7px; }
  .difficulty-icon { width: 39px; height: 39px; padding: 9px; }
  .difficulty-name { font-size: 13.5px; }
  .difficulty-desc { font-size: 10.5px; }
  .difficulty-card[data-difficulty="extreme"] .difficulty-name { font-size: 12.5px; }
}

@media (max-height: 760px) {
  .topbar { min-height: 42px; }
  .game-header { padding-top: 0; }
  .game-meta { margin-top: 8px; }
  .tools { margin: 10px 0 7px; }
  .tool-button { min-height: 76px; }
  .number-pad button { height: 59px; }
  .brand-note { display: none; }
}

@media (min-width: 700px) {
  .app-shell { padding-left: 24px; padding-right: 24px; }
}

/* GAME MODE SELECTOR */
.mode-title { margin-bottom: 10px; }
.mode-switch {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 6px;
  border: 1px solid rgba(181,140,255,.2);
  border-radius: 24px;
  background: rgba(255,255,255,.56);
  box-shadow: var(--shadow-soft);
}
.mode-card {
  min-width: 0;
  min-height: 84px;
  padding: 10px 7px;
  border: 1px solid transparent;
  border-radius: 19px;
  background: transparent;
  color: var(--navy-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
}
.mode-card:active { transform: scale(.985); }
.mode-card.selected {
  color: var(--brand-deep);
  border-color: rgba(181,140,255,.55);
  background: linear-gradient(145deg, #fff, var(--brand-soft));
  box-shadow: 0 8px 20px rgba(112,71,232,.16);
}
.mode-icon { width: 26px; height: 26px; margin-bottom: 2px; }
.mode-icon svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.mode-name { max-width: 100%; font-size: 15px; font-weight: 900; line-height: 1; white-space: nowrap; }
.mode-desc { max-width: 100%; color: var(--muted); font-size: 10px; line-height: 1.05; white-space: nowrap; }
.mode-card.selected .mode-desc { color: #8065bd; }

/* DYNAMIC BOARD SIZES */
.sudoku-board.size-9 { grid-template-columns: repeat(9, 1fr); }
.sudoku-board.size-16 {
  grid-template-columns: repeat(16, 1fr);
  border-width: 2px;
}
.cell.edge-right { border-right: 0; }
.cell.edge-bottom { border-bottom: 0; }
.cell.box-right { border-right: 3px solid var(--line-strong); }
.cell.box-bottom { border-bottom: 3px solid var(--line-strong); }
.sudoku-board.size-16 .cell.box-right { border-right-width: 2px; }
.sudoku-board.size-16 .cell.box-bottom { border-bottom-width: 2px; }
.sudoku-board.size-16 .cell-value {
  font-size: clamp(11px, 3.15vw, 23px);
  font-weight: 700;
  letter-spacing: -.03em;
}
.sudoku-board.size-16 .cell.selected { box-shadow: inset 0 0 0 2px var(--brand-deep); }
.sudoku-board.size-16 .notes-grid {
  inset: 1px;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
}
.sudoku-board.size-16 .notes-grid span { font-size: clamp(3.5px, 1vw, 7px); }

/* KILLER CAGES */
.cell.killer-cell::after {
  content: "";
  position: absolute;
  inset: 2px;
  z-index: 1;
  pointer-events: none;
  border-color: rgba(112,71,232,.82);
}
.cell.killer-cell.cage-top::after { border-top: 1.5px dashed rgba(112,71,232,.86); }
.cell.killer-cell.cage-right::after { border-right: 1.5px dashed rgba(112,71,232,.86); }
.cell.killer-cell.cage-bottom::after { border-bottom: 1.5px dashed rgba(112,71,232,.86); }
.cell.killer-cell.cage-left::after { border-left: 1.5px dashed rgba(112,71,232,.86); }
.cell.same-cage:not(.selected) { background: rgba(181,140,255,.145); }
.cage-sum {
  position: absolute;
  top: 2px;
  left: 4px;
  z-index: 3;
  min-width: 0;
  max-width: calc(100% - 6px);
  overflow: hidden;
  color: #6c56a3;
  font-size: clamp(6px, 1.65vw, 11px);
  line-height: 1;
  font-weight: 850;
  white-space: nowrap;
  pointer-events: none;
}
.killer-cell .cell-value {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.killer-cell .notes-grid {
  z-index: 2;
  inset: 9px 3px 3px;
}
.killer-cell.selected .cell-value { transform: translateY(.5px); }

/* 16-SYMBOL KEYPAD */
.number-pad.size-16 {
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: clamp(4px, 1.2vw, 9px);
}
.number-pad.size-16 button {
  height: clamp(56px, 14.5vw, 82px);
  border-radius: 16px;
}
.number-pad.size-16 strong { font-size: clamp(23px, 6vw, 37px); }
.board-helper {
  margin-top: 12px;
  text-align: center;
  color: var(--muted);
  font-size: 11px;
}

.stats-mode { margin-top: 17px; }
.stats-mode h3 { margin: 0 0 3px; color: var(--brand-deep); font-size: 17px; }

@media (max-width: 470px) {
  .mode-switch { gap: 5px; padding: 5px; border-radius: 20px; }
  .mode-card { min-height: 72px; padding: 8px 3px; border-radius: 16px; }
  .mode-icon { width: 22px; height: 22px; }
  .mode-name { font-size: clamp(12px, 3.6vw, 15px); }
  .mode-desc { font-size: 8.5px; }
  .game-meta { grid-template-columns: minmax(0,1fr) auto minmax(0,1fr); font-size: clamp(11px, 3.2vw, 15px); }
  .difficulty-link { max-width: 170px; padding-inline: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .sudoku-board.size-16 { border-width: 2px; }
  .mode-hex16 .tools { margin-top: 12px; }
  .mode-hex16 .tool-button { min-height: 82px; padding-inline: 1px; }
  .mode-hex16 .tool-icon { width: 33px; height: 33px; }
}

@media (max-width: 370px) {
  .mode-desc { display: none; }
  .mode-card { min-height: 58px; }
  .mode-icon { width: 19px; height: 19px; }
  .mode-name { font-size: 12px; }
  .game-meta { font-size: 10.5px; }
  .difficulty-link { max-width: 145px; }
}
