:root {
    --bg-color: #0f172a;
    --card-bg: #1e293b;
    --input-bg: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent-color: #38bdf8;
    /* Light Blue */
    --accent-glow: rgba(56, 189, 248, 0.3);
    --gold: #fbbf24;
    --platinum: #e2e8f0;
    --palladium: #06b6d4;
    --rhodium: #a78bfa;
    --success: #22c55e;
    --danger: #ef4444;
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Inter', sans-serif;
}

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

body {
    font-family: var(--font-body);
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    padding: 20px;
    -webkit-user-select: none;
    /* Safari */
    user-select: none;
}

input,
textarea {
    -webkit-user-select: text;
    user-select: text;
}

.app-container {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 0;
    flex-wrap: wrap;
    gap: 1rem;
}

.logo-group {
    display: flex;
    align-items: center;
    gap: 2rem;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

/* Margin Control */
.margin-control {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    color: var(--text-secondary);
    font-size: 0.75rem;
    width: 140px;
}

.margin-inputs {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.margin-control input[type="number"] {
    width: 50px;
    padding: 0.25rem;
    border-radius: 4px;
    background: rgba(30, 41, 59, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--accent-color);
    font-weight: 700;
    text-align: center;
}

.margin-control input[type="range"] {
    flex: 1;
    accent-color: var(--accent-color);
    cursor: pointer;
}

.search-bar {
    position: sticky;
    top: 10px;
    z-index: 100;
    width: 300px;
}

.search-bar i {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
}

.search-bar input {
    width: 100%;
    padding-left: 2.5rem;
    background: rgba(30, 41, 59, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
}

.search-bar input:focus {
    border-color: var(--accent-color);
    background: rgba(30, 41, 59, 0.9);
}

/* Select Input */
select {
    width: 100%;
    background: var(--input-bg);
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 1rem;
    cursor: pointer;
    appearance: none;
    /* Custom arrow ideally, simplified for now */
    background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%2394a3b8%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    background-size: 0.65em auto;
}

select:focus {
    outline: none;
    border-color: var(--accent-color);
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1.5rem;
    color: var(--text-primary);
}

.logo i {
    color: var(--accent-color);
    filter: drop-shadow(0 0 8px var(--accent-glow));
}

.logo h1 {
    font-family: var(--font-heading);
    font-weight: 700;
    letter-spacing: -0.02em;
}

.logo .highlight {
    color: var(--accent-color);
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--card-bg);
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.875rem;
    color: var(--text-secondary);
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.dot {
    width: 8px;
    height: 8px;
    background-color: var(--success);
    border-radius: 50%;
    box-shadow: 0 0 8px var(--success);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        opacity: 1;
        box-shadow: 0 0 8px var(--success);
    }

    50% {
        opacity: 0.5;
        box-shadow: 0 0 2px var(--success);
    }

    100% {
        opacity: 1;
        box-shadow: 0 0 8px var(--success);
    }
}

/* Ticker */
.market-ticker {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1rem;
}

.ticker-card {
    background: rgba(30, 41, 59, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 16px;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: transform 0.2s, box-shadow 0.2s;
    position: relative;
    overflow: hidden;
}

.ticker-card::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
}

#card-platinum::after {
    background: var(--platinum);
}

#card-palladium::after {
    background: var(--palladium);
}

#card-rhodium::after {
    background: var(--rhodium);
}

.ticker-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.metal-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.metal-symbol {
    font-weight: 700;
    opacity: 0.5;
}

.metal-price-input {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.5rem;
    font-family: var(--font-heading);
}

.price-input {
    background: transparent;
    border: none;
    border-bottom: 2px solid rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    font-size: 1.5rem;
    font-weight: 700;
    width: 120px;
    padding: 0;
    font-family: var(--font-heading);
    appearance: textfield;
}

.price-input:focus {
    outline: none;
    border-color: var(--accent-color);
}

/* Remove Arrows from Number Input */
.price-input::-webkit-outer-spin-button,
.price-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.return-control {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    padding-top: 0.5rem;
}

.return-control input {
    width: 60px;
    padding: 2px 4px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--accent-color);
    font-weight: 600;
    text-align: right;
    font-size: 0.9rem;
}

.item-make-badge {
    display: inline-block;
    font-size: 0.75rem;
    background: rgba(255, 255, 255, 0.1);
    color: var(--accent-color);
    padding: 2px 8px;
    border-radius: 4px;
    margin-bottom: 4px;
    text-transform: uppercase;
    font-weight: 600;
}

.metal-price {
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 600;
    display: flex;
    align-items: baseline;
    gap: 2px;
}

.metal-price .unit {
    font-size: 1rem;
    color: var(--text-secondary);
    font-weight: 400;
}

.change {
    font-size: 0.875rem;
    font-weight: 500;
}

.change.positive {
    color: var(--success);
}

.change.negative {
    color: var(--danger);
}

/* Main Layout */
main {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 2rem;
    align-items: start;
}

@media(max-width: 900px) {
    main {
        grid-template-columns: 1fr;
    }
}

/* Forms & Cards */
.card {
    background: rgba(30, 41, 59, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 16px;
    padding: 1.5rem;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.input-section {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

h2 {
    font-family: var(--font-heading);
    font-size: 1.25rem;
    margin-bottom: 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

h2 i {
    color: var(--accent-color);
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

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

input {
    background: var(--input-bg);
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 1rem;
    transition: border-color 0.2s;
}

input:focus {
    outline: none;
    border-color: var(--accent-color);
}

.metal-inputs {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.btn-primary {
    width: 100%;
    background: linear-gradient(135deg, var(--accent-color), #0ea5e9);
    color: #fff;
    border: none;
    padding: 0.875rem;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
    margin-top: 0.5rem;
}

.btn-primary:hover {
    opacity: 0.9;
}

/* Summary Card */
.total-value {
    font-family: var(--font-heading);
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--success);
    margin-bottom: 1.5rem;
}

.total-value .currency {
    font-size: 1.5rem;
    color: var(--text-secondary);
    margin-right: 0.25rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.stat-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 0.75rem;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.stat-item .label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.stat-item .val {
    font-weight: 600;
}

/* Catalog List */
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.btn-text {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: color 0.2s;
}

.btn-text:hover {
    color: var(--danger);
}

.catalog-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* File Input */
.file-input {
    padding: 0.5rem;
    font-size: 0.875rem;
    background: rgba(0, 0, 0, 0.2);
}

/* Catalog Item with Image */
.catalog-item {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1rem;
    border: 1px solid rgba(255, 255, 255, 0.05);
    display: grid;
    grid-template-columns: 80px 1fr auto auto;
    gap: 1rem;
    align-items: center;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.item-img-container {
    width: 80px;
    height: 80px;
    background: #1a1a1a;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.item-thumb {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.item-main h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.item-breakdown {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.item-breakdown span i {
    margin-right: 4px;
}

.item-price {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--success);
    margin-left: auto;
    /* Push price to right on flex */
}

/* Form Actions */
.form-actions {
    display: flex;
    gap: 1rem;
    margin-top: 0.5rem;
}

.btn-secondary {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-secondary);
    padding: 0.875rem;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    flex: 1;
}

.btn-secondary:hover {
    border-color: var(--text-primary);
    color: var(--text-primary);
}

.form-actions .btn-primary {
    flex: 2;
    margin-top: 0;
}

.hidden {
    display: none !important;
}

/* Image Preview in Edit Mode */
.image-preview-container {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.5rem;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    margin-bottom: 0.5rem;
}

.image-preview-container img {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 4px;
}

.text-danger {
    color: var(--danger);
    font-size: 0.9rem;
}

/* Item Action Buttons */
.item-actions {
    display: flex;
    gap: 0.5rem;
}

.btn-icon {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-edit {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent-color);
}

.btn-edit:hover {
    background: rgba(56, 189, 248, 0.2);
}

/* Tabs */
.tabs-nav {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 0.5rem;
}

.tab-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1rem;
    font-weight: 500;
    padding: 0.5rem 1rem;
    cursor: pointer;
    transition: all 0.2s;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.tab-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
}

.tab-btn.active {
    background: var(--accent-color);
    color: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.tab-content {
    display: block;
    width: 100%;
}

.tab-content.hidden {
    display: none;
}

/* User List Item */
.user-item {
    display: grid;
    grid-template-columns: 1.5fr 1.5fr 1fr 1fr auto;
    gap: 1rem;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-md);
    align-items: center;
    margin-bottom: 0.5rem;
}

.user-item h4 {
    color: var(--text-primary);
    font-size: 1rem;
    margin: 0;
}

.user-item span {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.user-item .badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 600;
}

.badge.active {
    background: rgba(0, 255, 136, 0.2);
    color: #00ff88;
}

.badge.inactive {
    background: rgba(255, 255, 255, 0.1);
    color: #aaa;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.9);
    backdrop-filter: blur(4px);

    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 1;
    transition: opacity 0.3s;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
}



.modal-content {
    width: 90%;
    max-width: 450px;
    background: var(--card-bg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    /* Mobile Scroll Fix */
    max-height: 90vh;
    overflow-y: auto;
    border-radius: 12px;
}

.modal-actions {
    display: flex;
    gap: 1rem;
    margin-top: 1.5rem;
}

/* Buy Button */
.btn-buy {
    background: rgba(34, 197, 94, 0.1);
    /* Green tint */
    color: var(--success);
}

.btn-buy:hover {
    background: var(--success);
    color: #fff;
}

.btn-delete {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
}

.btn-delete:hover {
    background: rgba(239, 68, 68, 0.2);
}

/* Mobile Optimizations */
@media(max-width: 600px) {
    body {
        padding: 10px;
        align-items: flex-start;
    }

    .logo h1 {
        font-size: 1.25rem;
    }

    /* Stack Tickers */
    .market-ticker {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .ticker-card {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 1rem;
    }

    .ticker-card .metal-price {
        font-size: 1.25rem;
    }

    /* Stack Main Content */
    main {
        grid-template-columns: 1fr;
    }

    /* Bigger Inputs for Touch */
    input {
        padding: 1rem;
        font-size: 16px;
        /* prevent zoom on iOS */
    }

    .btn-primary {
        padding: 1rem;
        font-size: 1.1rem;
    }

    /* Stack Catalog Items */
    .catalog-item {
        grid-template-columns: 60px 1fr;
        grid-template-areas:
            "img info"
            "price action";
        gap: 0.75rem;
        padding: 1rem;
    }

    .item-img-container {
        grid-area: img;
        width: 60px;
        height: 60px;
    }

    .item-main {
        grid-area: info;
    }

    .item-price {
        grid-area: price;
        font-size: 1.25rem;
        align-self: center;
        margin-left: 0;
    }

    .item-actions {
        grid-area: action;
        justify-self: end;
        margin-top: 0.5rem;
    }

    .btn-icon {
        width: 42px;
        height: 42px;
    }

    .header-controls {
        width: 100%;
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .search-bar {
        width: 100%;
        order: 3;
        /* Push search to bottom of header controls on mobile */
        margin-top: 0.75rem;
        /* Sticky Search on Mobile */
        position: sticky;
        top: 10px;
        z-index: 100;
        background: rgba(15, 23, 42, 0.95);
        /* Add background so content doesn't bleed through */
        padding: 5px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
    }

    /* Sticky Bottom Cart (Collapsible) */
    #purchaseCard.active-cart {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        margin: 0;
        border-radius: 16px 16px 0 0;
        z-index: 999;
        background: rgba(15, 23, 42, 0.98);
        backdrop-filter: blur(12px);
        border-top: 1px solid var(--accent-color);
        display: flex !important;
        flex-direction: column;
        gap: 0;
        padding: 0 1rem;
        box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.6);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        transform: translateY(calc(100% - 70px));
        /* Default: Collapsed (Show only header) */
        cursor: pointer;
    }

    #purchaseCard.active-cart.expanded {
        transform: translateY(0);
        /* Expanded */
        padding-bottom: 2rem;
    }

    /* Drag Handle Indicator */
    #purchaseCard.active-cart::before {
        content: '';
        display: block;
        width: 40px;
        height: 5px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 3px;
        margin: 8px auto;
    }

    /* Header Row (Always Visible) */
    #purchaseCard .cart-header {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 60px;
        /* Fixed height for header */
        padding-bottom: 10px;
    }

    /* Hidden content when collapsed */
    #purchaseCard .cart-content {
        opacity: 0;
        transition: opacity 0.2s;
        pointer-events: none;
    }

    #purchaseCard.active-cart.expanded .cart-content {
        opacity: 1;
        pointer-events: auto;
    }

    /* Actions Row */
    #purchaseCard .form-actions {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 10px;
        margin-top: 10px;
    }

    #purchaseCard .btn-primary {
        padding: 0.75rem;
        font-size: 1.1rem;
        flex: 2;
    }

    #purchaseCard .btn-text {
        font-size: 0.9rem;
        display: flex;
        flex: 1;
        justify-content: center;
        padding: 0.75rem;
        background: rgba(255, 255, 255, 0.05);
        border-radius: 8px;
    }

    /* Hide non-essential cart elements on mobile (default) */
    #purchaseCard h2 {
        display: none;
    }

    /* Stats grid hidden when collapsed, visible when expanded */
    #purchaseCard .stats-grid {
        display: none;
    }

    #purchaseCard.active-cart.expanded .stats-grid {
        display: grid;
        width: 100%;
        margin-bottom: 15px;
        margin-top: 10px;
    }

    /* Adjust Total Value for Bar */
    #purchaseCard .total-value {
        font-size: 1.5rem;
        margin-bottom: 0;
    }

    /* Adjust Checkout Button */
    #purchaseCard .form-actions {
        margin-top: 0;
        width: auto;
    }

    #purchaseCard .btn-primary {
        padding: 0.5rem 1.5rem;
        font-size: 1rem;
        width: auto;
    }

    /* Add padding to body so content isn't covered */
    body {
        padding-bottom: 100px;
    }
}

/* Visibility Control via Class */
.purchase-card {
    display: none;
}

.purchase-card.active-cart {
    display: block !important;
}

.empty-state {
    text-align: center;
    padding: 3rem;
    color: var(--text-secondary);
    border: 2px dashed rgba(255, 255, 255, 0.05);
    border-radius: 16px;
}

.empty-state i {
    font-size: 2rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: var(--success);
    color: #fff;
    padding: 1rem 1.5rem;
    border-radius: 8px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    transform: translateY(0);
    opacity: 1;
    transition: all 0.3s;
    font-weight: 500;
}

.toast.hidden {
    transform: translateY(20px);
    opacity: 0;
    pointer-events: none;
}