@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&display=swap');

:root {
  --accent: #00f5ff;
  --glow: rgba(0, 245, 255, 0.14);
  --panel-bg: rgba(6, 8, 24, 0.72);
  --panel-border: rgba(0, 255, 255, 0.18);
  --panel-w: clamp(128px, 14vw, 188px);
  --gap: clamp(10px, 1.6vw, 26px);
  --radius: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

body {
  background: #05050f;
  font-family: 'Orbitron', sans-serif;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

#overlay { height: 100%; }
@supports (height: 100dvh) {
  #overlay { height: 100dvh; }
}

#world-canvas {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 0;
  display: block;
}

#ornament-canvas {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 1;
  pointer-events: none;
}

#world-fade {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 30;
  pointer-events: none;
  opacity: 0;
  background: #fff;
  transition: opacity 0.35s ease;
}

#world-fade.on { opacity: 0.85; }

/* ============================ HUD ============================ */
.game-wrapper {
  position: relative;
  z-index: 2;
  display: flex;
  gap: var(--gap);
  align-items: center;
  justify-content: center;
  transform-origin: center center;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.4vh, 16px);
  width: var(--panel-w);
  flex: 0 0 auto;
}

.panel-box {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: clamp(7px, 1.4vh, 13px) clamp(8px, 1.2vw, 14px);
  text-align: center;
  backdrop-filter: blur(10px);
  box-shadow: 0 0 24px rgba(0, 255, 255, 0.06), inset 0 0 24px rgba(0, 255, 255, 0.03);
  position: relative;
  overflow: hidden;
}

.panel-box::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent, #e63946), #fff, var(--accent, #e63946));
  opacity: 0.55;
  transition: background 0.8s;
}

.panel-box h3 {
  color: rgba(0, 255, 255, 0.72);
  font-size: clamp(8px, 1vh, 10px);
  letter-spacing: 3px;
  margin-bottom: clamp(4px, 0.8vh, 8px);
  text-transform: uppercase;
}

.panel-box .value {
  color: #fff;
  font-size: clamp(16px, 2.6vh, 24px);
  font-weight: 900;
  text-shadow: 0 0 16px var(--accent, rgba(0, 255, 255, 0.55));
  transition: text-shadow 0.5s;
}

.panel-box .value.small { font-size: clamp(9px, 1.4vh, 12px); letter-spacing: 1px; }

.panel-box .value.bump { animation: bump 0.25s ease-out; }

@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

.next-box .value { line-height: 1; }
#next-canvas { display: block; margin: 0 auto; width: 132px; height: 108px; }

.trait-badge {
  --trait: #00f5ff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-size: clamp(8px, 1.1vh, 10px);
  letter-spacing: 1px;
  color: #fff;
  background: color-mix(in srgb, var(--trait) 18%, transparent);
  border: 1px solid color-mix(in srgb, var(--trait) 55%, transparent);
  border-radius: 6px;
  padding: clamp(4px, 0.8vh, 7px) 8px;
  text-shadow: 0 0 10px var(--trait);
  box-shadow: 0 0 14px color-mix(in srgb, var(--trait) 35%, transparent);
  transition: all 0.25s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.trait-badge .t-icon { font-size: 14px; color: var(--trait); }

.hud-btn {
  font-family: inherit;
  cursor: pointer;
  color: rgba(0, 255, 255, 0.85);
  background: rgba(6, 8, 24, 0.6);
  border: 1px solid rgba(0, 255, 255, 0.25);
  border-radius: 8px;
  padding: clamp(5px, 0.9vh, 8px);
  font-size: clamp(9px, 1.2vh, 11px);
  letter-spacing: 2px;
  transition: background 0.2s, box-shadow 0.2s;
}

.hud-btn:hover { background: rgba(0, 255, 255, 0.12); box-shadow: 0 0 16px rgba(0, 255, 255, 0.25); }

.game-board {
  position: relative;
  flex: 0 0 auto;
  border: 2px solid rgba(0, 255, 255, 0.28);
  border-radius: 7px;
  box-shadow: 0 0 50px var(--glow, rgba(0, 255, 255, 0.12)), inset 0 0 50px rgba(0, 0, 0, 0.55);
  background: rgba(4, 5, 16, 0.82);
  transition: box-shadow 0.6s;
  touch-action: none;
}

.game-board::before,
.game-board::after {
  content: '';
  position: absolute;
  pointer-events: none;
  z-index: 8;
  height: 10px;
  left: -14px; right: -14px;
  opacity: 0.65;
  background: repeating-conic-gradient(var(--accent, #e63946) 0% 25%, #fff 0% 50%) 0 0 / 14px 10px;
  transition: background 0.8s;
}

.game-board::before { top: -15px; border-radius: 3px 3px 0 0; }
.game-board::after { bottom: -15px; border-radius: 0 0 3px 3px; }

#game-canvas { display: block; border-radius: 5px; }

#particle-canvas {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
  z-index: 5;
  border-radius: 5px;
}

.kbd-hint {
  color: rgba(0, 255, 255, 0.72);
  font-size: clamp(8px, 1.05vh, 9px);
  letter-spacing: 1px;
  line-height: 2.05;
  text-align: left;
}

.side-panel .kbd-hint {
  background: rgba(4, 6, 18, 0.55);
  border: 1px solid rgba(0, 255, 255, 0.12);
  border-radius: 9px;
  padding: 7px 10px;
  backdrop-filter: blur(8px);
}

.kbd-hint.touch-hint { display: none; }

@media (hover: hover) and (pointer: fine) {
  .hud-btn { display: none; }
}

#sound-state { font-size: clamp(9px, 1.3vh, 11px); letter-spacing: 2px; color: rgba(0, 255, 255, 0.75); }

.sound-box { cursor: pointer; transition: border-color 0.2s, box-shadow 0.2s; }
.sound-box:hover { border-color: rgba(0, 255, 255, 0.5); box-shadow: 0 0 24px rgba(0, 255, 255, 0.2); }

/* ---------- Экранные кнопки управления (тач) ---------- */
.touch-pad { display: none; }
.pad-group { display: flex; gap: 10px; pointer-events: auto; }

.tbtn {
  font-family: inherit;
  font-size: 24px;
  line-height: 1;
  color: rgba(255, 255, 255, 0.92);
  background: linear-gradient(180deg, rgba(16, 22, 48, 0.9), rgba(6, 9, 24, 0.92));
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45), 0 0 18px color-mix(in srgb, var(--accent) 12%, transparent);
  touch-action: none;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s, background 0.12s, box-shadow 0.12s;
  width: 62px;
  height: 62px;
}

.tbtn.sub { width: 54px; height: 54px; font-size: 20px; opacity: 0.9; }

.tbtn.drop {
  width: 68px;
  height: 68px;
  font-size: 27px;
  border-color: rgba(255, 220, 140, 0.55);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5), 0 0 22px rgba(255, 200, 90, 0.22);
  background: linear-gradient(180deg, rgba(58, 42, 18, 0.92), rgba(20, 12, 6, 0.94));
}

.tbtn.active,
.tbtn:active {
  transform: scale(0.95);
  background: color-mix(in srgb, var(--accent) 30%, rgba(6, 9, 24, 0.9));
  box-shadow: 0 0 26px color-mix(in srgb, var(--accent) 55%, transparent);
}

.tbtn.drop.active,
.tbtn.drop:active {
  background: rgba(255, 190, 80, 0.35);
  box-shadow: 0 0 30px rgba(255, 200, 90, 0.6);
}

/* ============================ Overlay / меню ============================ */
#overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 3vh, 40px) clamp(10px, 3vw, 40px);
  background: radial-gradient(ellipse at 50% 30%, rgba(10, 20, 50, 0.55) 0%, rgba(3, 4, 14, 0.88) 70%);
  backdrop-filter: blur(7px);
  transition: opacity 0.3s;
  overflow: hidden;
}

#overlay.hidden { opacity: 0; pointer-events: none; }

.menu-card {
  width: min(940px, 94vw);
  max-height: 94vh;
  overflow-y: auto;
  scrollbar-width: thin;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.6vh, 16px);
  padding: clamp(14px, 3vh, 34px) clamp(14px, 3vw, 38px);
  border-radius: 20px;
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  background: linear-gradient(180deg, rgba(8, 12, 32, 0.92), rgba(4, 6, 18, 0.94));
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.03) inset,
    0 30px 80px rgba(0, 0, 0, 0.6),
    0 0 60px color-mix(in srgb, var(--accent) 18%, transparent);
  text-align: center;
}

.menu-head { display: flex; flex-direction: column; gap: 4px; }

#overlay h1.title {
  font-size: clamp(26px, 5.4vw, 54px);
  font-weight: 900;
  letter-spacing: clamp(4px, 1vw, 14px);
  line-height: 1.15;
  color: #fff;
  text-shadow:
    0 0 18px color-mix(in srgb, var(--accent) 90%, transparent),
    0 0 60px color-mix(in srgb, var(--accent) 55%, transparent),
    0 0 120px color-mix(in srgb, var(--accent) 30%, transparent);
  animation: titleGlow 3.2s ease-in-out infinite;
}

@keyframes titleGlow {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.18); }
}

#overlay h2.title {
  font-size: clamp(20px, 3.4vw, 34px);
  font-weight: 900;
  letter-spacing: 6px;
  color: #fff;
  text-shadow: 0 0 24px color-mix(in srgb, var(--accent) 75%, transparent);
}

#overlay .tagline {
  color: color-mix(in srgb, var(--accent) 85%, #fff);
  font-size: clamp(9px, 1.4vw, 12px);
  letter-spacing: clamp(3px, 0.8vw, 8px);
  opacity: 0.85;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  justify-content: center;
  align-items: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: clamp(9px, 1.3vw, 12px);
  letter-spacing: 2px;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  padding: 6px 14px;
}

.chip.gold {
  color: #ffd54a;
  border-color: rgba(255, 213, 74, 0.45);
  background: rgba(255, 213, 74, 0.08);
  text-shadow: 0 0 12px rgba(255, 213, 74, 0.6);
}

/* ---------- кнопки ---------- */
.btn {
  font-family: inherit;
  cursor: pointer;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 2px;
  font-weight: 700;
  font-size: clamp(10px, 1.5vw, 13px);
  padding: clamp(8px, 1.5vh, 13px) clamp(14px, 2.4vw, 26px);
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--accent) 50%, transparent);
  background: color-mix(in srgb, var(--accent) 12%, rgba(4, 6, 18, 0.75));
  box-shadow: 0 0 0 rgba(0, 0, 0, 0);
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease, border-color 0.2s;
}

.btn:hover {
  transform: translateY(-2px);
  background: color-mix(in srgb, var(--accent) 20%, rgba(4, 6, 18, 0.75));
  box-shadow: 0 10px 26px color-mix(in srgb, var(--accent) 25%, transparent);
}

.btn:active { transform: translateY(0) scale(0.98); }

.btn.primary {
  font-size: clamp(12px, 1.9vw, 16px);
  padding: clamp(11px, 2vh, 16px) clamp(28px, 5vw, 54px);
  border-color: color-mix(in srgb, var(--accent) 85%, #fff);
  background: linear-gradient(135deg,
    color-mix(in srgb, var(--accent) 82%, #fff) 0%,
    color-mix(in srgb, var(--accent) 45%, #06122a) 100%);
  color: #041018;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
  box-shadow: 0 8px 30px color-mix(in srgb, var(--accent) 45%, transparent), 0 0 0 1px rgba(255, 255, 255, 0.25) inset;
  animation: btnPulse 2.6s ease-in-out infinite;
}

@keyframes btnPulse {
  0%, 100% { box-shadow: 0 8px 30px color-mix(in srgb, var(--accent) 40%, transparent), 0 0 0 1px rgba(255, 255, 255, 0.25) inset; }
  50% { box-shadow: 0 8px 46px color-mix(in srgb, var(--accent) 70%, transparent), 0 0 0 1px rgba(255, 255, 255, 0.4) inset; }
}

.btn.ghost { background: transparent; border-color: rgba(255, 255, 255, 0.22); color: rgba(255, 255, 255, 0.75); }
.btn.ghost:hover { border-color: rgba(255, 255, 255, 0.5); box-shadow: none; }
.btn.small { font-size: clamp(9px, 1.2vw, 11px); padding: clamp(6px, 1vh, 9px) clamp(10px, 1.6vw, 16px); letter-spacing: 1px; }
.btn.off { opacity: 0.45; border-style: dashed; }

.btn-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; }
.toggles { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

/* ---------- статистика ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: 10px;
  width: min(560px, 100%);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 8px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.stat b { font-size: clamp(16px, 2.6vw, 24px); color: #fff; text-shadow: 0 0 14px color-mix(in srgb, var(--accent) 60%, transparent); }
.stat span { font-size: clamp(8px, 1.1vw, 10px); letter-spacing: 2px; color: rgba(255, 255, 255, 0.55); }

#overlay .final-score {
  color: #fff;
  font-size: clamp(18px, 3vw, 30px);
  font-weight: 900;
  text-shadow: 0 0 26px color-mix(in srgb, var(--accent) 70%, transparent);
}

#overlay .record {
  color: #ffd54a;
  font-size: clamp(11px, 1.6vw, 15px);
  letter-spacing: 3px;
  text-shadow: 0 0 18px rgba(255, 213, 74, 0.8);
  animation: pulse 1.2s ease-in-out infinite;
}

#overlay .sub {
  color: rgba(255, 255, 255, 0.6);
  font-size: clamp(9px, 1.3vw, 12px);
  letter-spacing: 3px;
}

/* ---------- черты ---------- */
.traits-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  width: 100%;
  margin-top: 2px;
}

.traits-grid .trait-card { flex: 0 1 292px; }

.trait-card {
  --trait: #00f5ff;
  display: flex;
  align-items: center;
  gap: 9px;
  text-align: left;
  border: 1px solid color-mix(in srgb, var(--trait) 40%, transparent);
  background: linear-gradient(135deg,
    color-mix(in srgb, var(--trait) 14%, rgba(0, 0, 0, 0.5)),
    color-mix(in srgb, var(--trait) 5%, rgba(0, 0, 0, 0.6)));
  border-radius: 9px;
  padding: 7px 10px;
  font-size: clamp(9px, 1.15vw, 11px);
  letter-spacing: 0.4px;
  transition: transform 0.15s, box-shadow 0.2s;
}

.trait-card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px color-mix(in srgb, var(--trait) 25%, transparent); }

.trait-card .t-icon { color: var(--trait); font-size: 16px; text-shadow: 0 0 10px var(--trait); }
.trait-card .t-name { color: #fff; font-weight: 700; display: block; }
.trait-card .t-desc { color: rgba(255, 255, 255, 0.6); display: block; line-height: 1.5; }

#overlay .kbd-hint { margin-top: 4px; text-align: center; color: rgba(255, 255, 255, 0.5); }
#overlay .kbd-hint b { color: var(--accent); }

/* ============================ Прочее ============================ */
#level-banner {
  position: fixed;
  top: 10%;
  left: 0; right: 0;
  z-index: 15;
  pointer-events: none;
  text-align: center;
  opacity: 0;
  padding: 0 12px;
}

#level-banner.show { animation: banner 2.4s ease-out; }

#level-banner .world-name {
  font-size: clamp(20px, 4vw, 34px);
  font-weight: 900;
  letter-spacing: clamp(4px, 1.4vw, 12px);
  color: #fff;
  text-shadow: 0 0 30px var(--accent, #0ff), 0 0 70px var(--accent, #0ff);
}

#level-banner .level-sub {
  margin-top: 6px;
  font-size: clamp(9px, 1.3vw, 12px);
  letter-spacing: 5px;
  color: rgba(255, 255, 255, 0.75);
}

@keyframes banner {
  0% { opacity: 0; transform: translateY(-24px) scale(0.96); }
  15% { opacity: 1; transform: translateY(0) scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translateY(10px); }
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.82; transform: scale(1.02); }
}

#game-board.shake { animation: shake 0.32s ease-out; }

@keyframes shake {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  20% { transform: translate(-5px, 2px) rotate(-0.3deg); }
  40% { transform: translate(5px, -2px) rotate(0.3deg); }
  60% { transform: translate(-3px, 1px) rotate(-0.15deg); }
  80% { transform: translate(2px, 0) rotate(0.1deg); }
}

#game-board.tetris-shake { animation: shake-tetris 0.55s ease-out; }

@keyframes shake-tetris {
  0%, 100% { transform: translate(0, 0) scale(1); }
  12% { transform: translate(-9px, 4px) scale(1.012); }
  30% { transform: translate(9px, -4px) scale(1.012); }
  50% { transform: translate(-6px, 3px) scale(1.008); }
  70% { transform: translate(4px, -2px) scale(1.004); }
}

.screen-flash {
  position: absolute;
  inset: 0;
  z-index: 7;
  pointer-events: none;
  opacity: 0;
  border-radius: 5px;
  transition: opacity 0.12s;
}

body.reduced-fx .game-board.shake,
body.reduced-fx .game-board.tetris-shake { animation: none; }

body.no-webgl {
  background: radial-gradient(ellipse at 50% 120%, #4a1d5e 0%, #16123a 42%, #02030a 100%);
}
body.no-webgl #world-canvas { display: none; }
body.no-webgl #ornament-canvas { opacity: 0.5; }

/* ============================ Адаптивность ============================ */

/* Тач-устройства */
@media (hover: none), (pointer: coarse) {
  .btn:hover { transform: none; }
  body { -webkit-touch-callout: none; }
  .side-panel .kbd-hint.desktop-hint { display: none; }
  .side-panel .kbd-hint.touch-hint { display: block; text-align: center; }

  /* Кнопкі — фіксаваныя па баках, не займаюць месца ў раскладцы */
  .touch-pad {
    display: flex;
    position: fixed;
    left: 0; right: 0;
    bottom: calc(10px + env(safe-area-inset-bottom));
    padding: 0 14px;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
    z-index: 25;
  }

  .pad-center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0;
  }
}

/* ---------- Телефоны и планшеты: своя компоновка ---------- */
@media (max-width: 900px) {
  :root { --panel-w: auto; }

  .game-wrapper {
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    width: 100vw;
    padding:
      calc(3px + env(safe-area-inset-top))
      calc(6px + env(safe-area-inset-right))
      calc(3px + env(safe-area-inset-bottom))
      calc(6px + env(safe-area-inset-left));
  }

  .side-panel {
    flex-direction: row;
    flex-wrap: nowrap;
    width: 100%;
    gap: 5px;
    align-items: stretch;
  }

  /* Усё інфармацыйнае — зверху, ніз вольны для кнопак */
  .side-panel.left { order: 0; }
  .side-panel.right { order: 1; }
  .game-board { order: 2; align-self: center; }

  /* Верхняя панель: 4 равных чипа на всю ширину */
  .side-panel.left .panel-box {
    flex: 1 1 0;
    min-width: 0;
    padding: 4px 2px 5px;
    border-radius: 9px;
  }
  .side-panel.left .panel-box h3 { font-size: 7px; letter-spacing: 1.5px; margin-bottom: 1px; }
  .side-panel.left .panel-box .value { font-size: 16px; }

  /* Компактный орнамент поля, чтобы не наезжал на чипы */
  .game-board::before,
  .game-board::after { height: 6px; left: -8px; right: -8px; }
  .game-board::before { top: -9px; }
  .game-board::after { bottom: -9px; }

  /* Нижняя панель: следующая фигура + черта текущей + пауза */
  .side-panel.right .panel-box.next-box {
    display: flex;
    flex: 0 0 112px;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 4px 6px;
  }
  .side-panel.right .panel-box.next-box h3 {
    margin: 0;
    font-size: 6.5px;
    letter-spacing: 1px;
    line-height: 1.3;
  }
  .side-panel.right #next-canvas { width: 56px; height: 46px; }

  .side-panel.right .trait-badge {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 9.5px;
    padding: 6px 8px;
  }
  .side-panel.right .trait-badge .t-icon { font-size: 15px; }

  .side-panel.right .hud-btn {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 13px;
    font-size: 10px;
  }

  /* Подсказки управления живут в меню и на паузе, а не в HUD */
  .side-panel .kbd-hint { display: none !important; }
  .sound-box { display: none; }

  .menu-card {
    width: 96vw;
    max-height: calc(100dvh - 22px);
    padding: 16px 14px;
    gap: 10px;
  }
  .traits-grid { max-height: none; overflow: visible; }
  .traits-grid .trait-card { flex: 1 1 100%; }
  .trait-card { font-size: 10.5px; }
  #overlay { padding: 8px; }
}

/* Портрет на тач-устройстве: поле прыціскаецца да верху, ніз — пад кнопкі */
@media (max-width: 900px) and (orientation: portrait) and (hover: none),
       (max-width: 900px) and (orientation: portrait) and (pointer: coarse) {
  body {
    align-items: flex-start;
    padding-top: calc(4px + env(safe-area-inset-top));
  }
}

/* Телефоны в альбомной ориентации: ряд с узкими панелями по бокам */
@media (max-width: 900px) and (orientation: landscape) {
  .game-wrapper {
    flex-direction: row;
    align-items: center;
    width: auto;
    gap: 6px;
    padding:
      calc(4px + env(safe-area-inset-top))
      calc(6px + env(safe-area-inset-right))
      calc(4px + env(safe-area-inset-bottom))
      calc(6px + env(safe-area-inset-left));
  }

  .side-panel {
    flex-direction: column;
    flex-wrap: nowrap;
    width: 96px;
    gap: 4px;
  }

  .side-panel.left { order: 0; }
  .game-board { order: 1; }
  .side-panel.right { order: 2; }

  .side-panel .panel-box { flex: 0 0 auto; width: 100%; padding: 4px 6px; }
  .side-panel.left .panel-box h3 { font-size: 6.5px; margin-bottom: 0; }
  .side-panel.left .panel-box .value { font-size: 14px; }

  .side-panel.right .panel-box.next-box { flex: 0 0 auto; }
  .side-panel.right #next-canvas { width: 80px; height: 65px; }

  .side-panel.right .trait-badge { width: 100%; font-size: 8px; padding: 5px 6px; }
  .side-panel.right .trait-badge .t-name { white-space: nowrap; }

  .side-panel.right .hud-btn { width: 100%; padding: 8px 0; }

  /* Альбомная: кнопкі прыціснуты да нізу па баках, «скід» побач з правай групай */
  .touch-pad { padding: 0 8px; bottom: calc(6px + env(safe-area-inset-bottom)); }
  .pad-center { position: static; transform: none; margin-left: auto; margin-right: 10px; }
  .pad-group { gap: 8px; }
  .tbtn { width: 48px; height: 48px; font-size: 20px; }
  .tbtn.sub { width: 44px; height: 44px; font-size: 17px; }
  .tbtn.drop { width: 54px; height: 54px; font-size: 23px; }
}

/* Узкие телефоны */
@media (max-width: 420px) {
  .meta-row { gap: 6px; }
  .chip { padding: 5px 10px; letter-spacing: 1px; }
  .btn { letter-spacing: 1px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .tbtn { width: 58px; height: 58px; }
  .tbtn.sub { width: 50px; height: 50px; }
  .tbtn.drop { width: 62px; height: 62px; }
}

/* Компактный HUD на низких экранах (landscape телефоны) */
@media (max-height: 620px) {
  .side-panel { gap: 5px; }
  .panel-box { padding: 3px 9px; border-radius: 8px; }
  .panel-box h3 { font-size: 7px; letter-spacing: 1.5px; margin-bottom: 0; }
  .panel-box .value { font-size: 13px; }
  .panel-box .value.small { font-size: 8px; }
  #next-canvas { width: 58px; height: 48px; }
  .trait-badge { font-size: 7px; padding: 3px 7px; gap: 4px; }
  .trait-badge .t-icon { font-size: 11px; }
  .hud-btn { padding: 4px 10px; font-size: 9px; }
  .side-panel .kbd-hint { display: none !important; }
  .game-wrapper { gap: 8px; }
}

/* Очень низкие экраны */
@media (max-height: 480px) {
  .menu-card { max-height: 96vh; padding: 10px 14px; gap: 6px; }
  #overlay h1.title { font-size: clamp(20px, 5vw, 30px); }
  .traits-grid { max-height: 34vh; overflow-y: auto; }
  .stat b { font-size: 15px; }
}
