:root {
  /* Paleta oficial Valore (mesma do valore.css usado no Portal NFSe/Auditoria SPED) */
  --valore-blue: #0f2d4d;
  --valore-blue-dark: #0a1f35;
  --valore-blue-light: #dbeafe;
  --sidebar-accent: #1a6fcc;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;

  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface2: #f8f9fd;
  --border: #e5e7eb;
  --text: #273142;
  --text-2: #4a5568;
  --text-3: #8896b0;

  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #b45309;
  --warning-bg: #fef3c7;

  --sidebar-collapsed-width: 76px;
  --sidebar-expanded-width: 270px;
  --sidebar-transition: 0.28s ease;

  --header-height: 70px;
  --radius: 12px;
  --shadow: 0 4px 18px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; }
body { font-family: "Plus Jakarta Sans", "DM Sans", Arial, sans-serif; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; }
a { color: inherit; text-decoration: none; }

/* ============================= SIDEBAR ============================= */

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

.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 1000;
  width: var(--sidebar-collapsed-width);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(180deg, var(--valore-blue) 0%, var(--valore-blue-dark) 100%);
  color: #ffffff;
  transition: width var(--sidebar-transition), box-shadow var(--sidebar-transition);
}

.sidebar:hover, .sidebar.is-expanded {
  width: var(--sidebar-expanded-width);
  box-shadow: 8px 0 24px rgba(15, 23, 42, 0.16);
}

.sidebar-header {
  min-height: var(--header-height);
  display: flex;
  align-items: center;
  padding: 12px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.brand { display: flex; align-items: center; gap: 14px; min-width: 230px; }

/* Logo lisa, cor original preservada (sem caixa/chip) */
.brand-logo {
  width: 34px; height: 34px; flex: 0 0 34px;
  object-fit: contain;
  transition: width .18s ease, height .18s ease;
}
.sidebar:hover .brand-logo, .sidebar.is-expanded .brand-logo { width: 42px; height: 42px; flex-basis: 42px; }

.brand-text { opacity: 0; white-space: nowrap; transform: translateX(-8px); transition: opacity .18s ease, transform .18s ease; }
.sidebar:hover .brand-text, .sidebar.is-expanded .brand-text { opacity: 1; transform: translateX(0); }
.brand-title { display: block; font-size: 15px; font-weight: 700; }
.brand-subtitle { display: block; margin-top: 2px; font-size: 11px; color: rgba(255, 255, 255, 0.72); }

.sidebar-nav { flex: 1; padding: 16px 10px; overflow-y: auto; overflow-x: hidden; }
.sidebar-nav::-webkit-scrollbar { width: 5px; }
.sidebar-nav::-webkit-scrollbar-thumb { border-radius: 10px; background: rgba(255, 255, 255, 0.2); }

.menu-section { margin-bottom: 18px; }
.menu-section-title {
  min-width: 230px; margin: 0 12px 8px;
  font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: rgba(255, 255, 255, 0.5);
  opacity: 0; transition: opacity .18s ease;
}
.sidebar:hover .menu-section-title, .sidebar.is-expanded .menu-section-title { opacity: 1; }

.menu-list { list-style: none; }
.menu-item { margin-bottom: 5px; }

.menu-link {
  position: relative;
  width: 100%;
  min-height: 46px; min-width: 230px;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 13px; border-radius: 9px;
  border: none; background: transparent; cursor: pointer;
  color: rgba(255, 255, 255, 0.82); text-align: left;
  transition: background-color .18s ease, color .18s ease;
}
.menu-link:hover { background: rgba(255, 255, 255, 0.11); color: #fff; }
.menu-link.active { background: rgba(255, 255, 255, 0.17); color: #fff; }
.menu-link.active::before {
  content: ""; position: absolute; top: 9px; bottom: 9px; left: 0;
  width: 3px; border-radius: 0 3px 3px 0; background: var(--sidebar-accent);
}
.menu-icon { width: 30px; height: 30px; flex: 0 0 30px; display: flex; align-items: center; justify-content: center; }
.menu-icon svg { width: 18px; height: 18px; color: currentColor; }
.menu-text { font-size: 14px; font-weight: 500; white-space: nowrap; opacity: 0; transform: translateX(-7px); transition: opacity .18s ease, transform .18s ease; }
.sidebar:hover .menu-text, .sidebar.is-expanded .menu-text { opacity: 1; transform: translateX(0); }

.sidebar-footer { padding: 12px 10px; border-top: 1px solid rgba(255, 255, 255, 0.12); }
.user-card { min-width: 230px; display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.08); }
.user-avatar { width: 34px; height: 34px; flex: 0 0 34px; display: flex; align-items: center; justify-content: center; border-radius: 10px; background: var(--sidebar-accent); color: #fff; font-size: 13px; font-weight: 700; }
.user-info { min-width: 0; opacity: 0; transition: opacity .18s ease; }
.sidebar:hover .user-info, .sidebar.is-expanded .user-info { opacity: 1; }
.user-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; }
.user-email { display: block; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; color: rgba(255, 255, 255, 0.68); }

.logout-link {
  min-width: 230px; min-height: 42px;
  display: flex; align-items: center; gap: 16px;
  margin-top: 8px; padding: 9px 13px; border-radius: 9px;
  color: rgba(255, 255, 255, 0.8);
  transition: background-color .18s ease, color .18s ease;
}
.logout-link:hover { background: rgba(220, 38, 38, 0.18); color: #fff; }
.logout-link .menu-text { opacity: 0; transition: opacity .18s ease; }
.sidebar:hover .logout-link .menu-text, .sidebar.is-expanded .logout-link .menu-text { opacity: 1; }

/* Tela de "sem sessão" — cobre a página inteira até o /api/me confirmar login */
.tela-sem-sessao {
  position: fixed; inset: 0; z-index: 2000;
  display: flex; align-items: center; justify-content: center;
  background: var(--valore-blue-dark, #0a1f35);
  color: #fff; text-align: center; padding: 24px;
}
/* Sem isso, "display: flex" acima ganha do atributo HTML [hidden] — o JS marcava o elemento
   como hidden="true" corretamente, mas ele continuava visível (cobrindo a tela toda, por
   causa do position:fixed/z-index acima) mesmo com a sessão já autenticada. */
.tela-sem-sessao[hidden] {
  display: none;
}
.tela-sem-sessao .caixa { max-width: 420px; }
.tela-sem-sessao img { width: 64px; margin-bottom: 20px; }
.tela-sem-sessao h1 { font-size: 1.3rem; margin-bottom: 10px; }
.tela-sem-sessao p { color: rgba(255,255,255,.75); line-height: 1.6; margin-bottom: 18px; }
.tela-sem-sessao a.btn-primary { display: inline-flex; }

/* ============================= CONTEÚDO PRINCIPAL ============================= */

.main-wrapper { min-height: 100vh; margin-left: var(--sidebar-collapsed-width); transition: margin-left var(--sidebar-transition); }
body:has(.sidebar:hover) .main-wrapper, body:has(.sidebar.is-expanded) .main-wrapper { margin-left: var(--sidebar-expanded-width); }

.topbar {
  position: sticky; top: 0; z-index: 900;
  min-height: var(--header-height);
  display: flex; align-items: center; justify-content: space-between; gap: 20px;
  padding: 12px 28px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.mobile-menu-button { display: none; width: 42px; height: 42px; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 9px; background: #fff; color: var(--accent); cursor: pointer; }
.page-heading h1 { font-size: 20px; font-weight: 700; color: var(--text); }
.page-heading p { margin-top: 3px; font-size: 12px; color: var(--text-3); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-actions-group { display: flex; align-items: center; gap: 8px; }
.topbar-actions-group[hidden] { display: none; }
.topbar-button { min-height: 40px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 14px; border: 1px solid var(--border); border-radius: 9px; background: #fff; color: var(--text); cursor: pointer; transition: border-color .18s ease, background-color .18s ease; }
.topbar-button-icon { width: 40px; padding: 0; }
.topbar-button:hover { border-color: var(--accent); background: var(--valore-blue-light); }

.main-content { padding: 26px 28px 40px; }

/* ============================= COMPONENTES ============================= */

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }

.overlay { position: fixed; inset: 0; background: rgba(5, 11, 24, .45); opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 1200; }
.overlay.open { opacity: 1; pointer-events: auto; }

.drawer { position: fixed; top: 0; right: -560px; width: 560px; max-width: 94vw; height: 100vh; background: #fff; z-index: 1300; transition: right .2s ease; display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(10, 22, 40, .16); }
.drawer.open { right: 0; }
.drawer-head { padding: 18px 20px; border-bottom: 1px solid var(--border); background: linear-gradient(135deg, var(--valore-blue), var(--valore-blue-dark)); color: #fff; }
.drawer-close { float: right; border: none; background: transparent; color: #fff; font-size: 1rem; cursor: pointer; }
.drawer-id { font-size: .75rem; color: rgba(255,255,255,.7); text-transform: uppercase; letter-spacing: .1em; }
.drawer-head h2 { font-size: 1rem; margin-top: 6px; padding-right: 24px; }
.drawer-sub { font-size: .8rem; color: rgba(255,255,255,.72); margin-top: 4px; }
.drawer-body { padding: 18px 20px; overflow-y: auto; }
.drawer-section { margin-bottom: 16px; }
.drawer-section h4 { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--text-3); margin-bottom: 8px; }
.drawer-box { background: var(--surface2); border-radius: 8px; padding: 12px; color: var(--text-2); line-height: 1.5; }
.drawer-grid { display: grid; grid-template-columns: 150px 1fr; gap: 8px 12px; font-size: .9rem; }
.drawer-grid .k { color: var(--text-3); }
.drawer-grid .v { color: var(--text); font-weight: 600; }
.msg { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; background: #fbfcfe; }
.msg .mh { display: flex; justify-content: space-between; font-size: .75rem; color: var(--text-3); margin-bottom: 6px; }
.msg.msg-analista { background: var(--valore-blue-light); }
.msg.msg-sistema { background: var(--surface2); font-style: italic; color: var(--text-2); }

#drawer-reply-form { display: flex; flex-direction: column; gap: 8px; }
#drawer-reply-form textarea { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); resize: vertical; }
#drawer-reply-form button { align-self: flex-start; }
.drawer-hint { margin-top: 6px; font-size: .75rem; color: var(--text-3); }
#drawer-status-select { width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: #fff; font-weight: 600; }
#drawer-status-motivo-card { margin-top: 10px; padding: 12px; }
#drawer-status-motivo-card textarea { width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); resize: vertical; margin-top: 6px; }
#drawer-status-motivo-card .form-actions { margin-top: 8px; }
.anexo-label { font-size: .78rem; color: var(--text-2); font-weight: 600; }
#drawer-reply-files { font-size: .8rem; }
.anexo-lista { display: flex; flex-direction: column; gap: 2px; font-size: .78rem; color: var(--text-2); }
.msg-anexos { margin-top: 6px; display: flex; flex-direction: column; gap: 3px; }
.msg-anexos a { color: var(--accent); font-size: .8rem; }

.view { display: none; }
.view.active { display: block; }

.filters { padding: 14px 16px; display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.filters input, .filters select { padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text); }
.filters input { min-width: 240px; }

.tlist { display: flex; flex-direction: column; gap: 10px; }
.ticket { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: grid; grid-template-columns: 70px 1fr 190px 140px; gap: 14px; align-items: center; box-shadow: var(--shadow); cursor: pointer; }
.t-id { font-size: .75rem; color: var(--text-3); font-weight: 700; }
.t-subject { font-weight: 700; margin-bottom: 4px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.t-meta { font-size: .77rem; color: var(--text-2); }
.t-client { font-weight: 700; }
.t-product { font-size: .77rem; color: var(--text-2); margin-top: 2px; }

.badge { display: inline-block; font-size: .68rem; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.pri-urgente { background: var(--danger-bg); color: var(--danger); }
.pri-alta { background: var(--warning-bg); color: var(--warning); }
.pri-normal { background: var(--valore-blue-light); color: var(--accent); }
.pri-baixa { background: #eef2ff; color: #4338ca; }
.st-aberto { background: var(--danger-bg); color: var(--danger); }
.st-andamento { background: var(--valore-blue-light); color: var(--accent); }
.st-aguardando { background: var(--warning-bg); color: var(--warning); }
.st-resolvido { background: var(--success-bg); color: var(--success); }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { padding: 18px; }
.lbl { font-size: .72rem; color: var(--text-3); text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }
.kpi-num { font-size: 1.8rem; font-weight: 800; margin-top: 6px; color: var(--text); }
.sub { font-size: .77rem; color: var(--text-2); margin-top: 4px; }
.kpi.alert .kpi-num { color: var(--danger); }

.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.panel { padding: 16px 18px; }
.panel h3 { font-size: 1rem; margin-bottom: 10px; color: var(--text); }
.panel-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.panel-list li { padding: 8px 10px; background: var(--surface2); border-radius: 8px; color: var(--text-2); font-size: .9rem; }
.alert-panel { border-color: var(--warning-bg); }
.alert-panel h3 { color: var(--warning); }
.alert-panel .panel-list li { background: var(--warning-bg); color: #7a3d05; font-weight: 600; }

.uni-note { padding: 14px 16px; margin-bottom: 12px; color: var(--text-2); }

.form-card { padding: 16px; margin-bottom: 16px; }
.form-card h3 { margin-bottom: 12px; color: var(--text); font-size: 1rem; }
#email-form, #vinculo-form { display: grid; gap: 10px; grid-template-columns: repeat(4, minmax(0, 1fr)); }
#email-form input, #email-form select, #vinculo-form input, #vinculo-form select { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: #fff; }
#email-form input[readonly], #vinculo-form input[readonly] { background: var(--surface2); color: var(--text-2); cursor: not-allowed; }
.form-status { margin-top: 10px; color: var(--accent); font-weight: 600; }
.form-actions { display: flex; gap: 10px; grid-column: 1 / -1; }

#empresa-form { display: grid; gap: 10px; grid-template-columns: 2fr 1fr 2fr; }
#empresa-form input { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: #fff; }

/* Botões no padrão Valore */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 9px 16px;
  border: none; border-radius: 9px;
  font-weight: 600; font-size: .85rem; cursor: pointer;
  transition: background-color .18s ease, border-color .18s ease, transform .18s ease;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); }
.btn-ghost { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--accent); background: var(--valore-blue-light); }
.btn-small { min-height: 32px; padding: 6px 12px; font-size: .78rem; }

/* Empresas */
.hint-card { font-size: .82rem; color: var(--text-2); background: var(--valore-blue-light); border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab { padding: 8px 16px; border-radius: 8px; border: 1px solid var(--border); background: #fff; color: var(--text-2); font-weight: 600; font-size: .85rem; cursor: pointer; }
.tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.toolbar { padding: 14px 16px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.toolbar input[type="search"] { min-width: 260px; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; }
.toolbar-empresas { padding: 20px; }
.toolbar-empresas input[type="search"] { flex: 1; min-width: 320px; min-height: 46px; padding: 12px 16px; font-size: .95rem; }
.toolbar-empresas select { min-height: 46px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-size: .88rem; }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.pagination { display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap; margin-top: 16px; }
.pagination button { min-width: 36px; height: 36px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border); background: #fff; color: var(--text-2); font-weight: 600; font-size: .85rem; cursor: pointer; }
.pagination button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.pagination button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pagination button:disabled { opacity: .4; cursor: not-allowed; }
.pagination .ellipsis { padding: 0 4px; color: var(--text-3); }

.table-scroll { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; min-width: 640px; }
.table th, .table td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); font-size: .88rem; }
.table th { color: var(--text-3); text-transform: uppercase; font-size: .7rem; letter-spacing: .06em; font-weight: 700; }
.table td.emails-cell { color: var(--text-2); max-width: 320px; }
.table .row-actions { display: flex; gap: 6px; }

/* ============================= OVERLAY MOBILE ============================= */

.sidebar-overlay { position: fixed; inset: 0; z-index: 950; display: none; background: rgba(15, 23, 42, 0.5); }

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

@media (max-width: 1100px) {
  .dashboard-grid, .panel-grid { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 768px) {
  .sidebar { width: var(--sidebar-expanded-width); transform: translateX(-100%); transition: transform var(--sidebar-transition), box-shadow var(--sidebar-transition); }
  .sidebar:hover { width: var(--sidebar-expanded-width); }
  .sidebar.is-mobile-open { transform: translateX(0); box-shadow: 8px 0 24px rgba(15, 23, 42, .22); }
  .sidebar .brand-text, .sidebar .menu-section-title, .sidebar .menu-text, .sidebar .user-info { opacity: 1; transform: none; }

  .main-wrapper, body:has(.sidebar:hover) .main-wrapper, body:has(.sidebar.is-expanded) .main-wrapper { margin-left: 0; }

  .sidebar-overlay.is-visible { display: block; }
  .mobile-menu-button { display: inline-flex; }

  .topbar { padding: 12px 16px; }
  .topbar-actions { display: none; }
  .main-content { padding: 20px 16px 30px; }

  .ticket { grid-template-columns: 1fr; }
  #email-form, #vinculo-form, #empresa-form { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
