/**
 * Conversation History — frontend chat visual language.
 *
 * Loaded only on the Niwa AI History admin screen.
 *
 * @package Niwa_AI
 */

.niwa-history {
	--niwa-history-accent: #65d38a;
	--niwa-history-accent-active: #82e6a4;
	--niwa-history-accent-ink: #102019;
	--niwa-history-accent-subtle: rgba(101, 211, 138, 0.18);
	--niwa-history-gold: #d4a853;
	--niwa-history-glass: rgba(34, 35, 33, 0.88);
	--niwa-history-header: rgba(27, 32, 31, 0.94);
	--niwa-history-panel: rgba(255, 255, 255, 0.07);
	--niwa-history-panel-hover: rgba(255, 255, 255, 0.11);
	--niwa-history-panel-active: rgba(101, 211, 138, 0.18);
	--niwa-history-input: rgba(255, 255, 255, 0.10);
	--niwa-history-canvas: rgba(40, 48, 46, 0.96);
	--niwa-history-text: #fff;
	--niwa-history-text-secondary: #e1e6e3;
	--niwa-history-text-tertiary: #b7c0bc;
	--niwa-history-border: rgba(255, 255, 255, 0.18);
	--niwa-history-border-subtle: rgba(255, 255, 255, 0.11);
	--niwa-history-bubble-bot: rgba(66, 68, 65, 0.28);
	--niwa-history-bubble-user: rgba(34, 35, 33, 0.52);
	--niwa-history-bubble-text: #fff;
	--niwa-history-shadow: 0 18px 50px rgba(0, 0, 0, 0.26), 0 1px 0 rgba(255, 255, 255, 0.12) inset;
	font-family: 'Manrope', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.niwa-history .niwa-page-header {
	margin-bottom: var(--niwa-space-lg);
	font-family: inherit;
}

.niwa-history .niwa-page-header__icon {
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
}

.niwa-history-layout {
	position: relative;
	isolation: isolate;
	display: grid;
	grid-template-columns: 340px minmax(0, 1fr);
	height: clamp(620px, calc(100vh - 190px), 820px);
	min-height: 0;
	overflow: hidden;
	border: 1px solid var(--niwa-history-border);
	border-radius: 22px;
	background: var(--niwa-history-glass);
	box-shadow: var(--niwa-history-shadow);
	backdrop-filter: blur(24px) saturate(1.2);
	-webkit-backdrop-filter: blur(24px) saturate(1.2);
	color: var(--niwa-history-text);
}

.niwa-history-sidebar {
	display: flex;
	min-width: 0;
	min-height: 0;
	flex-direction: column;
	border-right: 1px solid var(--niwa-history-border);
	background: var(--niwa-history-panel);
}

.niwa-history-sidebar-header {
	flex: 0 0 auto;
	padding: 18px 16px 14px;
	border-bottom: 1px solid var(--niwa-history-border-subtle);
}

.niwa-history-sidebar-title {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 14px;
}

.niwa-history-sidebar-title h2 {
	margin: 0;
	color: var(--niwa-history-text);
	font-size: 18px;
	font-weight: 700;
	letter-spacing: -0.02em;
}

.niwa-history-sidebar-title > span {
	min-width: 24px;
	padding: 3px 8px;
	border-radius: 9999px;
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
	font-size: 11px;
	font-weight: 700;
	text-align: center;
}

.niwa-history-search {
	position: relative;
	display: flex;
	align-items: center;
}

.niwa-history-search input[type="search"] {
	width: 100%;
	height: 42px;
	margin: 0;
	padding: 0 42px 0 38px;
	border: 1px solid var(--niwa-history-border);
	border-radius: 9999px;
	background: var(--niwa-history-input);
	box-shadow: none;
	color: var(--niwa-history-text);
	font-family: inherit;
	font-size: 13px;
}

.niwa-history-search input[type="search"]::placeholder {
	color: var(--niwa-history-text-tertiary);
}

.niwa-history-search input[type="search"]:focus {
	border-color: var(--niwa-history-accent);
	box-shadow: 0 0 0 3px var(--niwa-history-accent-subtle);
	outline: none;
}

.niwa-history-search-icon {
	position: absolute;
	left: 13px;
	z-index: 1;
	display: inline-flex;
	width: 16px;
	height: 16px;
	color: var(--niwa-history-text-secondary);
	pointer-events: none;
}

.niwa-history-search button {
	position: absolute;
	right: 5px;
	display: inline-grid;
	width: 32px;
	height: 32px;
	padding: 0;
	place-items: center;
	border: 0;
	border-radius: 50%;
	background: transparent;
	color: var(--niwa-history-text-secondary);
	cursor: pointer;
}

.niwa-history-search button:hover,
.niwa-history-search button:focus-visible {
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
	outline: none;
}

.niwa-history-search button svg {
	width: 16px;
	height: 16px;
}

.niwa-history-search-clear {
	display: block;
	margin: 8px 2px 0 auto;
	color: var(--niwa-history-accent);
	font-size: 11px;
}

.niwa-history-session-list,
.niwa-history-chat-scroll {
	scrollbar-color: var(--niwa-history-border) transparent;
	scrollbar-width: thin;
}

.niwa-history-session-list {
	flex: 1 1 auto;
	min-height: 0;
	overflow-y: auto;
	overscroll-behavior: contain;
}

.niwa-history-session-item {
	position: relative;
	display: grid;
	grid-template-columns: 42px minmax(0, 1fr);
	gap: 11px;
	padding: 14px 14px 13px 16px;
	border-bottom: 1px solid var(--niwa-history-border-subtle);
	color: var(--niwa-history-text);
	text-decoration: none;
	transition: background-color 150ms ease-out;
}

.niwa-history-session-item::before {
	position: absolute;
	inset: 10px auto 10px 0;
	width: 3px;
	border-radius: 0 9999px 9999px 0;
	background: transparent;
	content: "";
}

.niwa-history-session-item:hover,
.niwa-history-session-item:focus-visible {
	background: var(--niwa-history-panel-hover);
	color: var(--niwa-history-text);
	box-shadow: none;
	outline: none;
}

.niwa-history-session-item:focus-visible {
	box-shadow: inset 0 0 0 2px var(--niwa-history-accent);
}

.niwa-history-session-item.is-active {
	background: var(--niwa-history-panel-active);
}

.niwa-history-session-item.is-active::before {
	background: var(--niwa-history-accent);
}

.niwa-history-session-avatar,
.niwa-history-chat-avatar {
	display: inline-grid;
	flex: 0 0 auto;
	place-items: center;
	border: 1px solid var(--niwa-history-border);
	border-radius: 50%;
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
}

.niwa-history-session-avatar {
	width: 42px;
	height: 42px;
}

.niwa-history-session-avatar svg {
	width: 21px;
	height: 21px;
}

.niwa-history-session-item.is-active .niwa-history-session-avatar {
	border-color: transparent;
	background: var(--niwa-history-accent);
	color: var(--niwa-history-accent-ink);
}

.niwa-history-session-copy {
	display: block;
	min-width: 0;
}

.niwa-history-session-top {
	display: flex;
	min-width: 0;
	align-items: baseline;
	justify-content: space-between;
	gap: 8px;
}

.niwa-history-session-top strong {
	overflow: hidden;
	color: var(--niwa-history-text);
	font-size: 13px;
	font-weight: 700;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.niwa-history-session-top time {
	flex: 0 0 auto;
	color: var(--niwa-history-text-tertiary);
	font-size: 10px;
}

.niwa-history-session-question,
.niwa-history-session-answer,
.niwa-history-session-meta {
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.niwa-history-session-question {
	margin-top: 4px;
	color: var(--niwa-history-text);
	font-size: 12px;
	font-weight: 600;
}

.niwa-history-session-answer {
	margin-top: 2px;
	color: var(--niwa-history-text-secondary);
	font-size: 11px;
}

.niwa-history-session-answer b {
	color: var(--niwa-history-accent);
}

.niwa-history-session-meta {
	margin-top: 6px;
	color: var(--niwa-history-text-tertiary);
	font-size: 10px;
	font-weight: 600;
	letter-spacing: 0.04em;
	text-transform: uppercase;
}

.niwa-history-sidebar-empty {
	display: grid;
	gap: 4px;
	padding: 36px 20px;
	text-align: center;
}

.niwa-history-sidebar-empty strong {
	color: var(--niwa-history-text);
}

.niwa-history-sidebar-empty span {
	color: var(--niwa-history-text-secondary);
	font-size: 12px;
}

.niwa-history-pagination {
	display: grid;
	min-height: 52px;
	flex: 0 0 auto;
	grid-template-columns: 32px 1fr 32px;
	align-items: center;
	gap: 8px;
	padding: 10px 14px;
	border-top: 1px solid var(--niwa-history-border-subtle);
	background: var(--niwa-history-panel);
	text-align: center;
}

.niwa-history-pagination a,
.niwa-history-pagination > span {
	display: grid;
	width: 30px;
	height: 30px;
	place-items: center;
	border-radius: 50%;
	text-decoration: none;
}

.niwa-history-pagination a {
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
}

.niwa-history-pagination a:hover,
.niwa-history-pagination a:focus-visible {
	background: var(--niwa-history-accent);
	color: var(--niwa-history-accent-ink);
	outline: none;
}

.niwa-history-pagination > span {
	background: var(--niwa-history-panel);
	color: var(--niwa-history-text-secondary);
}

.niwa-history-pagination small {
	color: var(--niwa-history-text-secondary);
	font-size: 11px;
	font-weight: 700;
}

.niwa-history-chat-pane {
	position: relative;
	display: flex;
	min-width: 0;
	min-height: 0;
	flex-direction: column;
	overflow: hidden;
	background: var(--niwa-history-canvas);
	outline: none;
}

.niwa-history-chat-pane::before {
	position: absolute;
	top: -180px;
	right: -120px;
	width: 420px;
	height: 420px;
	border-radius: 50%;
	background: radial-gradient(circle, color-mix(in srgb, var(--niwa-history-gold) 14%, transparent), transparent 68%);
	content: "";
	pointer-events: none;
}

.niwa-history-chat-header {
	position: relative;
	z-index: 1;
	display: flex;
	min-height: 72px;
	flex: 0 0 auto;
	align-items: center;
	gap: 12px;
	padding: 12px 20px;
	border-bottom: 1px solid var(--niwa-history-border);
	background: var(--niwa-history-header);
	backdrop-filter: blur(24px) saturate(1.2);
	-webkit-backdrop-filter: blur(24px) saturate(1.2);
}

.niwa-history-chat-avatar {
	width: 46px;
	height: 46px;
}

.niwa-history-chat-avatar svg {
	width: 24px;
	height: 24px;
}

.niwa-history-chat-heading {
	min-width: 0;
	flex: 1 1 auto;
}

.niwa-history-chat-heading h2 {
	margin: 0;
	color: var(--niwa-history-text);
	font-size: 15px;
	font-weight: 700;
	letter-spacing: -0.01em;
}

.niwa-history-chat-heading p {
	margin: 3px 0 0;
	color: var(--niwa-history-text-secondary);
	font-size: 11px;
}

.niwa-history-session-details {
	position: relative;
	flex: 0 0 auto;
}

.niwa-history-session-details summary {
	padding: 7px 11px;
	border: 1px solid var(--niwa-history-border);
	border-radius: 9999px;
	background: var(--niwa-history-panel);
	color: var(--niwa-history-text-secondary);
	font-size: 11px;
	font-weight: 600;
	cursor: pointer;
	list-style: none;
}

.niwa-history-session-details summary::-webkit-details-marker {
	display: none;
}

.niwa-history-session-details summary:focus-visible {
	border-color: var(--niwa-history-accent);
	outline: none;
}

.niwa-history-session-details code {
	position: absolute;
	top: calc(100% + 8px);
	right: 0;
	z-index: 3;
	width: min(420px, 60vw);
	padding: 10px 12px;
	border: 1px solid var(--niwa-history-border);
	border-radius: 10px;
	background: var(--niwa-history-glass);
	box-shadow: var(--niwa-history-shadow);
	color: var(--niwa-history-text);
	font-size: 11px;
	word-break: break-all;
	backdrop-filter: blur(24px);
	-webkit-backdrop-filter: blur(24px);
}

.niwa-history-chat-scroll {
	position: relative;
	z-index: 1;
	min-height: 0;
	flex: 1 1 auto;
	overflow-y: auto;
	overscroll-behavior: contain;
	transition: opacity 150ms ease-out;
}

.niwa-history-transcript {
	display: grid;
	align-content: start;
	gap: 8px;
	max-width: 1020px;
	min-height: 100%;
	margin: 0 auto;
	padding: 28px clamp(20px, 4vw, 56px) 38px;
}

.niwa-history-date-divider {
	display: flex;
	align-items: center;
	gap: 12px;
	margin: 4px 0 14px;
	color: var(--niwa-history-text-tertiary);
	font-size: 10px;
	font-weight: 600;
	letter-spacing: 0.05em;
	text-transform: uppercase;
}

.niwa-history-date-divider::before,
.niwa-history-date-divider::after {
	height: 1px;
	flex: 1 1 auto;
	background: var(--niwa-history-border-subtle);
	content: "";
}

.niwa-history-message {
	display: flex;
	width: fit-content;
	max-width: min(720px, 82%);
	flex-direction: column;
	align-items: flex-start;
	margin-bottom: 6px;
}

.niwa-history-message--customer {
	justify-self: end;
	align-items: flex-end;
}

.niwa-history-message__meta {
	display: flex;
	align-items: center;
	gap: 8px;
	margin: 0 7px 5px;
	color: var(--niwa-history-text-tertiary);
	font-size: 10px;
}

.niwa-history-message__meta strong {
	color: var(--niwa-history-text-secondary);
	font-size: 11px;
	font-weight: 600;
}

.niwa-history-message__text {
	padding: 12px 16px;
	border: 1px solid var(--niwa-history-border-subtle);
	border-radius: 20px 20px 20px 6px;
	background: var(--niwa-history-bubble-bot);
	box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08), 0 0 1px rgba(0, 0, 0, 0.05);
	color: var(--niwa-history-bubble-text);
	font-size: 13px;
	line-height: 1.5;
	overflow-wrap: anywhere;
	backdrop-filter: blur(12px);
	-webkit-backdrop-filter: blur(12px);
}

.niwa-history-message--customer .niwa-history-message__text {
	border-radius: 20px 20px 6px 20px;
	background: var(--niwa-history-bubble-user);
}

.niwa-history-chat-empty {
	display: grid;
	min-height: 100%;
	place-content: center;
	justify-items: center;
	padding: 32px;
	text-align: center;
}

.niwa-history-chat-empty__icon {
	display: grid;
	width: 72px;
	height: 72px;
	margin-bottom: 16px;
	place-items: center;
	border: 1px solid var(--niwa-history-border);
	border-radius: 50%;
	background: var(--niwa-history-accent-subtle);
	color: var(--niwa-history-accent);
}

.niwa-history-chat-empty__icon svg {
	width: 34px;
	height: 34px;
}

.niwa-history-chat-empty h2 {
	margin: 0;
	color: var(--niwa-history-text);
	font-size: 18px;
}

.niwa-history-chat-empty p {
	max-width: 360px;
	margin: 7px 0 0;
	color: var(--niwa-history-text-secondary);
	font-size: 13px;
}

.niwa-history-layout.is-loading .niwa-history-chat-scroll {
	opacity: 0.35;
	pointer-events: none;
}

.niwa-history-layout.is-loading .niwa-history-chat-pane::after {
	position: absolute;
	top: 50%;
	left: 50%;
	z-index: 4;
	width: 28px;
	height: 28px;
	margin: -14px 0 0 -14px;
	border: 3px solid var(--niwa-history-accent-subtle);
	border-top-color: var(--niwa-history-accent);
	border-radius: 50%;
	animation: niwa-history-spin 700ms linear infinite;
	content: "";
}

.niwa-history-load-error {
	position: absolute;
	top: 84px;
	left: 50%;
	z-index: 5;
	padding: 8px 14px;
	border: 1px solid var(--niwa-history-border);
	border-radius: 9999px;
	background: var(--niwa-history-glass);
	box-shadow: var(--niwa-history-shadow);
	color: var(--niwa-history-text);
	font-size: 12px;
	transform: translateX(-50%);
	backdrop-filter: blur(24px);
	-webkit-backdrop-filter: blur(24px);
}

#niwa-history-chat-end {
	display: block;
	height: 1px;
}

@keyframes niwa-history-spin {
	to {
		transform: rotate(360deg);
	}
}

@media screen and (max-width: 1100px) {
	.niwa-history-layout {
		grid-template-columns: 300px minmax(0, 1fr);
	}

	.niwa-history-message {
		max-width: 88%;
	}
}

@media screen and (max-width: 782px) {
	.niwa-history-layout {
		grid-template-columns: 1fr;
		height: auto;
		overflow: visible;
	}

	.niwa-history-sidebar {
		max-height: 420px;
		border-right: 0;
		border-bottom: 1px solid var(--niwa-history-border);
	}

	.niwa-history-chat-pane {
		min-height: 620px;
	}

	.niwa-history-chat-header {
		align-items: flex-start;
	}

	.niwa-history-session-details {
		margin-left: auto;
	}

	.niwa-history-message {
		max-width: 94%;
	}
}

@media (prefers-reduced-motion: reduce) {
	.niwa-history-session-item,
	.niwa-history-chat-scroll {
		transition: none;
	}

	.niwa-history-layout.is-loading .niwa-history-chat-pane::after {
		animation: none;
	}
}
