/* The widget, and each of its steps, is hidden with the [hidden] attribute until widget.js knows
   what to show. Layout rules below set display on several of these elements, so this has to win. */
.twb-chat[hidden],
.twb-chat [hidden],
.twb-float[hidden] {
	display: none !important;
}

.twb-chat-streaming::after {
	content: "▍";
	margin-left: 1px;
	animation: twb-blink 1s steps( 2 ) infinite;
}

@keyframes twb-blink {
	to { visibility: hidden; }
}

@media ( prefers-reduced-motion: reduce ) {
	.twb-chat-streaming::after { animation: none; }
}

.twb-chat {
	box-sizing: border-box;
	width: 100%;
	max-width: var( --twb-width, 640px );
	margin: 2em auto;
	border: 1px solid #ddd;
	border-radius: 12px;
	padding: 20px;
	font-family: inherit;
	font-size: var( --twb-font-size, 14px );
	background: #fafafa;
}

.twb-chat *,
.twb-chat *::before,
.twb-chat *::after {
	box-sizing: border-box;
}

.twb-chat-topbar {
	display: flex;
	align-items: center;
	gap: 10px;
	margin-bottom: 14px;
}

.twb-chat-heading {
	margin: 0;
	font-size: 1.2em;
}

.twb-chat-menu-toggle {
	display: none;
	border: 1px solid #ccc;
	background: #fff;
	border-radius: 6px;
	width: 32px;
	height: 32px;
	font-size: 16px;
	line-height: 1;
	cursor: pointer;
	flex-shrink: 0;
}

.twb-chat-body {
	display: flex;
	align-items: stretch;
	gap: 0;
	border: 1px solid #ddd;
	border-radius: 10px;
	background: #fff;
	overflow: hidden;
	position: relative;
}

.twb-chat-sidebar {
	width: 190px;
	flex: 0 0 190px;
	border-right: 1px solid #e5e5e5;
	background: #f6f6f6;
	padding: 10px;
	display: flex;
	flex-direction: column;
	gap: 8px;
	max-height: min( calc( var( --twb-height, 420px ) + 90px ), 75vh );
	overflow-y: auto;
}

.twb-chat-sidebar-head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
}

.twb-chat-sidebar-title {
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	font-weight: 700;
	color: #333;
}

/* Only meaningful where the sidebar is an overlay/panel that can be dismissed (mobile off-canvas,
   or the floating widget's history view below); on a permanent desktop column there's nothing to
   close, so it stays hidden there by default. */
.twb-chat-sidebar-close {
	display: none;
	border: none;
	background: none;
	cursor: pointer;
	font-size: 14px;
	line-height: 1;
	padding: 4px;
	color: #666;
	flex-shrink: 0;
}

.twb-chat-sidebar-close:hover {
	color: #111;
}

.twb-chat-new-chat {
	border: 1px solid #ccc;
	background: #fff;
	border-radius: 6px;
	padding: 8px 10px;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	font-weight: 600;
	cursor: pointer;
	text-align: left;
}

.twb-chat-sessions {
	display: flex;
	flex-direction: column;
	gap: 2px;
}

.twb-session-row {
	display: flex;
	align-items: center;
	gap: 4px;
}

.twb-session-item {
	flex: 1;
	min-width: 0;
	text-align: left;
	background: none;
	border: none;
	border-radius: 5px;
	padding: 7px 8px;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	color: #333;
	cursor: pointer;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.twb-session-item:hover,
.twb-session-item.twb-session-active {
	background: #e8e8e8;
}

.twb-session-item.twb-session-active {
	font-weight: 600;
}

.twb-session-rename {
	flex-shrink: 0;
	background: none;
	border: none;
	cursor: pointer;
	opacity: 0.5;
	padding: 4px;
	font-size: 12px;
}

.twb-session-rename:hover {
	opacity: 1;
}

.twb-sessions-empty {
	font-size: 12px;
	color: #888;
	padding: 6px 8px;
}

.twb-chat-main {
	flex: 1;
	min-width: 0;
	padding: 16px;
}

.twb-chat-sidebar-backdrop {
	display: none;
}

/* Mobile: sidebar becomes an off-canvas panel toggled by the menu button, rather than a
   permanent column that would otherwise eat most of a phone screen's width. */
@media ( max-width: 640px ) {
	.twb-chat-menu-toggle {
		display: block;
	}

	/* Clip the closed panel: it slides out past the left edge, which must not show. */
	.twb-chat-body {
		position: relative;
		overflow: hidden;
		overflow-x: clip;
		overflow-y: visible;
	}

	/* Room for the history list while it's open, even before any messages. */
	.twb-chat.twb-sidebar-open .twb-chat-body {
		min-height: 300px;
	}

	.twb-chat-sidebar {
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		z-index: 3;
		width: 78vw;
		max-width: 260px;
		max-height: none;
		border-radius: 10px 0 0 10px;
		box-shadow: 2px 0 12px rgba( 0, 0, 0, 0.15 );
		transform: translateX( -100% );
		/* Hidden once it has slid away, so it can't be seen, tabbed to or read out while closed. */
		visibility: hidden;
		transition: transform 0.2s ease, visibility 0s linear 0.2s;
	}

	.twb-chat-sidebar-close {
		display: inline-flex;
	}

	.twb-chat.twb-sidebar-open .twb-chat-sidebar {
		transform: translateX( 0 );
		visibility: visible;
		transition: transform 0.2s ease, visibility 0s linear 0s;
	}

	.twb-chat-sidebar-backdrop {
		display: none;
		position: absolute;
		inset: 0;
		background: rgba( 0, 0, 0, 0.25 );
		z-index: 2;
	}

	.twb-chat.twb-sidebar-open .twb-chat-sidebar-backdrop {
		display: block;
	}

	.twb-chat-main {
		padding: 14px;
	}
}

/* Tablet: dimensions still come from Settings, but capped so the widget never dominates a
   mid-size screen regardless of what desktop value is configured. */
@media ( max-width: 768px ) {
	.twb-chat {
		padding: 16px;
	}
}

/* Mobile: same idea, tighter cap, and less padding to leave more room for the conversation. */
@media ( max-width: 480px ) {
	.twb-chat {
		padding: 12px;
		border-radius: 8px;
	}
	.twb-chat-heading {
		font-size: 1.05em;
	}
}

.twb-chat .screen-reader-text {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

.twb-chat-messages {
	max-height: min( var( --twb-height, 420px ), 70vh );
	overflow-y: auto;
	margin-bottom: 14px;
}

@media ( max-width: 768px ) {
	.twb-chat-messages {
		max-height: min( var( --twb-height, 420px ), 340px, 60vh );
	}
}

@media ( max-width: 480px ) {
	.twb-chat-messages {
		max-height: min( var( --twb-height, 420px ), 280px, 50vh );
	}
}

.twb-msg {
	padding: 10px 14px;
	border-radius: 10px;
	margin-bottom: 10px;
	line-height: 1.5;
	max-width: 88%;
}

.twb-msg p {
	margin: 0 0 10px;
}

.twb-msg p:last-child {
	margin-bottom: 0;
}

.twb-msg ul {
	margin: 0 0 10px;
	padding-left: 1.3em;
}

.twb-msg ul:last-child {
	margin-bottom: 0;
}

.twb-msg li {
	margin-bottom: 4px;
}

.twb-msg li:last-child {
	margin-bottom: 0;
}

.twb-msg-user {
	background: var( --twb-user-bg, #2a2a2a );
	color: var( --twb-user-text, #ffffff );
	margin-left: auto;
	text-align: right;
}

.twb-msg-assistant {
	background: var( --twb-assistant-bg, #ffffff );
	border: 1px solid #e0e0e0;
	color: var( --twb-assistant-text, #222222 );
}

.twb-msg a {
	word-break: break-word;
}

/* Configurable on the Appearance tab. Scoped to bot messages only: a chosen colour could have poor
   contrast against the dark visitor bubble, so that side keeps a fixed white, underlined style. */
.twb-msg-assistant a {
	color: var( --twb-link-color, #111111 );
	background: var( --twb-link-bg, transparent );
	border: var( --twb-link-border-width, 0px ) solid var( --twb-link-border-color, transparent );
	border-radius: var( --twb-link-radius, 0px );
	padding: var( --twb-link-padding, 0px );
	font-weight: var( --twb-link-weight, 700 );
	text-decoration: var( --twb-link-decoration, underline );
	text-decoration-thickness: 2px;
	text-underline-offset: 2px;
	box-decoration-break: clone;
	-webkit-box-decoration-break: clone;
}

.twb-msg-assistant a:hover {
	opacity: 0.75;
}

.twb-msg-user a {
	color: #fff;
	font-weight: 700;
	text-decoration: underline;
	text-decoration-color: #fff;
	text-decoration-thickness: 2px;
	text-underline-offset: 2px;
}

.twb-msg-user a:hover {
	text-decoration-color: rgba( 255, 255, 255, 0.6 );
}

.twb-msg-thinking {
	opacity: 0.6;
	font-style: italic;
}

.twb-msg-error {
	background: #fdeaea;
	border: 1px solid #f3b8b8;
	color: #a12b2b;
}

.twb-msg-rating {
	margin-top: 8px;
	display: flex;
	gap: 4px;
}

.twb-rate-btn {
	background: none;
	border: 1px solid transparent;
	border-radius: 5px;
	padding: 2px 6px;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	cursor: pointer;
	opacity: 0.5;
	line-height: 1.4;
}

.twb-rate-btn:hover {
	opacity: 0.85;
}

.twb-rate-btn.twb-rate-active {
	opacity: 1;
	border-color: #ccc;
	background: #f0f0f0;
}

.twb-chat-form {
	display: flex;
	gap: 8px;
}

.twb-chat-input {
	flex: 1;
	resize: vertical;
	padding: 10px 12px;
	border: 1px solid #ccc;
	border-radius: 8px;
	font-family: inherit;
	font-size: var( --twb-font-size, 14px );
}

.twb-chat-submit {
	padding: 0 20px;
	border: none;
	border-radius: 8px;
	background: #111;
	color: #fff;
	font-weight: 600;
	cursor: pointer;
}

.twb-chat-submit:disabled {
	opacity: 0.5;
	cursor: default;
}

.twb-chat-verify {
	border: 1px solid #ddd;
	border-radius: 8px;
	padding: 14px;
	background: #fff;
}

.twb-verify-intro {
	margin: 0 0 10px;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	color: #444;
}

.twb-chat-verify input {
	display: block;
	width: 100%;
	box-sizing: border-box;
	padding: 8px 10px;
	margin-bottom: 8px;
	border: 1px solid #ccc;
	border-radius: 6px;
	font-family: inherit;
	font-size: var( --twb-font-size, 14px );
}

.twb-turnstile-widget {
	margin-bottom: 10px;
}

.twb-verify-send,
.twb-verify-confirm {
	padding: 8px 18px;
	border: none;
	border-radius: 6px;
	background: #111;
	color: #fff;
	font-weight: 600;
	cursor: pointer;
}

.twb-verify-send:disabled,
.twb-verify-confirm:disabled {
	opacity: 0.5;
	cursor: default;
}

.twb-verify-resend {
	background: none;
	border: none;
	color: #555;
	text-decoration: underline;
	cursor: pointer;
	padding: 8px 10px;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
}

.twb-verify-error {
	margin-top: 8px;
	color: #a12b2b;
	font-size: calc( var( --twb-font-size, 14px ) - 1px );
	min-height: 1em;
}

@media ( max-width: 480px ) {
	.twb-chat-form {
		flex-direction: column;
	}
	.twb-chat-submit {
		padding: 10px 20px;
	}
}

.twb-chat-powered-by {
	margin-top: 12px;
	text-align: center;
	font-size: 11px;
	color: #999;
}

.twb-chat-powered-by a {
	color: inherit;
	text-decoration: underline;
}

/* Email-list opt-in on the email step */
.twb-optin {
	display: flex;
	align-items: flex-start;
	gap: 8px;
	margin: 8px 0 10px;
	font-size: 13px;
	line-height: 1.4;
	cursor: pointer;
}

.twb-optin input {
	margin-top: 2px;
	flex-shrink: 0;
}

/* Talk to a person */
.twb-handoff-link {
	display: block;
	margin: 8px auto 0;
	background: none;
	border: 0;
	padding: 4px 8px;
	font: inherit;
	font-size: 13px;
	font-weight: 600;
	color: inherit;
	text-decoration: underline;
	text-underline-offset: 3px;
	cursor: pointer;
}

.twb-handoff {
	display: flex;
	flex-direction: column;
	gap: 6px;
	margin-top: 10px;
	padding: 14px;
	border: 1px solid #ddd;
	border-radius: 10px;
	background: #fafafa;
	font-size: 14px;
}

.twb-handoff[hidden] {
	display: none;
}

.twb-handoff-title {
	margin: 0;
	font-weight: 700;
	font-size: 15px;
}

.twb-handoff-intro {
	margin: 0 0 4px;
	font-size: 13px;
}

.twb-handoff label {
	font-weight: 600;
	font-size: 13px;
}

.twb-optional {
	font-weight: 400;
	opacity: 0.75;
}

.twb-handoff input,
.twb-handoff textarea {
	width: 100%;
	box-sizing: border-box;
	padding: 8px 10px;
	border: 1px solid #ccc;
	border-radius: 8px;
	font: inherit;
}

.twb-handoff-error {
	margin: 0;
	min-height: 1em;
	color: #b91c1c;
	font-size: 13px;
}

.twb-handoff-actions {
	display: flex;
	gap: 8px;
	align-items: center;
}

.twb-handoff-send {
	padding: 9px 16px;
	border: 0;
	border-radius: 8px;
	background: var( --twb-user-bg, #111 );
	color: var( --twb-user-text, #fff );
	font: inherit;
	font-weight: 700;
	cursor: pointer;
}

.twb-handoff-cancel {
	background: none;
	border: 0;
	font: inherit;
	text-decoration: underline;
	cursor: pointer;
}

/* Starter questions in an empty chat */
.twb-starters {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	padding: 4px 0;
}

.twb-starter {
	font: inherit;
	font-size: 13px;
	text-align: left;
	padding: 8px 12px;
	border: 1px solid #d4d4d4;
	border-radius: 999px;
	background: #fff;
	color: inherit;
	cursor: pointer;
	transition: border-color 0.15s ease, background 0.15s ease;
}

.twb-starter:hover {
	border-color: var( --twb-user-bg, #111 );
	background: #f6f6f6;
}
