:root {
  --bg: #0b0f19;
  --panel: rgba(17,24,39,0.75);
  --cell: #1f2937;
  --cell-hover: #374151;
  --border: rgba(255,255,255,0.08);
  --accent: #f59e0b;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --data: #38bdf8;
  --packet: #c084fc;
  --hash: #fbbf24;
  --core: #f472b6;
  --bit: #34d399;
  --good: #22c55e;
  --bad: #ef4444;
  --glass: rgba(17,24,39,0.65);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  overflow: hidden;
  display: flex; flex-direction: column;
}
button {
  background: var(--cell);
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.15s;
}
button:hover:not(:disabled) { background: var(--cell-hover); border-color: var(--muted); transform: translateY(-1px); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-accent { background: var(--accent); color: #000; border-color: var(--accent); font-weight: 700; }
.btn-accent:hover:not(:disabled) { background: #fbbf24; }
.btn-danger { background: #7f1d1d; border-color: #b91c1c; color: #fecaca; }
.glass {
  background: var(--glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
}

/* Loader */
.loader-screen {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  transition: opacity .6s;
}
.loader-box { text-align: center; }
.loader-spin { width: 64px; height: 64px; animation: spin 1.2s linear infinite; }
@keyframes spin { 100% { transform: rotate(360deg); } }
.loader-text { margin-top: 0.75rem; color: var(--muted); font-weight: 600; letter-spacing: 0.05em; }
.loader-bar { width: 160px; height: 3px; background: var(--cell); border-radius: 2px; margin: 0.75rem auto 0; overflow: hidden; }
.loader-bar > div { height: 100%; background: var(--accent); width: 0%; animation: load 1.5s ease-out forwards; }
@keyframes load { 100% { width: 100%; } }

/* Title Screen */
.title-body { overflow: auto; }
.title-bg {
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background: radial-gradient(circle at 50% 30%, rgba(245,158,11,0.08), transparent 60%);
}
.title-main {
  position: relative; z-index: 1; max-width: 900px; margin: 0 auto;
  padding: 4rem 1rem;
  display: flex; flex-direction: column; align-items: center; gap: 1.5rem;
  transition: opacity .6s;
  min-height: 100vh;
  justify-content: center;
}
.title-logo { display: flex; align-items: center; gap: 0.75rem; font-size: 2rem; font-weight: 900; letter-spacing: 0.08em; text-shadow: 0 0 30px rgba(245,158,11,0.3); }
.panel { padding: 1.5rem; width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 0.75rem; }
.panel p { color: var(--muted); font-size: 0.9rem; }
.panel h3 { margin-bottom: 0.25rem; }
.hidden { display: none !important; }
input[type="text"] {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 0.6rem; border-radius: 0.5rem; font-size: 1rem;
}
.session-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; width: 100%; max-width: 720px; }
.session-card {
  background: var(--glass); backdrop-filter: blur(10px); border: 1px solid var(--border);
  border-radius: 0.75rem; padding: 1rem; cursor: pointer; transition: transform .2s, border-color .2s;
  display: flex; flex-direction: column; gap: 0.5rem;
}
.session-card:hover { transform: translateY(-4px); border-color: var(--accent); }
.session-card h4 { font-size: 1rem; display: flex; align-items: center; gap: 0.4rem; }
.session-card .meta { font-size: 0.8rem; color: var(--muted); }
.session-card .actions { margin-top: auto; display: flex; gap: 0.4rem; }
.session-card.new { border-style: dashed; align-items: center; justify-content: center; min-height: 140px; color: var(--muted); }
.session-card.new:hover { color: var(--text); border-color: var(--good); }
.title-footer { margin-top: 2rem; text-align: center; color: var(--muted); font-size: 0.85rem; display: flex; flex-direction: column; gap: 0.5rem; }
.footer-links { display: flex; gap: 1rem; justify-content: center; align-items: center; }
.footer-link { color: var(--text); transition: all .2s; display: flex; align-items: center; }
.footer-link:hover { color: var(--accent); transform: translateY(-2px); }
.row { display: flex; gap: 0.5rem; }

/* Leaderboards */
.lb-select { width: 100%; margin-bottom: 0.75rem; padding: 0.4rem; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 0.375rem; }
.lb-table-wrap { max-height: 320px; overflow-y: auto; }
.lb-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.lb-table th { text-align: left; color: var(--muted); padding: 0.4rem; border-bottom: 1px solid var(--border); }
.lb-table td { padding: 0.4rem; border-bottom: 1px solid rgba(255,255,255,0.04); }
.lb-table tr:nth-child(1) td { color: var(--accent); font-weight: 700; }
.lb-table tr:nth-child(2) td { color: #cbd5e1; font-weight: 600; }
.lb-table tr:nth-child(3) td { color: #94a3b8; font-weight: 600; }

/* Game Layout */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 1rem; background: var(--panel);
  border-bottom: 1px solid var(--border); gap: 1rem; flex-wrap: wrap;
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 800; letter-spacing: 0.05em; font-size: 1.1rem; }
.resources { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.res {
  display: flex; align-items: center; gap: 0.35rem;
  background: var(--bg); padding: 0.35rem 0.6rem; border-radius: 0.5rem;
  border: 1px solid var(--border); font-variant-numeric: tabular-nums; font-weight: 600; font-size: 0.85rem;
  position: relative; cursor: help;
}
.res img { width: 16px; height: 16px; }
main { display: flex; flex: 1; overflow: hidden; }
#grid-wrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 1rem; overflow: auto; }
#grid {
  display: grid;
  grid-template-columns: repeat(var(--size, 4), 80px);
  grid-template-rows: repeat(var(--size, 4), 80px);
  gap: 0.6rem;
  position: relative;
}
.cell {
  background: var(--cell);
  border: 2px solid var(--border);
  border-radius: 0.6rem;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  position: relative; cursor: pointer; user-select: none;
  transition: transform .08s, border-color .2s, box-shadow .2s, background .2s;
}
.cell:hover { border-color: var(--muted); }
.cell.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245,158,11,0.25); animation: heartbeat 1.2s infinite; }
.cell.mergeable { border-color: var(--good); box-shadow: 0 0 0 3px rgba(34,197,94,0.25); }
.cell .fav { width: 32px; height: 32px; pointer-events: none; transition: transform .2s; }
.cell .lvl { position: absolute; bottom: 4px; right: 6px; font-size: 10px; color: var(--muted); font-weight: 700; }
.cell .synergy { position: absolute; top: 4px; left: 6px; font-size: 10px; color: var(--accent); font-weight: 700; }
.cell .terrain-badge { position: absolute; top: 4px; right: 6px; font-size: 9px; opacity: 0.7; }
.cell:active { transform: scale(0.96); }
.cell.drag-source { opacity: 0.35; }
.cell.drag-valid { border-color: var(--good); background: rgba(34,197,94,0.1); }
.cell.drag-invalid { border-color: var(--bad); background: rgba(239,68,68,0.1); }
.cell.drag-swap { border-color: #fbbf24; background: rgba(251,191,36,0.1); }
@keyframes heartbeat { 0%,100%{box-shadow: 0 0 0 3px rgba(245,158,11,0.2);} 50%{box-shadow: 0 0 0 6px rgba(245,158,11,0.08);} }
@keyframes pop { 0%{transform:scale(0);} 70%{transform:scale(1.15);} 100%{transform:scale(1);} }
.spawn-anim { animation: pop 0.35s cubic-bezier(0.175,0.885,0.32,1.275); }
@keyframes breathe { 0%,100%{transform:scale(1);} 50%{transform:scale(1.04);} }
.synergy-breathe { animation: breathe 2.5s ease-in-out infinite; }

/* Terrain */
.terrain-dead { background: #1f1515; border-color: #451a1a; }
.terrain-charged { box-shadow: inset 0 0 20px rgba(245,158,11,0.12); }
.terrain-rapid { box-shadow: inset 0 0 20px rgba(56,189,248,0.12); }
.terrain-rich { box-shadow: inset 0 0 20px rgba(34,197,94,0.12); }

/* Ghost */
.drag-ghost {
  position: fixed; pointer-events: none; z-index: 100;
  width: 80px; height: 80px;
  display: flex; align-items: center; justify-content: center;
  background: var(--cell); border: 2px solid var(--accent);
  border-radius: 0.6rem; opacity: 0.9;
  transition: none;
}
.drag-ghost .fav { width: 32px; height: 32px; }

aside {
  width: 400px; min-width: 320px; max-width: 42vw;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  backdrop-filter: blur(8px);
}
.tabs { display: flex; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs button { flex: 1; border-radius: 0; border: none; border-bottom: 2px solid transparent; background: transparent; white-space: nowrap; }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); }
.tab-content { display: none; padding: 1rem; overflow-y: auto; flex: 1; }
.tab-content.active { display: block; animation: slideIn .2s ease; }
@keyframes slideIn { from { opacity: 0; transform: translateX(8px); } to { opacity: 1; transform: translateX(0); } }

.context { background: var(--glass); border: 1px solid var(--border); border-radius: 0.5rem; padding: 0.75rem; margin-bottom: 1rem; }
.context h3 { font-size: 0.9rem; margin-bottom: 0.5rem; display: flex; align-items: center; gap: 0.4rem; }
.context .row { display: flex; gap: 0.5rem; margin-top: 0.5rem; flex-wrap: wrap; }

.shop-item, .upgrade-card, .research-card {
  display: flex; align-items: center; gap: 0.75rem;
  background: var(--glass); border: 1px solid var(--border);
  padding: 0.6rem; border-radius: 0.5rem; margin-bottom: 0.5rem;
}
.shop-item .info, .upgrade-card .left { flex: 1; }
.shop-item .name, .upgrade-card .name, .research-card .name { font-weight: 700; font-size: 0.9rem; }
.shop-item .meta, .upgrade-card .desc, .research-card .desc { font-size: 0.8rem; color: var(--muted); }
.shop-item .cost { font-weight: 700; color: var(--data); }
.upgrade-card .lvl { font-size: 0.75rem; color: var(--accent); margin-top: 0.1rem; }

/* Forge */
.forge-slots { display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 0.5rem; margin: 0.75rem 0; }
.forge-slot {
  aspect-ratio: 1; border: 2px dashed var(--border); border-radius: 0.5rem;
  display: flex; align-items: center; justify-content: center; font-size: 1.5rem; color: var(--muted);
  background: var(--glass);
}
.forge-slot.filled { border-style: solid; border-color: var(--accent); }

/* Toast Stack */
#toast-stack { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column; gap: 0.4rem; z-index: 50; pointer-events: none; }
.toast {
  background: var(--panel); border: 1px solid var(--border);
  padding: 0.6rem 0.9rem; border-radius: 0.5rem;
  box-shadow: 0 10px 20px rgba(0,0,0,0.4);
  transform: translateX(40px); opacity: 0; animation: toastIn .3s forwards;
  max-width: 280px; font-size: 0.9rem; pointer-events: auto;
}
@keyframes toastIn { to { transform: translateX(0); opacity: 1; } }
.toast.out { animation: toastOut .3s forwards; }
@keyframes toastOut { to { transform: translateX(40px); opacity: 0; } }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }
.modal { padding: 1.5rem; width: 90%; max-width: 520px; box-shadow: 0 20px 40px rgba(0,0,0,0.5); }
.modal h2 { margin-bottom: 0.75rem; font-size: 1.25rem; }
.modal p { color: var(--muted); margin-bottom: 1rem; line-height: 1.5; }
.modal .actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; }

.core-choice {
  background: var(--glass); border: 1px solid var(--border);
  padding: 1rem; border-radius: 0.5rem; cursor: pointer; margin-bottom: 0.5rem;
  transition: border-color .15s, transform .15s;
}
.core-choice:hover { border-color: var(--core); transform: translateX(4px); }
.core-choice h4 { color: var(--core); margin-bottom: 0.25rem; }
.core-choice p { font-size: 0.85rem; color: var(--muted); margin: 0; }

.achieve {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.5rem; border-radius: 0.375rem; margin-bottom: 0.4rem;
  background: var(--glass); border: 1px solid var(--border); opacity: 0.5;
}
.achieve.unlocked { opacity: 1; border-color: var(--good); }
.achieve .badge { font-size: 0.75rem; background: var(--good); color: #000; padding: 0.1rem 0.4rem; border-radius: 0.25rem; font-weight: 700; }

.float {
  position: fixed; pointer-events: none; font-weight: 800; font-size: 12px;
  animation: floatArc 1s forwards; z-index: 40;
}
@keyframes floatArc {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(-32px) rotate(var(--rot, 6deg)); opacity: 0; }
}

.progress-bar { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 0.25rem; }
.progress-bar > div { height: 100%; background: var(--accent); width: 0%; transition: width .2s; }

/* Countdown */
#countdown-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
}
#countdown-num { font-size: 8rem; font-weight: 900; color: var(--accent); animation: cdPop .8s ease-out; text-shadow: 0 0 40px rgba(245,158,11,0.5); }
@keyframes cdPop { 0%{transform:scale(2); opacity:0;} 50%{transform:scale(0.9); opacity:1;} 100%{transform:scale(1); opacity:0;} }

/* Lie Button */
.lie-btn {
  position: fixed; bottom: 1rem; left: 1rem; z-index: 45;
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--panel); border: 2px solid var(--bad);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; opacity: 0.7; transition: opacity .2s, transform .2s;
}
.lie-btn:hover { opacity: 1; transform: scale(1.1); }
.lie-btn:active { transform: scale(0.95); }

/* Tooltip */
.tooltip {
  position: fixed; z-index: 90; pointer-events: none;
  background: rgba(0,0,0,0.9); border: 1px solid var(--border);
  color: var(--muted); padding: 0.5rem 0.75rem; border-radius: 0.5rem;
  font-size: 0.8rem; max-width: 240px; line-height: 1.4;
  opacity: 0; transition: opacity .15s;
}
.tooltip.show { opacity: 1; }

/* Settings */
.setting-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; font-size: 0.9rem; }
.setting-row input[type="range"] { width: 140px; }

/* Zen Mode */
body.zen aside { width: 0; min-width: 0; padding: 0; border: none; opacity: 0; pointer-events: none; }
body.zen header .resources { opacity: 0; pointer-events: none; }
body.zen header { justify-content: center; }
body.zen #grid-wrap { padding: 2rem; }

/* Graph */
#stats-graph { width: 100%; background: var(--glass); border-radius: 0.5rem; border: 1px solid var(--border); }

/* Cursor */
body.pc-mode { cursor: crosshair; }
body.pc-mode .cell { cursor: pointer; }
body.pc-mode .cell:active { cursor: grabbing; }

/* Particles */
.particle {
  position: fixed; pointer-events: none; z-index: 35;
  width: 6px; height: 6px; border-radius: 1px;
  animation: particleBurst .6s ease-out forwards;
}
@keyframes particleBurst {
  0% { transform: translate(0,0) scale(1); opacity: 1; }
  100% { transform: translate(var(--tx), var(--ty)) scale(0); opacity: 0; }
}

/* Responsive */
@media (max-width: 900px) {
  aside { width: 340px; }
  #grid { grid-template-columns: repeat(var(--size, 4), 64px); grid-template-rows: repeat(var(--size, 4), 64px); }
}
@media (max-width: 700px) {
  main { flex-direction: column; }
  aside { width: 100%; max-width: 100%; border-left: none; border-top: 1px solid var(--border); height: 42vh; }
  .session-grid { grid-template-columns: 1fr; }
  .title-main { padding: 2rem 1rem; }
  .title-logo { font-size: 1.5rem; }
}
