/* ===========================================================
   Portir — design system
   Modern · minimal · bold. Near-black canvas, one indigo accent,
   heavy type, generous whitespace, hairline structure.
   =========================================================== */

:root {
  /* dark (default) */
  --bg: #0a0a0f;
  --bg-elev: #101019;
  --card: #14141d;
  --card-2: #1d1d29;
  --text: #fafafa;
  --muted: #8b8b9c;
  --faint: #5b5b6b;

  --accent: #6366f1;
  --accent-2: #8b5cf6;
  --accent-press: #4f46e5;
  --grad: linear-gradient(135deg, #6366f1, #8b5cf6);

  --green: #34d399;
  --amber: #fbbf24;
  --red: #fb7185;

  --hair: rgba(255,255,255,.07);
  --border: rgba(255,255,255,.10);
  --ring: rgba(99,102,241,.45);
  --overlay: rgba(5,5,10,.72);

  --r-card: 20px;
  --r-btn: 13px;
  --r-field: 13px;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
  --shadow-lg: 0 24px 60px rgba(0,0,0,.55);
  --shadow-accent: 0 8px 24px rgba(99,102,241,.35);
}

:root[data-theme="light"] {
  --bg: #f5f6fa;
  --bg-elev: #ffffff;
  --card: #ffffff;
  --card-2: #eef0f6;
  --text: #14141f;
  --muted: #6b6b7b;
  --faint: #a0a0b0;

  --hair: rgba(15,15,30,.07);
  --border: rgba(15,15,30,.11);
  --ring: rgba(99,102,241,.35);
  --overlay: rgba(15,17,28,.40);

  --shadow: 0 1px 2px rgba(15,23,42,.06);
  --shadow-lg: 0 24px 60px rgba(15,23,42,.18);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 500; font-size: 0.85em; }
.muted.small, .small { font-size: 0.8rem; }
.error { color: var(--red); font-size: 0.9rem; margin-top: 10px; font-weight: 500; }

/* Agent token reveal */
.warn-box { background: var(--card-2); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 10px; padding: 10px 12px; font-size: 0.85rem; color: var(--text); margin: 4px 0 14px; line-height: 1.4; }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.82rem;
  background: var(--card-2); border: 1px solid var(--border); border-radius: 10px; color: var(--text); word-break: break-all; }
.secret-row { display: flex; align-items: center; gap: 8px; }
.secret-row .secret { flex: 1; padding: 10px 12px; }
.secret.block { display: block; white-space: pre; overflow-x: auto; padding: 12px; margin: 6px 0 8px; line-height: 1.45; }

/* Pending (self-enrolled) agents */
.pending-head { display: flex; align-items: center; gap: 6px; font-size: 0.78rem; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--amber); margin: 4px 0 8px; }
.pending-row { border: 1px solid color-mix(in srgb, var(--amber) 35%, var(--border)); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: .04em; }

/* ---- login ---- */
.login-view { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card {
  position: relative;
  width: 100%; max-width: 400px;
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: 28px;
  padding: 44px 30px 32px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.lang-btn {
  position: absolute; top: 18px; right: 18px;
  background: var(--card-2); border: none; color: var(--muted);
  font-size: 0.72rem; font-weight: 700; letter-spacing: .06em;
  padding: 7px 11px; border-radius: 9px; cursor: pointer;
}
.logo {
  width: 76px; height: 76px; margin: 0 auto 22px;
  display: flex; align-items: center; justify-content: center;
  font-size: 2.4rem;
  background: var(--grad);
  border-radius: 22px;
  box-shadow: var(--shadow-accent);
}
.login-card h1 { font-size: 2rem; font-weight: 800; letter-spacing: -.03em; margin-bottom: 6px; }
.login-card p { margin-bottom: 26px; font-size: 0.95rem; }
.login-card input { margin-bottom: 12px; }
.login-card .btn { margin-top: 4px; }

/* ---- app shell ---- */
#app { display: flex; flex-direction: column; min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 16px;
  padding-top: calc(18px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  font-weight: 800; font-size: 1.5rem; letter-spacing: -.03em;
}
.topbar > span { display: flex; align-items: center; gap: 2px; }
.icon-btn {
  background: none; border: none; color: var(--muted);
  font-size: 1.2rem; cursor: pointer; padding: 8px; border-radius: 10px;
  line-height: 1; transition: background .15s, color .15s;
}
.icon-btn:active { background: var(--card-2); }
.lang-ico { font-size: 0.78rem !important; font-weight: 800; letter-spacing: .06em; }

#main {
  flex: 1; padding: 12px 18px;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  max-width: 760px; width: 100%; margin: 0 auto;
}

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: center; gap: 4px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-top: 1px solid var(--hair);
  padding: 8px 8px env(safe-area-inset-bottom);
  max-width: 760px; margin: 0 auto;
}
.tab {
  position: relative;
  flex: 1; max-width: 120px; background: none; border: none; color: var(--faint);
  padding: 10px 0 12px; font-size: 0.7rem; font-weight: 600; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: color .18s;
}
.tab .tab-ico { font-size: 1.35rem; transition: transform .18s; }
.tab.active { color: var(--accent); }
.tab.active .tab-ico { transform: translateY(-1px) scale(1.06); }
.tab.active::before {
  content: ""; position: absolute; top: 0; width: 26px; height: 3px;
  border-radius: 0 0 4px 4px; background: var(--accent);
}

/* ---- cards / rows ---- */
.card {
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--r-card);
  padding: 18px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.door-card { display: flex; align-items: center; gap: 15px; cursor: pointer; transition: transform .12s, border-color .15s; }
.door-card:active { transform: scale(.985); border-color: var(--border); }
.door-ico {
  width: 50px; height: 50px; flex-shrink: 0;
  background: var(--card-2); border-radius: 15px;
  display: flex; align-items: center; justify-content: center; font-size: 1.5rem;
}
.door-info { flex: 1; min-width: 0; }
.door-info .title { font-weight: 700; letter-spacing: -.01em; }
.door-info .sub { color: var(--muted); font-size: 0.8rem; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.door-counts { display: flex; gap: 6px; flex-shrink: 0; }

.pill {
  font-size: 0.68rem; font-weight: 700; letter-spacing: .02em;
  padding: 4px 9px; border-radius: 999px;
  background: var(--card-2); color: var(--muted); white-space: nowrap;
}
.pill.green { color: var(--green); background: color-mix(in srgb, var(--green) 16%, transparent); }
.pill.amber { color: var(--amber); background: color-mix(in srgb, var(--amber) 16%, transparent); }
.pill.red   { color: var(--red);   background: color-mix(in srgb, var(--red) 16%, transparent); }

.person-row { display: flex; align-items: center; gap: 13px; cursor: pointer; transition: transform .12s; }
.person-row:active { transform: scale(.99); }
.avatar {
  width: 46px; height: 46px; flex-shrink: 0; border-radius: 50%;
  background: var(--card-2); color: var(--accent);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 0.95rem;
}
.avatar.visitor { color: var(--amber); }
.avatar.arch { opacity: .5; }
.person-info { flex: 1; min-width: 0; }
.person-info .title { font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.person-info .sub { color: var(--muted); font-size: 0.78rem; margin-top: 3px; }
.row-actions { display: flex; gap: 6px; flex-shrink: 0; align-items: center; color: var(--faint); font-size: 1.3rem; }

/* ---- buttons ---- */
.btn {
  border: none; border-radius: var(--r-btn); padding: 13px 18px;
  background: var(--card-2); color: var(--text);
  font-family: inherit; font-size: 0.95rem; font-weight: 700; letter-spacing: -.01em;
  cursor: pointer; transition: transform .12s, filter .15s, opacity .15s;
}
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--grad); color: #fff; box-shadow: var(--shadow-accent); }
.btn.primary:active { filter: brightness(1.05); }
.btn.danger { background: color-mix(in srgb, var(--red) 18%, transparent); color: var(--red); }
.btn.small { padding: 9px 13px; font-size: 0.8rem; border-radius: 10px; }
.btn.block { width: 100%; padding: 15px; font-size: 1rem; }
.btn:disabled { opacity: .5; }

/* ---- inputs ---- */
input, select, textarea {
  width: 100%; padding: 14px 15px;
  border-radius: var(--r-field); border: 1px solid var(--border);
  background: var(--bg-elev); color: var(--text);
  font-family: inherit; font-size: 1rem; font-weight: 500;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: var(--faint); }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--ring);
}
label {
  display: block; font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .07em;
  color: var(--muted); margin: 16px 0 8px;
}
form label:first-of-type { margin-top: 0; }
label .muted { text-transform: none; letter-spacing: 0; font-weight: 500; }
select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%238b8b9c' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 15px center;
}

/* ---- segmented control ---- */
.seg { display: flex; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 13px; padding: 4px; gap: 4px; }
.seg-btn {
  flex: 1; border: none; border-radius: 9px; padding: 10px 0;
  background: none; color: var(--muted); font-family: inherit; font-weight: 700; font-size: 0.88rem;
  cursor: pointer; transition: background .15s, color .15s;
}
.seg-btn.active { background: var(--grad); color: #fff; }
.seg-btn.hidden { display: none; }

/* ---- door / building checklists ---- */
.door-checks { display: flex; flex-direction: column; gap: 8px; }
.door-check {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-elev); padding: 13px 14px; border-radius: 12px;
  border: 1px solid var(--border); cursor: pointer; font-size: 0.92rem; font-weight: 500;
  transition: border-color .15s, background .15s;
}
.door-check input { width: 20px; height: 20px; accent-color: var(--accent); flex-shrink: 0; }
.door-check.checked { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 9%, var(--bg-elev)); }

/* ---- section heads / empty / nav ---- */
.section-head { display: flex; align-items: center; justify-content: space-between; margin: 26px 2px 12px; }
.section-head h2 { font-size: 0.74rem; font-weight: 700; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .big { font-size: 2.6rem; margin-bottom: 10px; }
.empty p { margin-bottom: 4px; }
.back-link {
  color: var(--accent); background: none; border: none; font-size: 0.9rem; font-weight: 700;
  cursor: pointer; padding: 8px 0; margin: 4px 0 6px; display: inline-block;
}
.filter-row { display: flex; gap: 10px; margin-bottom: 14px; }
.filter-row input { flex: 1; }

/* ---- dashboard ---- */
.greeting { font-size: 1.7rem; font-weight: 800; letter-spacing: -.03em; margin: 14px 2px 18px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 6px; }
.stat {
  background: var(--card); border: 1px solid var(--hair);
  border-radius: 18px; padding: 18px; box-shadow: var(--shadow);
}
.stat-n { font-size: 2.4rem; font-weight: 900; letter-spacing: -.04em; line-height: 1; }
.stat-l { color: var(--muted); font-size: 0.78rem; font-weight: 600; margin-top: 8px; }
.stat:first-child { background: var(--grad); border-color: transparent; box-shadow: var(--shadow-accent); }
.stat:first-child .stat-n { color: #fff; }
.stat:first-child .stat-l { color: rgba(255,255,255,.85); }

/* ---- activity / access logs ---- */
.log-row { display: flex; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--hair); }
.log-row:last-child { border-bottom: none; }
.log-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-top: 7px; flex-shrink: 0; }
.log-dot.ok { background: var(--green); }
.log-dot.bad { background: var(--red); }
.log-body { flex: 1; min-width: 0; font-size: 0.9rem; font-weight: 500; }
.log-body b { font-weight: 700; }
.log-time { color: var(--faint); font-size: 0.74rem; margin-top: 2px; font-weight: 500; }

/* ---- filter chips ---- */
.chip-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.chip {
  border: 1px solid var(--border); background: var(--card); color: var(--muted);
  border-radius: 999px; padding: 8px 15px; font-family: inherit; font-size: 0.8rem; font-weight: 700;
  cursor: pointer; transition: transform .12s, background .15s, color .15s, border-color .15s;
}
.chip:active { transform: scale(.95); }
.chip.active { background: var(--text); color: var(--bg); border-color: transparent; }
.chip-sep { width: 1px; align-self: stretch; background: var(--border); margin: 2px; }

/* ---- detail ---- */
.kv { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 14px; font-size: 0.92rem; font-weight: 500; }
.person-row.mini { padding: 13px 15px; }
.person-row.mini .door-ico { box-shadow: none; }

/* ---- building / controller ---- */
.building-card { padding-bottom: 14px; }
.adm-head { display: flex; align-items: center; gap: 13px; }
.adm-head .person-info .title { font-size: 1.05rem; }
.adm-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 13px; }

.ctrl-row { border-top: 1px solid var(--hair); margin-top: 14px; padding-top: 14px; }
.ctrl-top { display: flex; align-items: center; gap: 12px; }
.ctrl-top .door-ico { color: var(--accent); }
.ctrl-meta {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin-top: 11px; font-size: 0.78rem; color: var(--muted); font-weight: 600;
}
.meta-bit { display: inline-flex; align-items: center; gap: 5px; }
.ctrl-row .adm-actions { margin-top: 12px; }
.sub-indent { padding: 10px 0 4px; font-size: 0.85rem; }

/* status chip */
.status {
  display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
  font-size: 0.7rem; font-weight: 800; letter-spacing: .02em; white-space: nowrap;
  padding: 5px 10px; border-radius: 999px;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-online   { color: var(--green); background: color-mix(in srgb, var(--green) 15%, transparent); }
.status-offline  { color: var(--red);   background: color-mix(in srgb, var(--red) 15%, transparent); }
.status-unknown  { color: var(--muted); background: var(--card-2); }
.status-checking { color: var(--muted); background: var(--card-2); padding: 5px 11px; }
.role-hint { margin: 10px 0 2px; font-size: 0.8rem; }

/* ---- NFC ---- */
.input-row { display: flex; gap: 10px; }
.input-row input { flex: 1; min-width: 0; }
.nfc-btn { padding: 0 18px; font-size: 1.15rem; flex-shrink: 0; }
.nfc-btn.scanning { background: var(--grad); color: #fff; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.nfc-hint { margin-top: 8px; font-size: 0.78rem; word-break: break-all; }
.nfc-hint a { color: var(--accent); font-weight: 700; }

/* ---- modal / sheet ---- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: var(--overlay);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal-card {
  position: relative;
  width: 100%; max-width: 580px; max-height: 92dvh; overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--hair);
  border-radius: 28px 28px 0 0;
  padding: 30px 22px calc(26px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow-lg);
  animation: rise .26s cubic-bezier(.2,.8,.2,1);
}
@keyframes rise { from { transform: translateY(40px); opacity: .4; } }
.modal-card::before {
  content: ""; position: absolute; top: 11px; left: 50%; transform: translateX(-50%);
  width: 42px; height: 5px; border-radius: 99px; background: var(--border);
}
@media (min-width: 600px) {
  .modal { align-items: center; }
  .modal-card { border-radius: 28px; }
  .modal-card::before { display: none; }
}
.modal-card h2 { font-size: 1.3rem; font-weight: 800; letter-spacing: -.02em; margin-bottom: 18px; }
.modal-actions { display: flex; gap: 12px; margin-top: 24px; }
.modal-actions .btn { flex: 1; }
.dialog-card { max-width: 440px; }
.dialog-card h2 { margin-bottom: 10px; }
.dialog-card #dialog-message { font-size: 0.95rem; line-height: 1.5; }
.dialog-card #dialog-input { margin-top: 16px; }

/* ---- toast ---- */
.toast {
  position: fixed; bottom: calc(104px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 13px 20px; border-radius: 14px; font-size: 0.9rem; font-weight: 600;
  z-index: 100; max-width: 90vw; text-align: center;
  box-shadow: var(--shadow-lg);
  animation: rise .26s cubic-bezier(.2,.8,.2,1);
}
.toast.err { background: var(--red); color: #1a0508; }

/* ---- line icons ---- */
.ico { width: 1.15em; height: 1.15em; flex-shrink: 0; vertical-align: -0.2em; }
.ico-inline { width: 1em; height: 1em; vertical-align: -0.14em; }
.ico-head { width: 15px; height: 15px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn .ico { width: 1.05em; height: 1.05em; vertical-align: 0; }
.btn.icon-only { padding: 9px; gap: 0; }
.btn.icon-only .ico { width: 16px; height: 16px; }

.icon-btn { display: inline-flex; align-items: center; justify-content: center; }
.icon-btn .ico { width: 21px; height: 21px; }

.logo svg { width: 40px; height: 40px; }

.tab-ico { display: inline-flex; }
.tab-ico svg { width: 25px; height: 25px; }

.door-ico .ico { width: 52%; height: 52%; color: var(--accent); }
.avatar .ico { width: 52%; height: 52%; }

.stat-l { display: inline-flex; align-items: center; gap: 6px; }
.stat-l .ico { width: 15px; height: 15px; }

.pill { display: inline-flex; align-items: center; gap: 4px; }
.pill .ico { width: 0.95em; height: 0.95em; vertical-align: 0; }

.section-head h2 { display: inline-flex; align-items: center; gap: 7px; }

.row-actions > .ico { width: 20px; height: 20px; }

.back-link { display: inline-flex; align-items: center; gap: 5px; }
.back-link .ico { width: 1.05em; height: 1.05em; vertical-align: 0; }

.empty .big svg { width: 46px; height: 46px; opacity: .8; }

.nfc-btn .ico { width: 20px; height: 20px; }

.person-info .sub .ico, .kv .ico { width: 1em; height: 1em; vertical-align: -0.14em; }

/* ---- platform (superadmin) ---- */
.view-platform { display: flex; flex-direction: column; min-height: 100dvh; }
#platform-main {
  flex: 1; padding: 12px 18px calc(40px + env(safe-area-inset-bottom));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.tenant-chip {
  display: inline-flex; align-items: center; gap: 5px;
  background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent);
  border: none; border-radius: 999px; padding: 5px 11px;
  font-size: 0.74rem; font-weight: 700; cursor: pointer; white-space: nowrap;
  max-width: 55vw; overflow: hidden; text-overflow: ellipsis;
}
.tenant-chip .ico { width: 0.95em; height: 0.95em; }

/* ---- misc ---- */
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- account menu ---------- */
.user-wrap { position: relative; display: inline-flex; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%; border: none; cursor: pointer;
  background: var(--grad); color: #fff; font-weight: 700; font-size: .78rem;
  display: inline-flex; align-items: center; justify-content: center; padding: 0;
}
.user-avatar:active { filter: brightness(1.08); }
.user-menu {
  position: absolute; top: 44px; right: 0; min-width: 216px; z-index: 60;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 14px 36px rgba(0,0,0,.38); padding: 6px;
}
.user-menu-head { padding: 10px 12px 9px; border-bottom: 1px solid var(--hair); margin-bottom: 4px; }
.user-menu-head .um-name { font-weight: 600; }
.user-menu-head .um-role { color: var(--muted); font-size: .78rem; margin-top: 2px; }
.menu-item {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  background: none; border: none; color: var(--text); cursor: pointer; border-radius: 10px;
  font-size: .9rem; text-align: left; font-family: inherit;
}
.menu-item:hover, .menu-item:active { background: var(--card-2); }
.menu-item .ico { width: 18px; height: 18px; flex: none; }
.menu-item.danger-text { color: var(--red); }
