.message-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0rem;
  width: 100%;
  min-height: 0;
  flex: 1 1 auto;
  position: relative;
  /* Isolate stacking context so sidebar z-indices don't compete with
     Portals (Command Palette, modals) that live at the body level. */
  isolation: isolate;
}

.message-layout--with-timeline {
  grid-template-columns: minmax(0, 1fr) 64px;
}

.message-layout--with-timeline::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 64px;
  width: 1px;
  background-color: var(--border-muted);
  pointer-events: none;
}

@media (max-width: 720px) {
  .message-layout--with-timeline {
    grid-template-columns: minmax(0, 1fr) 40px;
  }

  .message-layout--with-timeline::after {
    right: 40px;
  }
}

.message-stream-shell {
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
 }


 .message-stream-shell .message-stream {
  flex: 1 1 auto;
  min-height: 0;
 }
 
 .message-timeline-sidebar {
  width: 64px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 100;
 }


@media (max-width: 720px) {
  .message-timeline-sidebar {
    width: 40px;
  }
}

.message-timeline {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.25rem;
  overflow-y: auto;
  overflow-x: visible;
  border-radius: 8px;
  background-color: var(--surface-base);
  box-shadow: var(--panel-shadow);
}

.message-timeline--selection-active {
  padding-bottom: 4rem;
}

.message-timeline::-webkit-scrollbar {
  width: 5px;
}

.message-timeline::-webkit-scrollbar-thumb {
  background-color: var(--border-muted);
  border-radius: 9999px;
}

.message-timeline-segment {
  width: 100%;
  min-height: 1.5rem;
  border-radius: 0;
  border: 1px solid var(--border-base);
  background-color: var(--surface-secondary);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-primary);
  transition: transform 0.15s ease, background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}

.message-timeline-segment[data-delete-hover="true"]::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--status-error-bg);
  border-radius: 0;
  pointer-events: none;
  z-index: 2;
}

/* Ensure delete hover is visible even on the active segment styling. */
.message-timeline-segment[data-delete-hover="true"],
.message-timeline-segment[data-delete-hover="true"]:hover,
.message-timeline-segment[data-delete-hover="true"]:focus-visible,
.message-timeline-segment-active[data-delete-hover="true"],
.message-timeline-segment-active[data-delete-hover="true"]:hover,
.message-timeline-segment-active[data-delete-hover="true"]:focus-visible {
  /* Let the ::before overlay provide the highlight (matches stream behavior). */
  background-color: transparent !important;
  border-color: transparent !important;
  box-shadow: none !important;
  transform: none !important;
}

.message-timeline-label {
  position: relative;
  z-index: 3;
}

.message-timeline-segment.message-timeline-segment-hidden {
  display: none !important;
}

.message-timeline-segment-active {
  border-color: color-mix(in oklab, var(--timeline-segment-active-bg) 92%, var(--timeline-segment-active-text));
  background-color: var(--timeline-segment-active-bg);
  color: var(--timeline-segment-active-text);
  font-weight: 700;
  box-shadow: var(--timeline-segment-active-ring);
}

.message-timeline-segment:hover,
.message-timeline-segment:focus-visible {
  background-color: var(--surface-hover);
  color: var(--accent-primary);
  outline: none;
  transform: translateY(-1px);
}

.message-timeline-segment-active,
.message-timeline-segment-active:hover,
.message-timeline-segment-active:focus-visible {
  background-color: var(--timeline-segment-active-bg);
  color: var(--timeline-segment-active-text);
  transform: none;
  box-shadow: var(--timeline-segment-active-ring);
}

.message-timeline-segment:focus-visible {
  box-shadow: 0 0 0 2px var(--surface-base), 0 0 0 4px var(--accent-primary);
}

.message-timeline-user {
  border-color: var(--message-user-border);
  background-color: var(--message-user-bg);
}

.message-timeline-assistant {
  border-color: var(--message-assistant-border);
  background-color: var(--message-assistant-bg);
}

.message-timeline-tool {
  border-color: var(--message-tool-border);
  background-color: var(--surface-secondary);
}

.message-timeline-segment.message-timeline-segment-permission {
  border-color: var(--session-status-permission-fg);
  background-color: var(--session-status-permission-bg);
  color: var(--session-status-permission-fg);
}

.message-timeline-segment.message-timeline-segment-permission:hover,
.message-timeline-segment.message-timeline-segment-permission:focus-visible {
  background-color: var(--session-status-permission-bg);
  color: var(--session-status-permission-fg);
  border-color: var(--session-status-permission-fg);
  transform: translateY(-1px);
}

.message-timeline-segment-active.message-timeline-segment-permission,
.message-timeline-segment-active.message-timeline-segment-permission:hover,
.message-timeline-segment-active.message-timeline-segment-permission:focus-visible {
  background-color: var(--session-status-permission-bg) !important;
  border-color: var(--session-status-permission-fg) !important;
  color: var(--session-status-permission-fg) !important;
  transform: none;
  box-shadow: var(--timeline-segment-active-ring);
}

.message-timeline-compaction-auto {
  border-color: var(--session-status-compacting-fg);
  background-color: var(--surface-secondary);
}

.message-timeline-compaction-manual {
  border-color: var(--message-user-border);
  background-color: var(--message-user-bg);
}

.message-timeline-segment-active {
  background-color: var(--timeline-segment-active-bg) !important;
  border-color: color-mix(in oklab, var(--timeline-segment-active-bg) 92%, var(--timeline-segment-active-text)) !important;
  color: var(--timeline-segment-active-text) !important;
  font-weight: 700;
  box-shadow: var(--timeline-segment-active-ring);
}

.message-timeline-label {
  pointer-events: none;
}

.message-timeline-label-full {
  display: none;
}

.message-timeline-label-short {
  display: inline-flex;
  line-height: 1;
  align-items: center;
  justify-content: center;
}

.message-timeline-icon {
  width: 1rem;
  height: 1rem;
}

.message-timeline-tooltip {
  position: fixed;
  z-index: 1000;
  /* Allow interacting with the preview (copy/delete/etc). */
  pointer-events: auto;
}

.message-preview {
  width: 360px;
  max-height: 420px;
  overflow: auto;
  border-radius: 8px;
  border: 1px solid var(--border-base);
  background-color: var(--surface-base);
  box-shadow: var(--panel-shadow-strong);
  padding: 0.75rem;
}

.message-preview .message-item-base {
  font-size: 0.85rem;
}

/* --- Selection & Histogram Ribs --- */

.message-timeline-segment-selected {
  border-color: var(--accent-primary) !important;
  background-color: color-mix(in oklab, var(--accent-primary) 25%, var(--surface-base)) !important;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--accent-primary) 50%, transparent) inset !important;
  color: var(--accent-primary) !important;
}

/* When a whole message is selected for deletion (via stream checkbox),
   reflect that on all timeline segments for that message. */
.message-timeline-segment-delete-selected {
  border-color: color-mix(in oklab, var(--status-error) 55%, transparent) !important;
  background-color: color-mix(in oklab, var(--status-error) 18%, var(--surface-base)) !important;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--status-error) 35%, transparent) inset !important;
  color: var(--status-error) !important;
}

.message-timeline-segment-delete-selected:hover,
.message-timeline-segment-delete-selected:focus-visible {
  background-color: color-mix(in oklab, var(--status-error) 24%, var(--surface-base)) !important;
  color: var(--status-error) !important;
  transform: none;
}

.message-timeline-segment-selected:hover,
.message-timeline-segment-selected:focus-visible {
  background-color: color-mix(in oklab, var(--accent-primary) 35%, var(--surface-base)) !important;
  color: var(--accent-primary) !important;
  transform: none;
}

/* --- Group indicators: tools belong to the same message as their assistant --- */

/* Tool segments that are part of a group get a left accent border. */
.message-timeline-group-child {
  border-left: 3px solid color-mix(in oklab, var(--accent-primary) 35%, transparent);
}

/* The assistant "parent" at the bottom of a tool group gets the same border. */
.message-timeline-group-parent {
  border-left: 3px solid color-mix(in oklab, var(--accent-primary) 35%, transparent);
}

/* Extra spacing before the first tool in a group to separate from the
   preceding user/assistant badge. */
.message-timeline-group-start {
  margin-top: 0.35rem;
}

/* Subtle extra spacing after the group parent (assistant) to separate
   from the next user badge below. Uses adjacent sibling targeting. */
.message-timeline-group-parent + .message-timeline-user,
.message-timeline-group-parent + .message-timeline-compaction {
  margin-top: 0.35rem;
}

.message-timeline-container {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.message-timeline-xray-overlay {
  position: absolute;
  inset: 0;
  /* Extend the overlay box into the stream so ribs are not relying on
     overflow-visible behavior (which is brittle around scroll containers). */
  --xray-overhang: calc(var(--max-rib-width, 50vw) + 84px);
  left: calc(-1 * var(--xray-overhang));
  width: calc(100% + var(--xray-overhang));
  overflow: hidden;
  padding: 0.25rem;
  pointer-events: none;
  /* Above the scroll container background; still non-interactive. */
  z-index: 2;
  --xray-scroll-y: 0px;
}

.message-timeline-xray-overlay-inner {
  position: absolute;
  inset: 0;
  transform: translateY(var(--xray-scroll-y));
  will-change: transform;
}

.message-timeline-xray-rib {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  transform: translate(-100%, -50%);
}

.message-timeline-xray-token-label {
  position: absolute;
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-right: 4px;
  height: 1.5rem;
  display: flex;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--text-primary);
  background: var(--surface-base);
  padding: 1px 5px;
  border: 1px solid var(--border-base);
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
}

.message-timeline-relative-bar {
  height: 5px;
  width: calc(var(--segment-weight) * var(--max-rib-width, 50vw));
  background-color: color-mix(
    in srgb,
    var(--status-success) calc(100% - var(--segment-weight) * 100%),
    var(--status-error) calc(var(--segment-weight) * 100%)
  );
  border-radius: 3px 0 0 3px;
  transition: width 0.3s ease, background-color 0.3s ease;
  box-shadow: -2px 0 4px rgba(0, 0, 0, 0.25);
}

.message-timeline-absolute-bar {
  height: 3px;
  width: calc(var(--segment-weight) * var(--max-rib-width, 50vw));
  background-color: var(--text-muted);
  border-radius: 2px 0 0 2px;
  transition: width 0.3s ease;
  opacity: 0.5;
  position: relative;
}

.message-timeline-absolute-bar-overflow {
  opacity: 0.8;
}

.message-timeline-absolute-bar-overflow::before {
  content: "";
  position: absolute;
  left: -1px;
  top: -3px;
  bottom: -3px;
  width: 3px;
  border-radius: 2px;
  background: var(--status-error);
  box-shadow: 0 0 6px 2px var(--status-error);
}
