:root {
  --bg: #f5f6f2; --surface: #ffffff; --surface-2: #eef1ea; --border: #dde2d6;
  --text: #1d2a1f; --muted: #5d6b5f; --accent: #2f6b3a; --accent-ink: #ffffff; --accent-soft: #e2efe2;
  --danger: #b3362b; --warn: #a86a00; --focus: #4a90e2;
  --c-excellent: #1b7f3b; --c-good: #5aa832; --c-fair: #d9a400; --c-poor: #e06c00; --c-critical: #c62828; --c-dead: #555555; --c-unknown: #9aa39b;
  --radius: 8px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 15px; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121612; --surface: #1a201b; --surface-2: #222a23; --border: #2f3a30;
    --text: #e4ebe3; --muted: #9aaa9b; --accent: #6fbf73; --accent-ink: #0c1a0d; --accent-soft: #233626;
    --danger: #ef6f62; --warn: #e0a83a; --c-dead: #8a8a8a; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); line-height: 1.45; }
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 0 0 .5rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 1rem .75rem; display: flex; flex-direction: column; gap: .25rem; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; padding: .25rem .5rem 1rem; }
.brand svg { color: var(--accent); flex: none; }
.tenant-name { font-size: .8rem; color: var(--muted); font-weight: 500; display: block; }
.nav a { display: flex; align-items: center; gap: .6rem; padding: .5rem .65rem; border-radius: 6px; color: var(--text); text-decoration: none; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .badge-count { margin-left: auto; background: var(--danger); color: #fff; border-radius: 999px; font-size: .72rem; padding: 0 .45rem; }
.sidebar-foot { margin-top: auto; font-size: .85rem; padding: .5rem; border-top: 1px solid var(--border); }
.sidebar-foot select { width: 100%; margin: .35rem 0; }
main { padding: 1.5rem; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

/* components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }
.grid { display: grid; gap: 1rem; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.kpi .v { font-size: 1.9rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: .85rem; }
.kpi.alert .v { color: var(--danger); }
a.kpi { text-decoration: none; color: inherit; }
a.kpi:hover { border-color: var(--accent); }

button, .btn { font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: .45rem .85rem; border-radius: 6px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: .35rem; white-space: nowrap; }
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.toggled { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.link { border: none; background: none; padding: 0; color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: .45rem .6rem; min-width: 0; }
textarea { min-height: 80px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }
label.field > span { font-weight: 600; }
label.check { display: inline-flex; gap: .4rem; align-items: center; font-size: .9rem; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .75rem; }
.form-grid .wide { grid-column: 1 / -1; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .75rem; align-items: center; }
.filters input[type=search] { flex: 1 1 220px; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: .55rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; white-space: nowrap; position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent); }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.pager { display: flex; gap: .5rem; align-items: center; justify-content: flex-end; padding: .6rem; }

.badge { display: inline-flex; align-items: center; gap: .3rem; padding: .05rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--text); white-space: nowrap; }
.dot { width: .65rem; height: .65rem; border-radius: 50%; display: inline-block; flex: none; }
.badge.p-urgent { background: var(--danger); color: #fff; }
.badge.p-high { background: color-mix(in srgb, var(--warn) 25%, transparent); color: var(--warn); }
.badge.s-completed, .badge.s-converted { background: var(--accent-soft); color: var(--accent); }
.badge.s-cancelled, .badge.s-closed { color: var(--muted); }
.overdue { color: var(--danger); font-weight: 600; }

/* bars */
.bar-row { display: grid; grid-template-columns: minmax(110px, 38%) 1fr 3.5rem; gap: .6rem; align-items: center; font-size: .88rem; margin: .3rem 0; }
.bar { height: .7rem; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.bar > i.over { background: var(--warn); }
.stack { display: flex; height: 1.4rem; border-radius: 5px; overflow: hidden; gap: 2px; margin: .5rem 0 .75rem; }
.stack > i { display: block; height: 100%; min-width: 3px; }
.legend { display: flex; flex-wrap: wrap; gap: .4rem .9rem; font-size: .82rem; }
.legend span { display: inline-flex; gap: .35rem; align-items: center; }
.callout { border-left: 3px solid var(--warn); background: color-mix(in srgb, var(--warn) 10%, transparent); padding: .5rem .75rem; border-radius: 4px; font-size: .87rem; margin-top: .75rem; }
.activity { list-style: none; padding: 0; margin: 0; font-size: .87rem; }
.activity li { padding: .35rem 0; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; gap: 1rem; }
.activity li:last-child { border: none; }

/* map */
#map { height: calc(100vh - 170px); min-height: 420px; border-radius: var(--radius); border: 1px solid var(--border); }
.mini-map { height: 260px; border-radius: var(--radius); border: 1px solid var(--border); }
#map.adding { cursor: crosshair; }
.map-legend { background: var(--surface); color: var(--text); padding: .5rem .7rem; border-radius: 6px; box-shadow: var(--shadow); font-size: .8rem; line-height: 1.6; }
.leaflet-container { font: inherit; }
.leaflet-popup-content { margin: .7rem .9rem; }

/* detail */
.dl { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; font-size: .92rem; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; }
section.block { margin-top: 1.25rem; }

/* requests */
.req { display: grid; gap: .5rem; }
.req-head { display: flex; justify-content: space-between; gap: .5rem; flex-wrap: wrap; align-items: center; }
.req p { margin: 0; }
.nearest { font-size: .85rem; display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

/* modal + toast */
dialog { border: none; border-radius: 10px; padding: 0; width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 32px); background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.modal-head { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; }
.modal-body { padding: 1.25rem; overflow-y: auto; }
.modal-foot { padding: .85rem 1.25rem; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: .5rem; }
.error-msg { color: var(--danger); font-size: .88rem; margin: 0 0 .75rem; }
.toast { position: fixed; bottom: 1.25rem; right: 1.25rem; background: var(--text); color: var(--bg); padding: .65rem 1rem; border-radius: 6px; box-shadow: var(--shadow); z-index: 9999; max-width: 380px; }
.toast.err { background: var(--danger); color: #fff; }

/* login / public */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: .9rem; }
.login-card .brand { padding: 0; font-size: 1.2rem; }
.public-wrap { max-width: 760px; margin: 0 auto; padding: 1.5rem 16px 3rem; }

@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar, .nav { min-width: 0; }
  .grid.two { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: .5rem; border-right: none; border-bottom: 1px solid var(--border); }
  .brand { padding: .25rem .5rem; }
  .nav { display: flex; overflow-x: auto; width: 100%; gap: .25rem; }
  .nav a { white-space: nowrap; padding: .4rem .6rem; }
  .nav a svg { display: none; }
  .sidebar-foot { margin: 0; border: none; width: 100%; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
  .sidebar-foot select { width: auto; }
  main { padding: 1rem 16px; }
  #map { height: calc(100vh - 230px); }
  .bar-row { grid-template-columns: 1fr 1fr 3rem; }
}

.req-pin { width: 20px; height: 20px; border-radius: 4px; background: #7b3fe4; color: #fff; font-weight: 700; font-size: 13px; display: grid; place-items: center; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.tree-pin { width: 20px; height: 20px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.5); cursor: grab; }

@media print {
  .sidebar, .page-head .actions, .filters, .pager, .toast { display: none !important; }
  .app { display: block; }
  main { padding: 0; }
  .table-wrap { border: none; overflow: visible; }
  th { position: static; }
  select { border: none; appearance: none; padding: 0; }
}

/* --- field-operations components --- */
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.chip { display: inline-flex; align-items: center; gap: .35rem; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: .2rem .65rem; font-size: .85rem; }
.checks { display: flex; gap: 1.25rem; flex-wrap: wrap; }
.bulk-bar { display: flex; align-items: center; gap: .9rem; flex-wrap: wrap; background: var(--accent-soft); border: 1px solid var(--accent); border-radius: var(--radius); padding: .5rem .75rem; margin-bottom: .75rem; position: sticky; top: .5rem; z-index: 5; }
th.sel, td.sel { width: 2.2rem; text-align: center; }
td.sel input, th.sel input { width: 1.05rem; height: 1.05rem; cursor: pointer; }
tr.selected td { background: var(--accent-soft); }
tfoot th { background: none; border-top: 2px solid var(--border); }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .75rem; }
.photo { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.photo img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: var(--surface-2); }
.photo figcaption { padding: .4rem .55rem; font-size: .85rem; display: flex; flex-direction: column; gap: .1rem; }
.danger-link { color: var(--danger) !important; align-self: flex-start; }
fieldset.traq { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem .9rem; margin: 0; }
fieldset.traq legend { font-weight: 600; font-size: .9rem; padding: 0 .35rem; }
.traq-result { margin: .75rem 0 .25rem; font-size: .92rem; }
.map-btn { width: 34px; height: 34px; padding: 0; justify-content: center; font-size: 1.2rem; border: 2px solid rgba(0,0,0,.2); background: #fff; color: #333; border-radius: 4px; background-clip: padding-box; }
.map-btn:hover { background: #f4f4f4; }
.status-result { margin-top: 1rem; }
.status-steps { display: flex; gap: .4rem; flex-wrap: wrap; margin: .5rem 0; }
.status-steps span { padding: .2rem .6rem; border-radius: 999px; font-size: .82rem; background: var(--surface-2); color: var(--muted); }
.status-steps span.done { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
@media print { .bulk-bar, th.sel, td.sel { display: none !important; } }
