:root{
	--sui-bg: rgba(18, 12, 10, 0.48);
	--sui-border: rgba(255, 236, 190, 0.18);
	--sui-text: rgba(255,255,255,0.92);
	--sui-muted: rgba(255,255,255,0.72);
	--sui-gold: #f6d36b;
	--sui-gold2:#ffefb0;
	--sui-shadow: 0 20px 55px rgba(0,0,0,0.45);
}

*{ box-sizing: border-box; }
html, body{ height: 100%; }
body{ margin: 0; color: var(--sui-text); font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; background: #070707; overflow-x: hidden; }

/* Background mount: supports video or image */
.scratch-bg{
	position: fixed;
	inset: 0;
	z-index: 0;
	pointer-events: none;
	background-position: center;
	background-size: cover;
	background-repeat: no-repeat;
}

.scratch-bg video,
.scratch-bg img{
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

/* App wrapper: all editor variables live here */
.scratch-app{
	min-height: 100svh;
	display: grid;
	grid-template-rows: auto 1fr auto auto;
	gap: 14px;
	padding: 14px;
	padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
	position: relative;
	z-index: 1;
	font-family: var(--aps-font-family, inherit);

	/* Stage controls (editor-driven) */
	--stage-top-offset: 0px;
	--stage-max-width: 560px;
	--stage-action-gap: 12px;

	/* Ticket controls */
	--ticket-top: 50%;
	--ticket-left: 50%;
	--ticket-w: 100%;
	--ticket-h: auto;
	--ticket-rotate: 0deg;

	/* Scratch surface controls */
	--surface-top: 50%;
	--surface-left: 50%;
	--surface-w: 86%;
	--surface-h: 62%;
	--surface-rotate: 0deg;
	--surface-opacity: 1;

	/* Reveal controls */
	--reveal-top: 50%;
	--reveal-left: 50%;
	--reveal-w: 80%;
	--reveal-h: 56%;
	--reveal-scale: 1;
	--reveal-padding: 10px;
	--reveal-radius: 14px;
	--reveal-align: center;

	/* Grid controls (applies to .scratchRevealGrid inside #scratchRevealMount) */
	--grid-cols: 4;
	--grid-rows: 3;
	--grid-gap: 10px;
	--grid-row-gap: var(--grid-gap);
	--grid-col-gap: var(--grid-gap);
}

.scratch-header{
	position: sticky;
	top: 10px;
	z-index: 50;
	display: grid;
	grid-template-columns: 1fr auto 1fr;
	align-items: center;
	gap: 12px;
	padding: 12px 14px;
	border-radius: 16px;
	background: var(--sui-bg);
	border: 1px solid var(--sui-border);
	box-shadow: var(--sui-shadow);
	backdrop-filter: blur(14px);
}

.scratch-back{
	justify-self: start;
	color: var(--sui-text);
	text-decoration: none;
	font-weight: 700;
	font-size: 13px;
	padding: 10px 12px;
	border-radius: 12px;
	border: 1px solid rgba(255,255,255,0.10);
	background: rgba(0,0,0,0.18);
}

.scratch-brand{ justify-self: center; display: grid; place-items: center; }
.scratch-logo{ height: 44px; width: auto; filter: drop-shadow(0 10px 18px rgba(0,0,0,0.4)); user-select: none; }
.scratch-headerRight{
	justify-self: end;
	display: flex;
	align-items: center;
	gap: 10px;
}

.scratch-stageWrap{
	display: grid;
	place-items: center;
	padding-top: var(--stage-top-offset);
}


.scratch-stage{
	width: min(92vw, var(--stage-max-width));
	aspect-ratio: 10 / 14;
	position: relative;
	transform-style: preserve-3d;
}

/* ===== Paid action latency cover (New Card) ===== */

.scratch-ticket,
.scratch-reveal,
.scratch-surface{
	will-change: transform, opacity;
}

.scratch-stage.is-processing .scratch-ticket{
	animation: apsScratchCardFlip 420ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.scratch-stage.is-processing .scratch-reveal,
.scratch-stage.is-processing .scratch-surface{
	opacity: 0;
	transform: translate3d(0, 8px, 0);
	transition: opacity 180ms ease, transform 180ms ease;
}

.scratch-reveal,
.scratch-surface{
	transition: opacity 260ms ease, transform 260ms ease;
}

.scratch-stage.is-processing[data-aps-paid-phase="reveal"] .scratch-reveal,
.scratch-stage.is-processing[data-aps-paid-phase="reveal"] .scratch-surface{
	opacity: 1;
	transform: translate3d(0, 0, 0);
}

.scratch-stage.is-processing[data-aps-paid-late="1"] .scratch-ticket{
	animation: apsScratchCardFlip 420ms cubic-bezier(0.2, 0.8, 0.2, 1) both,
		apsScratchLatePulse 520ms ease-in-out infinite;
}

@keyframes apsScratchCardFlip{
	0%{ transform: translate3d(0, 0, 0) rotateY(0deg) scale(1); }
	50%{ transform: translate3d(0, 2px, 0) rotateY(88deg) scale(0.98); }
	100%{ transform: translate3d(0, 0, 0) rotateY(0deg) scale(1); }
}

@keyframes apsScratchLatePulse{
	0%, 100%{ opacity: 0.92; }
	50%{ opacity: 1; }
}

@media (prefers-reduced-motion: reduce){
	.scratch-stage.is-processing .scratch-ticket{
		animation: none;
	}
	.scratch-stage.is-processing[data-aps-paid-late="1"] .scratch-ticket{
		animation: none;
	}
}

.scratch-ticket,
.scratch-reveal,
.scratch-surface{
	position: absolute;
	inset: 0;
}

/* Ticket art positioning */
.scratch-ticket{
	z-index: 10;
	pointer-events: none;
}

.scratch-ticket img{
	position: absolute;
	left: var(--ticket-left);
	top: var(--ticket-top);
	width: var(--ticket-w);
	height: var(--ticket-h);
	transform: translate(-50%, -50%) rotate(var(--ticket-rotate));
	transform-origin: center;
	display: block;
	user-select: none;
	-webkit-user-drag: none;
}

/* Reveal positioning (under scratch surface, over ticket) */
.scratch-reveal{
	z-index: 20;
	pointer-events: none;
}

.scratch-reveal > .scratchRevealInner{
	position: absolute;
	left: var(--reveal-left);
	top: var(--reveal-top);
	width: var(--reveal-w);
	height: var(--reveal-h);
	transform: translate(-50%, -50%) scale(var(--reveal-scale));
	transform-origin: center;
	padding: var(--reveal-padding);
	border-radius: var(--reveal-radius);
	display: grid;
	align-content: start;
	justify-items: var(--reveal-align);
}

/* Surface mount where ScratchUI inserts canvases */
.scratch-surface{
	z-index: 30;
	opacity: var(--surface-opacity);
}

.scratch-surface > .scratchSurfaceInner{
	position: absolute;
	left: var(--surface-left);
	top: var(--surface-top);
	width: var(--surface-w);
	height: var(--surface-h);
	transform: translate(-50%, -50%) rotate(var(--surface-rotate));
	transform-origin: center;
}

.scratch-surface canvas{
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
}

/* The particle canvas is layered above the scratch canvas; it must not eat pointer events. */
.scratch-surface .scratchParticleCanvas{
	pointer-events: none;
}

.scratch-surface .scratchScratchCanvas{
	touch-action: none;
	cursor: grab;
}

.scratch-surface .scratchScratchCanvas:active{ cursor: grabbing; }
.scratch-surface.is-revealed .scratchScratchCanvas{ opacity: 0; pointer-events: none; transition: opacity 360ms ease; }
.scratch-surface.is-revealed .scratchParticleCanvas{ opacity: 0; transition: opacity 220ms ease; }

/*
	Once a card is revealed, the scratch surface no longer needs pointer events.
	Some games apply transform-based uiLayout overrides to #scratchSurfaceMount;
	in those cases the surface layer can overlap the action buttons and swallow
	clicks (e.g. "New Card").
*/
.scratch-surface.is-revealed{ pointer-events: none; }

.scratch-actions{
	display: flex;
	justify-content: center;
	align-items: center;
	gap: 12px;
	flex-wrap: wrap;
	margin-top: var(--stage-action-gap);
}

/* Optional HUD mounts (wallet / bet / outcome) */
.scratch-hud{
	width: min(92vw, 560px);
	margin: 0 auto;
	padding: 10px;
	border-radius: 16px;
	background: var(--sui-bg);
	border: 1px solid var(--sui-border);
	box-shadow: var(--sui-shadow);
	backdrop-filter: blur(14px);
	display: grid;
	grid-template-columns: 1fr 1fr 1fr;
	gap: 10px;
}

.scratch-hud__mount{
	min-height: 44px;
	padding: 10px 12px;
	border-radius: 14px;
	border: 1px solid rgba(255,255,255,0.10);
	background: rgba(0,0,0,0.18);
	color: var(--sui-text);
	overflow: hidden;
}

.scratch-hud[hidden]{
	display: none;
}

@media (max-width: 560px){
	.scratch-hud{ grid-template-columns: 1fr; }
}

.scratch-btn{
	appearance: none;
	border: 1px solid rgba(255, 255, 255, 0.2);
	color: rgba(255, 255, 255, 0.95);
	background: rgba(0,0,0,0.25);
	border-radius: 999px;
	padding: 10px 16px;
	font-weight: 900;
	letter-spacing: 0.04em;
	cursor: pointer;
	user-select: none;
}

.scratch-btn--ghost{ background: rgba(0,0,0,0.18); }
.scratch-btn--gold{
	background: linear-gradient(180deg, rgba(246, 211, 107, 0.18), rgba(0, 0, 0, 0.18));
	border-color: rgba(255, 236, 190, 0.22);
}

.scratch-progress{
	display: grid;
	grid-template-columns: 1fr auto;
	gap: 10px;
	align-items: center;
	width: min(92vw, 360px);
}

.scratch-progressBar{
	height: 10px;
	border-radius: 999px;
	background: rgba(0, 0, 0, 0.35);
	border: 1px solid rgba(255, 255, 255, 0.12);
	overflow: hidden;
}

.scratch-progressFill{
	height: 100%;
	width: 0%;
	background: linear-gradient(90deg, rgba(246, 211, 107, 0.55), rgba(255, 239, 176, 0.78));
	transition: width 120ms linear;
}

.scratch-progressText{
	font-weight: 900;
	letter-spacing: 0.08em;
	font-size: 12px;
	color: rgba(255, 255, 255, 0.92);
	min-width: 44px;
	text-align: right;
}

.scratch-footer{
	text-align: center;
	color: rgba(255,255,255,0.78);
	font-size: 12px;
	padding-bottom: 4px;
}

/* Modal */
.scratch-modal{ position: fixed; inset: 0; z-index: 80; display: none; }
.scratch-modal.is-open{ display: block; }
.scratch-modal__backdrop{ position: absolute; inset: 0; background: rgba(0,0,0,0.55); backdrop-filter: blur(4px); }
.scratch-modal__panel{
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
	width: min(560px, calc(100vw - 28px));
	max-height: min(74vh, 620px);
	overflow: auto;
	border-radius: 18px;
	background: rgba(14, 10, 10, 0.85);
	border: 1px solid rgba(255,236,190,0.18);
	box-shadow: var(--sui-shadow);
}
.scratch-modal__top{ display:flex; justify-content: space-between; align-items:center; gap: 10px; padding: 14px 14px 8px; }
.scratch-modal__content{ padding: 0 14px 14px; color: rgba(255,255,255,0.9); }
.scratch-modal__bottom{ padding: 0 14px 14px; display:flex; justify-content: flex-end; }
.scratch-modal__close{ appearance:none; border:0; background: rgba(255,255,255,0.10); color: var(--sui-text); border-radius: 12px; padding: 10px 12px; cursor: pointer; }

/* Simple form controls (used by Auto Play modal) */
.scratch-formRow{ display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; margin: 10px 0 8px; }
.scratch-formLabel{ font-weight: 900; letter-spacing: 0.02em; color: rgba(255,255,255,0.92); }
.scratch-formControl{
	width: 110px;
	border-radius: 12px;
	border: 1px solid rgba(255,255,255,0.14);
	background: rgba(0,0,0,0.26);
	color: rgba(255,255,255,0.92);
	padding: 10px 12px;
	font-weight: 900;
}
.scratch-formHint{ margin: 0; color: rgba(255,255,255,0.72); font-size: 12px; line-height: 1.35; }

/* DEV editor panel */
.scratch-editor{
	position: fixed;
	right: 14px;
	top: 88px;
	z-index: 90;
	width: 340px;
	max-width: calc(100vw - 20px);
	max-height: calc(100vh - 110px);
	border-radius: 16px;
	background: rgba(10, 8, 8, 0.86);
	border: 1px solid rgba(255,236,190,0.18);
	box-shadow: var(--sui-shadow);
	backdrop-filter: blur(12px);
	overflow: hidden;
	user-select: none;
	display: flex;
	flex-direction: column;
	/* Allow custom resize affordance positioning */
	padding-bottom: 0;
}

/* Mobile-only resize affordance for the dev editor panel */
.scratch-editor__resize{
	display: none;
}

.scratch-editor.is-mobileViewport .scratch-editor__resize{
	display: block;
	position: absolute;
	right: 6px;
	bottom: 6px;
	width: 22px;
	height: 22px;
	border-radius: 8px;
	border: 1px solid rgba(255,255,255,0.22);
	background: rgba(0,0,0,0.18);
	box-shadow: 0 0 0 1px rgba(0,0,0,0.25) inset;
	touch-action: none;
	cursor: nwse-resize;
}

.scratch-editor.is-mobileViewport .scratch-editor__resize::before{
	content: "";
	position: absolute;
	right: 6px;
	bottom: 6px;
	width: 10px;
	height: 10px;
	border-right: 2px solid rgba(255,255,255,0.5);
	border-bottom: 2px solid rgba(255,255,255,0.5);
	opacity: 0.85;
}

.scratch-editor__top{
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 10px;
	padding: 10px 12px;
	cursor: move;
}

.scratch-editor__title{ font-weight: 900; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; }
.scratch-editor__toggle{ cursor: pointer; }

.scratch-editor.is-collapsed .scratch-editor__body{ display: none; }

.scratch-editor__body{
	padding: 10px 12px 12px;
	display: grid;
	gap: 10px;
	overflow: auto;
	overscroll-behavior: contain;
	-webkit-overflow-scrolling: touch;
}

.scratch-editor__row{ display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.scratch-editor__row label{ font-size: 12px; color: rgba(255,255,255,0.86); }

.scratch-editor__field{ display:grid; grid-template-columns: 1fr 74px; gap: 10px; align-items: center; }
.scratch-editor__field input[type="range"]{ width: 100%; }
.scratch-editor__field input[type="number"],
.scratch-editor__field input[type="text"],
.scratch-editor__field select{
	width: 74px;
	border-radius: 10px;
	border: 1px solid rgba(255,255,255,0.14);
	background: rgba(0,0,0,0.26);
	color: rgba(255,255,255,0.92);
	padding: 8px 8px;
	font-weight: 800;
}

.scratch-editor__buttons{ display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; }
.scratch-editor__hint{ font-size: 11px; color: rgba(255,255,255,0.66); line-height: 1.35; }

@media (max-width: 560px){
	.scratch-header{ grid-template-columns: auto 1fr auto; }
	.scratch-logo{ height: 38px; }
}
