:root {
    --bg: #f4f6f2;
    --surface: #ffffff;
    --text: #1c2a1e;
    --muted: #5f6f62;
    --border: #dde3da;
    --primary: #2f7d3b;
    --primary-text: #ffffff;
    --danger: #b3261e;
    --ok-bg: #e3f3e5;
    --ok-text: #1f5e29;
    --err-bg: #fbe7e5;
    --err-text: #8c1d17;
    --warn-bg: #fff3d6;
    --warn-text: #7a5600;
    --code-bg: #f0f3ee;
    --radius: 10px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121812;
        --surface: #1b231c;
        --text: #e3ebe2;
        --muted: #9aab9c;
        --border: #2d3a2f;
        --primary: #5cb468;
        --primary-text: #0d1a0f;
        --danger: #f2867e;
        --ok-bg: #1f3a23;
        --ok-text: #a8e0ae;
        --err-bg: #3d1f1d;
        --err-text: #f4b4ae;
        --warn-bg: #3b3018;
        --warn-text: #f1d48d;
        --code-bg: #141b15;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--primary); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }

.topbar {
    display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
    padding: .75rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--text); }
.leaf { color: var(--primary); }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.topbar nav a { padding: .4rem .7rem; border-radius: 6px; text-decoration: none; color: var(--muted); }
.topbar nav a.active, .topbar nav a:hover { background: var(--bg); color: var(--text); }
.logout { display: flex; align-items: center; gap: .5rem; }

.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }

.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 1.25rem; margin-bottom: 1rem; min-width: 0;
}
.card.highlight { border-color: var(--primary); }
.card.narrow { width: 100%; max-width: 420px; }

.grid { display: grid; gap: 1rem; margin-bottom: 1rem; }
.grid > .card { margin-bottom: 0; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.stat { display: flex; flex-direction: column; gap: .15rem; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.center-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; }

label { display: block; margin-bottom: .85rem; font-weight: 500; }
input, select, textarea {
    display: block; width: 100%; margin-top: .3rem; padding: .55rem .7rem;
    border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font: inherit;
}
input[type="file"] { padding: .4rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .5rem; }
.inline-form input { width: auto; flex: 1; min-width: 200px; margin: 0; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; }
.filters input, .filters select { width: auto; flex: 1; min-width: 140px; margin: 0; }

.btn {
    display: inline-block; padding: .5rem .95rem; border-radius: 6px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-ghost { background: transparent; }
.btn-small { padding: .25rem .6rem; font-size: .85rem; }
.btn-block { width: 100%; }

.alert { padding: .7rem .9rem; border-radius: 6px; margin-bottom: 1rem; }
.alert-success { background: var(--ok-bg); color: var(--ok-text); }
.alert-error { background: var(--err-bg); color: var(--err-text); }
.alert-warning { background: var(--warn-bg); color: var(--warn-text); }

.badge {
    display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600;
    background: var(--code-bg); color: var(--muted);
}
.badge-ready, .badge-success, .badge-correct { background: var(--ok-bg); color: var(--ok-text); }
.badge-error, .badge-unreachable, .badge-incorrect { background: var(--err-bg); color: var(--err-text); }
.badge-loading, .badge-starting { background: var(--warn-bg); color: var(--warn-text); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .85rem; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0 0 1rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.json {
    background: var(--code-bg); border-radius: 6px; padding: 1rem; overflow: auto; max-height: 520px;
    font: 13px/1.45 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.token-display { font-size: 1.8rem; letter-spacing: .08em; font-weight: 700; margin: .5rem 0; user-select: all; overflow-wrap: anywhere; }
.query-image { max-width: 100%; border-radius: 6px; display: block; }
.pager { display: flex; gap: 1rem; align-items: center; justify-content: center; margin-top: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
code { background: var(--code-bg); padding: 0 .3rem; border-radius: 4px; }

@media (max-width: 640px) {
    .topbar { gap: .75rem; }
    .container { padding: 1rem 16px 2rem; }
    .kv { grid-template-columns: 1fr; }
    .kv dt { margin-top: .4rem; }
}

/* Update page */
.checks { list-style: none; padding: 0; margin: 0 0 1rem; }
.checks li { margin: .25rem 0; }
.inline-form label.inline { display: flex; gap: .4rem; align-items: center; margin: 0; }
.inline-form label.inline input { flex: none; min-width: 0; width: auto; }

/* Type-ahead name suggestions */
.typeahead { position: relative; flex: 1; min-width: 220px; }
.typeahead input { width: 100%; }
.typeahead-list {
    position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 2px); max-height: 18rem; overflow-y: auto;
    margin: 0; padding: .25rem 0; list-style: none; background: var(--surface); border: 1px solid var(--border);
    border-radius: 6px; box-shadow: 0 6px 18px rgba(0, 0, 0, .15);
}
.typeahead-list li { padding: .45rem .7rem; cursor: pointer; display: flex; justify-content: space-between; gap: .75rem; }
.typeahead-list li small { color: var(--muted); white-space: nowrap; }
.typeahead-list li.active, .typeahead-list li[data-index]:hover { background: var(--bg); }
.typeahead-list li.typeahead-empty { cursor: default; color: var(--muted); font-size: .85rem; }

/* Name catalogue import */
.progress { height: .5rem; background: var(--bg); border: 1px solid var(--border); border-radius: 4px; margin: .75rem 0 .25rem; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--primary); transition: width .3s; }

[hidden] { display: none !important; }

/* Training photos */
fieldset.choices { border: 0; padding: 0; margin: 0 0 .85rem; display: flex; gap: 1rem; flex-wrap: wrap; }
fieldset.choices legend { font-weight: 500; margin-bottom: .3rem; padding: 0; }
label.inline { display: flex; gap: .4rem; align-items: center; font-weight: 400; }
label.inline input { width: auto; margin: 0; }
.trainer-status { margin: .75rem 0 .25rem; }
.trainer-errors { color: var(--err-text); max-height: 12rem; overflow-y: auto; padding-left: 1.2rem; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.thumb { margin: 0; position: relative; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--bg); font-weight: 400; }
.thumb img, .thumb-empty { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; }
.thumb-empty { display: flex; align-items: center; justify-content: center; padding: .5rem; font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; text-align: center; }
.thumb-bar { display: flex; align-items: center; justify-content: space-between; gap: .4rem; padding: .3rem .4rem; }
.thumb-bar input { width: auto; margin: 0; }
.thumb-error { display: block; padding: 0 .4rem .35rem; font-size: .75rem; color: var(--err-text); overflow-wrap: anywhere; }
.badge-trained { background: var(--ok-bg); color: var(--ok-text); }
.badge-waiting, .badge-duplicate { background: var(--warn-bg); color: var(--warn-text); }
.badge-failed { background: var(--err-bg); color: var(--err-text); }

/* Review of training photos */
.badge-review { background: var(--code-bg); color: var(--text); }
.thumb-check { display: block; padding: .15rem .4rem .35rem; font-size: .75rem; overflow-wrap: anywhere; }
.thumb-check.check-match { color: var(--ok-text); }
.thumb-check.check-near { color: var(--warn-text); }
.thumb-check.check-miss, .thumb-check.check-error { color: var(--err-text); }
.login-name { margin: 0 0 .85rem; overflow-wrap: anywhere; }
.filters label.inline { flex: none; margin: 0; }
.filters label.inline input { flex: none; min-width: 0; width: auto; }

/* Soil analysis (1.7) */
.swatch { display: inline-block; width: 1.1em; height: 1.1em; border-radius: 4px; border: 1px solid rgba(0, 0, 0, .25); vertical-align: -0.15em; margin-right: .45em; }
.advice { margin: 0; padding-left: 1.2rem; }
.advice li { margin-bottom: .35rem; }

/* Web app of app users: one tab per kind of check */
.tabs { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.tabs a { padding: .45rem .9rem; border: 1px solid var(--border); border-radius: 999px; text-decoration: none; color: var(--text); background: var(--surface); }
.tabs a.active, .tabs a:hover { border-color: var(--primary); color: var(--primary); font-weight: 600; }

/* Feedback awaiting review */
.feedback-item { display: grid; grid-template-columns: 160px 1fr; gap: 1rem; align-items: start; }
.feedback-item img { width: 160px; aspect-ratio: 1; object-fit: cover; border-radius: 6px; display: block; }
.feedback-item .kv { margin-top: 0; }
.feedback-item .inline-form { margin-top: .5rem; }
.feedback-pick { display: flex; align-items: center; gap: .4rem; margin: .4rem 0 0; }
.feedback-pick input { width: auto; margin: 0; }
@media (max-width: 640px) {
    .feedback-item { grid-template-columns: 1fr; }
    .feedback-item img { width: 100%; max-width: 320px; }
}
