:root {
    color-scheme: light;
    --bg: #f7f8f5;
    --panel: #ffffff;
    --ink: #1d2721;
    --muted: #68736c;
    --line: #dfe5df;
    --primary: #1f6f5c;
    --primary-dark: #164f42;
    --warn: #b7791f;
    --danger: #b83232;
    --shadow: 0 8px 24px rgba(29, 39, 33, .08);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.5;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
input,
select,
textarea {
    font: inherit;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 58px;
    padding: env(safe-area-inset-top) 16px 0;
    background: rgba(247, 248, 245, .92);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(14px);
}

.brand {
    padding: 16px 0;
    font-weight: 800;
    color: var(--primary-dark);
}

.nav {
    display: flex;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    white-space: nowrap;
}

.nav a,
.nav button,
.link-button {
    display: inline-flex;
    align-items: center;
    min-height: 38px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
}

.page {
    width: min(100%, 760px);
    margin: 0 auto;
    padding: 18px 14px 96px;
}

.login-page {
    width: min(100%, 420px);
    padding-top: 12vh;
}

.logout-form {
    display: inline-flex;
    margin: 0;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 20px;
}

h1,
h2,
p {
    margin-top: 0;
}

h1 {
    font-size: 1.45rem;
}

h2 {
    font-size: 1rem;
    margin-bottom: 4px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 14px;
}

.stat {
    min-height: 82px;
    padding: 12px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.stat span {
    display: block;
    color: var(--muted);
}

.stat strong {
    display: block;
    font-size: 1.9rem;
}

.stat.warn strong {
    color: var(--warn);
}

.stat.danger strong {
    color: var(--danger);
}

.filters,
.form {
    display: grid;
    gap: 12px;
}

.filters {
    grid-template-columns: 1fr;
    margin-bottom: 14px;
}

label {
    display: grid;
    gap: 6px;
    font-weight: 650;
}

input,
select,
textarea {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--ink);
}

textarea {
    resize: vertical;
}

button,
.primary {
    min-height: 44px;
    border: 0;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    font-weight: 750;
    cursor: pointer;
}

.secondary {
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    font-weight: 750;
    cursor: pointer;
}

.primary:hover {
    background: var(--primary-dark);
}

.two-cols {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.inline-field {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
}

.photo-preview:empty {
    display: none;
}

.photo-preview img {
    display: block;
    width: auto;
    max-width: 100%;
    max-height: 260px;
    object-fit: contain;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--bg);
}

.photo-tools {
    display: flex;
    gap: 8px;
}

.photo-tools button {
    width: auto;
    padding: 0 16px;
}

.item-list {
    display: grid;
    gap: 10px;
}

.item {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 12px;
    align-items: center;
    padding: 14px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-left: 5px solid transparent;
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.item:not(.has-image) {
    grid-template-columns: 1fr auto;
}

.thumb {
    width: 56px;
    height: 56px;
    object-fit: cover;
    border-radius: 8px;
    background: var(--bg);
}

.item.soon {
    border-left-color: var(--warn);
}

.item.expired {
    border-left-color: var(--danger);
}

.item p {
    margin-bottom: 4px;
    color: var(--muted);
}

.expiry span {
    color: var(--ink);
    font-weight: 700;
}

.item-actions {
    display: grid;
    gap: 8px;
    justify-items: end;
}

.item-actions button,
.item-actions a {
    min-width: 54px;
    min-height: 38px;
    display: inline-grid;
    place-items: center;
    border-radius: 8px;
}

.item-actions a {
    border: 1px solid var(--line);
    background: var(--panel);
}

.fab {
    position: fixed;
    right: 18px;
    bottom: calc(18px + env(safe-area-inset-bottom));
    width: 58px;
    height: 58px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: 2rem;
    box-shadow: var(--shadow);
}

.alert,
.notice {
    padding: 12px;
    margin-bottom: 12px;
    border-radius: 8px;
}

.alert {
    background: #fff0f0;
    color: var(--danger);
}

.notice {
    background: #eef8f3;
    color: var(--primary-dark);
}

.muted {
    color: var(--muted);
}

.empty {
    text-align: center;
    padding: 36px 16px;
    background: var(--panel);
    border: 1px dashed var(--line);
    border-radius: 8px;
}

.scanner {
    position: relative;
    overflow: hidden;
    aspect-ratio: 4 / 3;
    margin-bottom: 12px;
    border-radius: 8px;
    background: #0d1512;
}

.scanner video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.scan-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.photo-scan {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 10px;
    padding: 0 14px;
    font-weight: 750;
}

.small {
    font-size: .82rem;
    margin-top: 6px;
}

.product-card {
    margin-top: 14px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
}

.product-card img {
    width: 96px;
    max-height: 120px;
    object-fit: contain;
    float: right;
    margin-left: 12px;
}

.product-actions {
    clear: both;
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 14px;
}

.quick-add-form {
    display: grid;
    gap: 10px;
}

.product-actions button,
.product-actions a {
    width: 100%;
    justify-content: center;
}

.danger-form {
    margin-top: 16px;
}

.danger-form button {
    width: 100%;
    background: var(--danger);
}

/* category tags */
.tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 6px;
}

.tag {
    display: inline-block;
    padding: 2px 9px;
    font-size: .8rem;
    font-weight: 650;
    color: var(--primary-dark);
    background: #eef8f3;
    border: 1px solid #cfe7dd;
    border-radius: 999px;
}

/* category chip filter / picker */
.chip-filter {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.chip {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--panel);
    color: var(--ink);
    font-weight: 650;
    cursor: pointer;
}

/* Driven live by the checkbox so toggling updates instantly both ways. */
.chip:has(input:checked) {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.chip input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

/* page header with actions */
.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.panel-head h1 {
    margin: 0;
}

.head-actions {
    display: flex;
    gap: 8px;
}

/* product / category management list */
.select-all {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    font-weight: 650;
}

.manage-list {
    display: grid;
    gap: 10px;
    padding-bottom: 84px;
}

.manage-item {
    display: grid;
    grid-template-columns: auto auto 1fr auto;
    gap: 12px;
    align-items: center;
    padding: 12px 14px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.manage-item:not(:has(.thumb)) {
    grid-template-columns: auto 1fr auto;
}

.manage-item .pick input,
.select-all input {
    width: 22px;
    height: 22px;
}

.manage-item .item-main h2 {
    margin: 0 0 2px;
}

/* bulk action bar */
.bulk-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    gap: 8px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: rgba(247, 248, 245, .96);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(14px);
}

.bulk-bar select {
    flex: 1;
    min-width: 0;
}

.bulk-bar button {
    width: auto;
    padding: 0 14px;
    white-space: nowrap;
}

/* categories page */
.inline-create {
    grid-template-columns: 1fr auto;
    align-items: end;
}

.manage-item .count {
    white-space: nowrap;
}

.category-field {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    display: grid;
    gap: 10px;
}

.category-field legend {
    padding: 0 6px;
    font-weight: 650;
}

/* color swatch picker */
.swatches {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.swatch {
    width: 30px;
    height: 30px;
    border-radius: 999px;
    cursor: pointer;
    box-shadow: inset 0 0 0 2px #fff;
    outline: 2px solid transparent;
    transition: outline-color .1s, transform .1s;
}

/* Selected state is driven live by the radio, so clicking updates instantly. */
.swatch:has(input:checked) {
    outline-color: var(--ink);
    transform: scale(1.12);
}

.swatch input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.manage-item.category-row {
    grid-template-columns: 1fr auto;
    align-items: start;
}

.rename-form {
    display: grid;
    gap: 8px;
}

.rename-top {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 8px;
    align-items: center;
}

.rename-top .tag {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.rename-form button {
    width: auto;
    padding: 0 14px;
    background: var(--panel);
    color: var(--ink);
    border: 1px solid var(--line);
}

.category-meta {
    display: grid;
    gap: 8px;
    justify-items: end;
}

@media (min-width: 680px) {
    .filters {
        grid-template-columns: 1fr 150px 120px auto;
        align-items: end;
    }

    .chip-filter {
        grid-column: 1 / -1;
    }
}
