:root {
  --bg: #f4f6fb; --surface: #ffffff; --surface-2: #f8fafc; --text: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --brand: #4f46e5; --brand-soft: #eef2ff; --on: #f59e0b; --on-soft: #fff7e6; --ok: #16a34a; --ok-soft: #dcfce7;
  --bad: #dc2626; --bad-soft: #fee2e2; --sk: #e8ecf3; --sk-hi: #f5f7fb; --shadow: 0 1px 2px rgba(15,23,42,.05), 0 8px 24px rgba(15,23,42,.06);
  --r: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------- top bar ---------- */
.top { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px max(16px, env(safe-area-inset-left)); background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -.01em; }
.logo { width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, #f59e0b, #f97316); box-shadow: 0 4px 12px rgba(245,158,11,.35); display: grid; place-items: center; }
.logo svg { width: 18px; height: 18px; fill: #fff; }
.top-right { display: flex; align-items: center; gap: 10px; }
.pill { display: inline-flex; align-items: center; gap: 7px; padding: 5px 11px; border-radius: 999px; font-size: 13px; font-weight: 600; background: var(--surface-2); border: 1px solid var(--line); white-space: nowrap; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.online .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.pill.offline .dot { background: var(--bad); box-shadow: 0 0 0 3px var(--bad-soft); }
.icon-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 7px 11px; font-size: 13px; font-weight: 600; }
.icon-btn:hover { background: var(--surface-2); }

.nav { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; width: fit-content; margin: 0 0 18px; }
.nav button { border: 0; background: none; padding: 8px 16px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.nav button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.page { max-width: 1040px; margin: 0 auto; padding: 22px 16px 48px; }
.h { font-size: 22px; font-weight: 700; letter-spacing: -.02em; margin: 0 0 4px; }
.sub { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- bulb cards ---------- */
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.bulb { position: relative; overflow: hidden; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 20px; box-shadow: var(--shadow); transition: border-color .25s, background .25s; }
.bulb.lit { border-color: color-mix(in srgb, var(--on) 55%, var(--line)); background: linear-gradient(180deg, var(--on-soft), var(--surface) 70%); }
.bulb-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.bulb-name { font-weight: 700; font-size: 16px; }
.bulb-pin { font-size: 12px; color: var(--muted); margin-top: 2px; }
.bulb-art { display: grid; place-items: center; height: 120px; margin: 8px 0 6px; }
.bulb-art svg { width: 76px; height: 76px; transition: filter .3s; }
.bulb-art .glass { fill: var(--sk-hi); stroke: var(--line); stroke-width: .6; transition: fill .3s; }
.bulb.lit .bulb-art .glass { fill: var(--on); stroke: none; }
.bulb.lit .bulb-art svg { filter: drop-shadow(0 0 18px rgba(245,158,11,.65)); }
.bulb-art .base { fill: var(--muted); opacity: .55; }
.bulb-foot { display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
.state { font-weight: 700; letter-spacing: .02em; }
.bulb.lit .state { color: #b45309; }


/* toggle switch */
.switch { position: relative; width: 54px; height: 30px; flex: none; border: 0; padding: 0; border-radius: 999px; background: var(--line); transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.2); transition: transform .2s; }
.switch[aria-checked="true"] { background: var(--on); }
.switch[aria-checked="true"]::after { transform: translateX(24px); }
.switch:focus-visible { outline: 3px solid var(--brand-soft); outline-offset: 2px; }

.actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.btn { border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600; background: var(--text); color: var(--bg); display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.ghost { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.btn.brand { background: var(--brand); color: #fff; }
.btn:disabled { opacity: .6; cursor: default; }
.btn .spin { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.info { margin-top: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px 18px; display: flex; flex-wrap: wrap; gap: 8px 24px; font-size: 13px; color: var(--muted); }
.info b { color: var(--text); font-weight: 600; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(1200px 500px at 50% -10%, var(--brand-soft), transparent); }
.login { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 28px; box-shadow: var(--shadow); }
.login .brand { justify-content: center; font-size: 18px; margin-bottom: 6px; }
.login p { text-align: center; margin: 0 0 22px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.input { position: relative; }
.input input { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); outline: none; transition: border-color .15s, box-shadow .15s; }
.input input:focus { border-color: var(--brand); box-shadow: 0 0 0 4px var(--brand-soft); }
.input .eye { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 600; padding: 6px; }
.err { color: var(--bad); background: var(--bad-soft); border-radius: 10px; padding: 9px 12px; font-size: 13px; margin-bottom: 14px; }
.login .btn { width: 100%; }

/* ---------- history ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 600; color: var(--muted); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.range { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.range input { padding: 6px 8px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); }
.stat .k { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-size: 24px; font-weight: 700; letter-spacing: -.02em; margin-top: 4px; }
.stat .d { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat .live { color: #b45309; font-weight: 600; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; }
.row { display: grid; grid-template-columns: 120px 1fr 220px; gap: 14px; padding: 13px 18px; border-bottom: 1px solid var(--line); align-items: center; }
.row:last-child { border-bottom: 0; }
.row.head { background: var(--surface-2); font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; padding-top: 10px; padding-bottom: 10px; }
.row .t { font-size: 13px; } .row .t div:last-child { color: var(--muted); font-size: 12px; }
.row .who { font-size: 13px; }
.row .who .ipx { color: var(--muted); font-size: 12px; }
.what { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ev { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; flex: none; font-size: 15px; background: var(--surface-2); }
.ev.on { background: var(--on-soft); } .ev.ok { background: var(--ok-soft); } .ev.bad { background: var(--bad-soft); } .ev.dev { background: var(--brand-soft); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: .03em; background: var(--surface-2); border: 1px solid var(--line); }
.tag.on { background: var(--on-soft); color: #b45309; border-color: transparent; }
.tag.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.tag.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.runs .row { grid-template-columns: 1.1fr 1fr 1fr .8fr 1.3fr; }
.ips .row { grid-template-columns: 1fr 1.3fr 1.4fr .9fr; }
.nick { display: flex; gap: 6px; }
.nick input { min-width: 0; flex: 1; padding: 7px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); }
.nick button { border: 0; border-radius: 9px; padding: 7px 12px; font-weight: 600; background: var(--text); color: var(--bg); font-size: 13px; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.note { font-size: 13px; color: var(--muted); margin: 0 0 12px; }

@media (max-width: 720px) {
  .row, .runs .row, .ips .row { grid-template-columns: 1fr; gap: 6px; }
  .row.head { display: none; }
  .row .t { display: flex; gap: 8px; } .row .t div:last-child { font-size: 13px; }
  .top .brand span:last-child { font-size: 15px; }
  .hide-sm { display: none; }
}

/* ---------- skeleton ---------- */
.sk { background: linear-gradient(90deg, var(--sk) 25%, var(--sk-hi) 50%, var(--sk) 75%); background-size: 300% 100%; animation: shimmer 1.3s ease-in-out infinite; border-radius: 12px; }
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
.sk-title { height: 26px; width: 220px; margin-bottom: 22px; }
.sk-card { height: 238px; border-radius: var(--r); }
.sk-stat { height: 86px; border-radius: 14px; }
.sk-row { height: 18px; margin: 18px; }
.sk-line { height: 12px; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) { .sk { animation: none; } }

/* ---------- toast ---------- */
#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600; transition: .25s; z-index: 50; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* v4 fixes */
.range[hidden] { display: none; }
.brand { min-width: 0; }
.brand span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn, .pill { white-space: nowrap; }
.nav { max-width: 100%; overflow-x: auto; scrollbar-width: none; }
.nav button { white-space: nowrap; }
@media (max-width: 420px) {
  .top { padding: 10px 12px; }
  .brand { gap: 8px; }
  .brand span:last-child { font-size: 14px; }
  .pill { padding: 4px 9px; font-size: 12px; }
}

/* v5: own nickname card, device / WiFi page */
.me { margin-top: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 16px 18px; box-shadow: var(--shadow); }
.me h3, .card h3 { margin: 0 0 4px; font-size: 15px; }
.me .nick { margin-top: 10px; max-width: 420px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-top: 12px; }
.kv div { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.kv span { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
.kv b { font-size: 15px; word-break: break-all; }
.wlist { margin: 12px 0 0; padding: 0; list-style: none; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.wlist li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.wlist li:last-child { border-bottom: 0; }
.wlist .del { border: 1px solid var(--line); background: var(--surface); border-radius: 9px; padding: 6px 10px; font-size: 13px; font-weight: 600; color: var(--bad); }
.wform { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; margin-top: 12px; }
.wform input { padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); min-width: 0; }
.steps { margin: 8px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.steps li { margin: 4px 0; }
.banner { border-radius: 12px; padding: 10px 14px; font-size: 13px; font-weight: 600; margin-top: 12px; }
.banner.ok { background: var(--ok-soft); color: var(--ok); }
.banner.wait { background: var(--on-soft); color: #b45309; }
@media (max-width: 600px) { .wform { grid-template-columns: 1fr; } }

.wbtns { display: flex; gap: 6px; flex-shrink: 0; }
.wlist .conn { border: 0; background: var(--brand); color: #fff; border-radius: 9px; padding: 6px 12px; font-size: 13px; font-weight: 600; }
.wlist .conn:disabled { opacity: .5; cursor: default; }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
