:root {
  --ink: #13202e;
  --ink-2: #2b3a4a;
  --muted: #5d6b79;
  --paper: #ffffff;
  --panel: #f6f8fa;
  --line: #d7dee5;
  --board: #cfd8e0;
  --accent: #2358c7;
  --accent-soft: #e6eefc;
  --danger: #c23b3b;
  --hit: #ffd21f;
  --radius: 6px;
  --font: "Archivo", "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--ink); background: var(--panel); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h3 { font-size: 15px; margin: 0 0 10px; }
h4 { font-size: 13px; margin: 16px 0 6px; color: var(--ink-2); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar {
  height: 48px; display: flex; align-items: center; gap: 24px; padding: 0 16px;
  background: var(--ink); color: #e9eef3;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 700; letter-spacing: .01em; }
.brand-mark {
  width: 20px; height: 20px; border: 2px solid #fff; border-radius: 2px; position: relative;
  background: linear-gradient(90deg, transparent 45%, #fff 45%, #fff 55%, transparent 55%);
}
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { color: #b9c5d1; text-decoration: none; padding: 6px 12px; border-radius: var(--radius); }
.topbar nav a:hover { color: #fff; background: rgba(255,255,255,.08); }
.topbar nav a.active { color: #fff; background: rgba(255,255,255,.14); }
.userbox { margin-left: auto; display: flex; align-items: center; gap: 12px; font-size: 13px; color: #b9c5d1; }
.userbox a, .userbox .link { color: #fff; }
.role { font-size: 11px; padding: 1px 6px; border-radius: 3px; background: rgba(255,255,255,.14); }
.link { background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; }

/* ---------- three-column workspace ---------- */
.layout { height: calc(100% - 48px); display: grid; grid-template-columns: 270px minmax(0, 1fr) 310px; }
.layout.no-right { grid-template-columns: 270px minmax(0, 1fr) 0; }
.layout.no-right .panel.right { display: none; }
.panel { background: var(--paper); overflow: auto; padding: 14px; }
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }

.canvas-wrap { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.toolbar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  background: var(--paper); border-bottom: 1px solid var(--line); min-height: 44px;
}
.toolbar .spacer { flex: 1; }
.floor-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#zoomLabel { min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.save-status { font-size: 12px; color: var(--muted); margin-right: 8px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 30px; padding: 0 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--paper); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #b7c3cf; background: var(--panel); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1b47a5; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fcecec; border-color: #eab3b3; }
.btn.sm { height: 26px; padding: 0 8px; font-size: 12px; }
.btn.icon { width: 30px; padding: 0; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.field { display: block; margin-bottom: 10px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
input[type=text], input[type=search], input[type=password], input[type=number], input[type=email],
input:not([type]), select, textarea {
  width: 100%; height: 32px; padding: 0 9px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff;
}
textarea { height: auto; padding: 7px 9px; resize: vertical; }
input[type=color] { width: 44px; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0 12px; }
.row { display: flex; gap: 6px; align-items: center; }
.row > .grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0 6px; }
.divider { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }

/* ---------- floor list, palette, legend ---------- */
.floor-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 14px; }
.floor-btn {
  text-align: left; padding: 7px 10px; border: 0; border-radius: var(--radius); background: none; cursor: pointer;
  border-left: 3px solid transparent;
}
.floor-btn:hover { background: var(--panel); }
.floor-btn.active { background: var(--accent-soft); border-left-color: var(--accent); font-weight: 600; }

.palette { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.palette-item {
  display: flex; align-items: center; gap: 8px; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff; cursor: pointer; text-align: left; min-width: 0;
}
.palette-item span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-item:hover { border-color: #b7c3cf; }
.palette-item.active { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }

.legend { display: flex; flex-direction: column; gap: 2px; }
.legend-item {
  display: flex; align-items: center; gap: 8px; padding: 5px 6px; border: 0; background: none; border-radius: var(--radius);
  cursor: pointer; text-align: left;
}
.legend-item:hover { background: var(--panel); }
.legend-item .count { margin-left: auto; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.legend-item.off { opacity: .4; }
.legend-item.off .swatch { filter: grayscale(1); }

.swatch {
  display: inline-block; width: 14px; height: 14px; flex: none;
  background-size: contain; background-repeat: no-repeat; background-position: center;
}

/* shapes (shared by swatches and map objects) */
.shape-square { border-radius: 2px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.28); }
.shape-circle { border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0,0,0,.28); }
.shape-triangle { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.shape-diamond { clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); }
.shape-icon { background-size: contain; background-repeat: no-repeat; background-position: center; }

/* ---------- search ---------- */
.search-box { position: relative; margin-bottom: 6px; }
.search-box input { height: 36px; padding-left: 32px; }
.search-box::before {
  content: ""; position: absolute; left: 11px; top: calc(50% - 7px); width: 10px; height: 10px;
  border: 2px solid var(--muted); border-radius: 50%;
}
.search-box::after {
  content: ""; position: absolute; left: 21px; top: calc(50% + 5px); width: 6px; height: 2px; background: var(--muted); transform: rotate(45deg);
}
.results { display: flex; flex-direction: column; gap: 4px; margin: 6px 0 12px; }
.result {
  display: block; width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff; cursor: pointer; text-align: left;
}
.result:hover { border-color: var(--accent); }
.result.here { border-left: 3px solid var(--hit); }
.result b { display: block; }
.result .where { font-size: 11px; color: var(--muted); }
.result .who { font-size: 12px; color: var(--ink-2); }

/* ---------- canvas ---------- */
.viewport {
  flex: 1; min-height: 0; overflow: auto; display: flex; position: relative; cursor: grab;
  background-color: var(--board);
  background-image:
    linear-gradient(rgba(19,32,46,.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(19,32,46,.06) 1px, transparent 1px);
  background-size: 24px 24px;
  user-select: none; -webkit-user-select: none; touch-action: none;
}
.viewport.panning { cursor: grabbing; }
.viewport.placing, .viewport.placing .fp-obj { cursor: crosshair; }
.fp-sizer { margin: auto; flex: none; position: relative; }
.fp-stage {
  position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff;
  box-shadow: 0 1px 3px rgba(19,32,46,.25), 0 8px 30px rgba(19,32,46,.12);
  --inv: 1;
}
.fp-stage.no-image {
  background-color: #fff;
  background-image:
    linear-gradient(#e8edf2 1px, transparent 1px),
    linear-gradient(90deg, #e8edf2 1px, transparent 1px);
  background-size: 50px 50px;
}
.fp-bg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.fp-shade {
  position: absolute; inset: 0; background: rgba(12,20,30,.62); opacity: 0; pointer-events: none;
  transition: opacity .2s;
}
.fp-stage.searching .fp-shade { opacity: 1; }
.fp-layer { position: absolute; inset: 0; }
.fp-empty { margin: auto; color: var(--ink-2); background: rgba(255,255,255,.8); padding: 14px 18px; border-radius: var(--radius); max-width: 360px; text-align: center; }

.fp-obj { position: absolute; cursor: pointer; transition: opacity .15s, filter .15s; }
.fp-shape { position: absolute; inset: 0; }
.fp-label {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-weight: 600; color: #0d1722; white-space: nowrap; line-height: 1; pointer-events: none;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff, 0 0 3px #fff;
}
.fp-obj.selected { outline: calc(2px * var(--inv)) solid var(--accent); outline-offset: calc(2px * var(--inv)); }
.fp-handle {
  display: none; position: absolute; right: calc(-6px * var(--inv)); bottom: calc(-6px * var(--inv));
  width: calc(11px * var(--inv)); height: calc(11px * var(--inv));
  background: #fff; border: calc(2px * var(--inv)) solid var(--accent); cursor: nwse-resize;
}
.fp-obj.selected .fp-handle { display: block; }
.fp-obj.type-hidden { display: none; }
.fp-obj.dim { opacity: .14; filter: grayscale(1); }
.fp-obj.hit {
  filter: drop-shadow(0 0 calc(3px * var(--inv)) var(--hit)) drop-shadow(0 0 calc(7px * var(--inv)) var(--hit));
}
.fp-obj.hit .fp-label { color: #0d1722; text-shadow: 0 0 3px var(--hit), 0 0 3px var(--hit), 0 0 4px #fff; }
.fp-obj.flash { animation: flash 1.6s ease-out 1; }
@keyframes flash {
  0%, 40%, 80% { outline: calc(4px * var(--inv)) solid var(--hit); outline-offset: calc(6px * var(--inv)); }
  20%, 60%, 100% { outline: calc(4px * var(--inv)) solid transparent; outline-offset: calc(2px * var(--inv)); }
}
@media (prefers-reduced-motion: reduce) {
  .fp-obj.flash { animation: none; outline: 3px solid var(--hit); }
  .fp-shade, .fp-obj { transition: none; }
}

/* hover card + object detail */
.fp-tip {
  position: fixed; z-index: 1000; display: none; pointer-events: none; max-width: 300px;
  background: var(--ink); color: #eef2f6; padding: 9px 11px; border-radius: var(--radius); font-size: 12.5px;
  box-shadow: 0 6px 24px rgba(0,0,0,.25);
}
.od-title { font-weight: 700; font-size: 14px; }
.od-type { display: flex; align-items: center; gap: 6px; font-size: 12px; opacity: .75; margin-bottom: 4px; }
.od-sec { margin-top: 7px; padding-top: 7px; border-top: 1px solid rgba(127,140,155,.3); }
.od-h { font-size: 11px; opacity: .7; margin-bottom: 2px; }
.od-row { padding: 1px 0; }
.od-muted { opacity: .65; }
.od-notes { white-space: pre-wrap; }
.info-card .od-title { font-size: 17px; }

/* ---------- property panel ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 4px 3px 9px; border-radius: 999px;
  background: var(--accent-soft); font-size: 12.5px;
}
.chip small { color: var(--muted); }
.chip-x { border: 0; background: none; cursor: pointer; width: 18px; height: 18px; border-radius: 50%; line-height: 1; color: var(--muted); }
.chip-x:hover { background: rgba(0,0,0,.08); color: var(--danger); }
.kbd-list { font-size: 12px; color: var(--muted); display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin-top: 10px; }
kbd { font: 11px/1 var(--font); padding: 3px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: #fff; color: var(--ink); white-space: nowrap; }

.picker { position: relative; }
.picker-list {
  display: none; position: absolute; z-index: 50; left: 0; right: 0; top: calc(100% + 2px); max-height: 240px; overflow: auto;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 6px 20px rgba(19,32,46,.14);
}
.picker-item { padding: 6px 9px; cursor: pointer; font-size: 13px; }
.picker-item.active, .picker-item:hover { background: var(--accent-soft); }
.picker-item.create { color: var(--accent); border-top: 1px solid var(--line); }

/* ---------- admin ---------- */
.admin-wrap { max-width: 1200px; margin: 0 auto; padding: 20px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tab { padding: 9px 14px; border: 0; background: none; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; color: var(--muted); }
.tab.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.admin-grid { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 18px; align-items: start; }
.card { background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 16px; }
.table-head { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.table-head input { max-width: 280px; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 7px 8px; border-bottom: 1px solid #edf1f4; vertical-align: middle; }
tr.editing td { background: var(--accent-soft); }
td.actions { white-space: nowrap; text-align: right; }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100%; background: var(--board); }
.login-card { width: 340px; background: var(--paper); border-radius: 8px; padding: 26px; box-shadow: 0 8px 30px rgba(19,32,46,.15); }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p { margin: 0 0 18px; color: var(--muted); }
.error-msg { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 2000; }
.toast { background: var(--ink); color: #fff; padding: 9px 14px; border-radius: var(--radius); font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 380px; }
.toast.error { background: var(--danger); }

@media (max-width: 900px) {
  .layout, .layout.no-right { grid-template-columns: 1fr; grid-template-rows: auto minmax(360px, 1fr) auto; height: auto; min-height: calc(100% - 48px); }
  .panel.left, .panel.right { border: 0; border-bottom: 1px solid var(--line); }
  .admin-grid { grid-template-columns: 1fr; }
  .topbar { gap: 10px; }
}
