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

:root {
    --blue: #4A6FA5;
    --blue-light: #5C83B8;
    --blue-pale: #E8EEF5;
    --blue-bg: #F0F4F8;
    --bg: #F7F8FA;
    --white: #FFF;
    --text: #2C3E50;
    --text-dim: #7B8D9E;
    --border: #D8E1EA;
    --green: #27AE60;
    --green-bg: #E8F8EF;
    --yellow: #E2A93B;
    --yellow-bg: #FEF5E0;
    --red: #E04848;
    --red-bg: #FDE8E8;
    --gray: #95A5A6;
    --gray-bg: #ECF0F1;
    --radius: 12px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* ===== Layout ===== */

.container {
    width: 100%;
    max-width: 480px;
    padding: 0 0 80px;
    display: flex;
    flex-direction: column;
}

header {
    text-align: center;
    padding: 16px 16px 8px;
}

.logo {
    width: 120px;
    height: auto;
    margin-bottom: 8px;
}

header h1 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--blue);
}

.connection-status {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: .7rem;
    color: var(--text-dim);
    padding: 4px;
    margin-top: 12px;
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
}

.status-dot.error {
    background: var(--red);
}

/* ===== Tabs ===== */

.tabs {
    display: flex;
    background: var(--white);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
}

.tab {
    flex: 1;
    padding: 10px 0;
    text-align: center;
    font-size: .8rem;
    font-weight: 600;
    color: var(--text-dim);
    border: none;
    background: none;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: .15s;
}

.tab.active {
    color: var(--blue);
    border-bottom-color: var(--blue);
}

/* ===== Pages ===== */

.page {
    display: none;
    padding: 16px;
    flex-direction: column;
    gap: 14px;
}

.page.active {
    display: flex;
}

/* ===== Cards (generic) ===== */

.card {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

/* ===== Scanner ===== */

#scanner-region {
    width: 100%;
    aspect-ratio: 4/3;
    background: #1a1a2e;
    position: relative;
}

#scanner-region video {
    object-fit: cover;
}

.scanner-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 10px;
    color: #8888a0;
}

.scanner-placeholder svg {
    width: 44px;
    height: 44px;
    opacity: .4;
}

.scanner-placeholder span {
    font-size: .8rem;
}

.scanner-controls {
    padding: 10px;
}

/* ===== Buttons ===== */

.btn {
    width: 100%;
    padding: 11px;
    border: none;
    border-radius: 10px;
    font-size: .85rem;
    font-weight: 600;
    cursor: pointer;
    transition: .15s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-primary {
    background: var(--blue);
    color: #fff;
}

.btn-primary:hover {
    background: var(--blue-light);
}

.btn-primary:active {
    transform: scale(.98);
}

.btn-primary:disabled {
    opacity: .5;
    cursor: not-allowed;
}

.btn-stop {
    background: var(--blue-pale);
    color: var(--blue);
}

/* ===== Manual Input ===== */

.manual-input {
    display: flex;
    gap: 8px;
    padding: 10px;
}

.manual-input input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    color: var(--text);
    font-size: .9rem;
    outline: none;
}

.manual-input input:focus {
    border-color: var(--blue);
}

.manual-input input::placeholder {
    color: var(--text-dim);
}

.manual-input button {
    background: var(--blue);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 10px 16px;
    font-size: .8rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

/* ===== History ===== */

.section-title {
    font-size: .8rem;
    font-weight: 600;
    color: var(--text-dim);
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-count {
    background: var(--blue-pale);
    color: var(--blue);
    font-size: .7rem;
    padding: 2px 8px;
    border-radius: 10px;
}

.history-list {
    max-height: 260px;
    overflow-y: auto;
}

.history-empty {
    padding: 24px;
    text-align: center;
    color: var(--text-dim);
    font-size: .78rem;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    animation: fadeIn .2s;
}

.history-item:last-child {
    border-bottom: none;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.history-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.history-dot.green  { background: var(--green); }
.history-dot.yellow { background: var(--yellow); }
.history-dot.red    { background: var(--red); }
.history-dot.gray   { background: var(--gray); }

.history-info {
    flex: 1;
    min-width: 0;
}

.history-barcode {
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: .82rem;
}

.history-name {
    font-size: .68rem;
    color: var(--text-dim);
    margin-top: 1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-time {
    font-size: .68rem;
    color: var(--text-dim);
}

.history-status {
    font-size: .68rem;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: 6px;
    flex-shrink: 0;
    white-space: nowrap;
}

.history-status.green  { background: var(--green-bg); color: var(--green); }
.history-status.yellow { background: var(--yellow-bg); color: var(--yellow); }
.history-status.red    { background: var(--red-bg); color: var(--red); }
.history-status.gray   { background: var(--gray-bg); color: var(--gray); }

/* ===== Result Popup ===== */

.result-popup {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
}

.result-popup.visible {
    opacity: 1;
    pointer-events: auto;
}

.result-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    backdrop-filter: blur(4px);
}

.result-content {
    position: relative;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 28px 20px 20px;
    text-align: center;
    max-width: 380px;
    width: 100%;
    transform: scale(.95);
    transition: transform .2s;
    max-height: 85vh;
    overflow-y: auto;
}

.result-popup.visible .result-content {
    transform: scale(1);
}

/* ===== Traffic Light ===== */

.traffic-light {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    background: #1a1a2e;
    border-radius: 14px;
    padding: 14px 20px;
    margin: 0 auto 14px;
    width: fit-content;
}

.tl-row {
    display: flex;
    gap: 8px;
}

.tl-dot {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #2a2a3e;
    transition: .3s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: .85rem;
}

.tl-dot.on-green  { background: var(--green); box-shadow: 0 0 12px var(--green); }
.tl-dot.on-yellow { background: var(--yellow); box-shadow: 0 0 12px var(--yellow); }
.tl-dot.on-red    { background: var(--red); box-shadow: 0 0 12px var(--red); }
.tl-dot.on-gray   { background: var(--gray); box-shadow: 0 0 8px var(--gray); }

/* ===== Result Details ===== */

.result-title {
    font-size: 1.05rem;
    font-weight: 700;
    margin-bottom: 2px;
}

.result-title.green  { color: var(--green); }
.result-title.yellow { color: var(--yellow); }
.result-title.red    { color: var(--red); }
.result-title.gray   { color: var(--gray); }

.result-subtitle {
    font-size: .75rem;
    color: var(--text-dim);
    margin-bottom: 10px;
}

.result-barcode {
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: .85rem;
    color: var(--text-dim);
    margin-bottom: 4px;
}

.result-product {
    font-size: .8rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 10px;
}

.result-db-badge {
    display: inline-block;
    font-size: .65rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 6px;
    margin-bottom: 10px;
}

.result-db-badge.yes { background: var(--green-bg); color: var(--green); }
.result-db-badge.no  { background: var(--gray-bg); color: var(--gray); }

/* ===== Allergen Matches ===== */

.result-matches {
    text-align: left;
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 12px;
    font-size: .75rem;
}

.result-matches.yellow { background: var(--yellow-bg); }
.result-matches.red    { background: var(--red-bg); }

.result-matches b {
    display: block;
    margin-bottom: 6px;
    font-size: .78rem;
}

.result-matches.yellow b { color: var(--yellow); }
.result-matches.red b    { color: var(--red); }

.match-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.match-tag {
    background: var(--white);
    border: 1px solid var(--border);
    padding: 3px 8px;
    border-radius: 6px;
    font-size: .7rem;
    font-weight: 500;
}

/* ===== Ingredients ===== */

.result-ingredients {
    text-align: left;
    margin-bottom: 12px;
}

.ing-toggle {
    font-size: .72rem;
    color: var(--blue);
    cursor: pointer;
    font-weight: 600;
    padding: 4px 0;
}

.ing-toggle:hover {
    text-decoration: underline;
}

.ing-text {
    font-size: .7rem;
    color: var(--text-dim);
    line-height: 1.5;
    margin-top: 6px;
    display: none;
    background: var(--bg);
    padding: 8px 10px;
    border-radius: 8px;
    word-break: break-word;
}

.ing-text.open {
    display: block;
}

.ing-text .ing-hit {
    background: var(--red-bg);
    color: var(--red);
    font-weight: 600;
    padding: 0 2px;
    border-radius: 3px;
}

/* ===== Loading Spinner ===== */

.result-loading {
    padding: 30px 0;
    text-align: center;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin .6s linear infinite;
    margin: 0 auto 10px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.result-loading span {
    font-size: .8rem;
    color: var(--text-dim);
}

.result-close {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 10px;
    padding: 9px 24px;
    font-size: .82rem;
    font-weight: 600;
    cursor: pointer;
}

.result-close:hover {
    background: var(--border);
}

/* ===== Allergens Page ===== */

.allergen-search {
    padding: 10px 12px;
}

.allergen-search input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: .85rem;
    color: var(--text);
    outline: none;
}

.allergen-search input:focus {
    border-color: var(--blue);
}

.allergen-search input::placeholder {
    color: var(--text-dim);
}

.cat-tabs {
    display: flex;
    gap: 6px;
    padding: 0 12px 10px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.cat-tabs::-webkit-scrollbar {
    display: none;
}

.cat-tab {
    flex-shrink: 0;
    padding: 6px 12px;
    border-radius: 8px;
    font-size: .72rem;
    font-weight: 600;
    border: 1px solid var(--border);
    background: var(--white);
    color: var(--text-dim);
    cursor: pointer;
    transition: .15s;
}

.cat-tab.active {
    background: var(--blue);
    color: #fff;
    border-color: var(--blue);
}

.allergen-list {
    max-height: 400px;
    overflow-y: auto;
}

.allergen-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background .1s;
}

.allergen-item:hover {
    background: var(--blue-pale);
}

.allergen-item:last-child {
    border-bottom: none;
}

.allergen-check {
    width: 20px;
    height: 20px;
    border-radius: 6px;
    border: 2px solid var(--border);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: .15s;
    font-size: .7rem;
    color: #fff;
}

.allergen-check.on {
    background: var(--blue);
    border-color: var(--blue);
}

.allergen-name {
    font-size: .8rem;
    font-weight: 500;
}

.allergen-inci {
    font-size: .68rem;
    color: var(--text-dim);
}

.my-count {
    background: var(--red-bg);
    color: var(--red);
    font-size: .7rem;
    padding: 2px 8px;
    border-radius: 10px;
}

/* ===== Education Cards ===== */

.cards-wrap {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius);
    touch-action: pan-y;
}

.cards-track {
    display: flex;
    transition: transform .3s ease;
}

.edu-card {
    min-width: 100%;
    padding: 20px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.edu-card-img {
    width: 100%;
    max-height: 140px;
    object-fit: contain;
    margin-bottom: 14px;
    border-radius: 8px;
}

.edu-card h3 {
    font-size: .95rem;
    color: var(--blue);
    margin-bottom: 10px;
    line-height: 1.3;
}

.edu-card .edu-body {
    font-size: .78rem;
    line-height: 1.6;
    color: var(--text);
}

.edu-card .edu-body b {
    color: var(--blue);
}

.cards-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 12px 0;
}

.cards-nav button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--white);
    color: var(--blue);
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cards-nav button:disabled {
    opacity: .3;
    cursor: not-allowed;
}

.cards-dots {
    display: flex;
    gap: 5px;
}

.cards-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--border);
    transition: .15s;
}

.cards-dot.active {
    background: var(--blue);
    width: 18px;
    border-radius: 4px;
}

/* ===== Footer ===== */

.site-footer {
    margin-top: auto;
    padding: 24px 16px 16px;
    text-align: center;
    border-top: 1px solid var(--border);
    background: var(--white);
    width: 100%;
    max-width: 480px;
}

.footer-logos {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    margin-bottom: 12px;
}

.footer-logo {
    height: 48px;
    width: auto;
    object-fit: contain;
}

.footer-group {
    font-size: .78rem;
    font-weight: 600;
    color: var(--blue);
    margin-bottom: 8px;
}

.footer-members {
    list-style: none;
    padding: 0;
    margin: 0 0 10px;
    font-size: .72rem;
    color: var(--text-dim);
    line-height: 1.7;
}

.footer-copy {
    font-size: .65rem;
    color: var(--text-dim);
    opacity: .7;
}

/* ===== Responsive ===== */

@media (min-width: 600px) {
    .container {
        padding-top: 8px;
    }
}
