@use './tokens' as *;
@use './confirm-dialog';

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

// Comments sidebar — Gutenberg-style right-edge panel, open by default
// on desktop. Was previously left-anchored; moved to the right to match
// the WP block-editor Settings sidebar layout.
:host {
	display: block;
	position: fixed;
	top: $bar-height;
	right: 0;
	width: $sidebar-width;
	// The Free-build review-page upsell bar is fixed to the bottom of the
	// viewport at 40px tall. When it's mounted it sets
	// `--flow-ew-upsell-bar-height` on `:root`; we subtract it here so the
	// sidebar content isn't hidden behind it. Fallback `0px` keeps the layout
	// unchanged when the bar
	// isn't present (Pro build, non-admin viewer, dismissed).
	height: calc(100vh - #{$bar-height} - var(--flow-ew-upsell-bar-height, 0px));
	z-index: $z-sidebar;
	overflow: hidden;
	background: $color-bg-white;
	border-left: 1px solid $color-border;
	box-shadow: -1px 0 0 0 rgba(0, 0, 0, 0.05);
	font-family: $font-stack;
	font-size: $font-size-base;
	line-height: $line-height-base;
	color: $color-text;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
	transform: translateX(0);
	opacity: 1;
	transition: transform 180ms ease, opacity 180ms ease;
	will-change: transform, opacity;
}

// Mobile: sidebar covers the viewport width so it's readable on phones; the
// bar above stays visible so the toggle remains reachable to close it.
@media (max-width: $mobile-bp) {
	:host {
		width: 100vw;
		max-width: 100vw;
		border-left: 0;
	}
}

:host([data-open='false']) {
	transform: translateX(100%);
	opacity: 0;
	pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
	:host {
		transition: none;
	}
}

button,
input,
select,
textarea {
	font-family: inherit;
	font-size: inherit;
	line-height: inherit;
	color: inherit;
}

.components-button {
	font-weight: 600;
}

// =============================================================================
// Sidebar shell
// =============================================================================

.flow-sidebar {
	display: flex;
	flex-direction: column;
	height: 100%;

	&__tablist-and-close {
		display: flex;
		flex-direction: column;
		height: 100%;
		position: relative;
	}

	// Gutenberg-style panel header — tabs on the left, close X on the
	// right, thin bottom border, ~48px tall. Same shape as the block
	// editor's `.interface-complementary-area-header` so the sidebar
	// reads as the WP Settings panel users already know.
	&__header {
		flex: 0 0 auto;
		display: flex;
		align-items: stretch;
		gap: 0;
		height: 48px;
		padding: 0 8px 0 0;
		border-bottom: 1px solid $color-border;
		background: $color-bg-white;
	}

	&__header-close.components-button {
		flex: 0 0 auto;
		align-self: center;
		width: 36px;
		min-width: 36px;
		height: 36px;
		padding: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		color: $color-text;

		.components-button__icon,
		svg {
			margin: 0;
			fill: currentColor;
		}
	}

	&__tab-panel {
		display: flex;
		flex-direction: column;
		flex: 1 1 auto;
		min-height: 0;
	}

	// Tab list lives inside the header now — fills the remaining width
	// next to the close button, no bottom border (the header has its own
	// divider) and no own background.
	&__tabs {
		flex: 1 1 auto;
		display: flex;
		align-items: stretch;
	}

	&__tab {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		min-width: 144px;
		padding: 10px 16px;
		background: none;
		border: 0;
		border-bottom: 2px solid transparent;
		font-family: inherit;
		font-size: $font-size-base;
		font-weight: 500;
		color: $color-muted;
		cursor: pointer;
		white-space: nowrap;
		transition: color $transition-fast ease, border-color $transition-fast ease;

		&:hover {
			color: $color-text;
		}

		&--active {
			color: $color-text;
			font-weight: 600;
			border-bottom-color: $color-accent;
		}
	}

	&__tab-label {
		vertical-align: middle;
	}

	&__tab-count {
		box-sizing: border-box;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		margin-left: 6px;
		min-width: 18px;
		height: 18px;
		padding: 0 6px;
		border-radius: 999px;
		font-size: 10px;
		font-weight: 700;
		line-height: 1;
		font-variant-numeric: tabular-nums;
		color: #fff;
		background: $error-text;
		border: 0;

		&--comments {
			background: var(--wp-admin-theme-color, #{$color-accent});
		}

		&--resolved {
			background: #458037;
		}
	}

	&__tab:not(&__tab--active) &__tab-count {
		background: $color-border;
		color: $color-muted;
	}

	&__section-divider {
		flex-shrink: 0;
		border-top: 2px solid $color-border;
	}

	&__tab-scroll {
		display: flex;
		flex-direction: column;
		flex-grow: 1;
		overflow-y: auto;
		scrollbar-gutter: auto;
	}

	&__tab-content {
		display: flex;
		flex-direction: column;
		flex-grow: 1;
		min-height: 0;
	}

	&__body {
		padding: 12px;
		flex-shrink: 0;
	}

	&__placeholder {
		padding: 24px 16px;
		color: $color-muted;
		font-size: $font-size-base;
		text-align: center;
	}
}


// =============================================================================
// Inline comments empty state (Comments tab)
// =============================================================================

.flow-inline-comments-empty {
	padding: 20px 16px 24px;
	text-align: left;
	color: $color-text;
	border-top: 1px solid $color-border;

	&__icon-wrap {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 48px;
		height: 48px;
		margin: 0 auto 14px;
		border-radius: 50%;
		background: #f0f6fc;
		color: $color-accent;

		svg {
			fill: currentColor;
		}
	}

	&__title {
		margin: 0 0 8px;
		font-size: 15px;
		font-weight: 600;
		line-height: 1.3;
		text-align: center;
		color: $color-text;
	}

	&__lead {
		margin: 0 0 14px;
		font-size: $font-size-base;
		line-height: $line-height-base;
		color: $color-text;
		text-align: center;
	}

	&__list {
		margin: 0;
		padding: 0 0 0 1.1em;
		font-size: 12px;
		line-height: 1.5;
		color: $color-muted;

		li {
			margin-bottom: 8px;

			&:last-child {
				margin-bottom: 0;
			}
		}
	}
}

// Text action buttons — borderless, no fill (Reply).
.flow-btn--text.components-button {
	background: transparent !important;
	border: none !important;
	box-shadow: none !important;
	color: var(--wp-admin-theme-color, #007cba);

	&:hover:not(:disabled),
	&:active:not(:disabled) {
		color: var(--wp-admin-theme-color, #007cba);
		background: #f0f6fc !important;
	}

	&:focus:not(:disabled) {
		box-shadow: 0 0 0 var(--wp-admin-border-width-focus, 2px) #fff,
			0 0 0 calc(var(--wp-admin-border-width-focus, 2px) + 1px) var(--wp-admin-theme-color, #007cba) !important;
		outline: 1px solid transparent;
	}

	&:disabled,
	&[aria-disabled='true'] {
		background: transparent !important;
		border: none !important;
		color: #8c8f94 !important;
		opacity: 1 !important;
	}
}

// Cancel — text button with underline.
.flow-btn--cancel.components-button {
	min-width: 80px !important;
	text-decoration: underline;
	text-underline-offset: 2px;

	&:hover:not(:disabled),
	&:active:not(:disabled) {
		text-decoration: none;
	}
}

// Secondary submit — blue border only, no fill (Add Comment, Reply, Save).
.flow-comment-editor__submit-btn.components-button {
	background: transparent !important;
	border: 1px solid var(--wp-admin-theme-color, #007cba) !important;
	color: var(--wp-admin-theme-color, #007cba) !important;
	box-shadow: none !important;

	&:hover:not(:disabled),
	&:active:not(:disabled) {
		background: #f0f6fc !important;
		border-color: var(--wp-admin-theme-color, #007cba) !important;
		color: var(--wp-admin-theme-color, #007cba) !important;
	}

	&:focus:not(:disabled) {
		box-shadow: 0 0 0 var(--wp-admin-border-width-focus, 2px) #fff,
			0 0 0 calc(var(--wp-admin-border-width-focus, 2px) + 1px) var(--wp-admin-theme-color, #007cba) !important;
		outline: 1px solid transparent;
	}

	&:disabled,
	&[aria-disabled='true'] {
		background: transparent !important;
		border-color: #dcdcde !important;
		color: #757575 !important;
		opacity: 1 !important;
	}
}

// =============================================================================
// Comment threads
// =============================================================================

.flow-comment-threads {
	display: flex;
	flex-direction: column;
	gap: 12px;
	padding: 12px;
}

// General (non-inline) threads in the sidebar — same hover as inline cards.
.flow-comment-threads > .flow-comment-thread:hover {
	border-color: #666a70;
}

.flow-comment-thread {
	border: 1px solid $color-border;
	border-radius: 4px;
	background: $color-bg-white;
	overflow: hidden;
	transition: border-color $transition-fast ease;
	--flow-comment-fade-bg: #{$color-bg-white};

	&__replies {
		padding: 0 12px 4px 12px;
		margin: 0 0 0 6px;
		border-left: 1px solid $color-border;
	}

	&__toggle {
		display: block;
		padding: 4px 0 8px;
		background: none;
		border: 0;
		color: $color-accent;
		font-size: 12px;
		font-weight: 400;
		cursor: pointer;
		text-decoration: underline;
		text-underline-offset: 2px;

		&:hover {
			text-decoration: underline;
		}
	}

	&__reply-editor {
		padding: 0 12px 12px;
	}

	&__resolve-row {
		padding: 12px 0;
		display: flex;
		justify-content: flex-start;

		.components-button {
			min-height: 30px;
			margin-left: 0;
		}
	}

	&__reply-btn.components-button.has-icon.has-text {
		margin-left: 12px;
		margin-right: 12px;
		gap: 8px !important;
		border-radius: $radius-sm;
		text-decoration: underline;
		text-underline-offset: 2px;
		transition: background-color 0.15s ease;

		&:hover:not(:disabled),
		&:focus-visible:not(:disabled) {
			background: #e8edfc !important;
			text-decoration: none;
		}

		svg,
		.components-button__icon {
			margin-right: 0 !important;
		}
	}
}

// =============================================================================
// Comment card
// =============================================================================

.flow-comment-card {
	padding: 12px;

	// Header layout — 3-col grid so author + date can stack in column 2
	// while the avatar (col 1) and the actions cluster (col 3) span both
	// rows. Frees up horizontal room in the comment row, which mattered
	// once we started showing longer author display names + timestamps.
	&__header {
		display: grid;
		grid-template-columns: auto minmax(0, 1fr) auto;
		grid-template-rows: auto auto;
		column-gap: 8px;
		row-gap: 1px;
		align-items: center;
		margin-bottom: 6px;
	}

	&__avatar {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 28px;
		height: 28px;
		border-radius: 50%;
		font-size: 11px;
		font-weight: 600;
		line-height: 1;
		user-select: none;
		grid-column: 1;
		grid-row: 1 / span 2;
		align-self: center;
	}

	&__avatar--fallback {
		background: #dcdcde;
		color: #757575;
	}

	&__avatar--img {
		display: block;
		object-fit: cover;
		background: transparent;
	}

	&__author {
		font-weight: 600;
		font-size: 12px;
		color: $color-text;
		white-space: nowrap;
		// Author still ellipsizes when the display name is huge.
		min-width: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		grid-column: 2;
		grid-row: 1;
	}

	&__date {
		font-size: 11px;
		color: $color-muted;
		white-space: nowrap;
		line-height: 1.2;
		grid-column: 2;
		grid-row: 2;
	}

	&__actions {
		display: flex;
		align-items: center;
		gap: 4px;
		grid-column: 3;
		grid-row: 1 / span 2;
		align-self: center;
	}

	&__action-btn.components-button {
		min-width: 0 !important;
		width: 28px !important;
		height: 28px !important;
		padding: 0 !important;
		color: $color-muted;
		border-radius: $radius-sm;

		&:hover:not(:disabled) {
			color: $color-text;
			background: #f0f0f0;
		}

		&.flow-comment-card__action-btn--destructive {
			color: #d63638;
		}

		&.flow-comment-card__action-btn--destructive:hover:not(:disabled) {
			color: #b32d2e;
			background: #fcf0f1;
		}

		svg {
			width: 20px;
			height: 20px;
		}
	}

	&__menu {
		display: flex;
		align-items: center;
		gap: 4px;
	}

	&__action-icon.components-button {
		min-width: 0 !important;
		width: 24px !important;
		height: 24px !important;
		padding: 0 !important;
		color: #1e1e1e !important;
		background: transparent !important;
		border: none !important;
		box-shadow: none !important;
		border-radius: $radius-sm;
		transition: background-color 0.15s ease;

		svg {
			width: 24px;
			height: 24px;
		}

		&:hover:not(:disabled),
		&:focus-visible:not(:disabled),
		&:active:not(:disabled) {
			color: #1e1e1e !important;
			box-shadow: none !important;
		}
	}

	&__action-icon--edit.components-button,
	&__action-icon--destructive.components-button,
	&__action-icon--close.components-button,
	&__menu-trigger.components-button {
		width: 32px !important;
		height: 32px !important;
		padding: 4px !important;

		svg {
			width: 20px;
			height: 20px;
		}
	}

	&__action-icon--edit.components-button {
		&:hover:not(:disabled),
		&:focus-visible:not(:disabled) {
			background: #e8edfc !important;
		}
	}

	&__action-icon--destructive.components-button {
		color: #c92222 !important;

		&:hover:not(:disabled),
		&:focus-visible:not(:disabled),
		&:active:not(:disabled) {
			color: #c92222 !important;
			background: #ffeaea !important;
		}
	}

	&__action-icon--close.components-button {
		&:hover:not(:disabled),
		&:focus-visible:not(:disabled) {
			background: #ebebeb !important;
		}
	}

	&__menu-trigger.components-button {
		&:hover:not(:disabled),
		&:focus-visible:not(:disabled),
		&:active:not(:disabled) {
			color: #1e1e1e !important;
			background: #ebebeb !important;
			box-shadow: none !important;
		}
	}

	&__resolve-icon.components-button {
		min-width: 0 !important;
		width: 32px !important;
		height: 32px !important;
		padding: 4px !important;
		color: #458037 !important;
		background: transparent !important;
		border: none !important;
		box-shadow: none !important;
		border-radius: $radius-sm !important;
		transition: background-color 0.15s ease;

		svg {
			width: 20px;
			height: 20px;

			.flow-comment-resolve__default,
			.flow-comment-resolve__hover {
				transition: opacity 0.15s ease;
			}

			.flow-comment-resolve__hover {
				opacity: 0;
			}
		}

		&:hover:not(:disabled),
		&:focus-visible:not(:disabled),
		&:active:not(:disabled) {
			color: #458037 !important;
			background: #e7f5e4 !important;
			box-shadow: none !important;

			svg .flow-comment-resolve__default {
				opacity: 0;
			}

			svg .flow-comment-resolve__hover {
				opacity: 1;
			}
		}
	}

	&__action-btn--collapse.components-button {
		min-width: 0 !important;
		width: 32px !important;
		height: 32px !important;
		padding: 4px !important;
		color: #1e1e1e;
		border-radius: $radius-sm;
		transition: background-color 0.15s ease;

		&:hover:not(:disabled),
		&:focus-visible:not(:disabled) {
			color: #1e1e1e;
			background: #ebebeb !important;
		}

		svg {
			width: 20px;
			height: 20px;
		}
	}

	&__action-btn--reply.components-button {
		opacity: 1;
		color: var(--wp-admin-theme-color, #007cba);

		&:hover:not(:disabled) {
			color: var(--wp-admin-theme-color, #007cba);
			background: #f0f6fc;
		}
	}

	&__resolved-icon {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		width: 24px;
		height: 24px;
		flex-shrink: 0;

		svg {
			width: 24px;
			height: 24px;
		}
	}

	&__body {
		font-size: $font-size-base;
		line-height: 1.6;
		color: $color-text;
		word-break: break-word;

		> * { margin: 0; }
		> * + * { margin-top: 0.4em; }

		strong { font-weight: 600; }
		em     { font-style: italic; }

		ul,
		ol {
			padding-left: 1.4em;
			margin: 0.3em 0;
		}

		a {
			color: $color-accent;
			text-decoration: underline;
		}

		h1 { font-size: 1.3em; font-weight: 600; }
		h2 { font-size: 1.1em; font-weight: 600; }
		h3 { font-size: 1em;   font-weight: 600; }

		&--truncated {
			max-height: 80px;
			overflow: hidden;
			position: relative;
			cursor: pointer;

			&::after {
				content: '';
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				height: 24px;
				background: linear-gradient(transparent, var(--flow-comment-fade-bg));
				pointer-events: none;
			}
		}
	}

	&--reply {
		padding: 8px 0 4px;
	}
}

// =============================================================================
// Inline comment anchor snippet (in sidebar thread header)
// =============================================================================

.flow-inline-anchor {
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	gap: 2px;
	width: 100%;
	padding: 10px 16px;
	margin: 0;
	background: $color-bg-subtle;
	border: 0;
	color: $color-accent;
	font-size: 12px;
	font-style: normal;
	line-height: 1.5;
	text-align: left;
	overflow: hidden;
	font-family: inherit;
	text-decoration: underline;
	text-underline-offset: 2px;
	pointer-events: none;

	&__text {
		display: block;
		width: 100%;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	&__hint {
		display: inline-block;
		font-size: 11px;
		font-style: normal;
		font-weight: 600;
		color: #7a5a00;
	}

	&--outdated {
		background: #fff6cc;
		color: #7a5a00;
	}
}

.flow-inline-thread--navigable:hover .flow-inline-anchor {
	background: #eef3f8;

	&--outdated {
		background: #ffefb3;
	}
}

.flow-inline-thread {
	border: 1px solid $color-border;
	border-radius: 4px;
	background: $color-bg-white;
	overflow: hidden;
	transition: border-color $transition-fast ease;

	&--navigable {
		cursor: pointer;

		&:hover {
			border-color: #666a70;
		}
	}

	.flow-comment-thread {
		border: none;
		border-radius: 0;
		background: transparent;
	}

	&--outdated {
		border-color: #e5c453;
		background: #fff6cc;

		&.flow-inline-thread--navigable:hover {
			border-color: #666a70;
		}

		.flow-comment-thread__replies {
			border-left-color: #e5c453;
		}
	}
}

// =============================================================================
// Rich-text comment editor
// =============================================================================

.flow-comment-editor {
	$editor-border-color: #1d2327;

	display: flex;
	flex-direction: column;

	&__toolbar {
		display: flex;
		align-items: stretch;
		gap: 0;
		padding: 0;
		background: $color-bg-white;
		border: 1px solid $editor-border-color;
		border-bottom: 0;
		border-radius: 2px 2px 0 0;
		overflow-x: auto;
		overflow-y: hidden;
		scrollbar-gutter: stable;

		.components-button {
			min-width: 32px !important;
			width: auto !important;
			height: 36px !important;
			padding: 0 6px !important;
			border-radius: 0;
			border-left: 1px solid $editor-border-color;
			color: $color-text;
			box-shadow: none;
			flex-shrink: 0;

			&:hover:not(:disabled) {
				background: #f0f0f0;
			}

			&.is-pressed:not(:disabled) {
				background: #f0f6fc;
				color: var(--wp-admin-theme-color, #007cba);
			}
		}

		.components-button.is-small,
		.components-button[data-size="small"] {
			min-width: 32px !important;
			width: auto !important;
			height: 36px !important;
			padding: 0 6px !important;
			border-radius: 0;
		}
	}

	&__toolbar-group {
		display: flex;
		align-items: stretch;
		flex-shrink: 0;
		border-left: 1px solid $editor-border-color;

		.components-button {
			border-left: 0;
		}
	}

	&__block-select {
		height: 36px;
		padding: 0 10px;
		border: 0;
		border-right: 0;
		border-radius: 0;
		background: $color-bg-white;
		font-size: 12px;
		cursor: pointer;
		flex-shrink: 0;
		min-width: 84px;

		&:hover { background: #f0f0f0; }

		&:focus {
			outline: none;
			box-shadow: inset 0 0 0 2px var(--wp-admin-theme-color, #007cba);
		}
	}

	&__toolbar-sep {
		display: none;
	}

	&__content {
		border: 1px solid $editor-border-color;
		border-radius: 0 0 $radius-sm $radius-sm;
		background: $color-bg-white;
		cursor: text;

		&:focus-within {
			border-color: $color-accent;
			box-shadow: 0 0 0 1px #{$color-accent};
		}

		.ProseMirror {
			position: relative;
			min-height: 75px;
			max-height: 280px;
			overflow-y: auto;
			padding: 8px 10px;
			line-height: 1.6;
			outline: none;
			word-break: break-word;

			p.is-editor-empty:first-child::before {
				content: attr(data-placeholder);
				color: $color-placeholder;
				pointer-events: none;
				float: left;
				height: 0;
			}

			ul,
			ol {
				padding-left: 1.5em;
				margin: 0.3em 0;
			}

			li { margin: 0.1em 0; }

			a {
				color: $color-accent;
				text-decoration: underline;
			}

			strong { font-weight: 600; }
			em     { font-style: italic; }

			h1,
			h2,
			h3 {
				font-weight: 600;
				line-height: 1.3;
				margin: 0.5em 0 0.25em;
			}

			h1 { font-size: 1.4em; }
			h2 { font-size: 1.2em; }
			h3 { font-size: 1.05em; }
		}
	}

	&--basic &__content {
		border-radius: $radius-sm;
	}

	&__textarea {
		display: block;
		width: 100%;
		min-height: 75px;
		max-height: 280px;
		margin: 0;
		padding: 8px 10px;
		border: 0;
		border-radius: inherit;
		background: $color-bg-white;
		font-family: inherit;
		font-size: 13px;
		line-height: 1.6;
		resize: vertical;
		box-sizing: border-box;
		outline: none;
		color: $color-text;

		&::placeholder {
			color: $color-placeholder;
		}
	}

	&__link-row {
		display: flex;
		align-items: center;
		gap: 4px;
		padding: 4px 6px;
		background: $color-bg-subtle;
		border: 1px solid $editor-border-color;
		border-top: none;
		border-bottom: none;
	}

	&__link-input {
		flex: 1;
		height: 28px;
		padding: 0 6px;
		border: 1px solid $color-border-input;
		border-radius: $radius-sm;
		background: $color-bg-white;
		font-size: 12px;

		&:focus {
			outline: none;
			border-color: $color-accent;
			box-shadow: 0 0 0 1px #{$color-accent};
		}
	}

	&__submit-row {
		display: flex;
		gap: 8px;
		margin-top: 10px;
		justify-content: flex-start;
		flex-wrap: wrap;

		.flow-comment-editor__submit-btn.components-button {
			flex: 0 0 auto;
			min-width: 120px;
			justify-content: center;
		}

		.flow-btn--cancel.components-button {
			flex: 0 0 auto;
			min-width: 80px !important;
			justify-content: center;
		}

		.flow-comment-editor__submit-btn:disabled,
		.flow-comment-editor__submit-btn[aria-disabled='true'] {
			background: transparent !important;
			border-color: #dcdcde !important;
			color: #757575 !important;
			opacity: 1 !important;
		}
	}

	&__error {
		margin: 8px 0 0;
		padding: 6px 10px;
		background: $error-bg;
		border: 1px solid $error-border;
		border-radius: $radius-sm;
		color: $error-text;
		font-size: 12px;
	}
}
