:root {
  color-scheme: dark;
  --bg: #09111b;
  --panel: rgba(16, 24, 34, .94);
  --panel-2: rgba(22, 31, 42, .94);
  --line: rgba(255, 255, 255, .1);
  --gold: #f7b72d;
  --text: #f5f7fa;
  --muted: #aab4c0;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-height: 100%; background: var(--bg); }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 20% 0%, rgba(60, 105, 150, .2), transparent 38%),
    linear-gradient(180deg, #111c29, #070c12 75%);
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app {
  width: min(1440px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 12px;
  padding: 12px;
}
.game-column, .leaderboard {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  box-shadow: 0 18px 55px rgba(0,0,0,.3);
  overflow: hidden;
}
.game-column { min-width: 0; display: flex; flex-direction: column; }
.topbar, .hud, .tips { flex: 0 0 auto; }
.topbar {
  min-height: 62px;
  padding: 12px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(27,39,54,.98), rgba(14,22,31,.98));
}
.brand { font-size: clamp(20px, 2.4vw, 34px); font-weight: 950; letter-spacing: -.04em; }
.brand span { color: var(--gold); }
.online { color: #70dc97; font-weight: 800; font-size: 13px; }
.online i { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #48d681; box-shadow: 0 0 14px #48d681; }
.hud {
  min-height: 62px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-bottom: 1px solid var(--line);
  background: rgba(8,13,20,.78);
}
.hud > div { display: grid; place-items: center; align-content: center; padding: 8px; }
.hud > div + div { border-left: 1px solid var(--line); }
.hud small { color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .08em; }
.hud strong { margin-top: 2px; font-size: clamp(22px, 3vw, 42px); line-height: 1; }

.canvas-wrap {
  position: relative;
  width: 100%;
  min-height: 320px;
  height: clamp(320px, 58vh, 660px);
  flex: 1 1 auto;
  overflow: hidden;
  background: #83acc5;
  touch-action: none;
}
#game { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }
.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  place-items: center;
  align-content: center;
  gap: 12px;
  padding: 22px;
  text-align: center;
  overflow-y: auto;
  background: linear-gradient(180deg, rgba(6,12,19,.68), rgba(5,9,14,.86));
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.overlay.visible { display: grid; }
.overlay h1, .overlay h2 { margin: 0; text-shadow: 0 3px 20px rgba(0,0,0,.5); }
.overlay h1 { font-size: clamp(25px, 4vw, 52px); }
.overlay p { margin: 0; color: #d2dae2; }
.characters { display: flex; gap: 10px; }
.character {
  min-width: 118px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 13px;
  color: var(--text);
  background: rgba(20,29,40,.9);
}
.character.active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(247,183,45,.2); }
.avatar { display: block; font-size: 34px; }
.name-label { width: min(360px, 100%); display: grid; gap: 6px; color: var(--muted); font-size: 12px; font-weight: 900; text-align: left; }
.name-label input { width: 100%; min-height: 44px; padding: 10px 13px; border: 1px solid var(--line); border-radius: 11px; color: #fff; background: rgba(5,10,16,.85); outline: none; }
.name-label input:focus { border-color: var(--gold); }
.play, .secondary, .secondary-btn {
  min-height: 44px;
  padding: 10px 22px;
  border-radius: 11px;
  font-weight: 950;
  cursor: pointer;
}
.play { border: 0; color: #1b1406; background: linear-gradient(180deg,#ffd66b,#efa519); box-shadow: 0 9px 26px rgba(238,166,25,.25); }
.secondary, .secondary-btn { border: 1px solid var(--line); color: #fff; background: rgba(24,34,46,.92); }
.message { min-height: 20px; color: #ff9696 !important; }
.final-score { font-size: clamp(36px, 7vw, 78px); font-weight: 950; color: var(--gold); }
.tips {
  min-height: 38px;
  padding: 8px 14px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 850;
  border-top: 1px solid var(--line);
}

.leaderboard { padding: 18px; background: var(--panel-2); }
.leader-title { display: flex; justify-content: space-between; align-items: center; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.leader-title small { color: var(--muted); font-weight: 850; }
.leader-title h2 { margin: 0; color: var(--gold); font-size: 34px; }
#refreshBtn { width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 50%; color: #fff; background: rgba(255,255,255,.06); font-size: 23px; }
#leaders { margin: 12px 0 0; padding: 0; list-style: none; }
.leader-row { display: grid; grid-template-columns: 24px 32px minmax(0,1fr) auto; gap: 7px; align-items: center; min-height: 43px; border-bottom: 1px solid rgba(255,255,255,.06); }
.rank { color: var(--gold); font-weight: 950; }
.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.points { font-weight: 900; }
.empty { padding: 30px 8px; color: var(--muted); text-align: center; }
.leader-note { color: var(--muted); font-size: 12px; line-height: 1.45; }

.game-notice { position: fixed; top: max(18px,var(--safe-top)); left: 50%; z-index: 1000; transform: translate(-50%,-12px); opacity: 0; pointer-events: none; padding: 11px 18px; border: 1px solid #75dfff; border-radius: 999px; color: #fff; font-weight: 900; background: rgba(18,95,145,.96); transition: .18s ease; }
.game-notice.visible { transform: translate(-50%,0); opacity: 1; }
.social-game-buttons { width: min(470px,100%); display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.social-game-buttons button { min-height: 43px; border: 1px solid var(--line); border-radius: 10px; color: #fff; font-weight: 850; background: #253140; }
.share-modal, .ios-install-help { position: fixed; inset: 0; z-index: 5000; display: grid; place-items: center; padding: 20px; background: rgba(3,7,12,.86); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.share-modal[hidden], .ios-install-help[hidden] { display: none; }
.share-modal-card, .ios-install-card { position: relative; width: min(500px,100%); padding: 26px 22px; border: 1px solid rgba(247,183,45,.45); border-radius: 20px; background: #111a25; box-shadow: 0 24px 80px rgba(0,0,0,.55); }
.share-modal-close, .ios-install-close { position: absolute; top: 7px; right: 10px; border: 0; color: #fff; font-size: 29px; background: transparent; }
.share-modal-link { display: block; margin: 12px 0; padding: 12px; overflow-wrap: anywhere; border: 1px solid rgba(90,180,255,.45); border-radius: 10px; color: #78c8ff; background: rgba(36,104,160,.16); }
.share-modal-actions { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; }
.share-modal-actions button, .ios-install-button { min-height: 44px; border: 1px solid var(--line); border-radius: 10px; color: #fff; font-weight: 850; background: #263443; }
.ios-install-card { text-align: center; }
.ios-install-icon { font-size: 44px; }

/* During a run, only scrolling/gestures are locked. Layout is not transformed. */
html.run-active, body.run-active { overflow: hidden; overscroll-behavior: none; }
body.run-active { touch-action: none; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .leaderboard { min-height: 300px; }
}

/* Phone landscape and installed iPhone PWA: fit the whole game without clipping. */
@media (orientation: landscape) and (max-height: 650px) {
  html, body { height: 100%; min-height: 100%; overflow: hidden; }
  body { padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left); }
  .app {
    width: 100%;
    height: calc(100dvh - var(--safe-top) - var(--safe-bottom));
    min-height: 0;
    padding: 4px;
    gap: 5px;
    grid-template-columns: minmax(0,1fr) minmax(170px,23vw);
  }
  .game-column, .leaderboard { border-radius: 10px; }
  .game-column { height: 100%; min-height: 0; }
  .topbar { min-height: 38px; padding: 5px 10px; }
  .brand { font-size: 21px; }
  .online { font-size: 10px; }
  .hud { min-height: 40px; }
  .hud small { font-size: 8px; }
  .hud strong { font-size: 22px; }
  .canvas-wrap { min-height: 0; height: auto; flex: 1 1 0; }
  .tips { min-height: 24px; padding: 4px 9px; font-size: 8px; }
  .leaderboard { height: 100%; padding: 8px; overflow-y: auto; }
  .leader-title { padding-bottom: 5px; }
  .leader-title h2 { font-size: 23px; }
  #refreshBtn { width: 30px; height: 30px; font-size: 18px; }
  .leader-row { min-height: 29px; grid-template-columns: 17px 24px minmax(0,1fr) auto; gap: 3px; font-size: 10px; }
  .leader-note { display: none; }
  .overlay { padding: 8px; gap: 6px; }
  .overlay h1 { font-size: 24px; }
  .overlay p { font-size: 10px; }
  .characters { gap: 6px; }
  .character { min-width: 90px; padding: 4px; font-size: 10px; }
  .avatar { font-size: 22px; }
  .name-label { width: 250px; gap: 2px; font-size: 8px; }
  .name-label input { min-height: 32px; padding: 5px 8px; }
  .play, .secondary, .secondary-btn { min-height: 32px; padding: 5px 15px; font-size: 11px; }
  .message { min-height: 12px; font-size: 9px; }
  .social-game-buttons { max-width: 360px; }
  .social-game-buttons button { min-height: 30px; font-size: 9px; }
}

@media (display-mode: standalone) {
  #fullscreenBtn { display: none; }
}

@media (max-width: 560px) and (orientation: portrait) {
  .app { padding: 6px; }
  .canvas-wrap { height: 56dvh; min-height: 360px; }
  .social-game-buttons, .share-modal-actions { grid-template-columns: 1fr; }
}

/* Final mobile layout: keep menu intact, show only game during a run. */
html, body { min-height: 100%; }
body.run-active { overflow: hidden; overscroll-behavior: none; touch-action: none; }

@media (orientation: landscape) and (max-height: 700px) {
  body.run-active { padding: 0 !important; }
  body.run-active .app {
    position: fixed;
    inset: 0;
    z-index: 100;
    width: 100%;
    height: 100svh;
    min-height: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    display: block;
    overflow: hidden;
    background: #09111b;
  }
  body.run-active .leaderboard,
  body.run-active .topbar,
  body.run-active .tips { display: none !important; }
  body.run-active .game-column {
    width: 100%;
    height: 100%;
    min-height: 0;
    border: 0;
    border-radius: 0;
    display: flex;
    flex-direction: column;
  }
  body.run-active .hud {
    flex: 0 0 48px;
    min-height: 48px;
  }
  body.run-active .hud small { font-size: 8px; }
  body.run-active .hud strong { font-size: 22px; }
  body.run-active .canvas-wrap {
    position: relative;
    flex: 1 1 auto;
    width: 100%;
    height: auto;
    min-height: 0;
    overflow: hidden;
  }
  body.run-active #game {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
  }
}

@media (display-mode: standalone) {
  #fullscreenBtn { display: none !important; }
}

/* Allow mobile Safari to collapse its browser interface */
html {
    width: 100%;
    min-height: calc(100% + 2px);
    overflow-x: hidden;
    overflow-y: auto;
}

body {
    width: 100%;
    min-height: calc(100vh + 2px);
    margin: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* ===== Адаптация игры под мобильный Safari ===== */

.mobile-play-hint {
    position: fixed;
    left: 50%;
    bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 10000;
    transform: translateX(-50%);
    max-width: calc(100% - 24px);
    padding: 10px 16px;
    border-radius: 999px;
    color: #fff;
    background: rgba(5, 10, 16, 0.92);
    box-shadow: 0 8px 30px rgba(0,0,0,.45);
    font-size: 13px;
    font-weight: 800;
    text-align: center;
    pointer-events: none;
    opacity: 0;
    transition: opacity .25s ease;
}

.mobile-play-hint.visible {
    opacity: 1;
}

@media (orientation: landscape) and (max-height: 750px) {
    html,
    body {
        width: 100%;
        min-height: 100%;
        margin: 0;
    }

    body.run-active {
        padding: 0 !important;
        overflow: hidden;
        background: #09111b;
    }

    body.run-active .app {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100dvh;
        min-height: 0;
        padding: 0;
        display: block;
        overflow: hidden;
        background: #09111b;
    }

    body.run-active .leaderboard,
    body.run-active .topbar,
    body.run-active .tips {
        display: none !important;
    }

    body.run-active .game-column {
        width: 100%;
        height: 100%;
        min-height: 0;
        border: 0;
        border-radius: 0;
        display: flex;
        flex-direction: column;
    }

    body.run-active .hud {
        flex: 0 0 44px;
        min-height: 44px;
    }

    body.run-active .hud small {
        font-size: 8px;
    }

    body.run-active .hud strong {
        font-size: 21px;
    }

    body.run-active .canvas-wrap {
        position: relative;
        flex: 1 1 0;
        width: 100%;
        height: 0;
        min-height: 0;
        overflow: hidden;
    }

    body.run-active #game {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
    }
}

/* Разрешаем Safari свернуть адресную строку после поворота */
@media (orientation: landscape) and (pointer: coarse) {
    html {
        width: 100%;
        height: auto !important;
        min-height: calc(100dvh + 80px) !important;
        overflow-x: hidden !important;
        overflow-y: scroll !important;
    }

    body {
        position: static !important;
        width: 100%;
        height: auto !important;
        min-height: calc(100dvh + 80px) !important;
        margin: 0;
        overflow-x: hidden !important;
        overflow-y: visible !important;
    }

    body.run-active .app {
        position: sticky !important;
        top: 0;
        width: 100%;
        height: 100dvh !important;
        min-height: 100dvh !important;
        overflow: hidden;
    }
}

/* Верхний буфер для сворачивания адресной строки Safari */
.safari-scroll-space {
    display: none;
}

@media (orientation: landscape) and (pointer: coarse) {
    html,
    body {
        height: auto !important;
        min-height: calc(100dvh + 120px) !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
    }

    body {
        position: static !important;
    }

    .safari-scroll-space {
        display: block;
        width: 100%;
        height: 100px;
        min-height: 100px;
        pointer-events: none;
    }

    .app {
        position: relative !important;
        min-height: 100dvh;
    }
}

/* Исправление: убираем пустоту над игрой */
.safari-scroll-space {
    display: none !important;
    height: 0 !important;
    min-height: 0 !important;
}

/* Запас для прокрутки размещаем под игрой */
@media (orientation: landscape) and (pointer: coarse) {
    html,
    body {
        height: auto !important;
        min-height: 100dvh !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
    }

    body {
        position: static !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    body::after {
        content: "";
        display: block;
        width: 100%;
        height: 100px;
        pointer-events: none;
    }

    .app {
        margin-top: 0 !important;
        top: auto !important;
        transform: none !important;
    }

    body.run-active .app {
        position: relative !important;
        top: auto !important;
        height: 100dvh !important;
        min-height: 100dvh !important;
    }
}

/* Область для сворачивания панелей Safari */
#safari-swipe-zone {
    display: none;
}

@media (orientation: landscape) and (pointer: coarse) {
    html,
    body {
        height: auto !important;
        min-height: calc(100dvh + 140px) !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior-y: auto !important;
    }

    body::after {
        content: "";
        display: block;
        width: 100%;
        height: 140px;
        pointer-events: none;
    }

    #safari-swipe-zone {
        position: fixed;
        display: flex;
        top: 0;
        left: 50%;
        z-index: 999999;
        transform: translateX(-50%);
        align-items: center;
        justify-content: center;
        width: min(420px, 70vw);
        height: 42px;
        padding-top: env(safe-area-inset-top);
        border-radius: 0 0 16px 16px;
        color: #fff;
        background: rgba(5, 13, 22, 0.88);
        font: 700 13px/1.2 Arial, sans-serif;
        letter-spacing: .04em;
        text-align: center;
        touch-action: pan-y !important;
        -webkit-user-select: none;
        user-select: none;
        transition: opacity .25s ease;
    }

    #safari-swipe-zone.hidden {
        opacity: 0;
        pointer-events: none;
    }
}

/* =====================================================
   Адаптивная раскладка: ТОП-10 под игрой на телефонах
   ===================================================== */

@media (max-width: 900px) {
    html,
    body {
        width: 100%;
        height: auto !important;
        min-height: 100%;
        margin: 0;
        overflow-x: hidden;
        overflow-y: auto !important;
    }

    /* Главный блок становится вертикальным */
    .app {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        max-width: none !important;
        height: auto !important;
        min-height: 100dvh;
        margin: 0 !important;
        padding: 0 !important;
        gap: 12px !important;
        overflow: visible !important;
    }

    /* Игра всегда первой */
    .game-column {
        order: 1;
        width: 100% !important;
        max-width: none !important;
        min-width: 0 !important;
        height: auto;
        margin: 0 !important;
        border-radius: 0 !important;
    }

    /* Рейтинг переносится под игру */
    .leaderboard {
        order: 2;
        display: block !important;
        position: relative !important;
        inset: auto !important;
        width: calc(100% - 24px) !important;
        max-width: none !important;
        height: auto !important;
        max-height: none !important;
        margin: 0 12px 24px !important;
        padding: 14px !important;
        overflow: visible !important;
        border-radius: 16px !important;
        transform: none !important;
    }

    .leaderboard-list,
    .leaderboard ol,
    .leaderboard ul {
        max-height: none !important;
        overflow: visible !important;
    }

    /* Верхнее меню не должно занимать лишнее место */
    .topbar {
        width: 100%;
        box-sizing: border-box;
    }
}

/* Горизонтальное положение телефона */
@media (max-width: 900px) and (orientation: landscape) {
    body {
        min-height: calc(100dvh + 120px) !important;
    }

    .app {
        min-height: 100dvh;
    }

    .game-column {
        min-height: 100dvh;
    }

    /* Игровая область занимает доступную высоту */
    .canvas-wrap {
        min-height: 0 !important;
    }

    /*
      Во время игры ТОП-10 остаётся ниже экрана.
      Пользователь увидит его после прокрутки вниз.
    */
    body.run-active .leaderboard {
        display: block !important;
        position: relative !important;
        width: calc(100% - 24px) !important;
        margin: 12px !important;
    }

    body.run-active .app {
        position: relative !important;
        inset: auto !important;
        height: auto !important;
        min-height: 100dvh !important;
        overflow: visible !important;
    }
}

/* На больших экранах сохраняем рейтинг справа */
@media (min-width: 901px) {
    .game-column {
        order: initial;
    }

    .leaderboard {
        order: initial;
    }
}
