/*
 * Kwalee BI — Chart tile component styles
 *
 * Styles for the chart tile toolbar, type toggle buttons, body,
 * fullscreen button, empty state overlay, and pivot tabs.
 *
 * Story 2.5: Chart Tile Component
 */

/* ─── Chart tile container ─── */
.chart-tile {
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-lg, 12px);
}

/* dark mode background comes from .dashboard-tile in dashboard.css (same neutral-900) */
[data-theme="dark"] .chart-tile {
  border: 1px solid var(--color-neutral-800);
}

[data-theme="light"] .chart-tile {
  background: var(--color-neutral-50);
  border: 1px solid var(--color-neutral-200);
  box-shadow: var(--shadow-sm);
}

/* ─── Toolbar row ─── */
.chart-tile__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px 4px;
  gap: var(--spacing-md, 16px);
}

.chart-tile__title {
  font-family: var(--font-heading, "Unbounded", system-ui, sans-serif);
  font-size: 1rem;
  font-weight: 600;
}

[data-theme="dark"] .chart-tile__title {
  color: var(--color-neutral-100);
}

[data-theme="light"] .chart-tile__title {
  color: var(--color-neutral-800);
}

.chart-tile__toolbar-right {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm, 8px);
}

/* ─── Chart type toggle buttons ─── */
.chart-type-toggle {
  display: inline-flex;
  border-radius: var(--radius-md, 8px);
  overflow: hidden;
}

[data-theme="dark"] .chart-type-toggle {
  background: var(--color-neutral-800);
  border: 1px solid var(--color-neutral-700);
}

[data-theme="light"] .chart-type-toggle {
  background: var(--color-neutral-100);
  border: 1px solid var(--color-neutral-200);
}

.chart-type-btn {
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition:
    background-color 0.15s,
    color 0.15s;
  line-height: 1.5;
}

[data-theme="dark"] .chart-type-btn {
  background: transparent;
  color: var(--color-neutral-400);
}

[data-theme="light"] .chart-type-btn {
  background: transparent;
  color: var(--color-neutral-500);
}

.chart-type-btn:hover {
  color: var(--color-primary);
}

.chart-type-btn--active {
  font-weight: 600;
}

[data-theme="dark"] .chart-type-btn--active {
  background: var(--color-neutral-700);
  color: var(--color-primary);
}

[data-theme="light"] .chart-type-btn--active {
  background: var(--color-neutral-50);
  color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}

/* ─── Fullscreen button ─── */
.chart-fullscreen-btn {
  padding: 4px 8px;
  font-size: 1rem;
  border: none;
  border-radius: var(--radius-sm, 4px);
  cursor: pointer;
  transition: background-color 0.15s;
  line-height: 1;
}

[data-theme="dark"] .chart-fullscreen-btn {
  background: transparent;
  color: var(--color-neutral-400);
}

[data-theme="light"] .chart-fullscreen-btn {
  background: transparent;
  color: var(--color-neutral-500);
}

.chart-fullscreen-btn:hover {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--color-primary);
}

/* ─── Chart body ─── */
.chart-tile__body {
  position: relative;
  flex: 1 1 0; /* KWBI-185 AC2: make flex basis explicit so height is grow-driven, not content-driven */
  min-height: 336px;
  padding: 0 10px 10px;
  overflow: hidden; /* KWBI-185 AC6: prevent overflow on chart-type/stack toggle */
}

.chart-tile__graph {
  width: 100%;
  height: 100%;
  min-height: 312px;
}

/* ─── Dimension pivot tabs ─── */
.chart-pivot-tabs {
  display: flex;
  gap: 2px;
  padding: 0 12px;
}

.chart-pivot-tab {
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 500;
  border: none;
  border-radius: var(--radius-sm, 4px);
  cursor: pointer;
  transition:
    background-color 0.15s,
    color 0.15s;
}

[data-theme="dark"] .chart-pivot-tab {
  background: transparent;
  color: var(--color-neutral-400);
}

[data-theme="light"] .chart-pivot-tab {
  background: transparent;
  color: var(--color-neutral-500);
}

.chart-pivot-tab:hover {
  color: var(--color-primary);
}

.chart-pivot-tab--active {
  font-weight: 600;
}

[data-theme="dark"] .chart-pivot-tab--active {
  background: var(--color-neutral-800);
  color: var(--color-primary);
}

[data-theme="light"] .chart-pivot-tab--active {
  background: var(--color-neutral-100);
  color: var(--color-primary);
}

/* ─── Empty state overlay ─── */
.chart-empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-sm, 8px);
  pointer-events: none;
}

[data-theme="dark"] .chart-empty-state {
  background: color-mix(in srgb, var(--bg-secondary) 85%, transparent);
}

[data-theme="light"] .chart-empty-state {
  background: color-mix(in srgb, var(--bg-elevated) 85%, transparent);
}

.chart-empty-state__icon {
  font-size: 2rem;
  opacity: 0.6;
}

.chart-empty-state__title {
  font-weight: 600;
  font-size: 0.875rem;
}

[data-theme="dark"] .chart-empty-state__title {
  color: var(--color-neutral-300);
}

.chart-empty-state__hint {
  font-size: 0.8125rem;
  color: var(--color-neutral-400);
}

/* ─── Legend overflow ─── */
.chart-legend-overflow {
  padding: var(--spacing-xs, 4px) var(--spacing-lg, 20px)
    var(--spacing-md, 16px);
  text-align: center;
}

.chart-legend-overflow__text {
  font-size: 0.75rem;
  color: var(--color-neutral-400);
}

/* ─── Toolbar action buttons ─── */
.chart-toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 0.875rem;
  border: none;
  border-radius: var(--radius-sm, 4px);
  cursor: pointer;
  transition:
    background-color 0.15s,
    color 0.15s;
  line-height: 1;
}

[data-theme="dark"] .chart-toolbar-btn {
  background: transparent;
  color: var(--color-neutral-400);
}

[data-theme="light"] .chart-toolbar-btn {
  background: transparent;
  color: var(--color-neutral-500);
}

[data-theme="light"] .chart-toolbar-btn,
[data-theme="light"] .chart-fullscreen-btn {
  color: var(--text-secondary);
}

.chart-toolbar-btn:hover {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--color-primary);
}

/* ─── CSS tooltips for toolbar buttons ─── */

.chart-toolbar-btn[data-tooltip],
.chart-fullscreen-btn[data-tooltip] {
  position: relative;
}

.chart-toolbar-btn[data-tooltip]::after,
.chart-fullscreen-btn[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 9px;
  background: var(--color-neutral-900);
  color: var(--color-neutral-50);
  font-size: 0.6875rem;
  font-weight: 500;
  white-space: nowrap;
  border-radius: 5px;
  box-shadow: var(--shadow-md);
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.15s ease,
    visibility 0.15s ease;
  z-index: 300;
}

[data-theme="light"] .chart-toolbar-btn[data-tooltip]::after,
[data-theme="light"] .chart-fullscreen-btn[data-tooltip]::after {
  background: color-mix(in srgb, var(--bg-elevated) 98%, transparent);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-md);
}

/* Tooltip arrow */
.chart-toolbar-btn[data-tooltip]::before,
.chart-fullscreen-btn[data-tooltip]::before {
  content: "";
  position: absolute;
  bottom: calc(100% + 3px);
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: var(--color-neutral-900);
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.15s ease,
    visibility 0.15s ease;
  z-index: 300;
}

[data-theme="light"] .chart-toolbar-btn[data-tooltip]::before,
[data-theme="light"] .chart-fullscreen-btn[data-tooltip]::before {
  border-top-color: var(--overlay-light-98);
}

.chart-toolbar-btn[data-tooltip]:hover::after,
.chart-toolbar-btn[data-tooltip]:hover::before,
.chart-fullscreen-btn[data-tooltip]:hover::after,
.chart-fullscreen-btn[data-tooltip]:hover::before {
  opacity: 1;
  visibility: visible;
}

/* ─── Info panel wrapper & panel ─── */

.chart-info-wrapper {
  position: relative;
  display: inline-flex;
}

.chart-info-panel {
  position: fixed;
  bottom: auto;
  z-index: 300;
  min-width: 220px;
  max-width: 360px;
  max-height: 300px;
  overflow-y: auto;
  padding: 12px 14px;
  border-radius: var(--radius-lg, 12px);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-6px);
  pointer-events: none;
  transition:
    opacity 0.15s ease,
    transform 0.15s ease,
    visibility 0.15s ease;
  /* Prevent overflow on any screen size */
  left: 50%;
  transform: translateX(-50%) translateY(-6px);
  margin: 0 auto;
}

/* Desktop: allow right-aligned variant when space permits */
@media (min-width: 1024px) {
  .chart-info-panel {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    left: auto;
    transform: translateY(-6px);
  }

  .chart-info-panel--open {
    transform: translateY(0);
  }
}

/* Mobile/Tablet: centered positioning */
@media (max-width: 1023px) {
  .chart-info-panel {
    position: fixed;
    left: 50%;
    transform: translateX(-50%) translateY(-6px);
    max-width: min(360px, calc(100vw - 24px));
    top: auto;
    bottom: 20px;
  }

  .chart-info-panel--open {
    transform: translateX(-50%) translateY(0);
  }
}

[data-theme="dark"] .chart-info-panel {
  background: var(--color-neutral-800);
  border: 1px solid var(--color-neutral-700);
  box-shadow: var(--shadow-lg);
}

[data-theme="light"] .chart-info-panel {
  background: var(--color-neutral-50);
  border: 1px solid var(--color-neutral-200);
  box-shadow: var(--shadow-lg);
}

/* Shared open state styles */
.chart-info-panel--open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* Info panel header (title + close button) */
.chart-info-panel__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-subtle);
}

/* Close button - hidden on desktop, shown on mobile */
.chart-info-panel__close {
  display: none;
  background: transparent;
  border: none;
  padding: 0;
  width: 20px;
  height: 20px;
  font-size: 1rem;
  cursor: pointer;
  color: var(--color-neutral-400);
  flex-shrink: 0;
  line-height: 1;
  transition: color 0.15s ease;
}

.chart-info-panel__close:hover {
  color: var(--color-primary);
}

/* Show close button on mobile/tablet */
@media (max-width: 1023px) {
  .chart-info-panel__close {
    display: block;
  }
}

.chart-info-panel__title {
  font-size: 0.8125rem;
  font-weight: 600;
  margin: 0;
  flex: 1;
  min-width: 0;
}

[data-theme="dark"] .chart-info-panel__title {
  color: var(--color-neutral-100);
}

[data-theme="light"] .chart-info-panel__title {
  color: var(--color-neutral-800);
}

/* ─── Plotly light-theme contrast fixes ─── */
[data-theme="light"] .chart-tile .js-plotly-plot .xtick text,
[data-theme="light"] .chart-tile .js-plotly-plot .ytick text,
[data-theme="light"] .chart-tile .js-plotly-plot .xaxislayer-above text,
[data-theme="light"] .chart-tile .js-plotly-plot .yaxislayer-above text,
[data-theme="light"] .chart-tile .js-plotly-plot .gtitle text,
[data-theme="light"] .chart-tile .js-plotly-plot .xtitle,
[data-theme="light"] .chart-tile .js-plotly-plot .ytitle,
[data-theme="light"] .chart-tile .js-plotly-plot .legend text {
  fill: var(--text-secondary) !important;
}

[data-theme="light"] .chart-tile .js-plotly-plot .hoverlayer .hovertext rect {
  fill: color-mix(in srgb, var(--bg-elevated) 98%, transparent) !important;
  stroke: var(--border-color) !important;
}

[data-theme="light"] .chart-tile .js-plotly-plot .hoverlayer .hovertext text,
[data-theme="light"] .chart-tile .js-plotly-plot .hoverlayer .hovertext tspan {
  fill: var(--text-primary) !important;
}

.chart-info-panel__meta {
  font-size: 0.75rem;
  margin: 4px 0;
  color: var(--color-neutral-400);
}

.chart-info-panel__custom {
  font-size: 0.75rem;
  margin: 0 0 8px;
  color: var(--color-neutral-300);
}

.chart-info-panel__custom :is(p, ul, ol, pre, table) {
  margin: 0 0 8px;
}

.chart-info-panel__custom :is(h3, h4) {
  margin: 0 0 8px;
  font-size: 0.75rem;
}

.chart-info-panel__custom a {
  color: var(--accent);
  text-decoration: underline;
}

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

.chart-info-panel__custom :is(th, td) {
  padding: 4px 6px;
  border: 1px solid var(--color-neutral-700);
}

.chart-info-panel__source {
  font-size: 0.6875rem;
  margin: 8px 0 0;
  padding-top: 8px;
  border-top: 1px solid var(--border-subtle);
  color: var(--color-neutral-400);
  font-style: italic;
}

/* ─── Fullscreen mode ─── */

.chart-tile--fullscreen {
  position: fixed !important;
  inset: 0 !important;
  z-index: 1100 !important;
  border-radius: 0 !important;
  width: 100vw !important;
  height: 100dvh !important;
  max-height: none !important;
  overflow: auto !important;
  display: flex !important;
  flex-direction: column !important;
}

.chart-tile--fullscreen .chart-tile__body {
  flex: 1 !important;
  min-height: 0 !important;
  /* KWBI-185: restore overflow in fullscreen so Plotly hover labels near the
     plot edges are not clipped (the AC6 clip only matters during in-grid toggles). */
  overflow: visible !important;
}

.chart-tile--fullscreen .chart-tile__graph {
  min-height: 0 !important;
  height: 100% !important;
}

.chart-fullscreen-backdrop {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  z-index: 1099;
  cursor: pointer;
}

/* ─── Controls bar (chart type toggle + pivot tabs) ─── */
.chart-tile__controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-sm, 8px);
  padding: 0 12px 4px;
}

/* ─── Kebab actions menu ─── */
/* Desktop: show action buttons inline, hide kebab trigger */
.chart-actions-menu {
  display: contents;
}

.chart-actions-menu__trigger {
  display: none;
}

.chart-actions-menu__popover {
  display: contents;
}

/* ─── Legend toggle — hidden on desktop ─── */
.chart-legend-toggle {
  display: none;
}

/* ─── Mobile optimisations (< 768px) ─── */
@media (max-width: 767px) {
  /* Tile needs positioning context for the kebab menu */
  .chart-tile {
    position: relative;
  }

  /* --- Toolbar: stack title above controls --- */
  .chart-tile__toolbar {
    flex-wrap: wrap;
    padding: 6px 8px 2px;
    padding-right: 32px; /* reserve space for kebab in top-right */
    gap: 4px;
  }

  .chart-tile__title {
    flex: 1 1 100%;
    font-size: 0.8125rem;
  }

  .chart-tile__toolbar-right {
    gap: 4px;
  }

  /* --- Controls bar: type toggle + pivots side-by-side, scrollable --- */
  .chart-tile__controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 0 8px 2px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }

  .chart-tile__controls::-webkit-scrollbar {
    display: none;
  }

  /* --- Chart type toggle: compact --- */
  .chart-type-toggle {
    border-radius: var(--radius-sm, 4px);
    flex-shrink: 0;
  }

  .chart-type-btn {
    padding: 2px 8px;
    font-size: 0.6875rem;
  }

  /* --- Kebab menu: pin to top-right of tile --- */
  .chart-actions-menu {
    position: absolute;
    top: 6px;
    right: 6px;
    display: inline-flex;
    z-index: 120;
  }

  .chart-actions-menu__trigger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--color-neutral-400);
    cursor: pointer;
    border-radius: var(--radius-sm, 4px);
    transition: background-color 0.15s;
  }

  .chart-actions-menu__trigger:hover {
    background: color-mix(in srgb, var(--accent) 10%, transparent);
    color: var(--color-primary);
  }

  .chart-actions-menu__popover {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    z-index: 200;
    flex-direction: column;
    gap: 2px;
    padding: 6px;
    border-radius: var(--radius-md, 8px);
    min-width: 140px;
  }

  [data-theme="dark"] .chart-actions-menu__popover {
    background: var(--color-neutral-800);
    border: 1px solid var(--color-neutral-700);
    box-shadow: var(--shadow-md);
  }

  [data-theme="light"] .chart-actions-menu__popover {
    background: var(--color-neutral-50);
    border: 1px solid var(--color-neutral-200);
    box-shadow: var(--shadow-md);
  }

  .chart-actions-menu:focus-within .chart-actions-menu__popover {
    display: flex;
  }

  /* Override button styles inside popover */
  .chart-actions-menu__popover .chart-toolbar-btn,
  .chart-actions-menu__popover .chart-fullscreen-btn {
    width: 100%;
    height: auto;
    padding: 6px 10px;
    justify-content: flex-start;
    border-radius: var(--radius-sm, 4px);
  }

  .chart-actions-menu__popover .chart-toolbar-btn::after {
    content: attr(title);
    margin-left: 8px;
    font-size: 0.75rem;
  }

  .chart-actions-menu__popover .chart-fullscreen-btn::after {
    content: attr(title);
    margin-left: 8px;
    font-size: 0.75rem;
  }

  /* Hide CSS tooltips inside popover (labels replace them) */
  .chart-actions-menu__popover .chart-toolbar-btn[data-tooltip]::after,
  .chart-actions-menu__popover .chart-fullscreen-btn[data-tooltip]::after {
    /* Use title text instead of tooltip */
    content: attr(title);
    position: static;
    transform: none;
    padding: 0;
    background: none;
    color: inherit;
    font-weight: 400;
    white-space: nowrap;
    border-radius: 0;
    box-shadow: none;
    opacity: 1;
    visibility: visible;
    margin-left: 8px;
    font-size: 0.75rem;
  }

  .chart-actions-menu__popover .chart-toolbar-btn[data-tooltip]::before,
  .chart-actions-menu__popover .chart-fullscreen-btn[data-tooltip]::before {
    display: none;
  }

  /* --- Pivot tabs: compact, no-shrink inside controls bar --- */
  .chart-pivot-tabs {
    padding: 0;
    gap: 0;
    flex-shrink: 0;
  }

  .chart-pivot-tab {
    padding: 3px 8px;
    font-size: 0.6875rem;
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* --- Chart body: tighter, reclaim Plotly right-margin space --- */
  .chart-tile__body {
    min-height: 240px;
    padding: 0;
    overflow: hidden;
  }

  .chart-tile__graph {
    min-height: 216px;
    /* Plotly reserves 140px right margin for the vertical legend which is
       hidden on mobile. Stretch the graph past its container so the plot
       area fills the full tile width. */
    width: calc(100% + 120px) !important;
    margin-right: -120px;
  }

  /* --- Legend: hide Plotly SVG legend, use HTML popover instead --- */
  .chart-tile__graph .legend {
    display: none !important;
  }

  .chart-legend-toggle {
    display: flex;
    justify-content: flex-end;
    padding: 2px 8px 0;
    position: relative;
    z-index: 10;
  }

  .chart-legend-toggle__btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    font-size: 0.6875rem;
    font-weight: 500;
    color: var(--color-neutral-400);
    cursor: pointer;
    border-radius: var(--radius-sm, 4px);
    border: 1px solid var(--color-neutral-700);
    user-select: none;
    list-style: none;
  }

  .chart-legend-toggle__btn::-webkit-details-marker {
    display: none;
  }

  .chart-legend-toggle__btn::before {
    content: "▸ ";
    font-size: 0.5625rem;
  }

  .chart-legend-toggle[open] .chart-legend-toggle__btn {
    color: var(--color-primary);
    border-color: var(--color-primary);
  }

  .chart-legend-toggle[open] .chart-legend-toggle__btn::before {
    content: "▾ ";
  }

  /* Popover: floats over the chart body below */
  .chart-legend-popover {
    position: absolute;
    top: 100%;
    left: 4px;
    right: 4px;
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    gap: 4px 10px;
    padding: 8px 10px;
    margin-top: 4px;
    border-radius: var(--radius-md, 8px);
    max-height: 100px;
    overflow-y: auto;
    scrollbar-width: thin;
  }

  [data-theme="dark"] .chart-legend-popover {
    background: color-mix(in srgb, var(--bg-secondary) 95%, transparent);
    border: 1px solid var(--color-neutral-700);
    box-shadow: var(--shadow-lg);
  }

  [data-theme="light"] .chart-legend-popover {
    background: color-mix(in srgb, var(--bg-elevated) 97%, transparent);
    border: 1px solid var(--color-neutral-200);
    box-shadow: var(--shadow-md);
  }

  .chart-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.6875rem;
    color: var(--color-neutral-300);
    white-space: nowrap;
  }

  .chart-legend-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    flex-shrink: 0;
  }
}
