:root {
  --bg: #111217;
  --surface: #1a1c23;
  --surface-2: #242733;
  --primary: #5a8dee;
  --text: #e7e9ee;
  --muted: #a5a9b6;
  --accent: #95c4ff;
}

* { box-sizing: border-box; }

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

h1, h2, h3 { margin: 0 0 0.5rem 0; }

/* --- Upload Page --- */
.upload-body { display: grid; place-items: center; min-height: 100vh; padding: 2rem; }
.upload-card { width: min(700px, 100%); background: var(--surface); border: 1px solid #2b2f3a; border-radius: 16px; padding: 1.25rem 1.5rem; box-shadow: 0 8px 30px rgba(0,0,0,0.25); }
.upload-card h1 { font-weight: 700; }
.upload-form { margin-top: 1rem; display: grid; gap: 1rem; }
.field { display: grid; gap: 0.5rem; }
.field > span { color: var(--muted); font-size: 0.95rem; }
input[type="file"], input[type="url"] { width: 100%; padding: 0.75rem 0.9rem; border-radius: 10px; border: 1px solid #2b2f3a; background: var(--surface-2); color: var(--text); }
.actions { margin-top: 0.5rem; }
.btn { border: none; border-radius: 10px; padding: 0.6rem 1rem; font-weight: 600; cursor: pointer; background: #2b2f3a; color: var(--text); }
.btn.primary { background: var(--primary); color: white; }
.btn:active { transform: translateY(1px); }
.flash { background: #382222; border: 1px solid #6b2d2d; border-radius: 10px; padding: 0.5rem 0.75rem; margin-top: 0.5rem; }

/* --- Viewer Layout --- */
.viewer-body .toolbar {
  position: sticky; top: 0; z-index: 10; display: grid; grid-template-columns: auto 1fr auto; align-items: center; justify-content: space-between; gap: 1rem; padding: 0.6rem 1rem; background: var(--surface); border-bottom: 1px solid #2b2f3a;
}
.controls .btn + .btn { margin-left: 0.5rem; }
.viewer { padding: 1rem; }
.wave-wrapper { background: var(--surface); border: 1px solid #2b2f3a; border-radius: 12px; overflow: hidden; padding: 0.5rem; }
.panes { display: grid; grid-template-columns: 1fr; gap: 1rem; margin-top: 1rem; }
.pane { background: var(--surface); border: 1px solid #2b2f3a; border-radius: 12px; padding: 0.75rem; min-height: 140px; }
.placeholder { margin-top: 0.5rem; color: var(--muted); font-style: italic; }
.selection-panel {
  margin: 1rem 0; background: var(--surface); border: 1px solid #2b2f3a; border-radius: 12px; padding: 0.75rem 1rem;
}
.selection-panel .trio { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.selection-panel .label { display: inline-block; min-width: 70px; color: var(--muted); }
.cursor-time { margin-left: 1rem; color: var(--muted); }
.cursor-time strong { color: var(--text); }

/* --- WaveSurfer & Timeline --- */
#timeline {
  position: relative; height: 28px; overflow: visible; z-index: 3; pointer-events: none;
}
#waveform {
  position: relative; z-index: 1; overflow-x: auto; overflow-y: hidden; scrollbar-gutter: stable both-edges; height: 280px;
}
ws-timeline { color: #fff; font-size: 12px; font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
#wave-scrollbar {
  position: relative; height: 14px; margin-top: 6px; background: rgba(255, 255, 255, 0.06); border-radius: 7px;
  overflow: hidden; user-select: none; -webkit-user-select: none; cursor: pointer; z-index: 2;
}
#wave-scrollbar.disabled { opacity: 0.5; cursor: default; }
#wave-scrollbar .thumb {
  position: absolute; top: 3px; height: 8px; left: 0; width: 20%; background: #5a8dee; border-radius: 6px; pointer-events: auto;
}
#timeline > ws-timeline::part(timeline) {
  color: #fff !important;
  font-size: 12px !important;
  font-family: system-ui,-apple-system,Segoe UI,Roboto,sans-serif !important;
  white-space: nowrap;
  overflow: visible;
}
#timeline > ws-timeline::part(tick) {
  border-color: rgba(255,255,255,0.5) !important;
}

/* --- Tooltips --- */
.marker-tooltip {
  position: fixed;
  z-index: 9999;
  background: rgba(20,22,28,0.95);
  color: var(--text);
  border: 1px solid #2b2f3a;
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 14px;
  line-height: 1.2;
  pointer-events: none;
  transform: translate(10px, -10px);
  max-width: 450px;
  display: none;
  white-space: normal;
}
.ws-annotation-marker {
  pointer-events: auto;
}

/* --- Stats & Metadata Panel --- */

/* 1. Main Container */
.stats-panel {
  margin: 1rem 0 2rem;
  background: transparent; /* Changed from surface to transparent, panel handles bg */
  border: none; 
  padding: 0; 
}

.meta-panel {
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid #2b2f3a;
  border-radius: 12px;
  color: var(--text);
  margin-bottom: 1.5rem;
}

/* 2. Header */
.meta-header {
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 1rem;
  margin-bottom: 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.meta-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 12px;
}

.meta-tag {
  background: var(--primary);
  color: #fff;
  font-size: 0.85rem;
  padding: 4px 10px;
  border-radius: 6px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.meta-subtitle {
  font-size: 0.95rem;
  color: var(--muted);
}

/* 3. Three-Column Meta Grid */
.meta-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Changed to 4 columns */
  gap: 24px; 
  width: 100%;
}

@media (max-width: 1400px) {
  .meta-grid { grid-template-columns: repeat(2, 1fr); gap: 16px; }
}

@media (max-width: 768px) {
  .meta-grid { grid-template-columns: 1fr; }
}

.meta-card {
  background: rgba(255,255,255,0.03); 
  border-radius: 10px;
  padding: 1.2rem;
  border: 1px solid rgba(255,255,255,0.06);
  display: flex;
  flex-direction: column;
}

.meta-card-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  margin-bottom: 1rem;
  color: rgba(255,255,255,0.5);
  border-bottom: 1px solid rgba(255,255,255,0.05);
  padding-bottom: 8px;
}

.meta-subgrid {
  display: grid;
  grid-template-columns: 1fr; 
  gap: 10px;
  font-size: 0.95rem;
}

.meta-subgrid > div {
  overflow-wrap: anywhere; 
  line-height: 1.4;
}

.meta-subgrid small {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 3px;
  font-weight: 500;
}

/* 4. Integrated Stats Row (The New Bottom Section) */
.meta-stats-row {
  display: grid;
  /* 4 columns for the 4 retained metrics */
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

@media (max-width: 900px) {
  .meta-stats-row { grid-template-columns: repeat(2, 1fr); }
}

.mini-stat {
  /* Cleaner look: no border, just spacing */
  padding: 0 0.5rem; 
}

/* Add left border to stats except the first one */
.mini-stat + .mini-stat {
  border-left: 1px solid rgba(255,255,255,0.08);
  padding-left: 1.5rem;
}

/* On mobile, remove borders as they stack */
@media (max-width: 900px) {
  .mini-stat + .mini-stat { border-left: none; padding-left: 0.5rem; }
}

.stat-label { 
  color: var(--muted); 
  font-size: 0.8rem; 
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
}

.stat-value { 
  font-size: 1.8rem; 
  font-weight: 700; 
  color: #fff;
  line-height: 1;
}

.stat-sub { 
  color: rgba(255,255,255,0.4); 
  font-size: 0.85rem; 
  margin-top: 6px; 
}

/* --- Tables --- */
.stats-tables {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
  padding: 1.5rem;
  border-top: 1px solid #2b2f3a;
}
.stats-table h3 { margin: 0.25rem 0 0.5rem 0; font-size: 1rem; color: var(--accent); }
.stats-table table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface-2);
  border: 1px solid #2b2f3a;
  border-radius: 10px;
  overflow: hidden;
}
.stats-table thead tr { background: rgba(255,255,255,0.04); }
.stats-table th, .stats-table td {
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid #2b2f3a;
  font-size: 0.95rem;
}
.stats-table tbody tr:hover { background: rgba(255,255,255,0.03); }

/* --- Index/Search Page --- */
.index-wrap {
  width: min(1400px, 95%); /* Increased from 920px */
  margin: 5rem auto;
  padding: 2rem;
  background: var(--surface);
  border: 1px solid #2b2f3a;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.index-hint { color: var(--muted); font-size: .9rem; margin-top: .25rem; }
.index-hr { border: 0; border-top: 1px solid rgba(255,255,255,0.08); margin: 1.5rem 0; }
.input, .select {
  width: 100%;
  padding: 0.9rem 1rem;
  border-radius: 10px;
  border: 1px solid #2b2f3a;
  background-color: var(--surface-2);
  color: var(--text);
  outline: none;
  
  /* CRITICAL: Strips Safari/iOS default glossy styling */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  
  /* Fix for font size consistency */
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.3;
}

/* Custom Dropdown Arrow for Select (since we removed the default one) */
.select {
  cursor: pointer;
  padding-right: 2.5rem; /* Make room for the arrow */
  
  /* SVG Arrow encoded as data URI */
  background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23a5a9b6%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  background-size: 0.7em auto;
}

/* Focus States */
.input:focus, .select:focus {
  border-color: rgba(90, 141, 238, 0.65);
  box-shadow: 0 0 0 3px rgba(90, 141, 238, 0.15);
}

/* Fix for Search Button height alignment in Safari */
#searchBtn {
  /* Safari sometimes calculates button height differently than inputs */
  height: 46px; 
  display: flex;
  align-items: center;
  justify-content: center;
}

.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.col { flex: 1; min-width: 240px; }
.col-sm { width: 200px; min-width: 200px; }
.tableWrap { margin-top: .75rem; overflow: auto; border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; max-height: 520px; }
.trace-table { width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; text-align: center; }
.trace-table thead th { position: sticky; top: 0; z-index: 2; background: var(--surface); border-bottom: 1px solid rgba(255,255,255,0.10); white-space: nowrap; }
.trace-table th, .trace-table td { padding: 10px 12px; vertical-align: middle; }

/* --- Trace Table Column Widths & Layout --- */

/* 1. Start Time (Col 1) */
.trace-table th:nth-child(1),
.trace-table td:nth-child(1) {
  width: 180px;
  white-space: nowrap;
}

/* 2. Duration (Col 2) - Right aligned */
.trace-table th:nth-child(2),
.trace-table td:nth-child(2) {
  width: 110px;
  white-space: nowrap;
  text-align: right;
  padding-right: 20px;
  font-variant-numeric: tabular-nums;
}

/* 3. Trace ID (Col 3) - WIDER and CENTERED */
.trace-table th:nth-child(3),
.trace-table td:nth-child(3) {
  width: 330px;       /* Increased width to stop overlapping */
  max-width: 330px;
  white-space: nowrap;
  text-align: center; /* Centers both the Header and the Hash */
  padding: 0 10px;
}

/* 4. From & To (Col 4, 5) */
.trace-table th:nth-child(4),
.trace-table td:nth-child(4),
.trace-table th:nth-child(5),
.trace-table td:nth-child(5) {
  width: 150px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

/* 5. Flow (Col 6) - Remainder */
.trace-table th:nth-child(6),
.trace-table td:nth-child(6) {
  width: auto;
  text-align: left;
}

/* --- Font Styling --- */

/* Ensure the Header uses the standard font */
.trace-table th:nth-child(3) {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-weight: 700;
}


/* Ensure the Body Cell uses Monospace */
.trace-table td.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !important;
  font-size: 0.85em;
  opacity: 0.9;
  letter-spacing: 0;
}


.clickRow { cursor: pointer; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }

/* --- Loading Overlay --- */
.loading-overlay {
  display:none; position:fixed; inset:0; background:rgba(0,0,0,0.55); z-index:9999; align-items:center; justify-content:center;
}
.loading-card { display:flex; gap:12px; align-items:center; background:var(--surface); border:1px solid #2b2f3a; border-radius:14px; padding:16px 18px; min-width:300px; box-shadow:0 10px 30px rgba(0,0,0,.35); }
.loading-spinner { width:18px; height:18px; border-radius:999px; border:2px solid rgba(255,255,255,0.25); border-top-color:var(--primary); animation:loadingSpin .9s linear infinite; }
.loading-title { font-weight:700; }
.loading-sub { color:var(--muted); font-size:13px; margin-top:2px; }
@keyframes loadingSpin { to { transform:rotate(360deg); } }

#homeBtn { opacity: 0.85; }
#homeBtn:hover { opacity: 1; }

/* --- Compact Node Stats --- */
.node-stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.node-stat-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  padding: 8px 12px; /* Super tight vertical padding */
}

.node-stat-label {
  font-size: 0.75rem;
  color: #a5a9b6;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.node-stat-val {
  font-size: 1.1rem;
  font-weight: 700;
  color: #80cbc4;
}