/* --- Universal & General Styles --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    line-height: 1.6;
    background: #f5f5f5;
    color: #333;
    padding: 20px 0 0 0;
}

/* --- Header --- */
header {
    background: linear-gradient(135deg, #003087 0%, #0050e6 100%);
    color: white;
    padding: 1.5rem 0;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    position: sticky;
    top: 0;
    z-index: 100;
    text-align: center;
}

header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.5px;
}

header h3 {
    font-size: 1.1rem;
    font-weight: 400;
    margin: 0.25rem 0 1rem 0;
    opacity: 0.95;
}

#header-bottom-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1200px;
    margin: 1rem auto 0 auto;
    padding: 0 2rem;
}

/* --- Navigation --- */
#nav-links {
    display: flex;
    gap: 1rem;
    align-items: center;
}

#nav-links a,
#nav-links button {
    padding: 0.5rem 1.25rem;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 6px;
    color: white;
    text-decoration: none;
    font-weight: 500;
    transition: all 0.3s ease;
    cursor: pointer;
    font-size: 0.95rem;
    font-family: inherit;
    line-height: 1;
}

#nav-links a:hover,
#nav-links button:hover {
    background: rgba(255,255,255,0.2);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

#nav-links a.active-link,
.active-link {
    background: rgba(255,255,255,0.25);
    border-color: rgba(255,255,255,0.4);
    font-weight: bold;
    text-decoration: underline;
}

/* --- Theme Toggle Button --- */
#theme-toggle-btn {
    width: 3rem;
    height: 3rem;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.3);
    background: rgba(255,255,255,0.1);
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

#theme-toggle-btn:hover {
    background: rgba(255,255,255,0.2);
    transform: rotate(20deg);
}

/* --- Main Content & Layout --- */
main {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 0 2rem;
}

#app-content,
#listings-section {
    background: white;
    border-radius: 12px;
    padding: 2rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    margin-bottom: 2rem;
}

#app-content h2,
#listings-section h2 {
    font-size: 1.8rem;
    color: #333;
    text-align: center;
    margin-bottom: 2rem;
    position: relative;
}

#app-content h2::after,
#listings-section h2::after {
    content: '';
    display: block;
    width: 100px;
    height: 2px;
    background: #f0f0f0;
    margin: 0.75rem auto 0 auto;
}

/* --- Buttons --- */
button, .button {
    padding: 0.75rem 1.5rem;
    background: #0050e6;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1rem;
}

button:hover, .button:hover {
    background: #003087;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,80,230,0.3);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

#create-listing-btn {
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    padding: 1rem 2rem;
    font-size: 1.1rem;
    margin: 1rem 0;
}

#create-listing-btn:hover {
    background: linear-gradient(135deg, #218838 0%, #1aa179 100%);
    box-shadow: 0 6px 20px rgba(40,167,69,0.3);
}

/* --- Forms & Search --- */
form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.auth-form {
    max-width: 500px;
    margin-left: auto;
    margin-right: auto;
}

form label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: #495057;
}

form input[type="text"],
form input[type="email"],
form input[type="password"],
form input[type="number"],
form input[type="file"],
form textarea,
#search-input {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 1rem;
    transition: all 0.3s ease;
    font-family: inherit;
}

form input:focus,
form textarea:focus,
#search-input:focus {
    outline: none;
    border-color: #0050e6;
    box-shadow: 0 0 0 3px rgba(0,80,230,0.1);
}

form textarea {
    min-height: 120px;
    resize: vertical;
}

#search-form {
    display: flex;
    gap: 1rem;
    max-width: 600px;
    margin: 0 auto 2rem auto;
}

#search-input {
    flex: 1;
}

/* --- Listings Grid & Cards --- */
#listings-grid,
#my-listings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-top: 2rem;
}

.listing-card {
    background: white;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    transition: all 0.3s ease;
    cursor: pointer;
}

.listing-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}

.listing-card img {
    width: 100%;
    height: 200px;
    object-fit: cover;
}

.listing-card-info {
    padding: 1rem;
}

.listing-card-info h3 {
    font-size: 1.2rem;
    margin: 0 0 0.5rem 0;
    color: #333;
}

.listing-card-info p {
    font-size: 1.1rem;
    font-weight: 600;
    color: #28a745;
    margin: 0 0 0.75rem 0;
}

.view-details-btn {
    width: 100%;
    padding: 0.5rem;
    background: #f8f9fa;
    color: #333;
    border: 1px solid #dee2e6;
    border-radius: 6px;
    font-weight: 500;
    transition: all 0.2s ease;
    margin-top: 0.75rem;
    pointer-events: none;
}

.view-details-btn:hover {
    background: #003087;
    color: white;
    transform: none;
    box-shadow: none;
}

/* --- Item Details --- */
.item-details h2 {
    margin-top: 0;
}

.item-details .price {
    font-size: 1.5rem;
    font-weight: bold;
    color: #333;
}

.item-details .seller {
    color: #555;
    font-style: italic;
}

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

#back-to-listings-btn {
    margin-bottom: 1.5rem;
}

/* --- Image Gallery --- */
.image-gallery {
    margin-bottom: 1.5rem;
}

.main-image-container {
    margin-bottom: 1rem;
}

#main-gallery-image {
    display: block;
    width: 100%;
    max-width: 600px;
    max-height: 500px;
    object-fit: contain;
    margin: 0 auto;
    border-radius: 12px;
}

.thumbnail-container {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
    flex-wrap: wrap;
}

.thumbnail-image {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
    border: 3px solid transparent;
    transition: all 0.2s ease;
    opacity: 0.6;
}

.thumbnail-image:hover {
    opacity: 1;
    transform: scale(1.05);
}

.thumbnail-image.active {
    border-color: #0050e6;
    opacity: 1;
}

/* --- Image Upload Preview with Drag & Drop --- */
#image-preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    margin: 20px 0;
    padding: 15px;
    min-height: 140px;
    border: 2px dashed #ddd;
    border-radius: 8px;
    background: #fafafa;
}

.preview-wrapper {
    position: relative;
    width: 120px;
    height: 120px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
    cursor: move;
    transition: transform 0.2s, box-shadow 0.2s;
    background: white;
}

.preview-wrapper:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* Preview thumbnail image */
.preview-thumbnail {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Drag handle - top left */
.drag-handle {
    position: absolute;
    top: 4px;
    left: 4px;
    color: #666;
    font-size: 12px;
    background: rgba(255, 255, 255, 0.95);
    padding: 2px 5px;
    border-radius: 4px;
    z-index: 2;
    pointer-events: none;
    line-height: 1;
}

/* Position badge - top right */
.position-badge {
    position: absolute;
    top: 4px;
    right: 4px;
    background: #4CAF50;
    color: white;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: bold;
    z-index: 2;
    pointer-events: none;
}

/* Remove button - bottom right corner */
.preview-wrapper .remove-preview-btn {
    position: absolute;
    bottom: 4px;
    right: 4px;
    width: 26px;
    height: 26px;
    background: rgba(244, 67, 54, 0.95);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-weight: bold;
    font-size: 14px;
    z-index: 2;
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.preview-wrapper:hover .remove-preview-btn {
    opacity: 1;
}

.preview-wrapper .remove-preview-btn:hover {
    background: #f44336;
    transform: scale(1.1);
}

/* Dragging states */
.preview-wrapper.dragging {
    opacity: 0.4;
    transform: scale(0.95);
    cursor: grabbing;
}

.preview-wrapper.drag-over {
    border-color: #4CAF50;
    border-width: 3px;
    background: #e8f5e9;
}

/* --- Status & Helper Classes --- */
.error {
    color: #dc3545;
    margin-top: 0.5rem;
    font-size: 0.9rem;
    text-align: center;
}

.success {
    color: #28a745;
    margin-top: 0.5rem;
    font-size: 0.9rem;
}

@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.6; }
    100% { opacity: 1; }
}

.loading {
    animation: pulse 1.5s infinite;
}

/* --- Upload Progress --- */
#upload-progress-container {
    margin-top: 1.5rem;
    text-align: center;
}

#upload-progress-container p {
    margin-bottom: 0.5rem;
    font-weight: 500;
}

#upload-progress-bar {
    width: 100%;
    height: 12px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #ccc;
}

#upload-progress-bar::-webkit-progress-bar {
    background-color: #f0f0f0;
}

#upload-progress-bar::-webkit-progress-value {
    background-color: #0050e6;
    transition: width 0.1s linear;
}

#upload-progress-bar::-moz-progress-bar {
    background-color: #0050e6;
}

/* --- Dark Mode Theme --- */
body.dark-mode {
    background-color: #1a1a1a;
    color: #e0e0e0;
}

body.dark-mode #app-content,
body.dark-mode #listings-section,
body.dark-mode .listing-card {
    background: #2a2a2a;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

body.dark-mode .listing-card:hover {
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

body.dark-mode #search-input,
body.dark-mode form input,
body.dark-mode form textarea {
    background: #3a3a3a;
    border-color: #4a4a4a;
    color: #e0e0e0;
}

body.dark-mode input::placeholder,
body.dark-mode textarea::placeholder {
    color: #888;
}

body.dark-mode #search-input:focus,
body.dark-mode form input:focus,
body.dark-mode form textarea:focus {
    border-color: #0050e6;
    box-shadow: 0 0 0 3px rgba(0,80,230,0.2);
}

body.dark-mode .view-details-btn {
    background: #3a3a3a;
    border-color: #4a4a4a;
    color: #e0e0e0;
}

body.dark-mode .view-details-btn:hover {
    background: #0050e6;
    border-color: #0050e6;
    color: white;
}

body.dark-mode h1,
body.dark-mode #app-content h2,
body.dark-mode #listings-section h2,
body.dark-mode .listing-card-info h3,
body.dark-mode .item-details .price {
    color: #ffffff;
}

body.dark-mode #app-content h2::after,
body.dark-mode #listings-section h2::after {
    background: #3a3a3a;
}

body.dark-mode #nav-links a,
body.dark-mode #nav-links button {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode #nav-links a:hover,
body.dark-mode #nav-links button:hover {
    background: rgba(255, 255, 255, 0.1);
}

body.dark-mode #theme-toggle-btn {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

/* Dark mode - Image preview */
body.dark-mode #image-preview-container {
    background: #2a2a2a;
    border-color: #444;
}

body.dark-mode .preview-wrapper {
    background: #333;
    border-color: #444;
}

body.dark-mode .preview-wrapper.drag-over {
    border-color: #4CAF50;
    background: #1b5e20;
}

body.dark-mode .drag-handle {
    background: rgba(50, 50, 50, 0.95);
    color: #aaa;
}

/* --- Media Queries for Responsiveness --- */
@media (max-width: 768px) {
    body {
        padding: 0;
    }

    header {
        padding: 1rem 0.5rem;
        position: relative;
    }

    header h1 {
        font-size: 1.8rem;
        margin-bottom: 0.25rem;
    }

    header h3 {
        font-size: 0.9rem;
        margin-bottom: 1rem;
    }

    #header-bottom-row {
        flex-direction: column;
        gap: 0.75rem;
        width: 100%;
        padding: 0;
    }

    #nav-links {
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.5rem;
        width: 100%;
        padding: 0;
    }

    #nav-links a,
    #nav-links button {
        padding: 0.4rem 0.8rem;
        font-size: 0.9rem;
        border-radius: 4px;
        background: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.2);
        white-space: nowrap;
    }
    
    #nav-links a:hover,
    #nav-links button:hover {
        background: rgba(255, 255, 255, 0.2);
        transform: none;
        box-shadow: none;
    }

    #theme-toggle-btn {
        position: absolute;
        top: 1rem;
        right: 1rem;
        font-size: 1.2rem;
        width: 2.5rem;
        height: 2.5rem;
    }

    main {
        padding: 1rem;
        margin: 0.5rem auto;
    }
    
    #app-content, 
    #listings-section {
        padding: 1rem;
    }

    #search-form {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
        max-width: 100%;
    }
    
    #listings-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 0.75rem;
    }
    
    .listing-card {
        font-size: 0.9rem;
    }
    
    .listing-card h3 {
        font-size: 1rem;
    }
    
    /* Mobile - Image preview adjustments */
    .preview-wrapper {
        width: 100px;
        height: 100px;
    }
    
    .preview-wrapper .remove-preview-btn {
        opacity: 1; /* Always visible on mobile */
        width: 24px;
        height: 24px;
        font-size: 12px;
    }
    
    .position-badge {
        width: 20px;
        height: 20px;
        font-size: 10px;
    }
    
    .drag-handle {
        font-size: 10px;
        padding: 1px 3px;
    }
    
    #image-preview-container {
        justify-content: center;
    }
}

@media (max-width: 400px) {
    header h1 {
        font-size: 1.5rem;
    }
    
    header h3 {
        font-size: 0.8rem;
    }
    
    #nav-links a,
    #nav-links button {
        padding: 0.35rem 0.7rem;
        font-size: 0.85rem;
    }
    
    #theme-toggle-btn {
        width: 2.2rem;
        height: 2.2rem;
        font-size: 1rem;
    }
}