:root {
  --bg: #0d1424;
  --bg2: #141d33;
  --panel: #1a2540;
  --line: #26345a;
  --empty: #1e2a49;
  --text: #eef2ff;
  --muted: #8d9bc2;
  --accent: #ffb547;
  --good: #5fe08a;
  --bad: #ff6b6b;
  --gap: 4px;
  --board: min(calc(100vw - 32px), 460px, calc(100svh - 300px));
  --cell: calc((var(--board) - 16px - 7 * var(--gap)) / 8);
  --tray-cell: calc(var(--cell) * 0.52);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at top, #1b2a55 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }

.app {
  min-height: 100svh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px env(safe-area-inset-bottom);
}

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0 4px;
}
.brand { font-weight: 800; font-size: 20px; letter-spacing: .5px; display: flex; align-items: center; gap: 10px; }
.logo {
  width: 22px; height: 22px; border-radius: 5px;
  background:
    linear-gradient(#ff8a3d, #ff8a3d) 0 0 / 10px 10px no-repeat,
    linear-gradient(#3db8ff, #3db8ff) 12px 0 / 10px 10px no-repeat,
    linear-gradient(#5fe08a, #5fe08a) 0 12px / 10px 10px no-repeat,
    linear-gradient(#b67bff, #b67bff) 12px 12px / 10px 10px no-repeat;
}
.stats { display: flex; align-items: center; gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; margin-right: 6px; }
.stat .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.stat span:last-child { font-weight: 700; font-size: 17px; color: var(--accent); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: #22305a; }

.play { display: flex; flex-direction: column; align-items: center; gap: 12px; padding-bottom: 16px; }
.score-wrap { position: relative; text-align: center; min-height: 64px; }
.score { font-size: 46px; font-weight: 800; line-height: 1.2; font-variant-numeric: tabular-nums; }
.combo {
  position: absolute; left: 50%; top: 100%; transform: translateX(-50%);
  font-weight: 800; color: var(--accent); white-space: nowrap; font-size: 15px; opacity: 0;
}
.combo.show { animation: comboIn 1.2s ease-out; }
@keyframes comboIn { 0% { opacity: 0; transform: translate(-50%, 6px) scale(.8); } 15% { opacity: 1; transform: translate(-50%, 0) scale(1.1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -6px); } }

.board-wrap { position: relative; }
.board {
  width: var(--board); height: var(--board);
  display: grid; grid-template-columns: repeat(8, 1fr); gap: var(--gap);
  padding: 8px; border-radius: 16px;
  background: var(--bg2); border: 1px solid var(--line);
  box-shadow: 0 20px 50px rgba(0,0,0,.35), inset 0 0 0 1px rgba(255,255,255,.02);
  touch-action: none;
}
.cell { border-radius: 6px; background: var(--empty); position: relative; }
.cell.filled, .pcell { box-shadow: inset 0 -4px 0 rgba(0,0,0,.25), inset 0 3px 0 rgba(255,255,255,.28); }
.cell.ghost { opacity: .45; }
.cell.will-clear { filter: brightness(1.45) saturate(1.2); }
.cell.pop { animation: pop .38s ease-in forwards; }
@keyframes pop { 0% { transform: scale(1); filter: brightness(1.8); } 100% { transform: scale(0); opacity: 0; } }

.fx { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.float {
  position: absolute; font-weight: 800; font-size: 22px; color: #fff; white-space: nowrap;
  text-shadow: 0 2px 8px rgba(0,0,0,.6); transform: translate(-50%, -50%);
  animation: floatUp 1s ease-out forwards;
}
@keyframes floatUp { 0% { opacity: 0; margin-top: 10px; } 20% { opacity: 1; } 100% { opacity: 0; margin-top: -40px; } }

.tray {
  width: var(--board); height: calc(var(--tray-cell) * 5 + 24px);
  display: grid; grid-template-columns: repeat(3, 1fr); align-items: center; justify-items: center;
  touch-action: none;
}
.slot { width: 100%; height: 100%; display: grid; place-items: center; cursor: grab; touch-action: none; }
.slot.empty { cursor: default; }
.slot.nofit .piece { opacity: .3; filter: grayscale(.7); }
.piece { display: grid; gap: 3px; transition: transform .15s; }
.slot:not(.empty):hover .piece { transform: scale(1.06); }
.slot.dragging .piece { opacity: 0; }
.slot.appear .piece { animation: appear .3s ease-out backwards; }
@keyframes appear { from { transform: scale(.3); opacity: 0; } }
.pcell { border-radius: 4px; }

.drag {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  display: grid; gap: var(--gap);
  filter: drop-shadow(0 12px 18px rgba(0,0,0,.45));
}
.drag .pcell { border-radius: 6px; }
.drag.back { transition: transform .18s ease-in, opacity .18s; }

.offline { color: var(--muted); font-size: 13px; }

/* Farben je Teil-Typ */
.c0 { background: #ff8a3d; } .c1 { background: #3db8ff; } .c2 { background: #5fe08a; }
.c3 { background: #b67bff; } .c4 { background: #ff5f8f; } .c5 { background: #ffd23d; }
.c6 { background: #3de0d0; } .c7 { background: #ff6b6b; }

/* Bestenliste */
.side {
  position: fixed; inset: 0 0 0 auto; width: min(360px, 100vw); z-index: 40;
  background: var(--panel); border-left: 1px solid var(--line);
  padding: 16px; transform: translateX(105%); transition: transform .25s ease;
  display: flex; flex-direction: column; overflow: hidden;
}
.side.open { transform: none; box-shadow: -20px 0 50px rgba(0,0,0,.4); }
.side-head { display: flex; align-items: center; justify-content: space-between; }
.side h2 { margin: 0; font-size: 20px; }
.tabs { display: flex; gap: 6px; margin: 12px 0; }
.tab { flex: 1; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: transparent; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--accent); color: #1a1200; border-color: var(--accent); font-weight: 700; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; counter-reset: rank; }
.list li {
  display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: 10px; counter-increment: rank;
}
.list li:nth-child(odd) { background: rgba(255,255,255,.03); }
.list li::before { content: counter(rank); font-weight: 800; color: var(--muted); text-align: center; }
.list li:nth-child(1)::before { content: "🥇"; } .list li:nth-child(2)::before { content: "🥈"; } .list li:nth-child(3)::before { content: "🥉"; }
.list li.me { background: rgba(255,181,71,.18); outline: 1px solid var(--accent); }
.list .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; user-select: text; }
.list .s { font-weight: 700; font-variant-numeric: tabular-nums; }
.list .empty-msg { display: block; color: var(--muted); text-align: center; padding: 20px 0; }
.list .empty-msg::before { content: none; }
.list.compact { max-height: 220px; margin: 4px 0 12px; text-align: left; }
.list.compact li { padding: 6px 10px; }

@media (min-width: 900px) {
  .app { grid-template-columns: 1fr 320px; column-gap: 32px; grid-template-rows: auto 1fr; }
  .top { grid-column: 1 / -1; }
  .side {
    position: static; transform: none; width: auto; border: 1px solid var(--line); border-radius: 16px;
    align-self: start; margin-top: 76px; max-height: calc(100svh - 120px);
  }
  .side .close, #btnBoard { display: none; }
}

/* Spielende */
.modal {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px;
  background: rgba(5,9,20,.7); backdrop-filter: blur(4px);
}
.modal[hidden] { display: none; }
.card {
  width: min(400px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 20px;
  padding: 24px; text-align: center; animation: appear .25s ease-out; max-height: 100%; overflow-y: auto;
}
.over-title { font-size: 22px; font-weight: 800; }
.over-score { font-size: 54px; font-weight: 800; color: var(--accent); line-height: 1.1; margin: 8px 0 2px; }
.over-score small { display: block; font-size: 13px; color: var(--muted); font-weight: 600; }
.over-best { color: var(--good); font-weight: 700; min-height: 20px; margin-bottom: 12px; }
.name-form { text-align: left; margin-bottom: 14px; }
.name-form label { font-size: 13px; color: var(--muted); }
.row { display: flex; gap: 8px; margin-top: 6px; }
input {
  flex: 1; min-width: 0; font: inherit; color: var(--text); background: var(--bg2);
  border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; user-select: text;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.btn {
  padding: 11px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg2);
  font-weight: 700; cursor: pointer; width: 100%;
}
.row .btn { width: auto; }
.btn.primary { background: var(--accent); color: #1a1200; border-color: var(--accent); }
.btn:disabled { opacity: .6; cursor: default; }
.form-msg { min-height: 18px; font-size: 13px; color: var(--bad); margin-top: 6px; }
.rank-box { font-weight: 700; margin-bottom: 4px; }
