* {
  box-sizing: border-box;
}

:root {
  --ink: #111827;
  --paper: #fffaf0;
  --given: #d8d0c0;
  --page: #f4f1e9;
  --panel: #ffffff;
  --muted: #64748b;
  --accent: #2563eb;
  --danger: #fecaca;
  --success: #bbf7d0;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html,
body {
  height: auto;
  min-height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(255,255,255,0.95), transparent 38rem),
    linear-gradient(135deg, #f8f5ee, #e9eef7);
  color: var(--ink);
}

body.auth-modal-open {
  overflow: hidden;
}

button,
.cell {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font: inherit;
}

.app {
  position: relative;
  width: min(100%, 760px);
  margin: 0 auto;
  padding: clamp(10px, 3vw, 20px);
  padding-bottom: calc(80px + var(--safe-bottom));
}

/* Header */

.hero {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: end;
  margin-bottom: 12px;
  padding-top: 44px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: clamp(0.64rem, 2.4vw, 0.76rem);
}

h1 {
  margin: 0;
  font-size: clamp(2.35rem, 12vw, 5rem);
  line-height: 0.95;
  letter-spacing: -0.075em;
}

.subtitle {
  margin: 7px 0 0;
  color: #52627a;
  font-size: clamp(0.9rem, 3.4vw, 1rem);
  max-width: 34rem;
}

/* Hide old top-right timer/stat block */
.hero .stats,
.stats {
  display: none !important;
}

/* Shared cards */

.game-card,
.rules {
  background: rgba(255,255,255,0.94);
  border-radius: clamp(18px, 5vw, 28px);
  box-shadow: 0 16px 40px rgba(23, 32, 51, 0.11);
}

.game-card {
  height: auto;
  min-height: auto;
  padding: clamp(10px, 3.6vw, 18px);
  overflow: visible;
}

.rules {
  height: auto;
  overflow: visible;
  margin-top: 12px;
  padding: clamp(14px, 4vw, 20px);
}

.rules h2 {
  margin: 0 0 8px;
  font-size: clamp(1.45rem, 6vw, 2rem);
}

.rules p {
  margin: 8px 0;
  color: #263449;
  line-height: 1.45;
}

.message {
  min-height: 1.7rem;
  margin-bottom: 10px;
  font-weight: 850;
  color: #334155;
  font-size: clamp(0.95rem, 3.8vw, 1.08rem);
}

/* Account bar */

.account-bar {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 50;

  width: auto;
  max-width: min(360px, calc(100vw - 28px));
  margin: 0;

  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(17, 24, 39, 0.08);
  border-radius: 999px;
  box-shadow: 0 12px 30px rgba(23, 32, 51, 0.12);
  backdrop-filter: blur(12px);

  padding: 7px 8px 7px 14px;

  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;

  font-size: 0.9rem;
  font-weight: 850;
  color: var(--ink);
}

.account-bar span {
  white-space: nowrap;
}

.account-bar strong {
  font-weight: 950;
}

.account-bar div {
  display: flex;
  gap: 6px;
}

.account-bar button {
  border: 0;
  border-radius: 999px;
  min-height: 34px;
  padding: 8px 12px;
  background: var(--ink);
  color: #fff;
  font-weight: 900;
  font-size: 0.86rem;
  cursor: pointer;
  line-height: 1;
}

.account-bar button:first-child {
  background: rgba(17, 24, 39, 0.09);
  color: var(--ink);
}

/* Puzzle meta and fairness boxes */

.fairness-stats-wrap {
  width: min(100%, 530px, calc(100vw - 40px));
  margin: 0 auto 12px;
}

.game-meta-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
  width: 100%;
}

.game-date-box {
  background: rgba(17, 24, 39, 0.06);
  border-radius: 12px;
  padding: 10px;
  text-align: center;
  font-weight: 800;
  margin-bottom: 0;
  min-width: 0;
}

.game-date-box strong {
  display: block;
  font-size: 1.05rem;
}

.fairness-stats {
  width: 100%;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.fairness-stats span {
  background: rgba(17, 24, 39, 0.06);
  border-radius: 12px;
  padding: 8px;
  text-align: center;
  font-weight: 800;
  font-size: 0.9rem;
}

.fairness-stats strong {
  display: block;
  font-size: 1.05rem;
}

.game-instruction {
  text-align: center;
  font-weight: 700;
  color: #475569;
  margin-top: 10px;
  font-size: 0.95rem;
}

/* Board */

.board-wrap {
  width: 100%;
  display: grid;
  place-items: center;
  overflow: visible;
}

.grid {
  display: grid;
  width: min(100%, 530px, calc(100vw - 40px));
  aspect-ratio: 1 / 1;
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 6px;
  overflow: hidden;
  gap: 0;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--paper);
  color: var(--ink);
  font-size: clamp(1.55rem, 10.5vw, 3rem);
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  min-width: 0;
  min-height: 0;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-style: solid;
  border-color: var(--ink);
  border-width: var(--bt, 1px) var(--br, 1px) var(--bb, 1px) var(--bl, 1px);
}

.cell.given {
  background: var(--given);
}

.cell.selected::after {
  content: "";
  position: absolute;
  inset: 5px;
  border: 3px solid var(--accent);
  border-radius: 2px;
  pointer-events: none;
}

.cell.conflict {
  background: var(--danger);
}

.cell.solved {
  background: var(--success);
}

/* Controls */

.number-pad,
.actions {
  display: grid;
  gap: clamp(7px, 2.4vw, 10px);
  width: min(100%, 530px, calc(100vw - 40px));
  margin-inline: auto;
}

.number-pad {
  grid-template-columns: repeat(5, 1fr);
  margin-top: 12px;
  margin-bottom: 10px;
}

.actions {
  grid-template-columns: repeat(4, 1fr);
  margin-top: 0;
}

.number-pad button,
.actions button,
.score-submit,
.auth-button,
.auth-tabs button {
  border: 0;
  border-radius: clamp(12px, 3.5vw, 16px);
  min-height: clamp(46px, 12vw, 58px);
  padding: 10px 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 900;
  font-size: clamp(1rem, 4.8vw, 1.28rem);
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: inline-grid;
  place-items: center;
}

.auth-tabs button,
.auth-button,
.score-submit {
  min-height: 50px;
  padding: 12px 14px;
  font-size: 1rem;
  font-weight: 950;
  box-shadow: 0 10px 22px rgba(17, 24, 39, 0.12);
}

.auth-tabs button.secondary {
  background: rgba(17, 24, 39, 0.08);
  color: var(--ink);
  box-shadow: none;
}

/* Leaderboards */

.leaderboard-section {
  margin-top: 1rem;
}

.leaderboard-section h3 {
  margin: 0 0 0.5rem;
}

.leaderboard-list {
  margin: 0;
  padding-left: 1.5rem;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.2rem 1rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid rgba(17, 24, 39, 0.12);
}

.leaderboard-list li:last-child {
  border-bottom: 0;
}

.leaderboard-list span {
  font-weight: 900;
}

.leaderboard-list strong {
  font-variant-numeric: tabular-nums;
}

.leaderboard-list small {
  grid-column: 1 / -1;
  color: #64748b;
  font-weight: 700;
}

.leaderboard-list a {
  color: var(--accent);
  font-weight: 900;
  text-decoration: none;
}

.leaderboard-list a:hover {
  text-decoration: underline;
}

/* Auth forms */

.score-box {
  border: 1px solid rgba(17, 24, 39, 0.08);
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px 0;
}

.auth-field,
.score-box label {
  margin-bottom: 10px;
}

.auth-field label,
.score-box label {
  display: block;
  font-weight: 950;
  color: var(--ink);
  margin-bottom: 6px;
}

.auth-field input,
.score-input,
.username-input {
  width: 100%;
  min-height: 52px;
  border: 2px solid rgba(17, 24, 39, 0.14);
  border-radius: 16px;
  background: #fff;
  color: var(--ink);
  padding: 0.78rem 0.95rem;
  font: inherit;
  font-size: 16px;
  font-weight: 750;
  outline: none;
  box-shadow: 0 8px 18px rgba(17, 24, 39, 0.06);
}

.auth-field input:focus,
.score-input:focus,
.username-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

.username-input-wrap {
  position: relative;
}

.username-input-wrap::before {
  content: "@";
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-weight: 950;
  color: var(--ink);
  pointer-events: none;
  z-index: 2;
}

.username-input-wrap .score-input,
.username-input-wrap .username-input {
  padding-left: 36px;
}

.auth-checkbox {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 12px 0;
  font-weight: 850;
  color: #334155;
}

.auth-checkbox input {
  width: 18px;
  height: 18px;
  accent-color: var(--ink);
}

.auth-message,
#scoreSubmitMessage,
#inlineAuthMessage {
  font-weight: 850;
  color: #b91c1c;
  margin-top: 10px;
}

.auth-link {
  background: none;
  border: 0;
  color: var(--accent);
  font: inherit;
  font-weight: 950;
  padding: 0;
  cursor: pointer;
}

/* Auth modal */

.auth-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(15, 23, 42, 0.48);
  backdrop-filter: blur(8px);
}

.auth-modal-overlay.is-open {
  display: flex;
}

.auth-modal {
  position: relative;
  width: min(100%, 460px);
  max-height: calc(100vh - 36px);
  overflow-y: auto;
  margin: 0;
  border-radius: 28px;
  padding: 22px;
  animation: authModalIn 160ms ease-out;
}

@keyframes authModalIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.auth-modal h2 {
  padding-right: 42px;
}

.auth-modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 999px;
  background: rgba(17, 24, 39, 0.08);
  color: var(--ink);
  font-size: 1.7rem;
  line-height: 1;
  font-weight: 900;
  cursor: pointer;
}

.auth-modal-close:hover {
  background: rgba(17, 24, 39, 0.14);
}

/* Mobile */

@media (max-width: 640px) {
  .account-bar {
    top: 8px;
    right: 8px;
    left: 8px;
    max-width: none;
    width: auto;
    border-radius: 18px;
    padding: 8px;
    justify-content: space-between;
    font-size: 0.84rem;
  }

  .account-bar div {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    min-width: 150px;
  }

  .account-bar button {
    min-height: 34px;
    padding: 7px 9px;
    font-size: 0.78rem;
  }

  .hero {
    padding-top: 64px;
  }
}

@media (max-width: 520px) {
  .app {
    width: 100%;
    padding-left: 8px;
    padding-right: 8px;
    padding-bottom: calc(120px + var(--safe-bottom));
  }

  .game-card {
    border-radius: 22px;
  }

  .grid,
  .number-pad,
  .actions,
  .fairness-stats-wrap {
    width: min(100%, calc(100vw - 28px));
  }

  .game-meta-row {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .game-date-box {
    padding: 8px 5px;
    font-size: 0.78rem;
  }

  .game-date-box strong {
    font-size: 0.85rem;
  }

  .actions {
    grid-template-columns: repeat(2, 1fr);
  }

  .subtitle {
    display: none;
  }

  .fairness-stats {
    gap: 6px;
  }

  .fairness-stats span {
    font-size: 0.78rem;
    padding: 7px 4px;
  }

  .fairness-stats strong {
    font-size: 0.92rem;
  }

  .auth-tabs {
    gap: 6px;
  }

  .auth-tabs button,
  .auth-button,
  .score-submit {
    min-height: 48px;
    font-size: 0.95rem;
  }

  .auth-modal-overlay {
    align-items: flex-end;
    padding: 10px;
  }

  .auth-modal {
    width: 100%;
    max-height: calc(100vh - 20px);
    border-radius: 24px 24px 18px 18px;
  }
}

@media (max-width: 380px) {
  .account-bar {
    font-size: 0.78rem;
  }

  .account-bar span {
    white-space: normal;
  }

  .account-bar div {
    min-width: 132px;
  }

  .account-bar button {
    font-size: 0.72rem;
    padding: 7px;
  }
}

@media (max-height: 720px) and (max-width: 520px) {
  .eyebrow {
    display: none;
  }

  h1 {
    font-size: 2.2rem;
  }

  .message {
    margin-bottom: 8px;
  }

  .number-pad {
    margin-top: 10px;
  }

  .number-pad button,
  .actions button {
    min-height: 44px;
  }
}


/* 6x6 board refinements */
.grid {
  max-width: 550px;
}

@media (max-width: 520px) {
  .cell {
    font-size: clamp(1.3rem, 8.8vw, 2.45rem);
  }

  .grid {
    border-width: 3px;
  }

  .cell.selected::after {
    inset: 3px;
    border-width: 2px;
  }
}

@media (max-width: 380px) {
  .cell {
    font-size: clamp(1.1rem, 8vw, 2rem);
  }
}
/* Fix empty 6x6 rows collapsing */
.grid {
  grid-auto-rows: 1fr;
}

.cell {
  width: 100%;
  height: 100%;
}

/* Notes / pencil marks */
.notes-toggle.active {
  background: var(--accent, #2563eb) !important;
}

.cell-notes {
  position: absolute;
  inset: 5px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  align-items: center;
  justify-items: center;
  color: #475569;
  font-size: clamp(0.48rem, 2.25vw, 0.82rem);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
}

.grid {
  grid-auto-rows: 1fr;
}

.cell {
  width: 100%;
  height: 100%;
}

@media (max-width: 520px) {
  .cell-notes {
    inset: 3px;
    font-size: clamp(0.45rem, 2.7vw, 0.72rem);
  }
}

.actions {
  grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
}

.anti-spam-field {
  position: absolute;
  left: -9999px;
  opacity: 0;
}