:root {
    --bg: #0b0b0d;
    --surface: #141417;
    --surface-2: #1e1e23;
    --border: #2b2b31;
    --text: #f2f2f4;
    --muted: #9a9aa4;
    --brand: #c1101b;
    --brand-2: #6f0a10;
    --danger: #ef3d31;
    --ok: #35c76a;
    --radius: 14px;
    --tabbar-h: 64px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior-y: none;
}

a { color: var(--brand-2); text-decoration: none; }
h1, h2 { margin: 0 0 .5rem; font-weight: 700; }
h2 { font-size: 1.05rem; }

/* ---------- Shell ---------- */
.app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(.75rem + var(--safe-top)) 1rem .75rem;
    background: rgba(11, 11, 13, .85);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: .5rem; color: var(--text); font-weight: 700; }
.brand-mark {
    display: inline-grid; place-items: center;
    width: 28px; height: 28px; border-radius: 8px;
    background: linear-gradient(135deg, var(--brand), var(--brand-2));
    color: #fff; font-size: .8rem;
}
.brand-text { font-size: 1.05rem; }

.app-main {
    flex: 1;
    padding: 1rem 1rem calc(var(--tabbar-h) + var(--safe-bottom) + 1rem);
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
}

.page-title { font-size: 1.4rem; margin-bottom: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.text-danger { color: var(--danger); }

/* ---------- Bottom tab bar ---------- */
.tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 30;
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: rgba(15, 15, 18, .96);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
}
.tab {
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    color: var(--muted); font-size: .68rem; padding: .35rem .6rem;
    border-radius: 12px; min-width: 56px;
}
.tab-icon { line-height: 0; }
.tab-icon svg { width: 24px; height: 24px; display: block; }
.tab.active { color: var(--text); background: var(--surface-2); }
.tab.active .tab-icon svg { color: #ff3b34; }

/* ---------- Cards & layout ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
}

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
@media (max-width: 480px) { .field-grid { grid-template-columns: 1fr; } }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .75rem; }
.field > span { font-size: .8rem; color: var(--muted); }
input, textarea, select {
    width: 100%;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 12px;
    padding: .75rem .85rem;
    font-size: 1rem;
    font-family: inherit;
}
input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(193, 16, 27, .35);
}
textarea { resize: vertical; }
.checkbox { display: flex; align-items: center; gap: .6rem; margin: .5rem 0; }
.checkbox input { width: auto; }
.validation-message, .field .validation-message { color: var(--danger); font-size: .8rem; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: .8rem 1.1rem;
    font-size: 1rem; font-weight: 600;
    cursor: pointer;
    min-height: 48px;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--brand), var(--brand-2)); border: none; color: #fff; }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-block { width: 100%; }
.link-btn { background: none; border: none; color: var(--brand-2); font: inherit; cursor: pointer; padding: .4rem; }
.link-btn.danger { color: var(--danger); }

.button-row { display: flex; flex-direction: column; gap: .6rem; }
.button-row.sticky-actions { position: sticky; bottom: calc(var(--tabbar-h) + var(--safe-bottom)); }

/* ---------- Search & chips ---------- */
.search-bar { margin-bottom: .75rem; }
.search-input { width: 100%; }
.tag-filter { display: flex; gap: .5rem; overflow-x: auto; padding-bottom: .5rem; margin-bottom: .75rem; -webkit-overflow-scrolling: touch; }
.chip {
    flex: 0 0 auto;
    background: var(--surface-2); color: var(--muted);
    border: 1px solid var(--border); border-radius: 999px;
    padding: .45rem .9rem; font-size: .85rem; cursor: pointer; white-space: nowrap;
}
.chip.active { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; border: none; }
.add-tag { display: flex; gap: .5rem; margin-top: .5rem; }
.add-tag .search-input { flex: 1; }

/* ---------- Contact list ---------- */
.contact-list { list-style: none; margin: 0; padding: 0; }
.contact-item {
    display: flex; align-items: center; gap: .8rem;
    padding: .8rem; border: 1px solid var(--border);
    border-radius: var(--radius); background: var(--surface);
    margin-bottom: .6rem; cursor: pointer;
}
.contact-item:active { background: var(--surface-2); }
.avatar {
    flex: 0 0 auto;
    width: 44px; height: 44px; border-radius: 50%;
    display: grid; place-items: center; font-weight: 700; color: #fff;
    background: linear-gradient(135deg, var(--brand), var(--brand-2));
}
.avatar-lg { width: 64px; height: 64px; font-size: 1.3rem; }
.contact-main { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; }
.contact-sub { color: var(--muted); font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag-row { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .3rem; }
.tag-badge { font-size: .7rem; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: .1rem .4rem; color: var(--muted); }
.followup-flag { flex: 0 0 auto; font-size: .78rem; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: .25rem .5rem; }
.followup-flag.due { color: #fff; background: var(--danger); }

.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.empty .btn { margin-top: 1rem; }

/* ---------- Detail ---------- */
.detail-header { display: flex; gap: 1rem; align-items: center; margin-bottom: 1rem; }
.detail-name { font-size: 1.5rem; margin: 0; }
.detail-sub { color: var(--muted); margin: .2rem 0 0; }
.quick-actions { display: flex; gap: .6rem; margin-bottom: 1rem; flex-wrap: wrap; }
.qa {
    flex: 1 1 0; min-width: 68px;
    display: flex; flex-direction: column; align-items: center; gap: .25rem;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 12px; padding: .7rem .4rem; font-size: 1.3rem; color: var(--text);
}
.qa span { font-size: .72rem; color: var(--muted); }
.kv { display: flex; justify-content: space-between; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid var(--border); }
.kv:last-child { border-bottom: none; }
.kv > span { color: var(--muted); }
.kv.col { flex-direction: column; gap: .25rem; }
.kv.col p { margin: 0; }

/* ---------- Scan ---------- */
.scan-drop { margin: 1rem 0; }
.scan-cta {
    display: flex; flex-direction: column; align-items: center; gap: .6rem;
    padding: 2rem 1rem; text-align: center;
    border: 2px dashed var(--border); border-radius: var(--radius);
    background: var(--surface); cursor: pointer;
}
.scan-icon { font-size: 2.6rem; }
.scan-cta input[type=file] { display: none; }

/* ---------- Reports ---------- */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; text-align: center; }
.stat-value { font-size: 1.8rem; font-weight: 800; }
.stat-label { color: var(--muted); font-size: .8rem; }
.stat-alert { border-color: var(--danger); }
.stat-alert .stat-value { color: var(--danger); }
.bar-row { display: flex; align-items: center; gap: .6rem; margin: .4rem 0; }
.bar-label { flex: 0 0 30%; font-size: .85rem; }
.bar-track { flex: 1; height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: linear-gradient(90deg, var(--brand), var(--brand-2)); }
.bar-count { flex: 0 0 auto; font-size: .85rem; color: var(--muted); min-width: 1.5rem; text-align: right; }

/* ---------- Generic lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .7rem 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: none; }
.list-row.clickable { cursor: pointer; }
.row-actions { display: flex; gap: .4rem; flex: 0 0 auto; }

/* ---------- Alerts ---------- */
.alert { border-radius: 12px; padding: .8rem 1rem; margin-bottom: 1rem; font-size: .9rem; }
.alert.error { background: rgba(255, 68, 56, .14); border: 1px solid var(--danger); color: #ffb3ad; }
.alert.success { background: rgba(53, 199, 106, .12); border: 1px solid var(--ok); color: #a9edc2; }
.alert.info { background: rgba(193, 16, 27, .14); border: 1px solid var(--brand); color: #ff9a9f; }

/* ---------- Auth ---------- */
.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 2rem 1.5rem; text-align: center; }
.auth-logo {
    width: 56px; height: 56px; margin: 0 auto 1rem; border-radius: 16px;
    display: grid; place-items: center; font-size: 1.6rem; color: #fff;
    background: linear-gradient(135deg, var(--brand), var(--brand-2));
}
.auth-card h1 { font-size: 1.4rem; }
.auth-card .field { text-align: left; }
.auth-card .btn { margin-top: .5rem; }

/* ---------- Blazor error ---------- */
#blazor-error-ui {
    display: none;
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000;
    background: #1a0a0b; color: #ffb3ad; padding: .8rem 1rem; border-top: 1px solid var(--danger);
}
#blazor-error-ui .reload { color: #fff; text-decoration: underline; margin-left: .5rem; }
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: 1rem; top: .8rem; }
