:root {
  --paper: #ece5d3;
  --ink: #1f2421;
  --accent: #d92d2d;
  --board-bg: #2a2f28;
  --grid-line: rgba(255, 255, 255, 0.15);
  --tray-w: 260px;
  --tray-h-mobile: 140px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Space Grotesk", system-ui, sans-serif;
  color: var(--ink);
  background: var(--board-bg);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Screens ---------- */

.screen {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.screen[hidden] {
  display: none;
}

/* ---------- Header ---------- */

.back-btn {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 6px 10px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--ink);
  cursor: pointer;
}

.back-btn:hover {
  background: var(--ink);
  color: var(--paper);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  background: var(--paper);
  border-bottom: 2px solid var(--ink);
}

.title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.progress {
  margin-left: auto;
  font-family: "DM Mono", monospace;
  font-size: 15px;
  font-weight: 500;
}

.progress.is-done {
  color: var(--accent);
  font-weight: 700;
}

.view-controls {
  display: flex;
  border: 1px solid var(--ink);
}

.view-btn {
  font: inherit;
  font-size: 14px;
  padding: 6px 10px;
  background: var(--paper);
  color: var(--ink);
  border: 0;
  cursor: pointer;
}

.view-btn + .view-btn {
  border-left: 1px solid var(--ink);
}

.reset-btn {
  font: inherit;
  font-size: 14px;
  padding: 6px 10px;
  background: var(--paper);
  color: var(--accent);
  border: 1px solid var(--accent);
  cursor: pointer;
}

/* ---------- Layout ---------- */

.app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ---------- Startseite ---------- */

.start-screen {
  background: var(--paper);
}

.start-main {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  padding: 16px 20px 32px;
}

.start-subtitle {
  margin: 4px 0 6px;
  font-size: 15px;
}

.overall-progress {
  margin: 0 0 18px;
  font-family: "DM Mono", monospace;
  font-size: 13px;
  font-weight: 500;
}

.level-list {
  display: flex;
  flex-direction: column;
  border-top: 2px solid var(--ink);
}

.level-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px 4px;
  font: inherit;
  font-size: 16px;
  text-align: left;
  background: var(--paper);
  color: var(--ink);
  border: 0;
  border-bottom: 1px solid var(--ink);
  cursor: pointer;
}

.level-row:hover {
  background: #e1d8c0;
}

.level-row:active {
  background: #d6cbb0;
}

.level-left {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.level-name {
  font-weight: 600;
}

.level-size {
  font-size: 13px;
  color: rgba(31, 36, 33, 0.7);
}

.level-progress {
  margin-left: auto;
  font-family: "DM Mono", monospace;
  font-size: 14px;
  font-weight: 500;
}

.level-chevron {
  font-family: "DM Mono", monospace;
  font-size: 22px;
  line-height: 1;
  color: var(--accent);
}

.level-row.is-unavailable {
  opacity: 0.45;
  cursor: default;
}

.level-row.is-unavailable:hover {
  background: var(--paper);
}

/* ---------- Spielfeld ---------- */

.board-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
  background: var(--board-bg);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.board-wrap.is-panning {
  cursor: grabbing;
}

.board {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
}

.board.is-gesture {
  will-change: transform;
}

.grid-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
}

.board-inner {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  overflow: visible;
}

.board-inner .piece {
  position: absolute;
  border: 1px solid var(--grid-line);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

/* ---------- Tray (mobile: unten, horizontal) ---------- */

.tray {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  height: var(--tray-h-mobile);
  padding: 10px 14px;
  background: var(--paper);
  border-top: 2px solid var(--ink);
  -webkit-overflow-scrolling: touch;
}

.tray .piece {
  flex: 0 0 auto;
  height: 116px;
  width: auto;
  max-width: none;
  background: var(--paper);
  border: 1px solid rgba(0, 0, 0, 0.25);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

.piece.is-dragging {
  opacity: 0.35;
}

/* ---------- Drag-Klon ---------- */

.drag-clone {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  border: 2px solid var(--ink);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  background: var(--paper);
  cursor: grabbing;
}

.drag-clone img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ---------- Lightbox (Großansicht) ---------- */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(31, 36, 33, 0.85);
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.lightbox-img {
  display: block;
  max-width: 85vw;
  max-height: 85vh;
  object-fit: contain;
  border: 2px solid var(--paper);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  background: var(--paper);
  cursor: default;
}

.lightbox-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-size: 26px;
  line-height: 1;
  background: var(--paper);
  color: var(--ink);
  border: 2px solid var(--ink);
  cursor: pointer;
}

.lightbox-close:hover {
  background: var(--ink);
  color: var(--paper);
}

/* ---------- Zell-Feedback (unskaliert, über dem Board) ---------- */

.cell-feedback {
  position: absolute;
  pointer-events: none;
}

@keyframes cellFlash {
  0% {
    box-shadow: 0 0 0 3px var(--accent), 0 0 0 8px rgba(217, 45, 45, 0.4);
  }
  100% {
    box-shadow: 0 0 0 3px rgba(217, 45, 45, 0), 0 0 0 8px rgba(217, 45, 45, 0);
  }
}

.cell-feedback.flash {
  animation: cellFlash 0.4s ease-out;
}

@keyframes snapIn {
  from {
    transform: scale(1.12);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.board-piece.snap-in {
  animation: snapIn 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* ---------- Attribution ---------- */

.attribution {
  padding: 6px 14px;
  background: var(--paper);
  border-top: 2px solid var(--ink);
  font-size: 12px;
}

.attribution p {
  margin: 0;
}

.attribution a {
  color: var(--ink);
}

/* ---------- Desktop: Tray rechts, Board füllt Rest ---------- */

@media (min-width: 901px) {
  .app {
    flex-direction: row;
  }

  .tray {
    flex: 0 0 var(--tray-w);
    height: auto;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    border-top: 0;
    border-left: 2px solid var(--ink);
    padding: 12px;
    gap: 10px;
  }

  .tray .piece {
    height: auto;
    width: 160px;
    max-width: 100%;
    margin: 0 auto;
  }
}
