:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #e2e5ea;
    --text: #17202b;
    --muted: #667085;
    --primary: #1f5eff;
    --primary-text: #ffffff;
    --danger: #d92d20;
    --ok-bg: #e7f6ec; --ok-text: #157f3b;
    --warn-bg: #fff4e0; --warn-text: #9a5b00;
    --err-bg: #fdecea; --err-text: #a3261b;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1318;
        --surface: #171c23;
        --surface-2: #1f262f;
        --border: #2a323d;
        --text: #e7ebf0;
        --muted: #98a2b3;
        --primary: #5b8cff;
        --ok-bg: #12301f; --ok-text: #6fd492;
        --warn-bg: #36280e; --warn-text: #f5c26b;
        --err-bg: #3a1714; --err-text: #ff9b91;
        --shadow: none;
    }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0; background: var(--bg); color: var(--text);
    font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 1.6rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
code { font: .9em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
.icon { width: 18px; height: 18px; flex: none; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mt { margin-top: 12px; }
.right { text-align: right; }

/* Topbar */
.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-right { display: flex; align-items: center; gap: 12px; }
.topbar-right form { margin: 0; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); }
.brand-lg { font-size: 1.3rem; justify-content: center; margin-bottom: 20px; }
.brand-mark {
    display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
    background: var(--primary); color: var(--primary-text); font-size: .95rem;
}
.container { max-width: 1100px; margin: 0 auto; padding: 24px 16px 48px; }
.page-head { margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; }
.back { display: inline-flex; align-items: center; gap: 2px; font-size: .9rem; margin-bottom: 6px; }

/* Tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.tile {
    display: flex; flex-direction: column; gap: 6px; padding: 18px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); color: var(--text); transition: border-color .15s, transform .15s;
}
a.tile:hover { border-color: var(--primary); transform: translateY(-1px); }
.tile-icon {
    display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 10px;
    background: var(--surface-2); color: var(--primary); margin-bottom: 4px;
}
.tile-icon .icon { width: 22px; height: 22px; }
.tile-name { font-weight: 600; font-size: 1.05rem; display: flex; align-items: center; gap: 8px; }
.tile-desc { color: var(--muted); font-size: .9rem; }
.tile-disabled { opacity: .6; }

/* Module layout */
.module { display: grid; grid-template-columns: 220px 1fr; gap: 20px; align-items: start; }
.subnav { display: flex; flex-direction: column; gap: 2px; position: sticky; top: 72px; }
.subnav a {
    display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: var(--text);
}
.subnav a:hover { background: var(--surface-2); }
.subnav a.active { background: var(--surface); box-shadow: var(--shadow); color: var(--primary); font-weight: 600; }
.module-body { min-width: 0; }
@media (max-width: 760px) {
    .module { grid-template-columns: 1fr; }
    .subnav { position: static; flex-direction: row; overflow-x: auto; gap: 6px; padding-bottom: 4px; }
    .subnav a { white-space: nowrap; background: var(--surface); border: 1px solid var(--border); }
    .hide-sm { display: none; }
}

/* Cards, forms */
.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; overflow-x: auto;
}
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.row form { margin: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: end; }
.grid-2 .span-2 { grid-column: 1 / -1; }
@media (max-width: 600px) { .grid-2 { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 5px; font-size: .9rem; font-weight: 500; }
input[type=text], input[type=email], input[type=password], input[type=url], input:not([type]), textarea, select {
    width: 100%; font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-weight: 400;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.row input { width: auto; flex: 1 1 200px; }
.input-code { max-width: 160px; letter-spacing: .2em; font-variant-numeric: tabular-nums; }
.check { display: flex; align-items: center; gap: 8px; font-size: .9rem; }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    font: inherit; font-weight: 600; padding: 9px 16px; border-radius: 8px; cursor: pointer;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 6px 10px; font-size: .85rem; }
.btn-block { width: 100%; padding: 11px; }

/* Feedback */
.alert { display: block; padding: 12px 14px; border-radius: 10px; margin-bottom: 16px; font-size: .92rem; }
.alert-success { background: var(--ok-bg); color: var(--ok-text); }
.alert-error { background: var(--err-bg); color: var(--err-text); }
.alert-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge-ok { background: var(--ok-bg); color: var(--ok-text); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }

/* Tables, lists */
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.table th, .table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table form { margin: 0; }
.row-warn td { color: var(--warn-text); }
.item { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; }
.item summary { cursor: pointer; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.codes { columns: 2; list-style: none; padding: 12px; background: var(--surface-2); border-radius: 8px; font-family: ui-monospace, Menlo, monospace; }
.qr { background: #fff; display: inline-block; padding: 12px; border-radius: 8px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 24px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
nav[role=navigation] { margin-top: 12px; }
nav[role=navigation] ul { display: flex; gap: 8px; list-style: none; padding: 0; }

/* Guest */
body.guest { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card {
    width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
    border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.auth-card details summary { cursor: pointer; font-size: .9rem; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 9px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; display: inline-flex; gap: 6px; align-items: center; }
.tabs a.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }

/* Scan */
.capture { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.capture.busy { opacity: .6; pointer-events: none; }
.btn-capture { padding: 12px 18px; }
.capture-type { width: auto; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.filters input[type=search] { flex: 1 1 220px; width: auto; }
.filters select { width: auto; flex: 0 1 180px; }
input[type=search], input[type=date], input[type=number] {
    width: 100%; font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
}
.input-color { width: 44px; height: 42px; padding: 2px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); flex: none; }
.item-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.card-item {
    display: flex; gap: 12px; padding: 10px; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); box-shadow: var(--shadow);
}
.card-item:hover { border-color: var(--primary); }
.card-item-img {
    flex: none; width: 76px; height: 76px; border-radius: 8px; overflow: hidden; background: var(--surface-2);
    display: grid; place-items: center; color: var(--muted);
}
.card-item-img img { width: 100%; height: 100%; object-fit: cover; }
.card-item-body { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.card-item-body strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-tight { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip {
    display: inline-block; font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 99px;
    color: var(--chip, var(--primary)); background: color-mix(in srgb, var(--chip, var(--primary)) 14%, transparent);
}
.thumb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin-bottom: 16px; }
.thumb { position: relative; display: flex; flex-direction: column; gap: 4px; }
.thumb img, .thumb-doc {
    width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; background: var(--surface-2);
    border: 1px solid var(--border); display: grid; place-items: center; font-weight: 700; color: var(--muted);
}
.thumb-meta { font-size: .78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumb-select { cursor: pointer; }
.thumb-select input { position: absolute; top: 8px; left: 8px; width: 22px; height: 22px; z-index: 1; accent-color: var(--primary); }
.thumb-select:has(input:checked) img, .thumb-select:has(input:checked) .thumb-doc { outline: 3px solid var(--primary); outline-offset: 2px; }
.thumb-actions { position: absolute; top: 6px; right: 6px; }
.thumb-actions summary {
    list-style: none; cursor: pointer; width: 30px; height: 30px; border-radius: 99px; display: grid; place-items: center;
    background: rgba(0,0,0,.55); color: #fff; font-weight: 700;
}
.thumb-actions summary::-webkit-details-marker { display: none; }
.thumb-actions[open] > :not(summary) { margin-top: 6px; }
.thumb-actions[open] {
    z-index: 5; width: 220px; right: 0; background: var(--surface); border: 1px solid var(--border);
    border-radius: 10px; padding: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
.thumb-actions[open] summary { margin-left: auto; }
.group-title { font-size: .95rem; color: var(--muted); margin: 8px 0 10px; }
.item-layout { display: grid; grid-template-columns: 1fr 300px; gap: 20px; align-items: start; }
.item-layout aside { position: sticky; top: 72px; }
.notes { white-space: normal; background: var(--surface-2); padding: 10px 12px; border-radius: 8px; font-size: .92rem; }
.assign { position: sticky; bottom: 0; z-index: 4; }
.empty { text-align: center; padding: 32px 20px; }
.align-start { align-items: start; }
@media (max-width: 760px) {
    .item-layout { grid-template-columns: 1fr; }
    .item-layout aside { position: static; order: -1; }
    .thumb-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
    .capture .btn-capture { flex: 1 1 140px; }
    .capture-type { flex: 1 1 100%; }
    .filters select { flex: 1 1 140px; }
}
