<script lang="ts">
	import Chevron from './ic/Chevron.svelte';
	import Link from './ic/Link.svelte';
	import Close from './ic/Close.svelte';
	import Clipboard from './ic/Clipboard.svelte';
	import Spark from './ic/Spark.svelte';

	interface Props {
		onBack?: () => void;
		onSubmit?: (url: string) => void;
		initialUrl?: string;
	}
	let { onBack, onSubmit, initialUrl = '' }: Props = $props();

	let url = $state(initialUrl);
	let focused = $state(false);
	let pasting = $state(false);

	const valid = $derived(/https?:\/\/(www\.)?instagram\.com\//.test(url));

	async function handlePaste() {
		pasting = true;
		try {
			const text = await navigator.clipboard?.readText();
			if (text) url = text;
		} catch {
			// clipboard access denied — do nothing
		} finally {
			setTimeout(() => (pasting = false), 200);
		}
	}
</script>

<div class="screen">
	<!-- Back bar -->
	<div class="back-bar">
		<button class="ic-btn-reset back-btn" onclick={onBack} aria-label="Back">
			<Chevron size={18} dir="left" color="var(--ink)" />
		</button>
		<div class="screen-title">Add a recipe</div>
		<div style="width: 40px;"></div>
	</div>

	<div class="content">
		<div class="step-label">STEP 01 · PASTE</div>
		<h1 class="ic-display headline">
			Drop the<br />
			<span class="ic-grad-text">Instagram</span><br />
			link here.
		</h1>
		<p class="sub-text">
			Reels, posts, carousels — anything with a recipe in the caption. We'll cook it down into
			something searchable.
		</p>

		<!-- URL input -->
		<div class="input-ring" class:focused>
			<div class="input-inner">
				<Link size={18} color="var(--muted)" />
				<input
					type="url"
					placeholder="instagram.com/reel/..."
					bind:value={url}
					onfocus={() => (focused = true)}
					onblur={() => (focused = false)}
					class="url-input"
				/>
				{#if url}
					<button class="ic-btn-reset clear-btn" onclick={() => (url = '')} aria-label="Clear">
						<Close size={12} color="var(--muted)" />
					</button>
				{/if}
			</div>
		</div>

		<!-- Paste button -->
		<button class="ic-btn-reset paste-btn" onclick={handlePaste} style="opacity: {pasting ? 0.6 : 1}">
			<Clipboard size={16} color="var(--ink-2)" /> Paste from clipboard
		</button>

		<!-- Pro tip -->
		<div class="hint-card">
			<div class="hint-title">
				<Spark size={12} color="var(--purple)" /> Pro move
			</div>
			<div class="hint-body">
				Add InstaChef to your share sheet and you can send recipes here straight from the Instagram
				app — no copy-paste required.
			</div>
		</div>
	</div>

	<!-- Submit button -->
	<div class="submit-wrap">
		<button
			class="ic-btn-reset submit-btn"
			onclick={() => valid && onSubmit?.(url)}
			disabled={!valid}
			style="
				background: {valid ? 'var(--brand-gradient)' : 'var(--surface-3)'};
				color: {valid ? '#fff' : 'var(--muted-2)'};
				box-shadow: {valid ? 'var(--shadow-lg)' : 'none'};
				cursor: {valid ? 'pointer' : 'not-allowed'};
				opacity: {valid ? 1 : 0.85};
			"
		>
			Start cooking <Chevron size={16} color={valid ? '#fff' : 'var(--muted-2)'} dir="right" />
		</button>
	</div>
</div>

<style>
	.screen {
		min-height: 100%;
		background: var(--bg);
		display: flex;
		flex-direction: column;
	}
	.back-bar {
		position: sticky;
		top: 0;
		z-index: 5;
		background: color-mix(in srgb, var(--bg) 92%, transparent);
		backdrop-filter: blur(14px) saturate(160%);
		-webkit-backdrop-filter: blur(14px) saturate(160%);
		padding: 60px 16px 12px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 12px;
		border-bottom: 1px solid var(--border);
	}
	.back-btn {
		width: 40px;
		height: 40px;
		border-radius: 12px;
		background: var(--surface-2);
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.screen-title {
		flex: 1;
		font-size: 16px;
		font-weight: 700;
		text-align: center;
		font-family: 'Lilita One', system-ui, sans-serif;
		letter-spacing: -0.005em;
		color: var(--ink);
	}
	.content {
		padding: 24px 22px;
		flex: 1;
	}
	.step-label {
		font-size: 11px;
		font-family: var(--font-mono);
		font-weight: 700;
		letter-spacing: 1.2px;
		text-transform: uppercase;
		color: var(--pink);
		margin-bottom: 10px;
	}
	.headline {
		font-size: 44px;
		line-height: 0.95;
		margin: 0 0 14px;
		letter-spacing: -0.01em;
		color: var(--ink);
	}
	.sub-text {
		font-size: 14px;
		color: var(--muted);
		line-height: 1.45;
		margin: 0 0 26px;
	}
	.input-ring {
		border-radius: 20px;
		padding: 2px;
		background: var(--border-strong);
		transition: background 0.2s;
	}
	.input-ring.focused {
		background: var(--brand-gradient);
	}
	.input-inner {
		background: var(--surface);
		border-radius: 18px;
		padding: 14px 16px;
		display: flex;
		align-items: center;
		gap: 10px;
	}
	.url-input {
		flex: 1;
		border: 0;
		outline: 0;
		background: transparent;
		font-family: var(--font-mono);
		font-size: 14px;
		color: var(--ink);
		min-width: 0;
	}
	.url-input::placeholder {
		color: var(--muted-2);
	}
	.clear-btn {
		width: 22px;
		height: 22px;
		border-radius: 99px;
		background: var(--surface-3);
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.paste-btn {
		width: 100%;
		margin-top: 10px;
		padding: 12px 16px;
		border-radius: 14px;
		background: var(--surface-2);
		color: var(--ink-2);
		font-size: 13px;
		font-weight: 600;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 8px;
		transition: opacity 0.15s;
	}
	.hint-card {
		margin-top: 26px;
		padding: 16px;
		background: var(--surface);
		border: 1px solid var(--border);
		border-radius: 18px;
	}
	.hint-title {
		display: flex;
		align-items: center;
		gap: 6px;
		font-size: 11px;
		font-weight: 700;
		font-family: var(--font-mono);
		color: var(--purple);
		letter-spacing: 1.2px;
		text-transform: uppercase;
		margin-bottom: 8px;
	}
	.hint-body {
		font-size: 13px;
		color: var(--ink-2);
		line-height: 1.4;
	}
	.submit-wrap {
		position: sticky;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 12px 16px 34px;
		background: linear-gradient(to top, var(--bg) 60%, transparent);
		z-index: 40;
	}
	.submit-btn {
		width: 100%;
		padding: 16px 20px;
		border-radius: 99px;
		font-size: 15px;
		font-weight: 700;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10px;
		transition: opacity 0.15s;
	}
</style>
