/* ============================================
   CARDS.CSS - Card Rendering & Animations
   ASCII-Art Cards, Deal/Flip Animations
   ============================================ */

/* ---- Card Base ---- */
.card {
    width: 110px;
    height: 154px;
    border: 2px solid var(--card-border);
    background-color: var(--card-bg);
    position: relative;
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-family: 'VT323', 'Courier New', monospace;
    font-size: 1rem;
    flex-shrink: 0;
    /* Enable 3D transforms for flip */
    transform-style: preserve-3d;
    transition: transform 0.15s ease;
    user-select: none;
}

.card:hover {
    transform: translateY(-2px);
    z-index: 10;
}

/* ---- Card Face (front) ---- */
.card-face {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 4px 6px;
    overflow: hidden;
}

/* Top-left: rank + suit */
.card-corner-top {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    line-height: 1;
}

.card-rank {
    font-size: 1.4rem;
    font-weight: bold;
    line-height: 1;
}

.card-suit-small {
    font-size: 1rem;
    line-height: 1;
}

/* Center suit (large) */
.card-suit-center {
    font-size: 2.2rem;
    text-align: center;
    line-height: 1;
    align-self: center;
}

/* Bottom-right: rank + suit (rotated) */
.card-corner-bottom {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    line-height: 1;
    transform: rotate(180deg);
}

/* ---- ASCII Art Card Face ---- */
.card-ascii {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    backface-visibility: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'VT323', 'Courier New', monospace;
    font-size: 0.95rem;
    line-height: 1.15;
    white-space: pre;
    overflow: hidden;
    padding: 2px;
}

/* ---- Card Back ---- */
.card-back {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    backface-visibility: hidden;
    transform: rotateY(180deg);
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--card-bg);
    border: none;
    overflow: hidden;
}

/* ASCII crosshatch pattern for card back */
.card-back-pattern {
    font-family: 'VT323', 'Courier New', monospace;
    font-size: 0.65rem;
    line-height: 1;
    white-space: pre;
    color: var(--text-dim);
    letter-spacing: 0px;
    overflow: hidden;
    padding: 3px;
}

/* Card back when showing back by default (not flipped) */
.card.face-down .card-face,
.card.face-down .card-ascii {
    visibility: hidden;
}

.card.face-down .card-back {
    transform: rotateY(0deg);
}

/* Alternative: card-back via generated pattern */
.card-back-grid {
    position: absolute;
    top: 4px;
    left: 4px;
    right: 4px;
    bottom: 4px;
    background-image:
        repeating-linear-gradient(
            0deg,
            var(--text-dim) 0px,
            var(--text-dim) 1px,
            transparent 1px,
            transparent 6px
        ),
        repeating-linear-gradient(
            90deg,
            var(--text-dim) 0px,
            var(--text-dim) 1px,
            transparent 1px,
            transparent 6px
        );
    opacity: 0.3;
    border: 1px solid var(--text-dim);
}

/* ---- Card Colors ---- */

/* Red suits (Herz, Karo) */
.card-red {
    color: var(--card-red);
}

/* Black suits (Pik, Kreuz) */
.card-black {
    color: var(--card-black);
}

/* In color theme, override for true red */
[data-theme="color"] .card-red {
    color: var(--apple-red);
}

[data-theme="color"] .card-black {
    color: var(--text);
}

/* ---- Hand (card container) ---- */
.hand {
    display: flex;
    align-items: flex-start;
    position: relative;
    min-height: 158px;
    padding: 2px 0;
}

/* Overlapping cards - human/dealer */
.hand .card:not(:first-child) {
    margin-left: -50px;
}

/* ---- Verdeckte Karte (Dealer-Lochkarte, Blindflug) ---- */
/* Eigene Klasse: .card-back ist fuer ein Innenelement gedacht und
   wuerde die ganze Karte wegdrehen (unsichtbar). */
.card-covered {
    color: var(--text-dim);
}

/* ---- Mini-Label (Rang + Farbe), nur in den KI-Haenden sichtbar ---- */
.card-mini {
    display: none;
}

/* ---- AI Player Hand: kleine Karten nebeneinander ---- */
.ai-players-area .player-cards.hand {
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
    min-height: 76px; /* = Kartenhoehe 72 + Innenabstand, Box waechst beim Austeilen nicht */
    padding: 2px 0;
}

.ai-players-area .player-cards.hand .card {
    width: 52px;
    height: 72px;
}

.ai-players-area .player-cards.hand .card:not(:first-child) {
    margin-left: -16px;
}

.ai-players-area .player-cards.hand .card-ascii {
    display: none;
}

.ai-players-area .player-cards.hand .card-mini {
    display: block;
    white-space: pre;
    text-align: center;
    font-size: 1.5rem;
    line-height: 1;
}

/* ---- Deal Animation ---- */
@keyframes dealCard {
    0% {
        opacity: 0;
        transform: translate(-200px, -150px) rotate(-15deg) scale(0.6);
    }
    50% {
        opacity: 0.8;
        transform: translate(-40px, -30px) rotate(-3deg) scale(0.9);
    }
    100% {
        opacity: 1;
        transform: translate(0, 0) rotate(0deg) scale(1);
    }
}

.card.dealing {
    animation: dealCard 0.4s cubic-bezier(0.25, 0.8, 0.25, 1) forwards;
}

/* Stagger deal delays for multiple cards */
.card.dealing:nth-child(1) { animation-delay: 0s; }
.card.dealing:nth-child(2) { animation-delay: 0.15s; }
.card.dealing:nth-child(3) { animation-delay: 0.3s; }
.card.dealing:nth-child(4) { animation-delay: 0.45s; }
.card.dealing:nth-child(5) { animation-delay: 0.6s; }
.card.dealing:nth-child(6) { animation-delay: 0.75s; }
.card.dealing:nth-child(7) { animation-delay: 0.9s; }
.card.dealing:nth-child(8) { animation-delay: 1.05s; }

/* ---- Flip Animation (Hole Card Reveal) ---- */
@keyframes flipCard {
    0% {
        transform: rotateY(180deg);
    }
    45% {
        transform: rotateY(90deg) scale(1.05);
    }
    55% {
        transform: rotateY(90deg) scale(1.05);
    }
    100% {
        transform: rotateY(0deg);
    }
}

.card.flipping {
    animation: flipCard 0.5s ease-in-out forwards;
}

/* ---- Bust Shake ---- */
@keyframes bustShake {
    0%, 100% { transform: translateX(0); }
    15% { transform: translateX(-4px) rotate(-1deg); }
    30% { transform: translateX(4px) rotate(1deg); }
    45% { transform: translateX(-3px); }
    60% { transform: translateX(3px); }
    75% { transform: translateX(-1px); }
    90% { transform: translateX(1px); }
}

.card.bust-shake {
    animation: bustShake 0.5s ease-out;
}

/* ---- Win Highlight ---- */
@keyframes winPulse {
    0%, 100% {
        box-shadow: 0 0 4px var(--glow-color-subtle);
    }
    50% {
        box-shadow: 0 0 12px var(--glow-color), 0 0 24px var(--glow-color-subtle);
    }
}

.card.winning {
    animation: winPulse 1s ease-in-out 3;
    border-color: var(--accent);
}

/* ---- Deck Representation ---- */
.deck {
    position: relative;
    width: 110px;
    height: 154px;
}

.deck-card {
    position: absolute;
    width: 110px;
    height: 154px;
    border: 2px solid var(--card-border);
    background-color: var(--card-bg);
}

/* Stacked deck effect */
.deck-card:nth-child(1) { top: 0; left: 0; }
.deck-card:nth-child(2) { top: -2px; left: 2px; }
.deck-card:nth-child(3) { top: -4px; left: 4px; }

.deck-count {
    position: absolute;
    bottom: -18px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.9rem;
    color: var(--text-dim);
    white-space: nowrap;
}

/* ---- Split Hand Indicator ---- */
.split-hands {
    display: flex;
    gap: 16px;
}

.split-hands .hand {
    border: 1px dashed var(--separator);
    padding: 4px;
    border-radius: 4px;
}

.split-hands .hand.active-split {
    border-color: var(--accent);
}

/* ---- Responsive Card Sizes ---- */
@media (max-width: 768px) {
    .card {
        width: 90px;
        height: 126px;
    }

    .card-ascii {
        font-size: 0.78rem;
    }

    .hand .card:not(:first-child) {
        margin-left: -40px;
    }

    .hand {
        min-height: 130px;
    }

    .ai-players-area .player-cards.hand .card {
        width: 32px;
        height: 44px;
    }

    .ai-players-area .player-cards.hand {
        min-height: 48px;
    }
}

@media (max-width: 480px) {
    .card {
        width: 76px;
        height: 106px;
    }

    .card-ascii {
        font-size: 0.65rem;
    }

    .hand .card:not(:first-child) {
        margin-left: -36px;
    }

    .hand {
        min-height: 110px;
    }

    .ai-players-area .player-cards.hand .card {
        width: 28px;
        height: 40px;
    }

    .ai-players-area .player-cards.hand {
        min-height: 44px;
    }
}

/* ---- Print - hide everything card related ---- */
@media print {
    .card {
        border: 1px solid #000 !important;
        background: #fff !important;
        color: #000 !important;
        box-shadow: none !important;
    }

    .card-back-pattern,
    .card-back-grid {
        display: none !important;
    }
}

/* ---- Offene Karte: Rang in den Ecken, Farbe mittig, fest positioniert ----
   (unabhaengig von Zeichenbreiten; Abstaende in em, wachsen mit der Kartenschrift) */
.card-ascii.card-face-pos {
    display: block;
    white-space: normal;
}

.card-face-pos .cf-rank {
    position: absolute;
    top: 0.35em;
    left: 0.55em;
    font-size: 1.25em;
    line-height: 1;
}

.card-face-pos .cf-rank-bottom {
    top: auto;
    left: auto;
    bottom: 0.35em;
    right: 0.55em;
}

.card-face-pos .cf-suit {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 1.5em;
    line-height: 1;
}

/* Schmale Fenster (iPad hochkant): KI-Karten sind nur 32 px breit; Schrift und
   Ueberlappung so, dass auch "10" und das Farbsymbol lesbar bleiben */
@media (max-width: 768px) {
    .ai-players-area .player-cards.hand .card-mini {
        font-size: 1.05rem;
        padding: 2px 1px;
    }
    .ai-players-area .player-cards.hand .card:not(:first-child) {
        margin-left: -8px;
    }
}
