:root {
  --ink: #18201c;
  --muted: #68716b;
  --paper: #f4f5f1;
  --card: #fff;
  --line: #dfe3dd;
  --green: #246047;
  --green-dark: #194b38;
  --green-soft: #deece3;
  --warning: #a65d19;
  --danger: #a83832;
  --shadow: 0 18px 48px rgba(22, 35, 28, .08);
  font-family: Inter, "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; min-height: 100vh; background: var(--paper); }
button, input, select { font: inherit; }
button, select { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); line-height: 1.6; }
.eyebrow { margin: 0 0 8px; color: var(--green); font-size: 11px; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }

.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-size: 15px; font-weight: 760; }
.brand-mark { position: relative; display: inline-block; box-sizing: border-box; width: 28px; height: 28px; border: 2px solid var(--green); border-radius: 50%; }
.brand-mark::before, .brand-mark::after { content: ""; position: absolute; top: 50%; left: 50%; border-radius: 50%; transform: translate(-50%, -50%); }
.brand-mark::before { box-sizing: border-box; width: 48%; height: 48%; border: 2px solid var(--green); }
.brand-mark::after { width: 4px; height: 4px; background: #d98542; }
.brand-mark i { display: none; }
.brand-mark.large { width: 40px; height: 40px; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--paper); }
.auth-form { width: min(360px, 100%); display: grid; gap: 19px; padding: 38px; background: var(--card); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); }
.auth-form h1 { margin: 4px 0 10px; font-size: 30px; line-height: 1.2; font-weight: 720; letter-spacing: -.035em; }
label { display: grid; gap: 7px; color: #4d5650; font-size: 13px; font-weight: 650; }
input, select { width: 100%; min-height: 46px; padding: 0 13px; color: var(--ink); background: #fff; border: 1px solid #ccd2cb; border-radius: 10px; outline: none; }
input:focus, select:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(36, 96, 71, .12); }
button { border: 0; border-radius: 10px; }
.primary { min-height: 47px; padding: 0 18px; color: #fff; background: var(--green); font-weight: 750; }
.primary:hover { background: var(--green-dark); }
.secondary { min-height: 44px; padding: 0 16px; color: #fff; background: var(--ink); font-weight: 700; }
.quiet { min-height: 36px; padding: 0 12px; color: var(--muted); background: transparent; border: 1px solid var(--line); }
.quiet:hover { color: var(--ink); background: #fff; }
.danger { min-height: 34px; padding: 0 10px; color: var(--danger); background: #fff4f2; border: 1px solid #f0d1cd; }
.form-error { min-height: 18px; margin: -6px 0 0; color: var(--danger); font-size: 13px; }

.topbar { position: sticky; z-index: 20; top: 0; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 68px; padding: 10px clamp(18px, 4vw, 60px); background: rgba(244, 245, 241, .92); border-bottom: 1px solid var(--line); backdrop-filter: blur(16px); }
.main-nav { display: flex; gap: 4px; padding: 4px; background: #e7eae5; border-radius: 11px; }
.nav-button { min-width: 72px; min-height: 34px; padding: 0 14px; color: var(--muted); background: transparent; font-size: 13px; font-weight: 720; }
.nav-button.active { color: var(--ink); background: #fff; box-shadow: 0 1px 5px rgba(24, 32, 28, .1); }
.header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; }
.device-picker { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.device-picker select { width: auto; min-width: 140px; min-height: 37px; padding-right: 30px; background: var(--card); }
.live-indicator { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.live-indicator i { width: 7px; height: 7px; background: #d09a57; border-radius: 50%; }
.live-indicator.live i { background: #3d9c6e; box-shadow: 0 0 0 4px rgba(61, 156, 110, .12); }

.dashboard { width: min(1240px, calc(100% - 40px)); margin: 34px auto 70px; }
.summary-card { display: grid; grid-template-columns: minmax(310px, .88fr) minmax(460px, 1.12fr); min-height: 470px; overflow: hidden; background: var(--card); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); }
.summary-main { display: flex; flex-direction: column; justify-content: space-between; padding: clamp(28px, 5vw, 56px); }
.summary-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; }
.place-context { margin: 26px 0 4px; color: var(--muted); font-size: 14px; font-weight: 700; letter-spacing: .02em; }
.summary-heading h1 { margin: 0; font-size: clamp(42px, 5vw, 66px); line-height: 1.08; font-weight: 680; letter-spacing: -.055em; }
.administrative-area { max-width: 470px; margin: 14px 0 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.administrative-area:empty { display: none; }
.detail-badge { flex: none; padding: 7px 11px; color: var(--green); background: var(--green-soft); border-radius: 999px; font-size: 11px; font-weight: 800; }
.motion-line { max-width: 430px; color: var(--muted); font-size: 15px; line-height: 1.65; }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; padding-top: 26px; border-top: 1px solid var(--line); }
.facts div { display: grid; gap: 6px; }
.facts span, .mini-card p { margin: 0; color: var(--muted); font-size: 11px; }
.facts strong { font-size: 14px; font-weight: 700; }

.map-wrap { position: relative; z-index: 0; isolation: isolate; min-height: 430px; overflow: hidden; background: #e5e8df; }
.map { width: 100%; height: 100%; min-height: 470px; border: 0; }
.amap-frame { display: block; background: #edf0ea; }
.map-empty { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 20px; color: #6c786f; background: radial-gradient(circle at 50% 45%, #f2eee1 0 7%, transparent 7.5%), repeating-radial-gradient(circle at 50% 45%, transparent 0 58px, rgba(44, 82, 63, .06) 59px 60px), #e8ebe4; }
.map-pulse { width: 16px; height: 16px; background: var(--green); border: 4px solid #fff; border-radius: 50%; box-shadow: 0 0 0 14px rgba(36, 93, 72, .13); }
.accuracy-chip { position: absolute; z-index: 900; right: 14px; bottom: 34px; padding: 8px 11px; color: #fff; background: rgba(25, 32, 28, .82); border-radius: 8px; font-size: 11px; backdrop-filter: blur(6px); pointer-events: none; }
.map-recenter { position: absolute; z-index: 900; top: 14px; right: 14px; display: inline-flex; align-items: center; gap: 7px; min-height: 38px; padding: 0 12px; color: var(--ink); background: rgba(255, 255, 255, .94); border: 1px solid rgba(23, 43, 33, .2); border-radius: 9px; box-shadow: 0 2px 10px rgba(25, 40, 32, .14); font-size: 12px; font-weight: 720; cursor: pointer; backdrop-filter: blur(6px); }
.map-recenter:hover { background: #fff; }
.map-recenter span:first-child { color: var(--green); font-size: 19px; line-height: 1; }
.osm-map { position: relative; overflow: hidden; background: #dce2da; }
.map-wrap, .map-wrap *, .leaflet-container { -webkit-user-select: none; user-select: none; }
.leaflet-container { font-family: inherit; touch-action: none; cursor: grab; }
.leaflet-container.leaflet-dragging, .leaflet-dragging .leaflet-grab { cursor: grabbing; }
.leaflet-tile { width: 257px !important; height: 257px !important; }
.leaflet-control-zoom a { color: var(--ink); }
.leaflet-control-attribution { font-size: 10px; }

.status-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 14px; }
.mini-card { display: flex; align-items: center; gap: 14px; padding: 18px 20px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; }
.mini-icon { display: grid; place-items: center; width: 34px; height: 34px; color: var(--green); background: var(--green-soft); border-radius: 10px; font-weight: 800; }
.mini-card div { display: grid; gap: 5px; }
.mini-card strong { font-size: 13px; }

.admin-page { display: grid; grid-template-columns: 220px minmax(0, 1fr); align-items: start; gap: 28px; }
.admin-nav { position: sticky; top: 98px; display: grid; gap: 7px; }
.admin-nav h1 { margin: 0 0 20px; font-size: 34px; letter-spacing: -.045em; }
.admin-tab { display: flex; align-items: center; justify-content: space-between; min-height: 46px; padding: 0 13px; color: var(--muted); background: transparent; text-align: left; font-weight: 700; }
.admin-tab:hover { color: var(--ink); background: rgba(255,255,255,.7); }
.admin-tab.active { color: var(--green-dark); background: var(--green-soft); }
.admin-tab small { display: grid; place-items: center; min-width: 24px; height: 24px; padding: 0 6px; background: rgba(255,255,255,.7); border-radius: 99px; font-size: 11px; }
.refresh-admin { justify-self: start; margin-top: 16px; }
.admin-content { min-width: 0; }
.admin-panel { min-height: 480px; padding: clamp(24px, 4vw, 42px); background: var(--card); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; padding-bottom: 24px; border-bottom: 1px solid var(--line); }
.panel-heading h2 { margin: 0; font-size: 30px; letter-spacing: -.035em; }
.panel-heading p { margin: 7px 0 0; color: var(--muted); font-size: 13px; }
.create-drawer { margin: 20px 0 8px; border: 1px solid var(--line); border-radius: 12px; }
.create-drawer summary { padding: 15px 17px; color: var(--green); cursor: pointer; font-size: 13px; font-weight: 760; list-style: none; }
.create-drawer summary::-webkit-details-marker { display: none; }
.create-drawer[open] summary { border-bottom: 1px solid var(--line); }
.compact-form { display: grid; grid-template-columns: minmax(220px, 1fr) auto; align-items: end; gap: 14px; padding: 18px; }
.compact-form.two-col { grid-template-columns: repeat(2, minmax(180px, 1fr)); }
.compact-form.two-col button { grid-column: 1 / -1; justify-self: start; }
.device-grants { grid-column: 1 / -1; margin: 0; padding: 13px; border: 1px solid var(--line); border-radius: 10px; }
.device-grants legend { padding: 0 6px; color: var(--muted); font-size: 12px; }
.device-grants div { display: flex; flex-wrap: wrap; gap: 12px; }
.device-grants label { display: flex; align-items: center; gap: 6px; font-weight: 500; }
.device-grants input { width: 16px; min-height: 16px; }
.item-list { display: grid; }
.management-list { margin-top: 18px; }
.list-item { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 17px 2px; border-top: 1px solid #e9eae6; }
.list-item:first-child { border-top: 0; }
.list-primary { display: grid; gap: 4px; min-width: 0; }
.list-primary strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; }
.list-primary span, .hint { color: var(--muted); font-size: 12px; line-height: 1.5; }
.list-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.hint { margin: 10px 0; }

dialog { width: min(560px, calc(100% - 28px)); max-height: calc(100vh - 36px); overflow: auto; padding: 28px; color: var(--ink); background: var(--card); border: 0; border-radius: 18px; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(18, 26, 21, .45); backdrop-filter: blur(4px); }
dialog h2 { margin: 0 0 20px; font-size: 28px; letter-spacing: -.035em; }
dialog pre { overflow: auto; margin: 20px 0; padding: 14px; white-space: pre-wrap; word-break: break-all; background: #eceee8; border-radius: 9px; user-select: all; }
.dialog-close { float: right; width: 32px; height: 32px; color: var(--muted); background: transparent; font-size: 24px; }
.dialog-form { display: grid; gap: 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.dialog-actions .primary { min-width: 120px; }
.map-picker-button { justify-self: start !important; }
.map-dialog { width: min(900px, calc(100% - 28px)); }
.map-dialog-heading h2 { margin-bottom: 6px; }
.map-dialog-heading p { margin: 0 44px 18px 0; color: var(--muted); font-size: 13px; }
.zone-picker-host { width: 100%; height: min(56vh, 520px); min-height: 340px; overflow: hidden; background: #dce2da; border: 1px solid var(--line); border-radius: 13px; }
.zone-picker-map { width: 100%; height: 100%; min-height: 340px; overflow: hidden; background: #dce2da; border: 0; }
.zone-picker-controls { display: grid; grid-template-columns: minmax(220px, 1fr) 120px auto; align-items: end; gap: 14px; margin: 18px 0; }
.zone-picker-controls input[type="range"] { min-height: 40px; padding: 0; accent-color: var(--green); border: 0; box-shadow: none; }
.coordinate-readout { margin: 0 0 13px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 11px; white-space: nowrap; }
.toast { position: fixed; z-index: 40; left: 50%; bottom: 26px; transform: translate(-50%, 20px); padding: 11px 17px; color: #fff; background: var(--ink); border-radius: 9px; opacity: 0; pointer-events: none; transition: .2s ease; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (max-width: 980px) {
  .topbar { grid-template-columns: auto 1fr auto; }
  .summary-card { grid-template-columns: 1fr; }
  .map-wrap, .map { min-height: 410px; }
  .admin-page { grid-template-columns: 1fr; }
  .admin-nav { position: static; display: flex; flex-wrap: wrap; align-items: center; }
  .admin-nav > div { width: 100%; }
  .admin-nav h1 { margin-bottom: 8px; }
  .admin-tab { flex: 1; min-width: 132px; }
  .refresh-admin { margin: 0; }
}

@media (max-width: 700px) {
  .topbar { grid-template-columns: auto 1fr; gap: 10px; }
  .main-nav { justify-self: end; }
  .header-actions { grid-column: 1 / -1; justify-content: space-between; padding-top: 9px; border-top: 1px solid var(--line); }
  .brand.compact span:last-child, .live-indicator { display: none; }
  .dashboard { width: min(100% - 20px, 1240px); margin-top: 14px; }
  .summary-card { border-radius: 15px; }
  .facts, .status-grid { grid-template-columns: 1fr; }
  .compact-form, .compact-form.two-col { grid-template-columns: 1fr; }
  .admin-panel { padding: 20px; }
  .admin-tab { min-width: calc(50% - 4px); }
  .list-item { align-items: flex-start; flex-direction: column; }
  .list-actions { flex-wrap: wrap; }
  .zone-picker-controls { grid-template-columns: 1fr 100px; }
  .coordinate-readout { grid-column: 1 / -1; margin-bottom: 0; }
  .map-dialog .dialog-actions { flex-wrap: wrap; }
  .map-dialog .dialog-actions button { flex: 1; min-width: 130px; }
}

@media (max-width: 430px) {
  .auth-form { padding: 28px 22px; }
  .device-picker > span { display: none; }
  .device-picker select { min-width: 128px; max-width: 55vw; }
  .nav-button { min-width: 62px; }
  .map-wrap, .map { min-height: 360px; }
}
