/*
 * K.I. Grocery assistant widget - calm, Apple-style look.
 *
 * Rules applied (from Apple's Human Interface Guidelines and apple.com):
 *  - Blur and translucency only on the functional layer (panel shell, header, composer); content
 *    surfaces (bubbles, product box) stay solid. Solid fallback where backdrop-filter is missing or
 *    the user asks for reduced transparency.
 *  - System font first, readable sizes (16px body, nothing under 13px), no light weights.
 *  - Neutral ink #1D1D1F on white / #F5F5F7 / #E9E9EB; ONE accent, KI orange #B84F08, used only for
 *    the send button, ticks, "Add selected to cart", the user's bubbles and the launcher.
 *  - Large soft radii, hairline separators instead of borders, very subtle layered shadows.
 *  - Motion is brief, interruptible and never the only signal; reduced motion switches it all off.
 *  - Every control is at least 44x44px.
 *
 * Contrast (WCAG 2.x): ink on white 16.8:1, ink on bubble grey 13.9:1, white on accent 5.05:1,
 * secondary text #5C5C61 on white 6.65:1 and on the worst-case translucent panel 5.1:1,
 * input border #8E8E93 on white 3.26:1 (non-text), red #B3261E on bubble grey 5.4:1.
 *
 * Every selector is prefixed "html body" so the theme's button and input styles cannot win.
 * Placement: the script sets --kiai-lift and --kiai-edge so the launcher stacks 12px above the
 * theme's go-up button (.scrollToTop) and shares its right edge.
 */

html body .kiai-launcher,
html body .kiai-panel,
html body .kiai-panel * {
	box-sizing: border-box;
	font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", Manrope, system-ui, "Segoe UI", Tahoma, "Noto Sans Arabic", sans-serif;
	-webkit-font-smoothing: antialiased;
}

/* ---- Launcher ---- */

html body .kiai-launcher {
	position: fixed;
	right: var(--kiai-edge, 16px);
	bottom: var(--kiai-lift, 16px);
	z-index: 99998;
	width: 56px;
	height: 56px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: var(--kiai-accent, #B84F08);
	color: #FFFFFF;
	box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.12), 0 6px 16px rgba(0, 0, 0, 0.18);
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition-property: scale, filter;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-launcher .kiai-ico {
	position: absolute;
	width: 26px;
	height: 26px;
	transition-property: opacity, scale, filter;
	transition-duration: 200ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-launcher .kiai-ico-close { opacity: 0; scale: 0.25; filter: blur(4px); }
html body .kiai-launcher[aria-expanded="true"] .kiai-ico-chat { opacity: 0; scale: 0.25; filter: blur(4px); }
html body .kiai-launcher[aria-expanded="true"] .kiai-ico-close { opacity: 1; scale: 1; filter: blur(0); }

@media (hover: hover) {
	html body .kiai-launcher:hover,
	html body .kiai-add:hover:not(:disabled),
	html body .kiai-send:hover:not(:disabled) { filter: brightness(0.92); }
	html body .kiai-chip:hover:not(:disabled),
	html body .kiai-cartlink:hover,
	html body .kiai-step:hover:not(:disabled) { background: #DCDCE0; }
}

/* Press feedback. */
html body .kiai-launcher:active,
html body .kiai-chip:active:not(:disabled),
html body .kiai-add:active:not(:disabled),
html body .kiai-send:active:not(:disabled),
html body .kiai-cartlink:active,
html body .kiai-step:active:not(:disabled),
html body .kiai-new:active:not(:disabled),
html body .kiai-close:active { scale: 0.96; }

/* Focus ring: ink, 16:1 on white, 3.4:1 on the accent. */
html body .kiai-launcher:focus-visible,
html body .kiai-panel button:focus-visible,
html body .kiai-panel input:focus-visible,
html body .kiai-panel textarea:focus-visible,
html body .kiai-panel a:focus-visible { outline: 3px solid #1D1D1F; outline-offset: 2px; }

/* ---- Panel (the frosted shell) ---- */

html body .kiai-panel {
	position: fixed;
	right: var(--kiai-edge, 16px);
	bottom: calc(var(--kiai-lift, 16px) + 56px + 12px);
	z-index: 99999;
	width: 392px;
	max-width: calc(100vw - 24px);
	height: 640px;
	max-height: calc(100vh - var(--kiai-lift, 16px) - 56px - 12px - 16px);
	max-height: calc(100dvh - var(--kiai-lift, 16px) - 56px - 12px - 16px);
	display: flex;
	flex-direction: column;
	overflow: hidden;
	color-scheme: light;
	background: #F5F5F7; /* solid fallback */
	color: #1D1D1F;
	border-radius: 28px;
	box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.08), 0 24px 56px rgba(0, 0, 0, 0.12);
	font-size: 16px;
	line-height: 1.4;
	/* The closed state doubles as the exit: short and quick. The enter eases out like a settling spring, without bounce. */
	opacity: 0;
	translate: 0 16px;
	scale: 0.94;
	transform-origin: bottom right;
	transition-property: opacity, translate, scale;
	transition-duration: 220ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-panel.kiai-open {
	opacity: 1;
	translate: 0 0;
	scale: 1;
	transition-duration: 420ms;
}
html body .kiai-panel[hidden] { display: none; }

@supports ((-webkit-backdrop-filter: blur(1px)) or (backdrop-filter: blur(1px))) {
	html body .kiai-panel {
		background: rgba(245, 245, 247, 0.92);
		-webkit-backdrop-filter: saturate(180%) blur(24px);
		backdrop-filter: saturate(180%) blur(24px);
	}
	html body .kiai-head,
	html body .kiai-dock {
		background: rgba(255, 255, 255, 0.88);
		-webkit-backdrop-filter: saturate(180%) blur(20px);
		backdrop-filter: saturate(180%) blur(20px);
	}
}
@media (prefers-reduced-transparency: reduce) {
	html body .kiai-panel { background: #F5F5F7; -webkit-backdrop-filter: none; backdrop-filter: none; }
	html body .kiai-head,
	html body .kiai-dock { background: #FFFFFF; -webkit-backdrop-filter: none; backdrop-filter: none; }
}

/* ---- Header ---- */

html body .kiai-head {
	flex: none;
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 14px 10px 12px 18px;
	background: #FFFFFF; /* solid fallback */
	box-shadow: 0 0.5px 0 rgba(0, 0, 0, 0.1);
}
html body .kiai-avatar {
	flex: none;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 40px;
	height: 40px;
	border-radius: 50%;
	background: #E9E9EB;
	color: #1D1D1F;
	font-size: 14px;
	font-weight: 600;
}
html body .kiai-headtext { flex: 1; min-width: 0; display: flex; flex-direction: column; }
html body .kiai-title { font-size: 17px; font-weight: 600; line-height: 1.25; color: #1D1D1F; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
html body .kiai-sub { font-size: 13px; font-weight: 400; line-height: 1.3; color: #5C5C61; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* 44px hit area around a 30px grey circle. */
html body .kiai-new,
html body .kiai-close {
	position: relative;
	isolation: isolate;
	flex: none;
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	background: transparent;
	color: #1D1D1F;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition-property: scale;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-new:disabled { opacity: 0.4; cursor: not-allowed; }
html body .kiai-new::before,
html body .kiai-close::before {
	content: "";
	position: absolute;
	inset: 7px;
	z-index: -1;
	border-radius: 50%;
	background: #E9E9EB;
}
html body .kiai-close svg { width: 16px; height: 16px; }
html body .kiai-new svg { width: 17px; height: 17px; }

/* ---- Conversation ---- */

html body .kiai-log {
	flex: 1;
	overflow-y: auto;
	padding: 16px;
	display: flex;
	flex-direction: column;
	gap: 6px;
	scrollbar-width: thin;
	overscroll-behavior: contain;
}
html body .kiai-msg {
	max-width: 82%;
	padding: 10px 14px;
	border-radius: 20px;
	overflow-wrap: anywhere;
	font-size: 16px;
	line-height: 1.4;
	color: #1D1D1F;
}
html body .kiai-msg p { margin: 0 0 6px; }
html body .kiai-msg p:last-child { margin-bottom: 0; }
html body .kiai-msg ul { margin: 0 0 6px; padding-inline-start: 20px; }
html body .kiai-bot {
	align-self: flex-start;
	border-end-start-radius: 6px;
	background: #E9E9EB;
}
html body .kiai-user {
	align-self: flex-end;
	border-end-end-radius: 6px;
	background: var(--kiai-accent, #B84F08);
	color: #FFFFFF;
}
html body .kiai-error { color: #B3261E; }
html body .kiai-cartlink {
	display: inline-flex;
	align-items: center;
	min-height: 44px;
	margin: 6px 6px 0 0;
	padding: 0 18px;
	border-radius: 999px;
	background: #FFFFFF;
	color: #1D1D1F !important;
	font-size: 15px;
	font-weight: 600;
	text-decoration: none;
	box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
	transition-property: scale, background-color;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}

/* Progress: three grey dots plus a short message while the assistant works. */
html body .kiai-typing { display: flex; align-items: center; gap: 10px; padding: 12px 16px; }
html body .kiai-dots { display: inline-flex; gap: 4px; }
html body .kiai-dots span {
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: #8E8E93;
	animation: kiai-bounce 1s infinite ease-in-out;
}
html body .kiai-dots span:nth-child(2) { animation-delay: 0.15s; }
html body .kiai-dots span:nth-child(3) { animation-delay: 0.3s; }
html body .kiai-progress { font-size: 14px; color: #5C5C61; }
@keyframes kiai-bounce { 0%, 80%, 100% { transform: translateY(0); opacity: 0.5; } 40% { transform: translateY(-4px); opacity: 1; } }

/* ---- Product cards ---- */

html body .kiai-proposal {
	align-self: stretch;
	margin-top: 6px;
	background: #FFFFFF;
	border-radius: 24px;
	padding: 8px 12px 12px;
	box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.06);
	transition-property: opacity;
	transition-duration: 200ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
	animation: kiai-rise 240ms cubic-bezier(0.32, 0.72, 0, 1) backwards;
}
html body .kiai-proposal.kiai-done { opacity: 0.6; }
html body .kiai-proposal-title { margin: 8px 0 4px; font-size: 17px; font-weight: 600; color: #1D1D1F; }
html body .kiai-card {
	display: flex;
	flex-direction: column;
	gap: 8px;
	padding: 12px 0;
	border-top: 0.5px solid rgba(0, 0, 0, 0.12);
	animation: kiai-rise 240ms cubic-bezier(0.32, 0.72, 0, 1) backwards;
	animation-delay: calc(var(--i, 0) * 100ms + 100ms);
}
html body .kiai-card:first-of-type { border-top: 0; }
@keyframes kiai-rise { from { opacity: 0; translate: 0 8px; } to { opacity: 1; translate: 0 0; } }
html body .kiai-check { display: flex; align-items: center; gap: 12px; min-height: 44px; cursor: pointer; }
html body .kiai-check input { width: 24px; height: 24px; flex: none; margin: 0; accent-color: #B84F08; }
html body .kiai-check img {
	width: 72px;
	height: 72px;
	object-fit: cover;
	border-radius: 16px;
	flex: none;
	background: #F5F5F7;
	outline: 1px solid rgba(0, 0, 0, 0.1);
	outline: 1px solid oklch(0 0 0 / 0.1);
	outline-offset: -1px;
}
html body .kiai-info { min-width: 0; }
html body .kiai-name { display: block; color: #1D1D1F; font-size: 16px; font-weight: 600; line-height: 1.3; text-decoration: none; }
html body .kiai-price { margin-top: 2px; font-size: 16px; font-weight: 400; color: #1D1D1F; font-variant-numeric: tabular-nums; }
html body .kiai-note { margin-top: 2px; font-size: 13px; line-height: 1.3; color: #5C5C61; }

/* Quantity stepper: grey pill, 44px buttons. */
html body .kiai-stepper {
	align-self: flex-end;
	display: inline-flex;
	align-items: center;
	background: #F5F5F7;
	border-radius: 999px;
}
html body .kiai-step {
	flex: none;
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: #E9E9EB;
	color: #1D1D1F;
	font-size: 22px;
	font-weight: 400;
	line-height: 1;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition-property: scale, background-color, opacity;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-step:disabled { opacity: 0.35; cursor: not-allowed; }
html body .kiai-qty {
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	background: transparent;
	color: #1D1D1F;
	font-size: 17px;
	font-weight: 600;
	text-align: center;
	font-variant-numeric: tabular-nums;
	-moz-appearance: textfield;
	appearance: textfield;
}
html body .kiai-qty::-webkit-outer-spin-button,
html body .kiai-qty::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

html body .kiai-foot {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	padding-top: 12px;
	border-top: 0.5px solid rgba(0, 0, 0, 0.12);
}
html body .kiai-total { font-size: 17px; font-weight: 600; color: #1D1D1F; font-variant-numeric: tabular-nums; }
html body .kiai-add {
	min-height: 44px;
	padding: 0 22px;
	border: 0;
	border-radius: 16px;
	background: var(--kiai-accent, #B84F08);
	color: #FFFFFF;
	font-size: 16px;
	font-weight: 600;
	cursor: pointer;
	transition-property: scale, filter, opacity;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-add:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---- Dock: quick replies and composer ---- */

html body .kiai-dock { flex: none; background: #FFFFFF; box-shadow: 0 -0.5px 0 rgba(0, 0, 0, 0.1); }
html body .kiai-chips {
	display: flex;
	flex-wrap: nowrap;
	gap: 8px;
	padding: 10px 12px 8px; /* The ring is 3px + 2px offset; this row clips overflow, so keep 5px free all round. */
	overflow-x: auto;
	scrollbar-width: none;
	scroll-snap-type: x proximity;
	scroll-padding-inline: 12px; /* Keyboard focus scrolls a chip in with its ring (5px) still inside the panel. */
}
html body .kiai-chips::-webkit-scrollbar { display: none; }
/* Chips that do not fit scroll sideways; the fade on the cut-off edge says so. */
html body .kiai-chips.kiai-chips-more {
	-webkit-mask-image: linear-gradient(to right, #000 calc(100% - 36px), transparent);
	mask-image: linear-gradient(to right, #000 calc(100% - 36px), transparent);
}
html body .kiai-chips.kiai-chips-start:not(.kiai-chips-more) {
	-webkit-mask-image: linear-gradient(to right, transparent, #000 36px);
	mask-image: linear-gradient(to right, transparent, #000 36px);
}
html body .kiai-chips.kiai-chips-more.kiai-chips-start {
	-webkit-mask-image: linear-gradient(to right, transparent, #000 36px, #000 calc(100% - 36px), transparent);
	mask-image: linear-gradient(to right, transparent, #000 36px, #000 calc(100% - 36px), transparent);
}
html body .kiai-chip {
	flex: none;
	scroll-snap-align: start;
	min-height: 44px;
	padding: 0 16px;
	border: 0;
	border-radius: 999px;
	background: #E9E9EB;
	color: #1D1D1F;
	font-size: 15px;
	font-weight: 500;
	line-height: 1.2;
	cursor: pointer;
	transition-property: scale, background-color, opacity;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-chip:disabled { opacity: 0.5; cursor: not-allowed; }

html body .kiai-form { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px 12px; }
html body .kiai-input {
	flex: 1;
	min-height: 44px;
	max-height: 110px;
	padding: 10px 16px;
	border: 1px solid #8E8E93;
	border-radius: 22px;
	background: #FFFFFF;
	color: #1D1D1F;
	font-size: 16px;
	line-height: 1.3;
	resize: none;
	overflow-y: hidden; /* The script turns scrolling on once the field is taller than its maximum. */
}
html body .kiai-input::placeholder { color: #6E6E73; opacity: 1; }
html body .kiai-send {
	flex: none;
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: var(--kiai-accent, #B84F08);
	color: #FFFFFF;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition-property: scale, filter, opacity;
	transition-duration: 150ms;
	transition-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
html body .kiai-send svg { width: 20px; height: 20px; }
html body .kiai-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* The theme upper-cases and letter-spaces buttons. */
html body .kiai-add,
html body .kiai-send,
html body .kiai-chip,
html body .kiai-step,
html body .kiai-cartlink { text-transform: none; letter-spacing: normal; }

/* ---- Small screens ---- */

/* The theme shows a bottom toolbar on tablets and phones; without a go-up button to measure, clear it. */
@media (max-width: 1024px) {
	html body .kiai-launcher { bottom: var(--kiai-lift, 76px); }
	html body .kiai-panel {
		bottom: calc(var(--kiai-lift, 76px) + 56px + 12px);
		max-height: calc(100vh - var(--kiai-lift, 76px) - 56px - 12px - 16px);
		max-height: calc(100dvh - var(--kiai-lift, 76px) - 56px - 12px - 16px);
	}
}

/* Short windows (phones in landscape): use the full height, to the left of the launcher. */
@media (max-height: 520px) and (orientation: landscape) {
	html body .kiai-panel {
		top: calc(8px + env(safe-area-inset-top, 0px));
		bottom: 8px;
		right: calc(var(--kiai-edge, 12px) + 56px + 12px);
		width: min(392px, calc(100vw - 100px));
		height: auto;
		max-height: none;
	}
	html body .kiai-head { padding-top: 8px; padding-bottom: 8px; }
}

@media (max-width: 480px) {
	html body .kiai-panel {
		inset: 0;
		width: 100%;
		max-width: none;
		height: 100vh;
		height: 100dvh;
		max-height: none;
		border-radius: 0;
	}
	html body .kiai-head { padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
	html body .kiai-form { padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }
	html body .kiai-launcher { right: var(--kiai-edge, 12px); }
}

/* Reduced motion: state changes still apply, instantly. */
@media (prefers-reduced-motion: reduce) {
	html body .kiai-launcher,
	html body .kiai-launcher *,
	html body .kiai-panel,
	html body .kiai-panel * {
		transition: none !important;
		animation: none !important;
	}
}
