* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: #111827;
  background: #f8fafc;
}

.app { display: grid; grid-template-rows: auto 1fr auto; min-height: 100%; }
.app__header, .app__footer { padding: 12px 16px; background: #ffffff; border-bottom: 1px solid #e5e7eb; }
.app__footer { border-top: 1px solid #e5e7eb; border-bottom: none; display: flex; justify-content: space-between; align-items: center; }

.controls { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.controls input[type="url"] { flex: 1; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; }
.controls button { padding: 8px 12px; border-radius: 6px; border: 1px solid #111827; background: #111827; color: white; cursor: pointer; }
.controls button:hover { background: #374151; border-color: #374151; }

.controls-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.controls-row button { padding: 6px 10px; border-radius: 6px; border: 1px solid #111827; background: #111827; color: white; cursor: pointer; }
.controls-row button:hover { background: #374151; border-color: #374151; }
.controls-row details { padding: 6px 8px; border: 1px solid #e5e7eb; border-radius: 8px; background: #ffffff; }
.controls-row details[open] { background: #f9fafb; }
.controls-row summary { cursor: pointer; font-weight: 500; }
.controls-row #pyCode { margin-top: 8px; }

.hints { display: flex; gap: 12px; color: #6b7280; font-size: 12px; margin-top: 6px; flex-wrap: wrap; }

.content { display: grid; grid-template-columns: auto 1fr; gap: 0; padding: 16px; position: relative; }
.sidebar-container { 
  position: relative;
  height: 100%;
}
.sidebar { 
  background: white; 
  border: 1px solid #e5e7eb; 
  border-radius: 12px; 
  padding: 12px; 
  overflow: auto;
  width: 280px;
  height: 100%;
  transition: transform 0.3s ease, opacity 0.3s ease;
  transform: translateX(0);
  opacity: 1;
  box-sizing: border-box;
}

.sidebar.collapsed {
  position: absolute;
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

.sidebar-toggle {
  position: absolute;
  right: -32px;
  top: 0;
  width: 32px;
  height: 40px;
  background: white;
  border: 1px solid #e5e7eb;
  border-left: none;
  border-radius: 0 12px 12px 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: transform 0.3s ease, background-color 0.2s ease;
}

.sidebar-toggle:hover {
  background-color: #f3f4f6;
}

.sidebar-toggle svg {
  transition: transform 0.3s ease;
}

.sidebar.collapsed + .sidebar-toggle svg {
  transform: rotate(180deg);
}
.sidebar__placeholder { padding: 8px; }
.tree { list-style: none; padding-left: 0; margin: 0; font-size: 14px; }
.tree li { margin: 2px 0; }
.tree a { color: #111827; text-decoration: none; padding: 2px 4px; border-radius: 6px; display: inline-block; }
.tree a:hover { background: #f3f4f6; }
.tree .active { background: #e0f2fe; border: 1px solid #93c5fd; }
.tree .badge { margin-right: 4px; }
.stage { display: grid; place-items: top; padding: 24px;}
.stage.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; place-items: initial; padding: 16px; }
.stage.two-col .slide { width: 100%; }
.slide {
  width: min(900px, 92vw);
  min-height: 320px;
  height: auto;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.06);
  padding: 20px 24px;
  outline: none;
}
.slide:focus { box-shadow: 0 0 0 3px #93c5fd, 0 10px 20px rgba(0,0,0,0.06); }

.placeholder { color: #6b7280; }

.breadcrumb { color: #6b7280; font-size: 12px; margin-bottom: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-title { font-weight: 600; font-size: 20px; margin: 6px 0 14px; }
.meta { display: grid; grid-template-columns: max-content 1fr; gap: 6px 12px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 13px; }
.meta .label { color: #6b7280; }
.meta .value { color: #111827; overflow-wrap: anywhere; }

.section { margin-top: 16px; }
.section h3 { margin: 0 0 8px; font-size: 14px; color: #374151; }
.codeblock { background: #f3f4f6; border: 1px solid #e5e7eb; border-radius: 8px; padding: 10px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 12px; overflow: auto; }
details summary { cursor: pointer; user-select: none; }
details summary::-webkit-details-marker { margin-right: 6px; }

/* Side-by-side layout for array Chunks and Attributes */
.section-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.section-col details { background: #ffffff; border: 1px solid #e5e7eb; border-radius: 8px; padding: 8px; }
.section-col details[open] { background: #f9fafb; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; background: #eef2ff; color: #3730a3; font-size: 12px; border: 1px solid #c7d2fe; }

.navbar { display: flex; gap: 6px; align-items: center; font-size: 12px; color: #6b7280; margin-top: 12px; }
.navbar .dot { width: 6px; height: 6px; border-radius: 999px; background: #9ca3af; }

/* Cards layout for dataset and aggregated attributes */
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 8px; }
.card { background: #ffffff; border: 1px solid #e5e7eb; border-radius: 12px; padding: 12px; }

.error { color: #b91c1c; background: #fee2e2; border: 1px solid #fecaca; padding: 8px 10px; border-radius: 8px; font-size: 13px; }

.small { font-size: 12px; color: #6b7280; }

/* Chunk visualization */
.chunkviz { display: inline-block; margin-left: 8px; vertical-align: middle; }
.chunkviz-1d { display: inline-grid; grid-auto-flow: column; gap: 2px; }
.chunkviz-2d { display: grid; gap: 2px; }
.chunkviz-2d .chunk-row { display: grid; grid-auto-flow: column; gap: 2px; align-items: center; }
.chunk-cell { width: 8px; height: 8px; background: #dbeafe; border: 1px solid #93c5fd; border-radius: 2px; }
.varname { font-weight: 600; margin-right: 6px; }
/* Xarray-like variable line enhancements */
.varline { 
  padding: 6px 12px; 
  line-height: 1.6;
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
  flex-wrap: wrap;
}

/* Ensure alternating colors work with the container structure */
.var-container:nth-child(odd) .varline { background-color: #f8f9fa; }
.var-container:nth-child(even) .varline { background-color: #f1f3f5; }
.var-container .varline:hover { background-color: #e9ecef; }

/* Combined variable header text */
.var-header-text {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  justify-content: flex-start;
  flex-wrap: nowrap;
}

.var-header-text > *:not(.var-actions) {
  flex-shrink: 0;
}

.var-header-text .var-actions {
  margin-left: auto;
  flex-shrink: 0;
  display: flex;
  gap: 4px;
}

.var-name {
  font-weight: 500;
  color: #111827;
}

.var-dims {
  color: #4b5563;
  font-family: 'Fira Code', 'Roboto Mono', monospace;
  font-size: 0.9em;
  opacity: 0.9;
}

/* Style for data type badges */
.varline .type-badge {
  font-family: 'Fira Code', 'Roboto Mono', monospace;
  background: #e9ecef;
  color: #495057;
  white-space: nowrap;
  margin-right: 6px;
  flex-shrink: 0;
  font-size: 0.8em;
  padding: 1px 4px;
  border-radius: 3px;
  white-space: nowrap;
  border: 1px solid #dee2e6;
  padding: 0 5px;
  border-radius: 3px;
  font-size: 11px;
  margin-left: 4px;
}

/* Variable actions (chunks, attributes) - now moved into var-header-text */
.var-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}


/* Time array button styles */
.time-array-container {
  padding: 4px 0 0 0;
  margin-left: -4px;
  background-color: inherit;
  border-radius: 0 0 4px 4px;
}

.btn-time-array {
  background-color: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 12px;
  color: #4b5563;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: 'Fira Code', 'Roboto Mono', monospace;
}

.btn-time-array:hover {
  background-color: #e9ecef;
  border-color: #cbd5e0;
}

.btn-time-array:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.time-array-values {
  margin-top: 6px;
  padding: 6px 8px;
  background-color: #f1f5f9;
  border-radius: 4px;
  font-size: 12px;
  font-family: 'Fira Code', 'Roboto Mono', monospace;
  color: #334155;
}

.time-array-values div {
  margin: 2px 0;
}

/* Adjust for dark mode if needed */
@media (prefers-color-scheme: dark) {
  .btn-time-array {
    background-color: #2d3748;
    border-color: #4a5568;
    color: #e2e8f0;
  }
  
  .btn-time-array:hover {
    background-color: #4a5568;
    border-color: #718096;
  }
  
  .time-array-values {
    background-color: #2d3748;
    color: #e2e8f0;
  }
}

/* Container for icon buttons */
.btn-icon {
  display: inline-flex;
  gap: 4px; /* Space between buttons */
  align-items: center;
  margin-left: auto;
}

/* Individual icon button */
.btn-icon-single {
  background: none;
  border: none;
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
  color: #6b7280;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin: 0; /* Reset any margins */
}

.btn-icon-single:hover {
  background: #e9ecef;
  color: #1a1a1a;
}

.btn-icon-single svg {
  width: 14px;
  height: 14px;
  opacity: 0.8;
}

/* Active state */
.btn-icon-single.active {
  background-color: #e9ecef;
  color: #111827;
}

/* Container for variable line and its details */
.var-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-bottom: 4px;
  position: relative; /* For proper positioning context */
}

/* Style for the variable line flex direction column*/
.varline {
  flex-wrap: wrap;
  display: flex;
  padding: 8px 10px;
  border-radius: 4px;
  font-family: 'Fira Code', 'Roboto Mono', monospace;
  width: 100%;
  box-sizing: border-box;
  gap: 6px;
  align-items: flex-start; 
  min-height: 40px; /* Ensure consistent height */
}

/* Ensure var-info stays consistently positioned */
.var-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 0; /* Allow it to shrink if needed */
}

/* Keep the header text properly aligned */
.var-header-text {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 0; /* Allow text truncation if needed */
  flex: 1;
}

/* Ensure buttons and actions stay on the right */
.var-actions {
  display: flex;
  gap: 4px;
  width: 100%;
  flex-shrink: 0; /* Prevent shrinking */
  justify-content: flex-start; /* Align content to the left */
  padding-left: 0; /* Remove any left padding */
  margin-left: 0; /* Remove any left margin */
  margin-top: 4px; /* Add some space above the actions */
}

.var-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  width: 100%;
  gap: 10px;
}

.var-info {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0; /* Allows text truncation */
}

/* Removed old varname styles as they're now part of var-header-text */

/* Style for expanded attribute/chunk details */
.var-details {
  width: 100%;
  background: #f8f9fa;
  margin: 0;
  padding: 8px 10px 8px 10px; /* Reduced left padding for better alignment */
  box-sizing: border-box;
  border-left: 2px solid #e5e7eb;
  border-right: 2px solid #e5e7eb;
  border-bottom: 2px solid #e5e7eb;
  border-radius: 0 0 4px 4px;
  display: none;
  margin-top: -4px; /* Pull up to connect with the row above */
  text-align: left;
  text-indent: 0;
}

/* Show details when not hidden */
.var-details:not([hidden]) {
  display: block;
}

/* Style for details content */
.var-details-content {
  display: block;
  width: 100%;
  text-align: left;
  padding-left: 0;
  margin-left: 0;
}

/* Adjust for even/odd rows */
.var-container:nth-child(odd) .varline {
  background-color: #f8f9fa;
}

.var-container:nth-child(even) .varline {
  background-color: #f1f3f5;
}

.var-container:nth-child(odd) .var-details {
  background-color: #f8f9fa;
}

.var-container:nth-child(even) .var-details {
  background-color: #f1f3f5;
}

/* Adjust the codeblock to be full width */
.codeblock {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* Icons (inline SVG) */
.icon { 
  width: 16px; 
  height: 16px; 
  vertical-align: middle; 
  fill: currentColor;
  flex-shrink: 0;
  margin-right: 6px;
}

/* Section headers with icons */
.section h3 {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 8px 0;
  font-size: 14px;
  font-weight: 600;
  color: #1a1a1a;
}

/* Badge colors */
.badge[data-type="coord"] { background-color: #d0ebff; color: #1864ab; border-color: #a5d8ff; }
.badge[data-type="data"] { background-color: #e6fcf5; color: #087f5b; border-color: #96f2d7; }
.badge[data-type="group"] { background-color: #f3f0ff; color: #5f3dc4; border-color: #d0bfff; }
.icon.dim { fill: #6b7280; }
.icon.coord { fill: #2563eb; }
.icon.data { fill: #7c3aed; }
.icon.group { fill: #10b981; }
.icon.attr { fill: #f59e0b; }

/* Xarray-like summary banner */
.xr-summary { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 8px 10px; margin-bottom: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 12px; color: #111827; }
.xr-summary .badge { background: #e5e7eb; color: #374151; border-color: #d1d5db; }

/* Chunk matrix */
.matrix2x2 { display: grid; grid-template-columns: max-content 1fr 1fr; gap: 6px 12px; align-items: center; }
.matrix2x2 .head { font-weight: 600; }
.matrix2x2 .rowhead { font-weight: 600; color: #374151; }

/* Aggregated attributes pivot */
.pivot-controls { display: flex; gap: 12px; align-items: center; margin: 8px 0; flex-wrap: wrap; }
.pivot-controls label { font-size: 13px; color: #374151; }
.pivot-controls select { margin-left: 6px; padding: 4px 6px; border: 1px solid #d1d5db; border-radius: 6px; background: #ffffff; }
.pivot-wrap { overflow: auto; }
.pivot-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.pivot-table th, .pivot-table td { border: 1px solid #e5e7eb; padding: 6px 8px; vertical-align: top; }
.pivot-table thead th { background: #f9fafb; }
.pivot-table .rowhead { background: #f9fafb; text-align: left; white-space: nowrap; }

/* Aggregated Panel */
.agg-panel-container {
  position: relative;
  width: 100%;
  grid-column: 2;
  grid-row: 1;
  transition: all 0.3s ease;
}

.agg-panel-toggle {
  position: absolute;
  left: -32px;
  top: 0;
  width: 32px;
  height: 40px;
  background: white;
  border: 1px solid #e5e7eb;
  border-right: none;
  border-radius: 12px 0 0 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: all 0.3s ease;
}

.agg-panel-toggle:hover {
  background-color: #f3f4f6;
}

.agg-panel-toggle svg {
  transition: transform 0.3s ease;
}

/* Hide the panel content when collapsed */
.agg-panel-container.collapsed #aggPanel {
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
  padding: 0;
  margin: 0;
  border: none;
}

/* Keep the toggle button visible and properly positioned */
.agg-panel-container.collapsed .agg-panel-toggle {
  position: absolute;
  left: 12px;
  transform: translateX(0);
  z-index: 10; /* Ensure it stays above other content */
}

/* Adjust the arrow direction when collapsed */
.agg-panel-container.collapsed .agg-panel-toggle svg {
  transform: rotate(180deg);
}

/* Responsive */
@media (max-width: 900px) {
  .content { 
    display: flex; 
    flex-direction: column;
    padding: 12px; 
  }
  
  .stage.two-col {
    display: flex;
    flex-direction: column;
  }
  
  .agg-panel-container {
    margin-top: 16px;
  }
  
  .agg-panel-toggle {
    left: 0;
    top: -32px;
    border-radius: 12px 12px 0 0;
    border-right: 1px solid #e5e7eb;
    border-bottom: none;
  }
  
  .agg-panel-container.collapsed {
    transform: translateY(20px);
  }
  .sidebar { 
    height: auto;
    max-height: 300px;
    margin-bottom: 12px;
    position: relative;
    transform: none;
    opacity: 1;
    pointer-events: auto;
    width: 100% !important;
  }
  .sidebar.collapsed {
    max-height: 40px;
    overflow: hidden;
    opacity: 1;
    transform: none;
    position: relative;
  }
  .sidebar-toggle {
    display: flex;
    right: 0;
    top: 0;
    border-radius: 0 12px 0 0;
    width: 40px;
    height: 40px;
  }
  .stage { padding: 0; }
  .slide { width: 100%; padding: 16px; }
  .hints { font-size: 11px; gap: 8px; }
  .controls { flex-direction: column; align-items: stretch; }
  .controls button { width: 100%; }
}

/* Make var-details full width on small screens */
@media (max-width: 768px) {
  .var-details {
    padding-left: 20px;
  }
  
  .var-details-content {
    overflow-x: auto;
  }
  
  .var-header {
    flex-direction: column;
    gap: 6px;
  }
  
  .var-actions {
    margin-left: auto;
    padding-left: 0;
    align-self: flex-start;
  }
  
  .varname {
    max-width: 100%;
  }
}

@media (max-width: 600px) {
  .app__header, .app__footer { padding: 10px 12px; }
  .slide { padding: 12px 14px; border-radius: 10px; }
  .codeblock { font-size: 11px; }
  .badge { font-size: 11px; padding: 1px 6px; }
  .tree { font-size: 13px; }
}
