:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --panel: #ffffff;
  --panel-soft: #f8fbff;
  --line: #d8e1eb;
  --line-strong: #c3cfdd;
  --text: #17212f;
  --subtle: #5b6778;
  --brand: #2563eb;
  --brand-soft: #dbeafe;
  --brand-deep: #1d4ed8;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --success: #166534;
  --success-soft: #ecfdf3;
  --warning: #9a6700;
  --warning-soft: #fff7ed;
  --shadow: 0 18px 40px rgba(15, 23, 42, 0.08);
  --shadow-soft: 0 10px 22px rgba(15, 23, 42, 0.05);
  --radius: 18px;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, 0.08), transparent 30%),
    radial-gradient(circle at top right, rgba(14, 165, 233, 0.06), transparent 24%),
    linear-gradient(180deg, #f7f9fc 0%, #eef3f9 100%);
}

a {
  color: inherit;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
input:not([type="checkbox"]):not([type="radio"]),
select,
textarea,
.nav-link,
.mobile-link {
  min-height: 44px;
}

input:not([type="checkbox"]):not([type="radio"]),
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

textarea {
  min-height: 148px;
  resize: vertical;
  line-height: 1.55;
}

input:not([type="checkbox"]):not([type="radio"]):focus,
select:focus,
textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

input:not([type="checkbox"]):not([type="radio"]):disabled,
select:disabled,
textarea:disabled,
button:disabled {
  opacity: 0.62;
  cursor: not-allowed;
}

input[type="checkbox"],
input[type="radio"] {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--brand);
  flex: 0 0 auto;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 28px 18px;
  border-right: 1px solid var(--line);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.9) 0%, rgba(244, 248, 255, 0.96) 100%);
  box-shadow: inset -1px 0 0 rgba(215, 225, 235, 0.95);
}

.brand {
  display: grid;
  gap: 8px;
  margin-bottom: 22px;
  padding: 6px 2px 16px;
  border-bottom: 1px solid rgba(215, 225, 235, 0.9);
}

.brand-kicker {
  font-size: 12px;
  letter-spacing: 0.22em;
  color: var(--subtle);
  text-transform: uppercase;
}

.brand-title {
  margin: 0;
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand-note {
  margin: 0;
  color: var(--subtle);
  line-height: 1.55;
  font-size: 14px;
}

.nav-group {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}

.nav-link,
.mobile-link {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-decoration: none;
  border-radius: 14px;
  border: 1px solid transparent;
  color: var(--text);
  padding: 0 16px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

.nav-link:hover,
.mobile-link:hover {
  background: rgba(255, 255, 255, 0.86);
  border-color: rgba(195, 207, 221, 0.9);
  box-shadow: var(--shadow-soft);
}

.nav-link.is-active,
.mobile-link.is-active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
  box-shadow: 0 10px 22px rgba(37, 99, 235, 0.24);
}

.page-main {
  padding: 28px;
  display: grid;
  gap: 18px;
  width: 100%;
  min-width: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  padding: 6px 2px 2px;
}

.eyebrow {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--subtle);
  text-transform: uppercase;
}

.page-title {
  margin: 0;
  font-size: 32px;
  line-height: 1.15;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.page-lead {
  margin: 8px 0 0;
  color: var(--subtle);
  line-height: 1.6;
  max-width: 680px;
  font-size: 15px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  backdrop-filter: blur(8px);
}

.panel-head {
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  background: linear-gradient(180deg, rgba(248, 251, 255, 0.96) 0%, rgba(255, 255, 255, 0.94) 100%);
}

.panel-title {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.panel-subtitle {
  margin: 6px 0 0;
  color: var(--subtle);
  font-size: 14px;
  line-height: 1.55;
}

.panel-body {
  padding: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(252, 253, 255, 0.96) 100%);
}

.page-grid {
  display: grid;
  grid-template-columns: minmax(0, 420px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
  width: 100%;
  min-width: 0;
}

.page-stack,
.stack {
  display: grid;
  gap: 18px;
  min-width: 0;
}

.history-layout {
  display: grid;
  grid-template-columns: minmax(0, 360px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.field-full {
  grid-column: 1 / -1;
}

.field-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--subtle);
  letter-spacing: 0.01em;
}

.field-help {
  font-size: 12px;
  line-height: 1.5;
  color: var(--subtle);
}

.button-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.button-row-tight {
  gap: 8px;
}

.button {
  border: 0;
  border-radius: 14px;
  padding: 0 16px;
  cursor: pointer;
  font-weight: 800;
  transition: transform 0.12s ease, opacity 0.12s ease, background 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button-primary {
  background: var(--brand);
  color: #fff;
  box-shadow: 0 10px 18px rgba(37, 99, 235, 0.18);
}

.button-primary:hover {
  background: var(--brand-deep);
}

.button-secondary {
  background: #e9eef6;
  color: var(--text);
  border: 1px solid transparent;
}

.button-ghost {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
}

.button-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #fecaca;
}

.button-secondary:hover,
.button-ghost:hover,
.button-danger:hover {
  box-shadow: var(--shadow-soft);
}

.status {
  padding: 12px 14px;
  border-radius: 14px;
  font-weight: 600;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.status-info {
  background: #eef4ff;
  color: #3758a8;
}

.status-success {
  background: var(--success-soft);
  color: var(--success);
}

.status-warning {
  background: var(--warning-soft);
  color: var(--warning);
}

.status-error {
  background: var(--danger-soft);
  color: var(--danger);
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: 16px;
  padding: 24px 18px;
  text-align: center;
  color: var(--subtle);
  background: linear-gradient(180deg, #f9fbff 0%, #f4f8ff 100%);
  min-height: 148px;
  display: grid;
  place-items: center;
  gap: 8px;
}

.empty-state::before {
  content: "";
  width: 42px;
  height: 4px;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.22);
}

.empty-state-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.empty-state-note {
  max-width: 280px;
  font-size: 12px;
  line-height: 1.6;
}

.chips {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: #1d4ed8;
  font-weight: 700;
  font-size: 12px;
}

.hint-banner {
  padding: 12px 14px;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(248, 251, 255, 0.95) 0%, rgba(242, 247, 255, 0.95) 100%);
  border: 1px solid var(--line);
  color: var(--subtle);
  line-height: 1.55;
}

.chain-banner {
  background: #eff6ff;
  border: 1px solid #c7ddff;
  color: #1d4ed8;
}

.image-grid,
.asset-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.image-card,
.asset-card,
.record-card,
.detail-card {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #fff;
  overflow: hidden;
  box-shadow: 0 10px 22px rgba(15, 23, 42, 0.04);
}

.image-box,
.asset-preview,
.preview-body {
  background: linear-gradient(45deg, #f3f4f6 25%, #e5e7eb 25%, #e5e7eb 50%, #f3f4f6 50%, #f3f4f6 75%, #e5e7eb 75%, #e5e7eb 100%);
  background-size: 18px 18px;
}

.image-box,
.asset-preview {
  border: 0;
  padding: 0;
  width: 100%;
  aspect-ratio: 1 / 1;
  cursor: pointer;
}

.image-box img,
.asset-preview img,
.preview-body img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-body,
.asset-body,
.record-body,
.detail-body {
  padding: 14px;
  display: grid;
  gap: 10px;
}

.image-title,
.asset-name {
  font-size: 14px;
  font-weight: 800;
  line-height: 1.5;
  word-break: break-all;
}

.image-note,
.asset-meta,
.record-note,
.record-meta {
  font-size: 12px;
  line-height: 1.55;
  color: var(--subtle);
}

.pick-row {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--subtle);
  font-size: 13px;
  cursor: pointer;
}

.dropzone {
  border: 2px dashed #bfd5ff;
  border-radius: 16px;
  padding: 16px;
  background: #f8fbff;
  text-align: center;
  color: #35538f;
  font-weight: 700;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.dropzone.is-dragover {
  border-color: var(--brand);
  background: #eef4ff;
}

.history-toolbar {
  display: grid;
  gap: 14px;
}

.filters {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  gap: 12px;
}

.record-list {
  display: grid;
  gap: 12px;
}

.record-card {
  cursor: pointer;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
}

.record-card:hover {
  transform: translateY(-1px);
  box-shadow: 0 14px 28px rgba(15, 23, 42, 0.08);
}

.record-card.is-active {
  border-color: var(--brand);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}

.record-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.family-card-layout {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.family-card-copy {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.family-thumb,
.family-thumb-placeholder {
  width: 120px;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.family-thumb {
  padding: 0;
  cursor: pointer;
  background: linear-gradient(135deg, #f4f7fb 0%, #e7eef8 100%);
}

.family-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.family-thumb-placeholder {
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, rgba(37, 99, 235, 0.08) 0%, rgba(37, 99, 235, 0.02) 100%);
  color: var(--subtle);
  font-size: 14px;
  font-weight: 800;
}

.record-prompt {
  font-size: 14px;
  line-height: 1.6;
}

.family-card .record-prompt {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.selection-toolbar {
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 251, 255, 0.98) 100%);
}

.family-toolbar,
.selection-toolbar-row,
.lineage-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
}

.family-toolbar-actions,
.lineage-actions {
  margin-left: auto;
}

.detail-card-graph {
  overflow: hidden;
}

.detail-body-graph {
  gap: 14px;
}

.lineage-viewport {
  position: relative;
  min-height: 420px;
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(248, 251, 255, 0.95) 0%, rgba(255, 255, 255, 0.98) 100%);
}

.lineage-viewport::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(rgba(216, 225, 235, 0.7) 1px, transparent 1px),
    linear-gradient(90deg, rgba(216, 225, 235, 0.7) 1px, transparent 1px);
  background-size: 48px 48px;
  opacity: 0.28;
  pointer-events: none;
}

.lineage-graph {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 420px;
  display: block;
  touch-action: none;
  cursor: grab;
}

.lineage-graph.is-panning {
  cursor: grabbing;
}

.lineage-edge {
  fill: none;
  stroke: rgba(91, 103, 120, 0.34);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.lineage-edge.is-mainline {
  stroke: var(--brand);
  stroke-width: 6;
}

.lineage-edge.is-branch {
  opacity: 0.54;
}

.lineage-node {
  cursor: pointer;
  outline: none;
  transition: opacity 0.15s ease;
}

.lineage-node.is-branch {
  opacity: 0.64;
}

.lineage-node.is-selected,
.lineage-node:hover {
  opacity: 1;
}

.lineage-node-frame,
.lineage-node-placeholder,
.lineage-node-overlay {
  transition: fill 0.15s ease, stroke 0.15s ease, opacity 0.15s ease, filter 0.15s ease;
}

.lineage-node-frame {
  fill: rgba(255, 255, 255, 0.9);
  stroke: var(--line-strong);
  stroke-width: 2;
}

.lineage-node.is-mainline .lineage-node-frame,
.lineage-node:hover .lineage-node-frame {
  stroke: var(--brand);
}

.lineage-node.is-selected .lineage-node-frame {
  stroke: var(--brand-deep);
  stroke-width: 4;
  filter: drop-shadow(0 14px 18px rgba(37, 99, 235, 0.18));
}

.lineage-node-overlay {
  fill: rgba(255, 255, 255, 0.12);
}

.lineage-node.is-branch .lineage-node-overlay {
  fill: rgba(255, 255, 255, 0.3);
}

.lineage-node.is-selected .lineage-node-overlay {
  fill: rgba(255, 255, 255, 0.02);
}

.lineage-node-placeholder {
  fill: var(--panel-soft);
}

.lineage-node-placeholder-text {
  fill: var(--subtle);
  font-size: 24px;
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: middle;
}

.lineage-node-badge {
  fill: var(--text);
  stroke: rgba(255, 255, 255, 0.96);
  stroke-width: 3;
}

.lineage-node.is-mainline .lineage-node-badge {
  fill: var(--brand);
}

.lineage-node.is-selected .lineage-node-badge {
  fill: var(--brand-deep);
}

.lineage-node-badge-text {
  fill: #fff;
  font-size: 12px;
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: middle;
}

.lineage-node-leaf {
  fill: #fff;
  stroke: var(--brand);
  stroke-width: 3;
}

.preview-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.48);
  z-index: 60;
}

.preview-modal.is-open {
  display: flex;
}

.preview-dialog {
  width: min(960px, 100%);
  max-height: calc(100vh - 48px);
  background: #fff;
  border-radius: 18px;
  overflow: hidden;
}

.preview-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.preview-title {
  font-weight: 800;
  font-size: 16px;
}

.preview-body {
  padding: 20px;
  overflow: auto;
  max-height: calc(100vh - 132px);
}

.preview-body img {
  max-width: 100%;
  max-height: calc(100vh - 172px);
  object-fit: contain;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.14);
}

.mobile-nav {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: none;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(215, 225, 235, 0.95);
  border-radius: 22px;
  backdrop-filter: blur(8px);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.12);
  z-index: 50;
}

.mobile-link {
  background: #f8fbff;
  border: 1px solid transparent;
  border-radius: 16px;
  font-size: 14px;
  justify-content: center;
}

@media (max-width: 1180px) {
  .page-grid,
  .history-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .page-main {
    padding: 16px 14px 108px;
    gap: 16px;
  }

  .page-header {
    padding: 0;
  }

  .page-header > div {
    width: 100%;
    padding: 14px 14px 4px;
    border-radius: 18px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.9) 0%, rgba(248, 251, 255, 0.84) 100%);
    border: 1px solid rgba(215, 225, 235, 0.92);
    box-shadow: var(--shadow-soft);
  }

  .mobile-nav {
    display: grid;
  }

  .page-header {
    gap: 10px;
  }

  .page-title {
    font-size: 26px;
  }

  .page-lead {
    margin-top: 6px;
    font-size: 14px;
    line-height: 1.55;
    max-width: none;
  }

  .panel {
    border-radius: 16px;
  }

  .panel-head {
    padding: 14px 14px 12px;
  }

  .panel-body {
    padding: 14px;
  }

  .button-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: stretch;
  }

  .button-row > .button:first-child {
    grid-column: 1 / -1;
  }

  .button-row > .button {
    width: 100%;
    justify-content: center;
  }

  .button-row-tight {
    grid-template-columns: 1fr 1fr;
  }

  .selection-toolbar {
    display: grid;
    gap: 10px;
  }

  .selection-toolbar .button {
    width: 100%;
  }

  .family-toolbar,
  .selection-toolbar-row,
  .lineage-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .family-toolbar-actions,
  .lineage-actions {
    margin-left: 0;
  }

  .family-card-layout {
    grid-template-columns: 96px minmax(0, 1fr);
    gap: 12px;
  }

  .family-thumb,
  .family-thumb-placeholder {
    width: 96px;
    border-radius: 16px;
  }

  .lineage-viewport,
  .lineage-graph {
    min-height: 340px;
    height: 340px;
  }
}

@media (max-width: 760px) {
  .app-shell,
  .page-grid,
  .history-layout,
  .page-stack,
  .panel,
  .record-list,
  .detail-card,
  .record-card {
    width: 100%;
    min-width: 0;
  }

  .form-grid,
  .image-grid,
  .asset-grid,
  .filters {
    grid-template-columns: 1fr;
  }

  .page-main {
    gap: 12px;
    padding-bottom: 132px;
  }

  .eyebrow {
    margin-bottom: 4px;
    font-size: 11px;
    letter-spacing: 0.16em;
  }

  .page-title {
    font-size: 24px;
  }

  .panel-head,
  .panel-body {
    padding: 12px;
  }

  .panel-subtitle,
  .field-help,
  .page-lead {
    font-size: 13px;
  }

  .button-row,
  .button-row-tight {
    grid-template-columns: 1fr;
  }

  .button-row > .button:first-child {
    grid-column: auto;
  }

  .record-head {
    flex-direction: column;
    align-items: stretch;
  }

  .family-card-layout {
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 10px;
    align-items: start;
  }

  .family-thumb,
  .family-thumb-placeholder {
    width: 88px;
    border-radius: 14px;
  }

  .family-card .record-prompt {
    -webkit-line-clamp: 2;
  }

  .empty-state {
    padding: 18px 14px;
  }

  .image-body,
  .asset-body,
  .record-body,
  .detail-body {
    padding: 12px;
  }

  .mobile-nav {
    left: 10px;
    right: 10px;
    bottom: 10px;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    gap: 6px;
  }

  .mobile-link {
    min-height: 42px;
    font-size: 13px;
  }

  .lineage-viewport,
  .lineage-graph {
    min-height: 300px;
    height: 300px;
  }
}
