:root {
    --navy: #071827;
    --blue: #145b9d;
    --bg: #eef2f7;
    --surface: #ffffff;
    --surface-2: #f6f8fb;
    --border: #d5dee8;
    --text: #0a192d;
    --muted: #5b6b7c;
    --green: #1f9463;
    --purple: #7b2d8e;
    --orange: #e07b12;
    --red: #c9443d;
    --green-soft: #e3f5ec;
    --purple-soft: #f3e6f6;
    --orange-soft: #fdf0e1;
    --red-soft: #fbe6e5;
    --blue-soft: #e3eef9;
    --shadow: 0 1px 2px rgba(7, 24, 39, 0.06), 0 2px 8px rgba(7, 24, 39, 0.06);
    --radius: 12px;
    --header-h: 56px;
    --nav-h: 62px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    color-scheme: light;
}

:root[data-theme="dark"] {
    --bg: #0b1520;
    --surface: #13212f;
    --surface-2: #182a3b;
    --border: #273b50;
    --text: #e6edf5;
    --muted: #94a6b8;
    --blue: #3a86d1;
    --green: #2fb07a;
    --purple: #b06ad0;
    --orange: #f09a3e;
    --red: #e5675f;
    --green-soft: #143327;
    --purple-soft: #2c1a35;
    --orange-soft: #3a2712;
    --red-soft: #3b1a19;
    --blue-soft: #132d46;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
}

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

html, body {
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 15px;
    -webkit-tap-highlight-color: transparent;
}

body { display: flex; flex-direction: column; min-height: 100vh; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

/* ---------- Top bars ---------- */
.install-banner {
    background: var(--blue);
    color: #fff;
    padding: 8px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
}
.install-actions { display: flex; gap: 6px; }

.app-header {
    position: sticky;
    top: 0;
    z-index: 50;
    height: var(--header-h);
    background: var(--navy);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 10px;
    padding-top: env(safe-area-inset-top, 0px);
    box-sizing: content-box;
}
.app-header h1 {
    flex: 1;
    font-size: 17px;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-left: 6px;
}
.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: none;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.2); }
.icon-btn.spinning { animation: spin 0.8s linear infinite; }

.offline-banner {
    background: var(--orange);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    padding: 8px 14px;
    text-align: center;
}

.app-main {
    flex: 1;
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
    padding: 16px 14px calc(var(--nav-h) + var(--safe-bottom) + 90px);
}

/* ---------- Bottom tab bar ---------- */
.tab-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    display: flex;
    background: var(--navy);
    height: calc(var(--nav-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
}
.tab-btn {
    flex: 1;
    border: none;
    background: none;
    color: rgba(255, 255, 255, 0.65);
    font-size: 12px;
    font-weight: 600;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
}
.tab-btn .tab-icon { font-size: 19px; line-height: 1; }
.tab-btn.active { color: #fff; box-shadow: inset 0 3px 0 var(--blue); background: rgba(255, 255, 255, 0.06); }

/* ---------- Building blocks ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}
.card h2 { font-size: 16px; margin-bottom: 6px; }
.card p, .muted { color: var(--muted); font-size: 13px; }
.section-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin: 18px 2px 8px;
}
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.text-center { text-align: center; }

label.field-label {
    display: block;
    font-size: 13px;
    font-weight: 700;
    margin: 14px 0 6px;
}
label.field-label .req { color: var(--red); }
input[type="text"], input[type="password"], input[type="date"], input[type="search"], select, textarea {
    width: 100%;
    padding: 11px 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    font-size: 15px;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
textarea { resize: vertical; min-height: 80px; }
.field-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.field-warn { font-size: 12px; color: var(--orange); margin-top: 4px; font-weight: 600; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
    padding: 13px 14px;
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 15px;
    font-weight: 700;
    margin-top: 10px;
    transition: filter 0.15s, transform 0.05s;
}
.btn:active { transform: scale(0.98); }
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.55; pointer-events: none; }
.btn-green { background: var(--green); }
.btn-blue { background: var(--blue); }
.btn-orange { background: var(--orange); }
.btn-red { background: var(--red); }
.btn-purple { background: var(--purple); }
.btn-navy { background: var(--navy); }
.btn-outline { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost-light { background: rgba(255, 255, 255, 0.15); }
.btn-sm { width: auto; padding: 8px 12px; font-size: 13px; margin-top: 0; border-radius: 8px; }

/* ---------- Login ---------- */
.login-wrap { max-width: 400px; margin: 6vh auto 0; }
.login-logo {
    width: 72px;
    height: 72px;
    border-radius: 18px;
    margin: 0 auto 14px;
    display: block;
}
.login-wrap h2 { text-align: center; font-size: 22px; }
.login-wrap .muted { text-align: center; margin-bottom: 8px; }
.form-error { color: var(--red); font-size: 13px; font-weight: 600; margin-top: 10px; min-height: 18px; }

/* ---------- Dashboard ---------- */
.hello { margin: 4px 2px 14px; }
.hello h2 { font-size: 22px; }
.hello p { color: var(--muted); font-size: 13px; margin-top: 2px; }

.lab-picker .row { margin-top: 6px; }
.lab-picker select { font-weight: 700; }

.tile-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile {
    text-align: left;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 128px;
    position: relative;
    overflow: hidden;
}
.tile::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--accent); }
.tile:active { transform: scale(0.98); }
.tile-icon {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: var(--accent-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}
.tile-count { font-size: 28px; font-weight: 800; line-height: 1.1; margin-top: 6px; }
.tile-label { font-size: 14px; font-weight: 700; }
.tile-sub { font-size: 12px; color: var(--muted); }
.tile-sub strong { color: var(--accent); }

.accent-green { --accent: var(--green); --accent-soft: var(--green-soft); }
.accent-purple { --accent: var(--purple); --accent-soft: var(--purple-soft); }
.accent-orange { --accent: var(--orange); --accent-soft: var(--orange-soft); }
.accent-red { --accent: var(--red); --accent-soft: var(--red-soft); }
.accent-blue { --accent: var(--blue); --accent-soft: var(--blue-soft); }

.summary-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.summary-row .num { font-size: 20px; font-weight: 800; }
.summary-row .lbl { font-size: 11px; color: var(--muted); }

/* ---------- Lists ---------- */
.toolbar { position: sticky; top: calc(var(--header-h) + env(safe-area-inset-top, 0px)); z-index: 10; background: var(--bg); padding: 2px 0 8px; margin: -2px 0 4px; }
.search-box { position: relative; }
.search-box input { padding-left: 38px; }
.search-box::before { content: "🔍"; position: absolute; left: 12px; top: 50%; transform: translateY(-50%); font-size: 15px; opacity: 0.6; }
.toolbar-row { display: flex; gap: 8px; margin-top: 8px; align-items: center; }
.toolbar-row select { width: auto; padding: 8px 10px; font-size: 13px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; flex: 1; }
.chips::-webkit-scrollbar { display: none; }
.chip {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
}
.chip.active { background: var(--navy); color: #fff; border-color: var(--navy); }
:root[data-theme="dark"] .chip.active { background: var(--blue); border-color: var(--blue); }
:root[data-theme="dark"] .btn-navy { background: #2a4157; }

.select-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 10px;
    margin-bottom: 8px;
    font-size: 13px;
    font-weight: 600;
}
.select-bar label { display: flex; align-items: center; gap: 8px; flex: 1; cursor: pointer; }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--blue); cursor: pointer; flex-shrink: 0; }

.item-card {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    margin-bottom: 8px;
    cursor: pointer;
    box-shadow: var(--shadow);
}
.item-card:active { background: var(--surface-2); }
.item-card.selected { border-color: var(--blue); box-shadow: 0 0 0 2px var(--blue-soft); }
.item-thumb {
    width: 54px;
    height: 54px;
    border-radius: 8px;
    object-fit: cover;
    flex-shrink: 0;
    background: var(--surface-2);
}
.item-thumb-empty {
    width: 54px;
    height: 54px;
    border-radius: 8px;
    flex-shrink: 0;
    background: var(--accent-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
}
.item-info { flex: 1; min-width: 0; }
.item-name { font-size: 15px; font-weight: 700; overflow-wrap: anywhere; }
.item-meta { font-size: 12px; color: var(--muted); margin-top: 3px; overflow-wrap: anywhere; }
.item-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
}
.badge-orange { background: var(--orange-soft); color: var(--orange); }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-blue { background: var(--blue-soft); color: var(--blue); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-purple { background: var(--purple-soft); color: var(--purple); }
.badge-grey { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }

.empty-state { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-state .big { font-size: 42px; margin-bottom: 8px; }

.fab-wrap {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
    z-index: 40;
    display: flex;
    justify-content: center;
    pointer-events: none;
    padding: 0 14px;
}
.fab-wrap .btn { pointer-events: auto; max-width: 732px; box-shadow: 0 6px 18px rgba(7, 24, 39, 0.25); margin: 0; }

.result-count { font-size: 12px; color: var(--muted); margin: 4px 2px 8px; }

/* ---------- Edit form ---------- */
.image-box {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    min-height: 150px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: pointer;
}
.image-box img { width: 100%; max-height: 260px; object-fit: contain; display: block; }
.image-box .placeholder { color: var(--muted); font-size: 13px; text-align: center; padding: 20px; }
.image-box .placeholder .big { font-size: 34px; display: block; margin-bottom: 4px; }
.image-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.image-actions .btn { margin: 0; padding: 10px 6px; font-size: 13px; }
.meta-line { font-size: 12px; color: var(--muted); margin-top: 14px; text-align: center; }

/* ---------- Logs ---------- */
.log-card { padding: 10px 12px; }
.log-head { display: flex; justify-content: space-between; gap: 8px; font-size: 11px; color: var(--muted); }
.log-details { font-size: 13px; margin-top: 4px; overflow-wrap: anywhere; }
.log-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }

/* ---------- More ---------- */
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 3px; margin-top: 8px; }
.segmented button { border: none; background: none; padding: 8px; border-radius: 8px; font-size: 13px; font-weight: 700; color: var(--muted); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.kv { display: flex; justify-content: space-between; font-size: 13px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.kv:last-child { border-bottom: none; }
.kv span:first-child { color: var(--muted); }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(4, 12, 20, 0.55);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    animation: fade 0.15s ease;
}
.modal {
    background: var(--surface);
    width: 100%;
    max-width: 480px;
    border-radius: 18px 18px 0 0;
    padding: 20px 18px calc(18px + var(--safe-bottom));
    animation: slide-up 0.2s ease;
    max-height: 90vh;
    overflow-y: auto;
}
@media (min-width: 600px) {
    .modal-backdrop { align-items: center; }
    .modal { border-radius: 18px; }
}
.modal h3 { font-size: 18px; margin-bottom: 8px; }
.modal .modal-body { font-size: 14px; color: var(--muted); white-space: pre-line; overflow-wrap: anywhere; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; }
.modal-actions .btn { margin: 0; }
.modal img.full-image { width: 100%; border-radius: 10px; }

/* ---------- Toast ---------- */
.toast-root {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--nav-h) + var(--safe-bottom) + 76px);
    z-index: 300;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
    padding: 0 14px;
}
.toast {
    background: var(--navy);
    color: #fff;
    padding: 11px 16px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
    max-width: 480px;
    animation: slide-up 0.2s ease;
}
.toast.success { background: var(--green); }
.toast.error { background: var(--red); }
.toast.warn { background: var(--orange); }

/* ---------- Loading ---------- */
.loading-screen { text-align: center; padding: 80px 0; color: var(--muted); }
.spinner {
    width: 34px;
    height: 34px;
    border: 3px solid var(--border);
    border-top-color: var(--blue);
    border-radius: 50%;
    margin: 0 auto 12px;
    animation: spin 0.8s linear infinite;
}
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 12px; }
.progress > div { height: 100%; background: var(--blue); transition: width 0.2s; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(20px); opacity: 0; } }

/* ---------- Print report ---------- */
#print-area { display: none; }
@media print {
    body.printing > *:not(#print-area) { display: none !important; }
    body.printing #print-area { display: block; color: #000; background: #fff; font-size: 11px; }
    body.printing { background: #fff; }
    #print-area h1 { font-size: 18px; margin-bottom: 2px; }
    #print-area h2 { font-size: 14px; margin: 16px 0 6px; }
    #print-area .print-meta { color: #444; margin-bottom: 10px; }
    #print-area table { width: 100%; border-collapse: collapse; page-break-inside: auto; }
    #print-area tr { page-break-inside: avoid; }
    #print-area th, #print-area td { border: 1px solid #999; padding: 4px 6px; text-align: left; vertical-align: top; }
    #print-area th { background: #e8e8e8; }
    @page { margin: 12mm; }
}
