:root {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --bg-card: #1c2128;
  --border: #30363d;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --green: #3fb950;
  --green-bg: rgba(63, 185, 80, 0.12);
  --red: #f85149;
  --red-bg: rgba(248, 81, 73, 0.12);
  --yellow: #d29922;
  --yellow-bg: rgba(210, 153, 34, 0.12);
  --orange: #f0a030;
  --orange-bg: rgba(240, 160, 48, 0.12);
  --purple: #bc8cff;
  --cyan: #39d2c0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --sidebar-w: 220px;
  --mobile-header-h: 50px;
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; overflow-x: hidden; }

body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

/* ═══ Layout ═══ */
.app { display: flex; min-height: 100vh; min-height: 100dvh; }

/* ── Sidebar (desktop) ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  height: 100dvh;
  z-index: 200;
  transition: transform 0.25s cubic-bezier(.4,0,.2,1);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sidebar-logo {
  padding: 0 16px 14px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-logo h1 {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.mode-badge {
  display: inline-block;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  text-transform: uppercase;
  white-space: nowrap;
}
.mode-badge.dry { background: var(--yellow-bg); color: var(--yellow); }
.mode-badge.live { background: var(--red-bg); color: var(--red); }

.sidebar nav { flex: 1; padding: 4px 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 16px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s;
  border-left: 3px solid transparent;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.nav-item:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.nav-item.active { color: var(--accent); background: rgba(88,166,255,0.08); border-left-color: var(--accent); }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ── Main content ── */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-w);
  padding: 20px 24px;
  min-height: 100vh;
  min-height: 100dvh;
  width: calc(100% - var(--sidebar-w));
  overflow-x: hidden;
}

/* ── Mobile header ── */
.mobile-header {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--mobile-header-h);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  z-index: 150;
  align-items: center;
  padding: 0 12px;
  gap: 10px;
}

.hamburger {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 24px;
  cursor: pointer;
  padding: 6px 10px;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  border-radius: 6px;
}
.hamburger:active { background: var(--bg-tertiary); }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 199;
  -webkit-tap-highlight-color: transparent;
}

/* ═══ Top bar ═══ */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 8px;
  min-height: 36px;
}
.top-bar h2 { font-size: 20px; font-weight: 600; white-space: nowrap; }

/* Action buttons group */
.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.connection-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
}
.connection-dot.on { background: var(--green); box-shadow: 0 0 6px var(--green); }
.connection-dot.off { background: var(--red); }

/* ═══ Cards ═══ */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 8px;
  flex-wrap: wrap;
}

.card-header h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* ═══ Stat grid ═══ */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-width: 0;
}

.stat-card .label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 2px;
}

.stat-card .value {
  font-size: 20px;
  font-weight: 700;
  font-family: var(--mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stat-card .value.green { color: var(--green); }
.stat-card .value.red { color: var(--red); }
.stat-card .value.blue { color: var(--accent); }
.stat-card .value.yellow { color: var(--yellow); }

.stat-card .sub {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ═══ Grid layouts ═══ */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

/* ═══ Data grid (for chain detail cards) ═══ */
.data-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.data-grid .data-item .data-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}
.data-grid .data-item .data-value {
  font-weight: 600;
  font-size: 14px;
}

/* ═══ Tables ═══ */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th {
  text-align: left;
  padding: 8px 10px;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  white-space: nowrap;
  position: sticky;
  top: 0;
  background: var(--bg-card);
}

td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
}

tr:hover td { background: rgba(255,255,255,0.02); }

.mono { font-family: var(--mono); font-size: 12px; }

/* ═══ Badges ═══ */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  vertical-align: middle;
}
.badge.green { background: var(--green-bg); color: var(--green); }
.badge.red { background: var(--red-bg); color: var(--red); }
.badge.yellow { background: var(--yellow-bg); color: var(--yellow); }
.badge.orange { background: var(--orange-bg); color: var(--orange); }
.badge.blue { background: rgba(88,166,255,0.12); color: var(--accent); }

/* ═══ Toggle ═══ */
.toggle {
  position: relative;
  width: 40px;
  height: 22px;
  cursor: pointer;
  display: inline-block;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle .slider {
  position: absolute;
  inset: 0;
  background: var(--bg-tertiary);
  border-radius: 11px;
  border: 1px solid var(--border);
  transition: 0.2s;
}
.toggle .slider::before {
  content: '';
  position: absolute;
  width: 16px; height: 16px;
  left: 2px; bottom: 2px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: 0.2s;
}
.toggle input:checked + .slider { background: var(--accent); border-color: var(--accent); }
.toggle input:checked + .slider::before { transform: translateX(18px); background: #fff; }

/* ═══ Buttons ═══ */
.btn {
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  transition: 0.15s;
  font-family: var(--font);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
  user-select: none;
}
.btn:hover { background: var(--border); }
.btn:active { transform: scale(0.96); opacity: 0.9; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger:hover { opacity: 0.8; }
.btn.sm { padding: 5px 10px; font-size: 12px; }

/* ═══ Forms ═══ */
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 16px;
  font-family: var(--mono);
  -webkit-appearance: none;
  appearance: none;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(88, 166, 255, 0.2);
}

/* ═══ Chain list ═══ */
.chain-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.chain-row:last-child { border-bottom: none; }

.chain-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.chain-icon {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  flex-shrink: 0;
}

.chain-detail {
  min-width: 0;
  flex: 1;
}
.chain-detail .chain-name {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chain-detail .chain-sub {
  font-size: 11px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chain-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.chain-right .chain-bal {
  text-align: right;
}
.chain-right .chain-bal-main {
  font-family: var(--mono);
  font-size: 12px;
}
.chain-right .chain-bal-sub {
  font-size: 10px;
}

/* ═══ Pages ═══ */
.page { display: none; }
.page.active { display: block; }

/* ═══ Scrollbar ═══ */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
.pulse { animation: pulse 2s infinite; }

.empty-state { text-align: center; padding: 24px 12px; color: var(--text-muted); font-size: 13px; }

.setup-notice {
  background: linear-gradient(135deg, rgba(88,166,255,0.08), rgba(188,140,255,0.08));
  border-color: var(--accent);
}
.form-group small { font-size: 11px; line-height: 1.4; }
.form-group input[type="password"] { letter-spacing: 1px; }

/* ═══ Toast ═══ */
#toast {
  max-width: calc(100vw - 32px);
  word-break: break-word;
}

/* ═══ Log Viewer ═══ */
.log-viewer {
  flex: 1;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.7;
  padding: 8px 12px;
  background: var(--bg-primary);
  min-height: 300px;
  max-height: calc(100vh - 220px);
}
.log-line {
  white-space: pre-wrap;
  word-break: break-all;
  padding: 1px 0;
  border-bottom: 1px solid rgba(48, 54, 61, 0.3);
}
.log-line:hover { background: rgba(88, 166, 255, 0.04); }
.log-ts { color: var(--text-muted); }
.log-level { font-weight: 600; display: inline-block; min-width: 72px; }
.log-loc { color: var(--purple); font-size: 11px; }
.log-msg { color: var(--text-primary); }
#page-logs { flex-direction: column; }
#page-logs.active { display: flex; }
#page-logs .card { min-height: 0; }

/* ═══════════ RESPONSIVE ═══════════ */

/* ── Tablet ── */
@media (max-width: 1024px) {
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .main-content { padding: 16px; }
}

/* ── Mobile ── */
@media (max-width: 768px) {
  :root { --sidebar-w: 270px; }

  .mobile-header { display: flex; }

  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,0.4); }
  .sidebar-overlay.open { display: block; }

  .main-content {
    margin-left: 0;
    width: 100%;
    padding: calc(var(--mobile-header-h) + 12px) 10px 20px;
  }

  .top-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .top-bar h2 { font-size: 18px; }

  .top-actions { justify-content: flex-start; }

  .stat-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }
  .stat-card { padding: 10px 8px; }
  .stat-card .value { font-size: 16px; }
  .stat-card .label { font-size: 9px; }
  .stat-card .sub { font-size: 9px; }

  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: 10px; }

  .card { padding: 12px 10px; }

  /* Chain rows stack on mobile */
  .chain-row { flex-wrap: wrap; }
  .chain-right { width: 100%; justify-content: space-between; padding-left: 42px; }
  .chain-right .chain-bal { text-align: left; }

  .data-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .data-grid .data-item .data-value { font-size: 13px; }

  table { font-size: 12px; }
  th, td { padding: 6px 8px; }

  .btn.sm { padding: 6px 10px; font-size: 11px; }

  .nav-item { padding: 13px 16px; font-size: 15px; }
}

/* ── Small phone ── */
@media (max-width: 400px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .stat-card .value { font-size: 14px; }
  .main-content { padding: calc(var(--mobile-header-h) + 8px) 8px 16px; }
  .data-grid { grid-template-columns: 1fr 1fr; gap: 6px; }
  .chain-right { padding-left: 0; }
  .top-actions .btn.sm { padding: 5px 8px; font-size: 10px; }
}
