/* IoT Panel Cloud portal. Tokens first, then base, layout, components. */
:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f0f3f8;
  --border: #dde3ec;
  --text: #17202e;
  --muted: #5b6678;
  --primary: #1f5fd6;
  --primary-hover: #194fb4;
  --primary-ink: #ffffff;
  --primary-soft: #e6eefc;
  --danger: #c62d3a;
  --danger-soft: #fbe9ea;
  --success: #17803d;
  --success-soft: #e3f5ea;
  --warn: #9a6200;
  --warn-soft: #fff1d6;
  --focus: #1f5fd6;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, 0.22);
  --sidebar: #fbfcfe;
  --radius: 10px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141c; --surface: #171e29; --surface-2: #1e2633; --border: #2b3544;
    --text: #e8edf5; --muted: #98a4b8;
    --primary: #5b95ff; --primary-hover: #7aa9ff; --primary-ink: #0b1220; --primary-soft: #1b2b4a;
    --danger: #ff6b76; --danger-soft: #3a1c21; --success: #4cc27d; --success-soft: #153324;
    --warn: #f0b64a; --warn-soft: #3a2d10; --focus: #8bb3ff; --sidebar: #131a24;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.35); --shadow-lg: 0 12px 40px rgba(0,0,0,.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141c; --surface: #171e29; --surface-2: #1e2633; --border: #2b3544;
  --text: #e8edf5; --muted: #98a4b8;
  --primary: #5b95ff; --primary-hover: #7aa9ff; --primary-ink: #0b1220; --primary-soft: #1b2b4a;
  --danger: #ff6b76; --danger-soft: #3a1c21; --success: #4cc27d; --success-soft: #153324;
  --warn: #f0b64a; --warn-soft: #3a2d10; --focus: #8bb3ff; --sidebar: #131a24;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.35); --shadow-lg: 0 12px 40px rgba(0,0,0,.6);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); min-height: 100vh; overflow-x: hidden; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.1rem; } h3 { font-size: 1rem; }
p { margin: 0; }
a { color: var(--primary); }
code, .mono { font-family: var(--mono); font-size: 0.9em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.sr-only, input.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* shell */
.shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: var(--s4); gap: var(--s4); position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: var(--s3); font-weight: 700; font-size: 1.05rem; text-decoration: none; color: var(--text); }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--primary); color: var(--primary-ink); display: grid; place-items: center; flex: none; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: var(--s3); padding: 10px 12px; border-radius: 8px; color: var(--text); text-decoration: none; font-weight: 500; min-height: 40px; }
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--primary-soft); color: var(--primary); }
.nav-label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); padding: var(--s3) 12px var(--s1); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--s3); border-top: 1px solid var(--border); padding-top: var(--s4); }
.who { min-width: 0; }
.who strong, .who span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar { display: none; }
.content { min-width: 0; padding: var(--s5) var(--s6) var(--s6); max-width: 1200px; width: 100%; }
.scrim { display: none; }

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .topbar { display: flex; align-items: center; gap: var(--s3); position: sticky; top: 0; z-index: 30; background: var(--sidebar); border-bottom: 1px solid var(--border); padding: 8px var(--s4); }
  .topbar .brand { font-size: 1rem; }
  .topbar .spacer { flex: 1; }
  .sidebar { position: fixed; z-index: 50; left: 0; top: 0; bottom: 0; width: 272px; max-width: 86vw; transform: translateX(-102%); transition: transform 0.2s ease; box-shadow: var(--shadow-lg); }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; }
  .content { padding: var(--s4); }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation-duration: 0.01ms !important; } }

/* page */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s3); margin-bottom: var(--s5); }
.page-head p { color: var(--muted); margin-top: var(--s1); }
.page-actions { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.crumb { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; font-size: 0.9rem; margin-bottom: var(--s3); }

/* surfaces */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: var(--s5); }
.card + .card, .stack > * + * { margin-top: var(--s4); }
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); }
.card-body { padding: var(--s5); }
.grid { display: grid; gap: var(--s4); }
.grid.cols-3 { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px var(--s4); font-size: 0.92rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; overflow-wrap: anywhere; }

/* buttons */
.btn { appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 600; padding: 8px 14px; min-height: 38px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); }
.btn-danger.solid { background: var(--danger); color: #fff; }
.btn-danger.solid:hover:not(:disabled) { background: var(--danger); filter: brightness(0.92); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: 0.85rem; }
.btn-icon { padding: 6px; min-width: 38px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn[aria-busy="true"]::after, .spinner { content: ""; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; display: inline-block; }
.btn[aria-busy="true"]::after { flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.btn-row { display: flex; flex-wrap: wrap; gap: var(--s2); }

/* forms */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--s4); min-width: 0; }
.field > label, .label { font-weight: 600; font-size: 0.9rem; }
.hint { font-size: 0.82rem; color: var(--muted); }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; min-height: 40px; width: 100%; min-width: 0;
}
textarea { min-height: 72px; resize: vertical; }
input:disabled, select:disabled { background: var(--surface-2); color: var(--muted); }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--primary); flex: none; }
.check { display: flex; align-items: center; gap: var(--s2); min-height: 32px; }
.input-group { display: flex; gap: var(--s2); }
.input-group input { flex: 1; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--s4); }
@media (max-width: 560px) { .form-row { grid-template-columns: 1fr; } }
.search { position: relative; flex: 1; min-width: 200px; max-width: 420px; }
.search input { padding-left: 36px; }
.search svg { position: absolute; left: 11px; top: 11px; color: var(--muted); pointer-events: none; }

/* tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 10px var(--s4); border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; white-space: nowrap; background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
td.num, th.num { text-align: right; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn { margin-left: 2px; }
.cell-title { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 0.85rem; }
.truncate { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--success-soft); color: var(--success); }
.badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--primary-soft); color: var(--primary); }

/* empty, skeleton */
.empty { text-align: center; padding: var(--s6) var(--s4); display: flex; flex-direction: column; align-items: center; gap: var(--s3); color: var(--muted); }
.empty h3 { color: var(--text); }
.empty svg { color: var(--muted); }
.skel { display: block; height: 14px; border-radius: 6px; background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }
.skel-rows { padding: var(--s5); display: grid; gap: var(--s4); }
.center-spin { display: grid; place-items: center; padding: var(--s6); color: var(--muted); }
.center-spin .spinner { width: 24px; height: 24px; }

/* app cards */
.app-card { padding: var(--s4); display: flex; flex-direction: column; gap: var(--s3); text-decoration: none; color: inherit; cursor: pointer; text-align: left; font: inherit; width: 100%; }
.app-card:hover { border-color: var(--primary); }
.app-head { display: flex; gap: var(--s3); align-items: center; min-width: 0; }
.app-icon { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; font-weight: 700; color: #fff; flex: none; font-size: 1.1rem; text-shadow: 0 1px 2px rgba(0,0,0,.35); }
.app-head > div { min-width: 0; }
.app-head strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

/* auth */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--s4); }
.auth-card { width: 100%; max-width: 420px; padding: var(--s6) var(--s5); }
.auth-card .brand { justify-content: center; margin-bottom: var(--s5); font-size: 1.2rem; }
.auth-card h1 { text-align: center; font-size: 1.3rem; margin-bottom: var(--s1); }
.auth-card .sub { text-align: center; color: var(--muted); margin-bottom: var(--s5); }
.auth-card .btn-block { width: 100%; }
.divider { display: flex; align-items: center; gap: var(--s3); color: var(--muted); font-size: 0.82rem; margin: var(--s4) 0; }
.divider::before, .divider::after { content: ""; height: 1px; background: var(--border); flex: 1; }
.auth-foot { text-align: center; margin-top: var(--s4); }
/* downloads */
.dl-public { width: 100%; max-width: 460px; padding: var(--s5) 0; }
.dl-public .brand { justify-content: center; margin-bottom: var(--s5); font-size: 1.2rem; }
.dl-public h1 { text-align: center; font-size: 1.3rem; margin-bottom: var(--s1); }
.dl-public .sub { text-align: center; color: var(--muted); margin-bottom: var(--s5); }
.dl-list { display: grid; gap: var(--s4); max-width: 460px; }
.dl-public .dl-list { max-width: none; }
.dl-list > .card { margin-top: 0; }
.dl-name { font-size: 1.1rem; margin: 0 0 var(--s1); }
.dl-desc { margin: 0 0 var(--s2); }
.dl-meta { margin: 0 0 var(--s4); }
.dl-btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; }
.dl-sha { margin: var(--s3) 0 0; display: grid; gap: 2px; }
.dl-sha code { font-size: 0.72rem; word-break: break-all; overflow-wrap: anywhere; }
.dl-help { margin: var(--s3) 0 0; }
.auth-theme { position: fixed; top: var(--s3); right: var(--s3); }
.notice { padding: var(--s3) var(--s4); border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: 0.92rem; }
.notice.info { background: var(--primary-soft); color: var(--primary); }

/* modal + toast */
.overlay { position: fixed; inset: 0; background: rgba(8, 12, 20, 0.55); z-index: 100; display: grid; place-items: center; padding: var(--s4); overflow-y: auto; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); width: 100%; max-width: 520px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; border: 1px solid var(--border); }
.modal-head { padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: var(--s3); }
.modal-body { padding: var(--s5); overflow-y: auto; }
.modal-foot { padding: var(--s4) var(--s5); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; flex-wrap: wrap; gap: var(--s2); }
.toasts { position: fixed; z-index: 200; right: var(--s4); bottom: var(--s4); display: flex; flex-direction: column; gap: var(--s2); width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--primary); border-radius: 8px; padding: 10px 14px; box-shadow: var(--shadow-lg); display: flex; gap: var(--s3); align-items: flex-start; font-size: 0.92rem; }
.toast.error { border-left-color: var(--danger); } .toast.success { border-left-color: var(--success); }
.toast span { flex: 1; overflow-wrap: anywhere; }
.toast button { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1.1rem; line-height: 1; padding: 0 2px; }
.cred-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: var(--s3) var(--s4); font-family: var(--mono); font-size: 0.9rem; white-space: pre-wrap; overflow-wrap: anywhere; margin-bottom: var(--s3); }

/* access matrix */
.matrix-wrap { overflow: auto; max-height: 70vh; max-width: 100%; }
.matrix { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.matrix th, .matrix td { border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); padding: 6px 8px; text-align: center; }
.matrix thead th { position: sticky; top: 0; z-index: 2; min-width: 130px; text-transform: none; letter-spacing: 0; font-size: 0.85rem; color: var(--text); vertical-align: bottom; }
.matrix th.rowhead, .matrix td.rowhead { position: sticky; left: 0; z-index: 1; background: var(--surface); text-align: left; min-width: 170px; max-width: 220px; box-shadow: 1px 0 0 var(--border); }
.matrix thead th.rowhead { z-index: 3; background: var(--surface-2); }
.matrix select { min-height: 34px; padding: 4px 8px; font-size: 0.85rem; }
.matrix td.dirty { background: var(--warn-soft); }
.matrix td.on { background: color-mix(in srgb, var(--primary-soft) 70%, transparent); }
.matrix td.dirty.on { background: var(--warn-soft); }
.matrix .colname { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; margin: 0 auto 4px; }
.savebar { position: sticky; bottom: var(--s3); z-index: 10; margin-top: var(--s4); display: flex; flex-wrap: wrap; gap: var(--s3); align-items: center; justify-content: space-between; padding: var(--s3) var(--s4); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.savebar.dirty { border-color: var(--warn); }

.access-list { display: grid; gap: 2px; }
.access-row { display: grid; grid-template-columns: 24px minmax(0, 1fr) 130px; gap: var(--s3); align-items: center; padding: 8px var(--s3); border-radius: 8px; }
.access-row:hover { background: var(--surface-2); }
.access-row select { min-height: 34px; padding: 4px 8px; }
.access-row .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.access-row .nm strong { display: block; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 420px) { .access-row { grid-template-columns: 24px minmax(0, 1fr) 108px; } }
.matrix tbody th.rowhead { text-transform: none; letter-spacing: 0; font-size: 0.9rem; color: var(--text); background: var(--surface); white-space: normal; font-weight: 400; }
.matrix tbody th.rowhead select, .matrix thead th select { width: 100%; max-width: 150px; }
.grid > .card, .grid > section { margin-top: 0; }
.kv { margin: 0; }

/* setup page */
.sr-only, input.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.done-mark { width: 52px; height: 52px; border-radius: 50%; margin: 0 auto var(--s3); display: grid; place-items: center; background: var(--success-soft); color: var(--success); }
.link-row { display: flex; gap: var(--s2); margin-bottom: var(--s3); }
.link-row input { flex: 1; min-width: 0; font-family: var(--mono); font-size: 0.82rem; }
