:root {
  --bg: #16181d;
  --card: #20232a;
  --card-2: #272b33;
  --edge: #333842;
  --ink: #eceef2;
  --ink-dim: #9aa1ad;
  --hot: #ff9d73;
  --hot-2: #ff5f6d;
  --bad: #ff6b6b;
  --warn: #ffcf5c;
  --radius: 20px;
  --tile: 112px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- PIN ---------- */
.pin-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.pin-card {
  width: 100%; max-width: 320px; text-align: center; padding: 28px;
  background: var(--card); border: 1px solid var(--edge); border-radius: 28px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, .5);
}
.pin-logo { width: 56px; height: 56px; margin: 0 auto; border-radius: 16px; }
.pin-card h1 { margin: 16px 0 4px; font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.pin-sub { margin: 0; font-size: 14px; color: var(--ink-dim); }
.pin-dots { display: flex; justify-content: center; gap: 12px; margin: 24px 0; }
.pin-dots span {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--card-2); box-shadow: inset 0 0 0 1px var(--edge); transition: all .15s;
}
.pin-dots span.on { background: var(--hot); box-shadow: 0 0 12px rgba(255, 157, 115, .6); transform: scale(1.1); }
.pin-msg { height: 20px; font-size: 12px; font-variant-numeric: tabular-nums; }
.pin-msg .bad { color: var(--bad); }
.pin-msg .warn { color: var(--warn); }
.pin-keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 16px; transition: opacity .2s; }
.pin-keys.off { opacity: .4; pointer-events: none; }
.pin-keys button {
  height: 56px; border-radius: 16px; border: 1px solid var(--edge);
  background: var(--card-2); font-size: 20px; font-weight: 500;
  display: grid; place-items: center;
}
.pin-keys button svg { width: 24px; height: 24px; }
.pin-keys button:hover { background: #2e333c; }
.pin-keys button:active { background: #363c47; transform: scale(.97); }
.shake { animation: shake .4s; }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

/* ---------- Header ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 14px clamp(14px, 4vw, 40px);
  background: rgba(22, 24, 29, .85); backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(51, 56, 66, .6);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: -.02em; margin-right: auto; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.search {
  flex: 0 1 280px; min-width: 0; height: 38px; padding: 0 14px;
  border-radius: 12px; border: 1px solid var(--edge); background: var(--card); outline: none;
}
.search:focus { border-color: var(--hot); }
.icon-btn {
  height: 38px; min-width: 38px; padding: 0 12px; border-radius: 12px;
  border: 1px solid var(--edge); background: var(--card);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
}
.icon-btn:hover { background: var(--card-2); }
.icon-btn.primary { background: linear-gradient(135deg, var(--hot), var(--hot-2)); border-color: transparent; color: #1a1a1a; font-weight: 600; }
.icon-btn svg { width: 18px; height: 18px; }
@media (max-width: 560px) {
  .top { flex-wrap: wrap; }
  .search { order: 5; flex: 1 1 100%; }
  .btn-label { display: none; }
}

/* ---------- Tiles ---------- */
main { padding: 8px clamp(14px, 4vw, 40px) 60px; max-width: 1400px; margin: 0 auto; }
.offline { margin: 12px 0 0; padding: 8px 12px; border-radius: 12px; background: rgba(255, 207, 92, .1); color: var(--warn); font-size: 13px; }
section.group { margin-top: 22px; }
.group-head { display: flex; align-items: center; gap: 8px; margin: 0 0 10px 4px; }
.group-head h2 { margin: 0; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-dim); }
.group-head button { border: 0; background: none; color: var(--ink-dim); padding: 2px 6px; border-radius: 6px; font-size: 12px; }
.group-head button:hover { color: var(--ink); background: var(--card); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--tile), 1fr)); gap: 12px; }
@media (max-width: 420px) { :root { --tile: 92px; } .grid { gap: 10px; } }

.tile {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 16px 8px 12px; min-height: calc(var(--tile) + 10px);
  border-radius: var(--radius); border: 1px solid var(--edge); background: var(--card);
  color: inherit; text-decoration: none; text-align: center;
  transition: transform .12s, background .12s, border-color .12s;
}
a.tile:hover, .tile[role="button"]:hover, button.tile:hover { background: var(--card-2); border-color: #454b57; transform: translateY(-2px); }
a.tile:active { transform: scale(.97); }
a.tile:focus-visible, .tile[role="button"]:focus-visible, button.tile:focus-visible { outline: 2px solid var(--hot); outline-offset: 2px; }
.tile-icon {
  width: 52px; height: 52px; border-radius: 14px; flex: none;
  display: grid; place-items: center; overflow: hidden;
  font-size: 30px; line-height: 1; background: var(--card-2);
}
.tile-icon img { width: 100%; height: 100%; object-fit: contain; }
.tile-icon.letter { font-size: 22px; font-weight: 700; color: #fff; }
.tile-name { font-size: 13px; font-weight: 500; line-height: 1.25; max-width: 100%; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.tile-host { font-size: 11px; color: var(--ink-dim); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: -6px; }

.editing .tile { cursor: grab; border-style: dashed; }
.editing .tile.dragging { opacity: .4; }
.editing .tile.drop-before { box-shadow: -4px 0 0 var(--hot); }
.tile-tools { position: absolute; inset: 4px 4px auto 4px; display: flex; justify-content: space-between; opacity: 0; transition: opacity .15s; }
.editing .tile-tools { opacity: 1; }
.tile-tools button {
  width: 24px; height: 24px; border-radius: 8px; border: 1px solid var(--edge);
  background: var(--bg); font-size: 12px; line-height: 1; display: grid; place-items: center; padding: 0;
}
.tile.add { border-style: dashed; color: var(--ink-dim); justify-content: center; background: transparent; cursor: pointer; }
.tile.add:hover { color: var(--ink); }
.tile.add .tile-icon { background: transparent; border: 1px dashed var(--edge); font-size: 26px; }
.empty { margin: 60px auto; text-align: center; color: var(--ink-dim); }

/* ---------- Dialog ---------- */
dialog {
  width: min(440px, calc(100vw - 24px)); padding: 0; border-radius: 24px;
  border: 1px solid var(--edge); background: var(--card); color: var(--ink);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, .6);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); backdrop-filter: blur(3px); }
dialog form { padding: 22px; display: grid; gap: 14px; }
dialog h3 { margin: 0; font-size: 17px; }
.field { display: grid; gap: 6px; }
.field > span { font-size: 12px; color: var(--ink-dim); font-weight: 500; }
.field input {
  height: 40px; padding: 0 12px; border-radius: 12px; outline: none;
  border: 1px solid var(--edge); background: var(--bg);
}
.field input:focus { border-color: var(--hot); }
.icon-row { display: flex; gap: 12px; align-items: center; }
.icon-row .tile-icon { width: 64px; height: 64px; font-size: 36px; }
.icon-actions { display: flex; flex-wrap: wrap; gap: 8px; flex: 1; }
.icon-actions .icon-btn { height: 32px; font-size: 13px; padding: 0 10px; }
.emoji-row { display: flex; flex-wrap: wrap; gap: 4px; }
.emoji-row button { width: 34px; height: 34px; border-radius: 10px; border: 1px solid transparent; background: var(--bg); font-size: 18px; padding: 0; }
.emoji-row button:hover { border-color: var(--edge); }
.form-error { min-height: 18px; margin: 0; font-size: 13px; color: var(--bad); }
.dlg-actions { display: flex; gap: 8px; }
.dlg-actions .spacer { flex: 1; }
.icon-btn.danger { color: var(--bad); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  padding: 10px 16px; border-radius: 12px; background: #2e333c; border: 1px solid var(--edge);
  font-size: 14px; z-index: 50; box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
}
