:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --surface-hover: #2a2a2a;
  --text: #f5f5f5;
  --text-dim: #999;
  --accent: #e88a1a;
  --secondary: #f5c518;
  --border: #333;
  --cell-bg: transparent;
  --cell-chassis: rgba(24, 24, 24, 0.7);
  --cell-hover: #2e2e2e;
  --green: #4caf50;
  --red: #f44336;
}

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

body {
  height: 100vh;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-areas:
    "header header header"
    "left   grid   shop";
  grid-template-columns: 200px 1fr 240px;
  grid-template-rows: auto 1fr;
  gap: 0.75rem;
  padding: 0.75rem;
  overflow: hidden;
}

/* Header */
header#top-bar {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 1rem;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
}

.back-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85rem;
  white-space: nowrap;
}
.back-link:hover { color: var(--text); }

.logo {
  display: none;
}

.money-display {
  margin-left: auto;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--secondary);
}

/* Left Panel (Stats + Inventory) */
#left-panel {
  grid-area: left;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  overflow-y: auto;
  min-height: 0;
}

#stats-panel {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 1rem;
}

#stats-panel h2 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 0.75rem;
}

#stats-table {
  width: 100%;
  border-collapse: collapse;
}

#stats-table tr { border-bottom: 1px solid var(--border); }
#stats-table td { padding: 0.4rem 0; font-size: 0.8rem; white-space: nowrap; }
.stat-bar-cell { width: 100%; padding: 0.4rem 0.35rem !important; }

.stat-bar {
  width: 100%;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.stat-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.3s ease, background-color 0.3s ease;
}

.stat-value {
  text-align: right;
  font-weight: 600;
  min-width: 28px;
}

.stat-delta {
  font-size: 0.7rem;
  font-weight: 700;
  margin-left: 4px;
}
.stat-delta.positive { color: var(--green); }
.stat-delta.negative { color: var(--red); }

/* Inventory Panel */
#inventory-panel {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 1rem;
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

#inventory-panel h2 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 0.75rem;
}

#inventory-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.inv-card {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: grab;
  touch-action: none;
  user-select: none;
  transition: background 0.15s, transform 0.1s;
}

.inv-card:hover {
  background: var(--surface-hover);
}

.inv-card:active {
  cursor: grabbing;
}

.inv-card .emoji { font-size: 1.3rem; }
.inv-card .name { font-size: 0.8rem; flex: 1; }
.inv-card .count {
  background: var(--accent);
  color: var(--bg);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

/* Car Grid */
#car-grid {
  grid-area: grid;
  display: grid;
  grid-template-columns: auto repeat(9, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 2px;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 6px;
  touch-action: none;
  user-select: none;
  position: relative;
  min-width: 0;
  overflow: hidden;
}

#car-grid::before {
  content: "";
  position: absolute;
  inset: 0;
  background: url("images/logo.png") center / 60% no-repeat;
  opacity: 0.6;
  pointer-events: none;
  z-index: 2;
  mix-blend-mode: multiply;
}

.grid-cell {
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  cursor: default;
  position: relative;
  transition: transform 0.15s ease, background-color 0.15s ease;
  z-index: 1;
}

.row-label {
  display: flex;
  align-items: center;
  font-size: 0.6rem;
  color: var(--text-dim);
  padding-right: 0.3rem;
  white-space: nowrap;
  user-select: none;
  z-index: 1;
}

.grid-cell.chassis {
  background: var(--cell-chassis);
  border-radius: 4px;
  border: 1px solid rgba(42, 42, 42, 0.7);
}

.grid-cell.has-part {
  cursor: grab;
}

.grid-cell.has-part:active {
  cursor: grabbing;
}

.grid-cell.drag-over {
  background: #2a3a2a;
  box-shadow: inset 0 0 0 2px var(--green);
}

.grid-cell.drag-over-invalid {
  background: #3a2a2a;
  box-shadow: inset 0 0 0 2px var(--red);
}

.grid-cell .part-placed {
  animation: popIn 0.2s ease;
}

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

/* Shop Panel (right sidebar) */
#shop-panel {
  grid-area: shop;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
  min-height: 0;
}

#shop-panel h2 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 0.75rem;
}

#shop-panel .shop-grid {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.shop-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  transition: border-color 0.15s;
}

.shop-card:hover { border-color: var(--accent); }

.shop-card .card-top {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.shop-card .emoji { font-size: 1.2rem; }

.shop-card .card-info {
  flex: 1;
}

.shop-card .card-name {
  font-size: 0.78rem;
  font-weight: 600;
}

.shop-card .card-tier {
  font-size: 0.65rem;
  color: var(--secondary);
}

.shop-card .card-stats {
  font-size: 0.65rem;
  color: var(--text-dim);
  line-height: 1.3;
}

.shop-card .card-buy {
  margin-top: 0.2rem;
  padding: 0.35rem;
  font-size: 0.75rem;
  font-weight: 700;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: var(--accent);
  color: var(--bg);
  transition: opacity 0.15s, transform 0.1s;
}

.shop-card .card-buy:hover:not(:disabled) {
  transform: scale(1.03);
}

.shop-card .card-buy:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Reset Button */
.reset-btn {
  position: fixed;
  bottom: 1.5rem;
  left: 1.5rem;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  font-size: 1.1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  cursor: pointer;
  color: var(--text-dim);
  transition: color 0.15s;
  z-index: 50;
}
.reset-btn:hover { color: var(--red); }

/* Drag Ghost */
.drag-ghost {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  font-size: 2rem;
  opacity: 0.85;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 2px 8px rgba(0,0,0,0.5));
}

/* Responsive */
@media (max-width: 768px) {
  body {
    grid-template-areas:
      "header"
      "grid"
      "left"
      "shop";
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    height: auto;
    overflow: auto;
  }

  #left-panel {
    max-height: 300px;
  }

  #shop-panel {
    max-height: 400px;
  }

  #shop-panel .shop-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }

  .grid-cell {
    aspect-ratio: auto;
    min-height: 32px;
  }
}
