:root {
  color-scheme: light;
  --bg: #f8f7fb;
  --surface: #ffffff;
  --ink: #161d2a;
  --muted: #687589;
  --line: #dde3ed;
  --cell: #f0f3f8;
  --cell-border: #d6deea;
  --fill: #1f2937;
  --accent: #f05f73;
  --accent-2: #f3a51c;
  --mark: #3f7ee8;
  --shadow: 0 20px 58px rgba(22, 29, 42, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    linear-gradient(135deg, rgba(240, 95, 115, 0.13), transparent 32%),
    linear-gradient(315deg, rgba(63, 126, 232, 0.1), transparent 34%),
    var(--bg);
  color: var(--ink);
  font-family:
    Arial, "Helvetica Neue", Helvetica, system-ui, -apple-system, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 160ms ease,
    border-color 160ms ease,
    background 160ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: rgba(22, 29, 42, 0.28);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

.primary {
  background: #161d2a;
  color: #fff;
  border-color: #161d2a;
}

.app-shell {
  width: min(1120px, calc(100% - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0 22px;
  display: grid;
  gap: 16px;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 14px;
  align-items: center;
}

.home-link {
  width: 66px;
  height: 44px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  font-size: 0.82rem;
  font-weight: 700;
}

.title-block {
  min-width: 0;
}

.kicker {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.9rem, 3.8vw, 2.9rem);
  line-height: 1.05;
  font-weight: 800;
}

.level-picker {
  height: 50px;
  display: grid;
  grid-template-columns: auto auto;
  gap: 10px;
  align-items: center;
  padding: 0 10px 0 14px;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.level-picker span {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

select {
  height: 34px;
  min-width: 118px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0 10px;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(124px, 0.38fr) minmax(340px, 1fr) minmax(144px, 0.38fr);
  gap: 20px;
  align-items: center;
  min-width: 0;
}

.panel {
  display: grid;
  gap: 10px;
}

.stats-panel {
  align-self: stretch;
  align-content: center;
}

.stat {
  min-height: 70px;
  padding: 12px;
  display: grid;
  gap: 8px;
  align-content: center;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stat span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
}

.stat strong {
  font-size: clamp(1.28rem, 2.3vw, 1.78rem);
  line-height: 1;
}

.board-section {
  display: grid;
  justify-items: center;
  gap: 12px;
  min-width: 0;
  width: 100%;
}

.nonogram-frame {
  width: min(74vh, 100%);
  max-width: 640px;
  min-width: 310px;
  aspect-ratio: 1;
  padding: clamp(10px, 2vw, 16px);
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.nonogram {
  --clue-size: clamp(56px, 16%, 104px);
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: var(--clue-size) 1fr;
  grid-template-rows: var(--clue-size) 1fr;
  gap: 8px;
}

.corner,
.column-clues,
.row-clues,
.pixel-board {
  min-width: 0;
  min-height: 0;
}

.corner {
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(240, 95, 115, 0.16), transparent 52%),
    #fff;
}

.column-clues {
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
  gap: 4px;
}

.row-clues {
  display: grid;
  grid-template-rows: repeat(var(--size), minmax(0, 1fr));
  gap: 4px;
}

.clue {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: clamp(0.62rem, 1.8vw, 0.86rem);
  font-weight: 700;
  line-height: 1.1;
  min-width: 0;
  overflow: hidden;
}

.column-clues .clue {
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
}

.row-clues .clue {
  justify-content: flex-end;
}

.clue.done {
  color: #129383;
  border-color: rgba(18, 147, 131, 0.32);
  background: rgba(27, 169, 165, 0.08);
}

.pixel-board {
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--size), minmax(0, 1fr));
  gap: 4px;
  touch-action: none;
  user-select: none;
}

.pixel-cell {
  position: relative;
  min-width: 0;
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  background: var(--cell);
  cursor: pointer;
  padding: 0;
}

.pixel-cell.filled {
  background: var(--fill);
  border-color: var(--fill);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.08);
}

.pixel-cell.marked::before,
.pixel-cell.marked::after {
  content: "";
  position: absolute;
  left: 22%;
  top: 48%;
  width: 56%;
  height: 2px;
  border-radius: 999px;
  background: var(--mark);
}

.pixel-cell.marked::before {
  transform: rotate(45deg);
}

.pixel-cell.marked::after {
  transform: rotate(-45deg);
}

.pixel-cell.wrong {
  outline: 3px solid rgba(240, 95, 115, 0.38);
  outline-offset: -3px;
}

.status {
  min-height: 30px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
  text-align: center;
}

.status.complete {
  color: #0f8f7d;
}

.control-panel {
  align-self: center;
}

.mode-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 6px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.mode {
  min-height: 38px;
  font-size: 0.82rem;
}

.mode.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.rules {
  display: flex;
  justify-content: center;
  color: var(--muted);
  text-align: center;
}

.rules p {
  max-width: 720px;
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.6;
}

@media (max-width: 900px) {
  .app-shell {
    width: min(100% - 20px, 720px);
    padding-top: 14px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
  }

  .home-link {
    grid-column: 1 / 2;
    grid-row: 1;
  }

  .title-block {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .top-actions {
    grid-column: 2;
    grid-row: 1;
  }

  .game-layout {
    grid-template-columns: 1fr;
    gap: 14px;
    width: 100%;
  }

  .stats-panel {
    grid-template-columns: repeat(4, 1fr);
    order: 2;
  }

  .stat {
    min-height: 64px;
    padding: 10px;
  }

  .stat strong {
    font-size: clamp(1.05rem, 5vw, 1.55rem);
  }

  .board-section {
    order: 1;
  }

  .nonogram-frame {
    width: 100%;
    max-width: 570px;
    min-width: 0;
  }

  .control-panel {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    order: 3;
  }

  .mode-group {
    grid-column: 1 / -1;
  }
}

@media (max-width: 520px) {
  .app-shell {
    width: calc(100% - 20px);
    max-width: 370px;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .home-link,
  .title-block,
  .top-actions {
    grid-column: 1;
    grid-row: auto;
  }

  .top-actions {
    width: 100%;
  }

  .level-picker {
    height: 48px;
    width: 100%;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 8px;
    padding: 6px 8px;
  }

  select {
    width: 100%;
    min-width: 0;
    height: 28px;
    font-size: 0.82rem;
  }

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

  .control-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mode-group {
    grid-template-columns: repeat(3, 1fr);
  }

  .nonogram {
    --clue-size: clamp(52px, 20vw, 78px);
    gap: 5px;
  }

  .pixel-board,
  .column-clues,
  .row-clues {
    gap: 3px;
  }

  button {
    min-height: 46px;
  }
}
