package ru.durakscore.app

import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.runtime.setValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.Animatable
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.sp

/*
 * ДУРАКОМЕТР — DARK JESTER UI
 *
 * Этот файл задает ОБЩИЙ визуальный язык приложения:
 * почти черный фон, темный карточный зеленый,
 * тонкие золотые рамки, красные опасные значения.
 *
 * Любой экран, который использует PokerScreen / PokerPanel /
 * PokerHeader / Poker*Button / PokerPlayerCard,
 * автоматически получает новый стиль.
 */
object PokerPalette {

    // Почти чёрный базовый фон.
    val BackgroundTop =
        Color(0xFF000000)

    val BackgroundBottom =
        Color(0xFF030504)

    // Карточный зелёный теперь очень тёмный и только как оттенок.
    val Table =
        Color(0xFF091510)

    val TableDark =
        Color(0xFF040806)

    // Панели ближе к главному меню: чёрные, но не плоские.
    val Panel =
        Color(0xF2070B09)

    val PanelStrong =
        Color(0xFF0A0F0C)

    val PanelBorder =
        Color(0xFF5A4828)

    val PanelBorderSoft =
        Color(0xFF25302A)

    val Gold =
        Color(0xFFD6AB51)

    val GoldBright =
        Color(0xFFEBC864)

    val GoldDark =
        Color(0xFF78612F)

    // Красный — главный эмоциональный акцент приложения.
    val Crimson =
        Color(0xFFA91F1B)

    val CrimsonBright =
        Color(0xFFE33B32)

    val CrimsonDark =
        Color(0xFF3D0B0A)

    val CrimsonGlow =
        Color(0x44A91F1B)

    val TextPrimary =
        Color(0xFFF2ECE2)

    val TextSecondary =
        Color(0xFFA6A19A)

    val Good =
        Color(0xFF70BD53)

    val Bad =
        Color(0xFFE44036)

    // +1 теперь тоже вписан в палитру, а не выбивается голубым.
    val BlueChip =
        Color(0xFF721816)

    val BlueChipText =
        Color(0xFFF7EDE4)

    val Danger =
        CrimsonBright

    val DangerDark =
        CrimsonDark

    val Disabled =
        Color(0xFF414743)

    val LastPointPanel =
        Color(0xF222100D)
}

val PokerDisplayFont =
    FontFamily.Serif

private val DurakometrDarkColors =
    darkColorScheme(
        primary =
            PokerPalette.CrimsonBright,
        onPrimary =
            PokerPalette.TextPrimary,
        primaryContainer =
            PokerPalette.CrimsonDark,
        onPrimaryContainer =
            PokerPalette.TextPrimary,
        secondary =
            PokerPalette.Gold,
        onSecondary =
            Color(0xFF171006),
        secondaryContainer =
            Color(0xFF211A0D),
        onSecondaryContainer =
            PokerPalette.GoldBright,
        background =
            PokerPalette.BackgroundTop,
        onBackground =
            PokerPalette.TextPrimary,
        surface =
            PokerPalette.PanelStrong,
        onSurface =
            PokerPalette.TextPrimary,
        surfaceVariant =
            PokerPalette.TableDark,
        onSurfaceVariant =
            PokerPalette.TextSecondary,
        outline =
            PokerPalette.GoldDark,
        error =
            PokerPalette.Bad,
        onError =
            PokerPalette.TextPrimary
    )

@Composable
fun DurakometrMaterialTheme(
    content: @Composable () -> Unit
) {
    MaterialTheme(
        colorScheme =
            DurakometrDarkColors,
        content =
            content
    )
}


@Composable
fun PokerScreen(
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit
) {
    Box(
        modifier =
            modifier
                .fillMaxSize()
                .background(
                    brush =
                        Brush.verticalGradient(
                            colors =
                                listOf(
                                    PokerPalette.BackgroundTop,
                                    PokerPalette.BackgroundBottom
                                )
                        )
                )
    ) {
        // Красный дым сверху справа.
        Box(
            modifier =
                Modifier
                    .fillMaxSize()
                    .background(
                        brush =
                            Brush.radialGradient(
                                colors =
                                    listOf(
                                        PokerPalette.CrimsonGlow,
                                        Color.Transparent
                                    ),
                                radius =
                                    760f
                            )
                    )
        )

        // Еле заметный карточный зелёный снизу слева.
        Box(
            modifier =
                Modifier
                    .fillMaxSize()
                    .background(
                        brush =
                            Brush.radialGradient(
                                colors =
                                    listOf(
                                        Color(0x100D4A30),
                                        Color.Transparent
                                    ),
                                radius =
                                    980f
                            )
                    )
        )

        BoxWithConstraints(
            modifier = Modifier.fillMaxSize()
        ) {
            /*
             * На телефонах с "Масштабом экрана" полезная ширина может
             * быть заметно меньше физического разрешения. Поэтому боковые
             * поля уменьшаются сами, а нижняя системная навигация больше
             * не перекрывает последний блок экрана.
             */
            val sidePadding =
                when {
                    maxWidth < 340.dp -> 10.dp
                    maxWidth < 380.dp -> 12.dp
                    maxWidth < 420.dp -> 15.dp
                    else -> 18.dp
                }

            Column(
                modifier =
                    Modifier
                        .fillMaxSize()
                        .statusBarsPadding()
                        .navigationBarsPadding()
                        .verticalScroll(
                            rememberScrollState()
                        )
                        .padding(
                            start = sidePadding,
                            end = sidePadding,
                            top = 10.dp,
                            bottom = 24.dp
                        ),
                content =
                    content
            )
        }
    }
}

@Composable
fun PokerHeader(
    title: String,
    subtitle: String? = null,
    badge: String? = null
) {
    val fontScale = LocalDensity.current.fontScale
    val titleSize =
        when {
            fontScale >= 1.30f -> 28.sp
            fontScale >= 1.15f -> 30.sp
            else -> 33.sp
        }

    Text(
        text = title,
        color = PokerPalette.TextPrimary,
        fontSize = titleSize,
        lineHeight = (titleSize.value + 4).sp,
        fontFamily = PokerDisplayFont,
        fontWeight = FontWeight.Bold,
        maxLines = 3,
        softWrap = true
    )

    Spacer(modifier = Modifier.height(4.dp))

    Box(
        modifier =
            Modifier
                .width(54.dp)
                .height(2.dp)
                .background(PokerPalette.Crimson)
    )

    if (subtitle != null) {
        Spacer(modifier = Modifier.height(7.dp))

        Text(
            text = subtitle,
            color = PokerPalette.TextSecondary,
            fontSize = 15.sp,
            lineHeight = 20.sp,
            maxLines = 5,
            softWrap = true
        )
    }

    if (badge != null) {
        Spacer(modifier = Modifier.height(11.dp))
        PokerBadge(text = badge)
    }
}

@Composable
fun PokerBadge(
    text: String
) {

    Box(
        modifier =
            Modifier
                .clip(
                    RoundedCornerShape(
                        100.dp
                    )
                )
                .background(
                    PokerPalette.CrimsonDark
                )
                .border(
                    width =
                        1.dp,
                    color =
                        PokerPalette.Crimson,
                    shape =
                        RoundedCornerShape(
                            100.dp
                        )
                )
                .padding(
                    horizontal =
                        13.dp,
                    vertical =
                        7.dp
                )
    ) {

        Text(
            text =
                text,
            color =
                PokerPalette.GoldBright,
            fontWeight =
                FontWeight.Bold,
            fontSize =
                13.sp
        )
    }
}

@Composable
fun PokerPanel(
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit
) {

    Card(
        modifier =
            modifier.fillMaxWidth(),
        colors =
            CardDefaults.cardColors(
                containerColor =
                    PokerPalette.Panel
            ),
        border =
            BorderStroke(
                width =
                    1.dp,
                color =
                    PokerPalette.PanelBorder
            ),
        shape =
            RoundedCornerShape(
                18.dp
            )
    ) {

        Column(
            modifier =
                Modifier.padding(
                    16.dp
                ),
            content =
                content
        )
    }
}

@Composable
fun PokerPrimaryButton(
    text: String,
    onClick: () -> Unit,
    enabled: Boolean = true
) {

    Button(
        onClick =
            onClick,
        enabled =
            enabled,
        modifier =
            Modifier.fillMaxWidth(),
        shape =
            RoundedCornerShape(
                16.dp
            ),
        colors =
            ButtonDefaults.buttonColors(
                containerColor =
                    PokerPalette.Crimson,
                contentColor =
                    PokerPalette.TextPrimary,
                disabledContainerColor =
                    PokerPalette.Disabled,
                disabledContentColor =
                    Color(0xFFCDD3CF)
            )
    ) {

        Text(
            text = text,
            fontSize = 18.sp,
            lineHeight = 22.sp,
            fontWeight = FontWeight.Bold,
            maxLines = 3,
            softWrap = true
        )
    }
}

@Composable
fun PokerSecondaryButton(
    text: String,
    onClick: () -> Unit,
    enabled: Boolean = true
) {

    OutlinedButton(
        onClick =
            onClick,
        enabled =
            enabled,
        modifier =
            Modifier.fillMaxWidth(),
        shape =
            RoundedCornerShape(
                16.dp
            ),
        border =
            BorderStroke(
                width =
                    1.dp,
                color =
                    if (
                        enabled
                    ) {
                        PokerPalette.GoldDark
                    } else {
                        PokerPalette.Disabled
                    }
            ),
        colors =
            ButtonDefaults
                .outlinedButtonColors(
                    contentColor =
                        PokerPalette.TextPrimary,
                    disabledContentColor =
                        Color(0xFFCDD3CF)
                )
    ) {

        Text(
            text = text,
            fontSize = 18.sp,
            lineHeight = 22.sp,
            fontWeight = FontWeight.SemiBold,
            maxLines = 3,
            softWrap = true
        )
    }
}

@Composable
fun PokerDangerButton(
    text: String,
    onClick: () -> Unit,
    enabled: Boolean = true
) {

    OutlinedButton(
        onClick =
            onClick,
        enabled =
            enabled,
        modifier =
            Modifier.fillMaxWidth(),
        shape =
            RoundedCornerShape(
                16.dp
            ),
        border =
            BorderStroke(
                width =
                    1.dp,
                color =
                    if (
                        enabled
                    ) {
                        PokerPalette.Danger
                    } else {
                        PokerPalette.Disabled
                    }
            ),
        colors =
            ButtonDefaults
                .outlinedButtonColors(
                    contentColor =
                        PokerPalette.Danger,
                    disabledContentColor =
                        PokerPalette.Disabled
                )
    ) {

        Text(
            text = text,
            fontSize = 18.sp,
            lineHeight = 22.sp,
            fontWeight = FontWeight.SemiBold,
            maxLines = 3,
            softWrap = true
        )
    }
}

/**
 * Анимация изменения счёта.
 *
 * Нужна не только игровому столу, но и LiveGameScreen,
 * поэтому это общий компонент PokerUi.
 */
@Composable
fun AnimatedScoreValue(
    score: Int,
    targetScore: Int,
    color: Color =
        PokerPalette.TextPrimary
) {
    var previousScore by remember {
        mutableIntStateOf(score)
    }

    val scale = remember { Animatable(1f) }

    LaunchedEffect(score) {
        val increased = score > previousScore
        previousScore = score

        if (!increased) {
            return@LaunchedEffect
        }

        scale.snapTo(1f)
        scale.animateTo(
            targetValue = 1.16f,
            animationSpec = tween(durationMillis = 110)
        )
        scale.animateTo(
            targetValue = 1f,
            animationSpec = tween(durationMillis = 185)
        )
    }

    val fontScale = LocalDensity.current.fontScale
    val scoreFont =
        when {
            fontScale >= 1.30f -> 34.sp
            fontScale >= 1.15f -> 38.sp
            else -> 42.sp
        }
    val targetFont =
        when {
            fontScale >= 1.30f -> 14.sp
            fontScale >= 1.15f -> 15.sp
            else -> 17.sp
        }

    Row(
        modifier =
            Modifier.graphicsLayer {
                scaleX = scale.value
                scaleY = scale.value
            },
        verticalAlignment = Alignment.Bottom,
        horizontalArrangement = Arrangement.spacedBy(4.dp)
    ) {
        Text(
            text = score.toString(),
            color = color,
            fontSize = scoreFont,
            fontWeight = FontWeight.Black
        )

        Text(
            text = "/ $targetScore",
            color = PokerPalette.TextSecondary,
            fontSize = targetFont,
            fontWeight = FontWeight.SemiBold,
            modifier = Modifier.padding(bottom = 7.dp)
        )
    }
}

@Composable
fun PokerPlayerCard(
    name: String,
    score: Int,
    targetScore: Int,
    onAddPoint: (() -> Unit)? = null,
    isLastPoint: Boolean = false,
    lastPointText: String = "последнее очко"
) {
    val fontScale = LocalDensity.current.fontScale

    Card(
        modifier = Modifier.fillMaxWidth(),
        colors =
            CardDefaults.cardColors(
                containerColor =
                    if (isLastPoint) {
                        PokerPalette.LastPointPanel
                    } else {
                        PokerPalette.Panel
                    }
            ),
        border =
            BorderStroke(
                width = if (isLastPoint) 2.dp else 1.dp,
                color =
                    if (isLastPoint) {
                        PokerPalette.CrimsonBright
                    } else {
                        PokerPalette.PanelBorderSoft
                    }
            ),
        shape = RoundedCornerShape(20.dp)
    ) {
        BoxWithConstraints(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .padding(
                        horizontal = 16.dp,
                        vertical = 14.dp
                    )
        ) {
            val stacked =
                maxWidth < 315.dp ||
                    fontScale >= 1.30f

            if (stacked) {
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(9.dp)
                ) {
                    Text(
                        text = name,
                        color =
                            if (isLastPoint) {
                                PokerPalette.GoldBright
                            } else {
                                PokerPalette.TextPrimary
                            },
                        fontSize = 21.sp,
                        lineHeight = 25.sp,
                        fontWeight = FontWeight.Bold,
                        maxLines = 3,
                        softWrap = true
                    )

                    if (isLastPoint) {
                        Text(
                            text = "• $lastPointText",
                            color = PokerPalette.Gold,
                            fontSize = 13.sp,
                            lineHeight = 17.sp,
                            fontWeight = FontWeight.SemiBold,
                            maxLines = 3
                        )
                    }

                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        AnimatedScoreValue(
                            score = score,
                            targetScore = targetScore,
                            color = PokerPalette.TextPrimary
                        )

                        if (onAddPoint != null) {
                            Button(
                                onClick = onAddPoint,
                                modifier =
                                    Modifier.size(
                                        width = 54.dp,
                                        height = 44.dp
                                    ),
                                contentPadding = PaddingValues(0.dp),
                                shape = RoundedCornerShape(13.dp),
                                colors =
                                    ButtonDefaults.buttonColors(
                                        containerColor = PokerPalette.BlueChip,
                                        contentColor = PokerPalette.BlueChipText
                                    )
                            ) {
                                Text(
                                    text = "+1",
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight.ExtraBold
                                )
                            }
                        }
                    }
                }
            } else {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    Column(
                        modifier = Modifier.weight(1f)
                    ) {
                        Text(
                            text = name,
                            color =
                                if (isLastPoint) {
                                    PokerPalette.GoldBright
                                } else {
                                    PokerPalette.TextPrimary
                                },
                            fontSize = 24.sp,
                            lineHeight = 28.sp,
                            fontWeight = FontWeight.Bold,
                            maxLines = 2,
                            softWrap = true
                        )

                        if (isLastPoint) {
                            Spacer(modifier = Modifier.height(4.dp))
                            Text(
                                text = "• $lastPointText",
                                color = PokerPalette.Gold,
                                fontSize = 13.sp,
                                lineHeight = 17.sp,
                                fontWeight = FontWeight.SemiBold,
                                maxLines = 2
                            )
                        }
                    }

                    AnimatedScoreValue(
                        score = score,
                        targetScore = targetScore,
                        color = PokerPalette.TextPrimary
                    )

                    if (onAddPoint != null) {
                        Button(
                            onClick = onAddPoint,
                            modifier =
                                Modifier.size(
                                    width = 54.dp,
                                    height = 44.dp
                                ),
                            contentPadding = PaddingValues(0.dp),
                            shape = RoundedCornerShape(13.dp),
                            colors =
                                ButtonDefaults.buttonColors(
                                    containerColor = PokerPalette.BlueChip,
                                    contentColor = PokerPalette.BlueChipText
                                )
                        ) {
                            Text(
                                text = "+1",
                                fontSize = 16.sp,
                                fontWeight = FontWeight.ExtraBold
                            )
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun PokerMenuTile(
    icon: String,
    title: String,
    subtitle: String? = null,
    onClick: () -> Unit,
    enabled: Boolean = true,
    redAccent: Boolean = false,
    modifier: Modifier = Modifier
) {

    val borderColor =
        when {
            !enabled ->
                PokerPalette.Disabled

            redAccent ->
                PokerPalette.Crimson

            else ->
                PokerPalette.PanelBorder
        }

    Card(
        modifier =
            modifier
                .fillMaxWidth()
                .clickable(
                    enabled =
                        enabled,
                    onClick =
                        onClick
                ),
        colors =
            CardDefaults.cardColors(
                containerColor =
                    if (
                        redAccent
                    ) {
                        Color(0xF2160C0B)
                    } else {
                        PokerPalette.Panel
                    }
            ),
        border =
            BorderStroke(
                width =
                    if (
                        redAccent
                    ) {
                        1.5.dp
                    } else {
                        1.dp
                    },
                color =
                    borderColor
            ),
        shape =
            RoundedCornerShape(
                18.dp
            )
    ) {

        Row(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .padding(
                        horizontal =
                            15.dp,
                        vertical =
                            14.dp
                    ),
            verticalAlignment =
                Alignment.CenterVertically,
            horizontalArrangement =
                Arrangement.spacedBy(
                    12.dp
                )
        ) {

            Box(
                modifier =
                    Modifier
                        .size(
                            43.dp
                        )
                        .clip(
                            RoundedCornerShape(
                                13.dp
                            )
                        )
                        .background(
                            if (
                                redAccent
                            ) {
                                PokerPalette.CrimsonDark
                            } else {
                                PokerPalette.TableDark
                            }
                        )
                        .border(
                            width =
                                1.dp,
                            color =
                                if (
                                    redAccent
                                ) {
                                    PokerPalette.Crimson
                                } else {
                                    PokerPalette.GoldDark
                                },
                            shape =
                                RoundedCornerShape(
                                    13.dp
                                )
                        ),
                contentAlignment =
                    Alignment.Center
            ) {

                Text(
                    text =
                        icon,
                    fontSize =
                        22.sp
                )
            }

            Column(
                modifier =
                    Modifier.weight(
                        1f
                    )
            ) {

                Text(
                    text =
                        title,
                    color =
                        if (
                            enabled
                        ) {
                            PokerPalette.TextPrimary
                        } else {
                            PokerPalette.Disabled
                        },
                    fontSize =
                        17.sp,
                    lineHeight =
                        21.sp,
                    fontWeight =
                        FontWeight.Bold,
                    maxLines =
                        3,
                    softWrap =
                        true
                )

                if (
                    subtitle !=
                    null
                ) {

                    Spacer(
                        modifier =
                            Modifier.height(
                                2.dp
                            )
                    )

                    Text(
                        text =
                            subtitle,
                        color =
                            if (
                                redAccent
                            ) {
                                Color(0xFFD9A09B)
                            } else {
                                PokerPalette.TextSecondary
                            },
                        fontSize =
                            11.sp,
                        lineHeight =
                            14.sp,
                        maxLines =
                            4,
                        softWrap =
                            true
                    )
                }
            }

            Text(
                text =
                    "›",
                color =
                    if (
                        redAccent
                    ) {
                        PokerPalette.CrimsonBright
                    } else {
                        PokerPalette.Gold
                    },
                fontSize =
                    29.sp,
                fontWeight =
                    FontWeight.Light
            )
        }
    }
}



@Composable
private fun PokerMenuArtIcon(
    iconRes: Int,
    size: androidx.compose.ui.unit.Dp,
    enabled: Boolean
) {

    val frameShape =
        RoundedCornerShape(
            11.dp
        )

    Box(
        modifier =
            Modifier
                .size(
                    size
                )
                .border(
                    width =
                        1.dp,
                    color =
                        if (
                            enabled
                        ) {
                            PokerPalette.GoldDark
                        } else {
                            PokerPalette.Disabled
                        },
                    shape =
                        frameShape
                )
                .padding(
                    3.dp
                ),
        contentAlignment =
            Alignment.Center
    ) {

        Image(
            painter =
                painterResource(
                    id =
                        iconRes
                ),
            contentDescription =
                null,
            contentScale =
                ContentScale.Fit,
            modifier =
                Modifier.fillMaxSize()
        )
    }
}

@Composable
fun PokerMenuArtTile(
    iconRes: Int,
    title: String,
    subtitle: String,
    onClick: () -> Unit,
    enabled: Boolean = true,
    redAccent: Boolean = false,
    compact: Boolean = false,
    modifier: Modifier = Modifier
) {

    val borderColor =
        when {
            !enabled ->
                PokerPalette.Disabled

            redAccent ->
                PokerPalette.Crimson

            else ->
                PokerPalette.PanelBorder
        }

    val fontScale =
        LocalDensity.current.fontScale

    /*
     * Раньше здесь была жёсткая height(134.dp / 88.dp).
     * На Samsung/Poco с увеличенным системным шрифтом текст становился
     * выше карточки и обрезался. Теперь это только минимальная высота:
     * если тексту нужно больше места, карточка спокойно растёт вниз.
     */
    val minimumHeight =
        if (compact) {
            when {
                fontScale >= 1.55f -> 184.dp
                fontScale >= 1.35f -> 166.dp
                fontScale >= 1.18f -> 150.dp
                else -> 134.dp
            }
        } else {
            when {
                fontScale >= 1.55f -> 132.dp
                fontScale >= 1.35f -> 116.dp
                fontScale >= 1.18f -> 102.dp
                else -> 88.dp
            }
        }

    val compactIconSize =
        when {
            fontScale >= 1.55f -> 46.dp
            fontScale >= 1.35f -> 50.dp
            fontScale >= 1.18f -> 54.dp
            else -> 58.dp
        }

    Card(
        modifier =
            modifier
                .fillMaxWidth()
                .heightIn(
                    min = minimumHeight
                )
                .clickable(
                    enabled =
                        enabled,
                    onClick =
                        onClick
                ),
        colors =
            CardDefaults.cardColors(
                containerColor =
                    if (
                        redAccent
                    ) {
                        Color(0xF2140908)
                    } else {
                        Color(0xF2050D0A)
                    }
            ),
        border =
            BorderStroke(
                width =
                    if (
                        redAccent
                    ) {
                        1.5.dp
                    } else {
                        1.dp
                    },
                color =
                    borderColor
            ),
        shape =
            RoundedCornerShape(
                18.dp
            )
    ) {

        if (
            compact
        ) {

            Column(
                modifier =
                    Modifier
                        .fillMaxWidth()
                        .padding(
                            horizontal =
                                12.dp,
                            vertical =
                                10.dp
                        )
            ) {

                Row(
                    modifier =
                        Modifier.fillMaxWidth(),
                    verticalAlignment =
                        Alignment.CenterVertically
                ) {

                    PokerMenuArtIcon(
                        iconRes =
                            iconRes,
                        size =
                            compactIconSize,
                        enabled =
                            enabled
                    )

                    Spacer(
                        modifier =
                            Modifier.weight(
                                1f
                            )
                    )

                    Text(
                        text =
                            "›",
                        color =
                            if (
                                enabled
                            ) {
                                PokerPalette.CrimsonBright
                            } else {
                                PokerPalette.Disabled
                            },
                        fontSize =
                            27.sp,
                        fontWeight =
                            FontWeight.Light
                    )
                }

                Spacer(
                    modifier =
                        Modifier.height(
                            4.dp
                        )
                )

                Text(
                    text =
                        title,
                    color =
                        if (
                            enabled
                        ) {
                            PokerPalette.TextPrimary
                        } else {
                            PokerPalette.Disabled
                        },
                    fontSize =
                        14.sp,
                    lineHeight =
                        17.sp,
                    fontWeight =
                        FontWeight.Black,
                    maxLines =
                        2,
                    softWrap =
                        true
                )

                Spacer(
                    modifier =
                        Modifier.height(
                            2.dp
                        )
                )

                Text(
                    text =
                        subtitle,
                    color =
                        PokerPalette.TextSecondary,
                    fontSize =
                        10.sp,
                    lineHeight =
                        13.sp,
                    maxLines =
                        3,
                    softWrap =
                        true
                )
            }

        } else {

            Row(
                modifier =
                    Modifier
                        .fillMaxWidth()
                        .padding(
                            horizontal =
                                13.dp,
                            vertical =
                                10.dp
                        ),
                verticalAlignment =
                    Alignment.CenterVertically
            ) {

                PokerMenuArtIcon(
                    iconRes =
                        iconRes,
                    size =
                        if (fontScale >= 1.45f) {
                            52.dp
                        } else {
                            60.dp
                        },
                    enabled =
                        enabled
                )

                Spacer(
                    modifier =
                        Modifier.size(
                            10.dp
                        )
                )

                Column(
                    modifier =
                        Modifier.weight(
                            1f
                        )
                ) {

                    Text(
                        text =
                            title,
                        color =
                            if (
                                enabled
                            ) {
                                PokerPalette.TextPrimary
                            } else {
                                PokerPalette.Disabled
                            },
                        fontSize =
                            16.sp,
                        lineHeight =
                            20.sp,
                        fontWeight =
                            FontWeight.Black,
                        maxLines =
                            2,
                        softWrap =
                            true
                    )

                    Spacer(
                        modifier =
                            Modifier.height(
                                2.dp
                            )
                    )

                    Text(
                        text =
                            subtitle,
                        color =
                            if (
                                redAccent
                            ) {
                                Color(0xFFD6A09A)
                            } else {
                                PokerPalette.TextSecondary
                            },
                        fontSize =
                            10.sp,
                        lineHeight =
                            13.sp,
                        maxLines =
                            3,
                        softWrap =
                            true
                    )
                }

                Text(
                    text =
                        "›",
                    color =
                        if (
                            redAccent
                        ) {
                            PokerPalette.CrimsonBright
                        } else {
                            PokerPalette.Gold
                        },
                    fontSize =
                        28.sp,
                    fontWeight =
                        FontWeight.Light
                )
            }
        }
    }
}

@Composable
fun PokerBottomBackButton(
    text: String = "←  В меню",
    onClick: () -> Unit
) {

    PokerSecondaryButton(
        text =
            text,
        onClick =
            onClick
    )
}

@Composable
fun PokerBackTextButton(
    text: String = "←  Назад",
    onClick: () -> Unit
) {

    TextButton(
        onClick =
            onClick
    ) {

        Text(
            text =
                text,
            color =
                PokerPalette.GoldBright,
            fontSize =
                16.sp,
            fontWeight =
                FontWeight.SemiBold
        )
    }
}
