:root {
    --primary-color: #007bff;
    --primary-hover: #0056b3;
    --bg-color: #f4f4f9;
    --surface-color: #ffffff;
    --text-color: #333;
    --border-color: #ddd;
    --danger-color: #dc3545;
    --success-color: #28a745;
}

body.dark-mode {
    --bg-color: #121212;
    --surface-color: #1e1e1e;
    --text-color: #e0e0e0;
    --border-color: #444;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    padding: 10px;
    padding-bottom: 15px;
    /* Space for footer */
    /* Mobile Safe Area */
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: calc(60px + env(safe-area-inset-bottom));
    transition: background-color 0.3s, color 0.3s;
}

.container {
    max-width: 600px;
    margin: 0 auto;
    background: var(--surface-color);
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transition: background-color 0.3s;
}

/* Header */
.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px;
}

h1 {
    text-align: center;
    font-size: 1.5rem;
    color: var(--primary-color);
    flex-grow: 1;
    margin: 0;
}

.theme-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 5px;
}

.subtitle {
    text-align: center;
    color: #aaa;
    margin-bottom: 20px;
    font-size: 0.9rem;
}

/* Controls Row */
.controls-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 15px;
}

.secondary-btn {
    flex: 1;
    background-color: #444;
    color: #fff;
    border: none;
    padding: 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

/* Scan Section (Sticky) */
.scan-section {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--surface-color);
    padding: 10px 0;
    border-bottom: 1px solid var(--border-color);
}

#scanInput {
    flex: 1;
    padding: 12px;
    font-size: 16px;
    /* Prevents iOS zoom */
    border: 2px solid var(--border-color);
    background-color: var(--bg-color);
    color: var(--text-color);
    border-radius: 6px;
    outline: none;
    min-height: 48px;
}

#scanInput:focus {
    border-color: var(--primary-color);
}

#addBtn {
    padding: 0 20px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    min-height: 48px;
}

/* Camera Controls */
.camera-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 15px;
    background: var(--bg-color);
    padding: 10px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

#cameraSelect {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 16px;
    background-color: var(--surface-color);
    color: var(--text-color);
    min-height: 48px;
}

.camera-btn {
    width: 100%;
    padding: 15px;
    font-size: 1.1rem;
    font-weight: bold;
    background: var(--success-color);
    color: white;
    border: none;
    border-radius: 8px;
    margin-bottom: 15px;
    min-height: 54px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

/* Scanner View */
.scanner-container {
    width: 100%;
    margin-bottom: 15px;
    display: none;
    background: #000;
    border-radius: 8px;
    overflow: hidden;
    position: relative;
    min-height: 250px;
}

.scanner-container video {
    width: 100% !important;
    height: auto !important;
    border-radius: 8px;
}

/* Last Scanned */
.last-scanned {
    text-align: center;
    margin-bottom: 15px;
    padding: 15px;
    background-color: var(--bg-color);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.last-scanned h3 {
    margin: 0;
    font-size: 1rem;
    color: var(--text-color);
    opacity: 0.7;
}

#lastScannedCode {
    display: block;
    font-size: 1.5rem;
    color: var(--primary-color);
    margin: 5px 0;
    font-family: monospace;
}

.product-name {
    font-size: 1.1rem;
    font-weight: bold;
    margin: 5px 0 0;
    min-height: 1.4em;
    color: var(--text-color);
}

/* Stats */
.stats {
    display: flex;
    justify-content: space-around;
    background: var(--bg-color);
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 15px;
    font-size: 0.9rem;
    border: 1px solid var(--border-color);
}

.stats p {
    margin: 0;
}

.stats span {
    font-weight: bold;
    font-size: 1.1rem;
    color: var(--primary-color);
}

/* Actions */
.actions {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.actions button {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    min-height: 48px;
}

.primary-btn {
    background-color: var(--primary-color);
    color: white;
}

.danger-btn {
    background-color: var(--danger-color);
    color: white;
}

/* Table */
.list-section {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

th,
td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-color);
}

th {
    background-color: var(--surface-color);
    border-bottom: 2px solid var(--border-color);
}

tr:hover {
    background-color: rgba(128, 128, 128, 0.1);
}

/* Inputs in Table */
.price-input {
    width: 70px;
    padding: 8px;
    border-radius: 4px;
    font-size: 1rem;
    border: 1px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-color);
    text-align: center;
}

.delete-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    padding: 5px 10px;
    color: var(--danger-color);
}

/* Footer */
footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: var(--surface-color);
    color: var(--text-color);
    text-align: center;
    padding: 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border-color);
    font-size: 0.8rem;
    z-index: 1000;
}

.footer-controls {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 5px;
}

.clear-cache-btn {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    opacity: 0.7;
    padding: 5px 10px;
    border-radius: 4px;
    font-size: 0.8rem;
    cursor: pointer;
}

.sm-btn {
    background: none;
    border: none;
    color: var(--text-color);
    opacity: 0.7;
    cursor: pointer;
}

/* Visual Feedback */
.flash-success {
    animation: flashGreen 0.5s;
}

.flash-error {
    animation: flashRed 0.5s;
}

@keyframes flashGreen {
    0% {
        background-color: rgba(40, 167, 69, 0.5);
    }

    100% {
        background-color: transparent;
    }
}

@keyframes flashRed {
    0% {
        background-color: rgba(220, 53, 69, 0.5);
    }

    100% {
        background-color: transparent;
    }
}

/* Button Animations */
button:active {
    transform: scale(0.96);
    transition: transform 0.05s;
}

/* Footer Version */
.version {
    font-size: 0.75rem;
    color: #999;
}

/* Mobile Optimizations */
@media (max-width: 600px) {

    input,
    button,
    select {
        min-height: 48px;
        /* Touch target size */
    }

    .price-input {
        width: 70px;
    }

    td,
    th {
        padding: 12px 6px;
    }
}

@media (min-width: 600px) {
    .container {
        padding: 20px;
        max-width: 800px;
    }

    .actions {
        /* Keep buttons stacked on very small, row on tablet */
        flex-direction: row;
    }
}