:root {
    --bg-primary: #0b0a10;
    --bg-secondary: #141219;
    --bg-tertiary: #1d1a24;
    --bg-card: #17151c;
    --text-primary: #f2f0f5;
    --text-secondary: #a8a3b5;
    --text-muted: #726d80;
    --border: #2c2836;
    --accent: #f0b429;
    --accent-hover: #ffc94d;
    --link: #b9a8f0;
    --danger: #e5484d;
    --danger-hover: #ff6369;
    --success: #2fae66;
    --success-hover: #3ec97d;
    --elixir: #9333ea;
    /* Atmospheric glow only (hero backgrounds) - never used for text/borders/buttons, so gold
       stays the one confident accent everywhere a highlight color is actually needed. */
    --glow-violet: #8b5cf6;
    --glow-indigo: #4f46e5;
    --shadow: rgba(0, 0, 0, 0.5);
}

[data-theme="light"],
:root:not([data-theme="dark"]) {
    --bg-primary: #faf9f7;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f0eeea;
    --bg-card: #ffffff;
    --text-primary: #1a1720;
    --text-secondary: #5c5668;
    --text-muted: #8b8598;
    --border: #e3e0e8;
    --shadow: rgba(20, 15, 30, 0.08);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg-primary: #0b0a10;
        --bg-secondary: #141219;
        --bg-tertiary: #1d1a24;
        --bg-card: #17151c;
        --text-primary: #f2f0f5;
        --text-secondary: #a8a3b5;
        --text-muted: #726d80;
        --border: #2c2836;
        --shadow: rgba(0, 0, 0, 0.5);
    }
}

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background:
        radial-gradient(1000px 480px at 20% -12%, rgba(139, 92, 246, 0.16), transparent 60%),
        radial-gradient(900px 520px at 85% 6%, rgba(79, 70, 229, 0.14), transparent 55%),
        var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a {
    color: var(--link);
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}

a:focus-visible,
button:focus-visible,
input:focus-visible,
.card-slot:focus-visible,
.card-picker-item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

img {
    max-width: 100%;
    height: auto;
}

/* Navbar */
.navbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1.5rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 4px 20px var(--shadow);
    position: sticky;
    top: 0;
    z-index: 100;
}

.navbar-brand a {
    position: relative;
    display: inline-block;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--accent);
    text-decoration: none;
    letter-spacing: 0.3px;
    padding-bottom: 4px;
}
.navbar-brand a::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 2px;
    background: linear-gradient(90deg, var(--accent), transparent);
    border-radius: 2px;
    opacity: 0.7;
}
.navbar-brand a:hover {
    text-decoration: none;
}

.navbar-links {
    display: flex;
    gap: 0.5rem;
    margin-left: auto;
}

.nav-link {
    padding: 0.375rem 0.75rem;
    border-radius: 6px;
    color: var(--text-secondary);
    transition: color 0.15s, background 0.15s;
}
.nav-link:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
    text-decoration: none;
}

.navbar-auth {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.username {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

/* Container */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    flex: 1;
    animation: fadeIn 0.35s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Footer */
.footer {
    text-align: center;
    padding: 1.5rem;
    color: var(--text-muted);
    font-size: 0.8125rem;
    border-top: 1px solid var(--border);
    margin-top: auto;
}

/* Buttons - ghost/outline is the default ("secondary"); .btn-primary is the gradient pill CTA */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1.25rem;
    border: 1.5px solid var(--border);
    border-radius: 999px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease, transform 0.05s ease;
    text-decoration: none;
    line-height: 1.4;
    gap: 0.375rem;
}
.btn:hover {
    border-color: var(--accent);
    color: var(--text-primary);
    text-decoration: none;
    transform: translateY(-1px);
}
.btn:active {
    transform: translateY(1px);
}
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

.btn-sm {
    padding: 0.3rem 0.875rem;
    font-size: 0.8125rem;
}

.btn-lg {
    padding: 0.8rem 1.75rem;
    font-size: 1rem;
}

.btn-primary {
    background: linear-gradient(135deg, #ffe8a3, var(--accent) 55%, #c98a12);
    color: #2b1502;
    font-weight: 700;
    border: 1.5px solid transparent;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.45) inset, 0 4px 16px rgba(240, 180, 41, 0.3);
}
.btn-primary:hover {
    background: linear-gradient(135deg, #fff0bf, var(--accent-hover) 55%, #d69914);
    color: #2b1502;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5) inset, 0 6px 22px rgba(240, 180, 41, 0.45);
}

.btn-danger {
    color: var(--danger);
    border-color: var(--danger);
}
.btn-danger:hover {
    background: var(--danger);
    color: #fff;
    border-color: var(--danger);
    box-shadow: 0 2px 8px rgba(218, 54, 51, 0.3);
}

.btn-icon {
    padding: 0.4rem;
    min-width: 2.25rem;
    border-radius: 50%;
}

/* Page Header */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-secondary);
}
.empty-state p {
    margin-bottom: 0.75rem;
}

/* Deck Grid List (gallery / dashboard) */
.deck-grid-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.75rem;
}

/* Deck Card (preview tile) */
.deck-card {
    background: linear-gradient(165deg, var(--bg-card), var(--bg-secondary));
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), 0 1px 3px var(--shadow);
    transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}
.deck-card:hover {
    border-color: var(--accent);
    box-shadow: 0 10px 24px var(--shadow);
    transform: translateY(-2px);
}

.deck-card-link {
    display: block;
    padding: 1rem;
    color: inherit;
    text-decoration: none;
}
.deck-card-link:hover {
    text-decoration: none;
}

.deck-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.deck-card-title {
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.deck-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 0.75rem;
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

.deck-owner {}
.avg-elixir {
    color: var(--elixir);
    font-weight: 600;
}

.deck-card-actions {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
}
.deck-card-actions form {
    display: inline;
}

/* Card Grid (2x4) */
.deck-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.625rem;
}

/*
 * No CSS-drawn border/rarity-color/glow frame here on purpose: the Clash Royale card art itself
 * already has its rarity/evolution/hero frame baked into the source image, so a second CSS frame
 * on top of it just doubled up as a redundant ring. Only corner clipping, an empty-slot
 * affordance, and interaction states (hover/selected/picked) live here.
 */
.card-slot,
.card-picker-item {
    position: relative;
    aspect-ratio: 3 / 4;
    border-radius: 10px;
    overflow: hidden;
    background: linear-gradient(160deg, var(--bg-tertiary), var(--bg-card));
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card-slot.empty {
    border: 2px dashed var(--border);
    background: var(--bg-tertiary);
}

.card-slot .card-image,
.card-picker-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    /* Card art bakes Supercell's own elixir readout into the top-left corner. Scale up slightly
       and shift the crop upward so that corner sits fully under our own elixir badge instead of
       peeking out beside it; the oversize keeps the frame fully covered on every edge. */
    transform: scale(1.14) translateY(-9%);
    transform-origin: center top;
}

.card-slot:not(.empty) .empty-slot-icon {
    display: none;
}

.elixir-badge {
    position: absolute;
    top: -6px;
    left: -6px;
    background: radial-gradient(circle at 30% 30%, #c072ea, var(--elixir));
    color: #fff;
    font-size: 0.6875rem;
    font-weight: 800;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    border: 2px solid rgba(255, 255, 255, 0.85);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.45);
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
    z-index: 2;
}

.card-name-overlay {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 0.3rem 0.375rem 0.3125rem;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.88));
    color: #fff;
    font-size: 0.625rem;
    font-weight: 700;
    text-align: center;
    line-height: 1.15;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    z-index: 1;
}

.badge {
    display: inline-block;
    padding: 0.125rem 0.375rem;
    border-radius: 4px;
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.public-badge {
    background: var(--success);
    color: #fff;
}

.private-badge {
    background: var(--text-muted);
    color: #fff;
}

.empty-slot-icon {
    font-size: 1.75rem;
    font-weight: 300;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.15s ease;
}

.deck-grid.editable .card-slot {
    cursor: pointer;
}
.deck-grid.editable .card-slot:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px var(--shadow);
}
.deck-grid.editable .card-slot:hover .empty-slot-icon {
    color: var(--accent);
}
.card-slot.selected {
    animation: selectPulse 1.6s ease-in-out infinite;
}

.card-slot:not(.empty) .card-image {
    animation: cardPlace 0.25s ease;
}

@keyframes selectPulse {
    0%, 100% {
        box-shadow: 0 0 0 3px var(--accent), 0 6px 16px var(--shadow);
    }
    50% {
        box-shadow: 0 0 0 5px var(--accent), 0 8px 20px var(--shadow);
    }
}

@keyframes cardPlace {
    from {
        opacity: 0;
        transform: scale(0.97) translateY(-9%);
    }
    to {
        opacity: 1;
        transform: scale(1.14) translateY(-9%);
    }
}

@keyframes pickedPulse {
    0% {
        box-shadow: 0 0 0 0 rgba(240, 180, 41, 0.5);
    }
    100% {
        box-shadow: 0 0 0 10px rgba(240, 180, 41, 0);
    }
}

/* Deck View */
.deck-view {
    max-width: 700px;
    margin: 0 auto;
}

.deck-view-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.deck-view-grid {
    max-width: 420px;
    margin: 0 auto 1.5rem;
}

.deck-view-stats {
    display: flex;
    gap: 2rem;
    margin-bottom: 1.5rem;
}

.stat {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-value {
    font-size: 1.125rem;
    font-weight: 600;
}

.deck-view-actions {
    display: flex;
    gap: 0.75rem;
    margin: 1.5rem 0;
}
.deck-view-actions form {
    display: inline;
}

.deck-view-link {
    padding: 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 6px;
    word-break: break-all;
    font-size: 0.875rem;
    margin-top: 1.5rem;
}

/* Card Details Table */
.card-details-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1.5rem;
    overflow-x: auto;
    display: block;
}
.card-details-table thead,
.card-details-table tbody,
.card-details-table tr {
    display: table;
    width: 100%;
    table-layout: fixed;
}
.card-details-table th,
.card-details-table td {
    padding: 0.5rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.card-details-table th {
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}
.card-details-table tbody tr:hover {
    background: var(--bg-secondary);
}

/* Forms */
.deck-form {
    max-width: 1100px;
    margin: 0 auto;
}

.deck-form-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    align-items: start;
}

.deck-form-picker .card-picker-grid {
    max-height: 400px;
}

.deck-form-submit {
    margin-top: 1.5rem;
    width: 100%;
}

@media (min-width: 960px) {
    .deck-form-grid {
        grid-template-columns: minmax(320px, 420px) 1fr;
    }
    .deck-form-picker .card-picker-grid {
        max-height: 640px;
    }
    .deck-form-submit {
        width: auto;
    }
}

.deck-form-edit {
    max-width: 460px;
    margin: 0 auto;
}
.deck-form-edit .deck-form-preview {
    max-width: 340px;
    margin: 0 auto;
}
.deck-form-edit .deck-form-submit {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
@media (min-width: 960px) {
    .deck-form-edit .deck-form-submit {
        width: 100%;
    }
}

.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.375rem;
    color: var(--text-secondary);
}

.form-group label.checkbox-label {
    display: inline-flex;
    align-items: center;
    gap: 0.625rem;
    font-size: 0.9375rem;
    font-weight: 400;
    color: var(--text-primary);
    margin-bottom: 0;
    cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}
.checkbox-box {
    flex-shrink: 0;
    width: 1.125rem;
    height: 1.125rem;
    border: 2px solid var(--border);
    border-radius: 4px;
    background: var(--bg-secondary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.checkbox-label input[type="checkbox"]:checked + .checkbox-box {
    background: var(--accent);
    border-color: var(--accent);
}
.checkbox-label input[type="checkbox"]:checked + .checkbox-box::after {
    content: "";
    width: 0.3rem;
    height: 0.55rem;
    border: solid #000;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg) translate(-1px, -1px);
}
.checkbox-label input[type="checkbox"]:focus-visible + .checkbox-box {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.form-input {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.9375rem;
    color: var(--text-primary);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 6px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    font-family: inherit;
}
.form-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(240, 180, 41, 0.15);
}

.form-inline {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
}
.form-inline .form-input {
    flex: 1;
}

.feedback {
    font-size: 0.8125rem;
    margin-top: 0.375rem;
    min-height: 1.25rem;
}

/* Card Picker */
.card-picker {
    margin-bottom: 1.5rem;
}
.card-picker .form-input {
    margin-bottom: 0.75rem;
}

/*
 * Flex-wrap of explicitly fixed-size tiles rather than an auto-fill grid: each tile carries its
 * own width/height instead of deriving from aspect-ratio + an implicit track size, so there is no
 * intrinsic-sizing edge case that can collapse a track and let tiles overlap. align-content keeps
 * wrapped rows packed against the top instead of spreading across the scrollable height.
 */
.card-picker-grid {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 0.75rem;
    max-height: 400px;
    overflow-y: auto;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-secondary);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), inset 0 1px 4px rgba(0, 0, 0, 0.2);
}

.card-picker-grid .card-picker-item {
    flex: 0 0 92px;
    width: 92px;
    height: 122.67px;
}

.card-picker-item {
    cursor: pointer;
}
.card-picker-item:hover {
    transform: translateY(-3px) scale(1.03);
    box-shadow: 0 8px 18px var(--shadow);
    z-index: 3;
}
.card-picker-item:active {
    transform: translateY(-1px) scale(0.97);
}
.card-picker-item.picked {
    opacity: 0.35;
    cursor: default;
    transform: none;
    animation: pickedPulse 0.4s ease;
}
.card-picker-item.picked:hover {
    transform: none;
    box-shadow: none;
}
.card-picker-item.picked::after {
    content: "In Deck";
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    z-index: 3;
}
.card-picker-item.hidden {
    display: none;
}

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    border: 1px solid;
}

.alert-error {
    background: rgba(218, 54, 51, 0.1);
    color: var(--danger);
    border-color: var(--danger);
}

.alert-success {
    background: rgba(35, 134, 54, 0.1);
    color: var(--success);
    border-color: var(--success);
}

/* Token Display */
.token-display {
    padding: 1rem;
}
.token-value {
    display: block;
    padding: 0.75rem;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.9375rem;
    word-break: break-all;
    margin: 0.5rem 0;
    user-select: all;
}
.token-warning {
    font-size: 0.8125rem;
    font-weight: 600;
}

/* Settings */
.settings-section {
    margin-bottom: 2rem;
}
.settings-section h2 {
    margin-bottom: 0.75rem;
}

.token-form {
    margin-bottom: 1.5rem;
}

.tokens-table {
    width: 100%;
    border-collapse: collapse;
}
.tokens-table th,
.tokens-table td {
    padding: 0.5rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.tokens-table th {
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

.muted {
    color: var(--text-muted);
}

/* Login Page */
.login-page {
    text-align: center;
    padding: 4rem 1rem;
}
.login-page h1 {
    margin-bottom: 0.5rem;
}
.login-page p {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

/* Responsive */
@media (max-width: 768px) {
    .navbar {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .navbar-links {
        order: 3;
        width: 100%;
        justify-content: center;
        margin-left: 0;
    }
    .deck-grid-list {
        grid-template-columns: 1fr;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .deck-view-stats {
        flex-direction: column;
        gap: 1rem;
    }
    .form-inline {
        flex-direction: column;
        align-items: stretch;
    }
    .card-picker-grid .card-picker-item {
        flex: 0 0 64px;
        width: 64px;
        height: 85.33px;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 1rem;
    }
    .deck-grid {
        gap: 0.25rem;
    }
    .elixir-badge {
        width: 1rem;
        height: 1rem;
        font-size: 0.5rem;
    }
}

/* Headings - light-weight display type; .heading-accent carries the one bolded key term */
h1 {
    font-size: 2.25rem;
    font-weight: 300;
    letter-spacing: -0.01em;
    margin-bottom: 1.5rem;
    color: var(--text-primary);
}
.heading-accent {
    font-weight: 700;
    color: var(--accent);
    text-shadow: 0 0 22px rgba(240, 180, 41, 0.22);
}
h2 { font-size: 1.375rem; font-weight: 700; margin-bottom: 0.75rem; color: var(--text-primary); }
h3 { font-size: 1.0625rem; font-weight: 600; margin-bottom: 0.75rem; }

.section-label {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    margin-top: 0.5rem;
    margin-bottom: 0.875rem;
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
