@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@300;400;500;700&display=swap');

:root {
    /* Light Theme (Default) */
    --bg-color: #f8fafc;
    --surface-color: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
    --border-color: #e2e8f0;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --card-bg: #ffffff;

    --font-main: 'Quicksand', sans-serif;
    --font-weight-thin: 300;
    --font-weight-reg: 400;
    --font-weight-bold: 500;
}

/* Support for manual toggle */
body.light-theme {
    --bg-color: #f8fafc;
    --surface-color: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
    --border-color: #e2e8f0;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --card-bg: #ffffff;
}

body.dark-theme {
    --bg-color: #0f172a;
    --surface-color: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --accent-color: #60a5fa;
    --accent-hover: #93c5fd;
    --border-color: #334155;
    --shadow: 0 10px 15px -3px rgb(0 0 0 / 0.3);
    --card-bg: #1e293b;
}

@media (prefers-color-scheme: dark) {
    :root:not(.light-theme) body:not(.light-theme) {
        --bg-color: #0f172a;
        --surface-color: #1e293b;
        --text-primary: #f1f5f9;
        --text-secondary: #94a3b8;
        --accent-color: #60a5fa;
        --accent-hover: #93c5fd;
        --border-color: #334155;
        --shadow: 0 10px 15px -3px rgb(0 0 0 / 0.3);
        --card-bg: #1e293b;
    }
}

/* ------------------------------------------------------------------
   2. BASE STYLES
   ------------------------------------------------------------------ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    overflow-wrap: break-word;
    /* Global safety for long strings */
}

html,
body {
    overflow-x: hidden;
    width: 100%;
}

body {
    font-family: var(--font-main);
    background-color: var(--bg-color);
    color: var(--text-primary);
    line-height: 1.6;
    font-weight: var(--font-weight-reg);
    transition: background-color 0.3s ease, color 0.3s ease;
    padding: 1.5rem;
}

@media (max-width: 640px) {
    body {
        padding: 1rem;
    }

    .card {
        padding: 1rem;
    }
}

.container {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 3rem;
    padding-top: 1rem;
}

h1 {
    font-size: 1.5rem;
    font-weight: var(--font-weight-thin);
    letter-spacing: -0.02em;
    color: var(--text-primary);
    margin-bottom: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

h1 .icon-box {
    opacity: 0.7;
}

/* ------------------------------------------------------------------
   3. CSS ART ICONS
   ------------------------------------------------------------------ */

/* Box Icon */
.icon-box {
    width: 24px;
    height: 24px;
    position: relative;
    display: inline-block;
    vertical-align: middle;
}

.icon-box::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 16px;
    border: 2px solid var(--accent-color);
    bottom: 2px;
    left: 2px;
    border-radius: 2px;
}

.icon-box::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 6px;
    background: var(--accent-color);
    top: 4px;
    left: 2px;
    border-radius: 2px 2px 0 0;
}

/* Plus Icon */
.icon-plus {
    width: 14px;
    height: 14px;
    position: relative;
    display: inline-block;
}

.icon-plus::before,
.icon-plus::after {
    content: '';
    position: absolute;
    background: currentColor;
    border-radius: 2px;
}

.icon-plus::before {
    width: 14px;
    height: 2px;
    top: 6px;
    left: 0;
}

.icon-plus::after {
    width: 2px;
    height: 14px;
    top: 0;
    left: 6px;
}

/* Magic Wand Icon */
.icon-magic {
    width: 18px;
    height: 18px;
    position: relative;
    display: inline-block;
}

.icon-magic::before {
    content: '★';
    position: absolute;
    font-size: 12px;
    top: -3px;
    right: -2px;
    color: var(--accent-color);
    line-height: 1;
    text-shadow: 0 0 5px var(--accent-color);
    /* Subtle glow */
}

.icon-magic::after {
    content: '';
    position: absolute;
    width: 2px;
    height: 14px;
    background: currentColor;
    transform: rotate(45deg);
    /* Slant it right */
    transform-origin: bottom left;
    bottom: 3px;
    left: 2px;
}

/* Sun Icon */
.icon-sun {
    width: 14px;
    height: 14px;
    background: currentColor;
    border-radius: 50%;
    position: relative;
    transition: all 0.3s ease;
}

.icon-sun::before {
    content: '';
    position: absolute;
    top: -4px;
    left: 50%;
    width: 1.5px;
    height: 22px;
    background: currentColor;
    transform: translateX(-50%);
}

.icon-sun::after {
    content: '';
    position: absolute;
    top: 50%;
    left: -4px;
    width: 22px;
    height: 1.5px;
    background: currentColor;
    transform: translateY(-50%);
}

.icon-sun-rays {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform: rotate(45deg);
}

.icon-sun-rays::before {
    content: '';
    position: absolute;
    top: -4px;
    left: 50%;
    width: 1.5px;
    height: 22px;
    background: currentColor;
    transform: translateX(-50%);
}

.icon-sun-rays::after {
    content: '';
    position: absolute;
    top: 50%;
    left: -4px;
    width: 22px;
    height: 1.5px;
    background: currentColor;
    transform: translateY(-50%);
}

/* Moon Icon */
.icon-moon {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    box-shadow: 5px 3px 0 0 currentColor;
    position: relative;
    top: -2px;
    /* Counter-acting the shadow offset for optical centering */
    left: -6px;
    transform: rotate(-20deg);
    transition: all 0.3s ease;
}

/* View Table Icon */
.icon-view-table {
    width: 18px;
    height: 14px;
    border: 1.5px solid currentColor;
    border-radius: 2px;
    position: relative;
    display: inline-block;
}

.icon-view-table::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 1.5px;
    background: currentColor;
    top: 4px;
    left: 0;
    box-shadow: 0 4px currentColor;
}

/* View Grid Icon */
.icon-view-grid {
    width: 18px;
    height: 18px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 2px;
}

.icon-view-grid span {
    border: 1.5px solid currentColor;
    border-radius: 2px;
}

/* Bug Icon (Beetle style) */
.icon-bug {
    width: 14px;
    height: 16px;
    background: currentColor;
    border-radius: 50% 50% 40% 40%;
    position: relative;
    display: inline-block;
}

.icon-bug::before {
    content: '';
    position: absolute;
    width: 8px;
    height: 6px;
    background: currentColor;
    border-radius: 50%;
    top: -4px;
    left: 3px;
}

.icon-bug::after {
    content: '';
    position: absolute;
    width: 22px;
    height: 1px;
    background: transparent;
    top: 6px;
    left: -4px;
    box-shadow: 0 -3px 0 currentColor, 0 3px 0 currentColor;
}

/* Expand Icon (Two arrows pointing out) */
.icon-expand {
    width: 18px;
    height: 14px;
    position: relative;
}

.icon-expand::before,
.icon-expand::after {
    content: '';
    position: absolute;
    width: 6px;
    height: 6px;
    border-top: 2px solid currentColor;
    border-right: 2px solid currentColor;
}

.icon-expand::before {
    transform: rotate(-135deg);
    top: 3px;
    left: 2px;
}

.icon-expand::after {
    transform: rotate(45deg);
    bottom: 3px;
    right: 2px;
}

/* ------------------------------------------------------------------
   4. COMPONENTS (Preliminary)
   ------------------------------------------------------------------ */
#products-container {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.5rem;
}

.view-table table {
    min-width: 600px;
    /* Prevent squishing on small screens */
}

.header-actions {
    display: flex;
    gap: 0.75rem;
}

.card-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 1.5rem;
}

.toolbar-left,
.toolbar-right {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.view-toggle,
.theme-toggle,
.test-toggle,
.expand-toggle,
.magic-btn,
.add-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s ease;
    position: relative;
    /* For tooltips */
}

.view-toggle:hover,
.theme-toggle:hover,
.test-toggle:hover,
.expand-toggle:hover,
.magic-btn:hover,
.add-btn:hover {
    transform: scale(1.05);
    border-color: var(--accent-color);
    color: var(--accent-color);
}

/* ------------------------------------------------------------------
   5. TOOLTIPS (Pure CSS)
   ------------------------------------------------------------------ */
[data-tooltip] {
    position: relative;
}

[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: var(--text-primary);
    color: var(--surface-color);
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    pointer-events: none;
    z-index: 100;
}

[data-tooltip]:hover::after {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

body.dark-theme .icon-sun,
body:not(.light-theme) .icon-sun {
    display: block;
}

body.dark-theme .icon-moon,
body:not(.light-theme) .icon-moon {
    display: none;
}

body.light-theme .icon-sun {
    display: none;
}

body.light-theme .icon-moon {
    display: block;
}

@media (prefers-color-scheme: dark) {
    body:not(.light-theme) .icon-sun {
        display: block;
    }

    body:not(.light-theme) .icon-moon {
        display: none;
    }
}

@media (prefers-color-scheme: light) {
    body:not(.dark-theme) .icon-sun {
        display: none;
    }

    body:not(.dark-theme) .icon-moon {
        display: block;
    }
}

.card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
    max-width: 100%;
    /* overflow: hidden; REMOVED to allow tooltips to float */
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    text-align: left;
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    font-weight: var(--font-weight-bold);
}

td {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.85rem;
    color: var(--text-primary);
}

.prod-name {
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.prod-category {
    font-size: 0.7rem;
    color: var(--text-secondary);
    background: var(--bg-color);
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    display: inline-block;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.col-extra {
    display: none !important;
    /* Force hide unless expanded */
}

.show-extra .col-extra {
    display: table-cell !important;
}

.view-grid.show-extra .col-extra {
    display: block !important;
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px dashed var(--border-color);
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.view-grid .col-qty::before {
    content: "Quantity: ";
    font-weight: bold;
    margin-right: 0.25rem;
}

.view-grid .col-extra::before {
    content: "Details: ";
    font-weight: bold;
    margin-right: 0.5rem;
}

.view-grid.show-extra .col-barcode::before {
    content: "Barcode: ";
}

.view-grid.show-extra .col-weight::before {
    content: "Weight: ";
}

.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: var(--font-weight-bold);
    text-transform: uppercase;
    white-space: nowrap;
    /* Prevent ugly line breaks */
}

.status-in {
    background: rgba(34, 197, 94, 0.1);
    color: #16a34a;
}

.status-out {
    background: rgba(239, 68, 68, 0.1);
    color: #dc2626;
}

/* ------------------------------------------------------------------
   6. ERROR STATES & INVALID DATA
   ------------------------------------------------------------------ */
.item-invalid {
    background: rgba(239, 68, 68, 0.05) !important;
    border-left: 4px solid #ef4444 !important;
}

.col-error {
    color: #dc2626;
    font-size: 0.85rem;
}

.error-msg {
    font-weight: var(--font-weight-bold);
    margin-bottom: 0.25rem;
}

.error-details {
    color: var(--text-secondary);
    font-size: 0.75rem;
}

/* Hide invalid items by default unless testing */
.item-invalid:not(.show-error) {
    display: none;
}

.view-grid .item-invalid {
    border: 1px solid #ef4444;
}

.view-grid table,
.view-grid thead,
.view-grid tbody,
.view-grid tr,
.view-grid td {
    display: block;
}

.view-grid thead {
    display: none;
}

.view-grid tbody {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
    gap: 1rem;
}

.view-grid tr {
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem;
    background: var(--card-bg);
    transition: transform 0.2s ease;
}

.view-grid tr:hover {
    transform: translateY(-4px);
}

.view-grid td {
    border: none;
    padding: 0.5rem 0;
}

.view-grid .col-name {
    font-size: 1.1rem;
    margin-bottom: 1rem;
}

.view-grid .col-manu {
    color: var(--text-secondary);
    font-size: 0.8rem;
    text-transform: uppercase;
}

.view-grid .col-price {
    font-size: 1.25rem;
    font-weight: var(--font-weight-bold);
    color: var(--accent-color);
    margin-top: 0.5rem;
}

.view-grid .col-stock {
    margin-top: 1rem;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.btn-primary {
    background: var(--accent-color);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgb(59 130 246 / 0.4);
}

/* ------------------------------------------------------------------
   7. MODAL & FORM
   ------------------------------------------------------------------ */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.modal-backdrop.active {
    opacity: 1;
    visibility: visible;
}

.modal-card {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    width: 90%;
    max-width: 450px;
    padding: 2rem;
    box-shadow: var(--shadow);
    transform: translateY(20px);
    transition: all 0.3s ease;
}

.modal-backdrop.active .modal-card {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.modal-header h2 {
    font-size: 1.25rem;
    font-weight: var(--font-weight-thin);
    margin: 0;
}

.close-modal {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
}

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

.form-group label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    font-weight: var(--font-weight-bold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: var(--font-main);
    font-size: 0.9rem;
    transition: all 0.2s ease;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
    margin-top: 2rem;
}

.btn-cancel {
    background: transparent;
    border: 1px solid var(--border-color);
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    font-family: var(--font-main);
}

.btn-submit {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    cursor: pointer;
    font-family: var(--font-main);
    font-weight: var(--font-weight-bold);
}