/* =========================================================
   EV Trade — App Shell (sidebar stile CRM)
   ---------------------------------------------------------
   La sidebar è `position: fixed` e il contenuto viene spostato
   con un padding-left sul body: così le 54 view esistenti non
   vanno toccate, e i calcoli tipo `calc(100vh - 56px)` restano
   validi perché la topbar mantiene la stessa altezza della
   vecchia navbar.
   ========================================================= */

:root {
  --shell-sidebar-w: 250px;
  --shell-sidebar-w-collapsed: 72px;
  --shell-topbar-h: 56px;
  /* Offset orizzontale del contenuto: 0 su mobile, larghezza
     sidebar su desktop. Gli elementi `position: fixed` a tutta
     larghezza (chat composer, sheet) lo usano per non finire
     sotto la sidebar. */
  --shell-left: 0px;
}

/* =========================================================
   SIDEBAR
   ========================================================= */
.app-sidebar {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  bottom: 0;
  left: 0;
  width: var(--shell-sidebar-w);
  z-index: 120;
  display: flex;
  flex-direction: column;
  background: var(--navy-dark);
  border-right: 1px solid var(--border);
  transition: width .22s cubic-bezier(.4, 0, .2, 1), transform .26s cubic-bezier(.32, .72, 0, 1);
  overscroll-behavior: contain;
}

/* ─── Testata: logo + collapse ─── */
.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--shell-topbar-h);
  padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
  text-decoration: none;
}

.sidebar-logo {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--blue), var(--violet));
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 800;
  font-size: 16px;
  flex-shrink: 0;
}

.sidebar-brand-text {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
}
.sidebar-brand-text em {
  font-style: normal;
  font-weight: 500;
  color: var(--text-dim);
}

.sidebar-collapse-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: color .15s, border-color .15s;
}
.sidebar-collapse-btn:hover {
  color: var(--text);
  border-color: var(--border-hover);
}
.sidebar-collapse-btn svg { transition: transform .22s ease; }

/* ─── CTA "Pubblica" ─── */
.sidebar-cta {
  margin: 14px 12px 6px;
  height: 40px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--blue), var(--violet));
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  flex-shrink: 0;
  box-shadow: 0 6px 18px rgba(94, 179, 255, .18);
  transition: filter .15s, transform .1s;
}
.sidebar-cta:hover { filter: brightness(1.08); }
.sidebar-cta:active { transform: scale(.98); }
.sidebar-cta svg { flex-shrink: 0; }
.sidebar-cta span { white-space: nowrap; }

/* ─── Lista voci ─── */
.sidebar-nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 10px 14px;
  scrollbar-width: thin;
}
.sidebar-nav::-webkit-scrollbar { width: 5px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.sidebar-group + .sidebar-group { margin-top: 14px; }

.sidebar-group-title {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-dimmer);
  padding: 0 10px 6px;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 11px;
  height: 38px;
  padding: 0 10px;
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 13.5px;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  transition: background .14s, color .14s;
}
.sidebar-link:hover {
  background: var(--surface2);
  color: var(--text);
}
.sidebar-link .sl-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: currentColor;
}
.sidebar-link .sl-icon svg { width: 18px; height: 18px; }
.sidebar-link .sl-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-link.active {
  background: var(--blue-bg);
  color: var(--blue);
  font-weight: 700;
}
.sidebar-link.active::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 20px;
  border-radius: 0 3px 3px 0;
  background: var(--blue);
}
.sidebar-link.is-admin { color: var(--gold); }
.sidebar-link.is-admin:hover { color: var(--gold-light); }

/* Badge numerico */
.sl-badge {
  flex-shrink: 0;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--rose);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'JetBrains Mono', monospace;
}
.sl-badge.sl-badge-muted { background: var(--surface3); color: var(--text-muted); }

/* ─── Piede: utente + monete ─── */
.sidebar-foot {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom, 0px));
}
.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 11px;
  text-decoration: none;
  transition: background .14s;
  min-width: 0;
}
.sidebar-user:hover { background: var(--surface2); }
.sidebar-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  border: 2px solid var(--border);
}
.sidebar-avatar-fallback {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--blue), var(--violet));
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sidebar-user-meta { min-width: 0; flex: 1; overflow: hidden; }
.sidebar-user-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-user-coins {
  font-size: 11px;
  color: var(--gold);
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
}

/* =========================================================
   SIDEBAR — stato COLLASSATO (solo desktop)
   ========================================================= */
@media (min-width: 768px) {
  body.sidebar-collapsed { --shell-left: var(--shell-sidebar-w-collapsed); }
  body.sidebar-collapsed .app-sidebar { width: var(--shell-sidebar-w-collapsed); }

  body.sidebar-collapsed .sidebar-brand-text,
  body.sidebar-collapsed .sidebar-cta span,
  body.sidebar-collapsed .sidebar-group-title,
  body.sidebar-collapsed .sidebar-link .sl-label,
  body.sidebar-collapsed .sidebar-user-meta { display: none; }

  body.sidebar-collapsed .sidebar-head { padding: 0 0 0 20px; }
  body.sidebar-collapsed .sidebar-collapse-btn { display: none; }
  body.sidebar-collapsed .sidebar-cta { margin: 14px 16px 6px; padding: 0; }
  body.sidebar-collapsed .sidebar-link { justify-content: center; padding: 0; }
  body.sidebar-collapsed .sidebar-user { justify-content: center; padding: 7px 0; }

  /* Il titolo di gruppo collassa in un separatore */
  body.sidebar-collapsed .sidebar-group + .sidebar-group {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
  }
  body.sidebar-collapsed .sidebar-group-title { padding: 0; height: 0; }

  /* Badge come pallino sull'icona */
  body.sidebar-collapsed .sl-badge {
    position: absolute;
    top: 3px;
    right: 10px;
    min-width: 8px;
    width: 8px;
    height: 8px;
    padding: 0;
    font-size: 0;
    border-radius: 50%;
  }

  /* Tooltip al passaggio del mouse quando è collassata */
  body.sidebar-collapsed .sidebar-link::after {
    content: attr(data-tip);
    position: absolute;
    left: calc(100% + 14px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--surface3);
    color: var(--text);
    border: 1px solid var(--border-light);
    font-size: 12px;
    font-weight: 600;
    padding: 5px 9px;
    border-radius: 8px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity .12s;
    z-index: 130;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .45);
  }
  body.sidebar-collapsed .sidebar-link:hover::after { opacity: 1; }
}

/* =========================================================
   TOPBAR
   ========================================================= */
.app-topbar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 55;
  height: var(--shell-topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: rgba(5, 9, 26, .88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
  text-decoration: none;
  transition: background .14s, color .14s, border-color .14s;
}
.topbar-icon-btn:hover {
  background: var(--surface2);
  color: var(--text);
}

.topbar-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* Ricerca globale */
.topbar-search {
  flex: 1;
  max-width: 420px;
  min-width: 0;
  position: relative;
  display: none;
}
.topbar-search input {
  width: 100%;
  height: 36px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  padding: 0 12px 0 34px;
  outline: none;
  transition: border-color .15s, background .15s;
  font-family: inherit;
}
.topbar-search input::placeholder { color: var(--text-dimmer); }
.topbar-search input:focus {
  border-color: var(--blue-muted);
  background: var(--surface2);
}
.topbar-search svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: var(--text-dimmer);
  pointer-events: none;
}

.topbar-spacer { flex: 1; min-width: 0; }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* Pillola monete */
.topbar-coins {
  display: none;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 11px;
  border-radius: 9px;
  background: var(--gold-bg);
  border: 1px solid rgba(255, 184, 61, .25);
  color: var(--gold);
  font-size: 12.5px;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
  text-decoration: none;
  transition: border-color .15s;
}
.topbar-coins:hover { border-color: var(--gold); }

.topbar-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--rose);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.topbar-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--border);
}
.topbar-avatar-fallback {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--blue), var(--violet));
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* =========================================================
   MOBILE — la sidebar diventa un drawer
   ========================================================= */
@media (max-width: 767px) {
  .app-sidebar {
    transform: translateX(-100%);
    box-shadow: none;
    width: min(84vw, 300px);
  }
  body.sidebar-open .app-sidebar {
    transform: translateX(0);
    box-shadow: 12px 0 40px rgba(0, 0, 0, .55);
  }
  #sidebar-scrim {
    position: fixed;
    inset: 0;
    z-index: 119;
    background: rgba(3, 6, 18, .66);
    backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity .24s ease;
  }
  body.sidebar-open #sidebar-scrim {
    opacity: 1;
    pointer-events: auto;
  }
  body.sidebar-open { overflow: hidden; }
}

/* =========================================================
   DESKTOP — il contenuto lascia spazio alla sidebar
   ========================================================= */
@media (min-width: 768px) {
  body.has-sidebar { --shell-left: var(--shell-sidebar-w); }
  body.has-sidebar { padding-left: var(--shell-left); }

  .sidebar-collapse-btn { display: flex; }
  #sidebar-scrim { display: none; }
  .sidebar-mobile-only,
  .topbar-mobile-only { display: none !important; }

  .topbar-search { display: block; }
  .topbar-coins { display: flex; }
  .app-topbar { padding: 0 20px; }

  /* Elementi fixed a tutta larghezza: non devono passare sotto la sidebar */
  body.has-sidebar .chat-composer,
  body.has-sidebar .composer-backdrop,
  body.has-sidebar .filter-drawer-backdrop,
  body.has-sidebar .sg-sheet-backdrop { left: var(--shell-left); }

  body.has-sidebar .composer-sheet { margin-left: calc(var(--shell-left) / 2); }
}

/* La sidebar non ha senso in modalità gioco a tutto schermo */
body.shell-fullscreen .app-sidebar,
body.shell-fullscreen .app-topbar { display: none; }
body.shell-fullscreen.has-sidebar { padding-left: 0; --shell-left: 0px; }
