:root {
  --bg: #f1f5f9;
  --bg2: #e8eef5;
  --card: #ffffff;
  --line: #e2e8f0;
  --line2: #cbd5e1;
  --text: #0f172a;
  --sub: #475569;
  --mute: #94a3b8;
  --brand: #6366f1;
  --brand-d: #4f46e5;
  --brand-soft: #eef2ff;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --err: #dc2626;
  --err-soft: #fee2e2;
  --warn: #d97706;
  --r: 10px;
  --r-lg: 14px;
  --shadow: 0 1px 3px rgba(15,23,42,.06), 0 4px 16px rgba(15,23,42,.04);
  --shadow-lg: 0 8px 32px rgba(15,23,42,.1);
  --tap: 0.14s ease;
  --top: 56px;
  --tab: 56px;
  --side: 240px;
  --font: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: 15px; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  min-height: 100dvh;
}
.hidden { display: none !important; }
.mobile-only { display: inline-flex; }
.desktop-only { display: none !important; }

.tap { transition: transform var(--tap), opacity var(--tap), background var(--tap); user-select: none; }
.tap:active:not(:disabled) { transform: scale(0.98); opacity: 0.9; }

/* Toast */
.toast-root {
  position: fixed; top: calc(env(safe-area-inset-top, 0) + 12px); left: 50%;
  transform: translateX(-50%); z-index: 9999;
  display: flex; flex-direction: column; gap: 8px;
  width: min(380px, calc(100vw - 24px)); pointer-events: none;
}
.toast {
  padding: 12px 16px; border-radius: var(--r);
  background: var(--text); color: #fff;
  font-size: 0.88rem; text-align: center;
  box-shadow: var(--shadow-lg); animation: toast-in .22s ease;
}
.toast.ok { background: var(--ok); }
.toast.err { background: var(--err); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Inputs & buttons */
.inp-label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--sub); margin: 0 0 6px; }
.inp {
  width: 100%; height: 42px; padding: 0 12px; margin-bottom: 14px;
  border: 1px solid var(--line2); border-radius: var(--r);
  background: #fff; font: inherit; font-size: 0.95rem; color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
textarea.inp { height: auto; padding: 10px 12px; resize: vertical; min-height: 80px; }
.inp:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: 40px; padding: 0 16px; border: none; border-radius: var(--r);
  font: inherit; font-size: 0.9rem; font-weight: 600; cursor: pointer;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-full { width: 100%; }
.btn-main { background: var(--brand); color: #fff; }
.btn-main:hover:not(:disabled) { background: var(--brand-d); }
.btn-lite { background: #fff; color: var(--text); border: 1px solid var(--line2); }
.btn-ghost { background: transparent; color: var(--sub); border: 1px solid var(--line2); }
.btn-danger { background: #fff; color: var(--err); border: 1px solid #fecaca; }

/* Login */
.login-screen {
  min-height: 100dvh;
  display: flex; align-items: stretch; justify-content: center;
  background: linear-gradient(135deg, #eef2ff 0%, #f1f5f9 50%, #f8fafc 100%);
}
.login-wrap {
  display: flex; width: 100%; max-width: 920px;
  margin: auto; min-height: 100dvh;
}
.login-brand {
  display: none; flex: 1; flex-direction: column; justify-content: center;
  padding: 48px; color: var(--text);
}
.login-brand h1 { font-size: 2rem; margin: 16px 0 8px; font-weight: 800; letter-spacing: -.02em; }
.login-brand p { color: var(--sub); font-size: 1rem; max-width: 320px; line-height: 1.6; margin: 0; }
.login-brand ul { margin: 24px 0 0; padding: 0; list-style: none; }
.login-brand li {
  padding: 8px 0; color: var(--sub); font-size: 0.9rem;
  display: flex; align-items: center; gap: 10px;
}
.login-brand li::before { content: "✓"; color: var(--brand); font-weight: 700; }
.login-panel {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 24px 20px;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--card); border-radius: var(--r-lg);
  padding: 32px 28px; box-shadow: var(--shadow-lg);
  border: 1px solid var(--line);
}
.login-head { text-align: center; margin-bottom: 24px; }
.login-head h2 { margin: 12px 0 4px; font-size: 1.35rem; font-weight: 700; }
.login-head .sub { margin: 0; font-size: 0.88rem; color: var(--mute); }
.brand-logo { display: block; object-fit: contain; border-radius: 12px; flex-shrink: 0; }
.brand-logo.lg { width: 64px; height: 64px; margin: 0 auto; }
.brand-logo.sm { width: 32px; height: 32px; }
.brand-logo.xs { width: 28px; height: 28px; border-radius: 8px; }
.topbar-logo { margin-right: 4px; }
.server-status {
  margin-top: 14px; padding: 10px 12px; border-radius: var(--r);
  font-size: 0.8rem; text-align: center; font-weight: 500;
}
.server-status.ok { background: var(--ok-soft); color: #15803d; }
.server-status.off { background: var(--err-soft); color: #b91c1c; }
.server-status.check { background: var(--bg2); color: var(--mute); }
.err { color: var(--err); font-size: 0.85rem; margin: 12px 0 0; text-align: center; }

/* App shell */
.app { display: flex; min-height: 100dvh; background: var(--bg); }
.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(15,23,42,.4); }
.sidebar {
  position: fixed; top: 0; left: 0; z-index: 50;
  width: min(var(--side), 85vw); height: 100dvh;
  background: var(--card); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 16px 12px;
  transform: translateX(-105%); transition: transform .2s ease;
  box-shadow: var(--shadow-lg);
}
.sidebar.open { transform: translateX(0); }
.sidebar-top {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 8px 20px; font-size: 1rem; font-weight: 700;
  border-bottom: 1px solid var(--line); margin-bottom: 12px;
}
.sidebar-menu { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.menu-item {
  height: 42px; padding: 0 12px; border: none; border-radius: var(--r);
  background: transparent; text-align: left;
  font: inherit; font-size: 0.92rem; color: var(--sub); cursor: pointer;
  display: flex; align-items: center; gap: 8px;
}
.menu-item.active { background: var(--brand-soft); color: var(--brand-d); font-weight: 600; }
.sidebar-bottom { border-top: 1px solid var(--line); padding-top: 12px; }
.user-tag { font-size: 0.8rem; color: var(--mute); display: block; margin-bottom: 8px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; width: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--top);
  padding: 0 16px; padding-top: env(safe-area-inset-top, 0);
  display: flex; align-items: center; gap: 8px;
  background: rgba(255,255,255,.9); border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
}
.topbar-title { flex: 1; margin: 0; font-size: 1.05rem; font-weight: 700; }
.topbar-end { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.btn-text {
  height: 34px; padding: 0 10px; border: none; background: transparent;
  font: inherit; font-size: 0.85rem; font-weight: 600;
  color: var(--sub); cursor: pointer; border-radius: var(--r);
}

.page {
  flex: 1; padding: 16px; padding-bottom: calc(var(--tab) + 16px + env(safe-area-inset-bottom, 0));
  max-width: 1200px; margin: 0 auto; width: 100%;
}
.row-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 16px;
}
.row-head h2 { margin: 0; font-size: 1.15rem; font-weight: 700; }

/* Tab bar mobile */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
  height: calc(var(--tab) + env(safe-area-inset-bottom, 0));
  padding-bottom: env(safe-area-inset-bottom, 0);
  display: flex; background: var(--card);
  border-top: 1px solid var(--line); box-shadow: 0 -4px 16px rgba(15,23,42,.06);
}
.tab-item {
  flex: 1; border: none; background: transparent;
  font: inherit; font-size: 0.7rem; font-weight: 600;
  color: var(--mute); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.tab-item.active { color: var(--brand-d); }

/* Cards & blocks */
.block, .feed .row, .card {
  background: var(--card); border-radius: var(--r);
  border: 1px solid var(--line); box-shadow: var(--shadow);
}
.block { padding: 16px; margin-bottom: 14px; }
.block h3 { margin: 0 0 12px; font-size: 0.88rem; font-weight: 700; color: var(--sub); text-transform: uppercase; letter-spacing: .04em; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.feed { display: flex; flex-direction: column; gap: 8px; }
.feed.spaced { margin-top: 12px; }
.feed .row {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px; cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.feed .row:hover:not(.static) { border-color: var(--brand); box-shadow: 0 2px 12px rgba(99,102,241,.08); }
.feed .row.static { cursor: default; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 0.95rem; font-weight: 600; margin: 0 0 4px; }
.row-sub { font-size: 0.82rem; color: var(--sub); margin: 0; }
.row-meta { font-size: 0.75rem; color: var(--mute); margin: 4px 0 0; }
.row-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.row-open { cursor: pointer; flex: 1; min-width: 0; }
.empty { text-align: center; padding: 32px 16px; color: var(--mute); font-size: 0.88rem; }

/* Stats */
.stat-scroll {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;
  margin-bottom: 16px;
}
.stat {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--r); padding: 16px; box-shadow: var(--shadow);
}
.stat .n { font-size: 1.75rem; font-weight: 800; line-height: 1; color: var(--brand-d); }
.stat .l { font-size: 0.78rem; color: var(--sub); margin-top: 4px; font-weight: 500; }

/* Tags */
.tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.tag {
  font-size: 0.65rem; font-weight: 700; padding: 3px 8px;
  border-radius: 999px; text-transform: uppercase; letter-spacing: .03em;
}
.tag.on { background: var(--ok-soft); color: #15803d; }
.tag.off { background: #f1f5f9; color: var(--mute); }
.tag.join { background: var(--brand-soft); color: var(--brand-d); }
.tag.shield { background: var(--err-soft); color: #b91c1c; }
.tag.flood { background: #fef3c7; color: #b45309; }

.inp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; margin-bottom: 10px; }
.inp-grid > div:last-child:nth-child(odd) { grid-column: 1 / -1; }
.log-block {
  background: #1e293b; color: #e2e8f0; border-radius: var(--r);
  padding: 14px; font-size: 0.75rem; line-height: 1.5;
  font-family: ui-monospace, Consolas, monospace;
  max-height: 55dvh; overflow: auto; white-space: pre-wrap; margin: 0;
}
.sep { border: none; border-top: 1px solid var(--line); margin: 16px 0; }
.hint { font-size: 0.82rem; color: var(--sub); margin: 10px 0 0; }
.btn-pair { display: flex; gap: 10px; margin-bottom: 10px; }
.btn-pair .btn { flex: 1; }

/* Modal */
.sheet { border: none; padding: 0; margin: 0; max-width: 100%; max-height: 100%; width: 100%; height: 100%; background: transparent; }
.sheet::backdrop { background: rgba(15,23,42,.45); }
.sheet-panel {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 94dvh;
  background: var(--card); border-radius: 16px 16px 0 0;
  display: flex; flex-direction: column; box-shadow: var(--shadow-lg);
  animation: sheet-up .25s ease;
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 16px; border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.sheet-head h3 { margin: 0; flex: 1; font-size: 1rem; text-align: center; font-weight: 700; }
.sheet-spacer { width: 60px; }
.back-btn {
  height: 36px; padding: 0 8px; border: none; background: transparent;
  font: inherit; font-size: 0.9rem; font-weight: 600;
  color: var(--brand-d); cursor: pointer; border-radius: var(--r);
}
.seg-tabs {
  display: flex; gap: 6px; padding: 10px 14px 0;
  overflow-x: auto; flex-shrink: 0; scrollbar-width: none;
}
.seg-tabs::-webkit-scrollbar { display: none; }
.seg {
  flex-shrink: 0; height: 32px; padding: 0 14px;
  border: 1px solid var(--line2); border-radius: 999px;
  background: #fff; font: inherit; font-size: 0.78rem;
  font-weight: 600; color: var(--sub); cursor: pointer;
}
.seg.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.sheet-body {
  flex: 1; overflow-y: auto;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom, 0));
}
.tab-panel.hidden { display: none; }

.detail-card {
  background: var(--bg); border-radius: var(--r);
  padding: 14px; margin-bottom: 12px; font-size: 0.88rem;
  border: 1px solid var(--line);
}
.detail-card p { margin: 0 0 6px; }

.action-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.act {
  height: 44px; border: none; border-radius: var(--r);
  font: inherit; font-size: 0.88rem; font-weight: 700; cursor: pointer; color: #fff;
}
.act.on { background: var(--ok); }
.act.off { background: var(--err); }
.act:not(.on):not(.off) { background: #fff; color: var(--text); border: 1px solid var(--line2); }
.danger-zone { margin-top: 16px; }

.mini-row { display: flex; gap: 6px; flex-shrink: 0; }
.mini-btn {
  height: 32px; padding: 0 12px; border: 1px solid var(--line2);
  border-radius: 8px; background: #fff; font: inherit; font-size: 0.78rem;
  font-weight: 600; cursor: pointer; color: var(--text);
}
.mini-btn.danger { color: var(--err); border-color: #fecaca; }

/* Alert & events */
.alert-banner {
  margin: 0 0 14px; padding: 12px 16px; border-radius: var(--r);
  background: var(--err-soft); border: 1px solid #fecaca;
  color: #b91c1c; font-size: 0.9rem; font-weight: 500;
}
.live-feed .event-row { border-left: 3px solid var(--line); padding-left: 12px; }
.live-feed .event-row.critical, .live-feed .event-row.error { border-left-color: var(--err); }
.live-feed .event-row.success { border-left-color: var(--ok); }
.live-feed .event-row.info { border-left-color: var(--brand); }
.event-type { font-size: 0.72rem; font-weight: 700; color: var(--mute); text-transform: uppercase; }

.triplet-card pre {
  margin: 6px 0 0; padding: 10px; background: #f8fafc;
  border-radius: 8px; font-size: 0.78rem; overflow-x: auto;
  border: 1px solid var(--line); color: var(--sub);
}
.stack-ok { color: var(--ok); font-weight: 600; }
.stack-off { color: var(--err); font-weight: 600; }
.stack-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 10px; }
.stack-item {
  padding: 12px; border-radius: var(--r); border: 1px solid var(--line);
  background: #f8fafc; font-size: 0.85rem;
}
.stack-item.ok { border-color: #86efac; background: var(--ok-soft); }
.stack-item.fail { border-color: #fecaca; background: var(--err-soft); }

.iso-table-wrap { overflow-x: auto; margin-top: 4px; border-radius: var(--r); border: 1px solid var(--line); }
.iso-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; background: #fff; }
.iso-table th, .iso-table td { padding: 10px 14px; border-bottom: 1px solid var(--line); text-align: left; }
.iso-table th { background: #f8fafc; color: var(--mute); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; }
.iso-table tr:last-child td { border-bottom: none; }
.iso-table tr:hover td { background: #fafbff; }
.iso-mono { font-family: ui-monospace, Consolas, monospace; font-size: 0.75rem; color: var(--sub); }

/* Accounts grid desktop */
.accounts-grid {
  display: flex; flex-direction: column; gap: 8px;
}

@media (max-width: 1023px) {
  .sidebar, .backdrop { display: none !important; }
}

@media (min-width: 640px) {
  .stat-scroll { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 768px) {
  .stack { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .stack .block:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .inp-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1024px) {
  html { font-size: 16px; }
  :root { --top: 60px; --side: 252px; }

  .mobile-only { display: none !important; }
  .desktop-only { display: block !important; }
  .tabbar { display: none; }

  .login-wrap { min-height: auto; margin: 48px auto; }
  .login-brand { display: flex; }
  .login-panel { padding: 48px; }

  .sidebar {
    display: flex !important; position: sticky; top: 0;
    transform: none; width: var(--side); height: 100dvh;
    flex-shrink: 0; padding: 20px 16px;
  }

  .page { padding: 24px 32px 32px; max-width: 1280px; }
  .accounts-grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 12px;
  }
  .accounts-grid .account-row { flex-direction: column; align-items: stretch; }
  .accounts-grid .row-actions { justify-content: flex-end; margin-top: 8px; }

  .sheet-panel {
    position: relative; max-width: 680px; margin: auto;
    border-radius: var(--r-lg); max-height: 90dvh;
  }
  .sheet { display: flex; align-items: center; justify-content: center; }
}

@media (min-width: 1400px) {
  .page { max-width: 1400px; }
}
