:root {
  --bg: #fef6e4;
  --ink: #2b2140;
  --card-bg: #ffffff;
  --accent: #f582ae;
  --accent-2: #8bd3dd;
  --accent-3: #b8f2e6;
  --good: #f5a524;
  --good-2: #e8830a;
  --bad: #ef4444;
  --shadow: 0 10px 30px rgba(43, 33, 64, 0.15);
  --radius: 22px;

  --range10-a: #37b893;
  --range10-b: #1f8f6f;
  --range20-a: #4a9ee8;
  --range20-b: #2f74c0;
  --range100-a: #ab94d6;
  --range100-b: #8874b8;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  font-family: 'Fredoka', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: hidden;
}

#app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(12px, 3vw, 32px);
  opacity: 0;
  transition: opacity 0.35s ease;
}

.screen--active {
  display: flex;
  opacity: 1;
}

.title {
  font-size: clamp(1.8rem, 5vw, 2.8rem);
  font-weight: 700;
  margin: 0 0 4px;
  text-align: center;
}

.subtitle {
  margin: 0 0 20px;
  font-size: clamp(0.95rem, 2vw, 1.15rem);
  opacity: 0.75;
  text-align: center;
}

/* ---------- START SCREEN ---------- */

.start-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(20px, 3vw, 36px);
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
}

.field {
  margin-bottom: 20px;
}

.field label {
  display: block;
  font-weight: 600;
  margin-bottom: 8px;
  font-size: 1rem;
}

#player-name {
  width: 100%;
  font-family: inherit;
  font-size: 1.1rem;
  padding: 14px 16px;
  border-radius: 14px;
  border: 2px solid #e6dfef;
  outline: none;
  background: #fbfaff;
  color: var(--ink);
}

#player-name:focus {
  border-color: var(--accent-2);
}

.range-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.range-btn {
  font-family: inherit;
  border: 3px solid transparent;
  border-radius: 16px;
  background: #f4f1fb;
  padding: 14px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
  min-height: 76px;
}

.range-btn:active {
  transform: scale(0.96);
}

.range-num {
  font-size: 1.6rem;
  font-weight: 700;
}

.range-label {
  font-size: 0.8rem;
  opacity: 0.7;
}

.range-btn.selected {
  background: linear-gradient(135deg, var(--accent-2), var(--accent-3));
  border-color: var(--accent);
}

.btn {
  font-family: inherit;
  border: none;
  border-radius: 16px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-big {
  width: 100%;
  padding: 16px;
  font-size: 1.2rem;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #ff8fbf);
  color: white;
  box-shadow: 0 6px 18px rgba(245, 130, 174, 0.45);
}

.btn-secondary {
  background: #eee9f7;
  color: var(--ink);
}

.highscore-panel {
  margin-top: 24px;
  padding-top: 18px;
  border-top: 2px dashed #ece6f7;
}

.highscore-panel h2 {
  font-size: 1.05rem;
  margin: 0 0 10px;
}

.highscore-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.highscore-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: #f8f6fd;
  font-size: 0.95rem;
}

.highscore-list li .hs-rank {
  font-weight: 700;
  width: 1.6em;
  text-align: center;
}

.highscore-list li .hs-avatar {
  flex: 1;
  display: flex;
  align-items: center;
}

.highscore-list li .hs-avatar svg {
  display: block;
  border-radius: 50%;
  background: #fff;
}

.highscore-list li .hs-time {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.highscore-empty {
  opacity: 0.6;
  font-style: italic;
  justify-content: center;
}

.highscore-list li.hs-new {
  background: linear-gradient(135deg, var(--accent-3), #fff5cc);
  outline: 2px solid var(--accent);
}

/* ---------- COUNTDOWN ---------- */

.countdown-number {
  font-size: clamp(6rem, 30vw, 14rem);
  font-weight: 700;
  color: var(--accent);
  text-shadow: 0 8px 24px rgba(245, 130, 174, 0.4);
  animation: pop 0.5s ease;
}

@keyframes pop {
  0% { transform: scale(0.4); opacity: 0; }
  60% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------- GAME SCREEN ---------- */

#screen-game {
  justify-content: flex-start;
  padding: 10px clamp(10px, 2vw, 24px);
}

.game-header {
  width: 100%;
  max-width: 1400px;
  display: flex;
  align-items: center;
  gap: clamp(8px, 2vw, 20px);
  background: var(--card-bg);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 10px 18px;
  margin-bottom: clamp(10px, 2vw, 22px);
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 70px;
}

.hud-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.55;
}

.hud-value {
  font-size: 1.25rem;
  font-weight: 700;
}

.hud-value--timer {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.hud-timer {
  flex: 1;
}

.btn-quit {
  margin-left: auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f3ecf7;
  color: var(--ink);
  font-size: 1.1rem;
  flex: none;
}

.game-hint {
  width: 100%;
  max-width: 1400px;
  margin: 0 0 10px;
  text-align: center;
  font-size: 0.95rem;
  opacity: 0.7;
}

.board {
  width: 100%;
  max-width: 1400px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 12px;
  min-height: 0;
  position: relative;
}

.board-section {
  background: rgba(255,255,255,0.5);
  border-radius: 18px;
  padding: 10px clamp(8px, 1.5vw, 16px) 14px;
}

.board-section--top {
  background: rgba(255,255,255,0.75);
}

.board-section-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.55;
  margin-bottom: 8px;
}

.board-divider {
  position: relative;
  text-align: center;
  color: var(--accent);
  opacity: 0.6;
  font-size: 1.3rem;
  letter-spacing: 0.4em;
  height: clamp(64px, 14vh, 140px);
  margin: 4px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.board-divider::before {
  content: "";
  position: absolute;
  left: 6%;
  right: 6%;
  top: 50%;
  border-top: 2px dashed rgba(245, 130, 174, 0.4);
}

.board-divider span {
  position: relative;
  background: var(--bg);
  padding: 0 10px;
  animation: bob 1.4s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(4px); }
}

.board-grid {
  display: grid;
  gap: clamp(4px, 1vw, 10px);
  align-content: start;
  align-items: start;
}

/* ---------- Board mode switch (classic grid vs. hearts, range 10) ---------- */

.board-classic {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.board-hearts {
  display: none;
  height: 100%;
  min-height: clamp(360px, 62vh, 640px);
  flex-direction: row;
  align-items: stretch;
  gap: 0;
}

.board.board--hearts .board-classic {
  display: none;
}

.board.board--hearts .board-hearts {
  display: flex;
}

.hearts-zone {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 100%;
  background: rgba(255,255,255,0.5);
  border-radius: 18px;
  /* Kein overflow:hidden - eine Herzhaelfte muss beim Ziehen ueber die
     Trennlinie hinweg in die andere Zone hinein sichtbar bleiben. */
}

.hearts-zone--left {
  background: rgba(255,255,255,0.5);
}

.hearts-zone--right {
  background: rgba(255,255,255,0.75);
}

.hearts-divider {
  position: relative;
  width: clamp(28px, 4vw, 56px);
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  opacity: 0.6;
  font-size: 1.3rem;
  pointer-events: none;
}

.hearts-divider::before {
  content: "";
  position: absolute;
  top: 6%;
  bottom: 6%;
  left: 50%;
  border-left: 2px dashed rgba(245, 130, 174, 0.4);
}

.hearts-divider span {
  position: relative;
  background: var(--bg);
  padding: 8px 0;
  animation: hearts-nudge 1.4s ease-in-out infinite;
}

@keyframes hearts-nudge {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(4px); }
}

/* ---------- Heart-half pieces ---------- */

.heart-piece {
  position: absolute;
  /* Breite ergibt sich aus der Hoehe im exakten Seitenverhaeltnis der viewBox
     (12:22), damit die Herz-SVG nie gestaucht/verzerrt wird. */
  height: clamp(115px, 15.5vw, 190px);
  width: calc(clamp(115px, 15.5vw, 190px) * 12 / 22);
  --drag-x: 0px;
  --drag-y: 0px;
  --heart-fill: var(--range10-a);
  transform: translate(calc(-50% + var(--drag-x)), calc(-50% + var(--drag-y)));
  transition: transform 0.28s cubic-bezier(.34,1.56,.64,1);
  will-change: transform;
  user-select: none;
  -webkit-user-select: none;
}

.heart-piece-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 5px 8px rgba(0,0,0,0.22));
}

.heart-piece-svg path {
  fill: var(--heart-fill);
  transition: fill 0.25s ease;
}

.heart-piece-num {
  font-family: 'Fredoka', 'Segoe UI', system-ui, sans-serif;
  font-weight: 700;
  fill: white;
  pointer-events: none;
}

/* Das Schrumpfen beim Andocken passiert per JS direkt ueber das font-size-
   Attribut (siehe dockHeartPair in app.js), nicht per CSS-transform: scale
   mit transform-box:fill-box - das ist fuer <text>-Elemente in Safari
   unzuverlaessig (der Skalierungsursprung landet dort nicht zuverlaessig auf
   der eigenen Bounding Box), wodurch die Zahl sichtbar aus der Mitte der
   Haelfte heraus in Richtung Viewport-Ecke rutschte. */

.heart-piece--left,
.heart-piece--right {
  cursor: grab;
  touch-action: none;
  z-index: 10;
}

.heart-piece--left.dragging,
.heart-piece--right.dragging {
  cursor: grabbing;
  transition: none;
  z-index: 50;
}

.heart-piece--docked {
  --heart-fill: var(--good);
  pointer-events: none;
  transition: left 0.4s cubic-bezier(.34,1.56,.64,1), top 0.4s cubic-bezier(.34,1.56,.64,1), transform 0.4s cubic-bezier(.34,1.56,.64,1);
}

.heart-piece--docked .heart-piece-svg {
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.2));
}

.heart-piece--right.heart-piece--docked {
  z-index: 6;
}

.heart-seal {
  position: absolute;
  width: clamp(26px, 3.6vw, 36px);
  height: clamp(26px, 3.6vw, 36px);
  border-radius: 50%;
  background: linear-gradient(135deg, #ffe08a, var(--good));
  color: #5b3a00;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(0.75rem, 1.3vw, 0.95rem);
  box-shadow: 0 4px 10px rgba(0,0,0,0.3), 0 0 0 3px rgba(255,255,255,0.85) inset;
  transform: translate(-50%, -50%) scale(0);
  animation: seal-pop 0.5s cubic-bezier(.34,1.56,.64,1) forwards;
  z-index: 45;
  pointer-events: none;
}

@keyframes seal-pop {
  to { transform: translate(-50%, -50%) scale(1); }
}

.heart-seal-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: clamp(0.6rem, 1.05vw, 0.75rem);
  font-weight: 700;
  color: var(--good-2);
  line-height: 1;
  text-shadow: 0 1px 2px rgba(0,0,0,0.2);
  pointer-events: none;
}

.heart-seal-arrow--left {
  right: 100%;
  margin-right: 2px;
}

.heart-seal-arrow--right {
  left: 100%;
  margin-left: 2px;
}

.tile {
  position: relative;
  height: clamp(92px, 15vh, 132px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 3px 8px;
  font-weight: 700;
  color: white;
  border-radius: 16px;
  background: linear-gradient(150deg, var(--range10-a), var(--range10-b));
  box-shadow: 0 6px 0 rgba(0,0,0,0.15), 0 10px 18px rgba(0,0,0,0.18);
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.28s cubic-bezier(.34,1.56,.64,1), box-shadow 0.2s ease, opacity 0.35s ease, background 0.3s ease, border-radius 0.3s ease;
  will-change: transform;
}

.tile:not(.has-dots) {
  font-size: clamp(1.6rem, 4.2vw, 2.4rem);
}

.tile-bottom {
  cursor: grab;
  touch-action: none;
}

.tile-bottom::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -9px;
  width: 22px;
  height: 16px;
  border-radius: 50%;
  background: inherit;
  transform: translateX(-50%);
}

.tile-top::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -9px;
  width: 22px;
  height: 16px;
  border-radius: 50%;
  background: var(--bg);
  transform: translateX(-50%);
}

.board[data-range="10"] .tile { background: linear-gradient(150deg, var(--range10-a), var(--range10-b)); }
.board[data-range="20"] .tile { background: linear-gradient(150deg, var(--range20-a), var(--range20-b)); }
.board[data-range="100"] .tile { background: linear-gradient(150deg, var(--range100-a), var(--range100-b)); }

.tile.dragging {
  cursor: grabbing;
  z-index: 50;
  transition: none;
  box-shadow: 0 16px 26px rgba(0,0,0,0.28);
}

.tile.tile--wrong {
  animation: shake 0.4s ease;
  box-shadow: 0 0 0 4px var(--bad), 0 10px 18px rgba(0,0,0,0.18);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px) rotate(-3deg); }
  40% { transform: translateX(8px) rotate(3deg); }
  60% { transform: translateX(-6px) rotate(-2deg); }
  80% { transform: translateX(6px) rotate(2deg); }
}

/* ---------- Docked (matched) pairs stay visible and fuse together ---------- */

.tile-top.tile-top--docked {
  background: linear-gradient(150deg, var(--good), var(--good-2)) !important;
  pointer-events: none;
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15), 0 6px 12px rgba(0,0,0,0.18);
}

.tile-top.tile-top--docked::after {
  display: none;
}

.tile-bottom.tile-bottom--docked {
  background: linear-gradient(150deg, var(--good), var(--good-2)) !important;
  pointer-events: none;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15), 0 6px 12px rgba(0,0,0,0.18);
  z-index: 5;
}

.tile-bottom.tile-bottom--docked::before {
  display: none;
}

.dock-heart {
  position: absolute;
  left: 50%;
  bottom: -13px;
  transform: translateX(-50%);
  font-size: 1rem;
  line-height: 1;
  z-index: 6;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.25));
}

.tile-check {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: white;
  color: var(--good);
  font-size: 0.9rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,0.25);
}

.tile-num {
  line-height: 1;
  font-weight: 600;
}

.tile.has-dots .tile-num {
  font-size: clamp(1.35rem, 3.4vw, 1.9rem);
}

/* Würfelbilder */

.tile-dice {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: clamp(16px, 3.4vw, 24px);
}

.die {
  width: clamp(16px, 3.4vw, 24px);
  height: clamp(16px, 3.4vw, 24px);
  background: rgba(255,255,255,0.95);
  border-radius: 4px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 2px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}

.pip-cell {
  display: flex;
  align-items: center;
  justify-content: center;
}

.pip-cell.active::after {
  content: "";
  display: block;
  width: 68%;
  height: 68%;
  border-radius: 50%;
  background: var(--ink);
}

/* Plättchen (mit Leerstellen) */

.tile-counters {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  width: 100%;
}

.counter-slot {
  aspect-ratio: 1 / 1;
  border-radius: 50%;
}

.counter-slot--filled {
  background: rgba(255,255,255,0.95);
}

.counter-slot--empty {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px rgba(255,255,255,0.65);
}

/* ---------- RESULT SCREEN ---------- */

.result-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(20px, 3vw, 36px);
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  text-align: center;
}

.newrecord {
  color: var(--accent);
  font-weight: 700;
  font-size: 1.1rem;
  margin: 0 0 16px;
}

.hidden {
  display: none !important;
}

.result-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-bottom: 12px;
}

.result-stat {
  display: flex;
  flex-direction: column;
  background: #f8f6fd;
  border-radius: 16px;
  padding: 14px 22px;
  min-width: 110px;
}

.result-stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.result-stat-label {
  font-size: 0.8rem;
  opacity: 0.6;
  margin-top: 4px;
}

.highscore-name-prompt {
  background: linear-gradient(135deg, var(--accent-3), #fff5cc);
  border-radius: 16px;
  padding: 16px;
  margin: 4px 0 20px;
}

.highscore-name-prompt-text {
  margin: 0 0 10px;
  font-weight: 600;
}

.avatar-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 10px;
}

.avatar-preview {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  flex: none;
}

.avatar-preview svg {
  display: block;
}

.avatar-nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.btn-avatar-arrow {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: #eee9f7;
  color: var(--ink);
  font-size: 0.8rem;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.btn-avatar-arrow:active {
  transform: scale(0.92);
}

.btn-avatar-arrow:disabled {
  opacity: 0.35;
  cursor: default;
}

.avatar-reroll {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.avatar-reroll-count {
  font-size: 0.75rem;
  opacity: 0.65;
  font-variant-numeric: tabular-nums;
}

#btn-reroll-avatar {
  padding: 10px 16px;
  white-space: nowrap;
}

#btn-reroll-avatar:disabled {
  opacity: 0.4;
  cursor: default;
}

#btn-save-highscore {
  padding: 12px 18px;
  white-space: nowrap;
}

.highscore-name-error {
  margin: 8px 0 0;
  color: #c0392b;
  font-weight: 600;
  font-size: 0.9rem;
}

.result-actions {
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---------- CONFETTI ---------- */

#confetti-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 999;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 16px;
  opacity: 0.9;
  animation-name: confetti-fall;
  animation-timing-function: cubic-bezier(.4,0,.6,1);
  animation-fill-mode: forwards;
}

@keyframes confetti-fall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(105vh) rotate(540deg); opacity: 0.4; }
}

/* ---------- Small screens / portrait tablet tweaks ---------- */

@media (max-width: 600px) {
  .game-header {
    flex-wrap: wrap;
  }
  .result-stats {
    gap: 12px;
  }
}
