:root {
  --bg-wall: #071b15;
  --bg-panel: #102a20;
  --bg-elev: #19382b;
  --border: #395747;
  --border-soft: #294839;
  --text: #e9e7dc;
  --muted: #b4c3a6;
  --accent: #8bb574;
  --accent-btn: #3c6048;
  --gold: #f2c96b;
  --danger: #9e3b33;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-wall);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
}

button, input[type="range"] {
  font: inherit;
  color: var(--text);
}

button {
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
}

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

.screen {
  display: none;
  width: 100%;
  height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  flex-direction: column;
  overflow: hidden;
}

.screen.active {
  display: flex;
}

.hidden {
  display: none !important;
}

/* SPLASH */
#splash-screen {
  justify-content: center;
  align-items: center;
  text-align: center;
  background: radial-gradient(circle at 50% 18%, #184736, var(--bg-wall) 72%);
}

.dino-logo {
  font-size: 60px;
  line-height: 1;
  margin-bottom: 12px;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.5));
}

.eyebrow {
  font-size: 11px;
  letter-spacing: 3px;
  color: var(--muted);
  margin: 0 0 8px;
}

h1 {
  font-size: clamp(36px, 11vw, 58px);
  line-height: 1.05;
  margin: 0 0 22px;
  letter-spacing: 2px;
}

.loader-bar {
  width: 190px;
  height: 7px;
  background: #143327;
  border-radius: 99px;
  overflow: hidden;
  margin: 0 auto;
  border: 1px solid #294839;
}

.loader-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #5b8c5a, #9ec97e);
  transition: width 1.3s ease-out;
}

/* TOP BAR & LIBRARY */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.top-bar h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pill-badge {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 6px 11px;
  font-weight: 700;
  font-size: 12px;
  color: var(--gold);
  white-space: nowrap;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.icon-btn:active,
.primary-btn:active,
.secondary-btn:active,
.chip:active,
.game-header button:active,
.game-controls button:active {
  transform: scale(0.97);
}

.resume-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid #59856b;
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.library-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
}

.lib-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.lib-card {
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  overflow: hidden;
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  flex-direction: column;
}

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

.lib-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(7, 27, 21, 0.86);
  border: 1px solid #456c56;
  border-radius: 99px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 700;
  color: #a6c18d;
  pointer-events: none;
}

.empty-library-note {
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 10px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
  flex-shrink: 0;
}

/* SELECTION SCREEN */
#selection-screen {
  justify-content: space-between;
  gap: 8px;
}

.sel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  gap: 6px;
}

.sel-card {
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
  padding: 8px 10px 10px;
  text-align: center;
  flex-shrink: 0;
}

.preview-wrap {
  position: relative;
  width: min(64vw, 31dvh);
  aspect-ratio: 1 / 1;
  margin: 0 auto 8px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #091d16;
}

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

#preview-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.reward-line {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--gold);
}

.opt-label {
  font-size: 11px;
  letter-spacing: 1.8px;
  color: var(--muted);
  text-align: center;
  margin: 2px 0 5px;
  font-weight: 700;
}

.h-scroll {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 2px 2px;
  scrollbar-width: none;
  touch-action: pan-x;
}

.h-scroll::-webkit-scrollbar {
  display: none;
}

.chip-row {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.chip {
  flex: 1 0 auto;
  min-width: 66px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-panel);
  font-weight: 700;
  font-size: 14px;
}

.chip.selected {
  background: #496d4c;
  border-color: #a6c18d;
  color: #ffffff;
}

.primary-btn,
.secondary-btn,
.danger-btn {
  border-radius: 14px;
  padding: 13px 16px;
  font-weight: 700;
  font-size: 14px;
}

.primary-btn {
  border: 1px solid #59856b;
  background: var(--accent-btn);
}

.secondary-btn {
  border: 1px solid var(--border);
  background: var(--bg-elev);
}

.danger-btn {
  border: 1px solid #c4685e;
  background: var(--danger);
}

.start-btn {
  width: 100%;
  flex-shrink: 0;
}

/* GAME SCREEN */
#game-screen {
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  touch-action: none;
}

.game-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding-bottom: 7px;
  flex-shrink: 0;
}

.top-btns {
  flex: 1;
  display: flex;
  gap: 5px;
  min-width: 0;
}

.top-btns button {
  flex: 1;
  min-width: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 4px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#piece-count {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
  min-width: 54px;
  text-align: right;
}

#puzzle-layout {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#board-container {
  flex: 1;
  width: 100%;
  min-height: 0;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  border-radius: 12px;
  border: 1px solid rgba(57, 87, 71, 0.45);
  background: radial-gradient(circle at 50% 35%, rgba(34, 72, 54, 0.55), rgba(5, 18, 13, 0.78));
  perspective: 950px;
}

#board {
  width: min(86vw, 43dvh);
  aspect-ratio: 1 / 1;
  position: relative;
  background: #ffffff;
  border: 2px solid var(--border);
  border-radius: 6px;
  transform-origin: 0 0;
  transform-style: preserve-3d;
  overflow: visible;
  flex-shrink: 0;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.48), inset 0 0 0 1px rgba(255, 255, 255, 0.14);
  will-change: transform;
}

/* 3D Puzzle Depth Mode */
body.mode-3d #board {
  box-shadow:
    0 22px 38px rgba(0, 0, 0, 0.62),
    0 6px 0 rgba(16, 36, 27, 0.85),
    inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

body.mode-3d #board .piece:not(.locked) {
  filter: drop-shadow(0 5px 6px rgba(0, 0, 0, 0.42));
}

body.mode-3d #board .piece.grouped:not(.locked) {
  filter: drop-shadow(0 7px 9px rgba(0, 0, 0, 0.5));
}

body.mode-3d #board .piece.dragging {
  filter: drop-shadow(0 18px 22px rgba(0, 0, 0, 0.74)) !important;
}

/* Pan Mode (prevents accidentally dragging pieces while moving a zoomed board) */
body.pan-mode #board .piece:not(.locked) {
  pointer-events: none;
}

body.pan-mode #board-container {
  cursor: grab;
  border-color: var(--gold);
}

#pan-mode-btn.active {
  background: #496d4c;
  border-color: var(--gold);
  color: #ffffff;
}

#board .piece {
  position: absolute;
  overflow: visible;
  touch-action: none;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.28));
  will-change: left, top, transform;
}

.dragging {
  filter: drop-shadow(0 12px 18px rgba(0, 0, 0, 0.68)) !important;
}

#board .piece.locked {
  pointer-events: none;
  filter: none;
}

#board .piece.locked .bevel {
  opacity: 0.55;
}

#board .piece.locked .edge-line {
  opacity: 0.45;
}

#tray-section {
  flex: none;
  height: 86px;
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  align-items: center;
  overflow: hidden;
}

#tray-grid {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x;
  scrollbar-width: none;
}

#tray-grid::-webkit-scrollbar {
  display: none;
}

.tray-item {
  flex: 0 0 auto;
  min-width: 66px;
  height: 66px;
  padding: 0 4px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  overflow: visible;
  filter: drop-shadow(0 4px 5px rgba(0, 0, 0, 0.48));
  touch-action: pan-x;
  position: relative;
}

.tray-item svg {
  display: block;
  overflow: visible;
  pointer-events: none;
}

.tray-float-drag {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  overflow: visible;
  filter: drop-shadow(0 14px 22px rgba(0, 0, 0, 0.75));
  transform-origin: center center;
}

.game-controls {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: stretch;
  gap: 5px;
  padding-top: 7px;
  flex-shrink: 0;
  width: 100%;
}

.game-controls button {
  flex: 1 1 0;
  min-width: 0;
  background-color: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 3px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* MODAL */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: rgba(0, 0, 0, 0.84);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

#modal-content {
  width: 100%;
  max-width: 410px;
  max-height: 86dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  padding: 20px;
  border-radius: 18px;
  text-align: center;
}

#modal-content h3 {
  margin: 0 0 8px;
  font-size: 18px;
}

#modal-content img {
  max-width: 100%;
  max-height: 48dvh;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  margin: 10px auto;
}

.swatches {
  display: flex;
  gap: 8px;
  margin: 8px 0 12px;
}

.swatch {
  flex: 1;
  height: 36px;
  border-radius: 10px;
  border: 2px solid var(--border);
}

.swatch.active {
  border-color: #ffffff;
}

.t-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: 12px;
  margin-bottom: 8px;
  font-weight: 700;
  font-size: 13px;
}

.slider-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: 12px;
  margin-bottom: 8px;
  text-align: left;
  font-size: 13px;
  font-weight: 700;
}

.slider-head {
  display: flex;
  justify-content: space-between;
  color: var(--text);
}

.slider-head span:last-child {
  color: var(--gold);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  height: 22px;
}

.help-ul {
  text-align: left;
  color: #c4c8bd;
  font-size: 13px;
  line-height: 1.5;
  padding-left: 18px;
  margin: 10px 0 16px;
}

.help-ul li {
  margin-bottom: 7px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.modal-actions button {
  flex: 1;
}