:root {
  --ink: #5a4a72;
  --pink: #ff9cc8;
  --lav: #cbb6ef;
  --panel: rgba(255, 251, 255, 0.94);
  font-family: "Nunito", "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #e9f6e4; overscroll-behavior: none; }
/* Mobil: långtryck och dubbeltryck ska inte markera text, visa kopiera-menyn eller zooma.
   Gäller allt utom namnfältet. touch-action: manipulation tar bort dubbeltryckszoom (iOS
   bortser från user-scalable=no). Canvasen har touch-action: none för pekspaken. */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }
*, *::before, *::after { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
body { -webkit-tap-highlight-color: transparent; }
input:not([type]), input[type="text"] { -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
img { -webkit-user-drag: none; }
/* Fast mot visningsytan i stället för 100vh, som i mobilsafari är högre än det som syns. */
#game { position: fixed; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; }
.hidden { display: none !important; }
kbd {
  background: #fff; border: 1px solid #d9cdee; border-bottom-width: 3px;
  border-radius: 6px; padding: 0 6px; font-size: 0.85em;
}

.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(233, 222, 250, 0.55); backdrop-filter: blur(3px); padding: 16px;
}
.panel {
  background: var(--panel); border-radius: 28px; padding: 22px 20px; max-width: 1060px;
  max-height: 100%; overflow-y: auto; text-align: center;
  box-shadow: 0 10px 40px rgba(120, 90, 170, 0.25); border: 3px solid #f2e8ff;
}
h1 { font-size: clamp(2rem, 6vw, 3.4rem); margin: 0 0 6px; color: #e472a8;
  text-shadow: 3px 3px 0 #fde3ee, 6px 6px 0 #e9dcfb; letter-spacing: 1px; }
h2 { margin: 0 0 14px; color: #b07ad9; }
.subtitle { margin: 0 0 18px; }
.hint { font-size: 0.9rem; opacity: 0.8; margin: 16px 0 0; line-height: 1.7; }

.cards { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; }
.card {
  width: 146px; display: flex; flex-direction: column; justify-content: flex-start; background: #fff; border: 3px solid #eee4fb; border-radius: 20px; padding: 12px;
  cursor: pointer; transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
  font: inherit; color: inherit; text-align: center;
}
.card:hover, .card:focus-visible, .card.selected {
  transform: translateY(-4px); border-color: var(--pink); outline: none;
  box-shadow: 0 8px 18px rgba(255, 140, 190, 0.3);
}
.card h3 { margin: 4px 0; }
.card p { margin: 4px 0; font-size: 0.85rem; line-height: 1.35; }
.card .stats { font-size: 0.75rem; opacity: 0.75; }
.card .icon { font-size: 2.2rem; }
.card .key { font-size: 0.75rem; opacity: 0.6; }
.card canvas { width: 110px; height: 100px; display: block; margin: 0 auto; }

button.big {
  font: inherit; font-weight: 700; font-size: 1.1rem; border: none; border-radius: 999px;
  padding: 12px 28px; margin: 6px; cursor: pointer; background: var(--pink); color: #fff;
  box-shadow: 0 4px 0 #e0709f;
}
button.big.secondary { background: var(--lav); box-shadow: 0 4px 0 #9f86d1; }
button.big:active { transform: translateY(2px); box-shadow: none; }

#hud { position: fixed; inset: 0; pointer-events: none; }
.xpbar {
  position: absolute; top: 8px; left: 8px; right: 8px; height: 22px; border-radius: 11px;
  background: rgba(255,255,255,0.75); border: 2px solid #e6dbf7; overflow: hidden;
}
#xpfill { height: 100%; width: 0; background: linear-gradient(90deg, #9fd6ff, #cbb6ef, #ffb3d9); transition: width 0.15s; }
#level { position: absolute; inset: 0; text-align: center; font-weight: 800; font-size: 0.85rem; line-height: 18px; }
.topline {
  position: absolute; top: 36px; left: 0; right: 0; display: flex; justify-content: center; gap: 24px;
  font-weight: 800; font-size: 1.3rem; text-shadow: 0 2px 0 #fff;
}
.hpbar {
  position: absolute; left: 50%; top: calc(50% + 14px); width: 44px; height: 6px; transform: translateX(-50%);
  background: rgba(90,74,114,0.25); border-radius: 3px; overflow: hidden;
}
#hpfill { height: 100%; width: 100%; background: #ff7fae; }
#weapons { position: absolute; left: 10px; top: 38px; display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; font-weight: 700; }
#weapons span { background: rgba(255,255,255,0.7); padding: 2px 8px; border-radius: 10px; }
.iconbtn {
  position: absolute; right: 10px; top: 38px; pointer-events: auto; border: none; border-radius: 50%;
  width: 40px; height: 40px; font-size: 1.1rem; background: rgba(255,255,255,0.8); cursor: pointer;
}

/* Ikoner och bilder ur grafikpaketet (assets/). Pixelgrafik ska skalas utan utjämning. */
.icon-img, .icon-inline, .portrait, .logo { image-rendering: pixelated; }
.icon-img { width: 48px; height: 48px; display: block; margin: 0 auto; }
.icon-inline { width: 1.25em; height: 1.25em; vertical-align: -0.25em; }
.card .portrait { width: 110px; height: 100px; display: block; margin: 0 auto; object-fit: contain; }
h1 .logo { width: min(520px, 80vw); height: auto; display: block; margin: 0 auto; }

/* Esc-meny och inställningar */
.menu-panel { min-width: min(360px, 92vw); position: relative; }
.menu-items { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
.menu-items button.big:focus-visible, #settingsback:focus-visible { outline: 3px solid #b07ad9; outline-offset: 2px; }
.sliders { display: flex; flex-direction: column; gap: 14px; margin: 6px 0 16px; text-align: left; }
.slider { display: grid; grid-template-columns: 8.5em 1fr 3.2em; align-items: center; gap: 10px; font-weight: 700; }
.slider input { width: 100%; accent-color: #e472a8; }
.slider output { text-align: right; font-variant-numeric: tabular-nums; }
.toggle { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.toggle input { width: 1.2em; height: 1.2em; accent-color: #e472a8; }
.panel { position: relative; }
.iconbtn.corner { position: absolute; top: 12px; right: 12px; }

.toast {
  position: absolute; top: 76px; left: 50%; transform: translateX(-50%);
  background: rgba(255, 251, 255, 0.92); border: 2px solid #f2c6dc; border-radius: 14px;
  padding: 6px 16px; font-weight: 800; color: #b0477c; white-space: nowrap;
  box-shadow: 0 4px 14px rgba(120, 90, 170, 0.2);
}

/* Topplista */
.iconbtn.corner.left { right: auto; left: 12px; }
.scores { width: 100%; border-collapse: collapse; margin: 4px 0 12px; font-variant-numeric: tabular-nums; }
.scores th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.5px; opacity: 0.6; padding: 2px 8px; }
.scores td { padding: 5px 8px; border-top: 2px solid #f2e8ff; }
.scores td.name { text-align: left; font-weight: 700; max-width: 9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scores td.num { text-align: right; }
.scores tr.mine td { background: #ffe8f3; color: #b0477c; font-weight: 800; }
.scores tr.mine td:first-child { border-radius: 10px 0 0 10px; }
.scores tr.mine td:last-child { border-radius: 0 10px 10px 0; }
.scores-empty { opacity: 0.7; margin: 8px 0 16px; }
#gameoverscores { max-width: 460px; margin: 0 auto 8px; }
.nameform { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; margin: 4px 0 10px; font-weight: 700; }
.nameform input { font: inherit; font-weight: 700; color: inherit; padding: 6px 12px; border-radius: 999px;
  border: 2px solid #eee4fb; width: 10em; text-align: center; }
.nameform input:focus { outline: none; border-color: var(--pink); }
button.big.small { font-size: 0.95rem; padding: 7px 18px; margin: 0; }

/* ---------- Startskärm ---------- */
.title-screen {
  background:
    radial-gradient(ellipse 60% 45% at 50% 42%, rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0) 70%),
    linear-gradient(180deg, rgba(214, 196, 245, 0.6), rgba(255, 214, 233, 0.35) 55%, rgba(233, 246, 228, 0.15));
  backdrop-filter: blur(2px);
  overflow: hidden;
}
.title-stage {
  position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center;
  text-align: center; max-height: 100%; overflow-y: auto; padding: 8px;
}
.title-bears { display: flex; gap: 2px; justify-content: center; margin-bottom: -10px; position: relative; z-index: 2; }
.title-bear { animation-name: sprite-play, hop; animation-duration: var(--dur), 2.4s;
  animation-timing-function: steps(var(--steps)), ease-in-out; animation-iteration-count: infinite, infinite; }

.logo-plate {
  position: relative; background: rgba(255, 252, 255, 0.92); border: 4px solid #f2e8ff;
  border-radius: 36px; padding: clamp(14px, 3vw, 26px) clamp(18px, 4vw, 44px) clamp(12px, 2.4vw, 20px);
  box-shadow: 0 8px 0 #e6d8fa, 0 18px 40px rgba(120, 90, 170, 0.28);
  animation: plate-bob 4s ease-in-out infinite;
}
.logo-plate::before, .logo-plate::after {
  content: '✦'; position: absolute; color: #ffb3d9; font-size: 1.6rem; animation: twinkle 1.8s ease-in-out infinite;
}
.logo-plate::before { top: -14px; left: 18px; }
.logo-plate::after { bottom: 6px; right: 14px; color: #9fd6ff; animation-delay: 0.9s; }
.title-logo { margin: 0; text-shadow: none; }
.title-logo .logo-text { font-size: clamp(2.6rem, 9vw, 5rem); color: #e472a8;
  text-shadow: 3px 3px 0 #fde3ee, 6px 6px 0 #e9dcfb; }
/* Loggans SVG är 640×160 med bokstäverna i rutan x 90–530, y 56–112: visa bara den. */
.logo-crop { display: block; position: relative; overflow: hidden; width: min(600px, 82vw); aspect-ratio: 440 / 56; }
.logo-crop .logo { position: absolute; left: -20.45%; top: -100%; width: 145.45%; max-width: none; height: auto; margin: 0; }
.tagline { margin: 10px 0 0; display: flex; flex-direction: column; align-items: center; line-height: 1.05; }
.tag-small { font-weight: 700; font-size: clamp(0.95rem, 2.4vw, 1.2rem); letter-spacing: 2px; text-transform: uppercase; color: #a08cc4; }
.tag-big { font-weight: 900; font-size: clamp(1.7rem, 5.4vw, 2.7rem); color: #7d7aa8; letter-spacing: 1px;
  text-shadow: 2px 2px 0 #e3e1f2, 4px 4px 0 #cfcde6; }

.title-menu { display: flex; flex-direction: column; align-items: stretch; gap: 4px; margin-top: clamp(16px, 3.5vh, 30px); min-width: min(260px, 80vw); }
.title-menu button.big { font-size: 1.25rem; padding: 14px 32px; }
.title-menu button.big:focus-visible { outline: 3px solid #b07ad9; outline-offset: 3px; }
.title-menu button.big:hover { filter: brightness(1.05); }
.best { margin: 12px 0 0; font-weight: 700; }
.title-stage .hint { margin-top: 10px; }

.title-foes { position: absolute; inset: 0; pointer-events: none; }
.foe { position: absolute; opacity: 0.9; animation-name: sprite-play, drift;
  animation-duration: var(--dur, 1s), 7s; animation-timing-function: steps(var(--steps, 1)), ease-in-out;
  animation-iteration-count: infinite, infinite; }
.foe.f1 { left: 7%; top: 12%; }
.foe.f2 { left: 20%; top: 64%; animation-delay: 0s, -2s; }
.foe.f3 { right: 6%; top: 8%; animation-delay: 0s, -4s; }
.foe.f4 { right: 14%; top: 58%; animation-delay: 0s, -1s; }
.foe.f5 { left: 5%; bottom: 6%; animation-delay: 0s, -3s; }
.foe.f6 { right: 7%; bottom: 10%; animation-delay: 0s, -5s; }

.sprite { background-repeat: no-repeat; background-position: 0 0; image-rendering: pixelated; flex: none; }
.sprite:not(.animated) { animation-name: drift; }
.title-bear:not(.animated) { animation-name: hop; }

@keyframes sprite-play { to { background-position-x: var(--sheet); } }
@keyframes hop { 0%, 70%, 100% { translate: 0 0; } 80% { translate: 0 -10px; } 90% { translate: 0 0; } }
@keyframes drift { 0%, 100% { translate: 0 0; } 33% { translate: 14px -12px; } 66% { translate: -10px 8px; } }
@keyframes plate-bob { 0%, 100% { translate: 0 0; } 50% { translate: 0 -5px; } }
@keyframes twinkle { 0%, 100% { opacity: 0.3; scale: 0.8; } 50% { opacity: 1; scale: 1.15; } }

@media (prefers-reduced-motion: reduce) {
  .title-bear, .foe, .logo-plate, .logo-plate::before, .logo-plate::after { animation: none; }
}

/* Nallevalet */
.select-title { font-size: clamp(1.6rem, 4.5vw, 2.2rem); color: #e472a8; margin-bottom: 6px; }
#selectback { font-weight: 800; }


/* ---------- Mobil: pekskärm, säkra områden, stående och liggande läge ---------- */

/* Tangentbordstips visas bara med tangentbord, pektips bara med pekskärm. */
body:not(.touch) .touch-only { display: none; }
body.touch .kb-only, body.touch .card .key, body.touch .toggle kbd { display: none; }

/* Notch, rundade hörn och hemindikator (viewport-fit=cover i index.html). */
.overlay {
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.xpbar { top: calc(8px + env(safe-area-inset-top)); left: max(8px, env(safe-area-inset-left)); right: max(8px, env(safe-area-inset-right)); }
.topline { top: calc(36px + env(safe-area-inset-top)); }
#weapons { top: calc(38px + env(safe-area-inset-top)); left: max(10px, env(safe-area-inset-left)); }
#hud .iconbtn { top: calc(38px + env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right)); }
.toast { top: calc(76px + env(safe-area-inset-top)); max-width: calc(100vw - 24px); white-space: normal; text-align: center; }
body.touch #pausebtn { width: 48px; height: 48px; font-size: 1.3rem; }

/* Stående telefon: smal skärm. */
@media (orientation: portrait) and (max-width: 560px) {
  .overlay { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .panel { padding: 16px 12px; border-radius: 22px; width: 100%; }
  .subtitle { font-size: 0.9rem; margin-bottom: 12px; }
  .topline { gap: 16px; font-size: 1.15rem; }
  /* Vapenlistan under tid/poäng så att den inte krockar med klockan. */
  #weapons { top: calc(76px + env(safe-area-inset-top)); font-size: 0.78rem; }
  .toast { top: calc(110px + env(safe-area-inset-top)); }
  /* Kort som rader: bild till vänster, text till höger. */
  .cards { flex-direction: column; flex-wrap: nowrap; gap: 8px; }
  .card {
    width: 100%; display: grid; grid-template-columns: 64px 1fr; column-gap: 12px; align-items: center;
    text-align: left; padding: 8px 12px; border-radius: 16px;
  }
  .card > :first-child { grid-row: 1 / span 3; }
  .card .portrait, .card canvas { width: 64px; height: 58px; }
  .card .icon { font-size: 1.8rem; }
  .card h3 { margin: 0; font-size: 1.05rem; }
  .card p { margin: 1px 0; font-size: 0.8rem; }
  .card:hover, .card:focus-visible, .card.selected { transform: none; }
}

/* Liggande telefon: låg skärm. Allt ska rymmas utan att man behöver skrolla. */
@media (orientation: landscape) and (max-height: 520px) {
  .overlay { padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .panel { padding: 10px 14px; border-radius: 20px; }
  h2 { margin-bottom: 8px; font-size: 1.3rem; }
  .select-title { font-size: 1.4rem; margin-bottom: 6px; }
  .subtitle { display: none; }
  .hint { margin-top: 8px; font-size: 0.8rem; line-height: 1.4; }
  button.big { padding: 9px 24px; font-size: 1rem; }
  .iconbtn.corner { top: 8px; }
  .menu-items { gap: 0; }

  /* Startskärm i två kolumner: loggan till vänster, menyn till höger. */
  .title-stage {
    display: grid; grid-template-columns: auto auto; column-gap: clamp(16px, 4vw, 40px); align-items: center;
    grid-template-areas: "bears menu" "logo menu" "logo best" "logo hint";
  }
  .title-bears { grid-area: bears; }
  .logo-plate { grid-area: logo; align-self: start; padding: 10px 18px 10px; border-radius: 24px; }
  .title-menu { grid-area: menu; margin-top: 0; min-width: 200px; align-self: end; }
  .title-menu button.big { font-size: 1.05rem; padding: 10px 24px; }
  .title-stage .best { grid-area: best; margin-top: 6px; }
  .title-stage .hint { grid-area: hint; margin-top: 6px; }
  .logo-crop { width: min(380px, 46vw); }
  .tag-small { font-size: 0.8rem; }
  .tag-big { font-size: clamp(1.3rem, 7vh, 2rem); }

  /* Sex nallar eller tre uppgraderingar på en rad. */
  .cards { flex-wrap: nowrap; gap: 8px; }
  .card { width: auto; flex: 1 1 0; min-width: 0; max-width: 170px; padding: 6px 6px 8px; border-radius: 16px; }
  #choices .card { max-width: 240px; }
  .card .portrait, .card canvas { width: 64px; height: 58px; }
  .card .icon { font-size: 1.7rem; }
  .icon-img { width: 36px; height: 36px; }
  .card h3 { margin: 2px 0; font-size: 0.95rem; }
  .card p { font-size: 0.72rem; line-height: 1.25; margin: 2px 0; }
  .card .stats { font-size: 0.66rem; }
  .card:hover, .card:focus-visible, .card.selected { transform: translateY(-2px); }

  .xpbar { height: 18px; }
  #level { line-height: 14px; font-size: 0.75rem; }
  .topline { top: calc(30px + env(safe-area-inset-top)); font-size: 1.1rem; }
  #weapons { top: calc(32px + env(safe-area-inset-top)); font-size: 0.75rem; }
  #hud .iconbtn { top: calc(30px + env(safe-area-inset-top)); }
  .toast { top: calc(62px + env(safe-area-inset-top)); }
}
