/*
  Färgschema: rött + komplement (krämvitt och mörk vinröd).
  Varningen när tiden är ute använder vitt så att den syns mot allt det röda.
*/
:root {
  --wine: #2b060c;
  --burgundy: #5a0d18;
  --red: #c8102e;
  --red-light: #e23a4e;
  --red-dark: #8f0c22;
  --cream: #fff6ec;
  --ink: #fff6ec;
  --muted: #efc9cd;
  --line: rgba(255, 246, 236, 0.22);
  --panel: rgba(43, 6, 12, 0.55);
  --alert: #ffffff;
  --radius: 24px;
  --emoji-font: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  --candy: repeating-linear-gradient(45deg, var(--red) 0 14px, var(--cream) 14px 28px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { height: 100%; }
body { min-height: 100%; }
.lights, .topbar { flex-shrink: 0; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1000px 600px at 50% -10%, rgba(226, 58, 78, 0.45) 0%, transparent 65%),
    linear-gradient(180deg, var(--burgundy) 0%, var(--wine) 100%);
  background-attachment: fixed;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* ---------- Snö ---------- */
.snow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.flake {
  position: absolute;
  top: -5vh;
  color: var(--cream);
  user-select: none;
  animation: fall linear infinite, sway ease-in-out infinite alternate;
}
@keyframes fall { to { transform: translateY(110vh); } }
@keyframes sway { from { margin-left: -20px; } to { margin-left: 20px; } }

/* Snötäcke längst ner */
body::after {
  content: "";
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: 34px;
  pointer-events: none;
  z-index: 0;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 400 34' preserveAspectRatio='none'%3E%3Cpath d='M0 20 Q 40 6 90 16 T 180 14 T 270 18 T 340 10 T 400 20 V34 H0 Z' fill='%23fff6ec'/%3E%3C/svg%3E") repeat-x bottom / 400px 34px;
  opacity: 0.95;
}

/* ---------- Julbelysning (en enda varm färg) ---------- */
.lights {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  height: 30px;
  overflow: hidden;
}
.lights::before {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 6px;
  border-top: 2px solid rgba(255, 246, 236, 0.25);
}
.bulb {
  position: relative;
  margin-top: 8px;
  width: 10px;
  height: 14px;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  background: #fff1dc;
  box-shadow: 0 3px 12px 2px rgba(255, 225, 190, 0.7);
  animation: twinkle 2.4s ease-in-out infinite;
}
.bulb:nth-child(2n) { animation-delay: 1.2s; }
@keyframes twinkle {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

/* ---------- Topbar ---------- */
.topbar {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 22px 14px;
}
.logo {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0.02em;
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
}
.logo .sub { color: var(--muted); font-size: 0.95rem; margin-left: 8px; font-style: normal; font-family: system-ui, sans-serif; }
.icon-btn {
  font-size: 1.3rem;
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: inherit;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.icon-btn-small { width: 36px; height: 36px; font-size: 1rem; flex: none; }

main {
  position: relative;
  z-index: 1;
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 8px 16px 60px;
}

/* ---------- Skärmar ---------- */
.screen { display: none; width: 100%; max-width: 760px; flex-direction: column; align-items: center; }
.screen.active { display: flex; animation: fade-in 0.35s ease; }

.panel {
  position: relative;
  width: 100%;
  max-width: 560px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 30px 28px 28px;
  backdrop-filter: blur(8px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}
.panel h2 {
  margin: 0 0 8px;
  font-size: 2.1rem;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--cream);
}
.lead { color: var(--muted); line-height: 1.5; margin: 0 0 20px; }
.rules-title { margin: 0 0 8px; font-size: 1.05rem; }
.rules {
  margin: 0 0 14px;
  padding-left: 1.4em;
  color: var(--muted);
  line-height: 1.45;
}
.rules li { margin-bottom: 4px; }
.rules li::marker { color: var(--cream); font-weight: 700; }
.note { color: var(--muted); font-size: 0.9rem; text-align: center; margin: 16px 0 0; }

fieldset { border: 0; padding: 0; margin: 0 0 22px; }
legend { font-weight: 700; margin-bottom: 10px; font-size: 1.05rem; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
  padding: 8px 14px;
  border-radius: 999px;
  font: inherit;
  cursor: pointer;
}
.chip:hover { background: rgba(255, 255, 255, 0.08); }
.chip.selected { background: var(--cream); color: var(--red-dark); border-color: var(--cream); font-weight: 700; }

.inline-input { display: inline-flex; align-items: center; gap: 10px; color: var(--muted); }
.inline-input input {
  width: 96px;
  font: inherit;
  font-size: 1.2rem;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.3);
  color: var(--ink);
}
.inline-input input:focus { outline: 2px solid var(--cream); outline-offset: 1px; }

/* ---------- Knappar ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  padding: 12px 20px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.1s ease, background 0.2s ease, opacity 0.2s;
}
.btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary {
  background: var(--red);
  color: #fff;
  border: 1px solid var(--red-light);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}
.btn-primary:hover:not(:disabled) { background: var(--red-light); }
.btn-big { width: 100%; font-size: 1.2rem; padding: 16px; }

button:focus-visible { outline: 3px solid var(--cream); outline-offset: 3px; }
/* Mobil: inga dubbeltrycks-zoomningar eller grå blink vid tryck */
button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

/* ---------- Spelhuvud ---------- */
.game-head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.head-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.counter { font-weight: 700; font-size: 1.1rem; color: var(--muted); white-space: nowrap; }

.timer {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 2.2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  padding: 6px 20px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  border: 2px solid var(--line);
  min-width: 140px;
  justify-content: center;
  transition: background 0.3s, border-color 0.3s, color 0.3s;
}
/* Sista sekunderna: rött och pulserande */
.timer.warn { background: var(--red); border-color: var(--cream); animation: pulse 1s ease-in-out infinite; }
#game.is-paused .timer { opacity: 0.6; }
/* Tiden ute: vitt med röd text – syns tydligt mot den röda bakgrunden */
#game.is-timeup .timer {
  background: var(--alert);
  border-color: var(--alert);
  color: var(--red);
  animation: pulse 0.8s ease-in-out 3;
}

/* Polkagrisrandig förloppsindikator */
.progress {
  width: 100%;
  height: 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  overflow: hidden;
  margin-bottom: 14px;
}
.progress-bar {
  height: 100%;
  width: 100%;
  background: var(--candy);
  background-size: 40px 40px;
  border-radius: inherit;
  transition: width 0.1s linear;
  animation: candy-move 1s linear infinite;
}
.progress-bar.warn { background: var(--red-light); animation: none; }
#game.is-timeup .progress-bar { background: var(--alert); animation: none; }
@keyframes candy-move { to { background-position: 40px 0; } }

.status {
  min-height: 1.6em;
  margin: 0 0 14px;
  font-size: 1.15rem;
  font-weight: 600;
  text-align: center;
}

/* ---------- Kortet ---------- */
/* ---------- Spelläge: sidan låses till skärmhöjden ---------- */
body.in-game { height: 100vh; height: 100dvh; overflow: hidden; }
body.in-game main { min-height: 0; padding-bottom: 30px; }
#game { min-height: 0; }

.card-wrap {
  width: 100%;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  container-type: size;
}
.card {
  /* Så stort som får plats – både på bredden och höjden */
  width: min(100cqw, 680px, 100cqh * 1.6);
  aspect-ratio: 16 / 10;
  container-type: size;
  flex: none;
  perspective: 1400px;
  border: 0;
  padding: 0;
  background: none;
  font: inherit;
  color: inherit;
  cursor: not-allowed;
  border-radius: var(--radius);
}
#game.is-timeup .card { cursor: pointer; }

.card-inner {
  display: block;
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.7s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.card.flipped .card-inner { transform: rotateY(180deg); }
.card.no-anim .card-inner { transition: none; }

.card-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: min(24px, 5cqw);
  border-radius: var(--radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

/* Framsida: krämvit med polkagriskant */
.card-front {
  color: var(--wine);
  border: 10px solid transparent;
  background:
    linear-gradient(var(--cream), var(--cream)) padding-box,
    var(--candy) border-box;
  transition: box-shadow 0.3s;
}

.emojis {
  font-family: var(--emoji-font);
  /* --emoji-scale sätts från JS beroende på hur lång rebusen är */
  /* --emoji-fit krymper rebusen ytterligare om den inte ryms på en rad (mäts i JS) */
  font-size: calc(min(24cqw, 40cqh, 7.5rem) * var(--emoji-scale, 1) * var(--emoji-fit, 1));
  white-space: nowrap;
  line-height: 1.1;
  letter-spacing: 0.08em;
  word-spacing: -0.25em;
  text-align: center;
  transition: filter 0.3s;
}

.paused-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: min(2rem, 9cqw);
  font-weight: 800;
  color: var(--red-dark);
  background: rgba(255, 246, 236, 0.6);
}
#game.is-paused .emojis { filter: blur(18px); }
#game.is-paused .paused-overlay { display: flex; }

.flip-hint {
  position: absolute;
  bottom: min(16px, 5cqh);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  background: var(--red);
  color: #fff;
  font-weight: 700;
  font-size: min(1rem, 4.6cqw);
  padding: 0.5em 1em;
  border-radius: 999px;
  display: none;
  animation: bob 1.2s ease-in-out infinite;
}
#game.is-timeup .card:not(.flipped) .flip-hint { display: block; }
#game.is-timeup .card:not(.flipped) .card-front {
  background:
    linear-gradient(var(--cream), var(--cream)) padding-box,
    linear-gradient(var(--red), var(--red)) border-box;
  animation: glow 1.2s ease-in-out infinite;
}

/* Baksida: rött julkort med krämvit ram */
.card-back {
  transform: rotateY(180deg);
  background: radial-gradient(circle at 50% 35%, var(--red-light), var(--red) 55%, var(--red-dark));
  color: #fff;
  text-align: center;
  gap: min(6px, 1.5cqh);
  box-shadow: inset 0 0 0 10px var(--red), inset 0 0 0 12px rgba(255, 246, 236, 0.8), 0 24px 60px rgba(0, 0, 0, 0.5);
}
.card-back::before,
.card-back::after {
  content: "❄";
  position: absolute;
  font-size: min(2rem, 7cqw);
  color: rgba(255, 246, 236, 0.75);
  top: min(24px, 6cqh);
}
.card-back::before { left: min(30px, 6cqw); }
.card-back::after { right: min(30px, 6cqw); }
.answer-label { text-transform: uppercase; letter-spacing: 0.15em; font-size: min(0.85rem, 3.6cqw); color: var(--muted); }
.answer {
  font-size: calc(min(3.4rem, 11cqw, 17cqh) * var(--answer-scale, 1));
  font-weight: 900;
  line-height: 1.1;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.25);
  padding: 0 min(40px, 8cqw);
}
.artist { font-size: min(1.6rem, 6cqw); font-weight: 600; }
.explanation {
  margin-top: min(12px, 2cqh);
  font-family: system-ui, var(--emoji-font);
  font-size: min(1rem, 4.2cqw);
  background: rgba(0, 0, 0, 0.2);
  padding: 6px 14px;
  border-radius: 999px;
}
.explanation:empty { display: none; }

.card.wiggle { animation: wiggle 0.45s ease; }
.card-wrap.shake { animation: shake 0.6s ease; }

/* ---------- Kontroller ---------- */
.controls { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 22px; }

/* ---------- Tiden ute: vit blixt + banner ---------- */
.flash {
  position: fixed;
  inset: 0;
  background: var(--alert);
  opacity: 0;
  pointer-events: none;
  z-index: 50;
}
.flash.show { animation: flash 0.9s ease-out; }

.timeup-banner {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.4);
  z-index: 60;
  pointer-events: none;
  opacity: 0;
  width: max-content;
  max-width: 90vw;
  text-align: center;
  font-size: clamp(1.6rem, 8vw, 5rem);
  font-weight: 900;
  text-transform: uppercase;
  color: var(--red);
  background: var(--alert);
  padding: 0.35em 0.6em;
  border-radius: 24px;
  border: 6px solid var(--red);
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.6);
}
.timeup-banner.show { animation: banner 2.2s cubic-bezier(0.2, 0.9, 0.3, 1.3) forwards; }

/* ---------- Sammanfattning ---------- */
.summary { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.summary li {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--line);
  border-left: 4px solid var(--red);
}
.summary .s-emojis { font-family: var(--emoji-font); font-size: 1.8rem; min-width: 6rem; white-space: nowrap; word-spacing: -0.3em; }
.summary .s-answer { font-weight: 800; display: block; }
.summary .s-artist { color: var(--muted); font-size: 0.95rem; }

/* ---------- Animationer ---------- */
@keyframes fade-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@keyframes bob { 0%, 100% { transform: translate(-50%, 0); } 50% { transform: translate(-50%, -6px); } }
@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 6px rgba(255, 255, 255, 0.55), 0 24px 60px rgba(0, 0, 0, 0.5); }
  50% { box-shadow: 0 0 0 16px rgba(255, 255, 255, 0.15), 0 24px 60px rgba(0, 0, 0, 0.5); }
}
@keyframes wiggle {
  0%, 100% { transform: rotate(0); }
  20% { transform: rotate(-3deg); }
  40% { transform: rotate(3deg); }
  60% { transform: rotate(-2deg); }
  80% { transform: rotate(2deg); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15%, 45%, 75% { transform: translateX(-14px); }
  30%, 60%, 90% { transform: translateX(14px); }
}
@keyframes flash { 0% { opacity: 0.5; } 100% { opacity: 0; } }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4) rotate(-6deg); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.05) rotate(-2deg); }
  25% { transform: translate(-50%, -50%) scale(1) rotate(-2deg); }
  80% { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(-2deg); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.1) rotate(-2deg); }
}

/* ---------- Mobil (stående) ---------- */
@media (max-width: 520px) {
  .lights { height: 24px; }
  .bulb { margin-top: 6px; width: 8px; height: 11px; }
  .topbar { padding: 4px 14px 8px; }
  .logo { font-size: 1.15rem; }
  .logo .sub { display: none; }
  .icon-btn { width: 38px; height: 38px; font-size: 1.1rem; }
  main { padding: 4px 12px 50px; }

  .panel { padding: 22px 18px 18px; }
  .panel h2 { font-size: 1.6rem; }
  .lead { font-size: 0.95rem; margin-bottom: 16px; }
  .rules { font-size: 0.95rem; }
  fieldset { margin-bottom: 16px; }
  legend { margin-bottom: 8px; }
  .chips { gap: 6px; margin-bottom: 10px; }
  .chip { padding: 7px 12px; }
  .btn-big { padding: 14px; }

  .game-head { margin-bottom: 8px; }
  .counter { font-size: 1rem; }
  .timer { font-size: 1.7rem; min-width: 104px; padding: 2px 14px; }
  .progress { height: 10px; margin-bottom: 10px; }
  .status { font-size: 1rem; margin-bottom: 10px; }
  /* Kortet fyller bredden och växer på höjden (som ett spelkort) */
  .card { aspect-ratio: auto; width: 100cqw; height: min(100cqh, 115cqw); }
  .controls { margin-top: 14px; width: 100%; flex-wrap: nowrap; }
  .controls .btn { flex: 1 1 0; white-space: nowrap; padding: 14px 10px; font-size: 1.05rem; }

  body.in-game main { padding-bottom: 26px; }
  body.in-game::after { height: 20px; background-size: 400px 20px; }
}

/* Mycket små skärmar (t.ex. iPhone SE 1:a gen) */
@media (max-width: 340px) {
  .lights { display: none; }
  .timer { font-size: 1.45rem; min-width: 88px; }
  .counter { font-size: 0.9rem; }
}

/* ---------- Mobil (liggande): kort till vänster, kontroller till höger ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  .lights { display: none; }
  .topbar { padding: 6px 16px; }
  main { padding: 4px 12px 30px; }

  body.in-game .topbar,
  body.in-game::after { display: none; }
  body.in-game main { padding: 10px 14px; }

  #game.active {
    max-width: none;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(180px, 28%);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
      "card head"
      "card progress"
      "card status"
      "card controls";
    column-gap: 16px;
    align-items: start;
  }
  .game-head { grid-area: head; flex-direction: column; align-items: stretch; gap: 8px; }
  .timer { font-size: 1.8rem; min-width: 0; }
  .progress { grid-area: progress; height: 10px; margin-bottom: 10px; }
  .status { grid-area: status; font-size: 0.95rem; text-align: left; margin-bottom: 10px; }
  .card-wrap { grid-area: card; height: 100%; }
  .card { aspect-ratio: 16 / 10; width: min(100cqw, 100cqh * 1.6); }
  .controls { grid-area: controls; flex-direction: column; margin-top: 0; align-self: end; }
  .controls .btn { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .card-inner { transition-duration: 0.2s; }
  .card-wrap.shake, .card.wiggle, .timer.warn, .flip-hint, .bulb, .progress-bar { animation: none; }
  .snow { display: none; }
}
