@layer components {
  /* Global search (topbar — outside .route-page) */
  .top-search-wrap {
    position: relative;
    width: min(420px, 46vw);
  }

  .topbar-search {
    width: 100%;
    padding-left: 34px;
  }

  .top-search-wrap::before {
    content: "⌕";
    position: absolute;
    left: 12px;
    top: 7px;
    color: #64748b;
    font-size: 14px;
  }

  .top-search-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    right: 0;
    background: rgba(255, 255, 255, 0.92);
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 14px;
    box-shadow: 0 16px 34px rgba(15, 23, 42, 0.16);
    padding: 8px;
    z-index: var(--z-dropdown);
  }

  .top-search-dropdown.hidden {
    pointer-events: none;
  }

  .top-search-item {
    width: 100%;
    border: 0;
    background: transparent;
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 8px;
    border-radius: 10px;
    text-align: left;
    cursor: pointer;
  }

  .top-search-item:hover {
    background: #eff6ff;
  }

  .top-search-type {
    font-size: 11px;
    color: #1d4ed8;
    background: #dbeafe;
    border-radius: 999px;
    padding: 2px 7px;
  }

  .top-search-main {
    display: grid;
    gap: 2px;
  }

  .top-search-main small {
    color: var(--muted);
  }

  .top-search-empty {
    color: var(--muted);
    text-align: center;
    padding: 8px;
  }

  /* Topbar extras (+ Nhanh, sync, user) */
  .quick-add-wrap,
  .user-menu-wrap {
    position: relative;
  }

  .quick-add-dropdown,
  .user-menu-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    width: min(280px, calc(100vw - 24px));
    min-width: 190px;
    max-height: min(70vh, calc(100dvh - 72px));
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 14px;
    padding: 7px;
    box-shadow: 0 16px 36px rgba(15, 23, 42, 0.15);
    z-index: var(--z-dropdown);
    box-sizing: border-box;
  }

  .quick-add-dropdown.hidden,
  .user-menu-dropdown.hidden {
    display: none;
    pointer-events: none;
  }

  .quick-add-item,
  .user-menu-item {
    width: 100%;
    border: 0;
    background: transparent;
    padding: 8px 9px;
    text-align: left;
    border-radius: 9px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text);
    min-height: 44px;
    max-height: 52px;
    height: auto;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    line-height: 1.25;
    box-sizing: border-box;
  }

  .user-menu-item--refresh {
    color: var(--text);
  }

  .user-menu-item__ico {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    flex: 0 0 auto;
    color: #64748b;
    overflow: hidden;
  }

  .user-menu-item__ico-svg,
  .user-menu-item__ico svg {
    width: 16px !important;
    height: 16px !important;
    display: block;
    flex-shrink: 0;
  }

  .user-menu-item__label {
    flex: 1 1 auto;
    min-width: 0;
  }

  .user-menu-divider {
    height: 1px;
    margin: 4px 8px;
    background: #e2e8f0;
    flex: 0 0 auto;
  }

  .quick-add-item:hover,
  .user-menu-item:hover {
    background: #f1f5f9;
  }

  .user-menu-item.danger {
    color: #b91c1c;
  }

  .sync-status {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 999px;
    padding: 7px 10px;
    font-size: 12px;
    color: var(--text);
    background: rgba(255, 255, 255, 0.55);
  }

  .sync-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #22c55e;
  }

  .sync-saving .sync-dot {
    background: #f59e0b;
  }

  .sync-offline .sync-dot {
    background: #ef4444;
  }

  .user-avatar-btn {
    width: 36px;
    height: 36px;
    border-radius: 999px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    background: linear-gradient(120deg, #2563eb, #1d4ed8);
    color: #fff;
    font-weight: 700;
    cursor: pointer;
  }

  /* Command palette */
  .command-palette-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--z-overlay);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: min(14vh, 140px) 16px 24px;
    background: rgba(15, 23, 42, 0.35);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 120ms ease;
  }

  .command-palette-overlay.is-open {
    opacity: 1;
    pointer-events: auto;
  }

  .command-palette-modal {
    width: min(720px, 100%);
    border-radius: 22px;
    border: 1px solid rgba(148, 163, 184, 0.3);
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 26px 64px rgba(15, 23, 42, 0.22);
    overflow: hidden;
    transform: scale(0.98) translateY(6px);
    opacity: 0;
    transition: transform 120ms ease, opacity 120ms ease;
  }

  .command-palette-overlay.is-open .command-palette-modal {
    transform: scale(1) translateY(0);
    opacity: 1;
  }

  .command-palette-input {
    width: 100%;
    border: 0;
    border-bottom: 1px solid rgba(148, 163, 184, 0.22);
    background: transparent;
    font-size: 16px;
    padding: 16px 18px;
    outline: none;
    color: var(--text);
  }

  .command-palette-list {
    max-height: min(56vh, 460px);
    overflow-y: auto;
    padding: 8px;
  }

  .command-palette-item {
    width: 100%;
    border: 0;
    border-radius: 14px;
    background: transparent;
    display: grid;
    grid-template-columns: 24px 1fr auto;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    cursor: pointer;
    text-align: left;
  }

  .command-palette-item:hover,
  .command-palette-item.is-active {
    background: #eff6ff;
  }

  .command-palette-icon {
    font-size: 15px;
    line-height: 1;
  }

  .command-palette-title {
    font-size: 14px;
    color: #0f172a;
  }

  .command-palette-tag {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: #1d4ed8;
    background: #dbeafe;
    border-radius: 999px;
    padding: 2px 8px;
  }

  .command-palette-empty {
    color: #64748b;
    text-align: center;
    padding: 14px;
  }

  body.command-palette-open {
    overflow: hidden;
  }

  /* Route loading bar (app.js toggles .active) */
  .global-loading-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 2px;
    width: 100%;
    transform-origin: left;
    transform: scaleX(0);
    opacity: 0;
    z-index: var(--z-toast);
    background: linear-gradient(90deg, #3b82f6, #06b6d4);
    transition: transform 220ms ease, opacity 120ms ease;
  }

  .global-loading-bar.active {
    opacity: 1;
    transform: scaleX(0.88);
  }

  @media (max-width: 1100px) {
    .top-search-wrap {
      width: min(260px, 44vw);
    }
  }

  @media (max-width: 768px) {
    .topbar-extras .sync-status {
      display: none;
    }

    .top-search-wrap {
      width: 180px;
    }

    .command-palette-overlay {
      padding: 72px 10px 16px;
    }

    .command-palette-modal {
      width: 100%;
      border-radius: 18px;
    }
  }
}

/*
 * Topbar chrome — UNLAYERED (implicit final layer).
 * style.css is also unlayered and loads earlier; equal specificity → later file wins.
 * Higher specificity below beats stray legacy .btn / backdrop fades washing controls out.
 */
body.use-new-ui .app-layout header.topbar .btn.primary.quick-add-btn,
body.use-new-ui .app-layout header.topbar #topQuickAdd.btn.primary {
  background: linear-gradient(120deg, #3b82f6, #2563eb);
  color: #fff;
  border-color: transparent;
  opacity: 1;
  -webkit-text-fill-color: #fff;
  box-shadow: 0 8px 18px rgba(59, 130, 246, 0.24);
}

body.use-new-ui .app-layout header.topbar .btn.primary.quick-add-btn:hover:not(:disabled),
body.use-new-ui .app-layout header.topbar #topQuickAdd.btn.primary:hover:not(:disabled) {
  filter: brightness(1.04);
  box-shadow: 0 12px 26px rgba(59, 130, 246, 0.32);
}

body.use-new-ui .app-layout header.topbar .sync-status {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  color: #0f172a;
  opacity: 1;
  font-weight: 600;
}

body.use-new-ui .app-layout header.topbar .sync-status span:last-child {
  color: #0f172a;
}

body.use-new-ui .app-layout header.topbar .notification-bell {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  color: #1e293b;
  opacity: 1;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

body.use-new-ui .app-layout header.topbar .notification-bell-icon {
  opacity: 1;
  filter: none;
}

body.use-new-ui .app-layout header.topbar .notification-badge {
  opacity: 1;
  box-shadow: 0 0 0 2px #ffffff;
}

body.use-new-ui .app-layout header.topbar .user-avatar-btn {
  background: linear-gradient(120deg, #2563eb, #1d4ed8);
  color: #ffffff;
  border: 1px solid rgba(37, 99, 235, 0.55);
  opacity: 1;
  -webkit-text-fill-color: #ffffff;
}

body.use-new-ui .app-layout header.topbar .topbar-search.input,
body.use-new-ui .app-layout header.topbar input.topbar-search {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  color: #0f172a;
  opacity: 1;
  -webkit-text-fill-color: #0f172a;
}

body.use-new-ui .app-layout header.topbar .top-search-dropdown {
  background: #ffffff;
  border: 1px solid #cbd5e1;
}

body.use-new-ui .app-layout header.topbar .quick-add-dropdown,
body.use-new-ui .app-layout header.topbar .user-menu-dropdown {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  box-shadow: 0 16px 36px rgba(15, 23, 42, 0.18);
}

body.use-new-ui .app-layout header.topbar .quick-add-item,
body.use-new-ui .app-layout header.topbar .user-menu-item {
  color: #0f172a;
  opacity: 1;
}

body.use-new-ui .app-layout header.topbar .quick-add-item:hover,
body.use-new-ui .app-layout header.topbar .user-menu-item:hover {
  background: #eff6ff;
}

/* Dashboard toolbar chart toggles sit below the fixed header, not inside header.topbar */
body.use-new-ui .dashboard-layout .chart-toggle-group .btn.primary {
  background: linear-gradient(120deg, #3b82f6, #2563eb);
  color: #fff;
  border-color: transparent;
  -webkit-text-fill-color: #fff;
  opacity: 1;
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.22);
}

body.use-new-ui .dashboard-layout .chart-toggle-group .btn.secondary {
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  color: #1e293b;
  opacity: 1;
  -webkit-text-fill-color: #1e293b;
}
