/* Ishikawa A3 — Design System */
:root {
  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #0f172a;
  --color-text-muted: #64748b;
  --color-primary: #1a6b9c;
  --color-primary-hover: #155a7a;
  --color-primary-light: #e8f2fa;
  --color-accent: #2d8bc9;
  --color-success: #059669;
  --color-success-light: #d1fae5;
  --color-warning: #d97706;
  --color-danger: #dc2626;
  --color-fish-spine: #1a6b9c;
  --color-fish-head: #1a6b9c;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 8px 30px rgba(15, 23, 42, 0.12);
  --font-sans: "DM Sans", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", monospace;
  --header-height: 64px;
}

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  background: linear-gradient(180deg, #ffffff 0%, #f5f9fd 100%);
  border-bottom: 1px solid #d4e4f0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(26, 107, 156, 0.08);
}

.app-header.compact {
  --header-height: 56px;
}

.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 1.5rem;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-start {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  min-width: 0;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 0.625rem;
  text-decoration: none;
  color: var(--color-text);
  flex-shrink: 0;
}

.brand-mark:hover {
  color: var(--color-primary);
}

.brand-mark-icon {
  width: 40px;
  height: 24px;
  color: var(--color-primary);
  flex-shrink: 0;
}

.brand-mark-name {
  font-size: 1.0625rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
  white-space: nowrap;
}

.brand-icon {
  width: 40px;
  height: 24px;
  color: var(--color-primary);
  flex-shrink: 0;
}

.brand h1 {
  font-size: 1.0625rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.subtitle {
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.nav-center {
  flex: 1;
  text-align: center;
  min-width: 0;
}

.back-link {
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.8125rem;
  font-weight: 500;
  white-space: nowrap;
  transition: color 0.15s;
  padding-right: 0.875rem;
  border-right: 1px solid #d4e4f0;
}

.back-link:hover {
  color: var(--color-primary);
}

.diagram-meta {
  min-width: 0;
}

.diagram-meta h2 {
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.diagram-meta .meta-tags {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 0.125rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: 0.8125rem;
}

.btn-primary {
  background: var(--color-primary);
  color: white;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
  box-shadow: 0 4px 12px rgba(26, 107, 156, 0.25);
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.7);
  color: var(--color-primary);
  border: 1px solid #c5d9e8;
}

.btn-ghost:hover {
  background: var(--color-primary-light);
  color: var(--color-primary-hover);
  border-color: #a8c8dc;
}

.btn-danger {
  background: transparent;
  color: var(--color-danger);
  border: 1px solid transparent;
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
}

.btn-danger:hover {
  background: #fef2f2;
}

/* Container */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  flex: 1;
  width: 100%;
}

/* Hero */
.hero-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  padding: 2rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  align-items: center;
  margin-bottom: 2.5rem;
  box-shadow: var(--shadow-sm);
}

.hero-content h2 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.75rem;
}

.hero-content p {
  color: var(--color-text-muted);
  margin-bottom: 1.25rem;
}

.feature-list {
  list-style: none;
  display: grid;
  gap: 0.5rem;
}

.feature-list li {
  font-size: 0.875rem;
  padding-left: 1.25rem;
  position: relative;
}

.feature-list li::before {
  content: "✓";
  position: absolute;
  left: 0;
  color: var(--color-success);
  font-weight: 600;
}

.hero-visual {
  display: flex;
  justify-content: center;
  align-items: center;
}

.hero-fish {
  width: 100%;
  max-width: 320px;
  height: auto;
}

/* Diagrams grid */
.section-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.section-header h2 {
  font-size: 1.125rem;
  font-weight: 600;
}

.badge {
  background: var(--color-primary-light);
  color: var(--color-primary);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
}

.diagrams-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
}

.diagram-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 1.25rem;
  text-decoration: none;
  color: inherit;
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.diagram-card:hover {
  border-color: var(--color-accent);
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.diagram-card h3 {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
}

.diagram-card .problem-preview {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.diagram-card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
  padding-top: 0.5rem;
  border-top: 1px solid var(--color-border);
}

.status-pill {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  background: var(--color-bg);
  color: var(--color-text-muted);
}

.status-pill.en_analisis {
  background: #fef3c7;
  color: var(--color-warning);
}

.status-pill.completado {
  background: var(--color-success-light);
  color: var(--color-success);
}

.a3-ref {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 3rem;
  color: var(--color-text-muted);
}

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

/* Modal */
.modal {
  border: none;
  border-radius: var(--radius-lg);
  padding: 0;
  max-width: 520px;
  width: calc(100% - 2rem);
  box-shadow: var(--shadow-lg);
}

.modal::backdrop {
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
}

.modal-form {
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.modal-header h3 {
  font-size: 1.0625rem;
  font-weight: 600;
}

.btn-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--color-text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem;
}

.modal-body {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--color-border);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.field span {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-text);
}

.field input,
.field textarea,
.field select {
  font-family: inherit;
  font-size: 0.875rem;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  transition: border-color 0.15s;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.checkbox-field {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.checkbox-field input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--color-primary);
}

/* Diagram workspace */
.diagram-page {
  overflow: hidden;
}

.diagram-workspace {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
  transition: grid-template-columns 0.2s ease;
}

.diagram-workspace.sidebar-collapsed {
  grid-template-columns: 1fr;
}

.diagram-workspace.sidebar-collapsed .sidebar {
  display: none;
}

.sidebar {
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: 1.25rem;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

#categories-panel {
  min-width: 0;
}

.sidebar h3 {
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
}

.category-panel-item {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  min-width: 0;
}

.category-panel-item.active {
  border-color: var(--color-accent);
  background: var(--color-primary-light);
}

.category-panel-item h4 {
  font-size: 0.8125rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.375rem;
  min-width: 0;
}

.category-panel-item .side-tag {
  font-size: 0.625rem;
  text-transform: uppercase;
  color: var(--color-text-muted);
  background: var(--color-bg);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  flex-shrink: 0;
}

.cause-list-item {
  font-size: 0.75rem;
  padding: 0.375rem 0;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.375rem;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
  min-width: 0;
}

.cause-list-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.35;
}

.cause-list-item:hover,
.cause-list-item.selected {
  background: var(--color-primary-light);
}

.cause-list-item.has-detail .cause-detail-dot {
  color: var(--color-accent);
  font-size: 0.5rem;
  margin-left: 0.25rem;
  vertical-align: middle;
}

.cause-hit {
  fill: rgba(255, 255, 255, 0.9);
  stroke: #cbd5e1;
  stroke-width: 1;
}

.cause-hit.root-cause {
  fill: #d1fae5;
  stroke: #059669;
  stroke-width: 1.5;
}

.cause-node.has-detail .cause-hit {
  stroke: var(--color-accent);
}

/* Popover descripción causa */
.cause-popover {
  position: fixed;
  z-index: 200;
  width: min(320px, calc(100vw - 2rem));
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 1rem 1.125rem 1rem;
}

.cause-popover[hidden] {
  display: none;
}

.cause-popover-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--color-text-muted);
  cursor: pointer;
  line-height: 1;
}

.cause-popover-label {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin: 0 0 0.25rem;
}

.cause-popover-label:not(:first-of-type) {
  margin-top: 0.75rem;
}

#cause-popover-title {
  font-size: 0.9375rem;
  font-weight: 600;
  margin: 0;
  line-height: 1.35;
  padding-right: 1.25rem;
}

.cause-popover-body {
  font-size: 0.8125rem;
  line-height: 1.55;
  color: var(--color-text);
  white-space: pre-wrap;
}

.cause-popover-body.empty {
  color: var(--color-text-muted);
  font-style: italic;
}

.cause-popover-badge {
  display: inline-block;
  margin-top: 0.75rem;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--color-success);
  background: var(--color-success-light);
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
}

.cause-popover-footer {
  margin-top: 0.875rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-border);
}

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

.cause-list-item.root-cause {
  color: var(--color-success);
  font-weight: 500;
}

.cause-list-item .cause-actions {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

.cause-list-item button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.6875rem;
  color: var(--color-text-muted);
  padding: 0.125rem;
}

.cause-list-item button:hover,
.btn-icon:hover {
  color: var(--color-primary);
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.6875rem;
  color: var(--color-text-muted);
  padding: 0.125rem;
}

.muted-sm {
  font-size: 0.75rem;
  color: #64748b;
}

.inline-form {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-border);
}

.add-category-details summary {
  list-style: none;
  cursor: pointer;
}

.add-category-details summary::-webkit-details-marker {
  display: none;
}

#diagram-workspace-swap[hidden] {
  display: none;
}

.add-cause-inline {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.375rem;
  margin-top: 0.625rem;
  padding-top: 0.625rem;
  border-top: 1px dashed var(--color-border);
}

.add-cause-inline textarea {
  grid-column: 1 / -1;
  width: 100%;
  font-family: inherit;
  font-size: 0.75rem;
  padding: 0.375rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  resize: vertical;
  min-height: 2.25rem;
  max-height: 4.5rem;
}

.add-cause-inline textarea:focus {
  outline: none;
  border-color: var(--color-accent);
}

.add-cause-inline input[type="text"] {
  min-width: 0;
  width: 100%;
  font-family: inherit;
  font-size: 0.75rem;
  padding: 0.375rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

.add-cause-inline input[type="text"]:focus {
  outline: none;
  border-color: var(--color-accent);
}

.add-cause-submit {
  align-self: center;
  flex-shrink: 0;
  padding-inline: 0.625rem;
}

.field-hint {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  margin: -0.25rem 0 0.25rem;
}

.sidebar-actions {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
}

/* Fishbone canvas — estilo Ishikawa profesional */
.fishbone-canvas-wrapper {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(135deg, #e8f2fa 0%, #f5f9fd 50%, #e4eef8 100%);
}

.fishbone-bg {
  pointer-events: none;
}

.fish-tail-fill {
  pointer-events: none;
}

.fish-tail-stroke {
  fill: none;
}

.fish-spine {
  stroke-linecap: round;
}

.fish-head-solid {
  filter: url(#shadow);
}

.fish-head-label {
  font-family: var(--font-sans);
}

.fish-head-sublabel {
  font-family: var(--font-sans);
}

.bone-line {
  stroke-linecap: round;
}

.category-label {
  font-family: var(--font-sans);
}

.cause-sub-line,
.cause-tick {
  stroke-linecap: round;
}

.cause-text {
  font-family: var(--font-sans);
  pointer-events: none;
}

.cause-text.root-cause {
  font-weight: 600;
}

.cause-node {
  cursor: pointer;
}

.canvas-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.625rem 1rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.toolbar-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
}

.toolbar-actions {
  display: flex;
  gap: 0.375rem;
}

.fishbone-canvas {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fishbone-svg {
  width: 100%;
  min-width: 800px;
  height: auto;
  max-height: calc(100vh - var(--header-height) - 60px);
}

/* SVG fishbone — legacy classes kept for compatibility */
.fish-tail {
  fill: none;
  stroke: #1a6b9c;
}

.fish-spine {
  stroke: #1a6b9c;
  stroke-width: 7;
  stroke-linecap: round;
}

.fish-head-bg,
.fish-head-fill {
  display: none;
}

.fish-problem-text,
.fish-problem-sub,
.fish-eye {
  display: none;
}

.bone-line-top,
.bone-line-bottom {
  stroke: #1a6b9c;
}

.cause-bg {
  display: none;
}

.tag-pill {
  display: inline-block;
  font-size: 0.6875rem;
  font-weight: 500;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--color-primary-light);
  color: var(--color-primary);
}

.tag-pill.muted {
  background: var(--color-bg);
  color: var(--color-text-muted);
}

/* Footer */
.app-footer {
  text-align: center;
  padding: 1.5rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  border-top: 1px solid var(--color-border);
}

/* Responsive */
@media (max-width: 900px) {
  .hero-card {
    grid-template-columns: 1fr;
  }

  .hero-visual {
    order: -1;
  }

  .diagram-workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: calc(100vh - var(--header-height));
  }

  .sidebar {
    max-height: 240px;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
  }

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

  .header-inner {
    flex-wrap: wrap;
    height: auto;
    padding: 0.75rem 1rem;
    min-height: var(--header-height);
  }

  .diagram-meta {
    order: 3;
    flex-basis: 100%;
    text-align: left;
    padding-top: 0.5rem;
  }

  .nav-center {
    order: 3;
    flex-basis: 100%;
    text-align: left;
    padding-top: 0.5rem;
  }

  .header-start {
    flex: 1;
  }

  #fishbone-svg {
    min-width: 600px;
  }
}

.header-start .subtitle {
  margin: 0;
  padding-left: 0.875rem;
  border-left: 1px solid #d4e4f0;
}

@media (max-width: 640px) {
  .header-start .subtitle {
    display: none;
  }

  .brand-mark-name {
    display: none;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 1rem;
  }

  .diagrams-grid {
    grid-template-columns: 1fr;
  }
}
