/* ==========================================================================
   Base
   ========================================================================== */

/* Fallback for Tailwind's hidden class if CDN is delayed or unavailable. */
.hidden {
  display: none !important;
}

body {
  background: #f3f4f6;
  color: #111827;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.45;
}

/* Standard form input styling (selects, date inputs, text fields on filter bars) */
.input {
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  background: #ffffff;
  color: #111827;
}

/* ==========================================================================
   Breadcrumb
   ========================================================================== */

#breadcrumb-list li {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

#breadcrumb-list li:not(:last-child)::after {
  content: "›";
  margin-left: 0.25rem;
  color: #9ca3af;
  font-size: 0.875rem;
}

#breadcrumb-list a {
  color: #6b7280;
  text-decoration: none;
  transition: color 0.1s;
}

#breadcrumb-list a:hover {
  color: #111827;
  text-decoration: underline;
}

#breadcrumb-list li:last-child span {
  color: #111827;
  font-weight: 500;
}

/* ==========================================================================
   Components
   ========================================================================== */

.panel {
  background: #ffffff;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.03);
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.panel-header {
  background: #f3f4f6;
  border-bottom: 1px solid #d1d5db;
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th {
  background: #f3f4f6;
  color: #374151;
  font-weight: 600;
  text-align: left;
}

.table th,
.table td {
  padding: 12px 14px;
  border-bottom: 1px solid #e5e7eb;
  vertical-align: top;
  line-height: 1.4;
}

.table tr[data-clickable="true"] {
  cursor: pointer;
}

.table tr[data-clickable="true"]:hover {
  background: #f9fafb;
}

.sort-indicator {
  margin-left: 6px;
  font-size: 11px;
  color: #6b7280;
}

.btn {
  background: #e5e7eb;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 500;
  color: #111827;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.btn:hover {
  background: #e2e8f0;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled,
button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #111827;
  border-color: #111827;
  color: #ffffff;
}

.btn-primary:hover {
  background: #1f2937;
  border-color: #1f2937;
}

.btn-danger {
  background: #ffffff;
  border-color: #fca5a5;
  color: #b91c1c;
}

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

.btn-danger-solid {
  background: #dc2626;
  border-color: #dc2626;
  color: #ffffff;
}

.btn-danger-solid:hover {
  background: #b91c1c;
  border-color: #b91c1c;
}

.link {
  color: #111827;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
}

.tag {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 999px;
  border: 1px solid #d1d5db;
  font-size: 11px;
  color: #374151;
  background: #f9fafb;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.stat-card {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #ffffff;
}

/* ==========================================================================
   Layout And Overflow
   ========================================================================== */

/* Keep dense admin tables usable on narrow screens. */
#programs-list,
#sessions-list,
#users-list,
#diagnostics-list,
#page-feedback .panel,
#user-detail-sessions-table {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

#programs-list .table {
  min-width: 900px;
}

#sessions-list .table {
  min-width: 1200px;
}

#users-list .table {
  min-width: 1400px;
}

#diagnostics-list .table {
  min-width: 900px;
}

#page-feedback .table {
  min-width: 1000px;
}

#user-detail-sessions-table {
  min-width: 700px;
}

/* Cap vertical growth on detail-page panels so the page doesn't scroll
   endlessly.  The sticky thead rule (.table thead th) keeps headers
   visible inside these scrollable regions. */
#user-detail-sessions-scroll,
#user-detail-parts,
#user-detail-parts-timeline,
#user-detail-onboarding,
#user-detail-modules,
#user-detail-journey,
#user-detail-permissions,
#user-parts-content,
#session-detail-messages,
#session-detail-parts,
#session-detail-wisdom,
#session-detail-summary,
#program-flags-panel,
#program-users-list {
  max-height: 400px;
  overflow-y: auto;
}

/* Full-page list tables and diagnostics get more room since they're primary content */
#session-detail-diagnostics,
#recent-sessions,
#programs-list,
#sessions-list,
#users-list,
#feedback-session-panel > .panel,
#feedback-message-panel > .panel,
#diagnostics-list,
#finances-list {
  max-height: 600px;
  overflow-y: auto;
}

#top-nav-links {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  /* Fade edges to hint at scrollable overflow on narrow screens */
  mask-image: linear-gradient(to right, transparent, black 24px, black calc(100% - 24px), transparent);
  -webkit-mask-image: linear-gradient(to right, transparent, black 24px, black calc(100% - 24px), transparent);
}

/* Remove fade mask when nav is wide enough to not scroll */
@media (min-width: 960px) {
  #top-nav-links {
    mask-image: none;
    -webkit-mask-image: none;
  }
}

#top-nav-links .nav-link {
  flex: 0 0 auto;
  transition: background 0.15s ease, color 0.15s ease;
}

#top-nav-links .nav-link.active {
  background: #111827;
  color: #ffffff;
}

/* Improve keyboard navigation clarity across controls. */
.btn:focus-visible,
.link:focus-visible,
.nav-link:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid #1f2937;
  outline-offset: 2px;
}

.table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

.table tbody tr:nth-child(even) {
  background: #fcfcfd;
}

.page-header {
  margin-bottom: 1.25rem;
}

.page-header h2 {
  letter-spacing: -0.01em;
}

@keyframes skeleton-shimmer {
  0% { background-position: -400px 0; }
  100% { background-position: 400px 0; }
}

.skeleton {
  border-radius: 6px;
  background: linear-gradient(90deg, #f3f4f6 25%, #e5e7eb 37%, #f3f4f6 63%);
  background-size: 800px 100%;
  animation: skeleton-shimmer 1.2s infinite linear;
}

.skeleton-line {
  height: 12px;
  width: 100%;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 768px) {
  .table th,
  .table td {
    padding: 10px 12px;
  }

  .panel {
    border-radius: 10px;
  }

  #main-content main {
    padding: 1rem;
  }

  #main-content h2 {
    font-size: 1.5rem;
    line-height: 1.9rem;
  }
}

/* ==========================================================================
   Diff View
   ========================================================================== */

.diff-line {
  position: relative;
}

.diff-line.diff-added:hover,
.diff-line.diff-removed:hover {
  filter: brightness(0.95);
}

/* Blame tooltip on hover */
.diff-line[title]:hover::after {
  content: attr(title);
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: #1f2937;
  color: #f9fafb;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 10px;
  font-family: system-ui, -apple-system, sans-serif;
  white-space: nowrap;
  z-index: 10;
  pointer-events: none;
}

/* History item selection states */
.history-item {
  transition: all 0.15s ease;
}

/* ==========================================================================
   Prompts Page — Source Selector & Editor
   ========================================================================== */

.source-selector {
  display: flex;
  gap: 4px;
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 3px;
}

.source-option {
  flex: 1;
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 500;
  color: #6b7280;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s ease;
  background: transparent;
}

.source-option:hover {
  color: #374151;
  background: #e5e7eb;
}

.source-option.active {
  background: #ffffff;
  color: #111827;
  border-color: #d1d5db;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  font-weight: 600;
}

.source-option.active[data-source="live"] {
  border-color: #d1d5db;
}

.source-option.active[data-source="team_test"] {
  border-color: #f59e0b;
  background: #fffbeb;
}

.source-option.active[data-source="my_test"] {
  border-color: #8b5cf6;
  background: #f5f3ff;
}

/* Editor border accents per source */
.editor-live {
  border-left: 3px solid #10b981;
  background: #f9fafb;
  color: #6b7280;
  cursor: default;
}

.editor-staging {
  border-left: 3px solid transparent;
}

.editor-team-test {
  border-left: 3px solid #f59e0b;
}

.editor-my-test {
  border-left: 3px solid #8b5cf6;
}

/* Prompt toolbar */
.prompt-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.prompt-toolbar > div {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 6px;
}

.prompt-toolbar .toolbar-sep {
  width: 1px;
  height: 20px;
  background: #d1d5db;
  margin: 0 4px;
}

.btn-icon {
  padding: 6px 8px;
  font-size: 14px;
  line-height: 1;
  min-width: 30px;
  text-align: center;
  background: transparent;
  border-color: transparent;
  color: #6b7280;
}

.btn-icon:hover:not(:disabled) {
  background: #e5e7eb;
  color: #374151;
}

.btn-icon:disabled {
  opacity: 0.3;
}

.prompt-toolbar .spacer {
  flex: 1;
}

.prompt-toolbar .char-info {
  font-size: 11px;
  color: #9ca3af;
  white-space: nowrap;
  padding-left: 10px;
  border-left: 1px solid #e5e7eb;
}

/* Collapsible sections */
.collapsible-section {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
}

.collapsible-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: #f9fafb;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: #374151;
  user-select: none;
  transition: background 0.15s ease;
}

.collapsible-header:hover {
  background: #f3f4f6;
}

.collapsible-header .chevron {
  transition: transform 0.2s ease;
  font-size: 11px;
  color: #9ca3af;
}

.collapsible-header.open .chevron {
  transform: rotate(90deg);
}

.collapsible-body {
  display: none;
  padding: 12px 14px;
  border-top: 1px solid #e5e7eb;
}

.collapsible-body.open {
  display: block;
}

.count-badge {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  background: #e5e7eb;
  color: #374151;
  font-size: 11px;
  font-weight: 600;
  margin-left: 6px;
}

/* Prompt type dropdown */
.prompt-type-select {
  font-size: 16px;
  font-weight: 700;
  padding: 6px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #ffffff;
  color: #111827;
  cursor: pointer;
  min-width: 200px;
}

.prompt-type-select:focus {
  outline: 2px solid #1f2937;
  outline-offset: 2px;
}

/* Toolbar primary actions use .btn-primary (defined in base).
   Secondary actions use the default .btn style.
   Icon buttons use .btn-icon (ghost style). */

/* Diff toggle active state */
.btn-icon.diff-active {
  background: #dbeafe;
  border-color: #3b82f6;
  color: #1d4ed8;
}

/* ==========================================================================
   Inline Diff Backdrop (line numbers integrated into backdrop)
   ========================================================================== */

.diff-backdrop {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 0.875rem;
  line-height: 1.25rem;       /* Must match Tailwind text-sm line-height */
  padding: 8px 12px 8px 60px; /* 48px gutter + 12px text padding */
  white-space: pre-wrap;
  word-break: break-word;
  overflow-y: hidden;
  pointer-events: none;
  color: transparent;
  border: 1px solid transparent;
  border-radius: 0.375rem;
  box-sizing: border-box;
  z-index: 0;
}

/* Gutter background column (pseudo-element) */
.diff-backdrop::before {
  content: '';
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 48px;
  background: #fafafa;
  border-right: 1px solid #e5e7eb;
  border-radius: 0.375rem 0 0 0.375rem;
  pointer-events: none;
}

#prompt-editor-content.diff-active {
  background: transparent !important;
  position: relative;
  z-index: 1;
  caret-color: #111827;
}

/* When diff-active, shift textarea right for gutter */
#prompt-edit-mode:has(.diff-active) {
  margin-left: 48px;
}

/* Hide the old separate gutter div (line numbers are now in the backdrop) */
.diff-gutter-col { display: none !important; }

/* Each logical line in the backdrop */
.diff-backdrop .diff-line {
  display: block;
  position: relative;
  min-height: 1.25rem; /* Prevent empty lines from collapsing */
}

/* Line number positioned in the gutter area */
.diff-backdrop .line-num {
  position: absolute;
  top: 0;
  left: -47px;
  width: 34px;
  text-align: right;
  color: #9ca3af;
  font-size: 11px;
  line-height: 1.25rem;
  user-select: none;
}
.diff-backdrop .line-num.num-added { color: #16a34a; font-weight: 600; }

/* Removed-line marker (takes no vertical space) */
.diff-backdrop .diff-removed-marker {
  display: block;
  height: 0;
  position: relative;
  overflow: visible;
}
.diff-backdrop .diff-removed-marker::before {
  content: '';
  position: absolute;
  left: -52px;
  width: 40px;
  height: 2px;
  background: #ef4444;
  top: -1px;
}

/* Line-level backdrop colors */
.diff-backdrop .line-added { background: rgba(34, 197, 94, 0.10); }
.diff-backdrop .line-unchanged { background: transparent; }
.diff-backdrop .word-added { background: rgba(34, 197, 94, 0.25); border-radius: 2px; }

/* Diff overlay — Cursor-style full diff with gutter */
#prompt-diff-overlay .diff-line {
  display: flex;
  line-height: 1.5;
}

#prompt-diff-overlay .diff-gutter {
  flex: 0 0 48px;
  text-align: right;
  padding-right: 8px;
  font-size: 11px;
  color: #9ca3af;
  user-select: none;
  background: #fafafa;
  border-right: 1px solid #e5e7eb;
}

#prompt-diff-overlay .diff-content {
  flex: 1;
  padding-left: 12px;
  min-width: 0;
}

#prompt-diff-overlay .diff-added {
  background: rgba(34, 197, 94, 0.10);
  color: #111827;
}

#prompt-diff-overlay .diff-added .diff-gutter { color: #16a34a; }

#prompt-diff-overlay .diff-removed {
  background: rgba(239, 68, 68, 0.08);
  color: #111827;
}

#prompt-diff-overlay .diff-removed .diff-gutter { color: #ef4444; }

#prompt-diff-overlay .diff-unchanged {
  color: #6b7280;
}

#prompt-diff-overlay .word-added { background: rgba(34, 197, 94, 0.25); border-radius: 2px; }
#prompt-diff-overlay .word-removed { background: rgba(239, 68, 68, 0.20); border-radius: 2px; }

/* Saved/unsaved indicator */
#prompt-editor-status.status-saved {
  color: #16a34a;
}

#prompt-editor-status.status-unsaved {
  color: #d97706;
}

#prompt-editor-status.status-success {
  color: #16a34a;
  font-weight: 600;
}

#prompt-editor-status.status-error {
  color: #dc2626;
  font-weight: 600;
}

#prompt-editor-status.status-progress {
  color: #6b7280;
}

/* Prompt construction indicator */
.prompt-construction {
  padding: 10px 12px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.prompt-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.prompt-row + .prompt-row {
  border-top: 1px solid #f3f4f6;
}

.prompt-row-label {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 11px;
  color: #9ca3af;
  min-width: 90px;
  flex-shrink: 0;
}

.prompt-row-pills {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.prompt-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid #d1d5db;
  background: #ffffff;
  color: #374151;
  transition: all 0.15s ease;
}

.prompt-pill:hover {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.prompt-pill.pill-active {
  background: #dbeafe;
  border-color: #3b82f6;
  color: #1d4ed8;
  font-weight: 600;
}

.prompt-pill.pill-runtime {
  background: #f9fafb;
  border-color: #e5e7eb;
  color: #9ca3af;
  font-style: italic;
}

.prompt-pill.pill-empty {
  opacity: 0.4;
}

.prompt-arrow {
  color: #d1d5db;
  font-size: 14px;
  font-weight: 300;
}

/* ==========================================================================
   Finances Page
   ========================================================================== */

/* Status badges */
.sub-status-active { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.sub-status-trial { background: #dbeafe; color: #1e40af; border: 1px solid #bfdbfe; }
.sub-status-paused { background: #fef9c3; color: #854d0e; border: 1px solid #fde68a; }
.sub-status-cancelled { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.sub-status-free { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }

/* Billing model badges */
.sub-billing-monthly { background: #f3e8ff; color: #6b21a8; border: 1px solid #e9d5ff; }
.sub-billing-annual { background: #e0e7ff; color: #3730a3; border: 1px solid #c7d2fe; }
.sub-billing-pay-as-you-go { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; }
.sub-billing-one-time { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.sub-billing-free { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }

.sub-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}

/* Category filter active state */
.finance-filter.active {
  background: #111827;
  border-color: #111827;
  color: #ffffff;
}

/* Table horizontal scroll for mobile */
#finances-list {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

#finances-list .table {
  min-width: 900px;
}

/* Staging source selector accent */
.source-option.active[data-source="staging"] {
  border-color: #10b981;
  background: #ecfdf5;
}

/* ==========================================================================
   User Detail Page
   ========================================================================== */

/* Tab navigation */
.user-tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

/* Copyable ID chip */
.copy-id-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 12px;
  color: #6b7280;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.copy-id-btn:hover {
  background: #e5e7eb;
  border-color: #d1d5db;
}

.copy-id-btn .copy-icon {
  font-size: 11px;
  opacity: 0.5;
}

/* Module progress bar */
.module-progress-bar {
  height: 6px;
  border-radius: 3px;
  background: #e5e7eb;
  overflow: hidden;
}

.module-progress-fill {
  height: 100%;
  border-radius: 3px;
  background: #16a34a;
  transition: width 0.3s ease;
}

/* Toast notification */
.admin-toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: #ffffff;
  background: #111827;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 100;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.admin-toast.toast-visible {
  opacity: 1;
  transform: translateY(0);
}

.admin-toast.toast-error {
  background: #b91c1c;
}

.admin-toast.toast-success {
  background: #16a34a;
}

/* Form row alignment for permissions */
.perm-form-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.perm-form-row:last-child {
  border-bottom: none;
}

.perm-form-label {
  flex: 0 0 140px;
  font-size: 13px;
  color: #6b7280;
}

.perm-form-control {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Feedback message panel overflow */
#feedback-message-panel .panel {
  overflow-x: auto;
}

/* Finance category badges */
.sub-cat-ai-api { background: #ede9fe; color: #5b21b6; }
.sub-cat-infrastructure { background: #dbeafe; color: #1e40af; }
.sub-cat-monitoring { background: #fef3c7; color: #92400e; }
.sub-cat-project-management { background: #d1fae5; color: #065f46; }
.sub-cat-business { background: #fce7f3; color: #9d174d; }
.sub-cat-dev-tools { background: #e5e7eb; color: #374151; }

/* Diagnostic log filter chips */
.filter-chip {
  padding: 2px 8px;
  border-radius: 9999px;
  font-size: 11px;
  font-weight: 500;
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  color: #9ca3af;
  cursor: pointer;
  transition: background 0.1s, color 0.1s, border-color 0.1s;
}

.filter-chip:hover {
  background: #f3f4f6;
}

/* Level + Category chips: bold when active (= include matching lines). */
.filter-chip.active {
  background: #eef2ff;
  color: #4338ca;
  border-color: #c7d2fe;
}

/*
  "Hide noise" chips invert: active = hide. The default look is the same
  as inactive Level/Category chips (off), so we restyle when they're on.
*/
.filter-chip[data-filter-hide].active {
  background: #fef2f2;
  color: #b91c1c;
  border-color: #fecaca;
}
