/*
 * Chart Analyzer styles.
 * All colors and spacing exposed as CSS variables so the host theme can override them.
 */

.chart-analyzer {
	--ca-bg:        #ffffff;
	--ca-fg:        #1a1a1a;
	--ca-muted:     #6b7280;
	--ca-border:    #e5e7eb;
	--ca-accent:    #2d6cdf;
	--ca-up:        #2f9e44;
	--ca-down:      #c92a2a;
	--ca-flat:      #6b7280;
	--ca-radius:    8px;
	--ca-pad:       1rem;

	background: var(--ca-bg);
	color: var(--ca-fg);
	border: 1px solid var(--ca-border);
	border-radius: var(--ca-radius);
	padding: var(--ca-pad);
	margin: 1.5rem 0;
	font-size: 0.95rem;
	line-height: 1.45;
	box-sizing: border-box;
	width: 100%;
	/* Never wider than viewport, even when the parent is.
	   The 1rem subtraction leaves a safety gutter so we never trigger
	   horizontal scroll on iOS Safari where 100vw can include the scrollbar. */
	max-width: min(100%, calc(100vw - 1rem));
	min-width: 0;
	overflow-x: hidden; /* hard guarantee that the chart-analyzer never breaks the page width */
}

.chart-analyzer *,
.chart-analyzer *::before,
.chart-analyzer *::after {
	box-sizing: border-box;
}

.chart-analyzer__title {
	margin: 0 0 0.75rem;
	font-size: 24px;
	font-weight: 700;
	line-height: 1.2;
}

.chart-analyzer__chart-wrap {
	position: relative;
	/* Cap height by both pixel value and viewport-relative size.
	   In landscape on a phone (small vh), the chart shrinks so the title
	   and insights stay visible. min-height keeps it usable on tiny viewports. */
	height: min(360px, 65vh);
	min-height: 240px;
	width: 100%;
	max-width: 100%;
	min-width: 0;
	overflow: hidden;
}

/* Chart.js sets inline width/height on the canvas; these rules just guarantee
   it stays within its parent on iOS Safari, where percentage sizing can lag. */
.chart-analyzer__chart-wrap canvas {
	display: block;
	max-width: 100% !important;
	max-height: 100% !important;
	box-sizing: border-box;
}

.chart-analyzer__insights {
	margin-top: 1rem;
}

.chart-analyzer__summary {
	font-size: 0.95rem;
	margin-bottom: 0.75rem;
	color: var(--ca-fg);
}

.chart-analyzer__muted {
	color: var(--ca-muted);
	font-weight: normal;
	font-size: 0.85em;
}

/* Wrap insights tables so they scroll horizontally instead of breaking layout. */
.chart-analyzer__table-wrap {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	max-width: 100%;
}

.chart-analyzer__table {
	width: 100%;
	border-collapse: collapse;
	font-size: 0.9rem;
}

.chart-analyzer__table th,
.chart-analyzer__table td {
	padding: 0.4rem 0.6rem;
	border-bottom: 1px solid var(--ca-border);
	text-align: left;
	white-space: nowrap;
}

.chart-analyzer__table th {
	color: var(--ca-muted);
	font-weight: 600;
	font-size: 0.8rem;
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.chart-analyzer__num {
	text-align: right;
	font-variant-numeric: tabular-nums;
}

.chart-analyzer__swatch {
	display: inline-block;
	width: 10px;
	height: 10px;
	border-radius: 2px;
	margin-right: 0.5rem;
	vertical-align: middle;
}

.chart-analyzer__trend--increasing { color: var(--ca-up);   font-weight: 600; }
.chart-analyzer__trend--decreasing { color: var(--ca-down); font-weight: 600; }
.chart-analyzer__trend--flat       { color: var(--ca-flat); font-weight: 600; }

.chart-analyzer__error {
	padding: 0.75rem 1rem;
	background: #fff5f5;
	border: 1px solid #ffd6d6;
	color: #b91c1c;
	border-radius: var(--ca-radius);
	font-size: 0.9rem;
	line-height: 1.5;
}

.chart-analyzer__debug {
	font-size: 0.9rem;
}

.chart-analyzer__debug h4 {
	margin: 0 0 0.5rem;
	font-size: 1rem;
}

.chart-analyzer__debug code {
	background: rgba(0,0,0,0.06);
	padding: 1px 4px;
	border-radius: 3px;
	font-size: 0.85em;
}

/* ---------- Mobile (≤ 600px) ---------- */
@media (max-width: 600px) {
	.chart-analyzer {
		--ca-pad: 0.75rem;
		font-size: 0.9rem;
		margin: 1rem 0;
	}

	/* On narrow viewports, also lower the absolute cap a bit. min-height
	   from the base rule still applies, so very short landscape phones still
	   get a usable chart. */
	.chart-analyzer__chart-wrap {
		height: min(320px, 65vh);
	}

	.chart-analyzer__table {
		font-size: 0.8rem;
	}

	.chart-analyzer__table th,
	.chart-analyzer__table td {
		padding: 0.35rem 0.5rem;
	}

	.chart-analyzer__summary {
		font-size: 0.9rem;
	}
}

/* ---------- Very small phones (≤ 380px) ---------- */
@media (max-width: 380px) {
	.chart-analyzer__chart-wrap {
		height: min(290px, 65vh);
		min-height: 220px;
	}
}

/* ---------- Optional explicit dark mode ----------
   Add the class manually (e.g. via the host theme) to opt in:
       <div class="chart-analyzer chart-analyzer--dark">...</div>
   We do NOT auto-switch on prefers-color-scheme, because that fights with
   host themes that have their own (light or different) styling. */
.chart-analyzer--dark {
	--ca-bg:     #1f2024;
	--ca-fg:     #f1f1f1;
	--ca-muted:  #9aa0a6;
	--ca-border: #34363c;
}
.chart-analyzer--dark .chart-analyzer__error {
	background: #2a1414;
	border-color: #5a2222;
	color: #ffb4b4;
}
