:root {
  --bg: #f3eefb;
  --panel: #ffffff;
  --ink: #1a1028;
  --muted: #6b5b7a;
  --brand: #8b3dff;
  --brand-2: #6d28d9;
  --brand-soft: #c084fc;
  --brand-glow: #e9d5ff;
  --line: #e5d8f5;
  --danger: #b33a3a;
  --ok: #2f6b4a;
  --sidebar: #0d0818;
  --sidebar-2: #1a0f2e;
  --radius: 14px;
  --shadow: 0 12px 32px rgba(76, 29, 149, 0.08);
  --font: "Segoe UI", "Noto Sans TC", "PingFang TC", sans-serif;
  --grad: linear-gradient(135deg, #c084fc 0%, #8b3dff 48%, #5b21b6 100%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(ellipse 55% 40% at 100% 0%, rgba(192, 132, 252, 0.28), transparent 55%),
    radial-gradient(ellipse 45% 35% at 0% 0%, rgba(139, 61, 255, 0.16), transparent 50%),
    var(--bg);
}
body:not(.is-authed) .shell { display: none !important; }
body.is-authed .login-view { display: none !important; }

button, input, textarea, select { font: inherit; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 11px;
  padding: 8px 14px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { border-color: color-mix(in srgb, var(--brand) 35%, var(--line)); }
.btn.primary {
  background: var(--grad);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 8px 20px rgba(139, 61, 255, 0.28);
}
.btn.primary:hover { filter: brightness(1.05); transform: translateY(-1px); }
.btn.danger {
  background: #fff;
  border-color: #e2b4b4;
  color: var(--danger);
}
.btn.ghost { background: transparent; }
.btn.sm { padding: 6px 10px; font-size: 13px; }

code {
  font-family: ui-monospace, "Cascadia Code", Menlo, monospace;
  font-size: 0.9em;
  color: var(--brand-2);
}

.muted { color: var(--muted); }
.err { color: var(--danger); }
.ok { color: var(--ok); }

.banner {
  margin: 0 28px 12px;
  padding: 10px 12px;
  border-radius: 11px;
  font-size: 14px;
}
.banner.err { background: #fdecec; }
.banner.ok { background: #e9f7ef; }

/* Login */
.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(ellipse 60% 50% at 50% 0%, rgba(139, 61, 255, 0.35), transparent 60%),
    linear-gradient(180deg, #0d0818 0%, #1a0f2e 45%, #2a1548 100%);
}
.login-card {
  width: min(440px, 100%);
  background: rgba(255, 255, 255, 0.97);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 22px;
  padding: 28px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.login-mark {
  width: 72px;
  height: 72px;
  object-fit: contain;
  filter: drop-shadow(0 8px 18px rgba(139, 61, 255, 0.35));
}
.login-wordmark {
  width: min(280px, 100%);
  height: auto;
  object-fit: contain;
  /* wordmark is designed on black; invert-ish via mix for light card */
  background: #0d0818;
  border-radius: 14px;
  padding: 12px 16px;
}
.login-card h1 {
  margin: 14px 0 8px;
  font-size: 1.7rem;
  text-align: center;
  letter-spacing: -0.02em;
}
.login-card .muted { text-align: center; }
.login-card label {
  display: block;
  margin: 18px 0 10px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
}
.login-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #faf7ff;
  color: var(--ink);
}
.login-card input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(139, 61, 255, 0.16);
}
.login-card .btn { width: 100%; margin-top: 8px; }

/* Shell */
.shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}
.sidebar {
  background:
    radial-gradient(ellipse 80% 40% at 0% 0%, rgba(139, 61, 255, 0.35), transparent 55%),
    linear-gradient(180deg, var(--sidebar) 0%, var(--sidebar-2) 100%);
  color: #f3e8ff;
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 8px;
}
.sidebar-mark {
  width: 42px;
  height: 42px;
  object-fit: contain;
  flex-shrink: 0;
  filter: drop-shadow(0 4px 12px rgba(192, 132, 252, 0.45));
}
.sidebar-brand-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.sidebar-brand-copy strong {
  font-size: 14px;
  letter-spacing: -0.01em;
}
.sidebar-brand-copy span {
  font-size: 11px;
  color: rgba(233, 213, 255, 0.55);
  font-weight: 600;
}
.sidebar nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav {
  text-align: left;
  border: none;
  background: transparent;
  color: rgba(233, 213, 255, 0.72);
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.nav-icon {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.nav-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.nav:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  transform: scale(1.03);
}
.nav.active {
  background: linear-gradient(135deg, rgba(192, 132, 252, 0.28), rgba(139, 61, 255, 0.22));
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(233, 213, 255, 0.18);
}
.sidebar-logout {
  margin-top: auto;
  color: #e9d5ff;
  border-color: rgba(255, 255, 255, 0.14);
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-start;
  width: 100%;
}
.sidebar-logout .nav-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.sidebar-logout:hover {
  background: rgba(255, 255, 255, 0.08);
  transform: scale(1.03);
}

.main { min-width: 0; }
.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 22px 28px 10px;
  gap: 16px;
}
.topbar-kicker {
  margin: 0 0 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--brand);
}
.topbar h1 {
  margin: 0;
  font-size: 1.45rem;
  letter-spacing: -0.02em;
}

.view { padding: 8px 28px 36px; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 12px; font-size: 1.05rem; }
.card h3 { margin: 0 0 8px; font-size: 0.95rem; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.card-head h2 { margin: 0; }

.hero-card {
  display: grid;
  grid-template-columns: 1.4fr auto;
  gap: 20px;
  align-items: center;
  background:
    radial-gradient(ellipse 50% 80% at 100% 50%, rgba(192, 132, 252, 0.18), transparent 60%),
    var(--panel);
  overflow: hidden;
}
.hero-copy h2 { margin-bottom: 10px; }
.hero-logo {
  width: min(180px, 28vw);
  height: auto;
  object-fit: contain;
  background: #0d0818;
  border-radius: 18px;
  padding: 14px;
  box-shadow: 0 12px 28px rgba(76, 29, 149, 0.2);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.stat::before {
  content: '';
  position: absolute;
  inset: 0 auto 0 0;
  width: 3px;
  background: var(--grad);
}
.stat .label { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat .value {
  font-size: 1.6rem;
  font-weight: 800;
  margin-top: 4px;
  letter-spacing: -0.02em;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tips { margin: 0; padding-left: 1.2em; color: var(--muted); line-height: 1.65; }

.split {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 16px;
  align-items: start;
}
.list-card { position: sticky; top: 12px; }
.tenant-list { display: flex; flex-direction: column; gap: 6px; }
.tenant-item {
  text-align: left;
  border: 1px solid var(--line);
  background: #faf7ff;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}
.tenant-item:hover {
  border-color: color-mix(in srgb, var(--brand) 40%, var(--line));
  background: #f3eaff;
  transform: translateY(-1px);
}
.tenant-item.active {
  border-color: var(--brand);
  background: linear-gradient(135deg, #f3eaff, #ebe0ff);
  box-shadow: 0 0 0 1px rgba(139, 61, 255, 0.15);
}
.tenant-item .name { font-weight: 700; font-size: 14px; }
.tenant-item .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tenant-item.off { opacity: 0.55; }

.form .fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
.form label.span-2 { grid-column: 1 / -1; }
.form label.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  margin-top: 18px;
}
.form input, .form textarea, .form select,
#k-key, #k-extra, #usage-filter {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px 12px;
  color: var(--ink);
  background: #faf7ff;
  font-weight: 400;
}
.form input:focus, .form textarea:focus, .form select:focus,
#k-key:focus, #k-extra:focus, #usage-filter:focus {
  outline: none;
  border-color: var(--brand);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(139, 61, 255, 0.14);
}
.form input[type="color"] {
  padding: 2px;
  height: 40px;
}
.token-row { display: flex; gap: 8px; }
.token-row input { flex: 1; }
.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.embed-box {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.embed-box pre {
  margin: 0 0 8px;
  padding: 14px;
  background: linear-gradient(160deg, #12081f, #1e1033);
  color: #e9d5ff;
  border-radius: 12px;
  font: 12px/1.45 ui-monospace, Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-all;
  border: 1px solid rgba(192, 132, 252, 0.2);
}

.rules {
  margin: 0 0 14px;
  padding: 12px;
  background: #faf7ff;
  border: 1px solid var(--line);
  border-radius: 12px;
  font: 12px/1.5 ui-monospace, Menlo, monospace;
  white-space: pre-wrap;
  max-height: 180px;
  overflow: auto;
}

#view-knowledge label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 12px;
  font-weight: 600;
}

.table-wrap {
  overflow: auto;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
th {
  color: var(--muted);
  font-weight: 700;
  background: #faf7ff;
}
tr:last-child td { border-bottom: none; }

.tenant-detail-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.tenant-detail-head h2 { margin: 0 0 4px; }
.tenant-detail-head .muted { margin: 0; }
.status-pill {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.status-pill.on {
  color: #166534;
  background: #ecfdf5;
  border-color: #a7f3d0;
}
.status-pill.off {
  color: #991b1b;
  background: #fef2f2;
  border-color: #fecaca;
}

.detail-section {
  margin: 18px 0;
}
.detail-section h3 {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 700;
}

.channel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}
.channel-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: #faf7ff;
}
.channel-card .channel-name { font-weight: 700; font-size: 13px; }
.channel-card .channel-state { font-size: 12px; margin-top: 4px; color: var(--muted); }
.channel-card .channel-extra {
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
  word-break: break-all;
}
.channel-card.connected {
  border-color: #a7f3d0;
  background: #f0fdf4;
}
.channel-card.connected .channel-state { color: #166534; font-weight: 600; }
.channel-card.disabled {
  border-color: #fde68a;
  background: #fffbeb;
}
.channel-card.disabled .channel-state { color: #92400e; font-weight: 600; }
.channel-card.disconnected {
  opacity: 0.85;
}

.meter-list { display: flex; flex-direction: column; gap: 10px; }
.meter {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: #faf7ff;
}
.meter.over { border-color: #fecaca; background: #fef2f2; }
.meter-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
}
.meter-top.muted { font-weight: 500; color: var(--muted); }
.meter-bar {
  margin-top: 8px;
  height: 8px;
  border-radius: 999px;
  background: #ede9fe;
  overflow: hidden;
}
.meter-bar i {
  display: block;
  height: 100%;
  background: var(--grad);
  border-radius: inherit;
}
.meter.over .meter-bar i { background: linear-gradient(90deg, #ef4444, #f97316); }

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  margin: 0;
}
.info-grid > div {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #faf7ff;
}
.info-grid dt {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}
.info-grid dd {
  margin: 4px 0 0;
  font-size: 13px;
  font-weight: 600;
  word-break: break-word;
}

.form-hint { margin: 0 0 12px; font-size: 13px; }
.check-inline {
  display: flex !important;
  align-items: center;
  gap: 8px;
  margin-top: 28px;
}

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar-logout { margin-top: 0; width: auto; }
  .split { grid-template-columns: 1fr; }
  .list-card { position: static; }
  .form .fields { grid-template-columns: 1fr; }
  .hero-card { grid-template-columns: 1fr; }
  .hero-logo { width: 140px; justify-self: start; }
}
