:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-soft: #f4f4f4;
  --text: #111111;
  --muted: #666666;
  --line: #111111;
  --line-soft: #cfcfcf;
  --accent: #111111;
  --accent-inverse: #ffffff;
  --shadow: 0 14px 34px rgba(0, 0, 0, 0.08);
  --radius-xl: 24px;
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --cell-size: min(9vw, 58px);
  --transition-fast: 140ms ease;
  --transition-med: 220ms ease;
}

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

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

body {
  display: flex;
  justify-content: center;
}

button, input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(100%, 1100px);
  min-height: 100vh;
  padding: 20px 16px 28px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-mark {
  width: 56px;
  height: 56px;
  border: 2px solid var(--line);
  border-radius: 16px;
  display: grid;
  place-items: center;
  font-size: 28px;
  font-weight: 700;
  background: var(--panel);
}

.brand-copy h1 {
  margin: 0 0 3px;
  font-size: 30px;
  line-height: 1;
}

.brand-copy p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.topbar-best {
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: 14px;
  white-space: nowrap;
}

.panel {
  border: 2px solid var(--line);
  border-radius: var(--radius-xl);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.menu-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: 1.15fr 0.85fr;
}

.hero-panel, .side-panel, .difficulty-panel, .game-panel, .stats-panel {
  padding: 20px;
}

.hero-panel {
  min-height: 520px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.hero-title {
  margin: 0 0 8px;
  font-size: clamp(32px, 5vw, 54px);
  line-height: 0.95;
}

.hero-subtitle {
  margin: 0 0 20px;
  max-width: 620px;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.5;
}

.play-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.play-card {
  border: 2px solid var(--line);
  background: var(--panel);
  border-radius: 22px;
  padding: 18px;
  min-height: 138px;
  text-align: left;
  transition: transform var(--transition-fast), background var(--transition-fast);
}

.play-card:hover {
  transform: translateY(-2px);
  background: var(--panel-soft);
}

.play-card strong {
  display: block;
  font-size: 24px;
  margin-bottom: 8px;
}

.play-card span {
  display: block;
  color: var(--muted);
  line-height: 1.4;
}

.play-card-wide {
  grid-column: 1 / -1;
}

.hero-footer {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: 20px;
}

.mini-card, .stats-card {
  border: 2px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  padding: 14px;
}

.mini-card-label, .stats-card-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.mini-card-value, .stats-card-value {
  font-size: 28px;
  font-weight: 700;
}

.mini-card-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.mini-card-plays {
  border-color: var(--accent);
}

.hero-footer {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 520px;
}

.side-panel h2, .difficulty-panel h2, .stats-panel h2 {
  margin: 0 0 8px;
  font-size: 28px;
}

.side-panel p, .difficulty-panel p, .stats-panel p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.side-stack {
  display: grid;
  gap: 12px;
  margin-top: 10px;
}

.side-note {
  border: 2px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  background: var(--panel-soft);
}

.side-note strong {
  display: block;
  margin-bottom: 8px;
  font-size: 18px;
}

.back-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.back-row-copy h2 {
  margin: 0 0 6px;
}

.back-row-copy p {
  margin: 0;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.difficulty-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.diff-card {
  border: 2px solid var(--line);
  border-radius: 22px;
  background: var(--panel);
  padding: 18px;
  min-height: 180px;
  text-align: left;
  transition: transform var(--transition-fast), background var(--transition-fast);
}

.diff-card:hover {
  transform: translateY(-2px);
  background: var(--panel-soft);
}

.diff-card strong {
  display: block;
  font-size: 24px;
  margin-bottom: 10px;
}

.diff-card span {
  color: var(--muted);
  line-height: 1.5;
}

.btn {
  border: 2px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 11px 16px;
  transition: transform var(--transition-fast), background var(--transition-fast), color var(--transition-fast);
}

.btn:hover {
  transform: translateY(-1px);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-inverse);
}

.btn-ghost {
  background: var(--panel-soft);
}

.game-panel {
  padding: 18px;
}

.game-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
}

.game-meta {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.badge {
  padding: 8px 12px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  font-size: 13px;
  white-space: nowrap;
}

.badge-dark {
  background: var(--accent);
  color: var(--accent-inverse);
}

.game-title {
  margin: 0;
  font-size: 28px;
}

.game-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.game-stats {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  min-width: min(100%, 430px);
}

.board-area {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 18px;
}

.board-panel-inner {
  border: 2px solid var(--line);
  border-radius: 24px;
  padding: 16px;
  background: var(--panel);
}

.board-status {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 14px;
}

.board-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(var(--cell-size) * 7);
  position: relative;
}

.board {
  display: grid;
  border: 3px solid var(--line);
  background: #ffffff;
  user-select: none;
  touch-action: manipulation;
  position: relative;
}

.board.clutch {
  animation: clutchPulse 800ms ease-in-out infinite;
}

.board.loading {
  opacity: 0.45;
}

.cell {
  border: 0;
  background: var(--panel);
  color: var(--text);
  font-size: clamp(20px, 3vw, 28px);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background var(--transition-fast), transform var(--transition-fast), color var(--transition-fast);
}

.cell.fixed {
  background: var(--panel-soft);
}

.cell.editable:hover {
  background: #f1f1f1;
}

.cell.related {
  background: #f8f8f8;
}

.cell.selected {
  background: #eaeaea;
}

.cell.same-value {
  background: #eeeeee;
}

.cell.invalid {
  background: var(--accent);
  color: var(--accent-inverse);
  animation: pulseInvalid 260ms ease;
}

.cell.success {
  animation: popIn 200ms ease;
}

.board-sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sidebar-card {
  border: 2px solid var(--line);
  border-radius: 20px;
  padding: 16px;
  background: var(--panel);
}

.sidebar-card h3 {
  margin: 0 0 10px;
  font-size: 18px;
}

.sidebar-card p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.sidebar-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.sidebar-item {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
}

.sidebar-item span:last-child {
  font-weight: 700;
}

.game-controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.helpbar {
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

kbd {
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 8px;
  padding: 2px 6px;
  font-size: 12px;
  background: #fff;
}

.stats-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.84);
  backdrop-filter: blur(4px);
  display: none;
  place-items: center;
  padding: 20px;
  z-index: 50;
}

.overlay.is-visible {
  display: grid;
  animation: fadeUp 180ms ease;
}

.overlay-card {
  width: min(100%, 440px);
  border: 2px solid var(--line);
  border-radius: 28px;
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 24px;
  text-align: center;
}

.overlay-icon {
  width: 78px;
  height: 78px;
  border: 2px solid var(--line);
  border-radius: 999px;
  display: grid;
  place-items: center;
  margin: 0 auto 14px;
  font-size: 36px;
  font-weight: 700;
}

.overlay-card h3 {
  margin: 0 0 8px;
  font-size: 30px;
}

.overlay-card p {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.5;
}

.overlay-badges {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ── Time select ── */
.time-select-block {
  margin: 18px 0 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.time-select-label {
  font-size: 15px;
  font-weight: 700;
}

.time-select-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.time-input {
  width: 90px;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--text);
  font-size: 28px;
  font-weight: 700;
  text-align: center;
  padding: 10px 8px;
  -moz-appearance: textfield;
}

.time-input::-webkit-inner-spin-button,
.time-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.time-input:focus {
  outline: none;
  border-color: var(--accent);
}

.time-select-hint {
  color: var(--muted);
  font-size: 14px;
}

.time-select-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

/* ── Ad slot ── */
.ad-slot {
  margin-top: 20px;
  padding: 14px;
  border: 2px dashed var(--line-soft);
  border-radius: var(--radius-md);
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  background: var(--panel-soft);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(16px);
  opacity: 0;
  pointer-events: none;
  background: var(--accent);
  color: var(--accent-inverse);
  padding: 12px 16px;
  border-radius: 999px;
  transition: opacity var(--transition-med), transform var(--transition-med);
  z-index: 60;
  font-size: 14px;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.mobile-digit-input {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.loading-copy {
  text-align: center;
  color: var(--muted);
  font-size: 16px;
}

.hidden {
  display: none !important;
}

@keyframes pulseInvalid {
  0% { transform: scale(1); }
  50% { transform: scale(0.94); }
  100% { transform: scale(1); }
}

@keyframes popIn {
  0% { transform: scale(0.82); }
  100% { transform: scale(1); }
}

@keyframes fadeUp {
  0% { opacity: 0; transform: translateY(12px); }
  100% { opacity: 1; transform: translateY(0); }
}

@keyframes clutchPulse {
  0% { box-shadow: 0 0 0 0 rgba(17, 17, 17, 0.03); }
  50% { box-shadow: 0 0 0 10px rgba(17, 17, 17, 0.10); }
  100% { box-shadow: 0 0 0 0 rgba(17, 17, 17, 0.03); }
}

@media (max-width: 980px) {
  .menu-grid, .board-area { grid-template-columns: 1fr; }
  .game-stats, .stats-grid, .difficulty-grid { grid-template-columns: 1fr; }
  .play-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero-footer { grid-template-columns: 1fr; }
  .game-head { flex-direction: column; }
}

@media (max-width: 720px) {
  :root { --cell-size: min(9.6vw, 44px); }
  .topbar { flex-direction: column; align-items: stretch; }
  .back-row { flex-direction: column; align-items: stretch; }
  .button-row, .game-controls { width: 100%; }
  .button-row .btn, .game-controls .btn, .overlay-actions .btn { flex: 1 1 0; }
  .hero-panel, .side-panel, .difficulty-panel, .game-panel, .stats-panel { padding: 16px; }
  .play-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 420px) {
  :root { --cell-size: min(9.3vw, 38px); }
  .brand-copy h1 { font-size: 26px; }
  .game-title { font-size: 24px; }
  .overlay-card h3 { font-size: 26px; }
  .play-grid { grid-template-columns: 1fr; }
}