:root {
  --bg: var(--tg-theme-bg-color, #0f172a);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #162033);
  --section-bg: var(--tg-theme-section-bg-color, #1b2840);
  --text: var(--tg-theme-text-color, #f1f5f9);
  --hint: var(--tg-theme-hint-color, #94a3b8);
  --link: var(--tg-theme-link-color, #38bdf8);
  --accent: var(--tg-theme-button-color, #2563eb);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #ef4444);
  --success: #10b981;
  --warning: #f59e0b;
  --border: rgba(148, 163, 184, 0.16);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  --radius: 16px;
  --radius-sm: 11px;
  --font: "IBM Plex Sans Arabic", "Tajawal", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  direction: rtl;
  overscroll-behavior-y: none;
}

.hidden {
  display: none !important;
}

.mt-6 { margin-top: 6px; }
.mt-10 { margin-top: 10px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }
.mb-10 { margin-bottom: 10px; }

.muted {
  color: var(--hint);
  font-size: 13px;
  margin: 2px 0 0;
}

.muted-sm {
  color: var(--hint);
  font-size: 12px;
  margin: 0;
}

/* Center Screens (Guard, Loading, Error) */
.screen-center {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.guard-card {
  width: 100%;
  max-width: 400px;
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 28px 22px;
  text-align: center;
  box-shadow: var(--shadow);
}

.guard-icon {
  font-size: 46px;
  margin-bottom: 12px;
}

.guard-card h1,
.guard-card h2 {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 700;
}

.guard-card p {
  margin: 0 0 12px;
  color: var(--hint);
  font-size: 14px;
}

.guard-sub {
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text) !important;
}

.guard-card code {
  background: rgba(56, 189, 248, 0.14);
  color: var(--link);
  padding: 2px 6px;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.loader-box {
  text-align: center;
  color: var(--hint);
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 14px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* App Shell */
.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(74px + env(safe-area-inset-bottom, 0px));
}

/* Sticky Top Header */
.top-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: color-mix(in srgb, var(--secondary-bg) 92%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  padding: 12px 14px 10px;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.user-block {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.avatar-badge {
  width: 40px;
  height: 40px;
  border-radius: 13px;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 17px;
  flex-shrink: 0;
}

.user-meta {
  min-width: 0;
}

.user-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.user-name {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 175px;
}

.role-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.2);
  color: var(--link);
  border: 1px solid rgba(56, 189, 248, 0.28);
}

.role-badge.owner {
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
  border-color: rgba(251, 191, 36, 0.35);
}

.scope-summary {
  font-size: 12px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--section-bg);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.icon-btn:active {
  transform: scale(0.92);
}

.group-bar {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.group-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  white-space: nowrap;
}

.select-wrap {
  flex: 1;
  min-width: 0;
}

.group-select {
  width: 100%;
  background: var(--section-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 8px 12px;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  outline: none;
}

/* Main Content */
.main-content {
  padding: 14px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.tab-panel {
  display: none;
  animation: fadeIn 0.18s ease;
}

.tab-panel.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.section-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.pill-badge {
  background: rgba(37, 99, 235, 0.18);
  color: var(--link);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

/* Cards */
.card {
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

.card h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

.card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-info {
  flex: 1;
  min-width: 0;
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 12px 0;
}

/* 14 Locks Grid */
.locks-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin-bottom: 12px;
}

.lock-card {
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.lock-card.active {
  border-color: rgba(239, 68, 68, 0.45);
  background: color-mix(in srgb, var(--section-bg) 88%, #ef4444 12%);
}

.lock-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.lock-ico {
  font-size: 18px;
  flex-shrink: 0;
}

.lock-label {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: rgba(148, 163, 184, 0.34);
  border-radius: 999px;
  transition: background 0.2s ease;
}

.slider::before {
  content: "";
  position: absolute;
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background: #ffffff;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s ease;
}

.switch input:checked + .slider {
  background: var(--accent);
}

.switch input:checked + .slider::before {
  transform: translateX(20px);
}

.switch input:disabled + .slider {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Controls & Steppers */
.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.control-row:last-child {
  margin-bottom: 0;
}

.control-block {
  margin-top: 8px;
}

.control-title {
  font-size: 14px;
  font-weight: 600;
}

.stepper {
  display: inline-flex;
  align-items: center;
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: 11px;
  overflow: hidden;
  flex-shrink: 0;
}

.step-btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.step-btn:active {
  background: rgba(255, 255, 255, 0.08);
}

.step-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.step-val {
  min-width: 44px;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Segmented Controls */
.segmented {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(72px, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.seg-btn {
  border: 1px solid var(--border);
  background: var(--secondary-bg);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 6px;
  font-family: var(--font);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.16s ease;
}

.seg-btn.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.seg-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Welcome Editor */
.label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.var-chips {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0;
}

.chip-btn {
  border: 1px solid rgba(56, 189, 248, 0.35);
  background: rgba(56, 189, 248, 0.12);
  color: var(--link);
  border-radius: 999px;
  padding: 4px 10px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 12px;
  cursor: pointer;
}

.chip-btn:active {
  transform: scale(0.95);
}

.textarea,
.input {
  width: 100%;
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: var(--font);
  font-size: 14px;
  outline: none;
  transition: border-color 0.16s ease;
}

.textarea:focus,
.input:focus {
  border-color: var(--accent);
}

.textarea {
  resize: vertical;
  min-height: 118px;
  line-height: 1.65;
}

.info-strip {
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 13px;
  color: var(--hint);
}

.info-strip strong {
  color: var(--text);
}

/* Forms & Checkboxes */
.form-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.field-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--hint);
  margin-bottom: 4px;
}

.check-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
}

.check-card {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

.check-card input {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 12px;
  padding: 11px 14px;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  transition: transform 0.14s ease, opacity 0.14s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-block {
  width: 100%;
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 9px;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
}

.btn-accent {
  background: linear-gradient(135deg, #d97706, #f59e0b);
  color: #111827;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.35);
}

.btn-ghost {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
}

/* Lists (Admins, Delegates, Audit) */
.item-list,
.audit-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-item {
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.list-item-main {
  min-width: 0;
  flex: 1;
}

.list-item-title {
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.list-item-sub {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 5px;
}

.mini-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 7px;
  background: rgba(56, 189, 248, 0.14);
  color: var(--link);
}

.mini-badge.gold {
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
}

.mini-badge.green {
  background: rgba(16, 185, 129, 0.16);
  color: #34d399;
}

/* Proactive 4-Way Grid */
.proactive-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.pro-btn {
  border: 1px solid var(--border);
  background: var(--secondary-bg);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 10px;
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  transition: all 0.16s ease;
}

.pro-btn.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.pro-btn.active .pro-desc {
  color: rgba(255, 255, 255, 0.85);
}

.pro-emoji { font-size: 22px; }
.pro-title { font-size: 14px; font-weight: 700; }
.pro-desc { font-size: 11.5px; color: var(--hint); }

/* Vault Stats & Progress */
.progress-track {
  width: 100%;
  height: 9px;
  border-radius: 999px;
  background: var(--secondary-bg);
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #38bdf8);
  border-radius: 999px;
  transition: width 0.35s ease;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

.stat-tile {
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 9px 6px;
  text-align: center;
}

.stat-val {
  display: block;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.stat-lbl {
  display: block;
  font-size: 11px;
  color: var(--hint);
}

/* Audit Card */
.audit-item {
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: 13px;
  padding: 11px 13px;
}

.audit-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.audit-action {
  font-size: 13.5px;
  font-weight: 700;
}

.audit-time {
  font-size: 11px;
  color: var(--hint);
  font-variant-numeric: tabular-nums;
}

.audit-actor {
  font-size: 12.5px;
  color: var(--link);
  margin-top: 3px;
}

.audit-details {
  font-size: 12.5px;
  color: var(--hint);
  margin-top: 4px;
  word-break: break-word;
}

.empty-box {
  background: var(--section-bg);
  border: 1px dashed var(--border);
  border-radius: 14px;
  padding: 20px;
  text-align: center;
  color: var(--hint);
  font-size: 13.5px;
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 40;
  background: color-mix(in srgb, var(--secondary-bg) 95%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom, 0px));
  gap: 4px;
}

.nav-btn {
  border: none;
  background: transparent;
  color: var(--hint);
  border-radius: 12px;
  padding: 6px 2px;
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  transition: all 0.16s ease;
}

.nav-btn.active {
  background: rgba(37, 99, 235, 0.16);
  color: var(--link);
}

.nav-ico { font-size: 18px; line-height: 1.1; }
.nav-txt { font-size: 11px; font-weight: 700; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(82px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 60;
  background: #111827;
  color: #f8fafc;
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.35);
  max-width: calc(100vw - 28px);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.success {
  border-color: rgba(16, 185, 129, 0.6);
  background: #064e3b;
}

.toast.error {
  border-color: rgba(239, 68, 68, 0.65);
  background: #7f1d1d;
}

@media (max-width: 360px) {
  .locks-grid,
  .field-grid,
  .check-grid {
    grid-template-columns: 1fr;
  }
}
