:root {
  --bg: #F6F5F2;
  --surface: #FFFFFF;
  --ink: #34343A;
  --muted: #8C8B86;
  --faint: #E4E2DC;
  --red: #F0607A;
  --accent: linear-gradient(135deg, #93DBC8, #9AAEF2);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body { overflow: hidden; }

#app { position: fixed; inset: 0; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

svg.ico { display: block; flex: none; }

/* ---------------------------------------------------------------- выбор уровня */

.menu {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.menu-bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.55;
  pointer-events: none;
}

.menu-inner {
  position: relative;
  max-width: 980px;
  margin: 0 auto;
  padding: calc(34px + var(--safe-t)) calc(24px + var(--safe-r)) calc(40px + var(--safe-b)) calc(24px + var(--safe-l));
}

.menu-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
}

.menu h1 {
  margin: 0;
  font-size: 30px;
  font-weight: 300;
  letter-spacing: 0.01em;
}

.menu .sub {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.total {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  padding-bottom: 2px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 22px 8px;
  margin-top: 30px;
}

.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  min-width: 0;
}

.card:disabled { opacity: 0.45; cursor: default; }

.card .num {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 300;
  transition: transform 0.15s ease;
}

.card.done .num { background: var(--accent); color: #fff; }
.card.open .num {
  border: 2px solid transparent;
  background: linear-gradient(var(--surface), var(--surface)) padding-box, var(--accent) border-box;
}
.card.locked .num { border: 1.5px solid var(--faint); color: var(--muted); }

@media (hover: hover) {
  .card:not(:disabled):hover .num { transform: scale(1.07); }
}
.card:not(:disabled):active .num { transform: scale(0.96); }

.card .name {
  margin-top: 8px;
  max-width: 100%;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card .stars { margin-top: 5px; }

.menu-foot {
  margin-top: 38px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.7;
}

.menu-foot kbd, .keys kbd {
  display: inline-block;
  min-width: 18px;
  padding: 0 5px;
  border: 1px solid var(--faint);
  border-radius: 5px;
  background: var(--surface);
  font: inherit;
  font-size: 11px;
  text-align: center;
  color: var(--ink);
}

@media (hover: none) {
  .menu-foot .desktop { display: none; }
}

/* ---------------------------------------------------------------- игра */

.game {
  position: absolute;
  inset: 0;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

.game canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.game.locked canvas { cursor: default; }
.game.zoomed.locked canvas { cursor: grab; }
.game.tool-eraser:not(.locked) canvas { cursor: cell; }
.game.tool-light:not(.locked) canvas { cursor: pointer; }

/* когда карта приближена, она уезжает под верхнюю панель — подложка, чтобы надписи читались */
.topfade {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: calc(76px + var(--safe-t));
  background: linear-gradient(var(--bg) 62%, rgba(246, 245, 242, 0));
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}
.game.zoomed .topfade { opacity: 1; }

.hud {
  position: absolute;
  top: calc(12px + var(--safe-t));
  display: flex;
  align-items: center;
}

.hud-l {
  left: calc(14px + var(--safe-l));
  right: 230px;
  gap: 12px;
  pointer-events: none;
}
.hud-l > * { pointer-events: auto; }

.hud-r {
  right: calc(14px + var(--safe-r));
  gap: 14px;
}

.circle {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1.5px solid var(--ink);
  display: grid;
  place-items: center;
  flex: none;
}

.circle.play { width: 38px; height: 38px; }
.circle.fill { border-color: transparent; background: var(--accent); color: #fff; }

.titles { min-width: 0; }

.title {
  font-size: 15px;
  font-weight: 300;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.progress {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}

.bar {
  width: 120px;
  max-width: 40vw;
  height: 4px;
  border-radius: 2px;
  background: var(--faint);
  overflow: hidden;
  flex: none;
}

.bar i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 2px;
  background: var(--accent);
  background-size: 120px 100%;
}

.money {
  display: flex;
  align-items: center;
  gap: 4px;
  font-variant-numeric: tabular-nums;
}
.money.flash { color: var(--red); }

.iconbtn {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
}

.speed {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 5px 10px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}
.speed.on { background: var(--accent); color: #fff; }

/* инструменты */

.tools {
  position: absolute;
  right: calc(14px + var(--safe-r));
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: flex-start;
}

.toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(30, 34, 51, 0.04);
}

.toolbar.dim .tbtn { opacity: 0.35; }

.tbtn {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: none;
}

.tbtn.on { background: var(--accent); color: #fff; }
.tbtn.off { color: var(--faint); }

.tbtn .corner {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 9px;
  height: 9px;
  background: var(--muted);
  clip-path: polygon(0 0, 100% 100%, 0 100%);
}

.badge {
  position: absolute;
  top: 0;
  right: 0;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 500;
  line-height: 15px;
  text-align: center;
}

.sep { width: 20px; height: 1px; background: var(--faint); flex: none; }

.roadmenu {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 6px 0 0;
  transform-origin: 100% 50%;
  transition: opacity 0.14s ease, transform 0.14s ease;
}

.roadmenu.hidden {
  opacity: 0;
  transform: scale(0.4);
  pointer-events: none;
}

.rm {
  position: relative;
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
}

.rm span.c {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(30, 34, 51, 0.22);
}
.rm.on span.c { background: var(--accent); color: #fff; }

.rm .badge { width: 16px; height: 16px; line-height: 16px; background: var(--ink); }
.rm .badge.zero { background: var(--faint); }

/* на узких вертикальных экранах панель инструментов ложится вниз */
.game.portrait .tools {
  top: auto;
  right: auto;
  left: 50%;
  bottom: calc(12px + var(--safe-b));
  transform: translateX(-50%);
  flex-direction: column;
  align-items: flex-start;
}
.game.portrait .toolbar { flex-direction: row; }
.game.portrait .sep { width: 1px; height: 20px; }
.game.portrait .roadmenu { padding: 0 0 6px 0; transform-origin: 20px 100%; flex-direction: row-reverse; }
.game.portrait .hud-l { right: 196px; gap: 10px; }
.game.portrait .title { font-size: 14px; }
.game.portrait .title .lv { display: none; }
.game.portrait .hud-r { gap: 8px; }
.game.portrait .bar { width: 64px; }

/* всплывающее сообщение */

.toast {
  position: absolute;
  top: calc(64px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 32px));
  padding: 8px 16px;
  border-radius: 18px;
  background: var(--ink);
  color: var(--surface);
  font-size: 13px;
  text-align: center;
  pointer-events: none;
  transition: opacity 0.18s ease;
  z-index: 3;
}
.toast.hidden { opacity: 0; }

/* панель светофора */

.lightpanel {
  position: absolute;
  left: 0;
  top: 0;
  max-width: calc(100vw - 24px);
  padding: 10px 16px;
  border-radius: 20px;
  background: var(--surface);
  box-shadow: 0 2px 10px rgba(30, 34, 51, 0.08);
  z-index: 2;
}
.lightpanel[hidden] { display: none; }

.lp-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 300;
  white-space: nowrap;
}
.lp-head .grow { flex: 1; min-width: 16px; }

.lp-row {
  display: flex;
  align-items: center;
  height: 30px;
}
.lp-row.off { color: var(--muted); }

.switch {
  width: 36px;
  height: 21px;
  margin: 0 10px 0 8px;
  padding: 3px;
  border-radius: 999px;
  background: var(--faint);
  display: flex;
  flex: none;
}
.switch.on { background: var(--accent); justify-content: flex-end; }
.switch i { width: 15px; height: 15px; border-radius: 50%; background: #fff; }

.lp-row .secs {
  width: 36px;
  margin-left: 8px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.lp-row .offnote { width: 234px; max-width: 52vw; font-size: 12px; color: var(--muted); }
.lp-note { font-size: 12px; color: var(--muted); padding: 2px 0 2px; }

input[type=range].slider {
  -webkit-appearance: none;
  appearance: none;
  width: 190px;
  min-width: 80px;
  flex: 1 1 190px;
  height: 30px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  --f: 50%;
  touch-action: none;
}
input[type=range].slider::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: linear-gradient(90deg, #9AAEF2 var(--f), var(--faint) var(--f));
  background: linear-gradient(90deg, var(--a2) var(--f), var(--faint) var(--f));
}
input[type=range].slider::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--a2) var(--f), var(--faint) var(--f));
}
input[type=range].slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 17px;
  height: 17px;
  margin-top: -7px;
  border-radius: 50%;
  background: #fff;
  border: 1.5px solid var(--ink);
}
input[type=range].slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 1.5px solid var(--ink);
}

/* карточки поверх поля */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(246, 245, 242, 0.74);
  z-index: 4;
  animation: fade 0.18s ease;
}
.overlay[hidden] { display: none; }

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } }

.dialog {
  max-width: 460px;
  padding: 22px 32px;
  border-radius: 24px;
  background: var(--surface);
  text-align: center;
  box-shadow: 0 6px 30px rgba(30, 34, 51, 0.07);
  animation: pop 0.22s ease;
}

.dialog .over { font-size: 13px; color: var(--muted); }
.dialog h2 { margin: 2px 0 0; font-size: 22px; font-weight: 300; }
.dialog p { margin: 10px 0 0; font-size: 14px; line-height: 20px; }
.dialog .meta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
}
.dialog .meta .gap { width: 10px; }
.dialog .small { margin-top: 2px; font-size: 12px; color: var(--muted); }
.dialog .big-stars { display: flex; justify-content: center; margin: 14px 0 12px; }
.dialog .buttons { display: flex; justify-content: center; gap: 12px; margin-top: 18px; }
.dialog .keys { margin-top: 14px; font-size: 11px; color: var(--muted); line-height: 1.9; }

@media (hover: none) {
  .dialog .keys { display: none; }
}

.pill {
  padding: 10px 22px;
  border-radius: 999px;
  border: 1.5px solid var(--ink);
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
}
.pill.fill { border-color: transparent; background: var(--accent); color: #fff; }

@media (hover: hover) {
  .pill:hover, .circle:hover, .rm:hover span.c { filter: brightness(0.97); }
  .iconbtn:hover, .speed:not(.on):hover, .toolbar:not(.dim) .tbtn:not(.on):hover { background: rgba(52, 52, 58, 0.06); }
}
