:root {
  color-scheme: light;
  --bg: #ffffff;
  --panel: #ffffff;
  --panel2: #f8f8f8;
  --stroke: #c8c8c8;
  --stroke-soft: #e4e4e4;
  --muted: #767676;
  --text: #040404;
  --accent: #000000;
  --hover-bg: #f2f5ff;
  --hover-border: #8da8ff;
  --radius: 5px;
}

.dark-theme {
  color-scheme: dark;
  --bg: #111318;
  --panel: #161920;
  --panel2: #1f232d;
  --stroke: #343946;
  --stroke-soft: #282c37;
  --muted: #98a0b3;
  --text: #f3f4f6;
  --accent: #ffffff;
  --hover-bg: #252b3b;
  --hover-border: #6888ff;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

button, input {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible, input:focus-visible {
  outline: 2px solid #4d7cfe;
  outline-offset: 2px;
}

a {
  color: inherit;
  text-decoration: none;
}

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 305px;
  background: var(--bg);
}

/* Hide legacy studio rail in reference/Infinite Craft mode */
.rail {
  display: none;
}

.board-shell {
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  background: var(--bg);
}

/* Minimal top bar inspired by neal.fun/infinite-craft */
.topbar {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
  z-index: 10;
  background: transparent;
  pointer-events: none;
}

.topbar > * {
  pointer-events: auto;
}

.site-brand-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}

.neal-logo {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  font-size: 19px;
  letter-spacing: -0.4px;
  color: var(--text);
  padding: 4px 9px;
  border-radius: 6px;
  transition: transform 0.15s ease, background 0.15s ease;
}

.neal-logo:hover {
  background: var(--panel2);
  transform: translateY(-1px);
}

.neal-star {
  color: #f59e0b;
  font-size: 17px;
}

.site-game-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  border-left: 1px solid var(--stroke-soft);
  padding-left: 12px;
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.site-game-title small {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}

.header-buttons {
  display: flex;
  align-items: center;
  gap: 5px;
}

.tool-btn, .share-btn {
  height: 31px;
  border: 1px solid var(--stroke);
  border-radius: 5px;
  background: var(--panel);
  color: var(--text);
  padding: 0 9px;
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
  transition: border-color 0.12s, background 0.12s, transform 0.1s;
}

.tool-btn {
  min-width: 31px;
}

.tool-btn:hover, .share-btn:hover {
  border-color: var(--text);
  background: var(--panel2);
}

.tool-btn:active, .share-btn:active {
  transform: scale(0.97);
}

.zoom-level {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  min-width: 38px;
  text-align: center;
  color: var(--muted);
}

/* Infinite Craft Canvas */
.canvas {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  background: var(--bg);
}

.canvas-grid {
  position: absolute;
  inset: 0;
  background-image: radial-gradient(rgba(128, 128, 128, 0.16) 1px, transparent 1px);
  background-size: 24px 24px;
  pointer-events: none;
}

.guide {
  position: absolute;
  top: 47%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  width: min(370px, 86%);
  pointer-events: none;
  opacity: 0.65;
}

.guide-icon {
  font-size: 32px;
  margin-bottom: 10px;
}

.guide h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px;
  color: var(--text);
}

.guide p {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.55;
  margin: 0;
}

/* Bottom-right canvas controls like Infinite Craft */
.canvas-floating-tools {
  position: absolute;
  right: 16px;
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 25;
}

.canvas-stat-pill {
  background: var(--panel);
  border: 1px solid var(--stroke-soft);
  border-radius: 5px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.canvas-stat-pill strong {
  color: var(--text);
  font-weight: 700;
}

.floating-broom {
  width: 34px;
  height: 34px;
  border-radius: 5px;
  border: 1px solid var(--stroke-soft);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
  display: grid;
  place-items: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  transition: border-color 0.15s, transform 0.1s;
}

.floating-broom:hover {
  border-color: var(--text);
  transform: scale(1.04);
}

.board-bottom {
  height: 28px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  border-top: 1px solid var(--stroke-soft);
  font-size: 11px;
  color: var(--muted);
  background: var(--bg);
}

/* Canvas Element Pill — exact neal.fun/infinite-craft look */
.piece {
  position: absolute;
  background: var(--panel);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px 7px;
  min-height: 34px;
  white-space: nowrap;
  border-radius: 5px;
  border: 1px solid var(--stroke);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.06);
  font-weight: 500;
  font-size: 15px;
  line-height: 1.15;
  cursor: grab;
  user-select: none;
  touch-action: none;
  z-index: 5;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.piece:hover {
  border-color: #969696;
  background: var(--hover-bg);
}

.piece:active {
  cursor: grabbing;
}

.piece .emoji {
  font-size: 18px;
  line-height: 1;
}

.piece.selected-piece {
  border-color: #4d7cfe;
  box-shadow: 0 0 0 2px rgba(77, 124, 254, 0.25);
}

.piece.dragging {
  z-index: 200;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.16);
  border-color: var(--text);
}

.piece.combine-target {
  border-color: #3b82f6;
  background: var(--hover-bg);
  transform: scale(1.07) !important;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.22);
}

.piece.new {
  animation: popIn 0.26s cubic-bezier(0.2, 1.5, 0.5, 1);
}

.first-discovery-badge {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  font-weight: 700;
  color: #b45309;
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: 4px;
  padding: 1px 5px;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.dark-theme .first-discovery-badge {
  background: #3b2a0a;
  color: #fbbf24;
  border-color: #d97706;
}

@keyframes popIn {
  0% { transform: scale(0.5); opacity: 0.3; }
  100% { transform: scale(1); opacity: 1; }
}

/* Right Sidebar — exact neal.fun/infinite-craft inline pill layout */
.inventory {
  border-left: 1px solid var(--stroke);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 15;
}

.inventory-head {
  display: none;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--stroke-soft);
}

.inventory-head h2 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
}

.inventory-head .eyebrow {
  display: none;
}

.inventory-head button {
  background: none;
  border: 0;
  font-size: 22px;
  color: var(--text);
}

/* Inline-flex wrap of element chips just like Infinite Craft! */
.items {
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 7px;
  padding: 11px 10px 16px;
  flex: 1;
}

.items::-webkit-scrollbar {
  width: 6px;
}

.items::-webkit-scrollbar-thumb {
  background: var(--stroke);
  border-radius: 6px;
}

.item {
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 9px 6px;
  min-height: 32px;
  border-radius: 5px;
  background: var(--panel);
  border: 1px solid var(--stroke);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  color: var(--text);
  font-size: 14.5px;
  font-weight: 500;
  line-height: 1.15;
  transition: background 0.1s, border-color 0.1s, transform 0.08s;
  user-select: none;
  touch-action: pan-y;
}

.item:hover, .item.selected {
  background: var(--hover-bg);
  border-color: var(--hover-border);
}

.item:active {
  transform: scale(0.96);
}

.item .emoji {
  font-size: 16.5px;
  line-height: 1;
}

.item .name {
  white-space: nowrap;
}

/* Hide the non-Infinite-Craft plus dot */
.item .item-dot {
  display: none;
}

/* Bottom sticky sidebar controls (Sort tabs + Search input) */
.sidebar-controls {
  border-top: 1px solid var(--stroke);
  background: var(--panel);
}

.sort-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--stroke);
}

.sort-tab {
  border: 0;
  border-right: 1px solid var(--stroke-soft);
  background: var(--panel);
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 500;
  padding: 8px 4px;
  text-align: center;
  transition: background 0.12s, color 0.12s;
}

.sort-tab:last-of-type {
  border-right: 0;
}

.sort-tab:hover {
  background: var(--panel2);
  color: var(--text);
}

.sort-tab.active {
  background: var(--panel2);
  color: var(--text);
  font-weight: 700;
}

.search-wrap {
  height: 40px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  padding: 0 11px;
  border-bottom: 1px solid var(--stroke-soft);
}

.search-icon {
  font-size: 17px;
  color: var(--muted);
}

.search-wrap input {
  border: none;
  background: none;
  min-width: 0;
  color: var(--text);
  outline: 0;
  flex: 1;
  font-size: 13.5px;
}

.search-wrap input::placeholder {
  color: var(--muted);
}

.search-wrap kbd {
  font-size: 10px;
  background: var(--panel2);
  border: 1px solid var(--stroke-soft);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--muted);
}

.inventory-bottom {
  padding: 6px 10px 8px;
}

.recipe-hint {
  display: none;
}

.save-drawer summary {
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  padding: 3px 2px;
  user-select: none;
}

.save-drawer summary:hover {
  color: var(--text);
}

.backup-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  margin: 6px 0;
}

.backup-actions button {
  border: 1px solid var(--stroke-soft);
  padding: 6px 5px;
  border-radius: 4px;
  background: var(--panel2);
  color: var(--text);
  font-size: 11px;
  font-weight: 500;
}

.backup-actions button:hover {
  border-color: var(--text);
}

.atlas-status {
  font-size: 10.5px;
  color: var(--muted);
  margin: 2px 0 4px;
}

.reset {
  border: 0;
  background: none;
  color: #dc2626;
  font-size: 11px;
  text-align: left;
  padding: 4px 2px;
}

.reset:hover {
  text-decoration: underline;
}

/* Subtle clean toast notifications */
.toast-region {
  position: absolute;
  bottom: 16px;
  left: 16px;
  display: grid;
  gap: 6px;
  z-index: 300;
  pointer-events: none;
}

.toast {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--stroke);
  padding: 7px 12px;
  border-radius: 5px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  animation: popIn 0.18s ease;
}

.mobile-footer {
  display: none;
}

/* Modal Dialog */
dialog {
  padding: 0;
  border: 1px solid var(--stroke);
  border-radius: 10px;
  max-width: 410px;
  width: calc(100% - 32px);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
}

.dialog-inner {
  padding: 26px;
  position: relative;
}

.dialog-close {
  position: absolute;
  right: 14px;
  top: 10px;
  font-size: 24px;
  color: var(--muted);
  background: none;
  border: 0;
}

.eyebrow {
  font-size: 10px;
  letter-spacing: 1.5px;
  font-weight: 700;
  color: var(--muted);
}

.dialog-inner h2 {
  font-size: 22px;
  font-weight: 700;
  margin: 10px 0 12px;
}

.dialog-inner p, .dialog-inner li {
  font-size: 14px;
  line-height: 1.65;
  color: var(--muted);
}

.dialog-inner ol {
  padding-left: 20px;
}

.primary {
  border: 1px solid var(--text);
  background: var(--text);
  color: var(--bg);
  width: 100%;
  padding: 11px;
  border-radius: 6px;
  font-weight: 600;
  margin-top: 10px;
}

.touch-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  white-space: nowrap;
  padding: 6px 10px;
  border: 1px solid var(--text);
  border-radius: 5px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.18);
  font-size: 14px;
  transform: translate(-10%, -10%);
}

/* Responsive Mobile Drawer */
@media (max-width: 850px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    height: calc(100dvh - 56px);
    position: relative;
    overflow: hidden;
  }

  .inventory {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(330px, 88vw);
    box-shadow: -12px 0 36px rgba(0, 0, 0, 0.15);
    transform: translateX(105%);
    transition: transform 0.22s ease;
    z-index: 40;
  }

  .inventory.open {
    transform: translateX(0);
  }

  .inventory-head {
    display: flex;
  }

  .site-game-title {
    display: none;
  }

  .topbar {
    height: 44px;
    padding: 0 10px;
  }

  .board-bottom {
    display: none;
  }

  .mobile-footer {
    position: relative;
    display: flex;
    z-index: 30;
    height: 56px;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid var(--stroke);
    background: var(--panel);
    padding: 0 12px;
    gap: 8px;
  }

  .mobile-home {
    font-family: Georgia, serif;
    font-weight: 700;
    font-size: 15px;
    color: var(--muted);
  }

  .mobile-footer button {
    border: 1px solid var(--stroke);
    background: var(--panel);
    color: var(--text);
    padding: 7px 11px;
    border-radius: 5px;
    font-size: 13px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  .mobile-footer button b {
    background: var(--panel2);
    border: 1px solid var(--stroke-soft);
    border-radius: 4px;
    padding: 1px 6px;
    font-size: 12px;
  }
}

@media (max-width: 520px) {
  #challenge-top span, .share-btn span, .zoom-level {
    display: none;
  }
  .tool-btn, .share-btn {
    padding: 0 7px;
    min-width: 28px;
    height: 29px;
  }
  .canvas-stat-pill span:last-child {
    display: none;
  }
}
