:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #1b2230;
  --text-2: #5b6474;
  --text-3: #8a93a3;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef0ff;
  --danger: #dc2626;
  --danger-soft: #fdeceb;
  --success: #16a34a;
  --success-soft: #e8f6ed;
  --warn-soft: #fff6e0;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .14), 0 2px 6px rgba(16, 24, 40, .06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 14px; color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .input:focus, .select:focus { outline: none; box-shadow: 0 0 0 3px rgba(79, 70, 229, .22); border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn.icon { width: 34px; padding: 0; }
.btn.sm.icon { width: 28px; }
.btn svg { width: 16px; height: 16px; flex: none; }

.input, .select {
  width: 100%; height: 38px; padding: 0 12px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
}
.select { padding-right: 28px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-weight: 500; font-size: 13px; }
.field .hint { font-size: 12px; color: var(--text-3); }
.input-row { display: flex; gap: 8px; }
.error-text { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- top bar ---------- */
.topbar {
  height: 56px; display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: relative; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; color: var(--text); }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 18px; height: 18px; }
.nav { display: flex; gap: 4px; margin-left: 12px; }
.nav a { padding: 7px 12px; border-radius: 8px; color: var(--text-2); font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.spacer { flex: 1; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 600; font-size: 13px; flex: none;
}
.user-btn { display: flex; align-items: center; gap: 10px; background: none; border: 0; cursor: pointer; padding: 4px 6px; border-radius: 8px; }
.user-btn:hover { background: var(--surface-2); }
.user-btn .who { text-align: left; line-height: 1.2; }
.user-btn .who b { display: block; font-size: 13px; }
.user-btn .who span { font-size: 12px; color: var(--text-3); text-transform: capitalize; }

/* ---------- dropdown menus ---------- */
.dropdown { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); right: 0; min-width: 190px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-lg);
  z-index: 50; display: none;
}
.menu.left { left: 0; right: auto; }
.dropdown.open > .menu { display: block; }
.menu button, .menu a {
  display: flex; width: 100%; align-items: center; gap: 8px; padding: 8px 10px; border: 0; background: none;
  border-radius: 6px; cursor: pointer; text-align: left; color: var(--text); font-size: 13px;
}
.menu button:hover, .menu a:hover { background: var(--surface-2); }
.menu .danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }

/* ---------- page layout ---------- */
.page { max-width: 1180px; margin: 0 auto; padding: 28px 20px 60px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head .search { max-width: 260px; }

/* ---------- map cards ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: visible; transition: border-color .12s, box-shadow .12s; position: relative;
}
.card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-lg); }
.card .thumb {
  height: 110px; border-radius: 12px 12px 0 0; background: var(--surface-2); display: grid; place-items: center;
  color: inherit; border-bottom: 1px solid var(--border);
}
.card .thumb svg { width: 150px; height: 80px; }
.card .body { padding: 12px 14px; display: flex; gap: 8px; align-items: flex-start; }
.card .body .info { flex: 1; min-width: 0; }
.card .title { font-weight: 600; color: var(--text); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .meta { font-size: 12px; color: var(--text-3); margin-top: 3px; }
.empty {
  text-align: center; padding: 70px 20px; border: 1.5px dashed var(--border-strong); border-radius: 14px; color: var(--text-2);
}
.empty h3 { margin: 0 0 6px; color: var(--text); }

/* ---------- admin ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; }
.panel-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 18px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
td .user-cell { display: flex; align-items: center; gap: 10px; }
td .user-cell b { display: block; font-weight: 600; }
td .user-cell span { font-size: 12px; color: var(--text-3); }
td .select { height: 30px; width: auto; font-size: 13px; }
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 500; padding: 3px 9px; border-radius: 99px; }
.badge.on { background: var(--success-soft); color: var(--success); }
.badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.you { background: var(--accent-soft); color: var(--accent); }
.switch { position: relative; width: 36px; height: 20px; flex: none; display: inline-block; vertical-align: middle; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border-strong); border-radius: 99px; cursor: pointer; transition: .15s; }
.switch span::after { content: ''; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; background: #fff; border-radius: 50%; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--success); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:disabled + span { opacity: .5; cursor: not-allowed; }
.role-help { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-top: 18px; }
.role-help div { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; font-size: 13px; color: var(--text-2); }
.role-help b { color: var(--text); display: block; margin-bottom: 2px; }

/* ---------- modal & toast ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 20, 30, .42); display: grid; place-items: center; z-index: 100; padding: 16px;
  animation: fade .12s ease-out;
}
.modal { width: 100%; max-width: 440px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); animation: pop .14s ease-out; }
.modal.wide { max-width: 560px; }
.modal-head { padding: 18px 20px 4px; font-size: 17px; font-weight: 650; }
.modal-body { padding: 12px 20px 6px; color: var(--text-2); line-height: 1.5; }
.modal-body .field { color: var(--text); }
.modal-foot { padding: 12px 20px 18px; display: flex; justify-content: flex-end; gap: 8px; }
.copy-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; color: var(--text); white-space: pre-wrap; word-break: break-all; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 200; align-items: center; pointer-events: none; }
.toast { background: #1b2230; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow-lg); font-size: 13px; animation: pop .15s ease-out; max-width: 90vw; }
.toast.error { background: var(--danger); }
.toast.success { background: #15803d; }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100%; padding: 20px;
  background: radial-gradient(1200px 600px at 10% -10%, #e6e8ff 0, transparent 60%), radial-gradient(900px 500px at 110% 110%, #e2f5ee 0, transparent 60%), var(--bg); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow-lg); padding: 32px 30px 26px; }
.login-card .brand { justify-content: center; font-size: 20px; margin-bottom: 6px; }
.login-card .brand-mark { width: 36px; height: 36px; border-radius: 10px; }
.login-card .brand-mark svg { width: 22px; height: 22px; }
.login-card p.sub { text-align: center; color: var(--text-2); margin: 0 0 24px; }
.login-card .btn { width: 100%; height: 40px; margin-top: 4px; }
.login-card .foot { text-align: center; font-size: 12px; color: var(--text-3); margin-top: 18px; }

/* ---------- editor ---------- */
.editor-page { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.editor-page .topbar { height: 52px; }
.toolbar {
  display: flex; align-items: center; gap: 4px; padding: 6px 12px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-wrap: wrap; position: relative; z-index: 10;
}
.toolbar .sep { width: 1px; height: 22px; background: var(--border); margin: 0 6px; }
.title-input { border: 1px solid transparent; background: transparent; font-weight: 600; font-size: 15px; padding: 5px 8px; border-radius: 6px; width: 240px; max-width: 40vw; }
.title-input:hover { border-color: var(--border); }
.title-input:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.status { font-size: 12px; color: var(--text-3); min-width: 64px; display: inline-flex; align-items: center; gap: 5px; }
.status.err { color: var(--danger); }
.status .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--success); }
.status.saving .dot { background: #e5a50a; }
.status.err .dot { background: var(--danger); }
.zoom-label { font-size: 12px; color: var(--text-2); width: 44px; text-align: center; font-variant-numeric: tabular-nums; }
.readonly-pill { font-size: 12px; background: var(--warn-soft); color: #92600a; padding: 3px 9px; border-radius: 99px; font-weight: 500; }
.swatches { display: none; grid-template-columns: repeat(5, 26px); gap: 6px; padding: 10px; min-width: 0; }
.dropdown.open > .swatches { display: grid; }
.swatch { width: 26px; height: 26px; border-radius: 7px; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--border-strong); cursor: pointer; padding: 0; }
.swatch:hover { transform: scale(1.08); }
.swatch.reset { background: conic-gradient(#e5484d, #e5a50a, #30a46c, #0090ff, #6e56cf, #e5484d); }
.banner { display: flex; align-items: center; gap: 12px; padding: 9px 16px; background: var(--warn-soft); border-bottom: 1px solid #f2dfa8; font-size: 13px; flex-wrap: wrap; }
.banner .spacer { flex: 1; }
#canvas-wrap { position: relative; flex: 1; overflow: hidden; background-color: #fbfbfc;
  background-image: radial-gradient(#dfe2e8 1px, transparent 1px); background-size: 22px 22px; outline: none; }
#canvas { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; }
#canvas.panning { cursor: grabbing; }
#canvas .n { cursor: pointer; }
#canvas .n .tgh { opacity: 0; transition: opacity .1s; }
#canvas .n:hover .tgh { opacity: 1; }
#canvas .toggle { cursor: pointer; }
#editor-input {
  position: absolute; z-index: 5; resize: none; overflow: hidden; border: 2px solid var(--accent); border-radius: 6px;
  background: #fff; color: #1b2230; box-shadow: var(--shadow-lg); outline: none; white-space: pre; margin: 0;
}
#drag-ghost { position: fixed; pointer-events: none; z-index: 60; background: var(--accent); color: #fff; padding: 5px 10px; border-radius: 6px; font-size: 13px; box-shadow: var(--shadow-lg); opacity: .9; max-width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hint-bar { position: absolute; left: 12px; bottom: 12px; font-size: 12px; color: var(--text-3); background: rgba(255,255,255,.85); padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border); pointer-events: none; }
.kbd-list { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; font-size: 13px; color: var(--text); }
kbd { display: inline-block; font-family: var(--font); font-size: 12px; padding: 2px 6px; border-radius: 5px; border: 1px solid var(--border-strong); border-bottom-width: 2px; background: var(--surface); margin-right: 2px; color: var(--text); }

@media (max-width: 720px) {
  .user-btn .who { display: none; }
  .nav { margin-left: 0; }
  .btn .lbl { display: none; }
  .title-input { width: 150px; }
  th, td { padding: 10px 12px; }
  .hint-bar { display: none; }
}
