:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1a2332;
  --muted: #5c6b7a;
  --accent: #0066cc;
  --accent-dark: #004c99;
  --success: #1f7a4c;
  --warning: #b45309;
  --border: #d8dee6;
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

header {
  background: var(--accent);
  color: white;
  padding: 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

header .header-inner {
  max-width: 1600px;
  margin: 0 auto;
}

header h1 {
  margin: 0;
  font-size: 1.1rem;
}

header p {
  margin: 0.25rem 0 0;
  opacity: 0.9;
  font-size: 0.85rem;
}

main {
  max-width: 1600px;
  margin: 0 auto;
  padding: 1rem;
}

.detail-grid {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.detail-main,
.detail-side {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

.detail-side .card {
  margin-bottom: 0;
}

.detail-main .card {
  margin-bottom: 0;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.card h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

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

.assembly-list {
  display: grid;
  gap: 0.75rem;
}

.assembly-item {
  display: block;
  text-decoration: none;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.assembly-item:hover {
  border-color: var(--accent);
}

.assembly-item h3 {
  margin: 0 0 0.25rem;
  font-size: 1rem;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #e8f1fb;
  color: var(--accent);
}

.badge.obsolete {
  background: #fde8e8;
  color: #b42318;
}

.bom-item {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.bom-item:last-child {
  border-bottom: none;
}

.bom-item-main {
  flex: 1;
  min-width: 0;
}

.bom-item-price {
  flex-shrink: 0;
  text-align: right;
  font-weight: 600;
  white-space: nowrap;
}

.bom-item-price .bom-qty {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--muted);
}

.bom-item.optional {
  opacity: 0.7;
}

.bom-meta {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.wiring-subtitle {
  font-size: 0.95rem;
  margin: 1rem 0 0.5rem;
}

.wiring-node {
  margin-bottom: 0.75rem;
}

.wiring-node-title {
  font-size: 0.9rem;
  margin-bottom: 0.35rem;
}

.connector-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.connector-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8rem;
  background: #f8fafc;
}

.connector-kind {
  color: var(--muted);
  font-size: 0.72rem;
}

.wiring-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin-top: 0.35rem;
}

.wiring-table th,
.wiring-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.45rem 0.25rem;
  text-align: left;
  vertical-align: top;
}

.wiring-table th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}

.component-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.85rem;
  margin-top: 0.35rem;
}

.component-link {
  display: inline-block;
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
}

.component-link:hover {
  text-decoration: underline;
}

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  margin-bottom: 0.75rem;
}

textarea {
  min-height: 80px;
  resize: vertical;
}

button,
.back-link {
  appearance: none;
  border: none;
  border-radius: 8px;
  padding: 0.75rem 1rem;
  font: inherit;
  cursor: pointer;
}

button.primary {
  background: var(--accent);
  color: white;
  width: 100%;
}

button.primary:hover {
  background: var(--accent-dark);
}

.back-link {
  display: inline-block;
  margin-bottom: 1rem;
  color: var(--accent);
  text-decoration: none;
  background: transparent;
  padding: 0;
}

.build-record {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.build-record:last-child {
  border-bottom: none;
}

.status-tested { color: var(--success); }
.status-failed { color: #b42318; }

.photo-item {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.photo-thumb-btn {
  display: block;
  width: 100%;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: white;
  cursor: zoom-in;
  overflow: hidden;
  aspect-ratio: 4 / 3;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.photo-thumb-btn:hover,
.photo-thumb-btn:focus-visible {
  border-color: var(--accent, #2563eb);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
  outline: none;
}

.photo-thumb-btn img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.photo-caption {
  font-size: 0.875rem;
  line-height: 1.4;
}

#photos-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
}

.photos-hint {
  margin: 0 0 0.75rem;
  font-size: 0.875rem;
}

#wiring-diagram {
  max-width: 900px;
  cursor: zoom-in;
}

.hidden { display: none; }

.image-lightbox {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  color: #1a2332;
  touch-action: none;
  overscroll-behavior: none;
}

.image-lightbox.hidden {
  display: none;
}

.image-lightbox-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: max(0.75rem, env(safe-area-inset-top));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  flex-shrink: 0;
  border-bottom: 1px solid #e5e7eb;
  background: #ffffff;
}

.lightbox-close {
  width: 3rem;
  height: 3rem;
  border: 1px solid #d8dee6;
  border-radius: 8px;
  background: #f4f6f8;
  color: #1a2332;
  font-size: 1.75rem;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.lightbox-close:hover {
  background: #e8edf2;
}

.lightbox-zoom-controls {
  display: flex;
  gap: 0.5rem;
}

.lightbox-zoom-btn {
  width: 3rem;
  height: 3rem;
  border: 1px solid #d8dee6;
  border-radius: 8px;
  background: #f4f6f8;
  color: #1a2332;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.lightbox-zoom-btn:hover {
  background: #e8edf2;
}

.image-lightbox-stage {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  cursor: grab;
  -webkit-user-select: none;
  user-select: none;
  background: #ffffff;
}

.image-lightbox-stage.is-dragging {
  cursor: grabbing;
}

#lightbox-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transform-origin: center center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  pointer-events: none;
}

.image-lightbox-caption {
  margin: 0;
  padding: 0.75rem 1rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  text-align: center;
  font-size: 0.9rem;
  color: #5c6b7a;
  flex-shrink: 0;
  border-top: 1px solid #e5e7eb;
  background: #ffffff;
}

body.lightbox-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  left: 0;
  right: 0;
}

.message {
  padding: 0.75rem;
  border-radius: 8px;
  margin-bottom: 1rem;
}

.message.success {
  background: #e8f7ee;
  color: var(--success);
}

.message.error {
  background: #fde8e8;
  color: #b42318;
}

@media (min-width: 600px) {
  header h1 { font-size: 1.25rem; }
}

@media (min-width: 768px) {
  main,
  header {
    padding: 1.25rem 2rem;
  }

  .assembly-list {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  }
}

@media (min-width: 1024px) {
  .detail-grid {
    flex-direction: row;
    align-items: flex-start;
  }

  .detail-main,
  .detail-side {
    flex: 1 1 50%;
  }

  .detail-side {
    position: sticky;
    top: 5.5rem;
    max-height: calc(100vh - 6rem);
    overflow-y: auto;
  }

  #photos-gallery {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }
}
