:root {
  --bg: #f5ebe0;
  --bg-dark: #e8d5c4;
  --surface: #fff8f0;
  --primary: #c97b63;
  --primary-dark: #a85d47;
  --accent: #7d9d6a;
  --text: #3d2c29;
  --text-muted: #6b5b54;
  --border: #d4b896;
  --shadow: rgba(61, 44, 41, 0.12);
  --cell-size: 48px;
  --radius: 12px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  font-family: var(--font);
  background: linear-gradient(160deg, var(--bg) 0%, #e6d0b8 100%);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
}

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

.screen {
  display: none;
  width: 100%;
  height: 100%;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* Titelscreen */
#screen-title {
  align-items: center;
  justify-content: center;
}

.title-content {
  text-align: center;
  padding: 2rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px var(--shadow);
  max-width: 480px;
}

.title-content h1 {
  font-size: 2.2rem;
  margin-bottom: 0.5rem;
}

.subtitle {
  color: var(--text-muted);
  margin-bottom: 2rem;
  line-height: 1.5;
}

.title-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Buttons */
.btn {
  font-family: inherit;
  font-size: 1rem;
  padding: 0.75rem 1.5rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  background: var(--bg);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary-dark);
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

.btn-secondary {
  background: var(--surface);
}

.btn-small {
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
}

.btn-back {
  padding: 0.4rem 0.8rem;
  font-size: 0.9rem;
}

/* Screen header */
.screen-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  flex-shrink: 0;
}

.screen-header h2 {
  flex: 1;
  font-size: 1.25rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.coins {
  font-weight: 600;
  font-size: 1.1rem;
  white-space: nowrap;
}

.btn-task {
  display: none;
}

.task-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.task-panel-header h3 {
  margin-bottom: 0;
}

.btn-panel-close {
  display: none;
  padding: 0.15rem 0.55rem;
  font-size: 1.35rem;
  line-height: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
}

.task-backdrop {
  display: none;
}

/* Build layout */
.build-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.palette {
  width: 160px;
  background: var(--surface);
  border-right: 2px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
  flex-shrink: 0;
}

.palette h3,
.task-panel h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.palette-group-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 0.75rem 0 0.4rem;
}

.palette-group-title:first-child {
  margin-top: 0;
}

.palette-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: 0.75rem;
}

.palette-item-plant {
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
}

.plant-emoji {
  font-size: 1.5rem;
  line-height: 1;
}

.plant-info {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  font-size: 0.75rem;
}

.plant-price {
  color: var(--text-muted);
  font-size: 0.7rem;
}

.garden-panel {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 2px solid var(--border);
}

.garden-panel.hidden {
  display: none;
}

.garden-panel h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.garden-status-list {
  list-style: none;
}

.garden-status-list li {
  font-size: 0.8rem;
  padding: 0.35rem 0;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  line-height: 1.3;
}

.garden-status-list li.ready {
  color: #3d6a3d;
  font-weight: 600;
}

.garden-status-list li.empty {
  color: var(--text-muted);
  font-style: italic;
}

.shop-panel {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 2px solid var(--border);
}

.shop-panel.hidden {
  display: none;
}

.shop-panel h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.shop-stock-list {
  list-style: none;
  margin-bottom: 0.75rem;
}

.shop-stock-list li {
  font-size: 0.85rem;
  padding: 0.35rem 0;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.shop-stock-list li.empty {
  color: var(--text-muted);
  font-style: italic;
}

.shop-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.shop-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.shop-actions .btn {
  width: 100%;
  font-size: 0.85rem;
  padding: 0.5rem 0.75rem;
}

.finale-card {
  text-align: center;
  max-width: 520px;
}

.finale-card h2 {
  font-size: 1.6rem;
  margin-bottom: 1rem;
  color: var(--primary);
}

.finale-card p {
  line-height: 1.6;
  margin-bottom: 0.75rem;
}

.finale-sub {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.finale-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

#btn-shop:not(.hidden) {
  background: var(--accent);
  color: white;
  border-color: #5a7a4a;
}

.palette-items {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.palette-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: grab;
  background: white;
  user-select: none;
  touch-action: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.palette-item:hover {
  border-color: var(--primary);
  box-shadow: 0 2px 8px var(--shadow);
}

.palette-item.dragging {
  opacity: 0.5;
  cursor: grabbing;
}

.palette-item canvas {
  width: 40px;
  height: 40px;
  image-rendering: pixelated;
}

.palette-item span {
  font-size: 0.8rem;
}

.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  overflow: auto;
  position: relative;
  min-width: 0;
  min-height: 0;
}

.floor-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.floor-tab {
  padding: 0.4rem 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font-size: 0.85rem;
}

.floor-tab.active {
  background: var(--accent);
  color: white;
  border-color: #5a7a4a;
}

.floor-tab.locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.grid-container {
  position: relative;
  border: 3px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px var(--shadow);
  background: #a8c686;
  max-width: 100%;
}

#game-canvas {
  display: block;
  cursor: crosshair;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

.expansion-lots {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.expansion-lot {
  position: absolute;
  pointer-events: all;
  padding: 0.5rem;
  background: rgba(255, 248, 240, 0.9);
  border: 2px dashed var(--primary);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.75rem;
  text-align: center;
  transition: background 0.15s;
}

.expansion-lot:hover {
  background: var(--surface);
}

/* Task panel */
.task-panel {
  width: 220px;
  background: var(--surface);
  border-left: 2px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
  flex-shrink: 0;
}

/* Mobile / narrow: canvas first, palette strip, task as drawer */
@media (max-width: 800px) {
  .screen-header {
    gap: 0.5rem;
    padding: 0.55rem 0.75rem;
  }

  .screen-header h2 {
    font-size: 1rem;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-actions {
    gap: 0.4rem;
  }

  .btn-small,
  .btn-back {
    padding: 0.35rem 0.55rem;
    font-size: 0.8rem;
  }

  .coins {
    font-size: 0.95rem;
  }

  .btn-task {
    display: inline-flex;
  }

  .btn-panel-close {
    display: inline-flex;
  }

  .build-layout {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas:
      "canvas"
      "palette";
  }

  .canvas-area {
    grid-area: canvas;
    padding: 0.5rem;
    justify-content: flex-start;
  }

  .floor-tabs {
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
  }

  .palette {
    grid-area: palette;
    width: 100%;
    max-height: none;
    border-right: none;
    border-top: 2px solid var(--border);
    padding: 0.65rem 0.75rem;
    overflow: visible;
  }

  .palette h3 {
    margin-bottom: 0.5rem;
  }

  .palette-items {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 0.5rem;
    padding-bottom: 0.25rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .palette-group-title,
  .palette-hint {
    flex: 0 0 auto;
    writing-mode: horizontal-tb;
    align-self: center;
    max-width: 7rem;
    margin: 0;
    white-space: normal;
  }

  .palette-item {
    flex: 0 0 auto;
    min-width: 7.5rem;
    max-width: 9rem;
  }

  .palette-item-plant {
    min-width: 8.5rem;
  }

  .task-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(20rem, 88vw);
    z-index: 80;
    border-left: 2px solid var(--border);
    box-shadow: -8px 0 28px var(--shadow);
    transform: translateX(105%);
    transition: transform 0.22s ease;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }

  .task-panel.open {
    transform: translateX(0);
  }

  .task-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(61, 44, 41, 0.35);
    z-index: 70;
    border: none;
    padding: 0;
    cursor: pointer;
  }

  .task-backdrop[hidden] {
    display: none;
  }

  .title-content {
    margin: 1rem;
    max-width: calc(100vw - 2rem);
    padding: 1.5rem 1.25rem;
  }

  .title-content h1 {
    font-size: 1.7rem;
  }

  .overlay-content {
    margin: 1rem;
    padding: 1.5rem;
    max-height: min(85dvh, 85vh);
    overflow-y: auto;
  }

  .dialog-bubble {
    bottom: max(1rem, env(safe-area-inset-bottom));
    width: calc(100% - 2rem);
    max-width: none;
  }

  .chapter-list {
    padding: 1rem;
  }
}

@media (max-width: 480px) {
  .screen-header h2 {
    flex: 1 1 100%;
    order: -1;
  }

  .header-actions {
    margin-left: auto;
  }
}

#task-text {
  font-size: 0.9rem;
  line-height: 1.5;
  margin-bottom: 1rem;
  color: var(--text-muted);
}

.task-checklist {
  list-style: none;
}

.task-checklist li {
  padding: 0.4rem 0;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.task-checklist li.done::before {
  content: "✅";
}

.task-checklist li:not(.done)::before {
  content: "⬜";
}

/* Chapter list */
.chapter-list {
  padding: 1.5rem;
  overflow-y: auto;
  flex: 1;
}

.chapter-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 0.75rem;
}

.chapter-card.locked {
  opacity: 0.5;
}

.chapter-card h4 {
  margin-bottom: 0.25rem;
}

.chapter-card p {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.chapter-card button {
  margin-top: 0.5rem;
}

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(61, 44, 41, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.overlay.hidden {
  display: none;
}

.overlay-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 2rem;
  max-width: 480px;
  width: 90%;
  position: relative;
  box-shadow: 0 12px 48px var(--shadow);
}

.overlay-close {
  position: absolute;
  top: 0.75rem;
  right: 1rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-muted);
}

.letter-card h3 {
  margin-bottom: 1rem;
  color: var(--primary);
}

.letter-card p {
  line-height: 1.7;
  margin-bottom: 1.5rem;
}

/* Dialog bubble */
.dialog-bubble {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  box-shadow: 0 4px 16px var(--shadow);
  z-index: 50;
  max-width: 400px;
  animation: bubble-in 0.3s ease;
}

.dialog-bubble.hidden {
  display: none;
}

.dialog-emoji {
  font-size: 2rem;
}

@keyframes bubble-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* Confetti */
.confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 200;
}
