:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-soft: #f9fafb;
  --surface-elevated: #ffffff;
  --text: #1f2937;
  --text-strong: #0f172a;
  --muted: #6b7280;
  --line: #d1d5db;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --accent: #059669;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning-soft: #fff7ed;
  --selected: #bfdbfe;
  --peer: #e6efff;
  --same: #c7ddff;
  --same-text: #1e40af;
  --conflict: #ffdfe0;
  --conflict-text: #b91c1c;
  --preview: #d1fae5;
  --preview-ring: #10b981;
  --fixed: #111827;
  --board-line: #111827;
  --platform-hint: #64748b;
  --overlay-bg: rgba(17, 24, 39, 0.62);
  --toast-bg: #111827;
  --toast-text: #ffffff;
  --success-line: #a7f3d0;
  --success-bg: #ecfdf5;
  --success-text: #065f46;
  --progress-track: #e5e7eb;
  --progress-fill-start: #22c55e;
  --progress-fill-end: #16a34a;
  --warning-line: #fdba74;
  --warning-text: #9a3412;
  --warning-ring: #f97316;
  --shadow: 0 10px 24px rgba(17, 24, 39, 0.08);
  --safe-top: 0px;
  --safe-right: 0px;
  --safe-bottom: 0px;
  --safe-left: 0px;
}

:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #111827;
  --surface-soft: #172033;
  --surface-elevated: #1b263b;
  --text: #e5e7eb;
  --text-strong: #f8fafc;
  --muted: #9ca3af;
  --line: #334155;
  --primary: #60a5fa;
  --primary-soft: #1e3a8a;
  --selected: #1d4ed8;
  --peer: #1f2937;
  --same: #1e40af;
  --same-text: #dbeafe;
  --conflict: #7f1d1d;
  --conflict-text: #fecaca;
  --preview: #064e3b;
  --preview-ring: #34d399;
  --fixed: #f8fafc;
  --board-line: #64748b;
  --platform-hint: #94a3b8;
  --overlay-bg: rgba(2, 6, 23, 0.72);
  --toast-bg: #e2e8f0;
  --toast-text: #0f172a;
  --success-line: #166534;
  --success-bg: #052e1d;
  --success-text: #bbf7d0;
  --progress-track: #334155;
  --progress-fill-start: #22c55e;
  --progress-fill-end: #16a34a;
  --warning-line: #c2410c;
  --warning-text: #fdba74;
  --warning-ring: #fb923c;
  --shadow: 0 10px 24px rgba(2, 6, 23, 0.45);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  padding: calc(16px + var(--safe-top)) calc(14px + var(--safe-right)) calc(24px + var(--safe-bottom))
    calc(14px + var(--safe-left));
}

.app--miniapp {
  min-height: 100dvh;
}

.screen {
  display: none;
}

.screen--active {
  display: block;
}

.topbar {
  margin-bottom: 14px;
}

.topbar--game {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.game-header-actions {
  margin-left: auto;
  display: flex;
  align-items: flex-start;
}

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

.platform-hint {
  margin: 6px 0 0;
  color: var(--platform-hint);
  font-size: 12px;
}

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

.game-meta {
  min-width: 0;
}

.game-stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.game-stat {
  font-size: 13px;
  color: var(--text);
}

.panel--play,
#daily-panel {
  margin-bottom: 10px;
}

#daily-panel {
  margin-top: 12px;
}

#daily-start-btn {
  margin-top: 10px;
  width: 100%;
}

.panel__actions {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.meta-grid {
  display: grid;
  gap: 10px;
  margin-bottom: 12px;
}

.player-center {
  display: grid;
  gap: 10px;
  margin-bottom: 12px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 12px;
  box-shadow: var(--shadow);
}

.panel__title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.panel__text {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.achievements-list,
.level-breakdown-list,
.challenges-list,
.session-history-list {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 6px;
}

.achievement-item {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 9px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface-elevated);
}

.achievement-item--done {
  border-color: var(--success-line);
  background: var(--success-bg);
  color: var(--success-text);
}

.stats-grid {
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.stat-card {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px;
  background: var(--surface-elevated);
}

.stat-card__label {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.stat-card__value {
  margin: 4px 0 0;
  font-size: 17px;
  font-weight: 700;
  color: var(--text-strong);
}

.level-breakdown-item,
.session-history-item {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 9px;
  background: var(--surface-elevated);
}

.level-breakdown-item__title,
.session-history-item__title {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-strong);
}

.level-breakdown-item__meta,
.session-history-item__meta {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--text);
}

.challenge-item {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px;
  background: var(--surface-elevated);
}

.challenge-item--done {
  border-color: var(--success-line);
  background: var(--success-bg);
}

.challenge-item__title {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-strong);
}

.challenge-item__meta {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--text);
}

.challenge-progress {
  margin-top: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--progress-track);
  overflow: hidden;
}

.challenge-progress__fill {
  height: 100%;
  background: linear-gradient(90deg, var(--progress-fill-start), var(--progress-fill-end));
  transition: width 0.2s ease;
}

.levels-list {
  display: grid;
  gap: 10px;
}

.level-card {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.level-card__name {
  font-size: 16px;
  font-weight: 700;
}

.level-card__meta {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
}

.level-card__extra {
  margin-top: 4px;
  font-size: 12px;
  color: var(--text);
}

.level-card:disabled {
  opacity: 0.55;
  box-shadow: none;
}

.board-wrapper {
  background: var(--surface);
  border-radius: 14px;
  padding: 10px;
  box-shadow: var(--shadow);
}

.board-wrapper--hidden {
  visibility: hidden;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  border: 2px solid var(--board-line);
  touch-action: manipulation;
}

.cell {
  appearance: none;
  border: 1px solid var(--line);
  min-height: 38px;
  aspect-ratio: 1 / 1;
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 22px;
  font-weight: 600;
  padding: 0;
  position: relative;
}

.cell:nth-child(3n + 1) {
  border-left: 2px solid var(--board-line);
}

.cell:nth-child(-n + 9) {
  border-top: 2px solid var(--board-line);
}

.cell[data-col="2"],
.cell[data-col="5"],
.cell[data-col="8"] {
  border-right: 2px solid var(--board-line);
}

.cell[data-row="2"],
.cell[data-row="5"],
.cell[data-row="8"] {
  border-bottom: 2px solid var(--board-line);
}

.cell--fixed {
  color: var(--fixed);
  font-weight: 800;
  background: var(--surface-soft);
}

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

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

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

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

.cell--mistake {
  box-shadow: inset 0 0 0 2px var(--warning-ring);
}

.cell--preview-target {
  background: var(--preview);
  box-shadow: inset 0 0 0 2px var(--preview-ring);
  animation: preview-pulse 0.85s ease-in-out infinite alternate;
}

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  font-size: 9px;
  color: var(--muted);
  line-height: 1;
}

.note {
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 2px 8px;
  color: var(--muted);
  font-size: 14px;
}

.toggle-row--right {
  justify-content: flex-end;
}

.toggle-row__label {
  user-select: none;
}

.radio-toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  cursor: pointer;
}

.radio-toggle input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.radio-toggle__ui {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--surface-elevated);
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
  position: relative;
}

.radio-toggle__ui::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.2s ease;
}

.radio-toggle input:checked + .radio-toggle__ui {
  border-color: var(--primary);
  background: var(--primary-soft);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 20%, transparent);
}

.radio-toggle input:checked + .radio-toggle__ui::after {
  transform: translate(-50%, -50%) scale(1);
}

.radio-toggle input:focus-visible + .radio-toggle__ui {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.error-hint {
  margin: 0 2px 8px;
  min-height: 18px;
  font-size: 12px;
  color: var(--warning-text);
}

.tools,
.numpad {
  display: grid;
  gap: 8px;
}

.tools {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin-bottom: 10px;
}

.numpad {
  grid-template-columns: repeat(9, 1fr);
}

.btn {
  border: 1px solid var(--line);
  background: var(--surface-elevated);
  color: var(--text);
  border-radius: 10px;
  min-height: 42px;
  font-size: 14px;
  font-weight: 600;
}

.tools .btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 56px;
  padding: 6px 4px;
}

.btn__icon {
  margin-right: 0;
  line-height: 1;
}

.tools .btn .btn__icon,
.tools .btn .btn__icon-wrap {
  font-size: 18px;
}

.tools .btn .btn__icon-wrap {
  min-width: 20px;
}

.btn__icon-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
}

.hint-counter {
  position: absolute;
  top: -7px;
  right: -10px;
  min-width: 14px;
  height: 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-strong);
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.btn__label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tools .btn .btn__label {
  font-size: 11px;
  line-height: 1.05;
}

.btn__label--sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.btn--secondary {
  width: 96px;
}

.btn--active {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}

.btn--depleted {
  opacity: 0.55;
  border-style: dashed;
}

.btn--play {
  margin-top: 10px;
  width: 100%;
}

.btn--icon {
  min-height: 36px;
  width: 36px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}

.btn--pause-top {
  min-height: 32px;
  width: 32px;
  border-radius: 999px;
  font-size: 16px;
}

.btn:disabled {
  opacity: 0.45;
}

.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
}

.overlay[hidden] {
  display: none;
}

.overlay__modal {
  width: min(340px, 100%);
  background: var(--surface);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.overlay__modal--levels {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  overflow: auto;
  text-align: left;
}

.overlay__title {
  margin: 0;
  font-size: 20px;
}

.overlay__text {
  margin: 10px 0 14px;
  color: var(--muted);
  font-size: 14px;
}

.overlay__actions {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr 1fr;
  margin-top: 8px;
}

.win-summary-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 6px;
}

.win-summary-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-elevated);
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  text-align: left;
}

.win-summary-item__label {
  font-size: 12px;
  color: var(--muted);
}

.win-summary-item__value {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-strong);
  text-align: right;
}

.levels-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#levels-overlay .levels-list {
  margin-top: 6px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%) translateY(20px);
  background: var(--toast-bg);
  color: var(--toast-text);
  font-size: 14px;
  border-radius: 10px;
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s ease;
  z-index: 30;
}

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

@keyframes preview-pulse {
  from {
    box-shadow: inset 0 0 0 1px var(--preview-ring);
  }

  to {
    box-shadow: inset 0 0 0 2px var(--preview-ring);
  }
}

@media (max-width: 420px) {
  .app {
    padding: calc(12px + var(--safe-top)) calc(10px + var(--safe-right)) calc(20px + var(--safe-bottom))
      calc(10px + var(--safe-left));
  }

  .cell {
    min-height: 34px;
    font-size: 20px;
  }

  .btn {
    min-height: 40px;
    font-size: 13px;
  }

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

  .tools .btn {
    gap: 3px;
    min-height: 54px;
    padding: 5px 3px;
  }

  .tools .btn .btn__icon,
  .tools .btn .btn__icon-wrap {
    font-size: 19px;
  }

  .tools .btn .btn__label {
    font-size: 10px;
  }

  .btn--pause-top {
    min-height: 30px;
    width: 30px;
    font-size: 15px;
  }

  .overlay__actions,
  .panel__actions {
    grid-template-columns: 1fr;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .overlay__modal--levels {
    max-height: 85vh;
  }
}
