:root {
  --bg: #1c1622; --bg2: #2a2131; --card: #33283b; --fg: #f6eef3; --muted: #bba9b8;
  --accent: #ff8fb1; --accent2: #ffc2d4; --danger: #ff6b6b; --ok: #8fe3a6;
  --key: #3a2f43; --key-hi: #4c3d57; --key-fg: #f6eef3;
  --safe-t: max(env(safe-area-inset-top), var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-b: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); overflow: hidden;
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; touch-action: none; }
body { position: fixed; inset: 0; }
#app { position: absolute; inset: 0; display: flex; flex-direction: column; padding-top: var(--safe-t); padding-bottom: var(--safe-b); }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: var(--key); border-radius: 12px; padding: 12px 18px; cursor: pointer;
  user-select: none; -webkit-user-select: none; touch-action: manipulation; }
button.primary { background: var(--accent); color: #2a1020; font-weight: 600; }
button:active { filter: brightness(1.15); }
button:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }

/* ---------- screens ---------- */
.screen { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px; padding: 24px 16px;
  background: radial-gradient(ellipse at 50% 0%, #4a2d44 0%, var(--bg) 70%); text-align: center; }
.brand h1 { margin: 0; font-size: clamp(22px, 7vw, 34px); color: var(--accent2); letter-spacing: .04em; text-shadow: 0 2px 18px #ff8fb155; }
.brand .sub { margin: 6px 0 0; color: var(--muted); font-size: 13px; letter-spacing: .12em; text-transform: uppercase; }
.brand { position: relative; }
.petals i { position: absolute; width: 10px; height: 10px; background: var(--accent); border-radius: 10px 0; opacity: .7; animation: fall 6s linear infinite; }
.petals i:nth-child(1) { left: 5%; animation-delay: 0s; } .petals i:nth-child(2) { left: 25%; animation-delay: 1.2s; }
.petals i:nth-child(3) { left: 45%; animation-delay: 2.5s; } .petals i:nth-child(4) { left: 65%; animation-delay: .6s; }
.petals i:nth-child(5) { left: 85%; animation-delay: 3.3s; } .petals i:nth-child(6) { left: 95%; animation-delay: 4.4s; }
@keyframes fall { 0% { transform: translateY(-60px) rotate(0); opacity: 0; } 15% { opacity: .8; } 100% { transform: translateY(90px) rotate(240deg); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .petals i { animation: none; display: none; } }
.status { display: flex; flex-direction: column; align-items: center; gap: 12px; color: var(--muted); min-height: 90px; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #ffffff22; border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { width: min(260px, 70vw); height: 6px; border-radius: 3px; background: #ffffff1a; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.error { max-width: 420px; background: var(--card); border-radius: 16px; padding: 20px; box-shadow: 0 8px 30px #0006; }
.error-title { margin: 0 0 8px; font-weight: 700; color: var(--danger); font-size: 17px; }
.error-text { margin: 0 0 16px; color: var(--fg); white-space: pre-line; }
.buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.buttons.column { flex-direction: column; align-items: stretch; width: min(320px, 86vw); }
.buttons.column button { padding: 14px; font-size: 16px; display: flex; flex-direction: column; align-items: center; }
.buttons small { font-weight: 400; opacity: .75; font-size: 12px; }
#btn-continue:not([hidden]) + #btn-title { background: var(--key); color: var(--fg); font-weight: 500; }
.start { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.hello { margin: 0; color: var(--muted); }
.hint { margin: 0; color: var(--muted); font-size: 12px; max-width: 340px; }

/* ---------- game ---------- */
#game { flex: 1; display: flex; flex-direction: column; min-height: 0; background: #000; }
#stage { position: relative; flex: 0 0 auto; display: flex; align-items: center; justify-content: center; background: #000; overflow: hidden; }
#canvas { display: block; image-rendering: auto; outline: none; touch-action: none; }
#toolbar { display: flex; align-items: center; gap: 6px; padding: 4px 8px; background: var(--bg); min-height: 34px; }
#toolbar .spacer { flex: 1; }
.tool { padding: 4px 10px; border-radius: 8px; font-size: 16px; background: transparent; }
.save-state { font-size: 12px; color: var(--muted); }
.save-state.offline { color: var(--danger); } .save-state.saved { color: var(--ok); }
#pad { flex: 1; display: flex; flex-direction: column; justify-content: space-evenly; gap: 8px; padding: 8px 14px 12px; background: var(--bg); min-height: 0; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.key { position: relative; background: var(--key); color: var(--key-fg); border-radius: 12px; padding: 0; font-size: 18px; box-shadow: 0 3px 0 #0005; }
.key.down-on { background: var(--key-hi); transform: translateY(2px); box-shadow: 0 1px 0 #0005; }
.key[data-l]::after { content: attr(data-t); position: absolute; left: 50%; transform: translateX(-50%); bottom: -16px; font-size: 10px;
  color: var(--muted); white-space: nowrap; pointer-events: none; }
.shoulders .sh { width: 22%; height: 40px; font-weight: 700; }
.shoulders .mid { width: 22%; height: 30px; font-size: 11px; border-radius: 15px; letter-spacing: .05em; }
.main { flex: 1; max-height: 210px; padding-bottom: 14px; }
.dpad, .face { position: relative; width: min(42vw, 190px); aspect-ratio: 1; }
.dpad .d { position: absolute; width: 34%; height: 34%; font-size: 15px; }
.d.up { left: 33%; top: 0; } .d.down { left: 33%; bottom: 0; } .d.left { left: 0; top: 33%; } .d.right { right: 0; top: 33%; }
.face .f { position: absolute; width: 34%; height: 34%; border-radius: 50%; font-size: 22px; font-weight: 700; }
.f.tri { left: 33%; top: 0; color: #8fe3c9; } .f.cr { left: 33%; bottom: 0; color: #9db7ff; }
.f.sq { left: 0; top: 33%; color: #ffb0e0; } .f.ci { right: 0; top: 33%; color: #ff9b9b; }
.f.ci { background: #4a3043; }
.f::after { bottom: -15px !important; }

/* landscape: game picture fills the height, pad halves float over the sides */
@media (orientation: landscape) {
  #game { position: relative; }
  #stage { flex: 1; }
  #toolbar { position: absolute; top: 4px; right: 6px; background: #0008; border-radius: 10px; z-index: 3; }
  #pad { position: absolute; inset: 0; background: transparent; pointer-events: none; padding: 40px 10px 10px; z-index: 2; }
  #pad .key { pointer-events: auto; opacity: .55; }
  #pad .key.down-on { opacity: .85; }
  #pad .main { max-height: none; align-items: flex-end; }
  .dpad, .face { width: min(22vh, 150px); }
  .key[data-l]::after { display: none; }
  .shoulders { align-items: flex-start; }
  .shoulders .sh { width: 72px; } .shoulders .mid { width: 64px; }
  body.pad-off #pad { display: none; }
}
body.pad-off #pad .key { visibility: hidden; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: #000a; display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
.card { background: var(--card); border-radius: 16px; padding: 20px; width: min(380px, 100%); box-shadow: 0 10px 40px #000a; text-align: center; }
.card .t { margin: 0 0 8px; font-weight: 700; font-size: 16px; }
.card .d { margin: 0 0 16px; color: var(--muted); font-size: 13px; }
.card input { width: 100%; font: inherit; font-size: 17px; padding: 10px 12px; border-radius: 10px; border: 2px solid var(--key-hi);
  background: var(--bg2); color: var(--fg); margin: 4px 0 14px; outline: none; }
.card input:focus { border-color: var(--accent); }
