/* =============================================================
   Klara 3D – utseende
   ============================================================= */

:root {
  --purple: #5b3fa0;
  --bg: #efe4ff;
  --panel: #ffffff;
  --font: 'Baloo 2', 'Arial Rounded MT Bold', 'Trebuchet MS', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--font);
  color: var(--purple);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

button { font-family: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ---------- Mätare överst ---------- */

#hud {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 4px solid #e2d4ff;
  z-index: 2;
}
#meters { display: flex; flex: 1; gap: 8px; min-width: 0; }
.meter { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; }
.m-icon { font-size: 24px; line-height: 1.1; }
.m-bar {
  width: 100%; max-width: 120px; height: 12px;
  background: #ece6f7; border-radius: 8px; overflow: hidden;
  border: 2px solid #d9cdf3;
}
.m-fill { height: 100%; width: 50%; border-radius: 6px; transition: width .3s, background .3s; }
.m-word { font-size: 12px; font-weight: 800; letter-spacing: .5px; line-height: 1.3; }
.meter.low .m-icon { animation: pulse .9s ease-in-out infinite; }
.meter.low .m-word { color: #e0304a; }
@keyframes pulse { 50% { transform: scale(1.3) rotate(-8deg); } }

#hud-right { display: flex; gap: 6px; align-items: center; }
#stars {
  background: #fff3b0; border: 3px solid #ffd43b; border-radius: 999px;
  padding: 2px 12px; font-size: 22px; font-weight: 800; white-space: nowrap;
}
#stars.bump { animation: bump .45s ease; }
@keyframes bump { 50% { transform: scale(1.35); } }
#mute {
  border: 3px solid #e2d4ff; background: #fff; border-radius: 50%;
  width: 44px; height: 44px; font-size: 20px; padding: 0;
}

/* ---------- 3D-bilden ---------- */

#stage-wrap { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #efe4ff; }
#gl { display: block; width: 100%; height: 100%; touch-action: none; }

/* Joystick */
#joy {
  position: absolute; left: 22px; bottom: 22px;
  width: 132px; height: 132px; border-radius: 50%;
  background: rgba(255, 255, 255, .35);
  border: 4px solid rgba(255, 255, 255, .8);
  box-shadow: 0 4px 16px rgba(91, 63, 160, .25);
  touch-action: none; z-index: 3;
  display: flex; align-items: center; justify-content: center;
}
#knob {
  width: 60px; height: 60px; border-radius: 50%;
  background: #ff6fae; border: 4px solid #fff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, .25);
  pointer-events: none;
}
#joy.active #knob { background: #ff4f9b; }

/* Effekter: hjärtan, stjärnor, Zzz */
#fx { position: absolute; inset: 0; pointer-events: none; z-index: 2; overflow: hidden; }
.fx {
  position: absolute; transform: translate(-50%, -50%);
  font-size: 34px; font-weight: 800; color: var(--purple);
  animation: floatup 1.6s ease-out forwards; white-space: nowrap;
  text-shadow: 0 2px 0 #fff, 0 0 6px #fff;
}
.fx.small { font-size: 22px; }
.fx.star { font-size: 30px; }
.fx.zzz { color: #fff; text-shadow: 0 0 8px #5b3fa0; animation-name: zzz; }
@keyframes floatup {
  0% { transform: translate(-50%, -50%) scale(.5); opacity: 0; }
  15% { transform: translate(-50%, -80%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -350%) scale(1.1); opacity: 0; }
}
@keyframes zzz {
  0% { transform: translate(-50%, -50%) scale(.4); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: translate(40%, -300%) scale(1.3); opacity: 0; }
}

/* Pratbubbla över Klara */
#bubble {
  position: absolute; z-index: 2; pointer-events: none;
  transform: translate(-50%, -100%);
  background: #fff; border: 4px solid var(--purple); border-radius: 18px;
  padding: 2px 14px; display: flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
#bubble:empty { display: none !important; }
#bubble::after {
  content: ''; position: absolute; left: 50%; bottom: -14px; margin-left: -10px;
  border: 10px solid transparent; border-top-color: var(--purple); border-bottom: 0;
}
.b-ic { font-size: 30px; }
.b-w { font-size: 24px; font-weight: 800; }
#bubble.pop { animation: bubblein .3s cubic-bezier(.2, 1.4, .4, 1); }
#bubble.urgent { animation: wiggle 1s ease-in-out infinite; }
@keyframes bubblein { from { transform: translate(-50%, -100%) scale(.3); opacity: 0; } }
@keyframes wiggle {
  0%, 100% { transform: translate(-50%, -100%) rotate(0); }
  25% { transform: translate(-50%, -100%) rotate(-5deg); }
  75% { transform: translate(-50%, -100%) rotate(5deg); }
}

/* Pil som visar var Klara är */
#arrow {
  position: absolute; z-index: 2; pointer-events: none;
  width: 64px; height: 64px; margin: -32px 0 0 -32px;
  background: #fff; border: 4px solid #ff6fae; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  animation: pulse .9s ease-in-out infinite;
}
.a-ic { font-size: 28px; }
.a-pt {
  position: absolute; inset: -4px; display: flex; align-items: center; justify-content: flex-end;
  color: #ff6fae; font-size: 26px; padding-right: 0; margin-right: -26px;
}

/* Övergång (svart/vit blink) när man går ut/in */
#fade {
  position: absolute; inset: 0; background: #fff; opacity: 0;
  pointer-events: none; transition: opacity .4s; z-index: 6;
}
#fade.on { opacity: 1; }

/* ---------- Knappar längst ner ---------- */

#buttons {
  display: flex; justify-content: center; gap: 8px;
  padding: 8px 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 4px solid #e2d4ff; z-index: 2;
}
.btn {
  flex: 1; max-width: 130px; min-width: 0; min-height: 70px;
  border: none; border-radius: 20px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 4px 2px; color: #fff;
  box-shadow: 0 5px 0 rgba(0, 0, 0, .22);
  transition: transform .08s, box-shadow .08s, opacity .2s;
}
.btn .ic { font-size: 30px; line-height: 1.1; }
.btn .w { font-size: 15px; font-weight: 800; letter-spacing: .5px; text-shadow: 0 1px 2px rgba(0, 0, 0, .25); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0, 0, 0, .22); }
.btn.big { max-width: 260px; flex: 2; }
.c-orange { background: #ff9a3c; }
.c-blue   { background: #3fa9ff; }
.c-green  { background: #4cc764; }
.c-teal   { background: #2ec4b6; }
.c-purple { background: #9b6fe8; }
.c-pink   { background: #ff6fae; }
.c-yellow { background: #ffbf1f; }
#buttons.busy .btn { opacity: .45; pointer-events: none; }

/* ---------- Meddelande ---------- */

#toast {
  position: absolute; left: 50%; top: 14px;
  transform: translate(-50%, -220%); opacity: 0;
  background: #fff; border: 4px solid #b48be8; border-radius: 20px;
  padding: 6px 20px; font-size: 24px; font-weight: 800;
  white-space: nowrap; max-width: 94%; overflow: hidden; text-overflow: ellipsis;
  box-shadow: 0 6px 18px rgba(91, 63, 160, .2);
  transition: transform .35s cubic-bezier(.2, 1.4, .4, 1), opacity .25s;
  pointer-events: none; z-index: 5;
}
#toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---------- Välkommen ---------- */

#welcome {
  position: fixed; inset: 0; background: rgba(70, 40, 120, .45);
  display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px;
}
#welcome[hidden] { display: none; }
.w-card {
  background: #fff; border-radius: 28px; border: 5px solid #b48be8;
  padding: 10px 28px 24px; text-align: center; max-width: 420px; width: 100%;
  animation: popin .4s cubic-bezier(.2, 1.4, .4, 1);
}
#w-cat { display: flex; justify-content: center; }
#w-cat canvas { width: 200px !important; height: 200px !important; }
.w-card h1 { margin: 0; font-size: 36px; }
.w-card p { margin: 4px 0 16px; font-size: 24px; font-weight: 600; }
.w-card .btn { margin: 0 auto; width: 180px; }
@keyframes popin { from { transform: scale(.6); opacity: 0; } }

/* ---------- Butik ---------- */

#panel {
  position: absolute; inset: 0; overflow-y: auto; padding: 12px; z-index: 4;
  background: rgba(239, 228, 255, .88);
}
#panel[hidden] { display: none; }
.shop { display: flex; gap: 16px; align-items: center; justify-content: center; min-height: 100%; flex-wrap: wrap; }
.shop-left { display: flex; flex-direction: column; align-items: center; }
.shop-title {
  font-size: 30px; font-weight: 800; background: #fff; border-radius: 999px;
  padding: 0 22px; border: 4px solid #ff6fae;
}
.preview {
  position: relative; width: 240px; max-width: 60vw; aspect-ratio: 1;
  background: #fff; border-radius: 50%; border: 5px solid #e2d4ff;
  display: flex; align-items: center; justify-content: center; margin-top: 8px; overflow: hidden;
}
.p-gl canvas { width: 230px !important; height: 230px !important; max-width: 58vw; max-height: 58vw; display: block; }
.p-hearts { position: absolute; inset: 0; pointer-events: none; }
.p-hearts span {
  position: absolute; top: 40%; font-size: 30px; opacity: 0;
  animation: heartup 1.6s ease-out forwards;
}
@keyframes heartup {
  0% { transform: translateY(0) scale(.5); opacity: 0; }
  15% { opacity: 1; transform: translateY(-10px) scale(1); }
  100% { transform: translateY(-120px) scale(1.1); opacity: 0; }
}
.shop-grid { display: grid; grid-template-columns: repeat(3, minmax(90px, 130px)); gap: 10px; }
.card {
  background: #fff; border: 4px solid #e2d4ff; border-radius: 20px;
  padding: 6px 4px 8px; display: flex; flex-direction: column; align-items: center;
  box-shadow: 0 5px 0 #d9cdf3; transition: transform .08s;
}
.card:active { transform: translateY(4px); box-shadow: 0 1px 0 #d9cdf3; }
.c-ic { font-size: 40px; line-height: 1.2; }
.c-w { font-size: 14px; font-weight: 800; }
.c-p { margin-top: 4px; font-size: 16px; font-weight: 800; background: #fff3b0; border-radius: 999px; padding: 0 10px; }
.card.on { border-color: #4cc764; }
.card.on .c-p { background: #4cc764; color: #fff; }
.card.off .c-p { background: #ece6f7; }
.card.cant { opacity: .55; }
.card.shake { animation: shake .4s; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }

/* ---------- Små skärmar ---------- */

@media (max-width: 520px) {
  #hud { padding: 6px 8px; gap: 6px; }
  #meters { gap: 4px; }
  .m-icon { font-size: 20px; }
  .m-word { font-size: 10px; }
  #stars { font-size: 18px; padding: 2px 8px; }
  #mute { width: 38px; height: 38px; font-size: 17px; }
  #buttons { gap: 5px; padding: 6px; }
  .btn { min-height: 60px; border-radius: 16px; }
  .btn .ic { font-size: 24px; }
  .btn .w { font-size: 11px; }
  #joy { width: 110px; height: 110px; left: 14px; bottom: 14px; }
  #knob { width: 50px; height: 50px; }
  .shop-grid { grid-template-columns: repeat(3, minmax(80px, 1fr)); gap: 8px; }
  .c-ic { font-size: 32px; }
}
@media (max-height: 500px) {
  .m-icon { font-size: 18px; }
  .btn { min-height: 54px; }
  .btn .ic { font-size: 22px; }
  #joy { width: 104px; height: 104px; }
  .preview { width: 160px; }
}
