/* =========================================================
   LAYOUT SYSTEM - SPA Shell
   ========================================================= */

body {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  margin: 0;
}

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

/* =========================================================
   SIDEBAR
========================================================= */
.sidebar {
  width: 220px;
  background-color: var(--sidebar-bg);
  color: rgba(255,255,255,0.85);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 100;
  border-right: none;
  transition: width 0.25s cubic-bezier(0.4,0,0.2,1),
              transform 0.25s cubic-bezier(0.4,0,0.2,1);
}

/* Sidebar colapsada (class toggled no JS) */
.sidebar.collapsed {
  width: 68px;
  /* Permite que os tooltips se projetem além da borda de 64px */
  overflow: visible;
}
.sidebar.collapsed .sidebar-header {
  padding: 1.5rem 0.25rem;
}
.sidebar.collapsed .sidebar-title,
.sidebar.collapsed .sidebar-subtitle,
.sidebar.collapsed .sidebar-nav .sidebar-label,
.sidebar.collapsed .sidebar-header-text {
  display: none !important;
}

/* BUG 3 FIX — Botão Sair nunca some; retraído: apenas ícone centralizado */
.sidebar.collapsed .sidebar-btn-sair .sidebar-btn-sair-label {
  display: none !important;
}
.sidebar.collapsed .sidebar-btn-sair {
  display: flex !important;
  justify-content: center;
  padding: 0.5rem;
  gap: 0;
}
.sidebar.collapsed .sidebar-item {
  justify-content: center;
  padding: 0.75rem 0;
  border-left: none;
  border-bottom: none;
  /* overflow:visible para o tooltip absoluto não ser cortado */
  overflow: visible;
}
.sidebar.collapsed .sidebar-item svg {
  margin-right: 0;
}
.sidebar.collapsed .sidebar-logo-box {
  width: 44px !important;
  height: 44px !important;
  min-width: 44px !important;
  font-size: 1.1rem !important;
}

/* Quando tem logomarca e está colapsado: centraliza e redimensiona a imagem */
.sidebar.collapsed .sidebar-logo-box.has-logo {
  width: 44px !important;
  height: 44px !important;
  min-width: 44px !important;
  border-radius: var(--radius-sm) !important;
  padding: 4px !important;
  background: transparent !important;
}

.sidebar.collapsed .sidebar-logo-box.has-logo img {
  width: 100% !important;
  height: 100% !important;
  object-fit: contain;
  object-position: center center;
}
.sidebar.collapsed #sidebar-toggle-desktop {
  position: static;
  margin-bottom: 0.5rem;
  /* ícone trocado via JS — sem rotate() */
}
.sidebar.collapsed .sidebar-footer {
  padding: 0.75rem 0;
  align-items: center;
}
.sidebar.collapsed .topbar-profile {
  justify-content: center;
}

/* BUG 2 FIX — Avatar: gap via CSS (inline style removido do HTML) */
.sidebar-user-row {
  gap: 0.625rem; /* gap movido do inline style para cá */
}

/* Retraído: sem gap, avatar centralizado */
.sidebar.collapsed .sidebar-user-row {
  justify-content: center !important;
  gap: 0 !important;
  padding: 0 !important;
  padding-bottom: 0.625rem !important;
}

/* Garantia extra: avatar nunca se desloca */
.sidebar.collapsed .sidebar-avatar {
  margin: 0 auto !important;
}
.sidebar.collapsed .sidebar-section-label {
  display: none !important;
}

/* Seção sem itens visíveis: ocultar label via classe (sem style inline) */
.sidebar-section-hidden {
  display: none !important;
}

/* SIDEBAR HEADER */

.sidebar-header {
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.75rem;
  min-height: 72px; /* alinha com topbar */
  flex-shrink: 0;
}

/* BUG 1 FIX — Logo box: fundo dourado apenas quando exibe o texto "1RI" (sem logo) */
.sidebar-logo-box {
  width: 38px;
  height: 38px;
  min-width: 38px;
  background-color: var(--color-primary);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 0.8rem;
  color: var(--color-gold);
  flex-shrink: 0;
  overflow: hidden;
}

/* Quando há logomarca: fundo transparente + expande para ocupar o header */
.sidebar-logo-box.has-logo {
  width: 100%;
  height: 52px;
  min-width: unset;
  border-radius: var(--radius-md);
  background-color: transparent !important;
  background: transparent !important;
  padding: 2px;
}

.sidebar-logo-box.has-logo img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: left center;
}

.sidebar-title {
  font-family: var(--font-display);
  font-size: 0.95rem;
  color: rgba(255,255,255,0.95);
  line-height: 1.2;
  white-space: nowrap;
}

.sidebar-subtitle {
  font-size: 0.55rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: rgba(255,255,255,0.50);
  font-weight: 700;
  white-space: nowrap;
}

/* Botão de colapso */
#sidebar-toggle {
  display: none; /* visível só via JS quando necessário */
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  border-radius: 6px;
  flex-shrink: 0;
  margin-left: auto;
}
#sidebar-toggle:hover { color: var(--color-primary); background: rgba(0,0,0,.04); }

/* NAV */
.sidebar-nav {
  padding: 1rem 0.625rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 1rem 0.6rem 1rem;
  color: rgba(255,255,255,0.60);
  text-decoration: none;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.8rem;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
  border-left: 4px solid transparent;
  white-space: nowrap;
  overflow: hidden;
  position: relative; /* necessário para o tooltip absoluto */
}
.sidebar-item:hover {
  background-color: rgba(255,255,255,0.08);
  color: rgba(255,255,255,0.90);
}
.sidebar-item.active {
  background-color: rgba(255,255,255,0.14);
  color: rgba(255,255,255,1);
  border-left-color: var(--accent-color);
  font-weight: 700;
}
.sidebar-item svg {
  width: 1.125rem;
  height: 1.125rem;
  opacity: 0.75;
  flex-shrink: 0;
}
.sidebar-item.active svg { opacity: 1; }

.sidebar-footer { padding: 1rem; margin-top: auto; border-top: 1px solid rgba(255,255,255,0.08); }

.btn-new-protocol {
  width: 100%;
  background-color: var(--color-primary);
  color: #fff;
  padding: 0.875rem;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 0.875rem;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s;
}
.btn-new-protocol:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(253, 195, 77, 0.35);
  filter: brightness(1.05);
}

/* Overlay para mobile */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 99;
  backdrop-filter: blur(2px);
}

/* =========================================================
   APP CONTAINER
========================================================= */
.app-container {
  display: flex;
  flex-direction: column;
  flex: 1;
  background-color: var(--surface-low);
  overflow: hidden;
  min-width: 0; /* previne overflow em flex */
}

/* =========================================================
   TOPBAR
========================================================= */
.topbar {
  height: 72px;
  min-height: 72px;
  background-color: var(--surface-lowest);
  border-bottom: 1px solid rgba(0,0,0,0.03);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  flex-shrink: 0;
  gap: 1rem;
}

@media (min-width: 769px) {
  .topbar { display: none !important; }
}

/* Botão hamburguer — visível no móbile */
#topbar-menu-btn {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 6px;
  border-radius: 8px;
  flex-shrink: 0;
}
#topbar-menu-btn:hover { color: var(--color-primary); background: rgba(0,0,0,.04); }

.topbar-search {
  flex: 1;
  max-width: 480px;
  position: relative;
}
.topbar-search svg {
  position: absolute;
  left: 1.25rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  width: 1rem;
  height: 1rem;
}
.topbar-search input {
  width: 100%;
  background-color: var(--surface-low);
  border: none;
  border-radius: var(--radius-xl);
  padding: 0.625rem 1.5rem 0.625rem 3rem;
  font-family: var(--font-ui);
  font-size: 0.875rem;
  color: var(--text-primary);
  transition: all 0.2s;
  box-sizing: border-box;
}
.topbar-search input:focus {
  outline: none;
  background-color: var(--surface-lowest);
  box-shadow: 0 0 0 2px rgba(232, 139, 21, 0.2);
}

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

.topbar-icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: color 0.2s;
  padding: 4px;
}
.topbar-icon-btn:hover { color: var(--color-primary-container); }
.topbar-icon-btn .badge-dot {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 8px;
  height: 8px;
  background-color: var(--status-err-text);
  border-radius: 50%;
  border: 2px solid var(--surface-lowest);
}

.topbar-profile {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  cursor: pointer;
  padding-left: 1.25rem;
  border-left: 1px solid rgba(0,0,0,0.05);
}

.topbar-avatar {
  width: 36px;
  height: 36px;
  min-width: 36px;
  background-color: var(--color-primary);
  color: var(--color-gold);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.875rem;
}

.topbar-user-info { display: flex; flex-direction: column; }
.topbar-username { font-size: 0.8rem; font-weight: 600; color: var(--text-primary); white-space: nowrap; }
.topbar-userrole { font-size: 0.6rem; color: var(--text-secondary); font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; white-space: nowrap; }

/* =========================================================
   MAIN CONTENT
========================================================= */
.main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.75rem;
  position: relative;
  min-width: 0;
}

.page-full {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Login ocupa a tela inteira — cancela o padding do main-content */
.main-content:has(.login-root),
.main-content:has(.chatbot-root) {
  padding: 0;
  overflow: hidden;
}

.page-split {
  display: flex;
  height: 100%;
  min-height: 0;
  gap: 1rem;
}

.page-header { margin-bottom: 1.75rem; }
.page-title { font-size: 1.6rem; color: var(--color-primary); font-family: var(--font-display); margin-bottom: 0.25rem; }
.page-subtitle { font-size: 0.875rem; color: var(--text-secondary); }

/* =========================================================
   TOAST
========================================================= */
#toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1.25rem;
  border-radius: var(--radius-lg);
  font-size: 0.875rem;
  font-weight: 600;
  box-shadow: 0 8px 32px rgba(0,0,0,.15);
  max-width: 380px;
  transform: translateX(120%);
  opacity: 0;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  background-color: var(--surface-lowest);
  border-left: 4px solid transparent;
}
.toast.show { transform: translateX(0); opacity: 1; }
.toast-ok { border-left-color: var(--status-ok-text); color: var(--status-ok-text); }
.toast-err { border-left-color: var(--status-err-text); color: var(--status-err-text); }
.toast-info { border-left-color: var(--status-info-text); color: var(--status-info-text); }
.toast-warn { border-left-color: var(--status-warn-text); color: var(--status-warn-text); }

/* =========================================================
   RESPONSIVIDADE
========================================================= */

/* Tablet — sidebar menor */
@media (max-width: 1024px) {
  .sidebar { width: 200px; }
  .main-content { padding: 1.25rem; }
  .topbar { padding: 0 1rem; }
  .topbar-search { max-width: 340px; }
  .topbar-user-info { display: none; }
}

/* Mobile — sidebar como drawer */
@media (max-width: 768px) {
  body { position: relative; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 240px;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px rgba(0,0,0,.15);
    z-index: 200;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  #sidebar-overlay.visible {
    display: block;
  }

  #topbar-menu-btn {
    display: flex;
  }

  .topbar-search { display: none; }
  #sidebar-toggle-desktop { display: none !important; }

  .main-content {
    padding: 1rem;
  }

  .topbar-profile {
    padding-left: 0.75rem;
    border-left: none;
  }
}

/* Telas muito pequenas */
@media (max-width: 480px) {
  .topbar { padding: 0 0.75rem; gap: 0.5rem; }
  .topbar-actions { gap: 0.5rem; }
  .main-content { padding: 0.75rem; }
}
