// ── Layout ────────────────────────────────────────────────────────────────────

:host {
  display: block;
  padding: 1rem;
}

// ── Card base (used by trace-tree, trace-link-form, trace-gaps-section) ──────
.gb-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 1rem;
}

// ── View Tab Bar ──────────────────────────────────────────────────────────────
.trace-view-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid #e8e8e8;
  margin-bottom: 16px;
}

.trace-view-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  color: #666;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;

  &:hover { color: #1a73e8; }

  &--active {
    color: #1a73e8;
    border-bottom-color: #1a73e8;
  }
}

.trace-gap-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  background: #fce4ec;
  color: #b71c1c;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 700;
}

.trace-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
  align-items: start;
  margin-top: 16px;
}

// ── Loading States ────────────────────────────────────────────────────────────

.trace-loading-inline {
  font-size: 13px;
  color: #888;
  margin-inline-start: 12px;
}

.trace-loading-block {
  padding: 32px 16px;
  color: #888;
  font-size: 14px;
}

// ── Screen Tree (Left Panel) ──────────────────────────────────────────────────

.trace-tree {
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

.trace-tree-heading {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
}

.trace-module {
  margin-bottom: 12px;
}

.trace-module-name {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #999;
  padding: 4px 8px;
}

.trace-node {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 6px 8px;
  border: none;
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
  text-align: start;
  font-size: 13px;
  line-height: 1.4;
  transition: background 0.15s;

  &:hover {
    background: #f0f4ff;
  }

  &--active {
    background: #e8f0fe;
    color: #1a73e8;
    font-weight: 500;
  }

  &--gap {
    color: #bbb;
  }
}

.trace-indicator {
  font-size: 10px;
  flex-shrink: 0;
  color: #bbb;

  &--covered {
    color: #2e7d32;
  }
}

.trace-node-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.trace-node-count {
  flex-shrink: 0;
  font-size: 11px;
  background: #e8f0fe;
  color: #1a73e8;
  border-radius: 10px;
  padding: 1px 6px;
  min-width: 18px;
  text-align: center;
}

// ── Detail Panel (Right) ──────────────────────────────────────────────────────

.trace-detail {
  min-width: 0;
}

.trace-placeholder {
  padding: 48px 24px;
  text-align: center;
  color: #999;
  font-size: 14px;
}

.trace-detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #eee;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.trace-detail-title-group {
  min-width: 0;
}

.trace-screen-title {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 500;
}

.trace-route {
  font-size: 12px;
  color: #888;
  background: #f5f5f5;
  padding: 2px 6px;
  border-radius: 3px;
}

// ── Stats Row ─────────────────────────────────────────────────────────────────

.trace-stats {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.trace-stat {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  background: #f5f5f5;
  color: #555;
  white-space: nowrap;

  &--p1 {
    background: #fce4ec;
    color: #b71c1c;
  }

  &--p2 {
    background: #fff8e1;
    color: #e65100;
  }

  &--pass {
    background: #e8f5e9;
    color: #2e7d32;
  }

  &--fail {
    background: #fce4ec;
    color: #b71c1c;
  }

  &--date {
    background: transparent;
    color: #888;
    font-size: 11px;
  }
}

// ── Test Cases ────────────────────────────────────────────────────────────────

.trace-cases {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 16px;
}

.trace-case {
  border: 1px solid #e8e8e8;
  border-radius: 6px;
  overflow: hidden;

  &--expanded {
    border-color: #c5d9f7;
  }
}

.trace-case-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  background: #fafafa;
  transition: background 0.15s;

  &:hover {
    background: #f0f4ff;
  }

  .trace-case--expanded & {
    background: #edf3ff;
  }
}

.trace-chevron {
  font-size: 12px;
  color: #888;
  flex-shrink: 0;
  width: 12px;
}

.trace-tc-id {
  font-size: 11px;
  color: #666;
  background: #eee;
  padding: 1px 5px;
  border-radius: 3px;
  flex-shrink: 0;
  white-space: nowrap;
}

.trace-tc-title {
  flex: 1;
  font-size: 14px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.trace-tc-badges {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

// ── Priority / Status Badges ──────────────────────────────────────────────────

.trace-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  background: #eee;
  color: #555;

  &--p1 {
    background: #fce4ec;
    color: #c62828;
  }

  &--p2 {
    background: #fff8e1;
    color: #e65100;
  }

  &--p3 {
    background: #f3e5f5;
    color: #6a1b9a;
  }

  &--pass {
    background: #e8f5e9;
    color: #2e7d32;
  }

  &--fail {
    background: #fce4ec;
    color: #b71c1c;
  }
}

// ── Gherkin Steps ─────────────────────────────────────────────────────────────

.trace-steps {
  padding: 12px 16px 12px 34px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #fff;
  border-top: 1px solid #e8e8e8;
}

.trace-step {
  display: flex;
  gap: 8px;
  font-size: 13px;
  line-height: 1.5;
}

.trace-step-keyword {
  flex-shrink: 0;
  min-width: 40px;
  font-weight: 600;
  font-style: italic;
  color: #999;

  &--given {
    color: #1565c0;
  }

  &--when {
    color: #e65100;
  }

  &--then {
    color: #2e7d32;
  }
}

.trace-step-text {
  color: #333;
}

.trace-no-steps {
  font-size: 12px;
  color: #bbb;
}

// ── Add Link Button ───────────────────────────────────────────────────────────

.trace-add-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  border: 1px dashed #1a73e8;
  border-radius: 4px;
  background: transparent;
  color: #1a73e8;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;

  &:hover {
    background: #e8f0fe;
  }
}

// ── Link Form ─────────────────────────────────────────────────────────────────

.trace-link-form {
  margin-top: 16px;
}

.trace-link-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid #e0e0e0;
  margin-bottom: 16px;
}

.trace-tab {
  padding: 8px 20px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  font-size: 14px;
  cursor: pointer;
  color: #666;
  transition: all 0.15s;
  margin-bottom: -1px;

  &:hover {
    color: #1a73e8;
  }

  &--active {
    color: #1a73e8;
    border-bottom-color: #1a73e8;
    font-weight: 500;
  }
}

.trace-link-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 480px;
}

.trace-input {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s;

  &:focus {
    outline: none;
    border-color: #1a73e8;
  }

  &[readonly] {
    background: #f5f5f5;
    cursor: default;
  }
}

.trace-form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #eee;
}

.trace-btn-primary {
  padding: 8px 20px;
  border: none;
  border-radius: 4px;
  background: #1a73e8;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;

  &:hover:not(:disabled) {
    background: #1557b0;
  }

  &:disabled {
    opacity: 0.5;
    cursor: default;
  }
}

.trace-btn-secondary {
  padding: 8px 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: transparent;
  font-size: 14px;
  cursor: pointer;
  color: #555;
  transition: background 0.15s;

  &:hover {
    background: #f5f5f5;
  }
}

// ── Unlink Controls ───────────────────────────────────────────────────────────

.trace-chevron-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 12px;
  color: #888;
  flex-shrink: 0;
  width: 20px;
  padding: 0;
  line-height: 1;
}

.trace-unlink-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  color: #bbb;
  padding: 2px 4px;
  border-radius: 3px;
  line-height: 1;
  transition: color 0.15s, background 0.15s;

  &:hover {
    color: #b71c1c;
    background: #fce4ec;
  }
}

.trace-unlink-confirm {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  font-size: 12px;
  color: #555;
}

.trace-btn-danger-sm {
  padding: 2px 8px;
  border: none;
  border-radius: 3px;
  background: #b71c1c;
  color: #fff;
  font-size: 12px;
  cursor: pointer;

  &:hover {
    background: #8b1313;
  }
}

.trace-btn-ghost-sm {
  padding: 2px 8px;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: transparent;
  font-size: 12px;
  cursor: pointer;
  color: #555;

  &:hover {
    background: #f5f5f5;
  }
}

// ── Gaps View ─────────────────────────────────────────────────────────────────

.trace-gaps-view {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 16px;
}

.trace-gaps-section {
  overflow: hidden;
}

.trace-gaps-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 600;
  color: #333;
}

.trace-gaps-desc {
  margin: 0 0 12px;
  font-size: 13px;
  color: #888;
}

.trace-gaps-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;

  th {
    text-align: start;
    padding: 8px 10px;
    background: #f5f5f5;
    font-size: 12px;
    font-weight: 600;
    color: #666;
    border-bottom: 1px solid #e0e0e0;
    white-space: nowrap;
  }

  td {
    padding: 8px 10px;
    border-bottom: 1px solid #f0f0f0;
    color: #333;
  }

  tbody tr:last-child td {
    border-bottom: none;
  }

  tbody tr:hover td {
    background: #fafafa;
  }
}

.trace-gap-title {
  font-weight: 500;
}

.trace-gap-module {
  color: #888;
  font-size: 12px;
}

.trace-gap-count {
  text-align: center;
  font-weight: 600;
  color: #2e7d32;

  &--zero {
    color: #b71c1c;
  }
}

// ── Misc ──────────────────────────────────────────────────────────────────────

.trace-empty {
  color: #999;
  font-size: 14px;
  font-style: italic;
  padding: 8px 0;
}

// ── RTL Support ───────────────────────────────────────────────────────────────

[dir="rtl"] {
  .trace-layout {
    direction: rtl;
  }

  .trace-detail-header {
    flex-direction: row-reverse;
  }

  .trace-stats {
    flex-direction: row-reverse;
  }

  .trace-case-row {
    flex-direction: row-reverse;
  }

  .trace-tc-badges {
    flex-direction: row-reverse;
  }

  .trace-step {
    flex-direction: row-reverse;
    text-align: end;
  }

  .trace-form-actions {
    flex-direction: row-reverse;
  }

  .trace-link-tabs {
    flex-direction: row-reverse;
  }

  .trace-node {
    flex-direction: row-reverse;
    text-align: end;
  }

  .trace-steps {
    padding-inline-start: 16px;
    padding-inline-end: 34px;
  }

  .trace-unlink-confirm {
    flex-direction: row-reverse;
  }

  .trace-gaps-table {
    th, td {
      text-align: end;
    }
  }

  .trace-gaps-heading {
    flex-direction: row-reverse;
  }
}
