:root {
  --bg: #2a2520;
  --keyboard-bg: #d4c5a9;
  --key-bg: #f5e6d3;
  --key-shadow: #8b7355;
  --key-pressed: #c4b59a;
  --text: #3a3028;
  --text-light: #f5e6d3;
  --panel-bg: #1a1510;
  --accent: #d4a574;
  --upgrade-bg: rgba(212, 197, 169, 0.1);
  --upgrade-border: rgba(212, 197, 169, 0.3);
}

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

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--keyboard-bg);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  overflow-y: auto;
}

.game-container { max-width: 1200px; width: 100%; }

/* Stats Panel */
.stats-panel {
  background: rgba(26, 21, 16, 0.5);
  padding: 20px; border-radius: 12px; margin-bottom: 30px;
  display: flex; justify-content: space-around; flex-wrap: wrap; gap: 20px;
  border: 2px solid var(--upgrade-border);
  position: relative; backdrop-filter: blur(10px);
}
.stat { display: flex; flex-direction: column; align-items: center; gap: 5px; }
.stat-label { font-size: 14px; opacity: 0.8; }
.stat-value { font-size: 28px; font-weight: bold; color: var(--accent); }

.reset-btn {
  position: absolute; top: 10px; right: 10px;
  background: rgba(255,107,107,0.2); border: 2px solid rgba(255,107,107,0.5);
  color: #ff6b6b; padding: 8px 16px; border-radius: 8px;
  font-size: 14px; font-weight: bold; cursor: pointer; transition: all 0.2s;
}
.reset-btn:hover { background: rgba(255,107,107,0.4); transform: scale(1.05); }

/* Build Container — same as keyboard-clicker keyboard container */
.build-container {
  background: var(--keyboard-bg);
  padding: 30px; border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  margin-bottom: 30px;
  animation: boardFloat 3s ease-in-out infinite;
  position: relative;
}
@keyframes boardFloat {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-5px); }
}

.build-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
}

.grid-cell {
  aspect-ratio: 1;
  min-height: 85px;
  background: rgba(58, 48, 40, 0.1);
  border: 1px solid rgba(58, 48, 40, 0.35);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s; cursor: pointer; position: relative;
}
.grid-cell:hover { border-color: var(--accent); background: rgba(212,165,116,0.1); }
.grid-cell.drag-over { border-color: var(--accent); background: rgba(212,165,116,0.15); border-style: solid; }
.grid-cell.combine-hint { border-color: #4ecdc4; background: rgba(78,205,196,0.15); border-style: solid; }

/* Keys — identical 3D keycap style */
.key {
  width: 100%; height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  border-radius: 8px; font-weight: bold;
  cursor: pointer; user-select: none;
  box-shadow: 0 4px 0 var(--key-shadow);
  transition: all 0.05s ease;
  position: relative;
  animation: keyIdle 2s ease-in-out infinite;
}
@keyframes keyIdle {
  0%, 100% { transform: translateY(0px) scale(1); }
  50% { transform: translateY(-2px) scale(1.02); }
}
.key:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 0 var(--key-shadow);
  animation: none; filter: brightness(1.1);
}
.key:active, .key.pressed {
  transform: translateY(4px);
  box-shadow: 0 0 0 var(--key-shadow);
  animation: none;
}
.key .key-letter { font-size: 28px; line-height: 1; }
.key .key-pts { font-size: 10px; opacity: 0.7; margin-top: 3px; }

/* Tier colors — colored keycaps */
.key-tier-1 { background: linear-gradient(180deg, #f5e6d3, #e0d0b8); color: var(--text); }
.key-tier-2 { background: linear-gradient(180deg, #a8e6cf, #7bc8a4); color: #2a4a3a; }
.key-tier-3 { background: linear-gradient(180deg, #87ceeb, #5ba8d0); color: #1a3a4a; }
.key-tier-4 { background: linear-gradient(180deg, #c9a0dc, #a070b8); color: #2a1a3a; }
.key-tier-5 { background: linear-gradient(180deg, #f7c873, #d4a030); color: #3a2a10; }
.key-tier-6 { background: linear-gradient(180deg, #f08080, #d05050); color: #fff; }
.key-tier-7 { background: linear-gradient(180deg, #f0a0c0, #d06090); color: #fff; }
.key-tier-8 { background: linear-gradient(180deg, #ffe066, #ffc800); color: #3a2a10;
  box-shadow: 0 0 15px rgba(255,200,0,0.4), 0 4px 0 var(--key-shadow); }

/* Golden key overlay */
.key-golden {
  background: linear-gradient(135deg, #ffd700, #ffaa00, #ffd700, #fff4a0, #ffd700) !important;
  color: #3a2000 !important;
  box-shadow: 0 0 18px rgba(255,215,0,0.6), 0 0 40px rgba(255,170,0,0.25), 0 4px 0 #b8860b !important;
  border: 2px solid rgba(255,215,0,0.7);
  position: relative;
}
.key-golden::before {
  content: '👑';
  position: absolute;
  top: -6px;
  right: -4px;
  font-size: 14px;
  filter: drop-shadow(0 0 4px rgba(255,215,0,0.8));
  z-index: 2;
}
.key-golden .key-letter {
  text-shadow: 0 0 8px rgba(255,215,0,0.6);
}
.key-golden .key-pts {
  color: #5a3000 !important;
}

/* Key shape variants */
.key-shape-wide { border-radius: 6px 6px 12px 12px; }
.key-shape-tall { border-radius: 12px 12px 6px 6px; }
.key-shape-round { border-radius: 50%; }
.key-shape-hex { clip-path: polygon(25% 0%,75% 0%,100% 50%,75% 100%,25% 100%,0% 50%); border-radius: 0; }
.key-shape-beveled { border-radius: 4px; }

/* Upgrades Panel — same as keyboard-clicker */
.upgrades-panel {
  background: rgba(26, 21, 16, 0.5);
  padding: 30px; border-radius: 12px;
  border: 2px solid var(--upgrade-border);
  backdrop-filter: blur(10px);
}
.tab-buttons { display: flex; gap: 10px; margin-bottom: 20px; justify-content: center; }
.tab-btn {
  background: var(--upgrade-bg); border: 2px solid var(--upgrade-border);
  color: var(--keyboard-bg); padding: 12px 30px; border-radius: 8px;
  font-size: 16px; font-weight: bold; cursor: pointer; transition: all 0.2s;
}
.tab-btn:hover { border-color: var(--accent); transform: translateY(-2px); }
.tab-btn.active { background: var(--accent); border-color: var(--accent); color: var(--text); }
.tab-content { display: none; }
.tab-content.active { display: block; }

.shop-container { text-align: center; max-width: 700px; margin: 0 auto; }
.shop-container h3 { color: var(--accent); font-size: 24px; margin-bottom: 10px; }
.shop-desc { opacity: 0.8; margin-bottom: 25px; }
.shop-section { margin-bottom: 30px; padding-bottom: 25px; border-bottom: 2px solid var(--upgrade-border); }
.shop-section:last-child { border-bottom: none; }
.shop-section h4 { color: var(--accent); font-size: 18px; margin-bottom: 15px; }

.shop-items-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px;
}
.shop-card {
  background: var(--upgrade-bg); border: 2px solid var(--upgrade-border);
  border-radius: 10px; padding: 14px; text-align: center; transition: all 0.2s;
}
.shop-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.shop-card .card-icon { font-size: 28px; margin-bottom: 6px; font-weight: bold; }
.shop-card .card-name { font-weight: bold; font-size: 15px; margin-bottom: 4px; }
.shop-card .card-desc { font-size: 11px; opacity: 0.6; margin-bottom: 10px; }

.buy-btn {
  background: var(--accent); color: var(--text); border: none;
  padding: 8px 18px; border-radius: 6px; font-weight: bold;
  font-size: 13px; cursor: pointer; transition: all 0.2s;
}
.buy-btn:hover { background: #e4b584; transform: scale(1.05); }
.buy-btn:disabled { background: #666; color: #999; cursor: not-allowed; transform: none; }

/* Upgrades grid */
.upgrades-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 15px; max-height: 500px; overflow-y: auto; padding-right: 10px;
}
.upgrades-grid::-webkit-scrollbar { width: 8px; }
.upgrades-grid::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); border-radius: 4px; }
.upgrades-grid::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 4px; }

.upgrade-card {
  background: var(--upgrade-bg); border: 2px solid var(--upgrade-border);
  border-radius: 8px; padding: 15px; transition: all 0.3s;
}
.upgrade-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.upgrade-card h3 { color: var(--accent); margin-bottom: 6px; font-size: 16px; }
.upgrade-card p { opacity: 0.8; margin-bottom: 12px; font-size: 13px; }
.upgrade-info { display: flex; justify-content: space-between; align-items: center; }
.upgrade-level { font-size: 14px; opacity: 0.9; }
.upgrade-btn {
  background: var(--accent); color: var(--text); border: none;
  padding: 8px 16px; border-radius: 6px; font-weight: bold;
  cursor: pointer; transition: all 0.2s; font-size: 14px;
}
.upgrade-btn:hover { background: #e4b584; transform: scale(1.05); }
.upgrade-btn:disabled { background: #666; cursor: not-allowed; opacity: 0.5; }

/* Achievements */
.achievements-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px;
}
.achievement-card {
  background: var(--upgrade-bg); border: 2px solid var(--upgrade-border);
  border-radius: 8px; padding: 14px; text-align: center; opacity: 0.4; transition: all 0.3s;
}
.achievement-card.unlocked { opacity: 1; border-color: var(--accent); }
.ach-icon { font-size: 30px; margin-bottom: 6px; }
.ach-name { font-weight: bold; font-size: 14px; margin-bottom: 4px; color: var(--accent); }
.ach-desc { font-size: 11px; opacity: 0.6; }

/* Drag ghost */
.drag-ghost {
  position: fixed; pointer-events: none; z-index: 10000;
  opacity: 0.85; transform: scale(1.1);
  width: 60px; height: 60px;
}

/* Float text */
.float-text {
  position: absolute; font-weight: bold; pointer-events: none; z-index: 100;
  animation: floatUp 0.8s ease-out forwards;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}
@keyframes floatUp {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-40px) scale(1.2); }
}

/* Combine flash */
.combine-flash {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 9999; opacity: 0;
}
.combine-flash.active {
  opacity: 1;
  background: radial-gradient(circle, rgba(212,165,116,0.25), transparent 70%);
  animation: cflash 0.4s ease-out;
}
@keyframes cflash { 0% { opacity: 1; } 100% { opacity: 0; } }

/* Toast notifications */
.toast {
  position: fixed; top: 20px; right: 20px; z-index: 5000;
  background: linear-gradient(135deg, #3a2f28, #2a2520);
  border: 2px solid var(--accent); border-radius: 10px; padding: 14px 20px;
  color: var(--keyboard-bg); font-weight: bold;
  box-shadow: 0 6px 25px rgba(0,0,0,0.5);
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.5s forwards;
  max-width: 320px;
}
.toast-title { color: var(--accent); font-size: 14px; margin-bottom: 4px; }
.toast-msg { font-size: 12px; opacity: 0.85; font-weight: normal; }
@keyframes toastIn { from { transform: translateX(120%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; transform: translateX(50px); } }

/* Neighbor chain pulse */
.key.neighbor-pulse {
  animation: neighborPop 0.25s ease !important;
  filter: brightness(1.2);
}
@keyframes neighborPop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* Keyboard press highlight */
.grid-cell.kb-highlight {
  box-shadow: 0 0 12px rgba(212,165,116,0.5);
}

@media (max-width: 768px) {
  .build-grid { grid-template-columns: repeat(5, 1fr); }
  .shop-items-grid { grid-template-columns: 1fr; }
  .upgrades-grid { grid-template-columns: 1fr; }
  .tab-btn { padding: 10px 16px; font-size: 14px; }
}

/* Unlock section */
.unlock-card { border-color: var(--accent); background: rgba(212,165,116,0.08); }
.unlock-all { border-color: #ffc800; background: rgba(255,200,0,0.06); }
.unlock-all .card-icon { color: #ffc800; }
.unlock-done { text-align: center; padding: 16px; color: var(--accent); font-weight: bold; font-size: 16px; }

/* Board controls */
.board-controls {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 12px;
}
.control-btn {
  background: var(--key-bg);
  border: 2px solid #000;
  color: #000;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s;
}
.control-btn:hover { border-color: var(--accent); transform: translateY(-1px); }
.control-btn.active {
  background: rgba(78,205,140,0.25);
  border-color: #4ecd8c;
  color: #4ecd8c;
  box-shadow: 0 0 10px rgba(78,205,140,0.3);
}

/* Trash zone (drag-to-sell) */
.trash-zone {
  margin-top: 10px;
  padding: 16px;
  border: 2px dashed var(--accent);
  border-radius: 10px;
  text-align: center;
  color: var(--accent);
  font-size: 16px;
  font-weight: bold;
  transition: all 0.2s;
  user-select: none;
  background: rgba(212,165,116,0.08);
}
.trash-zone.trash-hover {
  border-color: #d05050;
  background: rgba(208,80,80,0.25);
  color: #d05050;
  transform: scale(1.03);
  font-size: 18px;
}

/* Rebirth tab */
.rebirth-info {
  text-align: center;
  padding: 20px;
}
.rebirth-icon { font-size: 48px; margin-bottom: 10px; }
.rebirth-stat { font-size: 16px; margin-bottom: 6px; }
.rebirth-stat .accent { color: var(--accent); font-weight: bold; }
.rebirth-req { font-size: 13px; opacity: 0.6; margin: 12px 0 8px; }
.rebirth-progress {
  width: 100%; max-width: 300px; margin: 0 auto 8px;
  height: 12px; background: rgba(255,255,255,0.1);
  border-radius: 6px; overflow: hidden;
}
.rebirth-bar {
  height: 100%; background: linear-gradient(90deg, var(--accent), #ffc800);
  border-radius: 6px; transition: width 0.3s;
}
.rebirth-btn {
  margin-top: 16px;
  background: linear-gradient(135deg, var(--accent), #ffc800);
  color: var(--text); border: none;
  padding: 12px 32px; border-radius: 10px;
  font-size: 16px; font-weight: bold;
  cursor: pointer; transition: all 0.2s;
}
.rebirth-btn:hover { transform: scale(1.05); filter: brightness(1.1); }
.rebirth-btn:disabled { background: #666; color: #999; cursor: not-allowed; transform: none; filter: none; }

/* Background picker */
.bg-picker { margin-top: 25px; }
.bg-picker h4 { color: var(--accent); text-align: center; margin-bottom: 15px; }
.bg-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px;
}
.bg-card {
  background: var(--upgrade-bg); border: 2px solid var(--upgrade-border);
  border-radius: 8px; padding: 10px; text-align: center;
  cursor: pointer; transition: all 0.2s;
}
.bg-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.bg-card.active { border-color: #4ecd8c; background: rgba(78,205,140,0.1); }
.bg-card.locked { opacity: 0.4; cursor: not-allowed; }
.bg-preview {
  width: 100%; height: 40px; border-radius: 4px; margin-bottom: 6px;
  background: var(--panel-bg);
}
.bg-name { font-size: 12px; font-weight: bold; }
.bg-lock { font-size: 10px; opacity: 0.6; margin-top: 4px; }

/* Rebirth animation keyframes */
@keyframes rb-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}
@keyframes rb-ring {
  0% { width: 100px; height: 100px; opacity: 1; }
  100% { width: 800px; height: 800px; opacity: 0; }
}
@keyframes rb-pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.15); }
}
@keyframes rb-explode {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0) rotate(360deg); opacity: 0; }
}
@keyframes rb-fadein {
  from { opacity: 0; transform: scale(0.5); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes rb-fill {
  0% { width: 0%; }
  100% { width: 100%; }
}
@keyframes rb-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
@keyframes rb-flash {
  0% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 0; }
}

/* Wildcard key */
.key-wildcard {
  background: linear-gradient(135deg, #ff6b9d, #c44dff, #4dc9ff, #6bff8d, #ffd74d, #ff6b9d) !important;
  background-size: 300% 300%;
  animation: wildcardShift 3s ease infinite, keyIdle 2s ease-in-out infinite;
  color: #fff !important;
  text-shadow: 0 0 8px rgba(255,255,255,0.8);
  box-shadow: 0 0 15px rgba(196,77,255,0.5), 0 4px 0 #8a3ab0 !important;
  border: 2px solid rgba(255,255,255,0.4);
}
@keyframes wildcardShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* Challenge HUD */
.challenge-hud {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(26, 21, 16, 0.95);
  border: 2px solid var(--accent);
  border-radius: 12px;
  padding: 12px 24px;
  z-index: 5000;
  min-width: 280px;
  text-align: center;
  box-shadow: 0 6px 25px rgba(0,0,0,0.6);
  backdrop-filter: blur(10px);
}
.ch-title { color: var(--accent); font-weight: bold; font-size: 14px; margin-bottom: 4px; }
.ch-progress { font-size: 18px; font-weight: bold; color: var(--keyboard-bg); margin-bottom: 6px; }
.ch-bar-wrap {
  width: 100%; height: 8px; background: rgba(255,255,255,0.1);
  border-radius: 4px; overflow: hidden; margin-bottom: 4px;
}
.ch-bar {
  height: 100%; background: linear-gradient(90deg, var(--accent), #4ecd8c);
  border-radius: 4px; transition: width 0.3s;
}
.ch-time { font-size: 12px; opacity: 0.7; }

/* Stats tab */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  text-align: left;
  max-width: 500px;
  margin: 0 auto;
}
.stat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--upgrade-bg);
  border: 1px solid var(--upgrade-border);
  border-radius: 6px;
}
.stat-icon { font-size: 18px; flex-shrink: 0; }
.stat-lbl { flex: 1; font-size: 13px; opacity: 0.8; }
.stat-val { font-weight: bold; color: var(--accent); font-size: 14px; }

.save-section { text-align: center; }

/* Offline progress overlay */
.offline-overlay {
  position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
  background: rgba(0,0,0,0.8); z-index: 15000;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(5px);
}
.offline-box {
  background: linear-gradient(135deg, #2a2520, #1a1510);
  border: 2px solid var(--accent);
  border-radius: 16px;
  padding: 30px 40px;
  text-align: center;
  color: var(--keyboard-bg);
  box-shadow: 0 10px 50px rgba(0,0,0,0.8);
  max-width: 360px;
  animation: toastIn 0.3s ease;
}
