:root {
    --bg: #f7f5f0;
    --ink: #23291f;
    --muted: #6b7264;
    --line: #dcd8cc;
    --accent: #3f6b4a;
    --accent-soft: #e4ede4;
    --over: #a3402f;
    --over-soft: #f4e3df;
    --white: #ffffff;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

.wrap {
    max-width: 640px;
    margin: 0 auto;
    padding: 28px 20px 80px;
}

.top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
}

h1 {
    font-size: 20px;
    font-weight: 600;
    margin: 0;
    letter-spacing: -0.01em;
}

.daynav {
    display: flex;
    align-items: center;
    gap: 6px;
}

.daynav input[type="date"] {
    border: 1px solid var(--line);
    background: var(--white);
    border-radius: 6px;
    padding: 6px 8px;
    font-size: 13px;
    color: var(--ink);
}

button {
    font-family: inherit;
    cursor: pointer;
}

.ghost {
    border: 1px solid var(--line);
    background: var(--white);
    border-radius: 6px;
    width: 32px;
    height: 32px;
    font-size: 15px;
    color: var(--ink);
    line-height: 1;
}

.ghost:hover { background: var(--accent-soft); }

/* --- Remaining kcal counter: the one bold element on the page --- */
.counter {
    text-align: center;
    padding: 32px 16px 26px;
    margin-bottom: 28px;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 10px;
}

.counter-remaining {
    font-size: 64px;
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

.counter-remaining.over { color: var(--over); }

.counter-label {
    margin-top: 6px;
    font-size: 14px;
    color: var(--muted);
}

.counter-bar {
    margin: 18px auto 10px;
    max-width: 320px;
    height: 6px;
    background: var(--accent-soft);
    border-radius: 3px;
    overflow: hidden;
}

.counter-bar-fill {
    height: 100%;
    background: var(--accent);
    transition: width 0.3s ease;
}

.counter.over .counter-bar-fill { background: var(--over); }

.counter-sub {
    font-size: 13px;
    color: var(--muted);
}

.target-edit input {
    width: 56px;
    border: none;
    border-bottom: 1px dashed var(--line);
    background: transparent;
    font-size: 13px;
    color: var(--muted);
    text-align: right;
    padding: 0 2px;
}

.target-edit input:focus {
    outline: none;
    border-bottom-color: var(--accent);
    color: var(--ink);
}

/* --- Search / add --- */
.add-box {
    margin-bottom: 22px;
    position: relative;
}

.search-row {
    position: relative;
    display: flex;
    gap: 8px;
}

#searchInput {
    flex: 1;
    width: 100%;
    padding: 12px 14px;
    font-size: 15px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--white);
    color: var(--ink);
}

.scan-btn {
    flex-shrink: 0;
    width: 44px;
    border: 1px solid var(--line);
    background: var(--white);
    border-radius: 8px;
    color: var(--ink);
    display: flex;
    align-items: center;
    justify-content: center;
}

.scan-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

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

.dropdown {
    position: absolute;
    left: 0; right: 0; top: calc(100% + 4px);
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 8px;
    max-height: 280px;
    overflow-y: auto;
    z-index: 10;
    box-shadow: 0 6px 16px rgba(35, 41, 31, 0.08);
}

.dropdown.hidden { display: none; }

.dropdown-item {
    padding: 10px 14px;
    cursor: pointer;
    border-bottom: 1px solid var(--line);
    font-size: 14px;
}

.dropdown-item:last-child { border-bottom: none; }
.dropdown-item:hover, .dropdown-item.active { background: var(--accent-soft); }

.dropdown-item .brand,
.dropdown-item .kcal-tag {
    color: var(--muted);
    font-size: 12px;
}

.dropdown-empty, .dropdown-loading {
    padding: 12px 14px;
    font-size: 13px;
    color: var(--muted);
}

.selected-box {
    margin-top: 10px;
    padding: 14px;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.selected-box.hidden { display: none; }

.selected-name {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 10px;
}

.selected-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

#gramsInput {
    width: 90px;
    padding: 8px 10px;
    font-size: 14px;
    border: 1px solid var(--line);
    border-radius: 6px;
}

.unit { color: var(--muted); font-size: 13px; margin-right: auto; }

.computed {
    font-size: 13px;
    color: var(--accent);
    font-weight: 600;
    min-width: 70px;
}

.primary {
    background: var(--accent);
    color: var(--white);
    border: none;
    border-radius: 6px;
    padding: 9px 16px;
    font-size: 13px;
    font-weight: 600;
}

.primary:hover { background: #345a3d; }
.primary:disabled { opacity: 0.5; cursor: not-allowed; }

/* --- Log table --- */
.log table {
    width: 100%;
    border-collapse: collapse;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
}

.log td {
    padding: 11px 12px;
    font-size: 14px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}

.log tr:last-child td { border-bottom: none; }

.log .time { color: var(--muted); font-variant-numeric: tabular-nums; width: 50px; }
.log .name .brand { display: block; color: var(--muted); font-size: 12px; }
.log .grams { color: var(--muted); text-align: right; white-space: nowrap; }
.log .kcal { text-align: right; font-weight: 600; white-space: nowrap; }
.log .del { width: 30px; text-align: center; }

.del-btn {
    border: none;
    background: none;
    color: var(--muted);
    font-size: 18px;
    line-height: 1;
    padding: 2px 6px;
}
.del-btn:hover { color: var(--over); }

.empty {
    text-align: center;
    color: var(--muted);
    font-size: 14px;
    padding: 24px 0;
}

.empty.hidden { display: none; }

.manage-link {
    text-align: center;
    margin-top: 18px;
}

.link-btn {
    border: none;
    background: none;
    color: var(--muted);
    font-size: 13px;
    text-decoration: underline;
    text-underline-offset: 2px;
    padding: 4px;
}

.link-btn:hover { color: var(--accent); }

/* --- Own-products management modal --- */
.products-content {
    max-width: 440px;
    display: flex;
    flex-direction: column;
    max-height: 80vh;
}

.product-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 14px;
    border-bottom: 1px solid var(--line);
}

.product-form input {
    padding: 9px 10px;
    font-size: 14px;
    border: 1px solid var(--line);
    border-radius: 6px;
}

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

.product-form-row input { flex: 1; }

.products-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
}

.products-list li {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
    font-size: 14px;
}

.products-list li:last-child { border-bottom: none; }

.products-list .p-name { flex: 1; }
.products-list .p-name .brand { display: block; color: var(--muted); font-size: 12px; }
.products-list .p-kcal { color: var(--muted); white-space: nowrap; font-size: 13px; }

.products-list .del-btn {
    font-size: 18px;
    color: var(--muted);
}
.products-list .del-btn:hover { color: var(--over); }

#productsEmpty { padding: 18px 14px; }

.own-tag {
    display: inline-block;
    margin-left: 6px;
    font-size: 11px;
    color: var(--accent);
    background: var(--accent-soft);
    border-radius: 4px;
    padding: 1px 5px;
    vertical-align: middle;
}

/* --- Barcode scanner modal --- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(23, 27, 19, 0.72);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 100;
}

.modal.hidden { display: none; }

.modal-content {
    background: var(--white);
    border-radius: 10px;
    width: 100%;
    max-width: 420px;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    font-size: 14px;
    font-weight: 600;
    border-bottom: 1px solid var(--line);
}

.modal-header .ghost {
    border: none;
    background: none;
    font-size: 18px;
    width: auto;
    height: auto;
}

#scannerVideo {
    width: 100%;
    max-height: 60vh;
    display: block;
    background: #000;
    object-fit: cover;
}

.scanner-status {
    margin: 0;
    padding: 12px 14px;
    font-size: 13px;
    color: var(--muted);
    text-align: center;
}

.scanner-status.error { color: var(--over); }

@media (max-width: 480px) {
    .counter-remaining { font-size: 52px; }
    .selected-row { gap: 6px; }
    .unit { margin-right: 4px; }
}
