/* --- Design tokens --- */
:root {
  --bg-main: #0f1115;
  --bg-secondary: #151821;
  --card-bg: rgba(255, 255, 255, 0.04);

  --text-main: #f5f7fb;
  --text-secondary: #9aa3b2;

  --accent-main: #c9a96e;
  --accent-soft: #e7d3a7;

  --success: #4ade80;
  --error: #fb7185;

  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --r-card: 24px;
  --r-control: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--text-main);
  background: var(--bg-main);
  overflow-x: hidden;
}

/* Без прокрутки самой страницы на ПК: игра или стартовый экран — на весь вьюпорт, скролл внутри при необходимости */
@media (min-width: 600px) {
  body:has(#viewGame:not([hidden])),
  body:has(#viewStart:not([hidden])) {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }
}

.app-bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background: radial-gradient(circle at 20% 30%, rgba(201, 169, 110, 0.08), transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(255, 255, 255, 0.05), transparent 40%), #0f1115;
}

.app {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px calc(24px + var(--safe-bottom));
}

.view {
  width: 100%;
}

.view--start {
  width: 100%;
  max-width: 520px;
  margin-left: auto;
  margin-right: auto;
}

.view[hidden] {
  display: none !important;
}

.view__inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.75rem;
}

.view--start .view__inner--start {
  padding: 8px 4px 0;
  max-width: 440px;
  margin: 0 auto;
}

.view--game .game-board {
  animation: fadeInUp 0.6s ease 0.1s both;
}

/* --- Start --- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent-soft);
  background: linear-gradient(135deg, rgba(201, 169, 110, 0.2), rgba(255, 255, 255, 0.06));
  border: 1px solid rgba(201, 169, 110, 0.35);
  box-shadow: 0 0 24px rgba(201, 169, 110, 0.12);
}

.title {
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.12;
  margin: 0.4rem 0 0.25rem;
}

.title--hero {
  font-size: clamp(1.65rem, 5.5vw, 2.1rem);
  background: linear-gradient(120deg, var(--text-main) 0%, var(--accent-soft) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 20px rgba(201, 169, 110, 0.15));
}

.lede {
  font-size: clamp(0.9rem, 2.5vw, 1rem);
  line-height: 1.55;
  color: var(--text-secondary);
  margin: 0 0 1rem;
  max-width: 38ch;
}

/* Стартовый экран: описание, правила, управление */
.start-info {
  width: 100%;
  max-width: 100%;
  margin: 0 0 1.25rem;
  padding: 16px 18px 18px;
  border-radius: 18px;
  text-align: left;
  align-self: stretch;
}

.start-info__section {
  margin-bottom: 1rem;
}

.start-info__section:last-child {
  margin-bottom: 0;
}

.start-info__h {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent-soft);
}

.start-info__p {
  margin: 0;
  font-size: 0.85rem;
  line-height: 1.55;
  color: var(--text-secondary);
}

.start-info__list {
  margin: 0;
  padding: 0 0 0 1.15rem;
  font-size: 0.85rem;
  line-height: 1.55;
  color: var(--text-secondary);
}

.start-info__list li {
  margin-bottom: 0.4rem;
}

.start-info__list li:last-child {
  margin-bottom: 0;
}

.start-info__list strong {
  color: var(--text-main);
  font-weight: 600;
}

.start-info__kbd {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.start-info__list--controls .start-info__kbd {
  vertical-align: middle;
}

.microcopy {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin: 0.5rem 0 0;
  letter-spacing: 0.01em;
}

/* --- Glass HUD --- */
.hud {
  width: 100%;
  max-width: 100%;
  padding: 12px 16px 14px;
  border-radius: 18px;
  margin-bottom: 14px;
}

.glass {
  background: var(--card-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

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

.hud__item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.hud__label {
  font-size: 0.7rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.hud__value {
  font-size: 1.25rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
}

.hud__actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.icon-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
  background: rgba(201, 169, 110, 0.12);
}

.icon-button:active {
  transform: scale(0.95);
}

.icon-pause,
.icon-restart {
  display: block;
  width: 16px;
  height: 16px;
  position: relative;
}

.icon-pause::before,
.icon-pause::after {
  content: "";
  position: absolute;
  top: 0;
  width: 4px;
  height: 16px;
  background: var(--text-main);
  border-radius: 1px;
}
.icon-pause::before {
  left: 1px;
}
.icon-pause::after {
  right: 1px;
}

.icon-restart {
  border: 2px solid var(--text-main);
  border-radius: 50%;
  border-top-color: transparent;
}

/* --- Board --- */
.board-wrap {
  width: 100%;
  max-width: min(100%, 400px);
  margin: 0 auto;
}

.game-board {
  position: relative;
  width: 100%;
  max-width: 100%;
  max-height: min(60dvh, 400px);
  aspect-ratio: 1;
  height: auto;
  box-sizing: border-box;
  touch-action: none;
  outline: none;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--r-card);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35), inset 0 0 0 1px rgba(255, 255, 255, 0.04),
    inset 0 1px 40px rgba(201, 169, 110, 0.04);
  overflow: hidden;
  transition: box-shadow 0.4s ease;
}

.game-board.is-flash {
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35), inset 0 0 50px rgba(201, 169, 110, 0.25);
}

.game-board.is-shake {
  animation: boardShake 0.55s ease;
}

.game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  vertical-align: top;
  border-radius: calc(var(--r-card) - 2px);
  cursor: crosshair;
  outline: none;
  touch-action: none;
}

.game-board:focus-visible {
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35), inset 0 0 0 1px rgba(255, 255, 255, 0.04),
    inset 0 1px 40px rgba(201, 169, 110, 0.04), 0 0 0 2px rgba(201, 169, 110, 0.35);
}

@keyframes boardShake {
  0%,
  100% {
    transform: translate(0, 0);
  }
  20% {
    transform: translate(-4px, 2px);
  }
  40% {
    transform: translate(4px, -2px);
  }
  60% {
    transform: translate(-3px, -2px);
  }
  80% {
    transform: translate(3px, 1px);
  }
}

.board-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  padding: 20px 16px;
  text-align: center;
  background: rgba(12, 14, 18, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 2;
  animation: overlayIn 0.35s ease;
}

.board-overlay[hidden] {
  display: none;
}

@keyframes overlayIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.overlay__title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text-main);
}

.overlay__title--lg {
  font-size: clamp(1.35rem, 4vw, 1.6rem);
}

.overlay__text {
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.45;
  color: var(--text-secondary);
  max-width: 26ch;
}

.overlay__stats {
  display: flex;
  gap: 20px;
  margin: 0.4rem 0 0.2rem;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 100px;
}

.stat__label {
  font-size: 0.7rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stat__value {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--accent-soft);
  font-variant-numeric: tabular-nums;
}

.overlay__btns {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 240px;
  margin-top: 0.5rem;
}

.overlay__btns--pause {
  max-width: 260px;
  margin-top: 0.25rem;
}

/* --- Touch pad (mobile) --- */
.touch-pad {
  display: none;
  width: 100%;
  max-width: 280px;
  margin: 1rem auto 0;
  user-select: none;
}

.touch-pad--visible {
  display: block;
}

.touch-pad__row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  margin-bottom: 6px;
}

.d-pad {
  min-height: 52px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-main);
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.d-pad:hover,
.d-pad:focus-visible {
  background: rgba(201, 169, 110, 0.15);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.d-pad:active {
  transform: scale(0.95);
}

.d-pad--center {
  min-height: 8px;
  background: transparent;
  border: none;
  pointer-events: none;
}

/* --- Buttons --- */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 50px;
  padding: 0 28px;
  width: 100%;
  max-width: 300px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: linear-gradient(135deg, #c9a96e, #e7d3a7);
  color: #0f1115;
  border-radius: var(--r-control);
  transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease;
}

.button:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 30px rgba(201, 169, 110, 0.25);
}

.button:active {
  transform: scale(0.97);
}

.button--ghost {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: none;
}

.button--ghost:hover {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  transform: translateY(-1px);
}

/* --- Animations --- */
.fade-in {
  animation: fadeInUp 0.65s ease both;
}

.view--game .fade-in {
  animation-delay: 0.1s;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* --- Media --- */
@media (min-width: 600px) {
  .touch-pad--visible {
    display: none;
  }

  .app:has(#viewGame:not([hidden])) {
    min-height: 100dvh;
    width: 100%;
    max-width: none;
    padding: 0 16px 20px;
    box-sizing: border-box;
    align-items: stretch;
    justify-content: flex-start;
  }

  .view--game {
    max-width: none;
    width: 100%;
    min-height: 100dvh;
    flex: 1;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
  }

  .view--game .view__inner--game {
    width: 100%;
    max-width: none;
    min-height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    padding: 16px 12px 24px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
    gap: 0.5rem;
  }

  .view--game .hud {
    flex-shrink: 0;
    width: 100%;
    max-width: min(1200px, 100%);
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 0.5rem;
  }

  .view--game .board-wrap {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    min-width: 0;
    max-width: none;
    width: 100%;
  }

  /* Квадрат максимального размера: умещается в окно (HUD + поля) */
  .view--game .game-board {
    --game-side: min(100vw - 2.5rem, 100dvh - 9rem, 95vmin);
    width: var(--game-side);
    max-width: var(--game-side);
    max-height: var(--game-side);
    height: var(--game-side);
  }

  /* Стартовый экран с описанием — на весь экран, как игра */
  .app:has(#viewStart:not([hidden])) {
    min-height: 100dvh;
    width: 100%;
    max-width: none;
    padding: 0 24px 32px;
    box-sizing: border-box;
    align-items: stretch;
    justify-content: flex-start;
  }

  .view--start {
    max-width: none;
    width: 100%;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    margin: 0;
    min-height: 100dvh;
    max-height: 100dvh;
    overflow-y: auto;
    overflow-x: hidden;
  }

  .view--start .view__inner--start {
    max-width: min(1100px, 100%);
    width: 100%;
    margin: 0 auto;
    flex: 0 0 auto;
    min-height: min-content;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 32px 16px 40px;
    box-sizing: border-box;
  }

  .view--start .start-info {
    max-width: 100%;
  }
}

@media (max-width: 599px) {
  .hud {
    padding: 10px 12px 12px;
  }

  .hud__value {
    font-size: 1.15rem;
  }

  .app {
    min-height: 100dvh;
    min-height: 100svh;
    padding: 8px 10px calc(8px + var(--safe-bottom));
    padding-top: max(8px, env(safe-area-inset-top, 0px));
    align-items: stretch;
    justify-content: flex-start;
    max-width: 100%;
    box-sizing: border-box;
  }

  .view--start {
    max-width: 100%;
  }

  .view--game {
    max-width: 100%;
    min-height: 0;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .view--game .view__inner--game {
    width: 100%;
    max-width: 100%;
    min-height: 0;
    max-height: 100dvh;
    max-height: 100svh;
    padding: 4px 6px 6px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin: 0;
    flex: 1 1 auto;
  }

  .view--game .hud {
    flex-shrink: 0;
    width: 100%;
  }

  .view--game .board-wrap {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    max-width: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Квадрат: сторона = минимум из ширины экрана и оставшейся высоты под HUD и D-pad */
  .view--game .game-board {
    --m-side: min(calc(100vw - 1.25rem), calc(100svh - 13rem - env(safe-area-inset-bottom, 0px)));
    width: var(--m-side);
    max-width: var(--m-side);
    max-height: var(--m-side);
    height: var(--m-side);
    margin: 0 auto;
  }

  .touch-pad--visible {
    display: block;
    flex-shrink: 0;
  }
}

/* --- Встраивание в iframe (портфолио): цепочка 100% + без 100dvh, без полосы прокрутки у document --- */
html.is-embed {
  height: 100%;
  overflow: hidden;
  scrollbar-gutter: auto;
}

html.is-embed body {
  margin: 0;
  min-height: 0 !important;
  height: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
}

/* Перебиваем body:has(...) с 100dvh */
@media (min-width: 600px) {
  html.is-embed body:has(#viewGame:not([hidden])),
  html.is-embed body:has(#viewStart:not([hidden])) {
    height: 100% !important;
    max-height: 100% !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }
}

html.is-embed .app {
  min-height: 0 !important;
  height: 100% !important;
  max-height: 100% !important;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  padding: 6px 10px 10px;
  container-type: size;
  container-name: app;
}

@media (min-width: 600px) {
  html.is-embed .app:has(#viewStart:not([hidden])),
  html.is-embed .app:has(#viewGame:not([hidden])) {
    min-height: 0 !important;
    height: 100% !important;
    max-height: 100% !important;
    padding: 6px 12px 10px;
    align-items: stretch;
    justify-content: flex-start;
  }

  /* Старт: влезаем в iframe — компактная типографика, без max-height+scroll */
  html.is-embed .view--start {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 0 !important;
    max-height: 100% !important;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin: 0;
  }

  html.is-embed .view--start .view__inner--start {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4px 6px 8px;
    max-width: min(100%, 480px);
    width: 100%;
    justify-content: center;
    font-size: 0.92em;
  }

  html.is-embed .title--hero {
    font-size: clamp(1.25rem, 3.8vw, 1.7rem) !important;
  }

  html.is-embed .lede {
    font-size: clamp(0.8rem, 2vw, 0.9rem) !important;
    margin: 0 0 0.5rem;
    line-height: 1.4;
  }

  html.is-embed .start-info {
    padding: 8px 10px 10px;
    margin: 0 0 0.4rem;
    flex: 0 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  html.is-embed .start-info__section {
    margin-bottom: 0.4rem;
  }

  html.is-embed .start-info__h {
    font-size: 0.7rem;
  }

  html.is-embed .start-info__p,
  html.is-embed .start-info__list {
    font-size: 0.75rem;
    line-height: 1.4;
  }

  html.is-embed .start-info__list {
    padding-left: 0.9rem;
  }

  html.is-embed .button {
    min-height: 44px;
    padding: 0 20px;
    flex-shrink: 0;
  }

  html.is-embed .microcopy {
    font-size: 0.72rem;
    margin: 0.3rem 0 0;
  }

  html.is-embed .view--game {
    min-height: 0 !important;
    max-height: 100% !important;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  html.is-embed .view--game .view__inner--game {
    min-height: 0 !important;
    max-height: 100% !important;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    padding: 6px 8px 8px;
    overflow: hidden;
    box-sizing: border-box;
  }

  html.is-embed .view--game .hud {
    flex-shrink: 0;
  }

  html.is-embed .view--game .board-wrap {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* Поле: квадрат по меньшей стороне контейнера .app (iframe) */
  html.is-embed .view--game .game-board {
    --game-side: min(100cqi, 100cqb, 95vmin, 400px) !important;
  }
}

@supports not (width: 1cqi) {
  @media (min-width: 600px) {
    html.is-embed .view--game .game-board {
      --game-side: min(
        calc(100vw - 2.5rem),
        calc(100vh - 7rem),
        95vmin,
        400px
      ) !important;
    }
  }
}

@media (max-width: 599px) {
  html.is-embed {
    height: 100%;
    overflow: hidden;
  }

  html.is-embed body {
    min-height: 0 !important;
    height: 100% !important;
    max-height: 100% !important;
    overflow: hidden !important;
  }

  html.is-embed .app {
    min-height: 0 !important;
    height: 100% !important;
    max-height: 100% !important;
    padding: 4px 6px calc(4px + var(--safe-bottom));
  }

  html.is-embed .view--start,
  html.is-embed .view--game {
    max-height: 100%;
    overflow: hidden;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
