:root {
  --bg: var(--tg-theme-bg-color, #0d1218);
  --surface: var(--tg-theme-secondary-bg-color, #151c24);
  --surface-2: #1b2530;
  --surface-3: #222e3b;
  --text: var(--tg-theme-text-color, #f4f7fa);
  --muted: var(--tg-theme-hint-color, #8d9baa);
  --line: rgba(255, 255, 255, 0.08);
  --accent: #43b968;
  --accent-strong: #2f9d54;
  --danger: #e45858;
  --warning: #e7aa49;
  --radius: 18px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 80% -10%, rgba(67, 185, 104, 0.14), transparent 34rem),
    var(--bg);
}

button, input, select, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app-shell { min-height: 100vh; }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 252px;
  display: flex;
  flex-direction: column;
  padding: 28px 18px;
  background: rgba(15, 21, 28, 0.92);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(22px);
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 13px; padding: 0 10px 28px; }
.brand-mark {
  width: 43px; height: 43px; border-radius: 14px; display: grid; place-items: center;
  font-weight: 850; font-size: 22px; color: #07130b;
  background: linear-gradient(145deg, #66d986, #35a85a);
  box-shadow: 0 10px 26px rgba(67, 185, 104, 0.26);
}
.brand strong { display: block; font-size: 19px; letter-spacing: 0.01em; }
.brand span { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }

.nav { display: grid; gap: 7px; }
.nav-button {
  width: 100%; border: 0; border-radius: 13px; padding: 12px 13px;
  display: flex; align-items: center; gap: 11px; color: var(--muted);
  background: transparent; cursor: pointer; text-align: left; font-weight: 650;
}
.nav-button:hover { background: var(--surface); color: var(--text); }
.nav-button.active { background: rgba(67, 185, 104, 0.14); color: #75df94; }
.nav-icon { width: 22px; text-align: center; font-size: 18px; }

.sidebar-foot {
  margin-top: auto; padding: 14px 11px; display: flex; align-items: center; gap: 9px;
  color: var(--muted); font-size: 12px; border-top: 1px solid var(--line);
}
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warning); }
.status-dot.on { background: var(--accent); box-shadow: 0 0 0 5px rgba(67, 185, 104, 0.1); }
.status-dot.off { background: var(--danger); box-shadow: 0 0 0 5px rgba(228, 88, 88, 0.1); }

.main { margin-left: 252px; min-height: 100vh; padding: 0 34px 48px; }
.topbar {
  min-height: 106px; display: flex; align-items: center; justify-content: space-between;
  gap: 18px; position: sticky; top: 0; z-index: 8;
  background: linear-gradient(var(--bg) 72%, transparent);
}
.topbar-title { display: grid; align-items: center; }
.topbar-back {
  display: none; width: fit-content; margin: 0 0 8px; padding: 0; border: 0;
  color: var(--muted); background: transparent; cursor: pointer; font-size: 12px; font-weight: 750;
}
.topbar-back.visible { display: inline-flex; align-items: center; gap: 6px; }
.topbar-back:hover { color: var(--text); }
.eyebrow { margin: 0 0 5px; color: var(--accent); text-transform: uppercase; letter-spacing: .12em; font-size: 10px; font-weight: 800; }
h1 { margin: 0; font-size: clamp(25px, 3vw, 34px); letter-spacing: -0.035em; }
.admin-chip { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 9px 13px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font-size: 12px; }
.content { max-width: 1240px; margin: 0 auto; }

.page-intro {
  display: flex; align-items: end; justify-content: space-between; gap: 24px;
  margin: 2px 0 22px; padding: 23px 25px; border: 1px solid var(--line); border-radius: 22px;
  background:
    radial-gradient(circle at 94% 0%, rgba(67, 185, 104, .13), transparent 18rem),
    linear-gradient(145deg, rgba(28, 39, 50, .98), rgba(17, 24, 31, .98));
}
.page-intro-eyebrow { color: #68d889; text-transform: uppercase; letter-spacing: .13em; font-size: 10px; font-weight: 850; }
.page-intro h2 { margin: 7px 0 0; font-size: clamp(25px, 4vw, 38px); letter-spacing: -.045em; }
.page-intro p { max-width: 720px; margin: 8px 0 0; color: var(--muted); font-size: 13px; line-height: 1.55; }

.overview-hero {
  padding: 25px; border: 1px solid rgba(67, 185, 104, .19); border-radius: 24px;
  background:
    radial-gradient(circle at 80% -30%, rgba(67, 185, 104, .28), transparent 34rem),
    linear-gradient(155deg, #1b2731, #121a22 70%);
  box-shadow: var(--shadow);
}
.overview-head { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 17px; }
.system-pill { display: inline-flex; align-items: center; gap: 7px; padding: 6px 9px; border-radius: 999px; font-size: 10px; font-weight: 800; }
.system-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 12%, transparent); }
.system-pill.online { color: #71df92; background: rgba(67, 185, 104, .11); }
.system-pill.offline { color: #ff8585; background: rgba(228, 88, 88, .12); }
.overview-title { margin-top: 9px; font-size: clamp(22px, 3vw, 34px); font-weight: 820; letter-spacing: -.045em; }
.overview-hero > .segmented { width: fit-content; max-width: 100%; margin-bottom: 18px; background: rgba(8, 13, 18, .38); }
.overview-grid { display: grid; grid-template-columns: 1.25fr 1fr 1fr; gap: 11px; }
.overview-stat { min-height: 147px; display: flex; flex-direction: column; justify-content: flex-end; padding: 17px; border: 1px solid var(--line); border-radius: 17px; background: rgba(10, 15, 20, .38); }
.overview-stat.featured { border-color: rgba(67, 185, 104, .32); background: linear-gradient(145deg, rgba(67, 185, 104, .16), rgba(10, 15, 20, .38)); }
.overview-stat span { color: var(--muted); font-size: 12px; }
.overview-stat strong { margin-top: 10px; font-size: clamp(23px, 3vw, 35px); letter-spacing: -.045em; overflow-wrap: anywhere; }
.overview-stat.featured strong { color: #6de18e; }
.overview-stat small { margin-top: 7px; color: var(--muted); font-size: 11px; }
.compact-metrics .card { display: flex; flex-direction: column; min-height: 164px; }
.compact-metrics .button { width: fit-content; margin-top: auto; }

.toolbar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.toolbar-group { display: flex; gap: 8px; flex-wrap: wrap; }
.segmented { display: flex; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface); overflow-x: auto; }
.segmented button { border: 0; white-space: nowrap; border-radius: 9px; padding: 8px 11px; background: transparent; color: var(--muted); cursor: pointer; }
.segmented button.active { color: var(--text); background: var(--surface-3); }

.grid { display: grid; gap: 14px; }
.metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-bottom: 20px; }
.two-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.three-columns { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card {
  background: linear-gradient(155deg, rgba(29, 39, 50, .96), rgba(20, 28, 36, .98));
  border: 1px solid var(--line); border-radius: var(--radius); padding: 18px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.025) inset;
}
.card.clickable { cursor: pointer; transition: transform .16s ease, border-color .16s ease; }
.card.clickable:hover { transform: translateY(-2px); border-color: rgba(67, 185, 104, .34); }
.metric-label, .card-label { color: var(--muted); font-size: 12px; }
.metric-value { margin-top: 9px; font-size: clamp(21px, 3vw, 30px); font-weight: 780; letter-spacing: -0.035em; }
.metric-note { margin-top: 6px; color: var(--muted); font-size: 12px; }
.positive { color: #6ada8a; }
.danger-text { color: #ff7979; }
.warning-text { color: #f0bd67; }

.section-title { display: flex; align-items: end; justify-content: space-between; gap: 15px; margin: 26px 0 12px; }
.section-title h2 { margin: 0; font-size: 18px; }
.section-title p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }

.list { display: grid; gap: 9px; }
.list-row {
  display: grid; grid-template-columns: minmax(180px, 1.5fr) repeat(3, minmax(110px, .7fr)) auto;
  align-items: center; gap: 14px; min-height: 67px; padding: 13px 15px;
  border: 1px solid var(--line); border-radius: 15px; background: var(--surface); cursor: pointer;
}
.list-row:hover { border-color: rgba(67, 185, 104, .28); }
.list-primary strong { display: block; font-size: 14px; }
.list-primary span, .list-cell span { display: block; color: var(--muted); font-size: 11px; margin-top: 4px; }
.list-cell { font-size: 13px; }
.chevron { color: var(--muted); font-size: 20px; }

.finance-filters { width: fit-content; max-width: 100%; margin-bottom: 15px; }
.finance-summary { margin-bottom: 8px; }
.finance-row { grid-template-columns: auto minmax(200px, 1.5fr) minmax(145px, .7fr) auto; }
.finance-icon { width: 37px; height: 37px; display: grid; place-items: center; border-radius: 12px; font-size: 18px; font-weight: 850; }
.finance-icon.profit { color: #6de18e; background: rgba(67, 185, 104, .13); }
.finance-icon.payout { color: #c8d4e0; background: rgba(141, 155, 170, .12); }

.badge { display: inline-flex; align-items: center; gap: 6px; width: fit-content; border-radius: 999px; padding: 5px 8px; font-size: 10px; font-weight: 750; background: rgba(141, 155, 170, .12); color: var(--muted); }
.badge.paid, .badge.active { background: rgba(67, 185, 104, .14); color: #75df94; }
.badge.failed, .badge.inactive { background: rgba(228, 88, 88, .14); color: #ff8585; }
.badge.awaiting_payment, .badge.processing { background: rgba(231, 170, 73, .13); color: #f0bd67; }

.button {
  border: 0; border-radius: 12px; padding: 10px 14px; color: var(--text); background: var(--surface-3);
  font-weight: 700; cursor: pointer; transition: filter .15s ease, transform .15s ease;
}
.button:hover { filter: brightness(1.08); }
.button:active { transform: scale(.98); }
.button.primary { background: var(--accent-strong); color: white; }
.button.danger { background: rgba(228, 88, 88, .16); color: #ff8585; }
.button.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); }
.button.small { padding: 7px 10px; border-radius: 9px; font-size: 12px; }
.button:disabled { opacity: .45; cursor: wait; }

.input, .select, .textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px;
  color: var(--text); background: var(--surface); outline: none;
}
.input:focus, .select:focus, .textarea:focus { border-color: rgba(67, 185, 104, .65); box-shadow: 0 0 0 3px rgba(67, 185, 104, .1); }
.textarea { min-height: 130px; resize: vertical; }
.field { display: grid; gap: 7px; }
.field label { color: var(--muted); font-size: 12px; font-weight: 650; }
.form-grid { display: grid; gap: 13px; }
.search { max-width: 360px; }

.empty { padding: 34px 20px; text-align: center; border: 1px dashed var(--line); border-radius: var(--radius); color: var(--muted); }
.loading-card { display: flex; align-items: center; gap: 10px; padding: 28px; border-radius: var(--radius); background: var(--surface); color: var(--muted); }
.spinner { width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.12); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.modal { width: min(560px, calc(100vw - 28px)); max-height: calc(100vh - 40px); border: 0; padding: 0; border-radius: 22px; color: var(--text); background: transparent; box-shadow: var(--shadow); }
.modal::backdrop { background: rgba(0, 0, 0, .64); backdrop-filter: blur(5px); }
.modal-card { margin: 0; background: var(--surface-2); border: 1px solid var(--line); border-radius: 22px; overflow: hidden; }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 19px; }
.modal-back { margin: 0 0 7px; padding: 0; border: 0; color: var(--muted); background: transparent; cursor: pointer; font-size: 11px; font-weight: 750; }
.modal-back:hover { color: var(--text); }
.icon-button { width: 34px; height: 34px; border: 0; border-radius: 10px; color: var(--muted); background: var(--surface-3); font-size: 22px; cursor: pointer; }
.modal-body { max-height: 65vh; overflow: auto; padding: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; padding: 15px 20px 20px; }
.detail-stack { display: grid; gap: 13px; }
.detail-row { display: flex; justify-content: space-between; align-items: start; gap: 18px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.detail-row:last-child { border: 0; padding-bottom: 0; }
.detail-row span { color: var(--muted); font-size: 12px; }
.detail-row strong { text-align: right; font-size: 13px; overflow-wrap: anywhere; }
.action-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 17px; }

.management-grid { gap: 11px; }
.management-card { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 14px; min-height: 118px; }
.management-icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 14px; color: #75df94; background: rgba(67, 185, 104, .12); font-size: 19px; font-weight: 850; }
.management-value { margin-top: 5px; font-size: clamp(17px, 2vw, 22px); font-weight: 780; letter-spacing: -.025em; }
.management-arrow { color: var(--muted); font-size: 23px; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 120px); z-index: 100; max-width: min(420px, calc(100vw - 30px)); padding: 12px 16px; border-radius: 13px; background: #25313d; color: white; box-shadow: var(--shadow); opacity: 0; transition: .24s ease; text-align: center; font-size: 13px; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }
.toast.error { background: #6c2e32; }
.bottom-nav { display: none; }

@media (max-width: 980px) {
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .three-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .list-row { grid-template-columns: minmax(160px, 1.4fr) repeat(2, minmax(100px, .7fr)) auto; }
  .list-row .desktop-only { display: none; }
  .overview-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .overview-stat.featured { grid-column: span 2; }
}

@media (max-width: 720px) {
  .sidebar { display: none; }
  .main { margin: 0; padding: 0 14px calc(92px + env(safe-area-inset-bottom)); }
  .topbar { min-height: 88px; padding-top: env(safe-area-inset-top); }
  .topbar-back span { display: none; }
  .admin-chip { display: none; }
  .metrics, .two-columns, .three-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
  .card { padding: 15px; border-radius: 16px; }
  .metric-value { font-size: 21px; }
  .page-intro { margin-bottom: 15px; padding: 18px; border-radius: 18px; }
  .page-intro p { font-size: 12px; }
  .overview-hero { padding: 16px; border-radius: 20px; }
  .overview-head { align-items: stretch; flex-direction: column; }
  .overview-head .button { width: 100%; }
  .overview-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .overview-stat { min-height: 132px; padding: 13px; }
  .overview-stat.featured { grid-column: span 2; }
  .overview-stat strong { font-size: 23px; }
  .compact-metrics .card { min-height: 145px; }
  .bottom-nav {
    position: fixed; display: grid; grid-template-columns: repeat(5, 1fr); inset: auto 0 0;
    padding: 7px 7px calc(7px + env(safe-area-inset-bottom)); z-index: 20;
    background: rgba(18, 25, 33, .94); border-top: 1px solid var(--line); backdrop-filter: blur(20px);
  }
  .bottom-nav .nav-button { display: grid; justify-items: center; gap: 3px; padding: 6px 2px; font-size: 9px; border-radius: 10px; }
  .bottom-nav .nav-icon { font-size: 18px; }
  .list-row { grid-template-columns: 1fr auto; gap: 8px; }
  .list-row .list-cell { display: none; }
  .finance-row { grid-template-columns: auto minmax(0, 1fr) auto; }
  .finance-row .list-cell { display: none; }
  .toolbar { align-items: stretch; }
  .toolbar .search { max-width: none; flex: 1 0 100%; }
  .section-title { align-items: center; }
  .modal { width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 24px 24px 0 0; }
  .modal-card { border-radius: 24px 24px 0 0; padding-bottom: env(safe-area-inset-bottom); }
  .action-grid { grid-template-columns: 1fr; }
}

@media (max-width: 390px) {
  .metrics, .two-columns, .three-columns { grid-template-columns: 1fr; }
  .overview-grid { grid-template-columns: 1fr; }
  .overview-stat.featured { grid-column: span 1; }
  .management-card { grid-template-columns: auto minmax(0, 1fr) auto; }
}
