:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --accent: #3d9be9;
  --accent-dim: #2a6fa8;
  --text: #e8eef4;
  --text-muted: #8b9cb3;
  --danger: #e85d5d;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --bar-h: 56px;
  --anim-bar-h: 64px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

.top-bar {
  flex-shrink: 0;
  padding: calc(8px + var(--safe-top)) 16px 8px;
  background: linear-gradient(var(--bg) 60%, transparent);
  z-index: 2;
  pointer-events: none;
}

.top-bar h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.top-bar .hint {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
}

#canvas-wrap {
  flex: 1;
  position: relative;
  min-height: 0;
}

#viewer-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  outline: none;
}

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--bg);
  z-index: 5;
  transition: opacity 0.3s;
}

.loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--surface);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading p {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.error-banner {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  padding: 10px 14px;
  background: rgba(232, 93, 93, 0.15);
  border: 1px solid var(--danger);
  border-radius: 8px;
  font-size: 0.8rem;
  color: #ffb4b4;
  z-index: 4;
}

.error-banner.hidden {
  display: none;
}

/* BOM bottom sheet */
.bom-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 20;
  opacity: 1;
  transition: opacity 0.25s;
}

.bom-backdrop.hidden {
  opacity: 0;
  pointer-events: none;
}

.bom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 55vh;
  padding-bottom: calc(var(--anim-bar-h) + var(--safe-bottom));
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  z-index: 30;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
  overflow-y: auto;
}

.bom-sheet.open {
  transform: translateY(0);
}

.bom-sheet__handle {
  width: 36px;
  height: 4px;
  background: var(--text-muted);
  border-radius: 2px;
  margin: 10px auto 0;
  opacity: 0.5;
}

.bom-close {
  position: absolute;
  top: 8px;
  right: 12px;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.bom-sheet__content {
  padding: 8px 20px 24px;
}

.bom-category {
  display: inline-block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 4px;
}

.bom-sheet h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 4px;
  padding-right: 40px;
}

.bom-id {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: ui-monospace, monospace;
  margin-bottom: 12px;
}

.bom-sheet__content > p:not(.bom-id) {
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text);
  margin-bottom: 16px;
}

.bom-meta {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  font-size: 0.85rem;
}

.bom-meta dt {
  color: var(--text-muted);
}

.bom-meta dd {
  font-weight: 500;
}

/* Animation bar */
.anim-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  min-height: var(--anim-bar-h);
  z-index: 10;
}

.anim-buttons {
  flex: 1;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  padding: 2px 0;
}

.anim-buttons::-webkit-scrollbar {
  display: none;
}

.anim-btn {
  flex-shrink: 0;
  min-height: 44px;
  min-width: 44px;
  padding: 0 16px;
  border: none;
  border-radius: 22px;
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.anim-btn:active {
  transform: scale(0.97);
}

.anim-btn.active {
  background: var(--accent-dim);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.25);
}

.anim-btn--secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  padding: 0 12px;
}

.anim-btn.hidden {
  display: none;
}

/* BOM action buttons */
.bom-actions {
  margin-top: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.bom-action-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 20px;
  border: none;
  border-radius: 22px;
  background: var(--accent);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.bom-action-btn:active {
  transform: scale(0.96);
}

.bom-action-btn--toggle.active {
  background: var(--danger);
}

.bom-action-btn--sm {
  min-width: 44px;
  padding: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.3rem;
  line-height: 1;
}

.bom-action-intensity {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.bom-action-label {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text);
  text-align: center;
}

@media (min-width: 768px) {
  .top-bar h1 { font-size: 1.25rem; }
  .bom-sheet {
    left: auto;
    right: 24px;
    bottom: calc(var(--anim-bar-h) + 24px + var(--safe-bottom));
    width: 380px;
    max-height: 70vh;
    border-radius: 12px;
    padding-bottom: 0;
    transform: translateX(120%);
  }
  .bom-sheet.open {
    transform: translateX(0);
  }
}
