:root {
  --bg: #fcfbf3;
  --text: #2f312d;
  --line: #374036;
  --green: #669b6d;
  --badge: #7caf4a;
  --star: #eba229;
  --star-off: #d9d9d7;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

/* ── Шапка: по центру, сразу над полем (top задаёт game.js) ── */
.top {
  position: absolute;
  z-index: 2;
  left: 0;
  right: 0;
  top: calc(env(safe-area-inset-top) + 12px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.top h1 {
  margin: 0;
  text-align: center;
  font-size: 19px;
  line-height: 24px;
  font-weight: 700;
  letter-spacing: 0.1px;
  color: var(--text);
}

.stars {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.star {
  width: 24px;
  height: 24px;
  display: block;
}
.star path {
  fill: var(--star);
  stroke: var(--star);
  stroke-width: 1.6;
  stroke-linejoin: round;
  transition: fill .25s, stroke .25s;
}
.star.off path { fill: var(--star-off); stroke: var(--star-off); }
.star.lost { animation: star-lost .5s ease; }

@keyframes star-lost {
  0%   { transform: scale(1); }
  30%  { transform: scale(1.35) rotate(-12deg); }
  60%  { transform: scale(.85) rotate(6deg); }
  100% { transform: scale(1); }
}

/* ── Поле ──────────────────────────────── */
.board {
  position: relative;
  flex: 1;
  min-height: 0;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 26px, #000 calc(100% - 96px), transparent calc(100% - 28px));
          mask-image: linear-gradient(to bottom, transparent 0, #000 26px, #000 calc(100% - 96px), transparent calc(100% - 28px));
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: pointer;
}
#canvas.grabbing { cursor: grabbing; }

/* Слой для уползающих змеек — на весь экран, поверх шапки */
#fx {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 3;
  pointer-events: none;
}

/* ── Подсказка ─────────────────────────── */
.hint {
  position: absolute;
  z-index: 2;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 22px);
  transform: translateX(-50%);
  width: 46px;
  height: 46px;
  padding: 0;
  border: 0;
  border-radius: 13px;
  background: #fff;
  color: #6a9875;
  box-shadow: 0 1px 2px rgba(60, 64, 50, .06), 0 4px 14px rgba(60, 64, 50, .09);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform .12s ease;
}
.hint:active { transform: translateX(-50%) scale(.93); }
.hint svg { width: 28px; height: 28px; }
.hint:disabled { cursor: default; }
.hint:disabled svg { opacity: .45; }

.badge {
  position: absolute;
  top: -9px;
  right: -9px;
  min-width: 21px;
  height: 21px;
  padding: 0 5px;
  border-radius: 11px;
  background: var(--badge);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  line-height: 21px;
  text-align: center;
  box-shadow: 0 0 0 2px var(--bg);
}
.hint:disabled .badge { background: #c9c9c5; }

/* ── Оверлей (победа / поражение) ─────── */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(252, 251, 243, .78);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade-in .25s ease;
}
.overlay[hidden] { display: none; }

.card {
  width: min(300px, 100%);
  padding: 26px 22px 22px;
  border-radius: 20px;
  background: #fff;
  text-align: center;
  box-shadow: 0 2px 4px rgba(60, 64, 50, .05), 0 12px 36px rgba(60, 64, 50, .13);
  animation: pop-in .3s cubic-bezier(.2, 1.4, .4, 1);
}
.card h2 { margin: 10px 0 6px; font-size: 21px; font-weight: 700; }
.card p { margin: 0 0 20px; font-size: 15px; color: #7b7d76; }
.card-stars { display: flex; justify-content: center; gap: 6px; }
.card-stars .star { width: 38px; height: 38px; }

.btn {
  width: 100%;
  height: 46px;
  border: 0;
  border-radius: 13px;
  background: var(--green);
  color: #fff;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: filter .15s, transform .12s;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: scale(.97); }

@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: scale(.85); } }

/* ── Таймер ────────────────────────────── */
.timer {
  margin-top: 1px;
  font-size: 14px;
  line-height: 18px;
  font-weight: 600;
  color: #8a8c84;
  font-variant-numeric: tabular-nums;
  letter-spacing: .3px;
}
.card-time { margin: -10px 0 18px; font-size: 14px; color: #7b7d76; font-variant-numeric: tabular-nums; }
.card-time b { color: var(--text); }
.card-time .record { color: var(--green); font-weight: 600; }

/* ── Дуэль: прогресс игроков ───────────── */
.race {
  width: min(280px, calc(100vw - 32px));
  display: grid;
  gap: 5px;
  margin-top: 2px;
}
.race[hidden] { display: none; }
.race-row {
  display: grid;
  grid-template-columns: 82px 1fr 52px;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  line-height: 16px;
  color: #6f716a;
}
.race-name { display: flex; align-items: center; gap: 5px; font-weight: 600; white-space: nowrap; }
.race-row.me .race-name { color: var(--text); }
.race-bar {
  position: relative;
  height: 7px;
  border-radius: 4px;
  background: #ecebe2;
  overflow: hidden;
}
.race-bar i {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  border-radius: 4px;
  background: var(--green);
  transition: width .35s ease;
}
.race-row.opp .race-bar i { background: var(--star); }
.race-val { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.race-row[data-state="finished"] .race-val { color: var(--green); }
.race-row[data-state="failed"] .race-val { color: #ad4f3e; }
.race-row[data-state="offline"] .race-val,
.race-row[data-state="waiting"] .race-val { color: #a9aba3; font-weight: 500; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: #c9c9c5; flex: none; }
.race-row.opp:not([data-state="offline"]):not([data-state="waiting"]) .dot { background: var(--badge); }

/* ── Кнопка «Игра с другом» ────────────── */
.corner-btn {
  position: absolute;
  z-index: 4;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 12px;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: #fff;
  color: #6a9875;
  box-shadow: 0 1px 2px rgba(60, 64, 50, .06), 0 4px 14px rgba(60, 64, 50, .09);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform .12s ease;
}
.corner-btn:active { transform: scale(.93); }
.corner-btn svg { width: 22px; height: 22px; }

/* ── Отсчёт перед стартом ──────────────── */
.countdown {
  position: absolute;
  z-index: 4;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-size: 88px;
  font-weight: 800;
  color: var(--green);
  text-shadow: 0 0 0 var(--bg), 0 0 18px var(--bg), 0 0 36px var(--bg);
  font-variant-numeric: tabular-nums;
}
.countdown[hidden] { display: none; }
.countdown.pop { animation: cd-pop .6s cubic-bezier(.2, 1.3, .4, 1); }
@keyframes cd-pop { from { transform: scale(1.8); opacity: 0; } }

/* ── Тост ──────────────────────────────── */
.toast {
  position: absolute;
  z-index: 6;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 12px;
  background: var(--line);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(40, 44, 36, .2);
  pointer-events: none;
}
.toast[hidden] { display: none; }
.toast.show { animation: toast-in .3s ease; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ── Окно дуэли ────────────────────────── */
.card .btn + .btn { margin-top: 8px; }
.btn-ghost { background: transparent; color: #7b7d76; }
.btn-ghost:hover { background: #f4f3ec; filter: none; }
.btn:disabled { opacity: .55; cursor: default; filter: none; transform: none; }
.btn-small { width: auto; height: 40px; padding: 0 14px; font-size: 14px; flex: none; }
.card-icon {
  width: 52px; height: 52px; margin: 0 auto 2px;
  border-radius: 16px; background: #eef4ea; color: #6a9875;
  display: grid; place-items: center;
}
.card-icon svg { width: 28px; height: 28px; }
.link-row { display: flex; gap: 8px; margin-bottom: 10px; }
.link {
  flex: 1; min-width: 0; height: 40px; padding: 0 12px;
  border: 1px solid #e4e3da; border-radius: 12px; background: #fafaf5;
  font: inherit; font-size: 13px; color: var(--text);
  user-select: all; -webkit-user-select: all;
}
.link:focus { outline: 2px solid #cfe2c6; outline-offset: 0; }
.waiting {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin: 14px 0 10px; font-size: 14px; color: #8a8c84;
}
.waiting.ready { color: var(--green); font-weight: 600; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--badge); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; transform: scale(.7); } }
.cmd {
  display: block; margin: -8px 0 12px; padding: 10px;
  border-radius: 10px; background: #f4f3ec; font-size: 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.spinner {
  width: 28px; height: 28px; margin: 4px auto 14px; border-radius: 50%;
  border: 3px solid #e4e3da; border-top-color: var(--green);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.result-emoji { font-size: 44px; line-height: 1; }
.result-table { display: grid; gap: 6px; margin: -6px 0 6px; }
.result-table > div {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-radius: 12px; background: #f6f5ee; font-size: 15px;
}
.result-table > div.win { background: #eef4ea; }
.result-table > div.win b { color: var(--green); }
.result-table span { color: #7b7d76; }
.result-table b { font-variant-numeric: tabular-nums; }
