@import url('https://fonts.googleapis.com/css2?family=Silkscreen:wght@400;700&display=swap');

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

body {
  font-family: 'Silkscreen', monospace;
  background: #3b2d1e;
  overflow-x: hidden;
  min-height: 100vh;
  color: #e8d5b0;
  user-select: none;
  -webkit-user-select: none;
}

#canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

#ui-overlay {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

#header {
  text-align: center;
  padding: 16px 12px 8px;
  background: linear-gradient(180deg, rgba(30,20,10,0.95) 0%, rgba(30,20,10,0.7) 100%);
  border-bottom: 4px solid #5a3d1e;
}

#title {
  font-size: clamp(18px, 4vw, 36px);
  font-weight: 700;
  color: #f5c542;
  text-shadow: 3px 3px 0 #3b2000, -1px -1px 0 #3b2000;
  letter-spacing: 2px;
}

#subtitle {
  font-size: clamp(10px, 2vw, 14px);
  color: #a89070;
  margin-top: 2px;
}

#tier-counter {
  font-size: clamp(11px, 2vw, 15px);
  color: #7ecf6a;
  margin-top: 4px;
}

#tier-num {
  color: #f5c542;
  font-weight: 700;
}

#main-layout {
  display: flex;
  flex: 1;
  gap: 8px;
  padding: 8px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* PANELS */
#inventory-panel, #crafting-panel, #tower-panel {
  background: rgba(40, 28, 15, 0.92);
  border: 3px solid #6b4c2a;
  border-radius: 4px;
  padding: 10px;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.5), 0 4px 16px rgba(0,0,0,0.3);
}

#inventory-panel {
  flex: 0 0 220px;
  display: flex;
  flex-direction: column;
}

#crafting-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#tower-panel {
  flex: 0 0 200px;
  display: flex;
  flex-direction: column;
}

#inv-title, #craft-title, #tower-title {
  font-size: 13px;
  font-weight: 700;
  color: #c4a66a;
  text-align: center;
  margin-bottom: 8px;
  text-shadow: 1px 1px 0 #000;
  letter-spacing: 2px;
  border-bottom: 2px solid #5a3d1e;
  padding-bottom: 6px;
}

/* INVENTORY SLOTS */
#inv-slots {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin-bottom: 10px;
  flex: 1;
  overflow-y: auto;
  align-content: start;
  max-height: 400px;
}

.inv-slot {
  aspect-ratio: 1;
  background: #1a1208;
  border: 2px solid #4a3520;
  border-radius: 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: border-color 0.15s, transform 0.1s;
  min-height: 56px;
}

.inv-slot:hover {
  border-color: #f5c542;
  transform: scale(1.04);
}

.inv-slot.selected {
  border-color: #7ecf6a;
  box-shadow: 0 0 8px rgba(126, 207, 106, 0.5);
}

.inv-slot img {
  width: 36px;
  height: 36px;
  image-rendering: pixelated;
}

.inv-slot .count {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 10px;
  color: #fff;
  text-shadow: 1px 1px 0 #000;
  font-weight: 700;
}

.inv-slot .item-name {
  font-size: 7px;
  color: #a89070;
  text-align: center;
  line-height: 1.1;
  margin-top: 1px;
  max-width: 100%;
  overflow: hidden;
}

/* BUTTONS */
button {
  font-family: 'Silkscreen', monospace;
  cursor: pointer;
  border: none;
  padding: 8px 14px;
  font-size: 12px;
  border-radius: 3px;
  transition: all 0.15s;
}

#gather-btn {
  background: linear-gradient(180deg, #5a8f3a 0%, #3d6b22 100%);
  color: #e8f5d0;
  border: 2px solid #2d5015;
  margin-bottom: 6px;
  text-shadow: 1px 1px 0 #1a3008;
  width: 100%;
}

#gather-btn:hover {
  background: linear-gradient(180deg, #6ba848 0%, #4a7d2a 100%);
  transform: scale(1.03);
}

#gather-btn:active {
  transform: scale(0.96);
}

#quick-fill-btn {
  background: linear-gradient(180deg, #8a6a3a 0%, #6b4c2a 100%);
  color: #e8d5b0;
  border: 2px solid #4a3520;
  width: 100%;
  font-size: 10px;
}

#quick-fill-btn:hover {
  background: linear-gradient(180deg, #9a7a4a 0%, #7b5c3a 100%);
}

/* CRAFTING */
#crafting-area {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 10px 0;
}

#crafting-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  padding: 6px;
  background: #2a1c0e;
  border: 3px solid #6b4c2a;
  border-radius: 3px;
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.6);
}

.grid-slot {
  width: 52px;
  height: 52px;
  background: #140d05;
  border: 2px solid #3a2815;
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.15s;
}

.grid-slot:hover {
  border-color: #8a6a3a;
}

.grid-slot.filled {
  border-color: #5a8f3a;
}

.grid-slot img {
  width: 40px;
  height: 40px;
  image-rendering: pixelated;
}

#arrow {
  font-size: 28px;
  animation: arrow-pulse 1.5s ease-in-out infinite;
}

@keyframes arrow-pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

#output-slot {
  width: 64px;
  height: 64px;
  background: #140d05;
  border: 3px solid #3a2815;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

#output-slot.valid {
  border-color: #f5c542;
  box-shadow: 0 0 16px rgba(245, 197, 66, 0.4);
  animation: output-glow 1s ease-in-out infinite alternate;
}

@keyframes output-glow {
  0% { box-shadow: 0 0 8px rgba(245, 197, 66, 0.3); }
  100% { box-shadow: 0 0 20px rgba(245, 197, 66, 0.7); }
}

#output-slot img {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
}

#craft-actions {
  display: flex;
  gap: 8px;
}

#craft-btn {
  background: linear-gradient(180deg, #d4a030 0%, #b8841a 100%);
  color: #2a1a00;
  border: 2px solid #8a5a10;
  font-size: 16px;
  font-weight: 700;
  padding: 10px 28px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.2);
  letter-spacing: 2px;
}

#craft-btn:not(:disabled):hover {
  background: linear-gradient(180deg, #e4b040 0%, #c8942a 100%);
  transform: scale(1.05);
}

#craft-btn:not(:disabled):active {
  transform: scale(0.95);
}

#craft-btn:disabled {
  background: #3a2815;
  color: #5a4a30;
  border-color: #2a1c0e;
  cursor: not-allowed;
}

#clear-btn {
  background: linear-gradient(180deg, #6b3a3a 0%, #4a2020 100%);
  color: #e8c0c0;
  border: 2px solid #3a1515;
  font-size: 10px;
}

#clear-btn:hover {
  background: linear-gradient(180deg, #7b4a4a 0%, #5a3030 100%);
}

/* TOWER */
#tower-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
  gap: 4px;
  padding: 4px;
  max-height: 380px;
}

.tower-tier {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  background: #1a1208;
  border: 2px solid #3a2815;
  border-radius: 2px;
  transition: all 0.3s;
}

.tower-tier.current {
  border-color: #f5c542;
  background: #2a1c0e;
  box-shadow: 0 0 10px rgba(245, 197, 66, 0.3);
}

.tower-tier img {
  image-rendering: pixelated;
}

.tower-tier .tier-name {
  font-size: 8px;
  color: #c4a66a;
  line-height: 1.2;
}

.tower-tier .tier-count {
  font-size: 8px;
  color: #7ecf6a;
  margin-left: auto;
}

#stats-panel {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 2px solid #3a2815;
  font-size: 10px;
  color: #8a7050;
}

#stats-panel div {
  margin-bottom: 3px;
}

#stats-panel span {
  color: #c4a66a;
}

/* TOOLTIP */
.tooltip {
  position: fixed;
  background: #1a0e05;
  border: 2px solid #6b4c2a;
  padding: 8px 12px;
  border-radius: 3px;
  z-index: 100;
  pointer-events: none;
  max-width: 220px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.6);
}

.tooltip .tt-name {
  color: #f5c542;
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 4px;
}

.tooltip .tt-tier {
  color: #7ecf6a;
  font-size: 9px;
  margin-bottom: 3px;
}

.tooltip .tt-desc {
  color: #a89070;
  font-size: 8px;
  font-style: italic;
  line-height: 1.3;
}

/* TOAST */
#toast-container {
  position: fixed;
  top: 60px;
  right: 12px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: linear-gradient(135deg, #2a1c0e 0%, #1a0e05 100%);
  border: 2px solid #f5c542;
  padding: 12px 16px;
  border-radius: 4px;
  max-width: 320px;
  animation: toast-in 0.4s ease-out, toast-out 0.4s ease-in 3.6s forwards;
  box-shadow: 0 4px 20px rgba(245, 197, 66, 0.3);
}

.toast .toast-title {
  color: #f5c542;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 4px;
}

.toast .toast-text {
  color: #a89070;
  font-size: 9px;
  line-height: 1.3;
}

@keyframes toast-in {
  from { transform: translateX(120%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes toast-out {
  from { transform: translateX(0); opacity: 1; }
  to { transform: translateX(120%); opacity: 0; }
}

/* FOOTER */
#footer {
  text-align: center;
  padding: 12px;
  font-size: 10px;
  color: #5a4a30;
  border-top: 2px solid #3a2815;
  background: rgba(20, 13, 5, 0.8);
  margin-top: auto;
}

#footer a {
  color: #8a6a3a;
  text-decoration: underline;
}

#footer a:hover {
  color: #c4a66a;
}

/* SCREEN SHAKE */
.shake {
  animation: shake 0.3s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: translate(0); }
  20% { transform: translate(-4px, 2px); }
  40% { transform: translate(4px, -2px); }
  60% { transform: translate(-2px, 4px); }
  80% { transform: translate(2px, -4px); }
}

/* PARTICLES (rendered on canvas but style for spawn button) */
.gather-flash {
  animation: flash-green 0.2s ease-out;
}

@keyframes flash-green {
  0% { box-shadow: 0 0 0 rgba(126, 207, 106, 0); }
  50% { box-shadow: 0 0 20px rgba(126, 207, 106, 0.6); }
  100% { box-shadow: 0 0 0 rgba(126, 207, 106, 0); }
}

/* MOBILE */
@media (max-width: 768px) {
  #main-layout {
    flex-direction: column;
    padding: 4px;
    gap: 6px;
  }

  #inventory-panel {
    flex: none;
    order: 1;
  }

  #inv-slots {
    grid-template-columns: repeat(5, 1fr);
    max-height: 130px;
  }

  #crafting-panel {
    order: 2;
  }

  .grid-slot {
    width: 44px;
    height: 44px;
  }

  #tower-panel {
    flex: none;
    order: 3;
  }

  #tower-container {
    flex-direction: row;
    flex-wrap: wrap;
    max-height: 120px;
    overflow-y: auto;
  }

  .tower-tier {
    flex-direction: column;
    min-width: 60px;
  }
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: #1a0e05;
}

::-webkit-scrollbar-thumb {
  background: #5a3d1e;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #6b4c2a;
}