:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #dde1e6;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.18);
  --filled: rgba(22, 163, 74, 0.22);
  --filled-edge: #16a34a;
  --empty: rgba(148, 163, 184, 0.18);
  --empty-edge: #94a3b8;
  --match: rgba(234, 179, 8, 0.45);
  --danger: #dc2626;
  --grid: rgba(100, 116, 139, 0.18);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317; --surface: #1b1e24; --text: #e8eaed; --muted: #9aa1ab;
    --border: #2c313a; --accent: #5b8def; --accent-soft: rgba(91, 141, 239, 0.25);
    --grid: rgba(148, 163, 184, 0.12);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}
[hidden] { display: none !important; }
h1 { font-size: 1.4rem; margin: 0 0 .4rem; }
h2 { font-size: 1.2rem; margin: 0; }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.msg { min-height: 1.2em; font-size: .9rem; color: var(--muted); }
.msg.error { color: var(--danger); }

button, input, textarea {
  font: inherit; color: inherit;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); padding: .5rem .8rem;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Screens */
.screen { min-height: 100%; }
.center { display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.6rem; width: 100%; max-width: 380px; display: grid; gap: .6rem;
}

/* Top bar */
.head { position: sticky; top: 0; z-index: 5; }
.topbar {
  display: flex; align-items: center; gap: .8rem; flex-wrap: wrap;
  padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { white-space: nowrap; }
#search { flex: 1 1 220px; min-width: 0; }
.topbar-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.live { font-size: .8rem; color: var(--muted); }
.live.on { color: var(--filled-edge); }

/* Floor tabs */
.tabs {
  display: flex; gap: 4px; padding: 0 16px; overflow-x: auto;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.tab {
  border: none; border-bottom: 3px solid transparent; border-radius: 0;
  background: none; padding: .6rem .9rem; white-space: nowrap; color: var(--muted); font-weight: 600;
}
.tab:hover { color: var(--text); border-bottom-color: var(--border); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tab .count {
  display: inline-block; margin-left: .35rem; padding: 0 .45rem; border-radius: 999px;
  font-size: .75rem; background: var(--bg); color: var(--muted);
}
.tab .count.hit { background: var(--match); color: var(--text); }

/* Map + panel */
.layout { display: flex; gap: 16px; padding: 16px; align-items: flex-start; }
.map-wrap { flex: 1; min-width: 0; }
.map {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; max-width: 900px; margin: 0 auto;
}
.canvas { position: relative; width: 100%; aspect-ratio: 1545 / 1999; user-select: none; -webkit-user-select: none; }
.canvas img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
.canvas.blank {
  background-color: var(--surface);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 2.5% 2.5%;
}
.canvas.adding { cursor: crosshair; }
.blank-note {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  max-width: 70%; text-align: center; color: var(--muted); background: var(--surface);
  border: 1px dashed var(--border); border-radius: 8px; padding: .8rem 1rem; z-index: 1;
}
#hotspots { position: absolute; inset: 0; }
.spot {
  position: absolute; padding: 0; border-radius: 6px;
  border: 2px solid var(--empty-edge); background: var(--empty);
  font-size: clamp(8px, 1.1vw, 12px); font-weight: 600; color: var(--text);
  display: flex; align-items: flex-end; justify-content: center;
  transition: background .15s, transform .15s, box-shadow .15s;
}
.spot.filled { border-color: var(--filled-edge); background: var(--filled); }
.spot:hover { transform: scale(1.04); box-shadow: 0 2px 10px rgba(0,0,0,.2); }
.spot.selected { border-color: var(--accent); background: var(--accent-soft); }
.spot.match { background: var(--match); border-color: #ca8a04; }
.spot.dim { opacity: .35; }
.spot.flash { animation: flash 1.2s ease; }
@keyframes flash { 0%,100% { box-shadow: none; } 30% { box-shadow: 0 0 0 6px var(--accent-soft); } }
.spot-label { background: var(--surface); border-radius: 4px; padding: 0 3px; margin-bottom: 2px; pointer-events: none; white-space: nowrap; }
.spot.tv .spot-label { font-size: .85em; }

/* Layout mode */
.spot.editable { cursor: move; touch-action: none; border-style: dashed; }
.spot.editable:hover { transform: none; }
.spot.dragging { opacity: .85; box-shadow: 0 6px 18px rgba(0,0,0,.3); z-index: 3; transition: none; }
.spot.draft { border: 2px dashed var(--accent); background: var(--accent-soft); }
.handle {
  position: absolute; right: -6px; bottom: -6px; width: 14px; height: 14px;
  background: var(--accent); border: 2px solid var(--surface); border-radius: 3px;
  cursor: nwse-resize; touch-action: none;
}
.band { position: absolute; border: 2px dashed var(--accent); background: var(--accent-soft); border-radius: 6px; pointer-events: none; }
.maptools { max-width: 900px; margin: 0 auto .6rem; }
.view-tools { display: flex; justify-content: flex-end; }
.layout-tools {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem; flex-wrap: wrap;
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: var(--radius); padding: .5rem .7rem;
}
.layout-tools .hint { font-size: .9rem; }
.tool-buttons { display: flex; gap: .5rem; flex-wrap: wrap; }
button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { border-color: var(--danger); color: var(--danger); }
button.danger:hover { background: var(--danger); color: #fff; }
button:disabled { opacity: .5; cursor: default; }
.tab.add { color: var(--accent); margin-left: auto; }

/* Forms in the panel and dialogs */
.form { display: grid; gap: .35rem; }
.form label, .dialog label:not(.choice):not(.file-btn), .label { font-size: .85rem; color: var(--muted); margin-top: .4rem; display: block; }
.form input, .form select, .dialog input:not([type=radio]):not([type=file]), .dialog select { width: 100%; padding: .45rem .6rem; }
select { font: inherit; color: inherit; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); padding: .45rem .6rem; }

.dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(460px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.dialog::backdrop { background: rgba(0,0,0,.45); }
.dialog form, .dialog .dialog-body { padding: 1.2rem 1.3rem; display: grid; gap: .4rem; }
.dialog h2 { margin-bottom: .4rem; }
.dialog fieldset { border: 1px solid var(--border); border-radius: 10px; padding: .6rem .8rem; margin: .6rem 0 0; display: grid; gap: .4rem; }
.dialog legend { font-size: .85rem; color: var(--muted); padding: 0 .3rem; }
.choice { display: flex; align-items: center; gap: .5rem; cursor: pointer; }
.sub-row { padding-left: 1.6rem; display: grid; gap: .3rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .8rem; }
.fd-section { border-top: 1px solid var(--border); padding-top: .7rem; margin-top: .4rem; display: grid; gap: .4rem; justify-items: start; }
.inline { display: flex; gap: .5rem; width: 100%; flex-wrap: wrap; }
.inline input { flex: 1 1 160px; }
.file-btn {
  display: inline-block; cursor: pointer; border: 1px solid var(--accent); color: var(--accent);
  border-radius: 8px; padding: .5rem .8rem;
}
.file-btn:hover { background: var(--accent); color: #fff; }
.prev-row { display: flex; align-items: center; justify-content: space-between; gap: .6rem; width: 100%; padding: .3rem 0; border-bottom: 1px dashed var(--border); }
#fd-previous { width: 100%; }
.danger-zone .label { color: var(--danger); }

.panel {
  width: 380px; flex-shrink: 0; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem; position: sticky; top: 112px;
  max-height: calc(100vh - 130px); overflow: auto;
}
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .5rem; }
#panel-close { border: none; background: none; font-size: 1.1rem; }
.section { margin-top: 1rem; }
.section h3 { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 .35rem; }
.row { display: grid; grid-template-columns: 110px 1fr; gap: .5rem; padding: .25rem 0; border-bottom: 1px dashed var(--border); }
.row .k { color: var(--muted); font-size: .88rem; }
.row .v { word-break: break-word; white-space: pre-wrap; }
.row .v.empty { color: var(--muted); }
.row input, .row textarea { width: 100%; padding: .35rem .5rem; }
.row textarea { min-height: 70px; resize: vertical; }
.panel-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: 1rem; }
.notice { background: #fef3c7; color: #78350f; padding: .5rem .7rem; border-radius: 8px; font-size: .9rem; }
.hist-item { border-top: 1px solid var(--border); padding: .5rem 0; font-size: .88rem; }
.hist-item .chg { color: var(--muted); }

/* Phone: panel becomes a bottom sheet */
@media (max-width: 820px) {
  .layout { flex-direction: column; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; width: auto;
    max-height: 75vh; border-radius: 14px 14px 0 0; z-index: 10;
    box-shadow: 0 -6px 24px rgba(0,0,0,.25);
  }
  .row { grid-template-columns: 95px 1fr; }
  .head { position: static; }
}

/* Shortcuts */
button.ghost { border-color: transparent; background: none; color: var(--muted); }
button.ghost:hover { color: var(--text); border-color: var(--border); }
.view-tools { gap: .5rem; }
.keys { width: 100%; border-collapse: collapse; font-size: .9rem; }
.keys td { padding: .35rem .3rem; border-bottom: 1px dashed var(--border); vertical-align: top; }
.keys td:first-child { white-space: nowrap; width: 1%; padding-right: 1rem; }
.keys-head { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: .8rem 0 .2rem; }
kbd {
  display: inline-block; min-width: 1.6em; text-align: center; font: 600 .78rem/1.5 ui-monospace, monospace;
  padding: 0 .35em; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 5px; background: var(--bg);
}
#keys-dialog { width: min(560px, calc(100vw - 32px)); }

/* Item types */
.add-group { display: inline-flex; }
.add-group button { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.add-group select { border-top-left-radius: 0; border-bottom-left-radius: 0; border-left: none; padding: .5rem .5rem; }
.spot { flex-direction: column; align-items: center; justify-content: flex-end; }
.spot-tag {
  font-size: .78em; font-weight: 700; letter-spacing: .04em; line-height: 1.2;
  padding: 0 4px; border-radius: 4px; color: #fff; background: var(--tag, #64748b); margin-bottom: 1px; pointer-events: none;
}
.spot.k-server  { --tag: #7c3aed; border-color: #7c3aed; }
.spot.k-switch  { --tag: #0891b2; border-color: #0891b2; }
.spot.k-router  { --tag: #ea580c; border-color: #ea580c; }
.spot.k-printer { --tag: #4b5563; border-color: #4b5563; }
.spot.k-ap      { --tag: #db2777; border-color: #db2777; }
.spot.k-tv      { --tag: #475569; }
.spot.filled.k-server, .spot.filled.k-switch, .spot.filled.k-router, .spot.filled.k-printer, .spot.filled.k-ap { border-width: 3px; }
.spot.selected { border-color: var(--accent); }

/* Labels: just text on the map */
.spot.label-spot { background: none; border-color: transparent; justify-content: center; }
.spot.label-spot:hover { transform: none; box-shadow: none; background: var(--accent-soft); }
.spot.label-spot.editable { border: 1px dashed var(--muted); }
.spot.label-spot.selected { border: 2px solid var(--accent); background: var(--accent-soft); }
.label-text {
  font-weight: 700; font-size: clamp(9px, 1.15vw, 14px); color: var(--text); text-transform: uppercase; letter-spacing: .03em;
  background: color-mix(in srgb, var(--surface) 80%, transparent); padding: 0 4px; border-radius: 4px;
  white-space: nowrap; pointer-events: none; /* long labels may run past their box rather than get cut off */
}
.spot.label-spot { overflow: visible; }

/* Legend */
.legend { max-width: 900px; margin: .5rem auto 0; display: flex; flex-wrap: wrap; gap: .4rem .9rem; font-size: .85rem; }
.legend-item { display: inline-flex; align-items: center; gap: .35rem; }
.legend-item.k-server  { --tag: #7c3aed; }
.legend-item.k-switch  { --tag: #0891b2; }
.legend-item.k-router  { --tag: #ea580c; }
.legend-item.k-printer { --tag: #4b5563; }
.legend-item.k-ap      { --tag: #db2777; }
.legend-item.k-tv      { --tag: #475569; }
