:root {
  --color-charcoal: #15171a;
  --color-panel: #1d2024;
  --color-panel-border: #2c3036;
  --color-panel-soft: rgba(29, 32, 36, 0.82);
  --color-cyan: #3ddbd9;
  --color-orange: #ff9142;
  --color-amber: #f5c542;
  --color-red: #e5484d;
  --color-green: #4ade80;
  --color-text: #e6e8eb;
  --color-text-dim: #8b929c;
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--color-charcoal);
  color: var(--color-text);
  font-family: 'Segoe UI', 'Inter', system-ui, -apple-system, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#top-bar {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-area-top)) 16px 10px;
  background: linear-gradient(to bottom, rgba(21,23,26,0.96), rgba(21,23,26,0.62));
  border-bottom: 1px solid var(--color-panel-border);
  pointer-events: none;
  backdrop-filter: blur(14px);
}

#top-bar > * { pointer-events: auto; }

#factory-name {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.02em;
}

#milestone-badge {
  font-size: 11px;
  color: var(--color-text-dim);
  border: 1px solid var(--color-panel-border);
  border-radius: 999px;
  padding: 2px 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.top-bar-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

button {
  font-family: inherit;
  font-size: 13px;
  color: var(--color-text);
  background: var(--color-panel);
  border: 1px solid var(--color-panel-border);
  border-radius: 8px;
  padding: 9px 14px;
  min-height: 40px;
  min-width: 40px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  padding: 0;
  width: 40px;
  height: 40px;
  font-size: 15px;
  font-weight: 700;
}

.text-button {
  min-width: 48px;
  padding: 0 10px;
  font-variant-numeric: tabular-nums;
}

button:active { transform: scale(0.96); }

button:disabled {
  opacity: 0.4;
  cursor: default;
}

button:not(:disabled):hover {
  border-color: var(--color-cyan);
}

button.active {
  background: rgba(61, 219, 217, 0.14);
  border-color: var(--color-cyan);
  color: var(--color-cyan);
}

.hidden { display: none !important; }

#hud-bar {
  position: relative;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px 8px;
  pointer-events: none;
}

.hud-pill {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--color-text-dim);
  background: var(--color-panel-soft);
  border: 1px solid var(--color-panel-border);
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
  backdrop-filter: blur(10px);
}

.objective-pill {
  position: relative;
  overflow: hidden;
}

.objective-pill::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--objective-progress, 0%);
  background: linear-gradient(90deg, rgba(61, 219, 217, 0.16), rgba(74, 222, 128, 0.2));
  z-index: -1;
}

.hud-pill.warning {
  color: var(--color-amber);
  border-color: var(--color-amber);
}

.hud-pill.complete {
  color: var(--color-green);
  border-color: var(--color-green);
}

#blueprint-panel {
  position: fixed;
  z-index: 10;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(360px, calc(100vw - 32px));
  max-height: min(480px, calc(100vh - 32px));
  display: flex;
  flex-direction: column;
  background: var(--color-panel);
  border: 1px solid var(--color-panel-border);
  border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  padding: 14px;
  gap: 10px;
}

#inspector-panel {
  position: fixed;
  z-index: 3;
  left: 16px;
  bottom: calc(76px + var(--safe-area-bottom));
  width: min(330px, calc(100vw - 32px));
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  padding: 10px 10px 10px 12px;
  color: var(--color-text);
  background: var(--color-panel-soft);
  border: 1px solid rgba(61, 219, 217, 0.45);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.38);
  backdrop-filter: blur(14px);
  pointer-events: auto;
}

#inspector-title,
#inspector-detail {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#inspector-title {
  font-size: 13px;
  font-weight: 700;
}

#inspector-detail {
  max-width: 210px;
  margin-top: 2px;
  font-size: 11px;
  color: var(--color-text-dim);
  font-variant-numeric: tabular-nums;
}

.inspector-actions {
  display: flex;
  gap: 6px;
  flex: none;
}

.inspector-actions button {
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  font-size: 17px;
  line-height: 1;
}

#toast-stack {
  position: fixed;
  z-index: 20;
  top: calc(70px + var(--safe-area-top));
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(280px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  padding: 10px 12px;
  color: var(--color-text);
  background: rgba(29, 32, 36, 0.94);
  border: 1px solid var(--color-panel-border);
  border-left: 3px solid var(--color-cyan);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.34);
  font-size: 12px;
  line-height: 1.3;
  opacity: 0;
  transform: translateY(-6px);
  animation: toast-in 180ms ease forwards, toast-out 220ms ease forwards 2300ms;
}

.toast.warning { border-left-color: var(--color-amber); }
.toast.success { border-left-color: var(--color-green); }

@keyframes toast-in {
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  to { opacity: 0; transform: translateY(-6px); }
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 14px;
}

.panel-close {
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
}

.blueprint-save-row {
  display: flex;
  gap: 8px;
}

.blueprint-save-row input {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 13px;
  color: var(--color-text);
  background: var(--color-charcoal);
  border: 1px solid var(--color-panel-border);
  border-radius: 8px;
  padding: 9px 12px;
}

#blueprint-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.blueprint-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--color-panel-border);
  border-radius: 8px;
}

.blueprint-info {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--color-text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.blueprint-row button {
  min-height: 32px;
  padding: 6px 10px;
  font-size: 12px;
}

.blueprint-empty {
  color: var(--color-text-dim);
  font-size: 13px;
  margin: 8px 0;
}

#bottom-toolbar {
  position: relative;
  z-index: 2;
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px calc(10px + var(--safe-area-bottom));
  background: linear-gradient(to top, rgba(21,23,26,0.96), rgba(21,23,26,0.64));
  border-top: 1px solid var(--color-panel-border);
  overflow-x: auto;
  pointer-events: none;
  backdrop-filter: blur(14px);
  scrollbar-width: none;
}

#bottom-toolbar::-webkit-scrollbar {
  display: none;
}

#bottom-toolbar > * { pointer-events: auto; }

.toolbar-spacer {
  margin-left: auto;
}

.toolbar-divider {
  width: 1px;
  align-self: stretch;
  background: var(--color-panel-border);
  margin: 4px 2px;
}

#bottom-toolbar button[data-tool] {
  display: grid;
  grid-template-columns: 24px max-content;
  align-items: center;
  gap: 7px;
  flex: 0 0 auto;
  min-width: 104px;
  padding: 8px 12px;
}

.tool-icon {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  color: var(--color-charcoal);
  background: var(--color-text-dim);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 800;
  line-height: 1;
}

button.active .tool-icon {
  color: var(--color-charcoal);
  background: var(--color-cyan);
}

@media (max-width: 760px) {
  #top-bar {
    gap: 8px;
    padding: calc(8px + var(--safe-area-top)) 10px 8px;
  }

  #factory-name {
    max-width: 34vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #milestone-badge {
    display: none;
  }

  .top-bar-actions {
    gap: 5px;
  }

  .icon-button {
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
  }

  .text-button {
    min-width: 44px;
    font-size: 12px;
  }

  #hud-bar {
    padding: 0 10px 6px;
    gap: 6px;
  }

  .hud-pill {
    max-width: calc(100vw - 20px);
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #toast-stack {
    top: auto;
    right: 10px;
    bottom: calc(136px + var(--safe-area-bottom));
  }

  #inspector-panel {
    left: 10px;
    bottom: calc(82px + var(--safe-area-bottom));
    width: calc(100vw - 20px);
  }

  #inspector-detail {
    max-width: calc(100vw - 160px);
  }

  #bottom-toolbar {
    gap: 6px;
    padding: 8px 10px calc(8px + var(--safe-area-bottom));
  }

  #bottom-toolbar button[data-tool] {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 4px;
    width: 72px;
    min-width: 72px;
    min-height: 58px;
    padding: 7px 6px;
    font-size: 11px;
  }

  #bottom-toolbar > button:not([data-tool]) {
    min-width: 64px;
    min-height: 58px;
    padding: 7px 8px;
    font-size: 11px;
  }
}
