@font-face {
  font-family: "Fredoka";
  src: url("fonts/fredoka-latin.woff2") format("woff2");
  font-weight: 300 700;
  font-display: swap;
}

:root {
  --bg: #1a1d36;
  --bg-2: #262a50;
  --fg: #fdfbff;
  --muted: #aeb3d6;
  --panel: #282c52;
  --tile-empty: rgba(255, 255, 255, 0.05);
  --tile-filled: rgba(255, 255, 255, 0.09);
  --border: #454a78;
  --border-filled: #9aa0d4;
  --key-bg: #3a3f5e;
  --key-shadow: #252840;
  --key-fg: #ffffff;
  /* Ljusgrön + mörklila: skiljer sig både i nyans och ljushet, så de går att
     skilja åt även med de vanligaste formerna av färgblindhet. Tangenterna är
     mörka så att den lila färgen inte blandas ihop med dem. */
  --correct: #5be0a0;
  --on-correct: #06331f;
  --present: #9150e6;
  --on-present: #ffffff;
  --absent: #2a2e46;      /* "fel"-rutor på spelplanen */
  --on-absent: #c3c8ea;
  --absent-key: #24273c;  /* använda tangenter: mörkare än oanvända */
  --on-absent-key: #8d93bd;
  --toast-bg: #ffffff;
  --toast-fg: #2b2140;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(ellipse at 50% -10%, var(--bg-2), transparent 60%),
    var(--bg);
  color: var(--fg);
  font-family: "Fredoka", "Nunito", "Trebuchet MS", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
/* Små stjärnor i bakgrunden */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
  opacity: 0.55;
  background-image:
    radial-gradient(2px 2px at 8% 18%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 22% 64%, #ffe9a8, transparent),
    radial-gradient(2px 2px at 91% 12%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 78% 44%, #c9f0ff, transparent),
    radial-gradient(2px 2px at 64% 86%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 35% 32%, #ffd1e8, transparent),
    radial-gradient(2px 2px at 5% 88%, #ffe9a8, transparent),
    radial-gradient(1.5px 1.5px at 96% 72%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 50% 8%, #c9f0ff, transparent);
}
button { font-family: inherit; }

/* ---------- Header ---------- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  padding: 0 12px;
  flex-shrink: 0;
}
.title { display: flex; align-items: center; gap: 8px; }
.header-owl { width: 40px; height: 44px; display: block; }
.header-owl .owl { animation: wobble 4s ease-in-out infinite; transform-origin: 50% 90%; }
h1 { margin: 0; display: flex; gap: 4px; line-height: 1; }
h1 span {
  display: grid;
  place-items: center;
  width: 32px;
  height: 34px;
  border-radius: 8px;
  font-size: 22px;
  font-weight: 700;
  color: #fff;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}
h1 span:nth-child(5n+1) { background: var(--correct); color: var(--on-correct); transform: rotate(-4deg); }
h1 span:nth-child(5n+2) { background: var(--present); color: var(--on-present); transform: rotate(3deg); }
h1 span:nth-child(5n+3) { background: #ff7eb6; transform: rotate(-2deg); }
h1 span:nth-child(5n+4) { background: #4fb3ff; transform: rotate(4deg); }
h1 span:nth-child(5n) { background: #ff8a3d; transform: rotate(-3deg); }

.header-right { display: flex; gap: 4px; flex-shrink: 0; }
#btn-help { flex-shrink: 0; }
.icon-btn {
  background: none;
  border: none;
  color: var(--fg);
  cursor: pointer;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  font-size: 20px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
#btn-help {
  width: 34px;
  height: 34px;
  margin: 4px;
  border-radius: 50%;
  background: var(--key-bg);
  box-shadow: 0 3px 0 var(--key-shadow);
  font-size: 18px;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.08); }
#btn-help:hover { background: #5a53a0; }
.icon-btn.invisible { visibility: hidden; }
a.icon-btn { text-decoration: none; }

/* ---------- Spelplan ---------- */
main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 0;
  padding: 10px 6px;
}
/* Storlekar sätts i app.js (fitLayout) utifrån fönstrets mått */
:root { --game-w: min(528px, calc(100vw - 12px)); --kb-w: var(--game-w); --key-h: 56px; }
#board {
  display: grid;
  gap: 6px;
  width: var(--game-w);
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 3px solid var(--border);
  border-radius: 12px;
  background: var(--tile-empty);
  /* Halvgenomskinliga rutor, men stjärnorna bakom suddas ut så att de inte lyser igenom */
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  font-size: clamp(1.3rem, calc(var(--game-w) * 0.1), 2.8rem);
  font-weight: 700;
  text-transform: uppercase;
  user-select: none;
  aspect-ratio: 1;
}
.tile.filled { border-color: var(--border-filled); background: var(--tile-filled); animation: pop 120ms ease-in-out; }
.tile.correct, .tile.present, .tile.absent { border-color: transparent; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18); }
.tile.correct { background: var(--correct); color: var(--on-correct); }
.tile.present { background: var(--present); color: var(--on-present); }
.tile.absent { background: var(--absent); color: var(--on-absent); }
.tile.flip { animation: flip-in 250ms ease-in forwards; }
.tile.flip-out { animation: flip-out 250ms ease-out forwards; }
.row.shake { animation: shake 500ms; }
.tile.bounce { animation: bounce 1s ease; }

@keyframes pop { 50% { transform: scale(1.12); } }
@keyframes flip-in { from { transform: rotateX(0); } to { transform: rotateX(-90deg); } }
@keyframes flip-out { from { transform: rotateX(-90deg); } to { transform: rotateX(0); } }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-30px) rotate(-6deg); }
  50% { transform: translateY(5px); }
  60% { transform: translateY(-15px) rotate(4deg); }
  80% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}
@keyframes wobble {
  0%, 100% { transform: rotate(0); }
  45% { transform: rotate(0); }
  50% { transform: rotate(-8deg); }
  55% { transform: rotate(6deg); }
  60% { transform: rotate(0); }
}
@keyframes hop {
  0%, 100% { transform: translateY(0); }
  30% { transform: translateY(-14px) rotate(-5deg); }
  60% { transform: translateY(0); }
  75% { transform: translateY(-6px) rotate(3deg); }
}

/* ---------- Toasts ---------- */
#toasts {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 12px 20px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  transition: opacity 300ms;
}
.toast.fade { opacity: 0; }

/* ---------- Tangentbord ---------- */
#keyboard {
  flex-shrink: 0;
  padding: 0 6px 10px;
  width: calc(var(--kb-w) + 12px);
  max-width: 100%;
  margin: 0 auto;
  user-select: none;
}
/* Alla bokstavstangenter är lika breda (--u). Översta raden: 11 bokstäver + Sudda (1,5 --u)
   + 11 mellanrum à 5 px fyller hela bredden. Rad 2 är förskjuten som på ett riktigt tangentbord,
   rad 3 är centrerad med GISSA-knappen längst till höger. */
.kb-row {
  --u: calc((100% - 55px) / 12.5);
  display: flex;
  gap: 5px;
  margin-bottom: 8px;
  touch-action: manipulation;
}
.kb-row-2 .key:first-child { margin-left: calc(var(--u) * 0.3); }
.kb-row-3 { position: relative; justify-content: center; }
/* Utrymmet till höger om de centrerade bokstäverna är 2,75 --u + 12,5 px, så 2,5 --u får plats */
.key.guess {
  position: absolute;
  right: 0;
  top: 0;
  width: calc(var(--u) * 2.5);
  background: #ff8a3d;
  color: #fff;
  box-shadow: 0 4px 0 #b85a1f;
  font-size: clamp(0.8rem, calc(var(--key-h) * 0.3), 1.6rem);
  font-weight: 700;
  letter-spacing: 0.04em;
}
.key.guess:active { box-shadow: 0 1px 0 #b85a1f; }
.key {
  flex: 0 0 var(--u);
  height: var(--key-h);
  border: none;
  border-radius: 10px;
  background: var(--key-bg);
  color: var(--key-fg);
  box-shadow: 0 4px 0 var(--key-shadow);
  font-weight: 600;
  font-size: clamp(1.1rem, calc(var(--key-h) * 0.38), 2.2rem);
  text-transform: uppercase;
  cursor: pointer;
  padding: 0;
  display: grid;
  place-items: center;
  min-width: 0;
  transition: transform 60ms, box-shadow 60ms;
}
.key.wide { flex: 1 1 0; font-size: clamp(0.8rem, calc(var(--key-h) * 0.22), 1.3rem); }
.key.wide svg { width: calc(var(--key-h) * 0.42); height: auto; }
.key.correct { background: var(--correct); color: var(--on-correct); box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3); }
.key.present { background: var(--present); color: var(--on-present); box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3); }
.key.absent { background: var(--absent-key); color: var(--on-absent-key); box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3); }
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-shadow); }

/* ---------- Kompisar på sidorna (räven och kaninen) ---------- */
/* --buddy-w sätts i app.js; 0 betyder att det inte finns plats och då döljs de. */
.buddy {
  position: absolute;
  bottom: 2px;
  width: var(--buddy-w, 0px);
  pointer-events: none;
  transform: translateX(-50%);
  transform-origin: 50% 100%;
}
.buddy svg { display: block; width: 100%; height: auto; overflow: visible; animation: sway 5s ease-in-out infinite; transform-origin: 50% 100%; }
/* Mitt i utrymmet mellan skärmkanten och spelplanen */
.buddy-left { left: calc((100% - var(--game-w)) / 4); }
.buddy-right { left: calc(100% - (100% - var(--game-w)) / 4); }
.buddy-right svg { animation-delay: -2.5s; }
.no-buddies .buddy { display: none; }
.celebrate .buddy svg { animation: hop 0.9s ease-in-out 3; }
.celebrate .buddy-right svg { animation-delay: 0.15s; }
@keyframes sway {
  0%, 100% { transform: rotate(-2deg); }
  50% { transform: rotate(2deg); }
}

/* ---------- Ugglan ---------- */
[data-owl] { display: block; }
.owl { width: 100%; height: 100%; display: block; overflow: visible; }
.owl .eyes-happy, .owl .eyes-sad { display: none; }
.happy .owl .eyes-open, .sad .owl .eyes-open { display: none; }
.happy .owl .eyes-happy, .sad .owl .eyes-sad { display: inline; }

/* ---------- Resultatruta (ersätter tangentbordet när ett ord är klart) ---------- */
#end-panel {
  flex-shrink: 0;
  width: calc(var(--kb-w) + 12px);
  max-width: 100%;
  margin: 0 auto 10px;
  min-height: calc(3 * var(--key-h) + 24px);
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--panel);
  border-radius: 24px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.25);
  animation: slide-in 300ms ease-out;
}
.end-owl { width: 104px; height: 114px; flex-shrink: 0; }
.end-owl.happy .owl { animation: hop 1.2s ease-in-out infinite; transform-origin: 50% 100%; }
.end-body { display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; min-width: 0; }
#end-title { font-size: 28px; font-weight: 700; }
#end-text { font-size: 17px; color: var(--muted); }
#end-word {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #ffd166;
}
#btn-next { flex: none; width: min(260px, 100%); margin-top: 10px; }

/* ---------- Konfetti ---------- */
#confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 30; }
.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translate(var(--drift), 110vh) rotate(var(--spin)); }
}

/* ---------- Knappar ---------- */
.primary {
  border: none;
  border-radius: 16px;
  font-weight: 700;
  cursor: pointer;
  font-size: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 60ms, box-shadow 60ms;
  background: var(--correct);
  color: var(--on-correct);
  height: 56px;
  padding: 0 24px;
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.3);
}
.primary:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3); }
button:focus, a:focus { outline: none; }
button:focus-visible, a:focus-visible { outline: 3px solid rgba(255, 255, 255, 0.45); outline-offset: 3px; }

/* ---------- Dialoger ---------- */
dialog {
  background: var(--panel);
  color: var(--fg);
  border: none;
  border-radius: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 40px);
  padding: 22px 24px 24px;
}
dialog::backdrop { background: rgba(15, 12, 35, 0.7); }
dialog[open] { animation: slide-in 220ms ease-out; }
@keyframes slide-in { from { transform: translateY(30px); opacity: 0; } }
dialog h2 { margin: 0 0 14px; font-size: 22px; text-align: center; font-weight: 700; }
dialog p, dialog li { font-size: 16px; line-height: 1.45; }
dialog ul { padding-left: 20px; margin: 14px 0; }
.close {
  position: absolute;
  top: 12px;
  right: 14px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  border: none;
  color: var(--fg);
  font-size: 24px;
  cursor: pointer;
  line-height: 1;
}
.muted { color: var(--muted); font-size: 14px !important; }
.example { display: flex; gap: 5px; margin-top: 12px; }
.example .tile { width: 42px; height: 42px; font-size: 1.4rem; border-radius: 9px; }
.c-correct { color: var(--correct); }
.c-present { color: #b98cff; } /* ljusare än rutorna, för läsbarhet mot mörk bakgrund */
.c-absent { color: var(--on-absent); }
#btn-help-start { width: 100%; margin-top: 8px; }
.link-btn {
  display: block;
  margin: 14px auto 0;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14px;
  text-decoration: underline;
  cursor: pointer;
}
.credits { padding-left: 18px; }
.credits li { font-size: 15px; margin-bottom: 8px; }
.credits a { color: #b98cff; }

.speech { display: flex; align-items: center; gap: 12px; margin-right: 30px; }
.help-owl { width: 76px; height: 84px; flex-shrink: 0; }
.bubble {
  position: relative;
  background: #fff;
  color: #2b2140;
  padding: 12px 14px;
  border-radius: 18px;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.3;
}
.bubble::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  border: 8px solid transparent;
  border-right: 12px solid #fff;
  border-left: 0;
}

/* ---------- Små och låga skärmar ---------- */
/* Mindre bokstavsbrickor på smala skärmar, så att ugglan i sidhuvudet också får plats */
@media (max-width: 560px) {
  h1 { gap: 3px; }
  h1 span { width: 27px; height: 30px; font-size: 18px; border-radius: 7px; }
  .header-owl { width: 36px; height: 40px; }
}
@media (max-width: 380px) {
  header { padding: 0 8px; }
  .title { gap: 6px; }
  h1 span { width: 24px; height: 27px; font-size: 16px; border-radius: 6px; }
  .header-owl { width: 32px; height: 36px; }
  .end-owl { width: 80px; height: 88px; }
}
@media (max-height: 640px) {
  header { height: 50px; }
  .header-owl { display: none; }
  .end-owl { width: 80px; height: 88px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
  #confetti { display: none; }
}
