:root {
    color-scheme: light;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --ink: #182230;
    --muted: #667085;
    --line: #dfe4ea;
    --surface: #ffffff;
    --canvas: #f4f6f8;
    --nav: #0f1a2f;
    --brand: #2f6fed;
    --brand-dark: #1f56c2;
    --focus: #2f6fed;
    --danger: #b42318;
    --shadow: 0 12px 34px rgb(16 24 40 / 8%);
    --field: #ffffff;
    --table-head: #f8fafb;
    --row-hover: #f2f6ff;
    --hero-end: #eaf1ff;
    --alert-bg: #fef3f2;
    --alert-line: #fecdca;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --ink: #f5f7fb;
    --muted: #a9b7ce;
    --line: #31415d;
    --surface: #172338;
    --canvas: #0f192d;
    --nav: #0b1425;
    --brand: #2f6fed;
    --brand-dark: #245ed0;
    --focus: #6ea0ff;
    --danger: #ff5c65;
    --shadow: 0 12px 34px rgb(0 0 0 / 20%);
    --field: #111c2d;
    --table-head: #121e31;
    --row-hover: #1c2b45;
    --hero-end: #172b4d;
    --alert-bg: #361c25;
    --alert-line: #74313a;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--canvas); }
body { margin: 0; min-height: 100vh; color: var(--ink); background: var(--canvas); }
a { color: #175cd3; text-decoration: none; }
a:hover { text-decoration: underline; }

.app-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 1.5rem;
    padding: .8rem clamp(1rem, 3vw, 2.5rem);
    color: #fff;
    background: var(--nav);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 4px 16px rgb(0 0 0 / 18%);
}

.brand { display: flex; align-items: center; gap: .7rem; color: #fff; text-decoration: none; }
.brand:hover { text-decoration: none; }
.brand-mark {
    width: 2.25rem;
    height: 2.25rem;
    display: block;
    object-fit: contain;
}
.brand span:last-child { display: grid; line-height: 1.05; }
.brand small { margin-top: .25rem; color: #aeb9c6; font-size: .68rem; font-weight: 500; }

.logout-form { margin: 0; }

.app-shell { display: grid; grid-template-columns: 15rem minmax(0, 1fr); min-height: calc(100vh - 4.1rem); }
.side-nav { position: sticky; top: 4.1rem; height: calc(100vh - 4.1rem); padding: 1.25rem 1rem; background: var(--nav); border-right: 1px solid var(--line); overflow-y: auto; }
.side-nav nav { display: grid; gap: .25rem; }
.side-nav a { padding: .72rem .85rem; color: #dbe6f8; font-size: .9rem; font-weight: 650; border-radius: .45rem; }
.side-nav a:hover { color: #fff; background: #243655; text-decoration: none; }
.side-nav a.active { color: #fff; background: var(--brand); box-shadow: 0 6px 18px rgb(47 111 237 / 28%); }
.nav-settings { display: grid; gap: .6rem; margin-top: 1.2rem; padding-top: 1rem; color: #dbe6f8; border-top: 1px solid #31415d; }
.settings-panel { max-width: 42rem; }
.theme-toggle { width: 100%; margin: 0; color: #dbe6f8; background: transparent; border-color: #52627c; }
.nav-toggle { display: none; }
.page-content { width: min(1480px, 100%); margin: 0 auto; padding: clamp(1.25rem, 3vw, 2.5rem); }
h1 { margin: 0 0 1.4rem; font-size: clamp(1.8rem, 3vw, 2.55rem); letter-spacing: -.035em; }
h2 { margin: 0 0 1rem; font-size: 1.35rem; }
h3 { margin: 1.25rem 0 .75rem; }
p { line-height: 1.55; }
.muted { color: var(--muted); }
.field-error { margin: -.35rem 0 .5rem; color: #ff9ca2; font-size: .9rem; }
.patrol-form-section { margin-bottom: 1rem; padding: 1rem; background: color-mix(in srgb, var(--canvas) 45%, transparent); border: 1px solid var(--line); border-radius: .7rem; }
.patrol-form-section h3 { margin-top: 0; }
.search-add-row { display: grid; grid-template-columns: 1fr auto; gap: .5rem; align-items: center; }
.search-picker { position: relative; display: block; }
.search-picker > input { width: 100%; }
.search-options { position: absolute; z-index: 30; top: calc(100% + .3rem); left: 0; right: 0; display: none; max-height: 14rem; padding: .35rem; overflow-y: auto; background: var(--surface); border: 1px solid var(--line); border-radius: .55rem; box-shadow: 0 12px 30px rgb(0 0 0 / 30%); }
.search-picker:focus-within .search-options { display: grid; }
.vehicle-picker:not(.is-open) .search-options, .controlled-picker:not(.is-open) .search-options { display: none; }
.search-options button { justify-content: flex-start; width: 100%; min-height: 2.35rem; margin: 0; padding: .5rem .65rem; color: var(--ink); text-align: left; background: transparent; border-color: transparent; }
.search-options button:hover, .search-options button:focus { color: #fff; background: var(--brand); }
.search-empty { padding: .65rem; color: var(--muted); font-size: .88rem; }
.additional-guard-picker { padding-top: 1.25rem; }
.operation-feedback { padding: .7rem .85rem; color: #b9f6d2; background: rgb(18 120 70 / 18%); border: 1px solid rgb(65 211 132 / 45%); border-radius: .55rem; }
.realtime-toast-stack { position: fixed; z-index: 90; top: 5.75rem; right: 2.25rem; display: grid; gap: .65rem; width: min(23.75rem, calc(100vw - 2rem)); }
.realtime-toast { display: grid; grid-template-columns: 2.5rem minmax(0, 1fr) 1.625rem; gap: .75rem; align-items: start; padding: .875rem; color: #991b1b; background: #fef2f2; border: 1px solid #fca5a5; border-radius: .875rem; box-shadow: 0 6px 22px rgb(0 0 0 / 20%); animation: toast-enter .2s ease-out; }
.realtime-toast-icon { display: grid; place-items: center; width: 2.5rem; height: 2.5rem; color: #fff; background: #ef4444; border-radius: 50%; font-size: 1.15rem; }
.realtime-toast-content { display: grid; min-width: 0; }
.realtime-toast-content strong { font-size: .875rem; overflow-wrap: anywhere; }
.realtime-toast-content span:first-of-type { margin-top: .125rem; font-size: .8125rem; overflow-wrap: anywhere; }
.realtime-toast-content span:last-of-type { margin-top: .25rem; font-size: .75rem; }
.realtime-toast-content small { margin-top: .25rem; color: #667085; font-size: .6875rem; }
.realtime-toast-close { width: 1.625rem; min-height: 1.625rem; margin: 0; padding: 0; color: #991b1b; background: transparent; border: 0; border-radius: 50%; font-size: 1rem; }
.realtime-toast-close:hover { color: #fff; background: #ef4444; }
.inline-operation-error { margin: 0; padding: .7rem .85rem; color: #ffb4b8; background: rgb(239 68 68 / 12%); border: 1px solid rgb(239 68 68 / 45%); border-radius: .55rem; }
.phone-incident-card { padding: clamp(1.25rem, 2.5vw, 2rem); }
.phone-incident-content { display: grid; gap: 1.25rem; width: min(100%, 58rem); }
.phone-incident-header h2 { margin-bottom: .3rem; }
.phone-incident-header p { margin: 0; }
.phone-search-row { display: grid; grid-template-columns: minmax(16rem, 30rem) auto; gap: .75rem; align-items: end; }
.phone-search-row button { margin: 0; }
.phone-incident-card input, .phone-incident-card select, .phone-incident-card textarea { width: 100%; max-width: none; }
.unregistered-action { width: max-content; margin: -.35rem 0 0; }
.resident-summary { display: grid; gap: .2rem; padding: .85rem 1rem; background: rgb(47 111 237 / 9%); border: 1px solid rgb(47 111 237 / 35%); border-radius: .65rem; }
.resident-summary span { color: var(--muted); }
.phone-form-block { display: grid; gap: 1rem; padding: 1.15rem; background: color-mix(in srgb, var(--canvas) 45%, transparent); border: 1px solid var(--line); border-radius: .75rem; }
.phone-form-block h3, .phone-form-block p { margin: 0; }
.incident-data-block { margin-top: .25rem; }
.phone-form-actions { display: flex; justify-content: flex-end; padding-top: .15rem; }
.phone-form-actions button { min-width: 12rem; margin: 0; }
.selected-guard-list { display: grid; gap: .45rem; margin: .65rem 0; padding: 0; list-style: none; }
.selected-guard-list li { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .55rem .7rem; background: var(--canvas); border: 1px solid var(--line); border-radius: .55rem; }
.remove-chip { min-width: 2rem; min-height: 2rem; margin: 0; padding: 0; color: #fff; background: var(--danger); border-color: var(--danger); border-radius: 50%; font-size: 1.2rem; line-height: 1; }
.eyebrow { color: var(--brand-dark); font-size: .75rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }

button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 2.55rem;
    margin: .25rem .35rem .25rem 0;
    padding: .62rem 1rem;
    color: #fff;
    font: inherit;
    font-size: .88rem;
    font-weight: 700;
    background: var(--brand);
    border: 1px solid var(--brand);
    border-radius: .55rem;
    cursor: pointer;
}
button:hover, .button:hover { background: var(--brand-dark); border-color: var(--brand-dark); text-decoration: none; }
button:disabled { opacity: .5; cursor: not-allowed; }
.table-action { min-height: 2.2rem; margin: 0; padding: .45rem .75rem; }
.button-secondary { color: #fff; background: transparent; border-color: #4a596b; white-space: nowrap; }
.button-secondary:hover { background: #263445; border-color: #69798c; }
.button-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.button-danger:hover { background: #912018; border-color: #912018; }

form:not(.logout-form), section {
    margin: 1rem 0;
    padding: 1.25rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: .85rem;
    box-shadow: var(--shadow);
}
form:not(.logout-form) { display: flex; flex-wrap: wrap; align-items: end; gap: .9rem; }
label { display: grid; gap: .4rem; color: var(--ink); font-size: .82rem; font-weight: 700; }
.choice-group { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); gap: .5rem; margin: 0; padding: .8rem; border: 1px solid var(--line); border-radius: .55rem; }
.choice-group legend { padding: 0 .35rem; font-size: .82rem; font-weight: 700; }
.choice-item { display: flex; align-items: center; gap: .5rem; padding: .4rem; font-weight: 500; }
input, select, textarea {
    width: min(100%, 30rem);
    min-height: 2.55rem;
    padding: .58rem .72rem;
    color: var(--ink);
    font: inherit;
    background: var(--field);
    border: 1px solid var(--line);
    border-radius: .5rem;
    outline: none;
}
textarea { min-height: 6rem; resize: vertical; }
input[type="checkbox"] { width: 1rem; min-height: 1rem; }
input:focus, select:focus, textarea:focus { border-color: var(--focus); box-shadow: 0 0 0 3px rgb(46 144 250 / 18%); }

table { width: 100%; margin: 1rem 0; overflow: hidden; color: var(--ink); background: var(--surface); border-collapse: separate; border-spacing: 0; border: 1px solid var(--line); border-radius: .75rem; box-shadow: var(--shadow); }
th, td { padding: .85rem 1rem; text-align: left; vertical-align: middle; border-bottom: 1px solid #edf0f3; }
th { color: var(--muted); font-size: .72rem; letter-spacing: .045em; text-transform: uppercase; background: var(--table-head); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--row-hover); }
td button { min-height: 2.15rem; padding: .42rem .7rem; font-size: .78rem; }

[role="alert"], .alert { padding: .8rem 1rem; color: var(--danger); background: var(--alert-bg); border: 1px solid var(--alert-line); border-radius: .55rem; }
.hero-panel { min-height: 22rem; padding: clamp(2rem, 6vw, 5rem); background: linear-gradient(130deg, var(--surface) 0%, var(--surface) 55%, var(--hero-end) 100%); }
.hero-panel h1 { max-width: 14ch; margin-top: .6rem; font-size: clamp(2.5rem, 6vw, 4.75rem); }
.hero-panel p { max-width: 58ch; color: var(--muted); font-size: 1.1rem; }
.hero-actions { margin-top: 1.5rem; }

.auth-shell { display: grid; place-items: center; min-height: calc(100vh - 9rem); }
.auth-card { width: min(100%, 29rem); padding: clamp(1.5rem, 4vw, 2.5rem); }
.auth-card h1 { margin: .55rem 0 .4rem; font-size: 2rem; }
.auth-card form { display: grid; width: 100%; min-width: 0; padding: 0; border: 0; box-shadow: none; }
.auth-card form label, .auth-card form input, .auth-card form button { width: 100%; min-width: 0; max-width: none; }
.auth-card label, .auth-card input, .auth-card button { width: 100%; }
.auth-card button { margin: .3rem 0 0; }
.secure-note { margin: 1.25rem 0 0; color: var(--muted); font-size: .75rem; text-align: center; }

.pagination { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .35rem; margin: 1rem auto; }
.pagination button { min-height: 2.35rem; margin: 0; padding: .5rem .75rem; }
.pagination span { min-width: 9rem; padding: .5rem; text-align: center; font-weight: 700; }
.search-form input[type="text"], .search-form input:not([type]) { min-width: min(26rem, 70vw); }
.report-toolbar { display: flex; flex-wrap: wrap; align-items: end; gap: .9rem; }
.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 1rem; background: rgb(3 10 22 / 78%); backdrop-filter: blur(5px); animation: modal-fade .16s ease-out; }
.modal-panel { width: min(46rem, 100%); max-height: calc(100vh - 2rem); margin: 0; padding: 0; overflow-y: auto; border-color: #405272; box-shadow: 0 28px 90px rgb(0 0 0 / 55%); animation: modal-rise .2s ease-out; }
.modal-header { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: 1.2rem 1.4rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: .2rem 0 0; font-size: 1.55rem; }
.modal-close { width: 2.45rem; min-height: 2.45rem; margin: 0; padding: 0; color: var(--muted); font-size: 1.55rem; background: transparent; border-color: var(--line); border-radius: 50%; }
.modal-close:hover { color: var(--ink); background: var(--row-hover); border-color: var(--focus); }
.modal-body { display: grid; gap: 1rem; padding: 1.4rem; }
.modal-body > h2:first-child { display: none; }
.modal-body label { margin: 0; }
.modal-body input:not([type="checkbox"]), .modal-body select, .modal-body textarea { width: 100%; max-width: none; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.form-field-wide { grid-column: 1 / -1; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; padding-top: .25rem; border-top: 1px solid var(--line); }
.modal-actions button { margin-top: .8rem; }
.modal-open { overflow: hidden; }
.status-badge { display: inline-flex; align-items: center; width: max-content; padding: .28rem .58rem; font-size: .75rem; font-weight: 750; border: 1px solid transparent; border-radius: 999px; }
.status-danger { color: #b42318; background: #fef3f2; border-color: #fecdca; }
.status-warning { color: #b54708; background: #fffaeb; border-color: #fedf89; }
.status-success { color: #067647; background: #ecfdf3; border-color: #abefc6; }
.status-neutral { color: var(--muted); background: var(--table-head); border-color: var(--line); }
.status-info { color: #175cd3; background: #eff8ff; border-color: #b2ddff; }
:root[data-theme="dark"] .status-danger { color: #ff7078; background: rgb(239 68 68 / 14%); border-color: rgb(239 68 68 / 60%); }
:root[data-theme="dark"] .status-warning { color: #fdb536; background: rgb(245 158 11 / 14%); border-color: rgb(245 158 11 / 60%); }
:root[data-theme="dark"] .status-success { color: #35d399; background: rgb(16 185 129 / 14%); border-color: rgb(16 185 129 / 60%); }
:root[data-theme="dark"] .status-info { color: #74a7ff; background: rgb(47 111 237 / 14%); border-color: rgb(110 160 255 / 55%); }
:root[data-theme="dark"] .status-neutral { color: #b8c5d9; background: rgb(148 163 184 / 10%); border-color: rgb(148 163 184 / 42%); }
.empty-state { display: grid; justify-items: center; gap: .45rem; margin: 1rem 0; padding: 2.5rem 1.25rem; color: var(--muted); text-align: center; background: var(--surface); border: 1px dashed var(--line); border-radius: .85rem; }
.empty-state strong { color: var(--ink); font-size: 1rem; }
.empty-state p { max-width: 42rem; margin: 0; }
.empty-state-icon { color: var(--brand); font-size: 2rem; }
.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1rem; margin: 1rem 0 2rem; }
.stat-card { display: grid; gap: .4rem; padding: 1.1rem; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--brand); border-radius: .75rem; box-shadow: var(--shadow); }
.stat-card span { color: var(--muted); font-size: .8rem; font-weight: 700; }
.stat-card strong { font-size: 1.8rem; }
.stat-card-warning { border-top-color: #f79009; }.stat-card-danger { border-top-color: var(--danger); }.stat-card-success { border-top-color: #12b76a; }
.schedule-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 28rem), 1fr)); gap: 1rem; margin-top: 1rem; }
.schedule-card { margin: 0; }
.schedule-card h2 { margin-bottom: .85rem; }
.schedule-items { display: grid; gap: .55rem; }
.schedule-item { display: grid; grid-template-columns: 7.5rem minmax(0,1fr); align-items: start; gap: 1rem; min-height: 4.5rem; padding: .8rem .9rem; background: var(--table-head); border: 1px solid var(--line); border-radius: .6rem; }
.schedule-item > strong { padding-top: .1rem; color: var(--ink); }
.schedule-slot-content { display: grid; gap: .45rem; min-width: 0; padding-left: 1rem; border-left: 1px solid var(--line); }
.schedule-patrol { display: grid; gap: .15rem; }
.schedule-patrol + .schedule-patrol { padding-top: .45rem; border-top: 1px solid var(--line); }
.repeat-preview-list { display: grid; gap: .75rem; }
.repeat-preview-item { display: grid; gap: .75rem; padding: 1rem; background: var(--table-head); border: 1px solid var(--line); border-radius: .65rem; }
.repeat-preview-item.has-conflict { border-color: rgb(245 158 11 / 70%); }
.repeat-preview-item header { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .5rem; }
.detail-list { display: grid; grid-template-columns: 8rem minmax(0,1fr); gap: .65rem 1rem; margin: 0; }.detail-list dt { color: var(--muted); font-weight: 700; }.detail-list dd { margin: 0; overflow-wrap: anywhere; }
@keyframes modal-fade { from { opacity: 0; } }
@keyframes modal-rise { from { opacity: 0; transform: translateY(12px) scale(.985); } }
@keyframes toast-enter { from { opacity: 0; transform: translateY(-8px); } }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

ul { padding-left: 1.4rem; }
li { margin: .4rem 0; line-height: 1.45; }

@media (max-width: 980px) {
    .app-header { position: sticky; }
    .app-shell { display: block; }
    .nav-toggle { display: inline-flex; margin: 0; }
    .side-nav { position: fixed; inset: 4.1rem auto 0 0; z-index: 20; width: min(19rem, 86vw); height: calc(100vh - 4.1rem); transform: translateX(-105%); transition: transform .18s ease; box-shadow: 16px 0 40px rgb(0 0 0 / 30%); }
    .side-nav-open { transform: translateX(0); }
}

@media (max-width: 680px) {
    .page-content { padding: 1rem; }
    .realtime-toast-stack { top: 4.75rem; right: 1rem; }
    form:not(.logout-form) { display: grid; align-items: stretch; }
    input, select, textarea { width: 100%; }
    table { display: block; overflow-x: auto; white-space: nowrap; }
    .brand small { display: none; }
    .modal-backdrop { align-items: end; padding: .5rem; }
    .modal-panel { max-height: calc(100vh - 1rem); border-radius: .85rem .85rem 0 0; }
    .form-grid { grid-template-columns: 1fr; }
    .phone-search-row { grid-template-columns: 1fr; }
    .phone-search-row button, .unregistered-action, .phone-form-actions button { width: 100%; }
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .schedule-item { grid-template-columns: 6.5rem minmax(0,1fr); gap: .65rem; }
}
