:root {
  --bg-dark: #0f1419;
  --panel-bg: rgba(18, 25, 33, 0.92);
  --panel-border: rgba(255, 255, 255, 0.1);
  --accent-amber: #f59e0b;
  --accent-green: #10b981;
  --accent-blue: #3b82f6;
  --accent-red: #ef4444;
  --accent-nut: #d97706;
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-dark);
  color: var(--text-main);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

#game-container {
  position: relative;
  width: 100vw;
  height: 100vh;
}

#factory-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

/* Top HUD */
#top-hud {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  z-index: 20;
}

.hud-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  font-size: 28px;
  line-height: 1;
}

.hud-brand h1 {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.6px;
  color: var(--accent-amber);
}

#facility-subtitle {
  font-size: 11px;
  color: var(--text-muted);
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.stat-chip {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  padding: 5px 12px;
  display: flex;
  flex-direction: column;
  min-width: 105px;
}

.stat-chip .label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.stat-chip .value {
  font-size: 14px;
  font-weight: 700;
}

.stat-chip.money .value {
  color: var(--accent-green);
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-btn {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.hud-btn:hover {
  background: rgba(255, 255, 255, 0.16);
  transform: translateY(-1px);
}

.hud-btn.primary {
  background: linear-gradient(135deg, #d97706, #b45309);
  border-color: rgba(245, 158, 11, 0.5);
}

.hud-btn.primary:hover {
  background: linear-gradient(135deg, #f59e0b, #d97706);
}

.badge {
  background: #ef4444;
  color: #fff;
  border-radius: 999px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 700;
}

/* Inventory Strip */
#inventory-panel {
  position: fixed;
  top: 86px;
  left: 12px;
  width: 225px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 12px;
  backdrop-filter: blur(10px);
  z-index: 15;
  max-height: calc(100vh - 160px);
  overflow-y: auto;
}

#inventory-panel.collapsed {
  display: none;
}

#inventory-panel h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent-amber);
  margin-bottom: 10px;
}

.inv-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.inv-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding: 5px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
}

.inv-row .inv-name {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #e5e7eb;
}

.inv-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.inv-row .inv-qty {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #fff;
}

/* Interaction Prompt */
#interaction-prompt {
  position: fixed;
  bottom: 58px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.94);
  border: 2px solid var(--accent-amber);
  color: #fff;
  padding: 8px 18px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 6px 24px rgba(245, 158, 11, 0.3);
  z-index: 20;
  animation: pulseBorder 1.6s infinite;
}

@keyframes pulseBorder {
  0%, 100% { box-shadow: 0 6px 24px rgba(245, 158, 11, 0.25); }
  50% { box-shadow: 0 6px 28px rgba(245, 158, 11, 0.55); }
}

.key-cap {
  background: var(--accent-amber);
  color: #111827;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 13px;
}

/* Controls Bar */
#controls-bar {
  position: fixed;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 16px;
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: var(--text-muted);
  z-index: 15;
  white-space: nowrap;
}

kbd {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  color: #fff;
  margin-right: 2px;
}

/* Toast Notifications */
#toast-container {
  position: fixed;
  top: 86px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
  pointer-events: none;
}

.toast {
  background: rgba(17, 24, 39, 0.95);
  border-left: 4px solid var(--accent-amber);
  color: #fff;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  max-width: 300px;
  animation: slideIn 0.2s ease;
}

.toast.success { border-left-color: var(--accent-green); }
.toast.error { border-left-color: var(--accent-red); }
.toast.info { border-left-color: var(--accent-blue); }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* Money chip pop when the balance changes */
.stat-chip.money-gain { animation: moneyGain 0.9s ease; }
.stat-chip.money-loss { animation: moneyLoss 0.9s ease; }
@keyframes moneyGain {
  0% { transform: scale(1); box-shadow: 0 0 0 rgba(16, 185, 129, 0); }
  18% { transform: scale(1.12); box-shadow: 0 0 18px rgba(16, 185, 129, 0.75); border-color: #10b981; }
  100% { transform: scale(1); box-shadow: 0 0 0 rgba(16, 185, 129, 0); }
}
@keyframes moneyLoss {
  0% { transform: scale(1); }
  18% { transform: scale(0.94); box-shadow: 0 0 14px rgba(239, 68, 68, 0.6); border-color: #ef4444; }
  100% { transform: scale(1); }
}

/* "While you were away" welcome-back card */
.offline-hero {
  text-align: center;
  padding: 8px 0 4px;
}
.offline-hero .big {
  font-size: 40px;
  font-weight: 800;
  color: #10b981;
  letter-spacing: -0.5px;
  text-shadow: 0 0 24px rgba(16, 185, 129, 0.35);
}
.offline-hero .sub {
  color: #9ca3af;
  font-size: 13px;
  margin-top: 4px;
}
.offline-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.offline-stats div {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  padding: 8px;
  text-align: center;
  font-size: 12px;
  color: #9ca3af;
}
.offline-stats b {
  display: block;
  color: #f3f4f6;
  font-size: 15px;
  margin-top: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .stat-chip.money-gain, .stat-chip.money-loss { animation: none; }
}

/* Modal */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.68);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}

.hidden {
  display: none !important;
}

#modal-card {
  background: #161e27;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 14px;
  width: min(680px, 95vw);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.zone-tag {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.8px;
  color: var(--accent-amber);
  text-transform: uppercase;
}

.modal-header h2 {
  font-size: 18px;
  margin-top: 2px;
}

.close-btn {
  background: rgba(255, 255, 255, 0.08);
  border: none;
  color: #fff;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
}

.close-btn:hover {
  background: rgba(239, 68, 68, 0.35);
}

#modal-body {
  padding: 18px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-section {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: 10px;
  padding: 12px 14px;
}

.panel-section h4 {
  font-size: 13px;
  color: var(--accent-amber);
  margin-bottom: 8px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}

.mini-stat {
  background: rgba(0, 0, 0, 0.25);
  padding: 8px 10px;
  border-radius: 8px;
}

.mini-stat .k {
  font-size: 11px;
  color: var(--text-muted);
}

.mini-stat .v {
  font-size: 15px;
  font-weight: 700;
  margin-top: 2px;
}

.progress-bar {
  width: 100%;
  height: 10px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #f59e0b, #10b981);
  transition: width 0.2s ease;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.btn {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: 0.15s;
}

.btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.16);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.green {
  background: #059669;
  border-color: #10b981;
}

.btn.green:hover:not(:disabled) {
  background: #10b981;
}

.btn.amber {
  background: #d97706;
  border-color: #f59e0b;
}

.btn.amber:hover:not(:disabled) {
  background: #f59e0b;
}

.btn.red {
  background: #dc2626;
  border-color: #ef4444;
}

.slider-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  font-size: 13px;
}

.slider-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent-amber);
}

.order-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 12px;
}

.order-card h5 {
  font-size: 14px;
  margin-bottom: 4px;
}

.order-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.flow-diagram {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}

.flow-step {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.04);
  padding: 8px 12px;
  border-radius: 8px;
  border-left: 3px solid var(--accent-amber);
}

/* Mobile & Touch Controls */
.mobile-only {
  display: none;
}

#mobile-controls {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 14px;
  padding: 0 16px;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 25;
}

#joystick-zone {
  width: 120px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  touch-action: none;
}

#joystick-base {
  width: 104px;
  height: 104px;
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.65);
  border: 2px solid rgba(255, 255, 255, 0.22);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(6px);
}

#joystick-knob {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fbbf24, #d97706);
  border: 2px solid #fef3c7;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
  transform: translate(0, 0);
}

#mobile-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: auto;
}

.mobile-action-btn {
  background: rgba(15, 23, 42, 0.85);
  border: 2px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  padding: 12px 16px;
  border-radius: 999px;
  min-width: 96px;
  touch-action: manipulation;
  backdrop-filter: blur(6px);
}

.mobile-action-btn.active {
  background: #0284c7;
  border-color: #38bdf8;
}

.mobile-action-btn.primary:not(:disabled) {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  border-color: #fef3c7;
  color: #111827;
}

.mobile-action-btn:disabled {
  opacity: 0.4;
}

@media (pointer: coarse), (max-width: 960px) {
  .mobile-only {
    display: inline-flex;
  }

  #mobile-controls {
    display: flex;
  }

  #controls-bar {
    display: none;
  }

  #top-hud {
    top: 6px;
    left: 6px;
    right: 6px;
    padding: 8px 10px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .hud-brand h1 {
    font-size: 13px;
  }

  #facility-subtitle {
    display: none;
  }

  .hud-stats {
    gap: 6px;
  }

  .stat-chip {
    min-width: auto;
    padding: 4px 8px;
  }

  .stat-chip .label {
    font-size: 9px;
  }

  .stat-chip .value {
    font-size: 12px;
  }

  .hud-actions {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .hud-btn {
    padding: 6px 9px;
    font-size: 12px;
    white-space: nowrap;
  }

  #inventory-panel {
    top: 130px;
    left: 6px;
    width: 210px;
    max-height: 45vh;
    display: none;
  }

  #inventory-panel.mobile-open {
    display: block;
  }

  #interaction-prompt {
    bottom: 145px;
    font-size: 12px;
    padding: 6px 12px;
  }

  #modal-card {
    width: 96vw;
    max-height: 88vh;
  }
}
