:root{
    --bg1: #0f1724;
    --bg2: #0b2545;
    --card-bg: rgba(255,255,255,0.04);
    --panel-bg: rgba(255,255,255,0.03);
    --accent: #f6c85f;
    --muted: rgba(255,255,255,0.75);
}

html,body{height:100%;margin:0;}
body{
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial;
    background: radial-gradient(1200px 600px at 10% 10%, rgba(246,200,95,0.06), transparent),
                linear-gradient(180deg, var(--bg1), var(--bg2));
    color: var(--muted);
    display:flex;
    /* anchor app to the top so the top of the box doesn't move when inner content height changes */
    align-items:flex-start;
    justify-content:center;
    padding:28px;
    box-sizing:border-box;
}

/* make .app a column so header stays fixed and the card area can scroll without changing the top */
.app {
    width:100%;
    max-width:900px;
    background: linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
    border-radius:14px;
    padding:26px;
    box-shadow: 0 10px 30px rgba(2,6,23,0.6), inset 0 1px 0 rgba(255,255,255,0.02);
    backdrop-filter: blur(6px);
    display:flex;
    flex-direction:column;
    gap:16px;
    /* set a sensible min-height so the layout is stable; card/result area will expand if content grows */
    min-height: 520px;
    /* allow the app to grow a bit more so large images/buttons aren't clipped */
    max-height: calc(100vh - 28px);
    box-sizing:border-box;
}

header {
    display:flex;
    flex-direction:column;
    gap:16px;
    margin:0; /* remove bottom margin so header position is stable */
    flex:0 0 auto; /* don't let header grow or shrink */
}

.header-text {
    text-align:center;
}

h1 {
    font-size:20px;
    margin:0;
    color: #fff;
    letter-spacing:0.2px;
}

p.lead {
    margin:0;
    color: rgba(255,255,255,0.7);
    font-size:13px;
}

#controls {
    display:flex;
    align-items:center;
    justify-content:center;
    gap:16px;
    flex-wrap:wrap;
}

#mana-container {
    display:flex;
    gap:12px;
    align-items:center;
    justify-content:center;
    padding:8px;
    border-radius:10px;
    background: linear-gradient(90deg, rgba(255,255,255,0.012), transparent);
}

.mana-symbol {
    width: 50px;
    height: 50px;
    margin: 0;
    cursor: pointer;
    filter: grayscale(100%);
    transition: filter 140ms ease, transform 140ms ease, box-shadow 140ms ease;
    border-radius: 8px;
    object-fit: contain;
    background: rgba(255,255,255,0.02);
    padding:6px;
    box-sizing:border-box;
    border: 1px solid rgba(255,255,255,0.03);
}

.mana-symbol:hover {
    filter: none;
    transform: translateY(-2px) scale(1.04);
    box-shadow: 0 4px 16px rgba(2,6,23,0.4);
}

/* Selected state: remove grayscale, add a sleek highlight border */
.selected {
    filter: none;
    transform: scale(1.02);
    box-shadow: 0 0 0 2px rgba(246,200,95,0.8), 0 4px 16px rgba(246,200,95,0.3);
    border-color: rgba(246,200,95,0.6);
}

#get-commander-btn {
    background: linear-gradient(180deg, #ffd67a, #f6b93a);
    color: #0a0a0a;
    border: none;
    padding: 10px 16px;
    font-weight:600;
    border-radius:10px;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(246,200,95,0.14);
    transition: transform 140ms ease, box-shadow 140ms ease, opacity 120ms;
    /* Improve button performance */
    will-change: transform, box-shadow, opacity;
    contain: layout style;
}

#get-commander-btn:active { 
    transform: translateY(1px) scale(0.998); 
}

#get-commander-btn:hover { 
    box-shadow: 0 10px 26px rgba(246,200,95,0.18); 
}

#get-commander-btn:disabled {
    cursor: not-allowed;
    pointer-events: none;
}

/* Pauper Commander Toggle */
#pauper-toggle-container {
    display: flex;
    justify-content: center;
    margin-top: 8px;
}

.pauper-toggle {
    display: inline-block;
    cursor: pointer;
}

.pauper-toggle input {
    display: none;
}

.pauper-slider {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 16px;
    background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
    transition: all 140ms ease;
    position: relative;
    min-width: 140px;
}

.pauper-label {
    color: rgba(255,255,255,0.7);
    font-size: 13px;
    font-weight: 500;
    transition: color 140ms ease;
}

.pauper-toggle:hover .pauper-slider {
    background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.03));
    border-color: rgba(255,255,255,0.12);
}

.pauper-toggle input:checked + .pauper-slider {
    background: linear-gradient(180deg, rgba(246,200,95,0.2), rgba(246,200,95,0.1));
    border-color: rgba(246,200,95,0.3);
    box-shadow: 0 0 0 2px rgba(246,200,95,0.1);
}

.pauper-toggle input:checked + .pauper-slider .pauper-label {
    color: #f6c85f;
    font-weight: 600;
}

/* card area: prefer expanding the app rather than adding an inner scrollbar */
#result {
    margin-top: 0;
    display:flex;
    gap:18px;
    /* Align items to the top so long/short titles don't vertically re-center the image */
    align-items:flex-start;
    /* spread meta and image so they don't crowd/shift each other */
    justify-content:space-between;
    padding:18px;
    border-radius:12px;
    background: linear-gradient(180deg, rgba(255,255,255,0.012), rgba(255,255,255,0.01));
    border: 1px solid rgba(255,255,255,0.03);
    box-shadow: 0 8px 30px rgba(2,6,23,0.5);

    /* remove internal scrolling and allow this area to grow so image and controls aren't clipped */
    height: auto;
    min-height: 520px; /* increased to avoid cutting off tall card/art + buttons */
    overflow: visible;
    /* prevent the result pane from shrinking when other parts change */
    flex: 0 0 auto;
}

/* Reserve a stable area for the text/meta so varying title lengths don't push the image */
#result .meta {
    min-width: 220px;
    max-width:460px;
    text-align:left;
    color: rgba(255,255,255,0.9);
    /* give a fixed minimum height so the image position remains stable */
    min-height: 56px;
    box-sizing: border-box;
}

/* clamp the title to at most 2 lines to avoid vertical reflow when very long names load */
#result h2 {
    margin: 0 0 6px 0;
    font-size:18px;
    color:#fff;
    line-height:1.2;
    max-height: 2.4em; /* 2 lines */
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
}

#result p {
    margin:0;
    color: rgba(255,255,255,0.78);
    font-size:13px;
}

/* image area is a vertical stack: image on top, controls below */
#image-area {
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:flex-start; /* keep image at the top of its column */
    gap:8px;
    min-width: 340px; /* reserve space so the image doesn't shift horizontally */
    box-sizing:border-box;
}

#result img {
    max-width: 300px;
    max-height: 640px; /* allow taller images if needed */
    display:block;
    margin:0 auto;
    border-radius:8px;
    box-shadow: 0 8px 26px rgba(2,6,23,0.6);
    width: auto;
    height: auto;
}

footer {
    margin-top:12px;
    text-align:center;
    color: rgba(255,255,255,0.45);
    font-size:12px;
    flex:0 0 auto;
}

.flip-controls {
    display:flex;
    gap:8px;
    align-items:center;
    justify-content:center;
    margin-top:8px;
}

.flip-btn {
    background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
    color: var(--muted);
    border: none;
    padding: 6px 10px;
    border-radius:8px;
    cursor:pointer;
}

@media (max-width:640px){
    .app{ padding:18px; min-height: 560px; }
    /* on small screens prefer auto height but keep a reasonable minimum so controls are visible */
    #result{ 
        flex-direction:column; 
        gap:12px; 
        height: auto; 
        min-height: 420px;
        align-items:center; 
        justify-content:center; 
        overflow: visible;
    }
    #image-area { width: 100%; min-width: auto; }
    #result h2 { -webkit-line-clamp: 3; line-clamp: 3; max-height: 3.6em; }
    #result img { max-height: 52vh; } /* keep images reasonable on very small screens */
}