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

:root {
  --ink: #2a2640;
  --ink-soft: #6b6585;
  --glass: rgba(255, 255, 255, 0.72);
  --line: rgba(70, 55, 110, 0.10);
  --accent1: #7b5cff;
  --accent2: #ff6b8b;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at 50% 42%, #fdfaf5 0%, #f1ecf3 45%, #dcd5ea 100%) #e9e4f1;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#gl { cursor: grab; outline: none; }
#gl.turning { cursor: grabbing; }
#fx { pointer-events: none; z-index: 5; }

.hidden { display: none !important; }

/* ---------- buttons ---------- */
.btn {
  font-family: inherit;
  font-size: 16px;
  font-weight: 650;
  line-height: 1;
  color: var(--ink);
  min-height: 44px;
  min-width: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--glass);
  box-shadow: 0 3px 10px rgba(60, 40, 100, 0.10), inset 0 1px 0 rgba(255, 255, 255, 0.9);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.12s ease, background 0.15s ease, box-shadow 0.15s ease, opacity 0.15s;
  touch-action: manipulation;
}
.btn:hover { background: rgba(255, 255, 255, 0.92); }
.btn:active { transform: scale(0.95); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.btn.icon { padding: 0; width: 44px; }
.btn.primary {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent1), var(--accent2));
  box-shadow: 0 6px 18px rgba(123, 92, 255, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.35);
}
.btn.primary:hover { filter: brightness(1.06); background: linear-gradient(135deg, var(--accent1), var(--accent2)); }
.btn.big { min-height: 52px; font-size: 19px; padding: 0 26px; border-radius: 16px; }

#btnMute .off { display: none; }
#btnMute.muted .on { display: none; }
#btnMute.muted .off { display: inline; }

/* ---------- HUD & bar: portrait / square layout (top + bottom) ---------- */
.panel { position: fixed; z-index: 10; display: flex; align-items: center; }
#hud {
  top: 0; left: 0; right: 0;
  padding: calc(10px + var(--sat)) calc(12px + var(--sar)) 6px calc(12px + var(--sal));
  justify-content: space-between;
  gap: 8px;
}
.stats { display: flex; gap: 6px; align-items: stretch; }
.stat {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 64px; padding: 5px 10px; border-radius: 14px;
  background: rgba(255, 255, 255, 0.55); border: 1px solid var(--line);
}
.stat .lbl { font-size: 11px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.stat .val { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap; }
.stat.main .val { font-size: 22px; }
.stat.main { min-width: 104px; }

#bar {
  left: 0; right: 0; bottom: 0;
  padding: 6px calc(12px + var(--sar)) calc(12px + var(--sab)) calc(12px + var(--sal));
  flex-wrap: wrap; justify-content: center; gap: 8px;
}
.seg { display: flex; gap: 0; border-radius: 14px; box-shadow: 0 3px 10px rgba(60, 40, 100, 0.10); }
.seg .btn { border-radius: 0; box-shadow: none; padding: 0 12px; }
.seg .btn:first-child { border-radius: 14px 0 0 14px; }
.seg .btn:last-child { border-radius: 0 14px 14px 0; }
.seg .btn.on { background: var(--ink); color: #fff; }
.acts { display: flex; gap: 8px; }

/* narrow phones: stats tighter */
@media (max-width: 430px) {
  #hud { padding-left: calc(8px + var(--sal)); padding-right: calc(8px + var(--sar)); gap: 4px; }
  .stats { gap: 4px; }
  .stat { min-width: 54px; padding: 4px 6px; }
  .stat.main { min-width: 92px; }
  .stat .val { font-size: 16px; }
  .stat.main .val { font-size: 19px; }
  .btn { padding: 0 11px; font-size: 15px; }
  .seg .btn { padding: 0 14px; }
}

/* ---------- side layout for wide screens (left stats, right actions) ---------- */
body.side #hud {
  top: 0; bottom: 0; right: auto; width: calc(176px + var(--sal));
  flex-direction: column; justify-content: center; gap: 12px;
  padding: calc(12px + var(--sat)) 10px calc(12px + var(--sab)) calc(14px + var(--sal));
}
body.side .stats { flex-direction: column; gap: 10px; width: 100%; }
body.side .stat { width: 100%; padding: 8px 10px; }
body.side .stat.main .val { font-size: 26px; }
body.side #bar {
  top: 0; bottom: 0; left: auto; width: calc(176px + var(--sar));
  flex-direction: column; flex-wrap: nowrap; justify-content: center; gap: 12px;
  padding: calc(12px + var(--sat)) calc(14px + var(--sar)) calc(12px + var(--sab)) 10px;
}
body.side .seg { width: 100%; }
body.side .seg .btn { flex: 1; padding: 0 4px; }
body.side .acts { flex-direction: column; width: 100%; gap: 10px; }
body.side .acts .btn { width: 100%; justify-content: flex-start; padding-left: 16px; }
body.side.short #hud, body.side.short #bar { gap: 8px; }
body.side.short .stats { gap: 6px; }
body.side.short .stat { padding: 4px 8px; }
body.side.short .stat.main .val { font-size: 21px; }
body.side.short .acts { gap: 7px; }
body.side.short .stat .lbl { font-size: 10px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; z-index: 12; left: 50%; transform: translateX(-50%);
  top: 80px; max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 14px;
  background: rgba(42, 38, 64, 0.86); color: #fff;
  font-size: 15px; font-weight: 600; text-align: center; line-height: 1.3;
  pointer-events: none; box-shadow: 0 8px 24px rgba(30, 20, 60, 0.25);
  animation: toastIn 0.3s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -8px); } }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; padding: 16px; padding-bottom: calc(16px + var(--sab)); }
.overlay.dim { align-items: center; background: rgba(40, 30, 70, 0.28); backdrop-filter: blur(1.5px); -webkit-backdrop-filter: blur(1.5px); }
.card {
  position: relative;
  width: min(440px, 100%);
  max-height: 100%;
  overflow: hidden;
  padding: 22px 22px 18px;
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 18px 50px rgba(50, 30, 100, 0.22);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  text-align: center;
  animation: cardIn 0.35s cubic-bezier(.2, 1.3, .4, 1);
}
@keyframes cardIn { from { opacity: 0; transform: translateY(18px) scale(0.97); } }
h1 { font-size: 38px; font-weight: 900; letter-spacing: -0.02em; line-height: 1.05; }
h1 span { background: linear-gradient(135deg, var(--accent1), var(--accent2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
h2 { font-size: 34px; font-weight: 900; letter-spacing: -0.02em; }
.sub { color: var(--ink-soft); font-size: 15px; margin-top: 6px; line-height: 1.35; }
.help { color: var(--ink-soft); font-size: 13px; margin-top: 12px; line-height: 1.35; }
.sizes { display: flex; gap: 10px; margin: 16px 0 14px; }
.size {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-family: inherit; color: var(--ink); cursor: pointer;
  padding: 10px 4px; border-radius: 16px; min-height: 44px;
  background: rgba(255, 255, 255, 0.7); border: 2px solid var(--line);
  transition: transform 0.12s, border-color 0.15s, background 0.15s;
}
.size b { font-size: 22px; font-weight: 900; }
.size small { font-size: 12px; font-weight: 700; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.06em; }
.size em { font-style: normal; font-size: 13px; font-weight: 700; color: var(--ink-soft); font-variant-numeric: tabular-nums; margin-top: 2px; }
.size em::before { content: "Best "; font-weight: 600; }
.btn.big { font-weight: 800; }
.size.on { border-color: var(--accent1); background: #fff; box-shadow: 0 6px 16px rgba(123, 92, 255, 0.2); }
.size:active { transform: scale(0.96); }
#btnPlay { width: 100%; }

.close { position: absolute; top: 12px; right: 12px; box-shadow: none; }
.resTime { font-size: 52px; font-weight: 900; font-variant-numeric: tabular-nums; margin-top: 10px; letter-spacing: -0.02em; }
.resLine { font-size: 17px; font-weight: 700; color: var(--ink-soft); }
.resBest { margin: 12px auto 16px; display: inline-block; padding: 6px 14px; border-radius: 99px; font-weight: 800; font-size: 15px; background: #f1edfb; color: var(--ink); }
.resBest.new { background: linear-gradient(135deg, #ffd65a, #ff9f43); color: #4a2a00; }
.row { display: flex; gap: 10px; justify-content: center; }
.row .btn { flex: 1; padding: 0 12px; }

/* menu beside the cube on wide screens */
body.side #menu { justify-content: flex-end; align-items: center; padding-right: calc(5vw + var(--sar)); }
body.side #menu .card { width: min(420px, 46vw); }
body.short .card { padding: 14px 16px 12px; }
body.short h1 { font-size: 30px; }
body.short h2 { font-size: 28px; }
body.short .sub { font-size: 14px; margin-top: 4px; }
body.short .sizes { margin: 10px 0; }
body.short .size { padding: 6px 4px; }
body.short .size b { font-size: 19px; }
body.short .help { margin-top: 8px; }
body.short .resTime { font-size: 40px; margin-top: 4px; }
body.short .resBest { margin: 6px auto 10px; }
